From 1e8385775631613e0bbd14e13c521cb47c522bac Mon Sep 17 00:00:00 2001 From: snoop Date: Wed, 28 Jun 2017 16:19:52 +0900 Subject: [PATCH] added service target discovery --- .../service/TargetDiscoveryService.java | 142 ++++++++++++++ .../service/TargetDiscoveryServiceTest.java | 67 +++++++ src/test/resources/dh.json | 173 ++++++++++++++++++ 3 files changed, 382 insertions(+) create mode 100644 src/main/java/com/loafle/overflow/module/target/service/TargetDiscoveryService.java create mode 100644 src/test/java/com/loafle/overflow/module/target/service/TargetDiscoveryServiceTest.java create mode 100644 src/test/resources/dh.json diff --git a/src/main/java/com/loafle/overflow/module/target/service/TargetDiscoveryService.java b/src/main/java/com/loafle/overflow/module/target/service/TargetDiscoveryService.java new file mode 100644 index 0000000..af19b68 --- /dev/null +++ b/src/main/java/com/loafle/overflow/module/target/service/TargetDiscoveryService.java @@ -0,0 +1,142 @@ +package com.loafle.overflow.module.target.service; + +import com.loafle.overflow.module.discovery.model.Host; +import com.loafle.overflow.module.discovery.model.Port; +import com.loafle.overflow.module.discovery.type.PortType; +import com.loafle.overflow.module.infra.dao.*; +import com.loafle.overflow.module.infra.model.*; +import com.loafle.overflow.module.meta.model.MetaInfraVendor; +import com.loafle.overflow.module.probe.model.Probe; +import com.loafle.overflow.module.target.dao.TargetDAO; +import com.loafle.overflow.module.target.model.Target; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.transaction.Transactional; +import java.util.List; + +/** + * Created by snoop on 17. 6. 28. + */ +@Service +public class TargetDiscoveryService { + + @Autowired + private TargetDAO targetDAO; + + @Autowired + private InfraMachineDAO infraMachineDAO; + + @Autowired + private InfraOSDAO infraOSDAO; + + @Autowired + private InfraHostDAO infraHostDAO; + + @Autowired + private InfraDAO infraDAO; + + @Autowired + private InfraOSPortDAO infraOSPortDAO; + + @Autowired + private InfraServiceDAO infraServiceDAO; + + @Transactional + public void saveAllTarget(List hosts, Probe probe) { + + for(Host host : hosts) { + + InfraMachine infraMachine = new InfraMachine(); + infraMachine.setProbe(probe); + this.infraMachineDAO.save(infraMachine); + + Infra infraByMachine = Infra.CreateInfraByType(infraMachine.getId(), InfraMachine.class); + this.infraDAO.save(infraByMachine); + + + InfraOS infraOS = new InfraOS(); + infraOS.setMachine(infraMachine); + infraOS.setVendor(MetaInfraVendor.CreateInfraVendorByOS(host.getOs())); + this.infraOSDAO.save(infraOS); + + Infra infraByOS = Infra.CreateInfraByType(infraOS.getId(), InfraOS.class); + this.infraDAO.save(infraByOS); + + + InfraHost infraHost = new InfraHost(); + infraHost.setIp(host.getIp()); + infraHost.setMac(host.getMac()); + infraHost.setOs(infraOS); + this.infraHostDAO.save(infraHost); + + Infra infraByHost = Infra.CreateInfraByType(infraHost.getId(), InfraHost.class); + this.infraDAO.save(infraByHost); + + if(host.isTarget()) { + Target targetHost = new Target(); + targetHost.setInfra(infraByHost); + targetHost.setProbe(probe); + this.targetDAO.save(targetHost); + } + + if(host.getPorts() == null) { + continue; + } + + for(Port port : host.getPorts()) { + + InfraOSPort infraOSPort = new InfraOSPort(); + infraOSPort.setOs(infraOS); + infraOSPort.setPort(port.getPortNumber()); + infraOSPort.setPortType("UDP"); + if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { + infraOSPort.setPortType("TCP"); + } + if (port.getPortType() == PortType.TLS) { + infraOSPort.setTlsType(true); + } + infraOSPort.setVendor(MetaInfraVendor.CreateInfraVendorByPort(port.getPortNumber())); + this.infraOSPortDAO.save(infraOSPort); + + Infra infraByPort = Infra.CreateInfraByType(infraOSPort.getId(), InfraOSPort.class); + this.infraDAO.save(infraByPort); + + if(port.getServices() == null) { + continue; + } + + for(com.loafle.overflow.module.discovery.model.Service service : port.getServices()) { + + InfraService infraService = new InfraService(); + infraService.setHost(infraHost); + infraService.setPort(port.getPortNumber()); + infraService.setPortType("UDP"); + if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { + infraService.setPortType("TCP"); + } + if (port.getPortType() == PortType.TLS) { + infraService.setTlsType(true); + } + infraService.setVendor(MetaInfraVendor.CreateInfraVendorByService(service.getServiceName())); + this.infraServiceDAO.save(infraService); + + Infra infraByService = Infra.CreateInfraByType(infraService.getId(), InfraService.class); + this.infraDAO.save(infraByService); + + if(service.isTarget()) { + Target targetService = new Target(); + targetService.setInfra(infraByService); + targetService.setProbe(probe); + this.targetDAO.save(targetService); + } + + } + + } + + + } + + } +} diff --git a/src/test/java/com/loafle/overflow/module/target/service/TargetDiscoveryServiceTest.java b/src/test/java/com/loafle/overflow/module/target/service/TargetDiscoveryServiceTest.java new file mode 100644 index 0000000..9389e4a --- /dev/null +++ b/src/test/java/com/loafle/overflow/module/target/service/TargetDiscoveryServiceTest.java @@ -0,0 +1,67 @@ +package com.loafle.overflow.module.target.service; + +import com.loafle.overflow.module.discovery.model.Host; +import com.loafle.overflow.module.probe.model.Probe; +import com.loafle.overflow.spring.AppConfig; +import com.loafle.overflow.spring.JdbcConfiguration; +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ResourceLoader; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.List; + +import static org.junit.Assert.*; + +/** + * Created by snoop on 17. 6. 28. + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = {AppConfig.class, JdbcConfiguration.class}) +public class TargetDiscoveryServiceTest { + + @Autowired + private ResourceLoader resourceLoader; + + @Autowired + private TargetDiscoveryService targetDiscoveryService; + + @Test + public void saveAllTarget() throws Exception { + + String json = readFileAsString(resourceLoader.getResource("classpath:dh.json").getURI().getPath()); + + ObjectMapper mapper = new ObjectMapper(); + + mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + + List hosts = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, Host.class)); + + Probe probe = new Probe(); + probe.setId(1); + + this.targetDiscoveryService.saveAllTarget(hosts, probe); + } + + private String readFileAsString(String filePath) throws IOException { + StringBuffer fileData = new StringBuffer(); + BufferedReader reader = new BufferedReader( + new FileReader(filePath)); + char[] buf = new char[1024]; + int numRead=0; + while((numRead=reader.read(buf)) != -1){ + String readData = String.valueOf(buf, 0, numRead); + fileData.append(readData); + } + reader.close(); + return fileData.toString(); + } + +} \ No newline at end of file diff --git a/src/test/resources/dh.json b/src/test/resources/dh.json new file mode 100644 index 0000000..c7bb126 --- /dev/null +++ b/src/test/resources/dh.json @@ -0,0 +1,173 @@ +[{ + "firstScanRange": 1, + "lastScanRange": 10000, + "name": "", + "ip": 3232235818, + "mac": 91754662925, + "os":"Windows", + "ports": [{ + "createDate": -62135596800000, + "updateDate": -62135596800000, + "services": [{ + "createDate": -62135596800000, + "updateDate": -62135596800000, + "portType": "TCP", + "serviceName": "SSH", + "target":true + }], + "portType": "TCP", + "portNumber": 22 + }, + { + "createDate": -62135596800000, + "updateDate": -62135596800000, + "services": [{ + "createDate": -62135596800000, + "updateDate": -62135596800000, + "portType": "TCP", + "serviceName": "HTTP" + }], + "portType": "TCP", + "portNumber": 443 + }, + { + "createDate": -62135596800000, + "updateDate": -62135596800000, + "services": [{ + "createDate": -62135596800000, + "updateDate": -62135596800000, + "portType": "TCP", + "serviceName": "HTTP" + }], + "portType": "TCP", + "portNumber": 80 + }], + "createDate": 1498470178000, + "updateDate": 1498470178000 +}, + { + "firstScanRange": 1, + "lastScanRange": 10000, + "name": "", + "ip": 3232235781, + "mac": 91754660625, + "os":"Windows", + "ports": [{ + "createDate": -62135596800000, + "updateDate": -62135596800000, + "services": [{ + "createDate": -62135596800000, + "updateDate": -62135596800000, + "portType": "TCP", + "serviceName": "SSH" + }], + "portType": "TCP", + "portNumber": 22 + }, + { + "createDate": -62135596800000, + "updateDate": -62135596800000, + "services": [{ + "createDate": -62135596800000, + "updateDate": -62135596800000, + "portType": "TCP", + "serviceName": "HTTP" + }], + "portType": "TCP", + "portNumber": 80 + }, + { + "createDate": -62135596800000, + "updateDate": -62135596800000, + "services": [{ + "createDate": -62135596800000, + "updateDate": -62135596800000, + "portType": "TCP", + "serviceName": "HTTP" + }], + "portType": "TCP", + "portNumber": 1936 + }, + { + "createDate": -62135596800000, + "updateDate": -62135596800000, + "services": null, + "portType": "TCP", + "portNumber": 443 + }], + "createDate": 1498470178000, + "updateDate": 1498470178000 + }, + { + "firstScanRange": 1, + "lastScanRange": 10000, + "name": "", + "ip": 3232235797, + "mac": 91754662913, + "os":"Windows", + "target":true, + "ports": [{ + "createDate": -62135596800000, + "updateDate": -62135596800000, + "services": [{ + "createDate": -62135596800000, + "updateDate": -62135596800000, + "portType": "TCP", + "serviceName": "HTTP" + }], + "portType": "TCP", + "portNumber": 80 + }, + { + "createDate": -62135596800000, + "updateDate": -62135596800000, + "services": [{ + "createDate": -62135596800000, + "updateDate": -62135596800000, + "portType": "TCP", + "serviceName": "SSH" + }], + "portType": "TCP", + "portNumber": 22 + }, + { + "createDate": -62135596800000, + "updateDate": -62135596800000, + "services": [{ + "createDate": -62135596800000, + "updateDate": -62135596800000, + "portType": "TCP", + "serviceName": "HTTP" + }], + "portType": "TCP", + "portNumber": 3343 + }, + { + "createDate": -62135596800000, + "updateDate": -62135596800000, + "services": [{ + "createDate": -62135596800000, + "updateDate": -62135596800000, + "portType": "TCP", + "serviceName": "HTTP" + }], + "portType": "TCP", + "portNumber": 443 + }], + "createDate": 1498470178000, + "updateDate": 1498470178000 + }, + { + "firstScanRange": 1, + "lastScanRange": 10000, + "name": "", + "ip": 3232235877, + "mac": 75361038758387, + "os":"Windows", + "ports": null, + "createDate": 1498470179000, + "updateDate": 1498470179000 + } + + +] \ No newline at end of file