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

View File

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

View File

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

View File

@ -66,72 +66,86 @@ SERVICE_LOOP:
switch metaIPTypeEnum {
case omm.MetaIPTypeEnumV4:
for _, ipv4 := range entry.AddrIPv4 {
h := discoverySession.AddHost(&omd.Host{
h := discoverySession.AddHost(
"mDNS",
&omd.Host{
MetaIPType: omm.ToMetaIPType(metaIPTypeEnum),
Name: hostName,
Address: ipv4.String(),
Meta: meta,
Zone: discoverySession.Zone(),
DiscoveredBy: "mDNS",
DiscoveredDate: omu.NowPtr(),
})
},
meta,
)
if 1 > port {
continue ENTRY_LOOP
}
p := discoverySession.AddPort(&omd.Port{
p := discoverySession.AddPort(
"mDNS",
&omd.Port{
MetaPortType: metaPortType,
PortNumber: json.Number(strconv.Itoa(port)),
Meta: meta,
Host: h,
DiscoveredBy: "mDNS",
DiscoveredDate: omu.NowPtr(),
})
},
meta,
)
discoverySession.AddService(&omd.Service{
discoverySession.AddService(
"mDNS",
&omd.Service{
MetaCryptoType: metaCryptoType,
Key: serviceName,
Name: name,
Port: p,
DiscoveredBy: "mDNS",
DiscoveredDate: omu.NowPtr(),
})
},
meta,
)
}
case omm.MetaIPTypeEnumV6:
for _, ipv6 := range entry.AddrIPv6 {
h := discoverySession.AddHost(&omd.Host{
h := discoverySession.AddHost(
"mDNS",
&omd.Host{
MetaIPType: omm.ToMetaIPType(metaIPTypeEnum),
Name: hostName,
Address: ipv6.String(),
Meta: meta,
Zone: discoverySession.Zone(),
DiscoveredBy: "mDNS",
DiscoveredDate: omu.NowPtr(),
})
},
meta,
)
if 1 > port {
continue ENTRY_LOOP
}
p := discoverySession.AddPort(&omd.Port{
p := discoverySession.AddPort(
"mDNS",
&omd.Port{
MetaPortType: metaPortType,
PortNumber: json.Number(strconv.Itoa(port)),
Meta: meta,
Host: h,
DiscoveredBy: "mDNS",
DiscoveredDate: omu.NowPtr(),
})
},
meta,
)
discoverySession.AddService(&omd.Service{
discoverySession.AddService(
"mDNS",
&omd.Service{
MetaCryptoType: metaCryptoType,
Key: serviceName,
Name: name,
Port: p,
DiscoveredBy: "mDNS",
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()
}
h := discoverySession.AddHost(&omd.Host{
h := discoverySession.AddHost(
"SNMP V2c",
&omd.Host{
MetaIPType: discoverySession.Zone().MetaIPType,
Name: "",
Address: target.String(),
Meta: meta,
Zone: discoverySession.Zone(),
DiscoveredBy: "SNMP V2c",
DiscoveredDate: omu.NowPtr(),
})
},
meta,
)
p := discoverySession.AddPort(&omd.Port{
p := discoverySession.AddPort("SNMP V2c",
&omd.Port{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumUDP),
PortNumber: credential.Port,
Meta: meta,
Host: h,
DiscoveredBy: "SNMP V2c",
DiscoveredDate: omu.NowPtr(),
})
},
meta,
)
discoverySession.AddService(&omd.Service{
discoverySession.AddService("SNMP V2c",
&omd.Service{
MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
Key: "SNMP",
Name: "SNMP V2c",
Port: p,
DiscoveredBy: "SNMP V2c",
DiscoveredDate: omu.NowPtr(),
})
},
meta,
)
// 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{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
PortNumber: json.Number(strconv.Itoa(port)),
DiscoveredBy: "TCP Connection",
DiscoveredDate: omu.NowPtr(),
}
p.Host = targetHost
ports[port] = p
go discoverySession.AddPort(p)
go discoverySession.AddPort("TCP Connection", p, nil)
}
func Ulimit() int64 {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,10 @@
package session
import (
"encoding/json"
"log"
omd "git.loafle.net/overflow/model/discovery"
"git.loafle.net/overflow_scanner/probe/discovery/types"
)
@ -20,14 +24,20 @@ type mockDiscoverySession struct {
ofDiscoverySession
}
// func (ds *mockDiscoverySession) AddHost(host *omd.Host) *omd.Host {
// return host
// }
func (ds *mockDiscoverySession) AddHost(discoveredBy string, host *omd.Host, meta map[string]string) *omd.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 {
// return port
// }
func (ds *mockDiscoverySession) AddPort(discoveredBy string, port *omd.Port, meta map[string]string) *omd.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 {
// return service
// }
func (ds *mockDiscoverySession) AddService(discoveredBy string, service *omd.Service, meta map[string]string) *omd.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 {
// 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(),
Key: discoveredMatcher.Key(),
Name: discoveredMatcher.Name(matchCtx),
Meta: matchCtx.GetAttributes(),
DiscoveredBy: "Service Matcher",
DiscoveredDate: omu.NowPtr(),
Port: targetPort,
})
},
matchCtx.GetAttributes(),
)
}
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 {
s := &omd.Service{
Key: _matcher.Key(),
Name: _matcher.Name(matchCtx),
Port: targetPort,
MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
DiscoveredDate: omu.NowPtr(),
Metadata: matchCtx.GetAttributes(),
Name: _matcher.Name(matchCtx),
}
go discoverySession.AddService(s)
go discoverySession.AddService("Service Matcher", s, matchCtx.GetAttributes())
return nil
}