This commit is contained in:
insanity 2018-08-13 16:19:59 +09:00
parent 6446e982f2
commit c98c75787b
22 changed files with 1522 additions and 54 deletions

293
discoverer/discoverer.go Normal file
View File

@ -0,0 +1,293 @@
package discoverer
import (
"sync"
"git.loafle.net/commons/util-go/net/cidr"
omd "git.loafle.net/overflow/model/discovery"
"git.loafle.net/overflow/model/util"
)
type DiscoveryDataType int
const (
DiscoveryDataTypeNone DiscoveryDataType = iota
DiscoveryDataTypeStart
DiscoveryDataTypeStop
DiscoveryDataTypeError
DiscoveryDataTypeZone
DiscoveryDataTypeHost
DiscoveryDataTypePort
DiscoveryDataTypeService
)
type DiscoveryData struct {
Type DiscoveryDataType
Result interface{}
Error error
}
func (dd *DiscoveryData) Release() {
releaseDiscoveryData(dd)
}
var _discoverer Discoverer
func GetDiscoverer() Discoverer {
if nil == _discoverer {
_discoverer = &defaultDiscoverer{}
}
return _discoverer
}
type Discoverer interface {
Retain() chan *DiscoveryData
Release(dataChan chan *DiscoveryData)
Stop()
DiscoverZone(dataChan chan *DiscoveryData, dz *omd.DiscoverZone)
DiscoverHost(dataChan chan *DiscoveryData, zone *omd.Zone, dh *omd.DiscoverHost)
DiscoverPort(dataChan chan *DiscoveryData, host *omd.Host, dp *omd.DiscoverPort)
DiscoverSerice(dataChan chan *DiscoveryData, port *omd.Port, ds *omd.DiscoverService)
}
type defaultDiscoverer struct {
dataChanPool chan chan *DiscoveryData
stopChan chan struct{}
}
func (d *defaultDiscoverer) Retain() chan *DiscoveryData {
if nil == d.dataChanPool {
d.dataChanPool = make(chan chan *DiscoveryData, 1)
d.dataChanPool <- make(chan *DiscoveryData, 256)
}
return <-d.dataChanPool
}
func (d *defaultDiscoverer) Release(dataChan chan *DiscoveryData) {
d.dataChanPool <- dataChan
}
func (d *defaultDiscoverer) Stop() {
if nil != d.stopChan {
close(d.stopChan)
d.stopChan = nil
}
}
func (d *defaultDiscoverer) DiscoverZone(dataChan chan *DiscoveryData, dz *omd.DiscoverZone) {
var wg sync.WaitGroup
d.stopChan = make(chan struct{})
dataChan <- retainDiscoveryData(DiscoveryDataTypeStart, util.Now(), nil, nil)
wg.Add(1)
go d.innerDiscoverZone(&wg, dataChan, dz)
wg.Wait()
if nil != d.stopChan {
close(d.stopChan)
d.stopChan = nil
}
dataChan <- retainDiscoveryData(DiscoveryDataTypeStop, util.Now(), nil, nil)
}
func (d *defaultDiscoverer) DiscoverHost(dataChan chan *DiscoveryData, zone *omd.Zone, dh *omd.DiscoverHost) {
var wg sync.WaitGroup
d.stopChan = make(chan struct{})
dataChan <- retainDiscoveryData(DiscoveryDataTypeStart, util.Now(), nil, nil)
wg.Add(1)
go d.innerDiscoverHost(&wg, dataChan, zone, dh)
wg.Wait()
if nil != d.stopChan {
close(d.stopChan)
d.stopChan = nil
}
dataChan <- retainDiscoveryData(DiscoveryDataTypeStop, util.Now(), nil, nil)
}
func (d *defaultDiscoverer) DiscoverPort(dataChan chan *DiscoveryData, host *omd.Host, dp *omd.DiscoverPort) {
var wg sync.WaitGroup
d.stopChan = make(chan struct{})
dataChan <- retainDiscoveryData(DiscoveryDataTypeStart, util.Now(), nil, nil)
wg.Add(1)
go d.innerDiscoverPort(&wg, dataChan, host, dp)
wg.Wait()
if nil != d.stopChan {
close(d.stopChan)
d.stopChan = nil
}
dataChan <- retainDiscoveryData(DiscoveryDataTypeStop, util.Now(), nil, nil)
}
func (d *defaultDiscoverer) DiscoverSerice(dataChan chan *DiscoveryData, port *omd.Port, ds *omd.DiscoverService) {
var wg sync.WaitGroup
d.stopChan = make(chan struct{})
dataChan <- retainDiscoveryData(DiscoveryDataTypeStart, util.Now(), nil, nil)
wg.Add(1)
go d.innerDiscoverSerice(&wg, dataChan, port, ds)
wg.Wait()
if nil != d.stopChan {
close(d.stopChan)
d.stopChan = nil
}
dataChan <- retainDiscoveryData(DiscoveryDataTypeStop, util.Now(), nil, nil)
}
func (d *defaultDiscoverer) innerDiscoverZone(wg *sync.WaitGroup, dataChan chan *DiscoveryData, dz *omd.DiscoverZone) {
defer func() {
wg.Done()
}()
taskScan(d,
func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}, stopChan chan struct{}) {
scanZone(dz, resultChan, errChan, doneChan, stopChan)
},
func(result interface{}) {
z := result.(*omd.Zone)
dataChan <- retainDiscoveryData(DiscoveryDataTypeZone, util.Now(), z, nil)
if nil != dz.DiscoverHost {
cr, _ := cidr.NewCIDRRanger(z.Network)
dh := &omd.DiscoverHost{
MetaIPType: z.MetaIPType,
FirstScanRange: cr.First().String(),
LastScanRange: cr.Last().String(),
DiscoverPort: dz.DiscoverHost.DiscoverPort,
}
wg.Add(1)
go d.innerDiscoverHost(wg, dataChan, z, dh)
}
},
func(err error) {
dataChan <- retainDiscoveryData(DiscoveryDataTypeError, util.Now(), nil, err)
},
)
}
func (d *defaultDiscoverer) innerDiscoverHost(wg *sync.WaitGroup, dataChan chan *DiscoveryData, zone *omd.Zone, dh *omd.DiscoverHost) {
defer func() {
wg.Done()
}()
taskScan(d,
func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}, stopChan chan struct{}) {
scanHost(zone, dh, resultChan, errChan, doneChan, stopChan)
},
func(result interface{}) {
h := result.(*omd.Host)
dataChan <- retainDiscoveryData(DiscoveryDataTypeHost, util.Now(), h, nil)
if nil != dh.DiscoverPort {
wg.Add(1)
go d.innerDiscoverPort(wg, dataChan, h, dh.DiscoverPort)
}
},
func(err error) {
dataChan <- retainDiscoveryData(DiscoveryDataTypeError, util.Now(), nil, err)
},
)
}
func (d *defaultDiscoverer) innerDiscoverPort(wg *sync.WaitGroup, dataChan chan *DiscoveryData, host *omd.Host, dp *omd.DiscoverPort) {
defer func() {
wg.Done()
}()
taskScan(d,
func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}, stopChan chan struct{}) {
scanPort(host, dp, resultChan, errChan, doneChan, stopChan)
},
func(result interface{}) {
p := result.(*omd.Port)
dataChan <- retainDiscoveryData(DiscoveryDataTypePort, util.Now(), p, nil)
if nil != dp.DiscoverService {
wg.Add(1)
go d.innerDiscoverSerice(wg, dataChan, p, dp.DiscoverService)
}
},
func(err error) {
dataChan <- retainDiscoveryData(DiscoveryDataTypeError, util.Now(), nil, err)
},
)
}
func (d *defaultDiscoverer) innerDiscoverSerice(wg *sync.WaitGroup, dataChan chan *DiscoveryData, port *omd.Port, ds *omd.DiscoverService) {
defer func() {
wg.Done()
}()
taskScan(d,
func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}, stopChan chan struct{}) {
scanService(port, ds, resultChan, errChan, doneChan, stopChan)
},
func(result interface{}) {
s := result.(*omd.Service)
dataChan <- retainDiscoveryData(DiscoveryDataTypeService, util.Now(), s, nil)
},
func(err error) {
dataChan <- retainDiscoveryData(DiscoveryDataTypeError, util.Now(), nil, err)
},
)
}
func taskScan(d *defaultDiscoverer,
taskFunc func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}, stopChan chan struct{}),
reesultFunc func(result interface{}),
errorFunc func(err error)) {
resultChan := make(chan interface{})
errChan := make(chan error)
stopChan := make(chan struct{})
doneChan := make(chan struct{})
defer func() {
}()
go taskFunc(resultChan, errChan, doneChan, stopChan)
for {
select {
case r := <-resultChan:
reesultFunc(r)
case err := <-errChan:
errorFunc(err)
case <-doneChan:
return
case <-d.stopChan:
close(stopChan)
<-doneChan
return
}
}
}
var discoveryDataPool sync.Pool
func retainDiscoveryData(discoveryDataType DiscoveryDataType, t util.Timestamp, result interface{}, err error) *DiscoveryData {
v := discoveryDataPool.Get()
var discoveryData *DiscoveryData
if v == nil {
discoveryData = &DiscoveryData{}
} else {
discoveryData = v.(*DiscoveryData)
}
discoveryData.Type = discoveryDataType
discoveryData.Result = result
discoveryData.Error = err
return discoveryData
}
func releaseDiscoveryData(discoveryData *DiscoveryData) {
discoveryData.Type = DiscoveryDataTypeNone
discoveryData.Result = nil
discoveryData.Error = nil
discoveryDataPool.Put(discoveryData)
}

