This commit is contained in:
crusader 2018-09-12 13:55:51 +09:00
parent b3a85b5e26
commit 97184ea723
14 changed files with 234 additions and 187 deletions

View File

@ -1,7 +1,6 @@
package __test package __test
import ( import (
"encoding/json"
"fmt" "fmt"
omd "git.loafle.net/overflow/model/discovery" omd "git.loafle.net/overflow/model/discovery"
@ -23,31 +22,33 @@ const (
// ) // )
func Zone() *omd.Zone { func Zone() *omd.Zone {
return &omd.Zone{ return omd.NewZone(
Network: fmt.Sprintf("%s.0/24", ZONE_NETWORK), ZONE_IFACE,
Iface: ZONE_IFACE, ZONE_MAC,
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4), omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Address: fmt.Sprintf("%s.%s", ZONE_NETWORK, ZONE_ADDRESS), fmt.Sprintf("%s.0/24", ZONE_NETWORK),
Mac: ZONE_MAC, fmt.Sprintf("%s.%s", ZONE_NETWORK, ZONE_ADDRESS),
} )
} }
func Host(hostName string, address string, mac string) *omd.Host { func Host(hostName string, address string, mac string) *omd.Host {
return &omd.Host{ h := omd.NewHost(
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4), Zone(),
Name: hostName, omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Address: fmt.Sprintf("%s.%s", ZONE_NETWORK, address), fmt.Sprintf("%s.%s", ZONE_NETWORK, address),
Mac: mac, )
Zone: Zone(), h.Name = hostName
} h.Mac = mac
return h
} }
func Port(host *omd.Host, port string) *omd.Port { func Port(host *omd.Host, port int) *omd.Port {
return &omd.Port{ return omd.NewPort(
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP), host,
PortNumber: json.Number(port), omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
Host: host, port,
} )
} }
func DiscoveryConfig() *omd.DiscoveryConfig { func DiscoveryConfig() *omd.DiscoveryConfig {

View File

@ -130,13 +130,13 @@ func handlePacketARP(zone *omd.Zone, targetHosts []net.IP, hosts map[string]*omd
return nil return nil
} }
h := &omd.Host{ h := omd.NewHost(
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4), zone,
Address: ip.String(), omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Mac: net.HardwareAddr(packet.SourceHwAddress).String(), ip.String(),
Zone: zone, )
DiscoveredDate: omu.NowPtr(), h.Mac = net.HardwareAddr(packet.SourceHwAddress).String()
} h.DiscoveredDate = omu.NowPtr()
hosts[ip.String()] = h hosts[ip.String()] = h

View File

@ -171,13 +171,13 @@ func handlePacketICMP4(zone *omd.Zone, targetHosts []net.IP, hosts map[string]*o
return nil return nil
} }
h := &omd.Host{ h := omd.NewHost(
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4), zone,
Address: ip.String(), omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Mac: net.HardwareAddr(ethLayer.SrcMAC).String(), ip.String(),
Zone: zone, )
DiscoveredDate: omu.NowPtr(), h.Mac = ethLayer.SrcMAC.String()
} h.DiscoveredDate = omu.NowPtr()
hosts[ip.String()] = h hosts[ip.String()] = h

View File

@ -156,13 +156,13 @@ func handlePacketICMP6(zone *omd.Zone, targetHosts []net.IP, hosts map[string]*o
return nil return nil
} }
h := &omd.Host{ h := omd.NewHost(
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV6), zone,
Address: ip.String(), omm.ToMetaIPType(omm.MetaIPTypeEnumV6),
Mac: net.HardwareAddr(ethLayer.SrcMAC).String(), ip.String(),
Zone: zone, )
DiscoveredDate: omu.NowPtr(), h.Mac = ethLayer.SrcMAC.String()
} h.DiscoveredDate = omu.NowPtr()
hosts[ip.String()] = h hosts[ip.String()] = h

View File

