166 lines
3.6 KiB
Go
166 lines
3.6 KiB
Go
package arp
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"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/discovery/session"
|
|
"git.loafle.net/overflow_scanner/probe/internal/pcap"
|
|
"github.com/google/gopacket"
|
|
"github.com/google/gopacket/layers"
|
|
)
|
|
|
|
func Scan(discoverySession session.DiscoverySession) error {
|
|
|
|
targetHosts := discoverySession.TargetHosts()
|
|
if nil == targetHosts || 0 == len(targetHosts) {
|
|
return nil
|
|
}
|
|
|
|
zone := discoverySession.Zone()
|
|
|
|
ps := discoverySession.PCapScanner()
|
|
if nil == ps {
|
|
return fmt.Errorf("Cannot retain pcap instance")
|
|
}
|
|
|
|
arpChan := ps.OpenARP()
|
|
defer func() {
|
|
ps.CloseARP(arpChan)
|
|
}()
|
|
|
|
timerStopped := make(chan struct{})
|
|
go func() {
|
|
hosts := make(map[string]*omd.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, targetHosts, hosts, packet); nil != h {
|
|
go discoverySession.AddHost(omm.ToMetaDiscovererType(omm.MetaDiscovererTypeEnumARP), h, nil)
|
|
}
|
|
case <-ticker.C:
|
|
if false == delay.Load().(bool) {
|
|
ticker.Stop()
|
|
close(timerStopped)
|
|
return
|
|
}
|
|
delay.Store(false)
|
|
}
|
|
}
|
|
}()
|
|
|
|
if err := sendARP(ps, zone, targetHosts); nil != err {
|
|
log.Printf("sendARP %v", err)
|
|
return nil
|
|
}
|
|
|
|
select {
|
|
case <-timerStopped:
|
|
return nil
|
|
case <-discoverySession.StopChan():
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func sendARP(ps pcap.PCapScanner, zone *omd.Zone, targetHosts []net.IP) 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 targetHosts {
|
|
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
|
|
}
|
|
|
|
select {
|
|
case <-time.After(time.Microsecond * 100):
|
|
}
|
|
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func handlePacketARP(zone *omd.Zone, targetHosts []net.IP, hosts map[string]*omd.Host, packet *layers.ARP) *omd.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 targetHosts {
|
|
if h.Equal(ip) {
|
|
inRange = true
|
|
break
|
|
}
|
|
}
|
|
if !inRange {
|
|
return nil
|
|
}
|
|
|
|
h := omd.NewHost(
|
|
zone,
|
|
omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
|
|
ip.String(),
|
|
)
|
|
h.Mac = net.HardwareAddr(packet.SourceHwAddress).String()
|
|
h.DiscoveredDate = omu.NowPtr()
|
|
|
|
hosts[ip.String()] = h
|
|
|
|
return h
|
|
}
|
|
|
|
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},
|
|
}
|
|
}
|