2018-08-31 05:15:46 +00:00
|
|
|
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 {
|
2018-09-01 06:07:15 +00:00
|
|
|
|
2018-08-31 05:15:46 +00:00
|
|
|
targetHosts := discoverySession.TargetHosts()
|
|
|
|
if nil == targetHosts || 0 == len(targetHosts) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
zone := discoverySession.Zone()
|
|
|
|
|
2018-09-04 18:58:33 +00:00
|
|
|
ps := discoverySession.PCapScanner()
|
|
|
|
if nil == ps {
|
|
|
|
return fmt.Errorf("Cannot retain pcap instance")
|
2018-08-31 05:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if h != nil {
|
2018-09-01 13:09:33 +00:00
|
|
|
discoverySession.AddHost(h)
|
2018-08-31 05:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-ticker.C:
|
|
|
|
if false == delay.Load().(bool) {
|
|
|
|
ticker.Stop()
|
2018-09-04 18:58:33 +00:00
|
|
|
close(timerStopped)
|
2018-08-31 05:15:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
delay.Store(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := sendARP(ps, zone, targetHosts); nil != err {
|
|
|
|
log.Printf("sendARP %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-timerStopped:
|
|
|
|
return nil
|
2018-09-03 10:33:20 +00:00
|
|
|
case <-discoverySession.StopChan():
|
|
|
|
return nil
|
2018-08-31 05:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-09-04 19:12:14 +00:00
|
|
|
case <-time.After(time.Microsecond * 100):
|
2018-08-31 05:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
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.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 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},
|
|
|
|
}
|
|
|
|
}
|