@ -2,10 +2,8 @@ package mdns
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"log" "log"
"strconv"
"strings" "strings"
"sync/atomic" "sync/atomic"
"time" "time"
@ -66,15 +64,17 @@ 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( h := omd.NewHost(
discoverySession.Zone(),
omm.ToMetaIPType(metaIPTypeEnum),
ipv4.String(),
)
h.Name = hostName
h.DiscoveredDate = omu.NowPtr()
h = discoverySession.AddHost(
"mDNS", "mDNS",
&omd.Host{ h,
MetaIPType: omm.ToMetaIPType(metaIPTypeEnum),
Name: hostName,
Address: ipv4.String(),
Zone: discoverySession.Zone(),
DiscoveredDate: omu.NowPtr(),
},
meta, meta,
) )
@ -82,41 +82,47 @@ SERVICE_LOOP:
continue ENTRY_LOOP continue ENTRY_LOOP
} }
p := discoverySession.AddPort( p := omd.NewPort(
h,
metaPortType,
port,
)
p.DiscoveredDate = omu.NowPtr()
p = discoverySession.AddPort(
"mDNS", "mDNS",
&omd.Port{ p,
MetaPortType: metaPortType,
PortNumber: json.Number(strconv.Itoa(port)),
Host: h,
DiscoveredDate: omu.NowPtr(),
},
meta, meta,
) )
s := omd.NewService(
p,
metaCryptoType,
serviceName,
)
s.Name = name
s.DiscoveredDate = omu.NowPtr()
discoverySession.AddService( discoverySession.AddService(
"mDNS", "mDNS",
&omd.Service{ s,
MetaCryptoType: metaCryptoType,
Key: serviceName,
Name: name,
Port: p,
DiscoveredDate: omu.NowPtr(),
},
meta, meta,
) )
} }
case omm.MetaIPTypeEnumV6: case omm.MetaIPTypeEnumV6:
for _, ipv6 := range entry.AddrIPv6 { for _, ipv6 := range entry.AddrIPv6 {
h := discoverySession.AddHost( h := omd.NewHost(
discoverySession.Zone(),
omm.ToMetaIPType(metaIPTypeEnum),
ipv6.String(),
)
h.Name = hostName
h.DiscoveredDate = omu.NowPtr()
h = discoverySession.AddHost(
"mDNS", "mDNS",
&omd.Host{ h,
MetaIPType: omm.ToMetaIPType(metaIPTypeEnum),
Name: hostName,
Address: ipv6.String(),
Zone: discoverySession.Zone(),
DiscoveredDate: omu.NowPtr(),
},
meta, meta,
) )
@ -124,26 +130,30 @@ SERVICE_LOOP:
continue ENTRY_LOOP continue ENTRY_LOOP
} }
p := discoverySession.AddPort( p := omd.NewPort(
h,
metaPortType,
port,
)
p.DiscoveredDate = omu.NowPtr()
p = discoverySession.AddPort(
"mDNS", "mDNS",
&omd.Port{ p,
MetaPortType: metaPortType,
PortNumber: json.Number(strconv.Itoa(port)),
Host: h,
DiscoveredDate: omu.NowPtr(),
},
meta, meta,
) )
s := omd.NewService(
p,
metaCryptoType,
serviceName,
)
s.Name = name
s.DiscoveredDate = omu.NowPtr()
discoverySession.AddService( discoverySession.AddService(
"mDNS", "mDNS",
&omd.Service{ s,
MetaCryptoType: metaCryptoType,
Key: serviceName,
Name: name,
Port: p,
DiscoveredDate: omu.NowPtr(),
},
meta, meta,
) )
} }

View File

