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"
)
// const (
// ZONE_NETWORK = "192.168.1"
// ZONE_IFACE = "enp3s0"
// ZONE_ADDRESS = "101"
// 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"
ZONE_IFACE = "enp3s0"
ZONE_ADDRESS = "101"
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 {
return &omd.Zone{
Network: fmt.Sprintf("%s.0/24", ZONE_NETWORK),

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -59,6 +59,8 @@ Loop:
select {
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 strings.Contains(err.Error(), "too many open files") {
select {
case <-discoverySession.StopChan():
return
default:
}
time.Sleep(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)
if p := handlePacketTCP4(discoverySession, targetHost, ports, packet); nil != p {
discoverySession.AddPort(p)
log.Print(p)
}
case <-ticker.C:
if false == delay.Load().(bool) {
@ -69,6 +70,8 @@ func scanV4(discoverySession session.DiscoverySession, targetHost *omd.Host) err
select {
case <-timerStopped:
return nil
case <-discoverySession.StopChan():
return nil
}
}

View File

@ -59,6 +59,8 @@ Loop:
select {
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 strings.Contains(err.Error(), "too many open files") {
select {
case <-discoverySession.StopChan():
return
default:
}
time.Sleep(timeout)
tryConnect(discoverySession, ports, targetHost, port, timeout)
}

View File

@ -70,6 +70,8 @@ func scanV4(discoverySession session.DiscoverySession, targetHost *omd.Host) err
select {
case <-timerStopped:
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
DiscoveredService(port *omd.Port, name string) map[string]*omd.Service
DiscoveredAllServices() map[*omd.Port]map[string]map[string]*omd.Service
StopChan() <-chan struct{}
}
type ofDiscoverySession struct {
@ -65,6 +67,8 @@ type ofDiscoverySession struct {
includeMachosts map[string]*omd.Host
ports map[*omd.Host]map[json.Number]map[string]*omd.Port
services map[*omd.Port]map[string]map[string]*omd.Service
stopChan chan struct{}
}
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.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.stopChan = make(chan struct{})
}
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
}
func (ds *ofDiscoverySession) StopChan() <-chan struct{} {
return ds.stopChan
}
func (ds *ofDiscoverySession) findPort(port *omd.Port) (p *omd.Port, modified bool) {
modified = false
var ok bool
@ -556,6 +566,8 @@ func RetainDiscoverySession() *ofDiscoverySession {
}
func ReleaseDiscoverySession(ds *ofDiscoverySession) {
close(ds.stopChan)
ds.discoveryRequest = nil
ds.zone = nil
ds.host = nil
@ -564,6 +576,7 @@ func ReleaseDiscoverySession(ds *ofDiscoverySession) {
ds.discoverPort = nil
ds.discoverService = nil
ds.includeMachosts = nil
ds.hosts = nil
ds.ports = nil
ds.services = nil

View File

@ -2,7 +2,6 @@ package service
import (
"fmt"
"log"
"net"
"time"
@ -20,6 +19,9 @@ func scanTCP(discoverySession session.DiscoverySession, targetPort *omd.Port) er
if err != nil {
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)
connectors := newConnectors()
@ -61,11 +63,17 @@ LOOP:
break LOOP
}
select {
case <-discoverySession.StopChan():
return nil
default:
}
}
if nil != 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(),
Key: discoveredMatcher.Key(),
Name: discoveredMatcher.Name(),
@ -73,7 +81,6 @@ LOOP:
DiscoveredDate: omu.NowPtr(),
Port: targetPort,
})
log.Print("Discovered Service: ", s, " \n Port: ", s.Port, " \n Host: ", s.Port.Host)
}
return nil
@ -181,7 +188,7 @@ 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 packetCount-1 == j {

View File

@ -36,8 +36,8 @@ func Test_scanTCP(t *testing.T) {
targetHost := __test.Host(
"atGame",
"1",
"00:11:32:7f:20:61",
"99",
"00:25:b3:fa:ca:9b",
)
type args struct {
@ -49,29 +49,29 @@ func Test_scanTCP(t *testing.T) {
args args
wantErr bool
}{
// {
// name: "80",
// args: args{
// discoverySession: s,
// targetPort: __test.Port(
// targetHost,
// "80",
// ),
// },
// wantErr: false,
// },
{
name: "139",
name: "80",
args: args{
discoverySession: s,
targetPort: __test.Port(
targetHost,
"139",
"9100",
),
},
wantErr: false,
},
// {
// name: "139",
// args: args{
// discoverySession: s,
// targetPort: __test.Port(
// targetHost,
// "139",
// ),
// },
// wantErr: false,
// },
// {
// name: "443",
// args: args{
// 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)
}
ms := matcher.GetUDPMatchers()
matchers := matcher.GetUDPMatchers()
mi := osm.NewMatchInfo(targetPort.Host.Address, portNumber)
for i := 0; i < len(ms); i++ {
m := ms[i]
for _, _matcher := range matchers {
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{
Key: m.Key(),
Key: _matcher.Key(),
Port: targetPort,
MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
DiscoveredDate: omu.NowPtr(),
Metadata: m.Meta(),
Name: m.Name(),
Metadata: _matcher.Meta(),
Name: _matcher.Name(),
}
discoverySession.AddService(s)
return nil
}
select {
case <-discoverySession.StopChan():
return nil
default:
}
}
return nil