This commit is contained in:
crusader 2018-09-11 16:59:21 +09:00
parent 502ff3ef89
commit d2e70072e3
14 changed files with 221 additions and 138 deletions

View File

@ -52,7 +52,7 @@ func Scan(discoverySession session.DiscoverySession) error {
} }
delay.Store(true) delay.Store(true)
if h := handlePacketARP(zone, targetHosts, hosts, packet); nil != h { if h := handlePacketARP(zone, targetHosts, hosts, packet); nil != h {
go discoverySession.AddHost(h) go discoverySession.AddHost("ARP", h, nil)
} }
case <-ticker.C: case <-ticker.C:
if false == delay.Load().(bool) { if false == delay.Load().(bool) {
@ -135,7 +135,6 @@ func handlePacketARP(zone *omd.Zone, targetHosts []net.IP, hosts map[string]*omd
Address: ip.String(), Address: ip.String(),
Mac: net.HardwareAddr(packet.SourceHwAddress).String(), Mac: net.HardwareAddr(packet.SourceHwAddress).String(),
Zone: zone, Zone: zone,
DiscoveredBy: "ARP",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
} }

View File

@ -65,7 +65,7 @@ func scanV4(discoverySession session.DiscoverySession) error {
} }
delay.Store(true) delay.Store(true)
if h := handlePacketICMP4(zone, targetHosts, hosts, packet); nil != h { if h := handlePacketICMP4(zone, targetHosts, hosts, packet); nil != h {
go discoverySession.AddHost(h) go discoverySession.AddHost("ICMP", h, nil)
} }
case <-ticker.C: case <-ticker.C:
if false == delay.Load().(bool) { if false == delay.Load().(bool) {
@ -176,7 +176,6 @@ func handlePacketICMP4(zone *omd.Zone, targetHosts []net.IP, hosts map[string]*o
Address: ip.String(), Address: ip.String(),
Mac: net.HardwareAddr(ethLayer.SrcMAC).String(), Mac: net.HardwareAddr(ethLayer.SrcMAC).String(),
Zone: zone, Zone: zone,
DiscoveredBy: "ICMP",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
} }

View File

@ -54,7 +54,7 @@ func scanV6(discoverySession session.DiscoverySession) error {
} }
delay.Store(true) delay.Store(true)
if h := handlePacketICMP6(zone, targetHosts, hosts, packet); nil != h { if h := handlePacketICMP6(zone, targetHosts, hosts, packet); nil != h {
go discoverySession.AddHost(h) go discoverySession.AddHost("ICMP", h, nil)
} }
case <-ticker.C: case <-ticker.C:
if false == delay.Load().(bool) { if false == delay.Load().(bool) {
@ -161,7 +161,6 @@ func handlePacketICMP6(zone *omd.Zone, targetHosts []net.IP, hosts map[string]*o
Address: ip.String(), Address: ip.String(),
Mac: net.HardwareAddr(ethLayer.SrcMAC).String(), Mac: net.HardwareAddr(ethLayer.SrcMAC).String(),
Zone: zone, Zone: zone,
DiscoveredBy: "ICMP",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
} }

View File

@ -66,72 +66,86 @@ SERVICE_LOOP:
switch metaIPTypeEnum { switch metaIPTypeEnum {
case omm.MetaIPTypeEnumV4: case omm.MetaIPTypeEnumV4:
for _, ipv4 := range entry.AddrIPv4 { for _, ipv4 := range entry.AddrIPv4 {
h := discoverySession.AddHost(&omd.Host{ h := discoverySession.AddHost(
"mDNS",
&omd.Host{
MetaIPType: omm.ToMetaIPType(metaIPTypeEnum), MetaIPType: omm.ToMetaIPType(metaIPTypeEnum),
Name: hostName, Name: hostName,
Address: ipv4.String(), Address: ipv4.String(),
Meta: meta,
Zone: discoverySession.Zone(), Zone: discoverySession.Zone(),
DiscoveredBy: "mDNS",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
}) },
meta,
)
if 1 > port { if 1 > port {
continue ENTRY_LOOP continue ENTRY_LOOP
} }
p := discoverySession.AddPort(&omd.Port{ p := discoverySession.AddPort(
"mDNS",
&omd.Port{
MetaPortType: metaPortType, MetaPortType: metaPortType,
PortNumber: json.Number(strconv.Itoa(port)), PortNumber: json.Number(strconv.Itoa(port)),
Meta: meta,
Host: h, Host: h,
DiscoveredBy: "mDNS",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
}) },
meta,
)
discoverySession.AddService(&omd.Service{ discoverySession.AddService(
"mDNS",
&omd.Service{
MetaCryptoType: metaCryptoType, MetaCryptoType: metaCryptoType,
Key: serviceName, Key: serviceName,
Name: name, Name: name,
Port: p, Port: p,
DiscoveredBy: "mDNS",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
}) },
meta,
)
} }
case omm.MetaIPTypeEnumV6: case omm.MetaIPTypeEnumV6:
for _, ipv6 := range entry.AddrIPv6 { for _, ipv6 := range entry.AddrIPv6 {
h := discoverySession.AddHost(&omd.Host{ h := discoverySession.AddHost(
"mDNS",
&omd.Host{
MetaIPType: omm.ToMetaIPType(metaIPTypeEnum), MetaIPType: omm.ToMetaIPType(metaIPTypeEnum),
Name: hostName, Name: hostName,
Address: ipv6.String(), Address: ipv6.String(),
Meta: meta,
Zone: discoverySession.Zone(), Zone: discoverySession.Zone(),
DiscoveredBy: "mDNS",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
}) },
meta,
)
if 1 > port { if 1 > port {
continue ENTRY_LOOP continue ENTRY_LOOP
} }
p := discoverySession.AddPort(&omd.Port{ p := discoverySession.AddPort(
"mDNS",
&omd.Port{
MetaPortType: metaPortType, MetaPortType: metaPortType,
PortNumber: json.Number(strconv.Itoa(port)), PortNumber: json.Number(strconv.Itoa(port)),
Meta: meta,
Host: h, Host: h,
DiscoveredBy: "mDNS",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
}) },
meta,
)
discoverySession.AddService(&omd.Service{ discoverySession.AddService(
"mDNS",
&omd.Service{
MetaCryptoType: metaCryptoType, MetaCryptoType: metaCryptoType,
Key: serviceName, Key: serviceName,
Name: name, Name: name,
Port: p, Port: p,
DiscoveredBy: "mDNS",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
}) },
meta,
)
} }
} }

View File

@ -138,33 +138,38 @@ func scanV2(target net.IP, discoverySession session.DiscoverySession, credential
meta[val.Oid.String()] = val.Variable.String() meta[val.Oid.String()] = val.Variable.String()
} }
h := discoverySession.AddHost(&omd.Host{ h := discoverySession.AddHost(
"SNMP V2c",
&omd.Host{
MetaIPType: discoverySession.Zone().MetaIPType, MetaIPType: discoverySession.Zone().MetaIPType,
Name: "", Name: "",
Address: target.String(), Address: target.String(),
Meta: meta,
Zone: discoverySession.Zone(), Zone: discoverySession.Zone(),
DiscoveredBy: "SNMP V2c",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
}) },
meta,
)
p := discoverySession.AddPort(&omd.Port{ p := discoverySession.AddPort("SNMP V2c",
&omd.Port{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumUDP), MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumUDP),
PortNumber: credential.Port, PortNumber: credential.Port,
Meta: meta,
Host: h, Host: h,
DiscoveredBy: "SNMP V2c",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
}) },
meta,
)
discoverySession.AddService(&omd.Service{ discoverySession.AddService("SNMP V2c",
&omd.Service{
MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE), MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
Key: "SNMP", Key: "SNMP",
Name: "SNMP V2c", Name: "SNMP V2c",
Port: p, Port: p,
DiscoveredBy: "SNMP V2c",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
}) },
meta,
)
// log.Printf("Host: %v, Port: %v, Service: %v", h, p, s) // log.Printf("Host: %v, Port: %v, Service: %v", h, p, s)