@ -2,7 +2,7 @@ package snmp
import ( import (
"encoding/json" "encoding/json"
"fmt" "log"
"net" "net"
"strconv" "strconv"
"sync" "sync"
@ -12,6 +12,7 @@ import (
omd "git.loafle.net/overflow/model/discovery" omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta" omm "git.loafle.net/overflow/model/meta"
omu "git.loafle.net/overflow/model/util" omu "git.loafle.net/overflow/model/util"
ouej "git.loafle.net/overflow/util-go/encoding/json"
"git.loafle.net/overflow_scanner/probe/discovery/session" "git.loafle.net/overflow_scanner/probe/discovery/session"
"github.com/k-sone/snmpgo" "github.com/k-sone/snmpgo"
) )
@ -94,8 +95,12 @@ func Scan(discoverySession session.DiscoverySession) error {
} }
func scanV2(target net.IP, discoverySession session.DiscoverySession, credential *omcc.SNMPCredential) bool { func scanV2(target net.IP, discoverySession session.DiscoverySession, credential *omcc.SNMPCredential) bool {
portNumber, err := ouej.NumberToInt(credential.Port)
address := fmt.Sprintf("%s:%s", target.String(), credential.Port.String()) if nil != err {
log.Print(err)
return false
}
address := net.JoinHostPort(target.String(), credential.Port.String())
timeout, _ := credential.Timeout.Int64() timeout, _ := credential.Timeout.Int64()
snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{ snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{
Version: snmpgo.V2c, Version: snmpgo.V2c,
@ -138,40 +143,47 @@ 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( h := omd.NewHost(
discoverySession.Zone(),
discoverySession.Zone().MetaIPType,
target.String(),
)
h.DiscoveredDate = omu.NowPtr()
h = discoverySession.AddHost(
"SNMP V2c", "SNMP V2c",
&omd.Host{ h,
MetaIPType: discoverySession.Zone().MetaIPType,
Name: "",
Address: target.String(),
Zone: discoverySession.Zone(),
DiscoveredDate: omu.NowPtr(),
},
meta, meta,
) )
p := discoverySession.AddPort("SNMP V2c", p := omd.NewPort(
&omd.Port{ h,
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumUDP), omm.ToMetaPortType(omm.MetaPortTypeEnumUDP),
PortNumber: credential.Port, portNumber,
Host: h, )
DiscoveredDate: omu.NowPtr(), p.DiscoveredDate = omu.NowPtr()
},
p = discoverySession.AddPort(
"SNMP V2c",
p,
meta, meta,
) )
discoverySession.AddService("SNMP V2c", s := omd.NewService(
&omd.Service{ p,
MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE), omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
Key: "SNMP", "SNMP",
Name: "SNMP V2c", )
Port: p, s.Name = "SNMP V2c"
DiscoveredDate: omu.NowPtr(), s.ServiceType = omm.MetaServiceTypeEnumMonitoring.String()
}, s.ServiceVersion = "2c"
s.DiscoveredDate = omu.NowPtr()
discoverySession.AddService(
"SNMP V2c",
s,
meta, meta,
) )
// log.Printf("Host: %v, Port: %v, Service: %v", h, p, s)
return true return true
} }

View File

@ -2,7 +2,6 @@ package connection
import ( import (
"context" "context"
"encoding/json"
"net" "net"
"os/exec" "os/exec"
"strconv" "strconv"
@ -93,12 +92,12 @@ func tryConnect(discoverySession session.DiscoverySession, ports map[int]*omd.Po
return return
} }
p := &omd.Port{ p := omd.NewPort(
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP), targetHost,
PortNumber: json.Number(strconv.Itoa(port)), omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
DiscoveredDate: omu.NowPtr(), port,
} )
p.Host = targetHost p.DiscoveredDate = omu.NowPtr()
ports[port] = p ports[port] = p

View File

@ -1,11 +1,9 @@
package syn package syn
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"net" "net"
"strconv"
"sync/atomic" "sync/atomic"
"time" "time"
@ -129,12 +127,12 @@ func handlePacketTCP4(discoverySession session.DiscoverySession, host *omd.Host,
} }
// olog.Logger().Debug("Discovery", zap.String("ip", host.Address), zap.Int("port", port)) // olog.Logger().Debug("Discovery", zap.String("ip", host.Address), zap.Int("port", port))
p := &omd.Port{ p := omd.NewPort(
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP), host,
PortNumber: json.Number(strconv.Itoa(port)), omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
DiscoveredDate: omu.NowPtr(), port,
} )
p.Host = host p.DiscoveredDate = omu.NowPtr()
ports[port] = p ports[port] = p

View File

@ -2,7 +2,6 @@ package connection
import ( import (
"context" "context"
"encoding/json"
"net" "net"
"os/exec" "os/exec"
"strconv" "strconv"
@ -93,12 +92,12 @@ func tryConnect(discoverySession session.DiscoverySession, ports map[int]*omd.Po
return return
} }
p := &omd.Port{ p := omd.NewPort(
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP), targetHost,
PortNumber: json.Number(strconv.Itoa(port)), omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
DiscoveredDate: omu.NowPtr(), port,
} )
p.Host = targetHost p.DiscoveredDate = omu.NowPtr()
ports[port] = p ports[port] = p

View File

