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"
|
|
|
|
|
|
|
|
"git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
|
|
|
|
"git.loafle.net/overflow/overflow_discovery/commons/pcap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ScanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}) {
|
2017-11-18 10:49:58 +00:00
|
|
|
defer func() {
|
|
|
|
doneChan <- 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() {
|
|
|
|
hosts := make(map[string]*model.Host)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case packet, ok := <-arpChan:
|
|
|
|
if !ok {
|
|
|
|
logging.Logger().Debug(fmt.Sprintf("Discovery: arp channel is closed"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if h := handlePacketARP(zone, cr, hosts, packet); nil != h {
|
|
|
|
logging.Logger().Debug(fmt.Sprintf("Discovery: Host[%v] is founded", h))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := sendARP(ps, zone, hostRanges); nil != err {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlePacketARP(zone *model.Zone, cr cidr.CIDRRanger, hosts map[string]*model.Host, packet *layers.ARP) *model.Host {
|
|
|
|
if packet.Operation != layers.ARPReply {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// logging.Logger().Debug(fmt.Sprintf("Discovery: arp packet %v", packet))
|
|
|
|
|
|
|
|
ip := net.IP(packet.SourceProtAddress)
|
|
|
|
if _, ok := hosts[ip.String()]; ok || !cr.Contains(ip) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
h := &model.Host{}
|
|
|
|
h.IP = ip.String()
|
|
|
|
h.Mac = net.HardwareAddr(packet.SourceHwAddress).String()
|
|
|
|
h.Zone = zone
|
|
|
|
|
|
|
|
hosts[ip.String()] = h
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendARP(ps pcap.PCapScanner, zone *model.Zone, hostRanges []net.IP) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
time.Sleep(time.Microsecond * 100)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTargetHostRange(dh *model.DiscoveryHost, cr cidr.CIDRRanger) ([]net.IP, error) {
|
|
|
|
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 := cr.Range(excludeIPs)
|
|
|
|
|
|
|
|
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},
|
|
|
|
}
|
|
|
|
}
|