This commit is contained in:
crusader 2018-09-03 19:33:20 +09:00
parent bf7500671e
commit dd8ccb2069
16 changed files with 110 additions and 44 deletions

View File

@ -8,20 +8,20 @@ import (
omm "git.loafle.net/overflow/model/meta" omm "git.loafle.net/overflow/model/meta"
) )
// const (
// ZONE_NETWORK = "192.168.1"
// ZONE_IFACE = "enp3s0"
// ZONE_ADDRESS = "101"
// ZONE_MAC = "44:8a:5b:f1:f1:f3"
// )
const ( const (
ZONE_NETWORK = "192.168.1" ZONE_NETWORK = "192.168.1"
ZONE_IFACE = "\\Device\\NPF_{1924FA2B-6927-4BA5-AF43-876C3F8853CE}" ZONE_IFACE = "enp3s0"
ZONE_ADDRESS = "103" ZONE_ADDRESS = "101"
ZONE_MAC = "30:9C:23:15:A3:09" ZONE_MAC = "44:8a:5b:f1:f1:f3"
) )
// const (
// ZONE_NETWORK = "192.168.1"
// ZONE_IFACE = "\\Device\\NPF_{1924FA2B-6927-4BA5-AF43-876C3F8853CE}"
// ZONE_ADDRESS = "103"
// ZONE_MAC = "30:9C:23:15:A3:09"
// )
func Zone() *omd.Zone { func Zone() *omd.Zone {
return &omd.Zone{ return &omd.Zone{
Network: fmt.Sprintf("%s.0/24", ZONE_NETWORK), Network: fmt.Sprintf("%s.0/24", ZONE_NETWORK),

View File

@ -5,8 +5,10 @@ import (
"reflect" "reflect"
"sync" "sync"
"testing" "testing"
"time"
omd "git.loafle.net/overflow/model/discovery" omd "git.loafle.net/overflow/model/discovery"
oub "git.loafle.net/overflow/util-go/benchmark"
"git.loafle.net/overflow_scanner/probe/__test" "git.loafle.net/overflow_scanner/probe/__test"
"git.loafle.net/overflow_scanner/probe/discovery/session" "git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/discovery/types" "git.loafle.net/overflow_scanner/probe/discovery/types"
@ -38,6 +40,10 @@ func TestInstance(t *testing.T) {
} }
func Test_ofDiscoverer_DiscoverHost(t *testing.T) { func Test_ofDiscoverer_DiscoverHost(t *testing.T) {
defer func(fnc func() time.Duration) {
log.Print(fnc())
}(oub.Elapsed())
i := Instance() i := Instance()
defer i.Shutdown() defer i.Shutdown()
@ -54,7 +60,7 @@ func Test_ofDiscoverer_DiscoverHost(t *testing.T) {
case types.DiscoveryMessageTypeHost: case types.DiscoveryMessageTypeHost:
log.Print("Discovered Host: ", msg.Data()) log.Print("Discovered Host: ", msg.Data())
case types.DiscoveryMessageTypePort: case types.DiscoveryMessageTypePort:
log.Print("Discovered Port: ", msg.Data()) log.Print("Discovered Port: ", msg.Data(), " \n Host: ", msg.Data().(*omd.Port).Host)
case types.DiscoveryMessageTypeService: case types.DiscoveryMessageTypeService:
log.Print("Discovered Service: ", msg.Data(), " \n Port: ", msg.Data().(*omd.Service).Port, " \n Host: ", msg.Data().(*omd.Service).Port.Host) log.Print("Discovered Service: ", msg.Data(), " \n Port: ", msg.Data().(*omd.Service).Port, " \n Host: ", msg.Data().(*omd.Service).Port.Host)
case types.DiscoveryMessageTypeStart: case types.DiscoveryMessageTypeStart:

View File

@ -10,7 +10,6 @@ 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"
oub "git.loafle.net/overflow/util-go/benchmark"
"git.loafle.net/overflow_scanner/probe/discovery/session" "git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/internal/pcap" "git.loafle.net/overflow_scanner/probe/internal/pcap"
"github.com/google/gopacket" "github.com/google/gopacket"
@ -18,7 +17,6 @@ import (
) )
func Scan(discoverySession session.DiscoverySession) error { func Scan(discoverySession session.DiscoverySession) error {
oub.Elapsed("ARP")
targetHosts := discoverySession.TargetHosts() targetHosts := discoverySession.TargetHosts()
if nil == targetHosts || 0 == len(targetHosts) { if nil == targetHosts || 0 == len(targetHosts) {
@ -84,6 +82,8 @@ func Scan(discoverySession session.DiscoverySession) error {
select { select {
case <-timerStopped: case <-timerStopped:
return nil return nil
case <-discoverySession.StopChan():
return nil
} }
} }

View File

@ -84,6 +84,8 @@ func scanV4(discoverySession session.DiscoverySession) error {
select { select {
case <-timerStopped: case <-timerStopped:
return nil return nil
case <-discoverySession.StopChan():
return nil
} }
} }

View File

@ -83,6 +83,8 @@ func scanV6(discoverySession session.DiscoverySession) error {
select { select {
case <-timerStopped: case <-timerStopped:
return nil return nil
case <-discoverySession.StopChan():
return nil
} }
} }

View File

@ -13,13 +13,12 @@ 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"
oub "git.loafle.net/overflow/util-go/benchmark"
"git.loafle.net/overflow_scanner/probe/discovery/session" "git.loafle.net/overflow_scanner/probe/discovery/session"
"github.com/grandcat/zeroconf" "github.com/grandcat/zeroconf"
) )
func Scan(discoverySession session.DiscoverySession) error { func Scan(discoverySession session.DiscoverySession) error {
oub.Elapsed("mDNS")
serviceEntries, err := browse("_services._dns-sd._udp", "local") serviceEntries, err := browse("_services._dns-sd._udp", "local")
if nil != err { if nil != err {
log.Print("Cannot find service ", err) log.Print("Cannot find service ", err)
@ -127,6 +126,12 @@ SERVICE_LOOP:
} }
} }
select {
case <-discoverySession.StopChan():
return nil
default:
}
} }
return nil return nil

View File

@ -78,6 +78,12 @@ func Scan(discoverySession session.DiscoverySession) error {
} }
} }
}(target) }(target)
select {
case <-discoverySession.StopChan():
return
default:
}
} }
}() }()
} }