View File

@ -0,0 +1,193 @@
package discoverer_test
import (
"encoding/json"
"log"
"strconv"
"testing"
logging "git.loafle.net/commons/logging-go"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
omu "git.loafle.net/overflow/model/util"
"git.loafle.net/overflow_scanner/probe/discoverer"
)
var (
dz = &omd.DiscoverZone{
ExcludePatterns: []string{},
}
z = &omd.Zone{
Network: "192.168.1.0/24",
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Address: "192.168.1.201",
Iface: " \\Device\\NPF_{1924FA2B-6927-4BA5-AF43-876C3F8853CE}",
Mac: "30:9C:23:15:A3:09",
}
dh = &omd.DiscoverHost{
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
FirstScanRange: "192.168.1.1",
LastScanRange: "192.168.1.254",
}
h = &omd.Host{
Zone: z,
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Address: "192.168.1.229",
Mac: "50:E5:49:46:93:28",
}
dp = &omd.DiscoverPort{
FirstScanRange: 1,
LastScanRange: 50000,
ExcludePorts: []int{
631,
},
IncludeTCP: true,
IncludeUDP: true,
}
p = &omd.Port{
Host: h,
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
PortNumber: json.Number(strconv.Itoa(21)),
DiscoveredDate: omu.NowPtr(),
}
ds = &omd.DiscoverService{
IncludeServices: []string{
"ACTIVEDIRECTORY",
"CASSANDRA",
"DHCP",
"DNS",
"FTP",
"HTTP",
"IMAP",
"LDAP",
"MONGODB",
"MSSQL",
"MYSQL",
"NETBIOS",
"ORACLE",
"POP",
"POSTGRESQL",
"REDIS",
"RMI",
"SMB",
"SMTP",
"SNMP",
"SSH",
"TELNET",
"WMI",
"UNKNOWN",
"SSH",
"WMI",
"SNMP V3",
},
}
dha = &omd.DiscoverHost{
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
FirstScanRange: "192.168.1.0",
LastScanRange: "192.168.1.255",
DiscoverPort: &omd.DiscoverPort{
FirstScanRange: 1,
LastScanRange: 50000,
ExcludePorts: []int{
631,
},
IncludeTCP: true,
IncludeUDP: false,
DiscoverService: ds,
},
}
)
func init() {
// runtime.LockOSThread()
logging.InitializeLogger("")
}
func TestDiscoverZone(t *testing.T) {
_discoverer := discoverer.GetDiscoverer()
handleDiscovery(_discoverer, func(dataChan chan *discoverer.DiscoveryData) {
_discoverer.DiscoverZone(dataChan, dz)
})
}
func TestDiscoverHost(t *testing.T) {
_discoverer := discoverer.GetDiscoverer()
handleDiscovery(_discoverer, func(dataChan chan *discoverer.DiscoveryData) {
_discoverer.DiscoverHost(dataChan, z, dh)
})
}
func TestDiscoverPort(t *testing.T) {
_discoverer := discoverer.GetDiscoverer()
handleDiscovery(_discoverer, func(dataChan chan *discoverer.DiscoveryData) {
_discoverer.DiscoverPort(dataChan, h, dp)
})
}
func TestDiscoverService(t *testing.T) {
_discoverer := discoverer.GetDiscoverer()
handleDiscovery(_discoverer, func(dataChan chan *discoverer.DiscoveryData) {
_discoverer.DiscoverSerice(dataChan, p, ds)
})
}
func TestDiscoverHostAll(t *testing.T) {
_discoverer := discoverer.GetDiscoverer()
handleDiscovery(_discoverer, func(dataChan chan *discoverer.DiscoveryData) {
_discoverer.DiscoverHost(dataChan, z, dha)
})
}
func handleDiscovery(_discoverer discoverer.Discoverer, discoveryFunc func(dataChan chan *discoverer.DiscoveryData)) error {
var dataChan chan *discoverer.DiscoveryData
retainChan := make(chan struct{})
go func() {
dataChan = _discoverer.Retain()
close(retainChan)
}()
select {
case <-retainChan:
}
defer func() {
_discoverer.Release(dataChan)
}()
go discoveryFunc(dataChan)
for {
select {
case data, ok := <-dataChan:
if !ok {
return nil
}
switch data.Type {
case discoverer.DiscoveryDataTypeStart:
log.Printf("DiscoveryService.DiscoveryStart")
case discoverer.DiscoveryDataTypeStop:
log.Printf("DiscoveryService.DiscoveryStop")
data.Release()
return nil
case discoverer.DiscoveryDataTypeError:
log.Printf("DiscoveryService.DiscoveryDataTypeError %v", data.Error)
case discoverer.DiscoveryDataTypeZone:
log.Printf("DiscoveryService.DiscoveryDataTypeZone %v", data.Result)
case discoverer.DiscoveryDataTypeHost:
log.Printf("DiscoveryService.DiscoveryDataTypeHost %v", data.Result)
case discoverer.DiscoveryDataTypePort:
log.Printf("DiscoveryService.DiscoveryDataTypePort %v", data.Result)
case discoverer.DiscoveryDataTypeService:
// log.Printf("DiscoveryService.DiscoveryDataTypeService %v", data.Result)
log.Println(data.Result)
}
data.Release()
}
}
}

