target create
This commit is contained in:
snoop 2017-06-27 18:50:05 +09:00
parent 5e4c270fe2
commit 845076056d
7 changed files with 492 additions and 32 deletions

View File

@ -43,15 +43,6 @@ public class MetaInfraVendor {
this.createDate = createDate;
}
// @Basic
// @Column(name = "TYPE_ID", nullable = false)
// public int getTypeId() {
// return typeId;
// }
//
// public void setTypeId(int typeId) {
// this.typeId = typeId;
// }
@ManyToOne
@JoinColumn(name = "TYPE_ID", nullable=false)
@ -64,27 +55,46 @@ public class MetaInfraVendor {
}
// @Override
// public boolean equals(Object o) {
// if (this == o) return true;
// if (o == null || getClass() != o.getClass()) return false;
//
// MetaInfraVendor that = (MetaInfraVendor) o;
//
// if (id != that.id) return false;
// if (typeId != that.typeId) return false;
// if (name != null ? !name.equals(that.name) : that.name != null) return false;
// if (createDate != null ? !createDate.equals(that.createDate) : that.createDate != null) return false;
//
// return true;
// }
//
// @Override
// public int hashCode() {
// int result = id;
// result = 31 * result + (name != null ? name.hashCode() : 0);
// result = 31 * result + (createDate != null ? createDate.hashCode() : 0);
// result = 31 * result + typeId;
// return result;
// }
public static MetaInfraVendor CreateInfraVendorByOS(String osName) {
MetaInfraVendor vendor = new MetaInfraVendor();
if(osName.equals("Windows")) {
vendor.setId(25);
}
else if(osName.equals("Linux")) {
vendor.setId(27); // ubuntu
}
return vendor;
}
public static MetaInfraVendor CreateInfraVendorByPort(int portNumber) {
return null;
}
public static MetaInfraVendor CreateInfraVendorByService(String serviceName) {
MetaInfraVendor vendor = new MetaInfraVendor();
if(serviceName.equals("mysql")) {
vendor.setId(39);
}
else if(serviceName.equals("portgresql")) {
vendor.setId(39);
}
else if(serviceName.equals("wmi")) {
vendor.setId(39);
}
else if(serviceName.equals("snmpv2")) {
vendor.setId(39);
}
else {
vendor.setId(43); // unknown
}
return vendor;
}
}

View File

@ -36,6 +36,8 @@ public class Service {
private Date updateDate;
private boolean target;
public Service() {}
public Service(Port port,PortType t, String serviceName) {
@ -84,4 +86,12 @@ public class Service {
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public boolean isTarget() {
return target;
}
public void setTarget(boolean target) {
this.target = target;
}
}

View File

@ -56,4 +56,38 @@ public class Infra {
this.createDate = createDate;
}
public static Infra CreateInfraByType(long id, Class c) {
Infra infra = new Infra();
infra.setChildId(id);
MetaInfraType type = new MetaInfraType();
if(c == InfraMachine.class) {
type.setId(1);
}
else if(c == InfraHost.class) {
type.setId(2);
}
else if(c == InfraOS.class) {
type.setId(3);
}
else if(c == InfraOSApplication.class) {
type.setId(4);
}
else if(c == InfraOSDaemon.class) {
type.setId(5);
}
else if(c == InfraOSPort.class) {
type.setId(6);
}
else if(c == InfraService.class) {
type.setId(7);
}
infra.setType(type);
return infra;
}
}

View File

@ -1,8 +1,10 @@
package com.loafle.overflow.module.target.dao;
import com.loafle.overflow.module.discovery.model.Host;
import com.loafle.overflow.module.probe.model.Probe;
import com.loafle.overflow.module.target.model.Target;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@ -10,6 +12,7 @@ import java.util.List;
/**
* Created by root on 17. 6. 5.
*/
@Repository
public interface TargetDAO extends JpaRepository<Target, Long> {
List<Target> findAllByProbe(Probe probe);
}

View File

@ -0,0 +1,143 @@
package com.loafle.overflow.module.target.service;
import com.loafle.overflow.meta.model.MetaInfraVendor;
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.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. 27.
*/
@Service
public class TargetService {
@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<Host> 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);
}
}
}
}
}
}

View File

@ -0,0 +1,87 @@
package com.loafle.overflow.module.target.service;
import com.loafle.overflow.AppConfig;
import com.loafle.overflow.JdbcConfiguration;
import com.loafle.overflow.module.discovery.model.Host;
import com.loafle.overflow.module.infra.model.Infra;
import com.loafle.overflow.module.infra.model.InfraService;
import com.loafle.overflow.module.probe.model.Probe;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.util.JSONPObject;
import org.codehaus.jackson.type.TypeReference;
import org.junit.Assert;
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. 27.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class, JdbcConfiguration.class})
public class TargetServiceTest {
@Autowired
private TargetService targetService;
@Autowired
private ResourceLoader resourceLoader;
@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<Host> hosts = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, Host.class));
Probe probe = new Probe();
probe.setId(1);
this.targetService.saveAllTarget(hosts, probe);
// Assert.assertNotEquals(this.targetService, null);
}
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();
}
@Test
public void testClassInfo() {
Class c = InfraService.class;
if(c == InfraService.class) {
System.out.println("??");
System.out.println(c.toString());
}
}
}

173
src/test/resources/dh.json Normal file
View File

@ -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
}
]