View File

@ -59,6 +59,8 @@ Loop:
select { select {
case <-timer.C: case <-timer.C:
case <-discoverySession.StopChan():
return nil
} }
} }
@ -74,6 +76,11 @@ func tryConnect(discoverySession session.DiscoverySession, ports map[int]*omd.Po
if err != nil { if err != nil {
if strings.Contains(err.Error(), "too many open files") { if strings.Contains(err.Error(), "too many open files") {
select {
case <-discoverySession.StopChan():
return
default:
}
time.Sleep(timeout) time.Sleep(timeout)
tryConnect(discoverySession, ports, targetHost, port, timeout) tryConnect(discoverySession, ports, targetHost, port, timeout)
} }

View File

@ -49,6 +49,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 {
discoverySession.AddPort(p) discoverySession.AddPort(p)
log.Print(p)
} }
case <-ticker.C: case <-ticker.C:
if false == delay.Load().(bool) { if false == delay.Load().(bool) {
@ -69,6 +70,8 @@ func scanV4(discoverySession session.DiscoverySession, targetHost *omd.Host) err
select { select {
case <-timerStopped: case <-timerStopped:
return nil return nil
case <-discoverySession.StopChan():
return nil
} }
} }

View File

@ -76,7 +76,7 @@ func Test_sendTCP4(t *testing.T) {
args args args args
wantErr bool wantErr bool
}{ }{
// TODO: Add test cases. // TODO: Add test cases.
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -99,7 +99,7 @@ func Test_handlePacketTCP4(t *testing.T) {
args args args args
want *omd.Port want *omd.Port
}{ }{
// TODO: Add test cases. // TODO: Add test cases.
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -120,7 +120,7 @@ func Test_makePacketPortTCP4(t *testing.T) {
want *PortPacketTCP4 want *PortPacketTCP4
wantErr bool wantErr bool
}{ }{
// TODO: Add test cases. // TODO: Add test cases.
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

View File

@ -59,6 +59,8 @@ Loop:
select { select {
case <-timer.C: case <-timer.C:
case <-discoverySession.StopChan():
return nil
} }
} }
@ -74,6 +76,11 @@ func tryConnect(discoverySession session.DiscoverySession, ports map[int]*omd.Po
if err != nil { if err != nil {
if strings.Contains(err.Error(), "too many open files") { if strings.Contains(err.Error(), "too many open files") {
select {
case <-discoverySession.StopChan():
return
default:
}
time.Sleep(timeout) time.Sleep(timeout)
tryConnect(discoverySession, ports, targetHost, port, timeout) tryConnect(discoverySession, ports, targetHost, port, timeout)
} }

View File

@ -70,6 +70,8 @@ func scanV4(discoverySession session.DiscoverySession, targetHost *omd.Host) err
select { select {
case <-timerStopped: case <-timerStopped:
return nil return nil
case <-discoverySession.StopChan():
return nil
} }
} }

View File

@ -41,6 +41,8 @@ type DiscoverySession interface {
DiscoveredAllPorts() map[*omd.Host]map[json.Number]map[string]*omd.Port DiscoveredAllPorts() map[*omd.Host]map[json.Number]map[string]*omd.Port
DiscoveredService(port *omd.Port, name string) map[string]*omd.Service DiscoveredService(port *omd.Port, name string) map[string]*omd.Service
DiscoveredAllServices() map[*omd.Port]map[string]map[string]*omd.Service DiscoveredAllServices() map[*omd.Port]map[string]map[string]*omd.Service
StopChan() <-chan struct{}
} }
type ofDiscoverySession struct { type ofDiscoverySession struct {
@ -65,6 +67,8 @@ type ofDiscoverySession struct {
includeMachosts map[string]*omd.Host includeMachosts map[string]*omd.Host
ports map[*omd.Host]map[json.Number]map[string]*omd.Port ports map[*omd.Host]map[json.Number]map[string]*omd.Port
services map[*omd.Port]map[string]map[string]*omd.Service services map[*omd.Port]map[string]map[string]*omd.Service
stopChan chan struct{}
} }
func (ds *ofDiscoverySession) init(request types.DiscoveryRequest) { func (ds *ofDiscoverySession) init(request types.DiscoveryRequest) {
@ -80,6 +84,8 @@ func (ds *ofDiscoverySession) init(request types.DiscoveryRequest) {
ds.includeMachosts = make(map[string]*omd.Host) ds.includeMachosts = make(map[string]*omd.Host)
ds.ports = make(map[*omd.Host]map[json.Number]map[string]*omd.Port) ds.ports = make(map[*omd.Host]map[json.Number]map[string]*omd.Port)
ds.services = make(map[*omd.Port]map[string]map[string]*omd.Service) ds.services = make(map[*omd.Port]map[string]map[string]*omd.Service)
ds.stopChan = make(chan struct{})
} }
func (ds *ofDiscoverySession) InitWithRequest(request types.DiscoveryRequest) error { func (ds *ofDiscoverySession) InitWithRequest(request types.DiscoveryRequest) error {
@ -351,6 +357,10 @@ func (ds *ofDiscoverySession) findHost(host *omd.Host) (h *omd.Host, modified bo
return return
} }
func (ds *ofDiscoverySession) StopChan() <-chan struct{} {
return ds.stopChan
}
func (ds *ofDiscoverySession) findPort(port *omd.Port) (p *omd.Port, modified bool) { func (ds *ofDiscoverySession) findPort(port *omd.Port) (p *omd.Port, modified bool) {
modified = false modified = false
var ok bool var ok bool
@ -556,6 +566,8 @@ func RetainDiscoverySession() *ofDiscoverySession {
} }
func ReleaseDiscoverySession(ds *ofDiscoverySession) { func ReleaseDiscoverySession(ds *ofDiscoverySession) {
close(ds.stopChan)
ds.discoveryRequest = nil ds.discoveryRequest = nil
ds.zone = nil ds.zone = nil
ds.host = nil ds.host = nil
@ -564,6 +576,7 @@ func ReleaseDiscoverySession(ds *ofDiscoverySession) {
ds.discoverPort = nil ds.discoverPort = nil
ds.discoverService = nil ds.discoverService = nil
ds.includeMachosts = nil
ds.hosts = nil ds.hosts = nil
ds.ports = nil ds.ports = nil
ds.services = nil ds.services = nil

View File

@ -2,7 +2,6 @@ package service
import ( import (
"fmt" "fmt"
"log"
"net" "net"
"time" "time"
@ -20,6 +19,9 @@ func scanTCP(discoverySession session.DiscoverySession, targetPort *omd.Port) er
if err != nil { if err != nil {
return fmt.Errorf("Service scan on %s:%s error has occurred %v ", hostAddress, targetPort.PortNumber, err) return fmt.Errorf("Service scan on %s:%s error has occurred %v ", hostAddress, targetPort.PortNumber, err)
} }
if 9100 == portNumber {
return nil
}
info := osm.NewMatchInfo(hostAddress, portNumber) info := osm.NewMatchInfo(hostAddress, portNumber)
connectors := newConnectors() connectors := newConnectors()
@ -61,11 +63,17 @@ LOOP:
break LOOP break LOOP
} }
select {
case <-discoverySession.StopChan():
return nil
default:
}
} }
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)
s := discoverySession.AddService(&omd.Service{ discoverySession.AddService(&omd.Service{
MetaCryptoType: discoveredConnector.metaCryptoType(), MetaCryptoType: discoveredConnector.metaCryptoType(),
Key: discoveredMatcher.Key(), Key: discoveredMatcher.Key(),
Name: discoveredMatcher.Name(), Name: discoveredMatcher.Name(),
@ -73,7 +81,6 @@ LOOP:
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
Port: targetPort, Port: targetPort,
}) })
log.Print("Discovered Service: ", s, " \n Port: ", s.Port, " \n Host: ", s.Port.Host)
} }
return nil return nil
@ -181,7 +188,7 @@ LOOP:
break INNER_LOOP break INNER_LOOP
} }
log.Printf("res: %s", string(buf[:n])) // log.Printf("res: %s", string(buf[:n]))
if err := matcher.Match(info, j+1, osm.NewPacket(buf, n)); err == nil { if err := matcher.Match(info, j+1, osm.NewPacket(buf, n)); err == nil {
if packetCount-1 == j { if packetCount-1 == j {

View File

@ -36,8 +36,8 @@ func Test_scanTCP(t *testing.T) {
targetHost := __test.Host( targetHost := __test.Host(
"atGame", "atGame",
"1", "99",
"00:11:32:7f:20:61", "00:25:b3:fa:ca:9b",
) )
type args struct { type args struct {
@ -49,29 +49,29 @@ func Test_scanTCP(t *testing.T) {
args args args args
wantErr bool wantErr bool
}{ }{
// {
// name: "80",
// args: args{
// discoverySession: s,
// targetPort: __test.Port(
// targetHost,
// "80",
// ),
// },
// wantErr: false,
// },
{ {
name: "139", name: "80",
args: args{ args: args{
discoverySession: s, discoverySession: s,
targetPort: __test.Port( targetPort: __test.Port(
targetHost, targetHost,
"139", "9100",
), ),
}, },
wantErr: false, wantErr: false,
}, },
// { // {
// name: "139",
// args: args{
// discoverySession: s,
// targetPort: __test.Port(
// targetHost,
// "139",
// ),
// },
// wantErr: false,
// },
// {
// name: "443", // name: "443",
// args: args{ // args: args{
// discoverySession: s, // discoverySession: s,

View File

@ -18,27 +18,33 @@ func scanUDP(discoverySession session.DiscoverySession, targetPort *omd.Port) er
return fmt.Errorf("Service scan port[%s] error %v ", targetPort.PortNumber, err) return fmt.Errorf("Service scan port[%s] error %v ", targetPort.PortNumber, err)
} }
ms := matcher.GetUDPMatchers() matchers := matcher.GetUDPMatchers()
mi := osm.NewMatchInfo(targetPort.Host.Address, portNumber) mi := osm.NewMatchInfo(targetPort.Host.Address, portNumber)
for i := 0; i < len(ms); i++ { for _, _matcher := range matchers {
m := ms[i]
p := osm.NewPacket(targetPort.UDPLayer.LayerPayload(), len(targetPort.UDPLayer.LayerPayload())) p := osm.NewPacket(targetPort.UDPLayer.LayerPayload(), len(targetPort.UDPLayer.LayerPayload()))
if err := m.Match(mi, 0, p); err == nil { if err := _matcher.Match(mi, 0, p); err == nil {
s := &omd.Service{ s := &omd.Service{
Key: m.Key(), Key: _matcher.Key(),
Port: targetPort, Port: targetPort,
MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE), MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
Metadata: m.Meta(), Metadata: _matcher.Meta(),
Name: m.Name(), Name: _matcher.Name(),
} }
discoverySession.AddService(s) discoverySession.AddService(s)
return nil return nil
} }
select {
case <-discoverySession.StopChan():
return nil
default:
}
} }
return nil return nil