From e8701e26959e9f73f79f82139335fb26940122c8 Mon Sep 17 00:00:00 2001 From: snoop Date: Tue, 3 Apr 2018 19:03:50 +0900 Subject: [PATCH] fixed discovery result - save infra but fixme mac address infra vendor --- .../commons/utils/StringConvertor.java | 16 ++ .../overflow/module/discovery/model/Host.java | 20 ++ .../overflow/module/discovery/model/Port.java | 10 + .../module/discovery/model/Service.java | 8 + .../module/meta/model/MetaInfraVendor.java | 4 +- .../service/TargetDiscoveryService.java | 259 +++++++++--------- .../service/TargetDiscoveryServiceTest.java | 33 +++ 7 files changed, 222 insertions(+), 128 deletions(-) diff --git a/src/main/java/com/loafle/overflow/commons/utils/StringConvertor.java b/src/main/java/com/loafle/overflow/commons/utils/StringConvertor.java index 42197f1..efbb7c0 100644 --- a/src/main/java/com/loafle/overflow/commons/utils/StringConvertor.java +++ b/src/main/java/com/loafle/overflow/commons/utils/StringConvertor.java @@ -12,4 +12,20 @@ public class StringConvertor { ( i & 0xFF); } + public static long ipToLong(String ipAddress) { + + String[] ipAddressInArray = ipAddress.split("\\."); + + long result = 0; + for (int i = 0; i < ipAddressInArray.length; i++) { + + int power = 3 - i; + int ip = Integer.parseInt(ipAddressInArray[i]); + result += ip * Math.pow(256, power); + + } + + return result; + } + } diff --git a/src/main/java/com/loafle/overflow/module/discovery/model/Host.java b/src/main/java/com/loafle/overflow/module/discovery/model/Host.java index 945a6e0..9218625 100644 --- a/src/main/java/com/loafle/overflow/module/discovery/model/Host.java +++ b/src/main/java/com/loafle/overflow/module/discovery/model/Host.java @@ -1,6 +1,7 @@ package com.loafle.overflow.module.discovery.model; import java.util.Date; +import java.util.Map; /** * Created by root on 17. 6. 4. @@ -12,9 +13,12 @@ public class Host { private String mac; private String os; private Date discoveredDate; + private boolean target; private Zone zone; + private Map ports; + public Host(){} public long getId() { @@ -64,4 +68,20 @@ public class Host { public void setZone(Zone zone) { this.zone = zone; } + + public boolean isTarget() { + return target; + } + + public void setTarget(boolean target) { + this.target = target; + } + + public Map getPorts() { + return ports; + } + + public void setPorts(Map ports) { + this.ports = ports; + } } diff --git a/src/main/java/com/loafle/overflow/module/discovery/model/Port.java b/src/main/java/com/loafle/overflow/module/discovery/model/Port.java index ada17f0..bf7fa86 100644 --- a/src/main/java/com/loafle/overflow/module/discovery/model/Port.java +++ b/src/main/java/com/loafle/overflow/module/discovery/model/Port.java @@ -8,6 +8,7 @@ package com.loafle.overflow.module.discovery.model; import com.loafle.overflow.module.discovery.type.PortType; import java.util.Date; +import java.util.Map; /** * Created by root on 17. 6. 4. @@ -23,6 +24,8 @@ public class Port { private Host host; + private Map services; + public Port() {} public long getId() { @@ -65,4 +68,11 @@ public class Port { this.host = host; } + public Map getServices() { + return services; + } + + public void setServices(Map services) { + this.services = services; + } } diff --git a/src/main/java/com/loafle/overflow/module/discovery/model/Service.java b/src/main/java/com/loafle/overflow/module/discovery/model/Service.java index f180bcd..a4213a2 100644 --- a/src/main/java/com/loafle/overflow/module/discovery/model/Service.java +++ b/src/main/java/com/loafle/overflow/module/discovery/model/Service.java @@ -14,6 +14,8 @@ public class Service { private Port port; + private boolean target; + public Service() {} public long getId() { @@ -56,5 +58,11 @@ public class Service { this.port = port; } + public boolean isTarget() { + return target; + } + public void setTarget(boolean target) { + this.target = target; + } } diff --git a/src/main/java/com/loafle/overflow/module/meta/model/MetaInfraVendor.java b/src/main/java/com/loafle/overflow/module/meta/model/MetaInfraVendor.java index b786c78..df2fa6f 100644 --- a/src/main/java/com/loafle/overflow/module/meta/model/MetaInfraVendor.java +++ b/src/main/java/com/loafle/overflow/module/meta/model/MetaInfraVendor.java @@ -60,7 +60,7 @@ public class MetaInfraVendor { MetaInfraVendor vendor = new MetaInfraVendor(); if(osName == null || osName.length() <= 0) { - vendor.setId(28); // FIXME: Unknown + vendor.setId(24); // FIXME: Unknown return vendor; } @@ -70,7 +70,7 @@ public class MetaInfraVendor { else if(osName.equals("Linux")) { vendor.setId(28); // ubuntu } else { - vendor.setId(28); // FIXME: Unknown + vendor.setId(24); // FIXME: Unknown } return vendor; 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 index 0895d71..800fd3c 100644 --- a/src/main/java/com/loafle/overflow/module/target/service/TargetDiscoveryService.java +++ b/src/main/java/com/loafle/overflow/module/target/service/TargetDiscoveryService.java @@ -44,177 +44,184 @@ public class TargetDiscoveryService { @Autowired private InfraServiceService infraServiceService; - // @Transactional - // public boolean saveAllTarget(List hosts, Probe probe) { + @Transactional + public boolean saveAllTarget(List hosts, Probe probe) { - // InfraHost infraHost = null; + InfraHost infraHost = null; - // for(Host host : hosts) { + for(Host host : hosts) { - // infraHost = this.createAndReadHost(host, probe); + infraHost = this.createAndReadHost(host, probe); - // this.createPort(infraHost, host, probe); + this.createPort(infraHost, host, probe); - // } - // return true; - // } + } + return true; + } - // private void createService(InfraHost infraHost, Port port, Probe probe) { + private void createService(InfraHost infraHost, Port port, Probe probe) { - // if(port.getServices() == null) { - // return; - // } + if(port.getServices() == null) { + return; + } - // MetaInfraType typeService = new MetaInfraType(); - // typeService.setId(7); + MetaInfraType typeService = new MetaInfraType(); + typeService.setId(7); - // String portType = "UDP"; + String portType = "UDP"; - // if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { - // portType = "TCP"; - // } + if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { + portType = "TCP"; + } - // for(com.loafle.overflow.module.discovery.model.Service service : port.getServices()) { + for(String key : port.getServices().keySet()) { - // InfraService dbInfraService = this.infraServiceService.readByService(infraHost.getId(), port.getPortNumber(), portType); + com.loafle.overflow.module.discovery.model.Service service = port.getServices().get(key); - // if(dbInfraService != null) { - // if(service.isTarget() && dbInfraService.getTarget() == null) { - // Target targetService = new Target(); - // targetService.setDisplayName(service.getServiceName() + "-Service"); - // this.targetService.regist(targetService); - // dbInfraService.setTarget(targetService); - // this.infraServiceService.regist(dbInfraService); - // } - // continue; - // } + InfraService dbInfraService = this.infraServiceService.readByService(infraHost.getId(), port.getPortNumber(), portType); - // InfraService infraService = new InfraService(); - // infraService.setHost(infraHost); - // infraService.setPort(port.getPortNumber()); - // infraService.setPortType(portType); - // infraService.setInfraType(typeService); - // infraService.setProbe(probe); + if(dbInfraService != null) { + if(service.isTarget() && dbInfraService.getTarget() == null) { + Target targetService = new Target(); + targetService.setDisplayName(service.getServiceName() + "-Service"); + this.targetService.regist(targetService); + dbInfraService.setTarget(targetService); + this.infraServiceService.regist(dbInfraService); + } + continue; + } - // if (port.getPortType() == PortType.TLS) { - // infraService.setTlsType(true); - // } - // infraService.setVendor(MetaInfraVendor.CreateInfraVendorByService(service.getServiceName())); + InfraService infraService = new InfraService(); + infraService.setHost(infraHost); + infraService.setPort(port.getPortNumber()); + infraService.setPortType(portType); + infraService.setInfraType(typeService); + infraService.setProbe(probe); - // if(service.isTarget()) { - // Target targetService = new Target(); - // targetService.setDisplayName(service.getServiceName() + "-Service"); - // this.targetService.regist(targetService); - // infraService.setTarget(targetService); - // } + if (port.getPortType() == PortType.TLS) { + infraService.setTlsType(true); + } + infraService.setVendor(MetaInfraVendor.CreateInfraVendorByService(service.getServiceName())); - // this.infraServiceService.regist(infraService); + if(service.isTarget()) { + Target targetService = new Target(); + targetService.setDisplayName(service.getServiceName() + "-Service"); + this.targetService.regist(targetService); + infraService.setTarget(targetService); + } - // } + this.infraServiceService.regist(infraService); - // } + } - // private void createPort(InfraHost infraHost, Host host, Probe probe) { + } - // if(host.getPorts() == null) { - // return; - // } + private void createPort(InfraHost infraHost, Host host, Probe probe) { - // String portType = "UDP"; + if(host.getPorts() == null) { + return; + } - // MetaInfraType typePort = new MetaInfraType(); - // typePort.setId(6); + String portType = "UDP"; - // InfraOS infraOS = infraHost.getOs(); + MetaInfraType typePort = new MetaInfraType(); + typePort.setId(6); - // for(Port port : host.getPorts()) { + InfraOS infraOS = infraHost.getOs(); - // if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { - // portType = "TCP"; - // } + for( String key: host.getPorts().keySet()) { - // InfraOSPort dbInfraOSPort = this.infraOSPortService.readByPort(infraOS.getId(), port.getPortNumber(), portType); - // if(dbInfraOSPort == null) { - // InfraOSPort infraOSPort = new InfraOSPort(); - // infraOSPort.setOs(infraOS); - // infraOSPort.setPort(port.getPortNumber()); - // infraOSPort.setPortType(portType); - // infraOSPort.setProbe(probe); - // infraOSPort.setInfraType(typePort); + Port port = host.getPorts().get(key); - // if (port.getPortType() == PortType.TLS) { - // infraOSPort.setTlsType(true); - // } - // infraOSPort.setVendor(MetaInfraVendor.CreateInfraVendorByPort(port.getPortNumber())); - // this.infraOSPortService.regist(infraOSPort); - // } + if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { + portType = "TCP"; + } - // this.createService(infraHost, port, probe); - // } - // } + InfraOSPort dbInfraOSPort = this.infraOSPortService.readByPort(infraOS.getId(), port.getPortNumber(), portType); + if(dbInfraOSPort == null) { + InfraOSPort infraOSPort = new InfraOSPort(); + infraOSPort.setOs(infraOS); + infraOSPort.setPort(port.getPortNumber()); + infraOSPort.setPortType(portType); + infraOSPort.setProbe(probe); + infraOSPort.setInfraType(typePort); - // private InfraHost createAndReadHost(Host host, Probe probe) { + if (port.getPortType() == PortType.TLS) { + infraOSPort.setTlsType(true); + } + infraOSPort.setVendor(MetaInfraVendor.CreateInfraVendorByPort(port.getPortNumber())); + this.infraOSPortService.regist(infraOSPort); + } - // InfraHost infraHost = this.infraHostService.readByIp(host.getIp()); - // if(infraHost != null) { + this.createService(infraHost, port, probe); + } + } - // if(host.isTarget() && infraHost.getTarget() == null) { - // Target target = new Target(); - // target.setDisplayName(String.valueOf(host.getIp()) + "-Host"); + private InfraHost createAndReadHost(Host host, Probe probe) { - // this.targetService.regist(target); - // infraHost.setTarget(target); - // this.infraHostService.regist(infraHost); - // } - // return infraHost; - // } else { - // MetaInfraType typeMachine = new MetaInfraType(); - // typeMachine.setId(1); // 1 = Machine; + InfraHost infraHost = this.infraHostService.readByIp(StringConvertor.ipToLong(host.getIp())); + if(infraHost != null) { - // MetaInfraType typeOS = new MetaInfraType(); - // typeOS.setId(3); // 3 = Os + if(host.isTarget() && infraHost.getTarget() == null) { + Target target = new Target(); + target.setDisplayName(String.valueOf(host.getIp()) + "-Host"); - // MetaInfraType typeHost = new MetaInfraType(); - // typeHost.setId(2); // 2 = Host + this.targetService.regist(target); + infraHost.setTarget(target); + this.infraHostService.regist(infraHost); + } - // InfraMachine infraMachine = new InfraMachine(); - // infraMachine.setProbe(probe); - // infraMachine.setInfraType(typeMachine); - // infraMachine.setMeta(StringConvertor.intToIp(host.getIp())+"-MACHINE"); - // this.infraMachineService.regist(infraMachine); + return infraHost; + } else { + MetaInfraType typeMachine = new MetaInfraType(); + typeMachine.setId(1); // 1 = Machine; - // InfraOS infraOS = new InfraOS(); - // infraOS.setMachine(infraMachine); - // infraOS.setVendor(MetaInfraVendor.CreateInfraVendorByOS(host.getOs())); - // infraOS.setInfraType(typeOS); - // infraOS.setProbe(probe); - // infraOS.setMeta(StringConvertor.intToIp(host.getIp())+"-OS"); - // this.infraOSService.regist(infraOS); + MetaInfraType typeOS = new MetaInfraType(); + typeOS.setId(3); // 3 = Os - // InfraHost newInfraHost = new InfraHost(); - // newInfraHost.setIp(host.getIp()); - // newInfraHost.setMac(host.getMac()); - // newInfraHost.setOs(infraOS); - // newInfraHost.setInfraType(typeHost); - // newInfraHost.setProbe(probe); + MetaInfraType typeHost = new MetaInfraType(); + typeHost.setId(2); // 2 = Host - // if(host.isTarget()) { - // Target target = new Target(); - // target.setDisplayName(StringConvertor.intToIp(host.getIp()) + "-Host"); + InfraMachine infraMachine = new InfraMachine(); + infraMachine.setProbe(probe); + infraMachine.setInfraType(typeMachine); + infraMachine.setMeta(host.getIp()+"-MACHINE"); + this.infraMachineService.regist(infraMachine); - // this.targetService.regist(target); - // newInfraHost.setTarget(target); - // } + InfraOS infraOS = new InfraOS(); + infraOS.setMachine(infraMachine); + infraOS.setVendor(MetaInfraVendor.CreateInfraVendorByOS(host.getOs())); + infraOS.setInfraType(typeOS); + infraOS.setProbe(probe); + infraOS.setMeta(host.getIp()+"-OS"); + this.infraOSService.regist(infraOS); - // this.infraHostService.regist(newInfraHost); - // infraHost = newInfraHost; - // } + InfraHost newInfraHost = new InfraHost(); + newInfraHost.setIp(StringConvertor.ipToLong(host.getIp())); +// newInfraHost.setMac(StringConvertor.ipToLong(host.getMac())); + newInfraHost.setMac(123123123); + newInfraHost.setOs(infraOS); + newInfraHost.setInfraType(typeHost); + newInfraHost.setProbe(probe); + + + if(host.isTarget()) { + Target target = new Target(); + target.setDisplayName(host.getIp() + "-Host"); + + this.targetService.regist(target); + newInfraHost.setTarget(target); + } + + this.infraHostService.regist(newInfraHost); + infraHost = newInfraHost; + } - // return infraHost; - // } + return infraHost; + } 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 index ba27202..bda3c3c 100644 --- a/src/test/java/com/loafle/overflow/module/target/service/TargetDiscoveryServiceTest.java +++ b/src/test/java/com/loafle/overflow/module/target/service/TargetDiscoveryServiceTest.java @@ -1,5 +1,6 @@ package com.loafle.overflow.module.target.service; +import com.loafle.overflow.commons.utils.StringConvertor; import com.loafle.overflow.module.discovery.model.Host; import com.loafle.overflow.module.probe.model.Probe; import com.loafle.overflow.spring.AppConfigTest; @@ -54,6 +55,38 @@ public class TargetDiscoveryServiceTest { // this.targetDiscoveryService.saveAllTarget(hosts, probe); } + @Test + public void saveAllTartget2() throws Exception { + + String json = readFileAsString(resourceLoader.getResource("classpath:180403-td.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); + + +// System.out.println(hosts.size()); + + this.targetDiscoveryService.saveAllTarget(hosts, probe); + + } + + @Test + public void testConvert() { + + String ip = "192.168.1.106"; + + long aaa = StringConvertor.ipToLong(ip); + + System.out.println(aaa); + + } + private String readFileAsString(String filePath) throws IOException { StringBuffer fileData = new StringBuffer(); BufferedReader reader = new BufferedReader(