26
discoverer/host.go Normal file
View File

@ -0,0 +1,26 @@
package discoverer
import (
"fmt"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
"git.loafle.net/overflow_scanner/probe/discoverer/ipv4"
"git.loafle.net/overflow_scanner/probe/discoverer/ipv6"
)
func scanHost(zone *omd.Zone, dh *omd.DiscoverHost, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}, stopChan chan struct{}) {
defer func() {
doneChan <- struct{}{}
}()
switch omm.ToMetaIPTypeEnum(dh.MetaIPType) {
case omm.MetaIPTypeEnumV4:
ipv4.ScanHost(zone, dh, resultChan, errChan, stopChan)
case omm.MetaIPTypeEnumV6:
ipv6.ScanHost(zone, dh, resultChan, errChan, stopChan)
default:
errChan <- fmt.Errorf("Discovery: Not supported MetaIPType")
}
}

View File

@ -2,20 +2,21 @@ package ipv4
import (
"fmt"
"log"
"net"
"sync/atomic"
"time"
"git.loafle.net/commons/util-go/net/cidr"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
oms "git.loafle.net/overflow/model/scan"
omu "git.loafle.net/overflow/model/util"
"git.loafle.net/overflow_scanner/probe/pcap"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
func ScanHost(zone *oms.Zone, dh *oms.DiscoverHost, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
func ScanHost(zone *omd.Zone, dh *omd.DiscoverHost, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
ps, err := pcap.RetainScanner(zone)
if nil != err {
errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err)
@ -45,7 +46,7 @@ func ScanHost(zone *oms.Zone, dh *oms.DiscoverHost, resultChan chan interface{},
timerStopped := make(chan struct{})
go func() {
hosts := make(map[string]*oms.Host)
hosts := make(map[string]*omd.Host)
var delay atomic.Value
delay.Store(false)
@ -60,7 +61,11 @@ func ScanHost(zone *oms.Zone, dh *oms.DiscoverHost, resultChan chan interface{},
}
delay.Store(true)
if h := handlePacketARP(zone, hostRanges, hosts, packet); nil != h {
resultChan <- h
if h != nil {
log.Println(h)
}
// resultChan <- h
}
case <-ticker.C:
if false == delay.Load().(bool) {
@ -88,7 +93,7 @@ func ScanHost(zone *oms.Zone, dh *oms.DiscoverHost, resultChan chan interface{},
}
}
func sendARP(ps pcap.PCapScanner, zone *oms.Zone, hostRanges []net.IP, stopChan chan struct{}) error {
func sendARP(ps pcap.PCapScanner, zone *omd.Zone, hostRanges []net.IP, stopChan chan struct{}) error {
hwAddr, err := net.ParseMAC(zone.Mac)
if nil != err {
return err
@ -123,7 +128,7 @@ func sendARP(ps pcap.PCapScanner, zone *oms.Zone, hostRanges []net.IP, stopChan
return nil
}
func handlePacketARP(zone *oms.Zone, hostRanges []net.IP, hosts map[string]*oms.Host, packet *layers.ARP) *oms.Host {
func handlePacketARP(zone *omd.Zone, hostRanges []net.IP, hosts map[string]*omd.Host, packet *layers.ARP) *omd.Host {
if packet.Operation != layers.ARPReply {
return nil
}
@ -144,7 +149,7 @@ func handlePacketARP(zone *oms.Zone, hostRanges []net.IP, hosts map[string]*oms.
return nil
}
h := &oms.Host{
h := &omd.Host{
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Address: ip.String(),
Mac: net.HardwareAddr(packet.SourceHwAddress).String(),
@ -157,7 +162,7 @@ func handlePacketARP(zone *oms.Zone, hostRanges []net.IP, hosts map[string]*oms.
return h
}
func getTargetHostRange(dh *oms.DiscoverHost, cr cidr.CIDRRanger) ([]net.IP, error) {
func getTargetHostRange(dh *omd.DiscoverHost, cr cidr.CIDRRanger) ([]net.IP, error) {
var firstIP net.IP
if "" != dh.FirstScanRange {
firstIP = net.ParseIP(dh.FirstScanRange)

View File

@ -3,22 +3,22 @@ package ipv4
import (
"testing"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
oms "git.loafle.net/overflow/model/scan"
omu "git.loafle.net/overflow/model/util"
)
func TestHostScan(t *testing.T) {
z := &oms.Zone{
z := &omd.Zone{
Network: "192.168.1.0/24",
Iface: "{1924FA2B-6927-4BA5-AF43-876C3F8853CE}",
Iface: " \\Device\\NPF_{1924FA2B-6927-4BA5-AF43-876C3F8853CE}",
Mac: "30:9C:23:15:A3:09",
Address: "192.168.1.201",
DiscoveredDate: omu.NowPtr(),
}
dp := &oms.DiscoverPort{
dp := &omd.DiscoverPort{
FirstScanRange: 1,
LastScanRange: 20000,
ExcludePorts: []int{
@ -29,7 +29,7 @@ func TestHostScan(t *testing.T) {
DiscoverService: nil,
}
dh := &oms.DiscoverHost{
dh := &omd.DiscoverHost{
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
FirstScanRange: "192.168.1.1",
LastScanRange: "192.168.1.255",

23
discoverer/ipv4/port.go Normal file
View File

@ -0,0 +1,23 @@
package ipv4
import (
"sync"
omd "git.loafle.net/overflow/model/discovery"
)
func ScanPort(host *omd.Host, dp *omd.DiscoverPort, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
var wg sync.WaitGroup
if dp.IncludeTCP {
wg.Add(1)
go scanPortTCP(host, dp, resultChan, errChan, stopChan, &wg)
}
if dp.IncludeUDP {
wg.Add(1)
go scanPortUDP(host, dp, resultChan, errChan, stopChan, &wg)
}
wg.Wait()
}

200
discoverer/ipv4/port_tcp.go Normal file
View File

@ -0,0 +1,200 @@
package ipv4
import (
"encoding/json"
"fmt"
"net"
"strconv"
"sync"
"sync/atomic"
"time"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
omu "git.loafle.net/overflow/model/util"
"git.loafle.net/overflow_scanner/probe/pcap"
logging "git.loafle.net/commons/logging-go"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
func scanPortTCP(host *omd.Host, dp *omd.DiscoverPort, resultChan chan interface{}, errChan chan error, stopChan chan struct{}, wg *sync.WaitGroup) {
defer func() {
wg.Done()
}()
ps, err := pcap.RetainScanner(host.Zone)
if nil != err {
errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err)
return
}
defer func() {
go pcap.ReleaseScanner(host.Zone)
}()
tcpChan := ps.OpenTCP(host.Address)
defer func() {
go ps.CloseTCP(host.Address, tcpChan)
}()
timerStopped := make(chan struct{})
go func() {
ports := make(map[int]*omd.Port)
var delay atomic.Value
delay.Store(false)
ticker := time.NewTicker(time.Millisecond * 500)
for {
select {
case packet, ok := <-tcpChan:
if !ok {
logging.Logger().Debugf("Discovery: tcp channel is closed")
return
}
delay.Store(true)
if p := handlePacketTCP(host, dp, ports, packet); nil != p {
resultChan <- p
}
case <-ticker.C:
if false == delay.Load().(bool) {
ticker.Stop()
timerStopped <- struct{}{}
return
}
delay.Store(false)
case <-stopChan:
return
}
}
}()
if err := sendTCP(host, dp, stopChan); nil != err {
errChan <- err
return
}
select {
case <-stopChan:
return
case <-timerStopped:
return
}
}
func sendTCP(host *omd.Host, dp *omd.DiscoverPort, stopChan chan struct{}) error {
tcpPacket, err := makePacketPortTCP(host)
if nil != err {
return err
}
defer func() {
tcpPacket.PacketConn.Close()
}()
buf := gopacket.NewSerializeBuffer()
Loop:
for portNumber := dp.FirstScanRange; portNumber < dp.LastScanRange; portNumber++ {
if nil != dp.ExcludePorts {
for _, exPortNumber := range dp.ExcludePorts {
if portNumber == exPortNumber {
continue Loop
}
}
}
tcpPacket.TCP.DstPort = layers.TCPPort(portNumber)
tcpPacket.TCP.SetNetworkLayerForChecksum(tcpPacket.IP)
if err := gopacket.SerializeLayers(buf, tcpPacket.Opts, tcpPacket.TCP); err != nil {
return err
}
if _, err := tcpPacket.PacketConn.WriteTo(buf.Bytes(), &net.IPAddr{IP: tcpPacket.IP.DstIP}); err != nil {
return err
}
timer := time.NewTimer(time.Microsecond * 100)
select {
case <-stopChan:
return nil
case <-timer.C:
}
}
return nil
}
func handlePacketTCP(host *omd.Host, dp *omd.DiscoverPort, ports map[int]*omd.Port, packet *layers.TCP) *omd.Port {
if nil == packet || packet.DstPort != 60000 {
return nil
}
if packet.RST {
return nil
}
port := int(packet.SrcPort)
logging.Logger().Debugf("Discovery: IP of TCP(%d) src %s", port, host.Address)
if _, ok := ports[port]; ok || !dp.Contains(port) {
return nil
}
p := &omd.Port{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
PortNumber: json.Number(strconv.Itoa(port)),
DiscoveredDate: omu.NowPtr(),
}
p.Host = host
ports[port] = p
return p
}
type PortPacketTCP struct {
IP *layers.IPv4
TCP *layers.TCP
Opts gopacket.SerializeOptions
PacketConn net.PacketConn
}
func makePacketPortTCP(host *omd.Host) (*PortPacketTCP, error) {
packetTCP := &PortPacketTCP{}
srcIP := net.ParseIP(host.Zone.Address)
if nil == srcIP {
return nil, fmt.Errorf("Discovery: IP(%s) of zone is not valid", host.Zone.Address)
}
dstIP := net.ParseIP(host.Address)
if nil == dstIP {
return nil, fmt.Errorf("Discovery: IP(%s) of host is not valid", host.Address)
}
packetTCP.IP = &layers.IPv4{
SrcIP: srcIP.To4(),
DstIP: dstIP.To4(),
Version: 4,
TTL: 64,
Protocol: layers.IPProtocolTCP,
}
packetTCP.TCP = &layers.TCP{
SrcPort: 60000,
DstPort: 0, // will be incremented during the scan
SYN: true,
Seq: 0,
}
packetTCP.Opts = gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
}
conn, err := net.ListenPacket("ip4:tcp", "0.0.0.0")
if err != nil {
return nil, fmt.Errorf("Discovery: SYN create socket error %v", err)
}
packetTCP.PacketConn = conn
return packetTCP, nil
}

172
discoverer/ipv4/port_udp.go Normal file
View File

@ -0,0 +1,172 @@
package ipv4
import (
"encoding/json"
"fmt"
"net"
"strconv"
"sync"
"sync/atomic"
"time"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
omu "git.loafle.net/overflow/model/util"
"git.loafle.net/overflow_scanner/probe/matcher"
"git.loafle.net/overflow_scanner/probe/pcap"
logging "git.loafle.net/commons/logging-go"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
func scanPortUDP(host *omd.Host, dp *omd.DiscoverPort, resultChan chan interface{}, errChan chan error, stopChan chan struct{}, wg *sync.WaitGroup) {
defer func() {
wg.Done()
}()
ps, err := pcap.RetainScanner(host.Zone)
if nil != err {
errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err)
return
}
defer func() {
pcap.ReleaseScanner(host.Zone)
}()
udpChan := ps.OpenUDP(host.Address)
defer func() {
ps.CloseUDP(host.Address, udpChan)
}()
timerStopped := make(chan struct{})
go func() {
ports := make(map[int]*omd.Port)
var delay atomic.Value
delay.Store(false)
ticker := time.NewTicker(time.Second * 3)
for {
select {
case packet, ok := <-udpChan:
if !ok {
logging.Logger().Debugf("Discovery: udp channel is closed")
return
}
delay.Store(true)
if p := handlePacketUDP(host, dp, ports, packet); nil != p {
resultChan <- p
}
case <-ticker.C:
if false == delay.Load().(bool) {
ticker.Stop()
timerStopped <- struct{}{}
return
}
delay.Store(false)
case <-stopChan:
return
}
}
}()
if err := sendUDP(host, dp, stopChan); nil != err {
errChan <- err
return
}
select {
case <-stopChan:
return
case <-timerStopped:
return
}
}
func sendUDP(host *omd.Host, dp *omd.DiscoverPort, stopChan chan struct{}) error {
ip := net.ParseIP(host.Address)
if nil == ip {
return fmt.Errorf("Discovery: IP(%s) of host is not valid", host.Address)
}
ms := matcher.GetUDPMatchers()
conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0})
if err != nil {
return err
}
defer func() {
conn.Close()
}()
for indexI := 0; indexI < len(ms); indexI++ {
m := ms[indexI]
Loop:
for portNumber := dp.FirstScanRange; portNumber < dp.LastScanRange; portNumber++ {
if nil != dp.ExcludePorts {
for _, exPortNumber := range dp.ExcludePorts {
if portNumber == exPortNumber {
continue Loop
}
}
}
if !m.IsSend(portNumber) {
continue
}
addr := &net.UDPAddr{IP: ip.To4(), Port: portNumber}
for i := 0; i < m.PacketCount(); i++ {
p := m.Packet(i)
if _, err := conn.WriteToUDP(p.Buffer, addr); err != nil {
logging.Logger().Errorf("Discovery: UDP write error %v", err)
}
}
timer := time.NewTimer(time.Microsecond * 200)
select {
case <-stopChan:
return nil
case <-timer.C:
}
}
}
return nil
}
func handlePacketUDP(host *omd.Host, dp *omd.DiscoverPort, ports map[int]*omd.Port, packet gopacket.Packet) *omd.Port {
ipLayer := packet.Layer(layers.LayerTypeIPv4)
if ipLayer.(*layers.IPv4).SrcIP.String() == host.Zone.Address {
return nil
}
if net := packet.NetworkLayer(); net == nil {
} else if udpLayer := packet.Layer(layers.LayerTypeUDP); udpLayer == nil {
} else if udp, ok := udpLayer.(*layers.UDP); ok {
srcIP := ipLayer.(*layers.IPv4).SrcIP
port := int(udp.SrcPort)
logging.Logger().Debugf("Discovery: IP of UDP(%d) src %v", port, srcIP)
if _, ok := ports[port]; ok || !dp.Contains(port) {
return nil
}
p := &omd.Port{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumUDP),
PortNumber: json.Number(strconv.Itoa(port)),
UDPLayer: udpLayer,
DiscoveredDate: omu.NowPtr(),
}
p.Host = host
ports[port] = p
return p
}
return nil
}

View File

@ -0,0 +1,52 @@
package ipv4
import (
"fmt"
cuej "git.loafle.net/commons/util-go/encoding/json"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
omu "git.loafle.net/overflow/model/util"
"github.com/google/gopacket/layers"
)
func ScanService(port *omd.Port, ds *omd.DiscoverService, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
portNumber, err := cuej.NumberToInt(port.PortNumber)
if err != nil {
errChan <- fmt.Errorf("Discovery: Service scan port[%s] error %v ", port.PortNumber, err)
return
}
switch omm.ToMetaPortTypeEnum(port.MetaPortType) {
case omm.MetaPortTypeEnumTCP:
if !scanServiceTCP(port, ds, resultChan, errChan, stopChan) {
if dName, ok := layers.TCPPortNames[layers.TCPPort(portNumber)]; ok {
description := fmt.Sprintf("Not Supported Service. Perhaps %s[%d]", dName, portNumber)
s := &omd.Service{
Key: omm.MetaTargetServiceTypeEnumUNKNOWN.String(),
Description: description,
DiscoveredDate: omu.NowPtr(),
Port: port,
MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumUNKNOWN),
}
resultChan <- s
}
}
case omm.MetaPortTypeEnumUDP:
if !scanServiceUDP(port, ds, resultChan, errChan, stopChan) {
if dName, ok := layers.UDPPortNames[layers.UDPPort(portNumber)]; ok {
description := fmt.Sprintf("Not Supported Service. Perhaps %s[%d]", dName, portNumber)
s := &omd.Service{
Key: omm.MetaTargetServiceTypeEnumUNKNOWN.String(),
Description: description,
DiscoveredDate: omu.NowPtr(),
Port: port,
MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumUNKNOWN),
}
resultChan <- s
}
}
}
}

View File

@ -0,0 +1,62 @@
package ipv4
import (
"crypto/tls"
"fmt"
"net"
"time"
omm "git.loafle.net/overflow/model/meta"
)
type serviceConnector interface {
MetaCryptoType() *omm.MetaCryptoType
Dial(ip string, port int) (net.Conn, error)
}
type normalServiceConn struct {
metaCryptoType *omm.MetaCryptoType
}
func (nsc *normalServiceConn) MetaCryptoType() *omm.MetaCryptoType {
return nsc.metaCryptoType
}
func (nsc *normalServiceConn) Dial(ip string, port int) (net.Conn, error) {
addr := fmt.Sprintf("%s:%d", ip, port)
conn, err := net.DialTimeout("tcp", addr, time.Duration(3)*time.Second)
if err != nil {
return nil, err
}
return conn, err
}
type tlsServiceConn struct {
metaCryptoType *omm.MetaCryptoType
}
func (tsc *tlsServiceConn) MetaCryptoType() *omm.MetaCryptoType {
return tsc.metaCryptoType
}
func (tsc *tlsServiceConn) Dial(ip string, port int) (net.Conn, error) {
addr := fmt.Sprintf("%s:%d", ip, port)
dialer := &net.Dialer{
Timeout: 3 * time.Second,
}
conn, err := tls.DialWithDialer(
dialer,
"tcp",
addr,
&tls.Config{
InsecureSkipVerify: true,
ServerName: ip,
},
)
if err != nil {
return nil, err
}
return conn, err
}

View File

@ -0,0 +1,255 @@
package ipv4
import (
"fmt"
"net"
"time"
logging "git.loafle.net/commons/logging-go"
csm "git.loafle.net/commons/service_matcher-go"
cuej "git.loafle.net/commons/util-go/encoding/json"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
omu "git.loafle.net/overflow/model/util"
"git.loafle.net/overflow_scanner/probe/matcher"
)
func scanServiceTCP(port *omd.Port, ds *omd.DiscoverService, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) bool {
hostIP := port.Host.Address
portNumber, err := cuej.NumberToInt(port.PortNumber)
if err != nil {
errChan <- fmt.Errorf("Discovery: Service scan on %s:%s error has occurred %v ", hostIP, port.PortNumber, err)
return false
}
info := csm.NewMatchInfo(hostIP, portNumber)
var s *omd.Service
scs := []serviceConnector{
&normalServiceConn{
metaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
},
&tlsServiceConn{
metaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumTLS),
},
}
LOOP:
for i := 0; i < len(scs); i++ {
sc := scs[i]
conn, err := sc.Dial(hostIP, portNumber)
if err != nil {
errChan <- fmt.Errorf("Discovery: Service scan[%s] on %s:%d error has occurred %v ", sc.MetaCryptoType().Key, hostIP, portNumber, err)
return false
}
logging.Logger().Debugf("Discovery: Service scan connected[%s:%d] %s", hostIP, portNumber, sc.MetaCryptoType().Key)
buf := make([]byte, 1024)
if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); nil != err {
logging.Logger().Debugf("Discovery: cannot set readdeadline connected[%s:%d] %s", hostIP, portNumber, sc.MetaCryptoType().Key)
return false
}
rn, err := conn.Read(buf)
if err != nil {
rn = 0
}
if rn != 0 {
s = hadlePrePacket(info, sc, conn, csm.NewPacket(buf, rn))
} else {
conn.Close()
s = hadlePostPacket(info, sc)
}
if nil != s {
break LOOP
}
select {
case <-stopChan:
return false
default:
}
}
if nil != s {
s.Port = port
resultChan <- s
return true
}
return false
}
func hadlePrePacket(info csm.MatchInfo, sc serviceConnector, conn net.Conn, packet *csm.Packet) *omd.Service {
defer func() {
conn.Close()
}()
// logging.Logger().Debugf("Discovery: Service scan pre packet length[%d], buf[%v]", packet.Len, packet.Buffer)
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d pre packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), packet.Len)
ms := matcher.GetTCPMatchers(true)
buf := make([]byte, 1024)
var s *omd.Service
LOOP:
for i := 0; i < len(ms); i++ {
m := ms[i]
if err := m.Match(info, 0, packet); err == nil {
packetCount := m.PacketCount()
if 0 == packetCount {
s = &omd.Service{
Key: m.Key(),
MetaCryptoType: sc.MetaCryptoType(),
DiscoveredDate: omu.NowPtr(),
}
break LOOP
}
found := false
for j := 0; j < packetCount; j++ {
tPacket := m.Packet(j)
// logging.Logger().Debugf("Discovery: Service scan send packet length[%d], buf[%v]", tPacket.Len, tPacket.Buffer)
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err {
logging.Logger().Debugf("Discovery: cannot set writeDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
break LOOP
}
wn, err := conn.Write(tPacket.Buffer)
if nil != err {
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err)
continue LOOP
}
if wn != tPacket.Len {
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d] not same with %d", sc.MetaCryptoType().Key, info.IP(), info.Port(), wn, tPacket.Len)
continue LOOP
}
if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); nil != err {
logging.Logger().Debugf("Discovery: cannot set readDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
break LOOP
}
rn, err := conn.Read(buf)
if nil != err {
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err)
break LOOP
}
if err := m.Match(info, j+1, csm.NewPacket(buf, rn)); err == nil {
// logging.Logger().Debugf("Discovery: Service scan receive match length[%d], buf[%v]", rn, buf)
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn)
found = true
} else {
// logging.Logger().Debugf("Discovery: Service scan receive not match length[%d], buf[%v]", rn, buf)
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive not match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn)
found = false
continue LOOP
}
}
if found {
s = &omd.Service{
Key: m.Key(),
MetaCryptoType: sc.MetaCryptoType(),
DiscoveredDate: omu.NowPtr(),
Metadata: m.Meta(),
}
break LOOP
}
}
}
return s
}
func hadlePostPacket(info csm.MatchInfo, sc serviceConnector) *omd.Service {
defer func() {
}()
ms := matcher.GetTCPMatchers(false)
buf := make([]byte, 1024)
var s *omd.Service
LOOP:
for i := 0; i < len(ms); i++ {
m := ms[i]
conn, err := sc.Dial(info.IP(), info.Port())
if err != nil {
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d socket dial error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err)
break LOOP
}
packetCount := m.PacketCount()
for j := 0; j < packetCount; j++ {
tPacket := m.Packet(j)
// logging.Logger().Debugf("Discovery: Service scan send packet length[%d], buf[%v]", tPacket.Len, tPacket.Buffer)
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err {
logging.Logger().Debugf("Discovery: cannot set writeDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
break
}
wn, err := conn.Write(tPacket.Buffer)
if nil != err {
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err)
break
}
if wn != tPacket.Len {
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d] not same with %d", sc.MetaCryptoType().Key, info.IP(), info.Port(), wn, tPacket.Len)
break
}
if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); nil != err {
logging.Logger().Debugf("Discovery: cannot set readDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
break
}
rn, err := conn.Read(buf)
if nil != err {
if !m.HasResponse(j) {
s = &omd.Service{
Key: m.Key(),
MetaCryptoType: sc.MetaCryptoType(),
DiscoveredDate: omu.NowPtr(),
}
break
}
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err)
break
}
if err := m.Match(info, j, csm.NewPacket(buf, rn)); err == nil {
if packetCount-1 == j {
s = &omd.Service{
Key: m.Key(),
MetaCryptoType: sc.MetaCryptoType(),
DiscoveredDate: omu.NowPtr(),
}
break
}
// logging.Logger().Debugf("Discovery: Service scan receive match length[%d], buf[%v]", rn, buf)
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn)
continue
} else {
// logging.Logger().Debugf("Discovery: Service scan receive not match length[%d], buf[%v]", rn, buf)
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive not match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn)
break
}
}
conn.Close()
if nil != s {
break LOOP
}
}
return s
}

View File

@ -0,0 +1,42 @@
package ipv4
import (
"fmt"
csm "git.loafle.net/commons/service_matcher-go"
cuej "git.loafle.net/commons/util-go/encoding/json"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
omu "git.loafle.net/overflow/model/util"
"git.loafle.net/overflow_scanner/probe/matcher"
)
func scanServiceUDP(port *omd.Port, ds *omd.DiscoverService, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) bool {
portNumber, err := cuej.NumberToInt(port.PortNumber)
if err != nil {
errChan <- fmt.Errorf("Discovery: Service scan port[%s] error %v ", port.PortNumber, err)
return false
}
ms := matcher.GetUDPMatchers()
mi := csm.NewMatchInfo(port.Host.Address, portNumber)
for i := 0; i < len(ms); i++ {
m := ms[i]
p := csm.NewPacket(port.UDPLayer.LayerPayload(), len(port.UDPLayer.LayerPayload()))
if err := m.Match(mi, 0, p); err == nil {
s := &omd.Service{
Key: m.Key(),
Port: port,
MetaCryptoType: omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
DiscoveredDate: omu.NowPtr(),
}
resultChan <- s
return true
}
}
return false
}

9
discoverer/ipv6/host.go Normal file
View File

@ -0,0 +1,9 @@
package ipv6
import (
omd "git.loafle.net/overflow/model/discovery"
)
func ScanHost(zone *omd.Zone, omd *omd.DiscoverHost, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
}

9
discoverer/ipv6/port.go Normal file
View File

@ -0,0 +1,9 @@
package ipv6
import (
omd "git.loafle.net/overflow/model/discovery"
)
func ScanPort(host *omd.Host, dp *omd.DiscoverPort, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
}

View File

@ -0,0 +1,9 @@
package ipv6
import (
omd "git.loafle.net/overflow/model/discovery"
)
func ScanService(port *omd.Port, ds *omd.DiscoverService, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
}

26
discoverer/port.go Normal file
View File

@ -0,0 +1,26 @@
package discoverer
import (
"fmt"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
"git.loafle.net/overflow_scanner/probe/discoverer/ipv4"
"git.loafle.net/overflow_scanner/probe/discoverer/ipv6"
)
func scanPort(host *omd.Host, dp *omd.DiscoverPort, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}, stopChan chan struct{}) {
defer func() {
doneChan <- struct{}{}
}()
switch omm.ToMetaIPTypeEnum(host.MetaIPType) {
case omm.MetaIPTypeEnumV4:
ipv4.ScanPort(host, dp, resultChan, errChan, stopChan)
case omm.MetaIPTypeEnumV6:
ipv6.ScanPort(host, dp, resultChan, errChan, stopChan)
default:
errChan <- fmt.Errorf("Discovery: Not supported MetaIPType")
}
}

26
discoverer/service.go Normal file
View File

@ -0,0 +1,26 @@
package discoverer
import (
"fmt"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
"git.loafle.net/overflow_scanner/probe/discoverer/ipv4"
"git.loafle.net/overflow_scanner/probe/discoverer/ipv6"
)
func scanService(port *omd.Port, ds *omd.DiscoverService, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}, stopChan chan struct{}) {
defer func() {
doneChan <- struct{}{}
}()
switch omm.ToMetaIPTypeEnum(port.Host.MetaIPType) {
case omm.MetaIPTypeEnumV4:
ipv4.ScanService(port, ds, resultChan, errChan, stopChan)
case omm.MetaIPTypeEnumV6:
ipv6.ScanService(port, ds, resultChan, errChan, stopChan)
default:
errChan <- fmt.Errorf("Discovery: Not supported MetaIPType")
}
}

98
discoverer/zone.go Normal file
View File

@ -0,0 +1,98 @@
package discoverer
import (
"net"
"regexp"
cun "git.loafle.net/commons/util-go/net"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
omu "git.loafle.net/overflow/model/util"
)
func scanZone(dz *omd.DiscoverZone, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}, stopChan chan struct{}) {
defer func() {
doneChan <- struct{}{}
}()
var err error
var ifaces []net.Interface
var addrs []net.Addr
var ipnet *cun.IPNet
var zones []*cun.IPNet
// var gwIP net.IP
// var gwIFace string
// if gwIP, gwIFace, err = gateway.DiscoverGateway(); nil != err {
// logChan <- err
// return
// }
if ifaces, err = net.Interfaces(); nil != err {
errChan <- err
return
}
zones = make([]*cun.IPNet, 0)
for _, i := range ifaces {
if addrs, err = i.Addrs(); nil != err {
errChan <- err
continue
}
for _, addr := range addrs {
if _, ipnet, err = cun.ParseCIDR(addr.String()); nil != err {
errChan <- err
continue
}
if ipnet.IP.IsLoopback() || checkSameZone(zones, ipnet) || checkExclude(dz.ExcludePatterns, i.Name) {
continue
}
zones = append(zones, ipnet)
z := &omd.Zone{
Network: ipnet.String(),
Iface: i.Name,
Mac: i.HardwareAddr.String(),
Address: addr.String(),
DiscoveredDate: omu.NowPtr(),
}
switch ipnet.Version() {
case 6:
z.MetaIPType = omm.ToMetaIPType(omm.MetaIPTypeEnumV6)
default:
z.MetaIPType = omm.ToMetaIPType(omm.MetaIPTypeEnumV4)
}
resultChan <- z
}
}
}
func checkExclude(ep []string, iface string) bool {
var r *regexp.Regexp
var err error
for _, p := range ep {
if r, err = regexp.Compile(p); nil != err {
return false
}
if r.MatchString(iface) {
return true
}
}
return false
}
func checkSameZone(zones []*cun.IPNet, ipnet *cun.IPNet) bool {
for _, i := range zones {
if i.Contains(ipnet.IP) {
return true
}
}
return false
}

