fixed discovery result - save infra

but
 fixme
   mac address
   infra vendor
This commit is contained in:
snoop 2018-04-03 19:03:50 +09:00
parent 396ab8c3ff
commit e8701e2695
7 changed files with 222 additions and 128 deletions

View File

@ -12,4 +12,20 @@ public class StringConvertor {
( i & 0xFF); ( 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;
}
} }

View File

@ -1,6 +1,7 @@
package com.loafle.overflow.module.discovery.model; package com.loafle.overflow.module.discovery.model;
import java.util.Date; import java.util.Date;
import java.util.Map;
/** /**
* Created by root on 17. 6. 4. * Created by root on 17. 6. 4.
@ -12,9 +13,12 @@ public class Host {
private String mac; private String mac;
private String os; private String os;
private Date discoveredDate; private Date discoveredDate;
private boolean target;
private Zone zone; private Zone zone;
private Map<String, Port> ports;
public Host(){} public Host(){}
public long getId() { public long getId() {
@ -64,4 +68,20 @@ public class Host {
public void setZone(Zone zone) { public void setZone(Zone zone) {
this.zone = zone; this.zone = zone;
} }
public boolean isTarget() {
return target;
}
public void setTarget(boolean target) {
this.target = target;
}
public Map<String, Port> getPorts() {
return ports;
}
public void setPorts(Map<String, Port> ports) {
this.ports = ports;
}
} }

View File

@ -8,6 +8,7 @@ package com.loafle.overflow.module.discovery.model;
import com.loafle.overflow.module.discovery.type.PortType; import com.loafle.overflow.module.discovery.type.PortType;
import java.util.Date; import java.util.Date;
import java.util.Map;
/** /**
* Created by root on 17. 6. 4. * Created by root on 17. 6. 4.
@ -23,6 +24,8 @@ public class Port {
private Host host; private Host host;
private Map<String, Service> services;
public Port() {} public Port() {}
public long getId() { public long getId() {
@ -65,4 +68,11 @@ public class Port {
this.host = host; this.host = host;
} }
public Map<String, Service> getServices() {
return services;
}
public void setServices(Map<String, Service> services) {
this.services = services;
}
} }

View File

@ -14,6 +14,8 @@ public class Service {
private Port port; private Port port;
private boolean target;
public Service() {} public Service() {}
public long getId() { public long getId() {
@ -56,5 +58,11 @@ public class Service {
this.port = port; this.port = port;
} }
public boolean isTarget() {
return target;
}
public void setTarget(boolean target) {
this.target = target;
}
} }

View File

@ -60,7 +60,7 @@ public class MetaInfraVendor {
MetaInfraVendor vendor = new MetaInfraVendor(); MetaInfraVendor vendor = new MetaInfraVendor();
if(osName == null || osName.length() <= 0) { if(osName == null || osName.length() <= 0) {
vendor.setId(28); // FIXME: Unknown vendor.setId(24); // FIXME: Unknown
return vendor; return vendor;
} }
@ -70,7 +70,7 @@ public class MetaInfraVendor {
else if(osName.equals("Linux")) { else if(osName.equals("Linux")) {
vendor.setId(28); // ubuntu vendor.setId(28); // ubuntu
} else { } else {
vendor.setId(28); // FIXME: Unknown vendor.setId(24); // FIXME: Unknown
} }
return vendor; return vendor;

View File

@ -44,177 +44,184 @@ public class TargetDiscoveryService {
@Autowired @Autowired
private InfraServiceService infraServiceService; private InfraServiceService infraServiceService;
// @Transactional @Transactional
// public boolean saveAllTarget(List<Host> hosts, Probe probe) { public boolean saveAllTarget(List<Host> 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) { if(port.getServices() == null) {
// return; return;
// } }
// MetaInfraType typeService = new MetaInfraType(); MetaInfraType typeService = new MetaInfraType();
// typeService.setId(7); typeService.setId(7);
// String portType = "UDP"; String portType = "UDP";
// if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) {
// 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) { InfraService dbInfraService = this.infraServiceService.readByService(infraHost.getId(), port.getPortNumber(), portType);
// 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 infraService = new InfraService(); if(dbInfraService != null) {
// infraService.setHost(infraHost); if(service.isTarget() && dbInfraService.getTarget() == null) {
// infraService.setPort(port.getPortNumber()); Target targetService = new Target();
// infraService.setPortType(portType); targetService.setDisplayName(service.getServiceName() + "-Service");
// infraService.setInfraType(typeService); this.targetService.regist(targetService);
// infraService.setProbe(probe); dbInfraService.setTarget(targetService);
this.infraServiceService.regist(dbInfraService);
}
continue;
}
// if (port.getPortType() == PortType.TLS) { InfraService infraService = new InfraService();
// infraService.setTlsType(true); infraService.setHost(infraHost);
// } infraService.setPort(port.getPortNumber());
// infraService.setVendor(MetaInfraVendor.CreateInfraVendorByService(service.getServiceName())); infraService.setPortType(portType);
infraService.setInfraType(typeService);
infraService.setProbe(probe);
// if(service.isTarget()) { if (port.getPortType() == PortType.TLS) {
// Target targetService = new Target(); infraService.setTlsType(true);
// targetService.setDisplayName(service.getServiceName() + "-Service"); }
// this.targetService.regist(targetService); infraService.setVendor(MetaInfraVendor.CreateInfraVendorByService(service.getServiceName()));
// infraService.setTarget(targetService);
// }
// 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) { private void createPort(InfraHost infraHost, Host host, Probe probe) {
// return;
// }
// String portType = "UDP"; if(host.getPorts() == null) {
return;
}
// MetaInfraType typePort = new MetaInfraType(); String portType = "UDP";
// typePort.setId(6);
// 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) { for( String key: host.getPorts().keySet()) {
// portType = "TCP";
// }
// InfraOSPort dbInfraOSPort = this.infraOSPortService.readByPort(infraOS.getId(), port.getPortNumber(), portType); Port port = host.getPorts().get(key);
// if(dbInfraOSPort == null) {
// InfraOSPort infraOSPort = new InfraOSPort();
// infraOSPort.setOs(infraOS);
// infraOSPort.setPort(port.getPortNumber());
// infraOSPort.setPortType(portType);
// infraOSPort.setProbe(probe);
// infraOSPort.setInfraType(typePort);
// if (port.getPortType() == PortType.TLS) { if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) {
// infraOSPort.setTlsType(true); portType = "TCP";
// } }
// infraOSPort.setVendor(MetaInfraVendor.CreateInfraVendorByPort(port.getPortNumber()));
// this.infraOSPortService.regist(infraOSPort);
// }
// 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()); this.createService(infraHost, port, probe);
// if(infraHost != null) { }
}
// if(host.isTarget() && infraHost.getTarget() == null) { private InfraHost createAndReadHost(Host host, Probe probe) {
// Target target = new Target();
// target.setDisplayName(String.valueOf(host.getIp()) + "-Host");
// this.targetService.regist(target);
// infraHost.setTarget(target);
// this.infraHostService.regist(infraHost);
// }
// return infraHost; InfraHost infraHost = this.infraHostService.readByIp(StringConvertor.ipToLong(host.getIp()));
// } else { if(infraHost != null) {
// MetaInfraType typeMachine = new MetaInfraType();
// typeMachine.setId(1); // 1 = Machine;
// MetaInfraType typeOS = new MetaInfraType(); if(host.isTarget() && infraHost.getTarget() == null) {
// typeOS.setId(3); // 3 = Os Target target = new Target();
target.setDisplayName(String.valueOf(host.getIp()) + "-Host");
// MetaInfraType typeHost = new MetaInfraType(); this.targetService.regist(target);
// typeHost.setId(2); // 2 = Host infraHost.setTarget(target);
this.infraHostService.regist(infraHost);
}
// InfraMachine infraMachine = new InfraMachine(); return infraHost;
// infraMachine.setProbe(probe); } else {
// infraMachine.setInfraType(typeMachine); MetaInfraType typeMachine = new MetaInfraType();
// infraMachine.setMeta(StringConvertor.intToIp(host.getIp())+"-MACHINE"); typeMachine.setId(1); // 1 = Machine;
// this.infraMachineService.regist(infraMachine);
// InfraOS infraOS = new InfraOS(); MetaInfraType typeOS = new MetaInfraType();
// infraOS.setMachine(infraMachine); typeOS.setId(3); // 3 = Os
// infraOS.setVendor(MetaInfraVendor.CreateInfraVendorByOS(host.getOs()));
// infraOS.setInfraType(typeOS);
// infraOS.setProbe(probe);
// infraOS.setMeta(StringConvertor.intToIp(host.getIp())+"-OS");
// this.infraOSService.regist(infraOS);
// InfraHost newInfraHost = new InfraHost(); MetaInfraType typeHost = new MetaInfraType();
// newInfraHost.setIp(host.getIp()); typeHost.setId(2); // 2 = Host
// newInfraHost.setMac(host.getMac());
// newInfraHost.setOs(infraOS);
// newInfraHost.setInfraType(typeHost);
// newInfraHost.setProbe(probe);
// if(host.isTarget()) { InfraMachine infraMachine = new InfraMachine();
// Target target = new Target(); infraMachine.setProbe(probe);
// target.setDisplayName(StringConvertor.intToIp(host.getIp()) + "-Host"); infraMachine.setInfraType(typeMachine);
infraMachine.setMeta(host.getIp()+"-MACHINE");
this.infraMachineService.regist(infraMachine);
// this.targetService.regist(target); InfraOS infraOS = new InfraOS();
// newInfraHost.setTarget(target); 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 = new InfraHost();
// infraHost = newInfraHost; 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;
// } }

View File

@ -1,5 +1,6 @@
package com.loafle.overflow.module.target.service; 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.discovery.model.Host;
import com.loafle.overflow.module.probe.model.Probe; import com.loafle.overflow.module.probe.model.Probe;
import com.loafle.overflow.spring.AppConfigTest; import com.loafle.overflow.spring.AppConfigTest;
@ -54,6 +55,38 @@ public class TargetDiscoveryServiceTest {
// this.targetDiscoveryService.saveAllTarget(hosts, probe); // 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<Host> 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 { private String readFileAsString(String filePath) throws IOException {
StringBuffer fileData = new StringBuffer(); StringBuffer fileData = new StringBuffer();
BufferedReader reader = new BufferedReader( BufferedReader reader = new BufferedReader(