View File

@ -96,14 +96,13 @@ func tryConnect(discoverySession session.DiscoverySession, ports map[int]*omd.Po
p := &omd.Port{ p := &omd.Port{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP), MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
PortNumber: json.Number(strconv.Itoa(port)), PortNumber: json.Number(strconv.Itoa(port)),
DiscoveredBy: "TCP Connection",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
} }
p.Host = targetHost p.Host = targetHost
ports[port] = p ports[port] = p
go discoverySession.AddPort(p) go discoverySession.AddPort("TCP Connection", p, nil)
} }
func Ulimit() int64 { func Ulimit() int64 {

View File

@ -47,7 +47,7 @@ func scanV4(discoverySession session.DiscoverySession, targetHost *omd.Host) err
} }
delay.Store(true) delay.Store(true)
if p := handlePacketTCP4(discoverySession, targetHost, ports, packet); nil != p { if p := handlePacketTCP4(discoverySession, targetHost, ports, packet); nil != p {
go discoverySession.AddPort(p) go discoverySession.AddPort("TCP SYN", p, nil)
} }
case <-ticker.C: case <-ticker.C:
if false == delay.Load().(bool) { if false == delay.Load().(bool) {
@ -132,7 +132,6 @@ func handlePacketTCP4(discoverySession session.DiscoverySession, host *omd.Host,
p := &omd.Port{ p := &omd.Port{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP), MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
PortNumber: json.Number(strconv.Itoa(port)), PortNumber: json.Number(strconv.Itoa(port)),
DiscoveredBy: "TCP SYN",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
} }
p.Host = host p.Host = host

View File

@ -96,14 +96,13 @@ func tryConnect(discoverySession session.DiscoverySession, ports map[int]*omd.Po
p := &omd.Port{ p := &omd.Port{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP), MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
PortNumber: json.Number(strconv.Itoa(port)), PortNumber: json.Number(strconv.Itoa(port)),
DiscoveredBy: "UDP Connection",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
} }
p.Host = targetHost p.Host = targetHost
ports[port] = p ports[port] = p
go discoverySession.AddPort(p) go discoverySession.AddPort("UDP Connection", p, nil)
} }
func Ulimit() int64 { func Ulimit() int64 {

View File

@ -47,7 +47,7 @@ func scanV4(discoverySession session.DiscoverySession, targetHost *omd.Host) err
} }
delay.Store(true) delay.Store(true)
if p := handlePacketUDP4(discoverySession, targetHost, ports, packet); nil != p { if p := handlePacketUDP4(discoverySession, targetHost, ports, packet); nil != p {
go discoverySession.AddPort(p) go discoverySession.AddPort("UDP Matcher", p, nil)
} }
case <-ticker.C: case <-ticker.C:
if false == delay.Load().(bool) { if false == delay.Load().(bool) {
@ -149,7 +149,6 @@ func handlePacketUDP4(discoverySession session.DiscoverySession, host *omd.Host,
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumUDP), MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumUDP),
PortNumber: json.Number(strconv.Itoa(port)), PortNumber: json.Number(strconv.Itoa(port)),
UDPLayer: udpLayer, UDPLayer: udpLayer,
DiscoveredBy: "UDP Matcher",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
} }
p.Host = host p.Host = host