View File

@ -4,7 +4,7 @@ import (
"sync"
"time"
oms "git.loafle.net/overflow/model/scan"
omd "git.loafle.net/overflow/model/discovery"
)
var mtx sync.Mutex
@ -14,7 +14,7 @@ func init() {
instances = make(map[string]PCapScanner, 0)
}
func RetainScanner(zone *oms.Zone) (PCapScanner, error) {
func RetainScanner(zone *omd.Zone) (PCapScanner, error) {
mtx.Lock()
defer mtx.Unlock()
@ -32,7 +32,7 @@ func RetainScanner(zone *oms.Zone) (PCapScanner, error) {
return ps, nil
}
func ReleaseScanner(zone *oms.Zone) {
func ReleaseScanner(zone *omd.Zone) {
go func() {
time.Sleep(2 * time.Second)

View File

@ -5,13 +5,13 @@ import (
"sort"
"sync"
oms "git.loafle.net/overflow/model/scan"
omd "git.loafle.net/overflow/model/discovery"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
func newPCapScanner(zone *oms.Zone) PCapScanner {
func newPCapScanner(zone *omd.Zone) PCapScanner {
ps := &pCapScan{
zone: zone,
}
@ -40,7 +40,7 @@ type PCapScanner interface {
type pCapScan struct {
pCapHandle *pcap.Handle
zone *oms.Zone
zone *omd.Zone
arpListenerChanMtx sync.RWMutex
arpListenerChans []chan *layers.ARP
@ -69,7 +69,9 @@ func (ps *pCapScan) start() error {
}
// set filter
// todo add tcp, udp filter
if err = h.SetBPFFilter("arp and src net " + ps.zone.Network + " or (((tcp[tcpflags] & (tcp-syn|tcp-ack) != 0) or (tcp[tcpflags] & (tcp-rst) != 0)) and port 60000) or udp "); nil != err {
// if err = h.SetBPFFilter("arp and src net " + ps.zone.Network + " or (((tcp[tcpflags] & (tcp-syn|tcp-ack) != 0) or (tcp[tcpflags] & (tcp-rst) != 0)) and port 60000) or udp "); nil != err {
if err = h.SetBPFFilter("arp"); nil != err {
h.Close()
return err
}

View File

@ -1,25 +0,0 @@
package scan
import (
"fmt"
oms "git.loafle.net/overflow/model/scan"
"git.loafle.net/overflow_scanner/probe/scan/ipv4"
"git.loafle.net/overflow_scanner/probe/scan/ipv6"
)
func scanHost(zone *oms.Zone, dh *oms.DiscoverHost, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}, stopChan chan struct{}) {
defer func() {
doneChan <- struct{}{}
}()
switch ocmm.ToMetaIPTypeEnum(dh.MetaIPType) {
case ocmm.MetaIPTypeEnumV4:
ipv4.ScanHost(zone, dh, resultChan, errChan, stopChan)
case ocmm.MetaIPTypeEnumV6:
ipv6.ScanHost(zone, dh, resultChan, errChan, stopChan)
default:
errChan <- fmt.Errorf("Discovery: Not supported MetaIPType")
}
}

View File

@ -1,9 +0,0 @@
package ipv6
import (
ocmd "git.loafle.net/overflow/commons-go/model/discovery"
)
func ScanHost(zone *ocmd.Zone, dh *ocmd.DiscoverHost, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
}