From 0df9f8c75feac9f0ea5dd670b8476f8ccc43c10e Mon Sep 17 00:00:00 2001 From: snoop Date: Tue, 27 Jun 2017 16:11:09 +0900 Subject: [PATCH] created discovery target --- dh.json | 173 ++++++++++++++++++++++++++++ proxy/infra/infra_service.go | 67 ++++++++--- proxy/meta/meta_infra_vendor.go | 42 ++++--- proxy/target/target_service.go | 156 ++++++++++++++++++++++--- proxy/target/target_service_test.go | 43 +++++++ 5 files changed, 434 insertions(+), 47 deletions(-) create mode 100644 dh.json diff --git a/dh.json b/dh.json new file mode 100644 index 0000000..8890e28 --- /dev/null +++ b/dh.json @@ -0,0 +1,173 @@ +{"hosts": [{ +"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 diff --git a/proxy/infra/infra_service.go b/proxy/infra/infra_service.go index 6ef9fae..4910379 100644 --- a/proxy/infra/infra_service.go +++ b/proxy/infra/infra_service.go @@ -6,6 +6,7 @@ import ( "git.loafle.net/overflow/overflow_service/proxy/meta" "git.loafle.net/overflow/overflow_service/proxy/probe" "git.loafle.net/overflow/overflow_service/proxy/utils" + "reflect" ) type InfraService struct { @@ -13,21 +14,53 @@ type InfraService struct { type Infra struct { Id json.Number `json:"id,Number,omitempty"` - MetaInfraType meta.MetaInfraType `json:"type,omitempty"` + MetaInfraType *meta.MetaInfraType `json:"type,omitempty"` ChildId json.Number `json:"childId,omitempty"` CreateDate timestamp.Timestamp `json:"createDate,omitempty"` } +func NewInfra(id json.Number, o interface{}) *Infra { + + ty := reflect.TypeOf(o) + + var mit meta.MetaInfraType + + strType := ty.String() + if strType == "*infra.InfraMachine" { + mit.Id = "1" + } else if strType == "*infra.InfraHost" { + mit.Id = "2" + } else if strType == "*infra.InfraOS" { + mit.Id = "3" + } else if strType == "*infra.InfraOSApplication" { + mit.Id = "4" + } else if strType == "*infra.InfraOSDaemon" { + mit.Id = "5" + } else if strType == "*infra.InfraOSPort" { + mit.Id = "6" + } else if strType == "*infra.InfraServiceApplication" { + mit.Id = "7" + } else { + return nil + } + + + return &Infra{ + MetaInfraType:&mit, + ChildId:id, + } +} + type InfraMachine struct { Id json.Number `json:"id,Number,omitempty"` - Probe probe.Probe `json:"probe,omitempty"` + Probe *probe.Probe `json:"probe,omitempty"` Meta string `json:"meta,omitempty"` CreateDate timestamp.Timestamp `json:"createDate,omitempty"` } type InfraHost struct { Id json.Number `json:"id,Number,omitempty"` - InfraOS InfraOS `json:"os,omitempty"` + InfraOS *InfraOS `json:"os,omitempty"` Ip json.Number `json:"ip,omitempty"` Mac json.Number `json:"mac,omitempty"` CreateDate timestamp.Timestamp `json:"createDate,omitempty"` @@ -35,42 +68,42 @@ type InfraHost struct { type InfraOS struct { Id json.Number `json:"id,Number,omitempty"` - InfraMachine InfraMachine `json:"machine,omitempty"` + InfraMachine *InfraMachine `json:"machine,omitempty"` Meta string `json:"meta,omitempty"` CreateDate timestamp.Timestamp `json:"createDate,omitempty"` - MetaInfraVendor meta.MetaInfraVendor `json:"vendor,omitempty"` + MetaInfraVendor *meta.MetaInfraVendor `json:"vendor,omitempty"` } type InfraOSApplication struct { Id json.Number `json:"id,Number,omitempty"` - InfraOS InfraOS `json:"os,omitempty"` + InfraOS *InfraOS `json:"os,omitempty"` Name string `json:"name,omitempty"` CreateDate timestamp.Timestamp `json:"createDate,omitempty"` } type InfraOSDaemon struct { Id json.Number `json:"id,Number,omitempty"` - InfraOS InfraOS `json:"os,omitempty"` + InfraOS *InfraOS `json:"os,omitempty"` Name string `json:"name,omitempty"` CreateDate timestamp.Timestamp `json:"createDate,omitempty"` } type InfraOSPort struct { Id json.Number `json:"id,Number,omitempty"` - InfraOS InfraOS `json:"os,omitempty"` + InfraOS *InfraOS `json:"os,omitempty"` CreateDate timestamp.Timestamp `json:"createDate,omitempty"` Port json.Number `json:"port,omitempty"` PortType string `json:"portType,omitempty"` - MetaInfraVendor meta.MetaInfraVendor `json:"vendor,omitempty"` + MetaInfraVendor *meta.MetaInfraVendor `json:"vendor,omitempty"` TlsType bool `json:"tlsType,omitempty"` } type InfraServiceApplication struct { Id json.Number `json:"id,Number,omitempty"` - InfraHost InfraHost `json:"host,omitempty"` + InfraHost *InfraHost `json:"host,omitempty"` PortType string `json:"portType,omitempty"` Port json.Number `json:"port,omitempty"` - MetaInfraVendor meta.MetaInfraVendor `json:"vendor,omitempty"` + MetaInfraVendor *meta.MetaInfraVendor `json:"vendor,omitempty"` CreateDate timestamp.Timestamp `json:"createDate,omitempty"` TlsType bool `json:"tlsType,omitempty"` } @@ -110,7 +143,7 @@ func (is *InfraService) RegistHost(i *InfraHost) (string, error) { } func (is *InfraService) RegistOS(i *InfraOS) (string, error) { - out, err := utils.InvokeDBByModel("InfraOS", "save", i, utils.MODEL_INFRA_OS) + out, err := utils.InvokeDBByModel("infraOS", "save", i, utils.MODEL_INFRA_OS) if err != nil { return "", err @@ -120,7 +153,7 @@ func (is *InfraService) RegistOS(i *InfraOS) (string, error) { } func (is *InfraService) RegistOSApplication(i *InfraOSApplication) (string, error) { - out, err := utils.InvokeDBByModel("InfraOSApplication", "save", i, utils.MODEL_INFRA_OS_APPLICATION) + out, err := utils.InvokeDBByModel("infraOSApplication", "save", i, utils.MODEL_INFRA_OS_APPLICATION) if err != nil { return "", err @@ -130,7 +163,7 @@ func (is *InfraService) RegistOSApplication(i *InfraOSApplication) (string, erro } func (is *InfraService) RegistOSDaemon(i *InfraOSDaemon) (string, error) { - out, err := utils.InvokeDBByModel("InfraOSDaemon", "save", i, utils.MODEL_INFRA_OS_DAEMON) + out, err := utils.InvokeDBByModel("infraOSDaemon", "save", i, utils.MODEL_INFRA_OS_DAEMON) if err != nil { return "", err @@ -140,7 +173,7 @@ func (is *InfraService) RegistOSDaemon(i *InfraOSDaemon) (string, error) { } func (is *InfraService) RegistOSPort(i *InfraOSPort) (string, error) { - out, err := utils.InvokeDBByModel("InfraOSPort", "save", i, utils.MODEL_INFRA_OS_PORT) + out, err := utils.InvokeDBByModel("infraOSPort", "save", i, utils.MODEL_INFRA_OS_PORT) if err != nil { return "", err @@ -149,8 +182,8 @@ func (is *InfraService) RegistOSPort(i *InfraOSPort) (string, error) { return out, nil } -func (is *InfraService) RegistService(i *InfraService) (string, error) { - out, err := utils.InvokeDBByModel("InfraService", "save", i, utils.MODEL_INFRA_SERVICE) +func (is *InfraService) RegistService(i *InfraServiceApplication) (string, error) { + out, err := utils.InvokeDBByModel("infraService", "save", i, utils.MODEL_INFRA_SERVICE) if err != nil { return "", err diff --git a/proxy/meta/meta_infra_vendor.go b/proxy/meta/meta_infra_vendor.go index fc1d6fa..6ad5308 100644 --- a/proxy/meta/meta_infra_vendor.go +++ b/proxy/meta/meta_infra_vendor.go @@ -4,22 +4,26 @@ import ( "encoding/json" "git.loafle.net/overflow/commons_go/model/timestamp" "strconv" -) +) const ( INFRA_VENDOR_TYPE_SERIVCE = 7 - INFRA_VENDOR_START_IDX = 39 - INFRA_VENDOR_MYSQL = INFRA_VENDOR_START_IDX + 1 - INFRA_VENDOR_POSTGRESQL = INFRA_VENDOR_START_IDX + 2 - INFRA_VENDOR_WMI = INFRA_VENDOR_START_IDX + 3 - INFRA_VENDOR_SNMPV2 = INFRA_VENDOR_START_IDX + 4 + INFRA_VENDOR_START_IDX = 38 + INFRA_VENDOR_MYSQL = INFRA_VENDOR_START_IDX + 1 + INFRA_VENDOR_POSTGRESQL = INFRA_VENDOR_START_IDX + 2 + INFRA_VENDOR_WMI = INFRA_VENDOR_START_IDX + 3 + INFRA_VENDOR_SNMPV2 = INFRA_VENDOR_START_IDX + 4 + + INFRA_VENDOR_UNKNOWN = INFRA_VENDOR_START_IDX + 5 ) + var metaInfraVendorMap = map[int]string{ - INFRA_VENDOR_MYSQL: "MYSQL", + INFRA_VENDOR_MYSQL: "MYSQL", INFRA_VENDOR_POSTGRESQL: "POSTGRESQL", - INFRA_VENDOR_WMI: "WMI", - INFRA_VENDOR_SNMPV2: "SNMPV2", + INFRA_VENDOR_WMI: "WMI", + INFRA_VENDOR_SNMPV2: "SNMPV2", + INFRA_VENDOR_UNKNOWN:"UNKNOWN", } var metaInfraVendorMapStr map[string]int @@ -27,7 +31,9 @@ var metaInfraVendorMapStr map[string]int func init() { metaInfraVendorMapStr = make(map[string]int) - //for + for k, v := range metaInfraVendorMap { + metaInfraVendorMapStr[v] = k + } } type MetaInfraVendor struct { @@ -37,13 +43,21 @@ type MetaInfraVendor struct { MetaInfraType MetaInfraType `json:"metaInfraType,omitempty"` } -func NewMetaInfraVendorByService(serviceName string) *MetaInfraVendor{ +func NewMetaInfraVendorByService(serviceName string) *MetaInfraVendor { idx := metaInfraVendorMapStr[serviceName] + if idx <= 0 { + return &MetaInfraVendor{ + Id: json.Number(strconv.Itoa(INFRA_VENDOR_UNKNOWN)), + Name: "UNKNOWN", + MetaInfraType: MetaInfraType{Id: json.Number(strconv.Itoa(INFRA_VENDOR_TYPE_SERIVCE))}, + } + } + return &MetaInfraVendor{ - Id: json.Number(strconv.Itoa(idx)), - Name:serviceName, - MetaInfraType:MetaInfraType{Id:json.Number(strconv.Itoa(INFRA_VENDOR_TYPE_SERIVCE))}, + Id: json.Number(strconv.Itoa(idx)), + Name: serviceName, + MetaInfraType: MetaInfraType{Id: json.Number(strconv.Itoa(INFRA_VENDOR_TYPE_SERIVCE))}, } } diff --git a/proxy/target/target_service.go b/proxy/target/target_service.go index 64a8864..2b673b9 100644 --- a/proxy/target/target_service.go +++ b/proxy/target/target_service.go @@ -3,12 +3,14 @@ package target import ( "encoding/json" "git.loafle.net/overflow/commons_go/model/timestamp" + "git.loafle.net/overflow/discovery/discovery/types" "git.loafle.net/overflow/overflow_service/proxy/infra" + "git.loafle.net/overflow/overflow_service/proxy/meta" "git.loafle.net/overflow/overflow_service/proxy/probe" "git.loafle.net/overflow/overflow_service/proxy/utils" "strconv" - "git.loafle.net/overflow/overflow_service/proxy/meta" - "git.loafle.net/overflow/discovery/discovery/types" + + ) type Target struct { @@ -19,9 +21,8 @@ type Target struct { } type TargetInfo struct { - - Hosts *[]*types.DiscoveryHost `json:"hosts,omitempty"` - Probe *probe.Probe `json:"probe,omitempty"` + Hosts *[]*types.DiscoveryHost `json:"hosts,omitempty"` + Probe *probe.Probe `json:"probe,omitempty"` } type TargetService struct { @@ -86,7 +87,7 @@ func (t *TargetService) RegistTarget(ti *TargetInfo) (string, error) { for _, host := range *ti.Hosts { im := infra.InfraMachine{} - im.Probe = *ti.Probe + im.Probe = ti.Probe out, err := is.RegistMachine(&im) @@ -99,50 +100,173 @@ func (t *TargetService) RegistTarget(ti *TargetInfo) (string, error) { return "", err } + iim := infra.NewInfra(im.Id, &im) + _, err = is.Regist(iim) + if err != nil { + return "", err + } + io := infra.InfraOS{} - io.InfraMachine = im + io.InfraMachine = &im miv := meta.MetaInfraVendor{} if host.Os == "Windows" { miv.Id = "25" } else if host.Os == "Linux" { miv.Id = "27" } - io.MetaInfraVendor = miv + io.MetaInfraVendor = &miv + + out, err = is.RegistOS(&io) + + if err != nil { + return "", err + } + + err = json.Unmarshal([]byte(out), &io) + if err != nil { + return "", err + } + + iio := infra.NewInfra(io.Id, &io) + _, err = is.Regist(iio) + if err != nil { + return "", err + } ih := infra.InfraHost{} - ih.InfraOS = io + ih.InfraOS = &io ih.Ip = json.Number(strconv.FormatInt(host.Ip, 10)) ih.Mac = json.Number(strconv.FormatInt(host.Mac, 10)) - //i := infra.Infra{} + out, err = is.RegistHost(&ih) + + if err != nil { + return "", err + } + + err = json.Unmarshal([]byte(out), &ih) + if err != nil { + return "", err + } + + iih := infra.NewInfra(ih.Id, &ih) + out, err = is.Regist(iih) + if err != nil { + return "", err + } + + err = json.Unmarshal([]byte(out), iih) + if err != nil { + return "", err + } - for _, port := range host.Ports{ + err = RegistTarget(host.Target, iih, ti.Probe) + if err != nil { + return "", err + } + + + for _, port := range host.Ports { ip := infra.InfraOSPort{} - ip.InfraOS = io + ip.InfraOS = &io ip.Port = json.Number(strconv.FormatUint(uint64(port.Number), 10)) ip.PortType = port.PortType //ip.MetaInfraVendor //ip.TlsType + out, err = is.RegistOSPort(&ip) + + if err != nil { + return "", err + } + + err = json.Unmarshal([]byte(out), &ip) + if err != nil { + return "", err + } + + iip := infra.NewInfra(ip.Id, &ip) + out, err = is.Regist(iip) + if err != nil { + return "", err + } + + err = json.Unmarshal([]byte(out), iip) + if err != nil { + return "", err + } + + err = RegistTarget(port.Target, iip, ti.Probe) + if err != nil { + return "", err + } + for _, service := range port.Services { isa := infra.InfraServiceApplication{} - isa.InfraHost = ih + isa.InfraHost = &ih isa.PortType = port.PortType //isa.TlsType isa.Port = json.Number(strconv.FormatUint(uint64(port.Number), 10)) //isa.MetaInfraVendor - isa.MetaInfraVendor = *meta.NewMetaInfraVendorByService(service.ServiceName) + isa.MetaInfraVendor = meta.NewMetaInfraVendorByService(service.ServiceName) + + out, err = is.RegistService(&isa) + + if err != nil { + return "", err + } + + err = json.Unmarshal([]byte(out), &isa) + if err != nil { + return "", err + } + + iisa := infra.NewInfra(isa.Id, &isa) + out, err = is.Regist(iisa) + if err != nil { + return "", err + } + + err = json.Unmarshal([]byte(out), iisa) + if err != nil { + return "", err + } + + err = RegistTarget(service.Target, iisa, ti.Probe) + if err != nil { + return "", err + } + } } - } - return "", nil +} + + +func RegistTarget(check bool, i *infra.Infra, p *probe.Probe) error { + + if check == false { + return nil + } + + ttt := &Target{} + ttt.Infra = i + ttt.Probe = p + + ts := NewTargetService() + _, err := ts.Regist(ttt) + + if err != nil { + return err + } + + return nil } \ No newline at end of file diff --git a/proxy/target/target_service_test.go b/proxy/target/target_service_test.go index 298cf67..22cc2f8 100644 --- a/proxy/target/target_service_test.go +++ b/proxy/target/target_service_test.go @@ -6,6 +6,10 @@ import ( "git.loafle.net/overflow/overflow_service/proxy/infra" "git.loafle.net/overflow/overflow_service/proxy/probe" "testing" + + + "io/ioutil" + "reflect" ) func TestTargetRegist(t *testing.T) { @@ -61,3 +65,42 @@ func TestTargetDelete(t *testing.T) { } t.Log(res) } + +func TestTargetDiscovery(t *testing.T) { + + contents,_ := ioutil.ReadFile("../../dh.json") + + ti := TargetInfo{} + + err := json.Unmarshal(contents, &ti) + + if err != nil{ + t.Fatal(err) + } + + t.Log(ti) + + ts := NewTargetService() + ppp := probe.Probe{} + ppp.Id = "1" + ti.Probe = &ppp + out, err := ts.RegistTarget(&ti) + + if err != nil { + t.Fatal(err) + } + + t.Log(out) +} + +func TestInterface(t *testing.T) { + + aaa := &infra.InfraMachine{} + + var o interface{} = aaa + + tt := reflect.TypeOf(o) + t.Log(tt.Name()) + t.Log(tt.String()) + +}