View File

@ -28,11 +28,16 @@ LOOP:
continue LOOP continue LOOP
} }
discoverySession.AddHost(&omd.Host{ discoverySession.AddHost(
"UPnP",
&omd.Host{
MetaIPType: discoverySession.Zone().MetaIPType, MetaIPType: discoverySession.Zone().MetaIPType,
Name: rd.FriendlyName, Name: rd.FriendlyName,
Address: rd.PresentationURL.URL.Host, Address: rd.PresentationURL.URL.Host,
Meta: map[string]string{ Zone: discoverySession.Zone(),
DiscoveredDate: omu.NowPtr(),
},
map[string]string{
"DeviceType": rd.DeviceType, "DeviceType": rd.DeviceType,
"Manufacturer": rd.Manufacturer, "Manufacturer": rd.Manufacturer,
"ManufacturerURL": rd.ManufacturerURL.Str, "ManufacturerURL": rd.ManufacturerURL.Str,
@ -43,10 +48,7 @@ LOOP:
"UDN": rd.UDN, "UDN": rd.UDN,
"UPC": rd.UPC, "UPC": rd.UPC,
}, },
Zone: discoverySession.Zone(), )
DiscoveredBy: "UPnP",
DiscoveredDate: omu.NowPtr(),
})
select { select {
case <-discoverySession.StopChan(): case <-discoverySession.StopChan():

View File

@ -35,9 +35,9 @@ type DiscoverySession interface {
SetDiscoveryDelegator(chan<- interface{}) SetDiscoveryDelegator(chan<- interface{})
AddHost(host *omd.Host) *omd.Host AddHost(discoveredBy string, host *omd.Host, meta map[string]string) *omd.Host
AddPort(port *omd.Port) *omd.Port AddPort(discoveredBy string, port *omd.Port, meta map[string]string) *omd.Port
AddService(service *omd.Service) *omd.Service AddService(discoveredBy string, service *omd.Service, meta map[string]string) *omd.Service
DiscoveredHost(address string) *omd.Host DiscoveredHost(address string) *omd.Host
DiscoveredAllHosts(includeMac bool) map[string]*omd.Host DiscoveredAllHosts(includeMac bool) map[string]*omd.Host
@ -225,7 +225,7 @@ func (ds *ofDiscoverySession) TargetHosts() []net.IP {
return ds.targetHosts return ds.targetHosts
} }
func (ds *ofDiscoverySession) AddHost(host *omd.Host) *omd.Host { func (ds *ofDiscoverySession) AddHost(discoveredBy string, host *omd.Host, meta map[string]string) *omd.Host {
ds.discoveredMtx.Lock() ds.discoveredMtx.Lock()
defer ds.discoveredMtx.Unlock() defer ds.discoveredMtx.Unlock()
@ -236,10 +236,16 @@ func (ds *ofDiscoverySession) AddHost(host *omd.Host) *omd.Host {
modified = true modified = true
} }
meta, metaModified := ds.appendMeta(h.Meta, host.Meta) discoveredBys, discoveredByModified := ds.appendDiscoveredBy(discoveredBy, h.DiscoveredBy)
if discoveredByModified {
h.DiscoveredBy = discoveredBys
modified = true
}
metas, metaModified := ds.appendMeta(discoveredBy, h.Meta, meta)
if metaModified { if metaModified {
h.Meta = meta h.Meta = metas
modified = metaModified modified = true
} }
if modified && nil != ds.discoveryDelegator { if modified && nil != ds.discoveryDelegator {
@ -249,15 +255,21 @@ func (ds *ofDiscoverySession) AddHost(host *omd.Host) *omd.Host {
return h return h
} }
func (ds *ofDiscoverySession) AddPort(port *omd.Port) *omd.Port { func (ds *ofDiscoverySession) AddPort(discoveredBy string, port *omd.Port, meta map[string]string) *omd.Port {
ds.discoveredMtx.Lock() ds.discoveredMtx.Lock()
defer ds.discoveredMtx.Unlock() defer ds.discoveredMtx.Unlock()
p, modified := ds.findPort(port) p, modified := ds.findPort(port)
meta, metaModified := ds.appendMeta(p.Meta, port.Meta) discoveredBys, discoveredByModified := ds.appendDiscoveredBy(discoveredBy, p.DiscoveredBy)
if discoveredByModified {
p.DiscoveredBy = discoveredBys
modified = true
}
metas, metaModified := ds.appendMeta(discoveredBy, p.Meta, meta)
if metaModified { if metaModified {
p.Meta = meta p.Meta = metas
modified = metaModified modified = metaModified
} }
@ -268,15 +280,21 @@ func (ds *ofDiscoverySession) AddPort(port *omd.Port) *omd.Port {
return p return p
} }
func (ds *ofDiscoverySession) AddService(service *omd.Service) *omd.Service { func (ds *ofDiscoverySession) AddService(discoveredBy string, service *omd.Service, meta map[string]string) *omd.Service {
ds.discoveredMtx.Lock() ds.discoveredMtx.Lock()
defer ds.discoveredMtx.Unlock() defer ds.discoveredMtx.Unlock()
s, modified := ds.findService(service) s, modified := ds.findService(service)
meta, metaModified := ds.appendMeta(s.Meta, service.Meta) discoveredBys, discoveredByModified := ds.appendDiscoveredBy(discoveredBy, s.DiscoveredBy)
if discoveredByModified {
s.DiscoveredBy = discoveredBys
modified = true
}
metas, metaModified := ds.appendMeta(discoveredBy, s.Meta, meta)
if metaModified { if metaModified {
s.Meta = meta s.Meta = metas
modified = metaModified modified = metaModified
} }
@ -441,18 +459,58 @@ func (ds *ofDiscoverySession) findService(service *omd.Service) (s *omd.Service,
return return
} }
func (ds *ofDiscoverySession) appendMeta(oriMeta map[string]string, newMeta map[string]string) (resultMap map[string]string, modified bool) { func (ds *ofDiscoverySession) appendDiscoveredBy(discoveredBy string, oriDiscoveredBy []string) (resultDiscoveredBy []string, modified bool) {
modified = false modified = false
if nil == newMeta {
resultMap = oriMeta if "" == discoveredBy {
resultDiscoveredBy = oriDiscoveredBy
return return
} }
if nil == oriMeta {
resultMap = newMeta if nil == oriDiscoveredBy {
resultDiscoveredBy = []string{
discoveredBy,
}
modified = true modified = true
return return
} }
for _, v := range oriDiscoveredBy {
if v == discoveredBy {
resultDiscoveredBy = oriDiscoveredBy
return
}
}
oriDiscoveredBy = append(oriDiscoveredBy, discoveredBy)
resultDiscoveredBy = oriDiscoveredBy
modified = true
return
}
func (ds *ofDiscoverySession) appendMeta(discoveredBy string, oriMetas map[string]map[string]string, newMeta map[string]string) (resultMetas map[string]map[string]string, modified bool) {
modified = false
if nil == newMeta || 0 == len(newMeta) {
resultMetas = oriMetas
return
}
if nil == oriMetas {
resultMetas = map[string]map[string]string{
discoveredBy: newMeta,
}
modified = true
return
}
oriMeta, ok := oriMetas[discoveredBy]
if !ok {
oriMetas[discoveredBy] = newMeta
resultMetas = oriMetas
return
}
LOOP: LOOP:
for k, v := range newMeta { for k, v := range newMeta {
_v, _ok := oriMeta[k] _v, _ok := oriMeta[k]
@ -468,7 +526,7 @@ LOOP:
modified = true modified = true
} }
resultMap = oriMeta resultMetas = oriMetas
return return
} }

View File

@ -1,6 +1,10 @@
package session package session
import ( import (
"encoding/json"
"log"
omd "git.loafle.net/overflow/model/discovery"
"git.loafle.net/overflow_scanner/probe/discovery/types" "git.loafle.net/overflow_scanner/probe/discovery/types"
) )
@ -20,14 +24,20 @@ type mockDiscoverySession struct {
ofDiscoverySession ofDiscoverySession
} }
// func (ds *mockDiscoverySession) AddHost(host *omd.Host) *omd.Host { func (ds *mockDiscoverySession) AddHost(discoveredBy string, host *omd.Host, meta map[string]string) *omd.Host {
// return host b, _ := json.Marshal(host)
// } log.Print("Host: ", string(b))
return ds.ofDiscoverySession.AddHost(discoveredBy, host, meta)
}
// func (ds *mockDiscoverySession) AddPort(port *omd.Port) *omd.Port { func (ds *mockDiscoverySession) AddPort(discoveredBy string, port *omd.Port, meta map[string]string) *omd.Port {
// return port b, _ := json.Marshal(port)
// } log.Print("Port: ", string(b))
return ds.ofDiscoverySession.AddPort(discoveredBy, port, meta)
}
// func (ds *mockDiscoverySession) AddService(service *omd.Service) *omd.Service { func (ds *mockDiscoverySession) AddService(discoveredBy string, service *omd.Service, meta map[string]string) *omd.Service {
// return service b, _ := json.Marshal(service)
// } log.Print("Service: ", string(b))
return ds.ofDiscoverySession.AddService(discoveredBy, service, meta)
}

View File

@ -78,15 +78,17 @@ LOOP:
if nil != discoveredMatcher { if nil != discoveredMatcher {
// log.Printf("discovered matcher: %s(%s) %v", discoveredMatcher.Name(), discoveredMatcher.Key(), discoveredMatcher) // log.Printf("discovered matcher: %s(%s) %v", discoveredMatcher.Name(), discoveredMatcher.Key(), discoveredMatcher)
go discoverySession.AddService(&omd.Service{ go discoverySession.AddService(
"Service Matcher",
&omd.Service{
MetaCryptoType: discoveredConnector.metaCryptoType(), MetaCryptoType: discoveredConnector.metaCryptoType(),
Key: discoveredMatcher.Key(), Key: discoveredMatcher.Key(),
Name: discoveredMatcher.Name(matchCtx), Name: discoveredMatcher.Name(matchCtx),
Meta: matchCtx.GetAttributes(),
DiscoveredBy: "Service Matcher",
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
Port: targetPort, Port: targetPort,
}) },
matchCtx.GetAttributes(),
)
} }
return nil return nil

View File

@ -27,14 +27,13 @@ func scanUDP(discoverySession session.DiscoverySession, targetPort *omd.Port) er
if err := _matcher.Match(matchCtx, 0, p); err == nil { if err := _matcher.Match(matchCtx, 0, p); err == nil {
s := &omd.Service{ s := &omd.Service{
Key: _matcher.Key(), Key: _matcher.Key(),
Name: _matcher.Name(matchCtx),
Port: targetPort, Port: targetPort,
MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE), MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
Metadata: matchCtx.GetAttributes(),
Name: _matcher.Name(matchCtx),
} }
go discoverySession.AddService(s) go discoverySession.AddService("Service Matcher", s, matchCtx.GetAttributes())
return nil return nil
} }