2017-11-17 15:12:38 +00:00
|
|
|
package ipv4
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.loafle.net/commons_go/logging"
|
|
|
|
"git.loafle.net/commons_go/util/net/cidr"
|
|
|
|
"github.com/google/gopacket"
|
|
|
|
"github.com/google/gopacket/layers"
|
|
|
|
|
2017-12-04 09:37:39 +00:00
|
|
|
discoveryM "git.loafle.net/overflow/overflow_commons_go/modules/discovery/model"
|
2017-11-17 15:12:38 +00:00
|
|
|
"git.loafle.net/overflow/overflow_discovery/commons/pcap"
|
|
|
|
)
|
|
|
|
|
2017-12-04 09:37:39 +00:00
|
|
|
func ScanHost(zone *discoveryM.Zone, dh *discoveryM.DiscoveryHost, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
|
2017-11-17 15:12:38 +00:00
|
|
|
ps, err := pcap.RetainScanner(zone)
|
|
|
|
if nil != err {
|
|
|
|
errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-11-18 10:49:58 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
pcap.ReleaseScanner(zone)
|
|
|
|
}()
|
|
|
|
|
2017-11-17 15:12:38 +00:00
|
|
|
cr, err := cidr.NewCIDRRanger(zone.Network)
|
|
|
|
if nil != err {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
2017-11-18 10:49:58 +00:00
|
|
|
|
2017-11-17 15:12:38 +00:00
|
|
|
hostRanges, err := getTargetHostRange(dh, cr)
|
|
|
|
if nil != err {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
arpChan := ps.OpenARP()
|
|
|
|
defer func() {
|
2017-11-18 10:49:58 +00:00
|
|
|
ps.CloseARP(arpChan)
|
2017-11-17 15:12:38 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2017-12-04 09:37:39 +00:00
|
|
|
hosts := make(map[string]*discoveryM.Host)
|
2017-11-17 15:12:38 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case packet, ok := <-arpChan:
|
|
|
|
if !ok {
|
2018-03-21 10:34:40 +00:00
|
|
|
logging.Logger().Debugf("Discovery: arp channel is closed")
|
2017-11-17 15:12:38 +00:00
|
|
|
return
|
|
|
|
}
|
2017-11-22 06:33:41 +00:00
|
|
|
if h := handlePacketARP(zone, hostRanges, hosts, packet); nil != h {
|
2017-11-18 13:03:37 +00:00
|
|
|
resultChan <- h
|
2017-11-17 15:12:38 +00:00
|
|
|
}
|
2017-11-18 12:08:08 +00:00
|
|
|
case <-stopChan:
|
|
|
|
return
|
2017-11-17 15:12:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-11-18 12:08:08 +00:00
|
|
|
if err := sendARP(ps, zone, hostRanges, stopChan); nil != err {
|
2017-11-17 15:12:38 +00:00
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-21 05:31:17 +00:00
|
|
|
timer := time.NewTimer(10 * time.Second)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-stopChan:
|
|
|
|
return
|
|
|
|
case <-timer.C:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-17 15:12:38 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 09:37:39 +00:00
|
|
|
func sendARP(ps pcap.PCapScanner, zone *discoveryM.Zone, hostRanges []net.IP, stopChan chan struct{}) error {
|
2017-11-17 15:12:38 +00:00
|
|
|
hwAddr, err := net.ParseMAC(zone.Mac)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ip := net.ParseIP(zone.IP)
|
|
|
|
if nil == ip {
|
|
|
|
return fmt.Errorf("Discovery: IP(%s) of zone is not valid", zone.IP)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2017-11-18 12:08:08 +00:00
|
|
|
|
2017-11-22 05:55:40 +00:00
|
|
|
timer := time.NewTimer(time.Microsecond * 100)
|
|
|
|
|
2017-11-18 12:08:08 +00:00
|
|
|
select {
|
|
|
|
case <-stopChan:
|
|
|
|
return nil
|
2017-11-22 05:55:40 +00:00
|
|
|
case <-timer.C:
|
2017-11-18 12:08:08 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 15:12:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-04 09:37:39 +00:00
|
|
|
func handlePacketARP(zone *discoveryM.Zone, hostRanges []net.IP, hosts map[string]*discoveryM.Host, packet *layers.ARP) *discoveryM.Host {
|
2017-11-18 13:03:37 +00:00
|
|
|
if packet.Operation != layers.ARPReply {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := net.IP(packet.SourceProtAddress)
|
2017-11-22 06:33:41 +00:00
|
|
|
if _, ok := hosts[ip.String()]; ok {
|
2017-11-18 13:03:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-11-22 06:33:41 +00:00
|
|
|
|
|
|
|
inRange := false
|
|
|
|
for _, h := range hostRanges {
|
|
|
|
if h.Equal(ip) {
|
|
|
|
inRange = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !inRange {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-04 09:37:39 +00:00
|
|
|
h := &discoveryM.Host{}
|
2017-11-18 13:03:37 +00:00
|
|
|
h.IP = ip.String()
|
|
|
|
h.Mac = net.HardwareAddr(packet.SourceHwAddress).String()
|
|
|
|
h.Zone = zone
|
|
|
|
|
|
|
|
hosts[ip.String()] = h
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2017-12-04 09:37:39 +00:00
|
|
|
func getTargetHostRange(dh *discoveryM.DiscoveryHost, cr cidr.CIDRRanger) ([]net.IP, error) {
|
2017-11-22 06:33:41 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-11-17 15:12:38 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-11-22 06:33:41 +00:00
|
|
|
ranges, err := cr.Ranges(firstIP, lastIP, includeIPs, excludeIPs)
|
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-17 15:12:38 +00:00
|
|
|
|
|
|
|
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},
|
|
|
|
}
|
|
|
|
}
|