222 lines
4.9 KiB
Go
222 lines
4.9 KiB
Go
|
package ipv4
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net"
|
||
|
"sync/atomic"
|
||
|
"time"
|
||
|
|
||
|
"git.loafle.net/commons/util-go/net/cidr"
|
||
|
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{}) {
|
||
|
ps, err := pcap.RetainScanner(zone)
|
||
|
if nil != err {
|
||
|
errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
defer func() {
|
||
|
pcap.ReleaseScanner(zone)
|
||
|
}()
|
||
|
|
||
|
cr, err := cidr.NewCIDRRanger(zone.Network)
|
||
|
if nil != err {
|
||
|
errChan <- err
|
||
|
return
|
||
|
}
|
||
|
|
||
|
hostRanges, err := getTargetHostRange(dh, cr)
|
||
|
if nil != err {
|
||
|
errChan <- err
|
||
|
return
|
||
|
}
|
||
|
|
||
|
arpChan := ps.OpenARP()
|
||
|
defer func() {
|
||
|
ps.CloseARP(arpChan)
|
||
|
}()
|
||
|
|
||
|
timerStopped := make(chan struct{})
|
||
|
go func() {
|
||
|
hosts := make(map[string]*oms.Host)
|
||
|
|
||
|
var delay atomic.Value
|
||
|
delay.Store(false)
|
||
|
ticker := time.NewTicker(time.Millisecond * 500)
|
||
|
|
||
|
for {
|
||
|
select {
|
||
|
case packet, ok := <-arpChan:
|
||
|
if !ok {
|
||
|
// logging.Logger().Debugf("arp channel is closed")
|
||
|
return
|
||
|
}
|
||
|
delay.Store(true)
|
||
|
if h := handlePacketARP(zone, hostRanges, hosts, packet); nil != h {
|
||
|
resultChan <- h
|
||
|
}
|
||
|
case <-ticker.C:
|
||
|
if false == delay.Load().(bool) {
|
||
|
ticker.Stop()
|
||
|
timerStopped <- struct{}{}
|
||
|
return
|
||
|
}
|
||
|
delay.Store(false)
|
||
|
case <-stopChan:
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
if err := sendARP(ps, zone, hostRanges, stopChan); nil != err {
|
||
|
errChan <- err
|
||
|
return
|
||
|
}
|
||
|
|
||
|
select {
|
||
|
case <-stopChan:
|
||
|
return
|
||
|
case <-timerStopped:
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func sendARP(ps pcap.PCapScanner, zone *oms.Zone, hostRanges []net.IP, stopChan chan struct{}) error {
|
||
|
hwAddr, err := net.ParseMAC(zone.Mac)
|
||
|
if nil != err {
|
||
|
return err
|
||
|
}
|
||
|
ip := net.ParseIP(zone.Address)
|
||
|
if nil == ip {
|
||
|
return fmt.Errorf("Discovery: IP(%s) of zone is not valid", zone.Address)
|
||
|
}
|
||
|
|
||
|
ethPacket := makePacketEthernet(hwAddr)
|
||
|
arpPacket := makePacketARP(hwAddr, ip.To4())
|
||
|
opts := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
|
||
|
buf := gopacket.NewSerializeBuffer()
|
||
|
|
||
|
for _, targetHost := range hostRanges {
|
||
|
arpPacket.DstProtAddress = []byte(targetHost)
|
||
|
// log.Printf("ARP:%v", arpPacket)
|
||
|
gopacket.SerializeLayers(buf, opts, ðPacket, &arpPacket)
|
||
|
if err := ps.WritePacketData(buf.Bytes()); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
timer := time.NewTimer(time.Microsecond * 100)
|
||
|
|
||
|
select {
|
||
|
case <-stopChan:
|
||
|
return nil
|
||
|
case <-timer.C:
|
||
|
}
|
||
|
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func handlePacketARP(zone *oms.Zone, hostRanges []net.IP, hosts map[string]*oms.Host, packet *layers.ARP) *oms.Host {
|
||
|
if packet.Operation != layers.ARPReply {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
ip := net.IP(packet.SourceProtAddress)
|
||
|
if _, ok := hosts[ip.String()]; ok {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
inRange := false
|
||
|
for _, h := range hostRanges {
|
||
|
if h.Equal(ip) {
|
||
|
inRange = true
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
if !inRange {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
h := &oms.Host{
|
||
|
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
|
||
|
Address: ip.String(),
|
||
|
Mac: net.HardwareAddr(packet.SourceHwAddress).String(),
|
||
|
Zone: zone,
|
||
|
DiscoveredDate: omu.NowPtr(),
|
||
|
}
|
||
|
|
||
|
hosts[ip.String()] = h
|
||
|
|
||
|
return h
|
||
|
}
|
||
|
|
||
|
func getTargetHostRange(dh *oms.DiscoverHost, cr cidr.CIDRRanger) ([]net.IP, error) {
|
||
|
var firstIP net.IP
|
||
|
if "" != dh.FirstScanRange {
|
||
|
firstIP = net.ParseIP(dh.FirstScanRange)
|
||
|
if nil == firstIP {
|
||
|
return nil, fmt.Errorf("Discovery: IP(%v) of FirstScanRange host is not valid", firstIP)
|
||
|
}
|
||
|
}
|
||
|
var lastIP net.IP
|
||
|
if "" != dh.LastScanRange {
|
||
|
lastIP = net.ParseIP(dh.LastScanRange)
|
||
|
if nil == lastIP {
|
||
|
return nil, fmt.Errorf("Discovery: IP(%v) of LastScanRange host is not valid", lastIP)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
includeIPs := make([]net.IP, 0)
|
||
|
for _, iHost := range dh.IncludeHosts {
|
||
|
iIP := net.ParseIP(iHost)
|
||
|
if nil == iIP {
|
||
|
return nil, fmt.Errorf("Discovery: IP(%v) of include host is not valid", iHost)
|
||
|
}
|
||
|
includeIPs = append(includeIPs, iIP)
|
||
|
}
|
||
|
|
||
|
excludeIPs := make([]net.IP, 0)
|
||
|
for _, eHost := range dh.ExcludeHosts {
|
||
|
eIP := net.ParseIP(eHost)
|
||
|
if nil == eIP {
|
||
|
return nil, fmt.Errorf("Discovery: IP(%v) of exclude host is not valid", eHost)
|
||
|
}
|
||
|
excludeIPs = append(excludeIPs, eIP)
|
||
|
}
|
||
|
|
||
|
ranges, err := cr.Ranges(firstIP, lastIP, includeIPs, excludeIPs)
|
||
|
if nil != err {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return ranges, nil
|
||
|
}
|
||
|
|
||
|
func makePacketEthernet(hw net.HardwareAddr) layers.Ethernet {
|
||
|
return layers.Ethernet{
|
||
|
SrcMAC: hw,
|
||
|
DstMAC: net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
||
|
EthernetType: layers.EthernetTypeARP,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func makePacketARP(hw net.HardwareAddr, ip net.IP) layers.ARP {
|
||
|
return layers.ARP{
|
||
|
AddrType: layers.LinkTypeEthernet,
|
||
|
Protocol: layers.EthernetTypeIPv4,
|
||
|
HwAddressSize: 6,
|
||
|
ProtAddressSize: 4,
|
||
|
Operation: layers.ARPRequest,
|
||
|
SourceHwAddress: []byte(hw),
|
||
|
SourceProtAddress: []byte(ip),
|
||
|
DstHwAddress: []byte{0, 0, 0, 0, 0, 0},
|
||
|
}
|
||
|
}
|