@ -1,11 +1,9 @@
package matcher package matcher
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"net" "net"
"strconv"
"sync/atomic" "sync/atomic"
"time" "time"
@ -145,13 +143,14 @@ func handlePacketUDP4(discoverySession session.DiscoverySession, host *omd.Host,
return nil return nil
} }
p := &omd.Port{ p := omd.NewPort(
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumUDP), host,
PortNumber: json.Number(strconv.Itoa(port)), omm.ToMetaPortType(omm.MetaPortTypeEnumUDP),
UDPLayer: udpLayer, port,
DiscoveredDate: omu.NowPtr(), )
} p.DiscoveredDate = omu.NowPtr()
p.Host = host p.UDPLayer = udpLayer
ports[port] = p ports[port] = p
return p return p

View File

@ -28,15 +28,17 @@ LOOP:
continue LOOP continue LOOP
} }
h := omd.NewHost(
discoverySession.Zone(),
discoverySession.Zone().MetaIPType,
rd.PresentationURL.URL.Host,
)
h.Name = rd.FriendlyName
h.DiscoveredDate = omu.NowPtr()
discoverySession.AddHost( discoverySession.AddHost(
"UPnP", "UPnP",
&omd.Host{ h,
MetaIPType: discoverySession.Zone().MetaIPType,
Name: rd.FriendlyName,
Address: rd.PresentationURL.URL.Host,
Zone: discoverySession.Zone(),
DiscoveredDate: omu.NowPtr(),
},
map[string]string{ map[string]string{
"DeviceType": rd.DeviceType, "DeviceType": rd.DeviceType,
"Manufacturer": rd.Manufacturer, "Manufacturer": rd.Manufacturer,

View File

@ -181,6 +181,7 @@ func (ds *ofDiscoverySession) InitWithRequest(request types.DiscoveryRequest) er
ds.pCapScanner = _pCapScanner ds.pCapScanner = _pCapScanner
log.Print("Privileged mode") log.Print("Privileged mode")
} else { } else {
log.Print(err)
log.Print("Unprivileged mode") log.Print("Unprivileged mode")
} }
@ -243,18 +244,18 @@ func (ds *ofDiscoverySession) AddHost(discoveredBy string, host *omd.Host, meta
modified = true modified = true
} }
if "HOST" == h.DeviceType && "" != host.DeviceType { if omd.DefaultHostType == h.HostType && "" != host.HostType {
h.DeviceType = host.DeviceType h.HostType = host.HostType
modified = true modified = true
} }
if h.DeviceVendor != host.DeviceVendor { if omd.DefaultHostVendor == h.HostVendor && "" != host.HostVendor {
h.DeviceVendor = host.DeviceVendor h.HostVendor = host.HostVendor
modified = true modified = true
} }
if h.DeviceModel != host.DeviceModel { if omd.DefaultHostModel == h.HostModel && "" != host.HostModel {
h.DeviceModel = host.DeviceModel h.HostModel = host.HostModel
modified = true modified = true
} }
@ -313,6 +314,21 @@ func (ds *ofDiscoverySession) AddService(discoveredBy string, service *omd.Servi
s, modified := ds.findService(service) s, modified := ds.findService(service)
if omd.DefaultServiceType == s.ServiceType && "" != service.ServiceType {
s.ServiceType = service.ServiceType
modified = true
}
if omd.DefaultServiceVendor == s.ServiceVendor && "" != service.ServiceVendor {
s.ServiceVendor = service.ServiceVendor
modified = true
}
if omd.DefaultServiceVersion == s.ServiceVersion && "" != service.ServiceVersion {
s.ServiceVersion = service.ServiceVersion
modified = true
}
discoveredBys, discoveredByModified := ds.appendDiscoveredBy(discoveredBy, s.DiscoveredBy) discoveredBys, discoveredByModified := ds.appendDiscoveredBy(discoveredBy, s.DiscoveredBy)
if discoveredByModified { if discoveredByModified {
s.DiscoveredBy = discoveredBys s.DiscoveredBy = discoveredBys
@ -422,11 +438,11 @@ func (ds *ofDiscoverySession) findHost(host *omd.Host) (h *omd.Host, modified bo
modified = false modified = false
var ok bool var ok bool
if "" == host.DeviceType { if "" == host.HostType {
host.DeviceType = "HOST" host.HostType = omd.DefaultHostType
} }
if "" == host.OsType { if "" == host.OsType {
host.OsType = "UNKNOWN" host.OsType = omd.DefaultOsType
} }
h, ok = ds.hosts[host.Address] h, ok = ds.hosts[host.Address]
@ -535,15 +551,15 @@ func (ds *ofDiscoverySession) addtionalHostMDNS(host *omd.Host, meta map[string]
_vendor, ok := meta["vendor"] _vendor, ok := meta["vendor"]
if ok || "" != _vendor { if ok || "" != _vendor {
h.DeviceVendor = _vendor h.HostVendor = _vendor
modified = true modified = true
} }
_model, ok := meta["model"] _model, ok := meta["model"]
if ok || "" != _model { if ok || "" != _model {
h.DeviceModel = _model h.HostModel = _model
if "Synology" == _vendor && strings.Contains(_model, "DS1817+") { if "Synology" == _vendor && strings.Contains(_model, "DS1817+") {
h.DeviceType = "NAS" h.HostType = "NAS"
} }
modified = true modified = true
} }
@ -590,16 +606,16 @@ func (ds *ofDiscoverySession) addtionalServiceMDNS(service *omd.Service, meta ma
case 515: case 515:
if "printer" == service.Key { if "printer" == service.Key {
_h := service.Port.Host _h := service.Port.Host
_h.DeviceType = "PRINTER" _h.HostType = "PRINTER"
_h.Name = service.Name _h.Name = service.Name
ds.AddHost("mDNS", _h, nil) ds.delegate(_h)
} }
case 9100: case 9100:
if "pdl-datastream" == service.Key { if "pdl-datastream" == service.Key {
_h := service.Port.Host _h := service.Port.Host
_h.DeviceType = "PRINTER" _h.HostType = "PRINTER"
_h.Name = service.Name _h.Name = service.Name
ds.AddHost("mDNS", _h, nil) ds.delegate(_h)
} }
} }

View File

@ -77,16 +77,20 @@ LOOP:
} }
if nil != discoveredMatcher { if nil != discoveredMatcher {
// log.Printf("discovered matcher: %s(%s) %v", discoveredMatcher.Name(), discoveredMatcher.Key(), discoveredMatcher) s := omd.NewService(
go discoverySession.AddService( targetPort,
discoveredConnector.metaCryptoType(),
discoveredMatcher.Key(),
)
s.Name = discoveredMatcher.Name(matchCtx)
s.ServiceType = discoveredMatcher.Type()
s.ServiceVendor = discoveredMatcher.Vendor(matchCtx)
s.ServiceVersion = discoveredMatcher.Version(matchCtx)
s.DiscoveredDate = omu.NowPtr()
discoverySession.AddService(
"Service Matcher", "Service Matcher",
&omd.Service{ s,
MetaCryptoType: discoveredConnector.metaCryptoType(),
Key: discoveredMatcher.Key(),
Name: discoveredMatcher.Name(matchCtx),
DiscoveredDate: omu.NowPtr(),
Port: targetPort,
},
matchCtx.GetAttributes(), matchCtx.GetAttributes(),
) )
} }

View File

@ -25,15 +25,22 @@ func scanUDP(discoverySession session.DiscoverySession, targetPort *omd.Port) er
p := osm.NewPacket(targetPort.UDPLayer.LayerPayload(), len(targetPort.UDPLayer.LayerPayload())) p := osm.NewPacket(targetPort.UDPLayer.LayerPayload(), len(targetPort.UDPLayer.LayerPayload()))
if err := _matcher.Match(matchCtx, 0, p); err == nil { if err := _matcher.Match(matchCtx, 0, p); err == nil {
s := &omd.Service{ s := omd.NewService(
Key: _matcher.Key(), targetPort,
Name: _matcher.Name(matchCtx), omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
Port: targetPort, _matcher.Key(),
MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE), )
DiscoveredDate: omu.NowPtr(), s.Name = _matcher.Name(matchCtx)
} s.ServiceType = _matcher.Type()
s.ServiceVendor = _matcher.Vendor(matchCtx)
s.ServiceVersion = _matcher.Version(matchCtx)
s.DiscoveredDate = omu.NowPtr()
go discoverySession.AddService("Service Matcher", s, matchCtx.GetAttributes()) discoverySession.AddService(
"Service Matcher",
s,
matchCtx.GetAttributes(),
)
return nil return nil
} }