2018-09-01 02:19:03 +00:00
|
|
|
package privileged
|
2018-08-31 10:05:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"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"
|
2018-09-05 05:59:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// IP flag
|
|
|
|
DF = 0x02
|
|
|
|
// // IP header len
|
|
|
|
// IPHLEN = 40
|
|
|
|
//
|
|
|
|
// SIOCGIFMTU = 0x8921
|
|
|
|
UINT16 = 65535
|
2018-08-31 10:05:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func scanV4(discoverySession session.DiscoverySession) error {
|
|
|
|
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 10:05:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
icmpChan := ps.OpenICMP4()
|
|
|
|
defer func() {
|
|
|
|
ps.CloseICMP4(icmpChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
timerStopped := make(chan struct{})
|
2018-09-05 05:59:34 +00:00
|
|
|
stopChan := make(chan struct{})
|
|
|
|
defer close(stopChan)
|
|
|
|
|
2018-08-31 10:05:49 +00:00
|
|
|
go func() {
|
|
|
|
hosts := make(map[string]*omd.Host)
|
|
|
|
|
|
|
|
var delay atomic.Value
|
|
|
|
delay.Store(false)
|
2018-09-05 05:59:34 +00:00
|
|
|
ticker := time.NewTicker(time.Millisecond * 1000)
|
2018-08-31 10:05:49 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case packet, ok := <-icmpChan:
|
|
|
|
if !ok {
|
|
|
|
// logging.Logger().Debugf("icmp channel is closed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delay.Store(true)
|
|
|
|
if h := handlePacketICMP4(zone, targetHosts, hosts, packet); nil != h {
|
2018-09-04 20:03:43 +00:00
|
|
|
go discoverySession.AddHost(h)
|
2018-08-31 10:05:49 +00:00
|
|
|
}
|
|
|
|
case <-ticker.C:
|
|
|
|
if false == delay.Load().(bool) {
|
|
|
|
ticker.Stop()
|
|
|
|
timerStopped <- struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delay.Store(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-09-05 05:59:34 +00:00
|
|
|
if err := sendICMP4(ps, discoverySession, stopChan); nil != err {
|
2018-08-31 10:05:49 +00:00
|
|
|
log.Printf("sendICMP %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 10:05:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 05:59:34 +00:00
|
|
|
func sendICMP4(ps pcap.PCapScanner, discoverySession session.DiscoverySession, stopChan chan struct{}) error {
|
|
|
|
targetHosts := discoverySession.TargetHosts()
|
2018-08-31 10:05:49 +00:00
|
|
|
|
2018-09-05 05:59:34 +00:00
|
|
|
icmp4Packet, err := makePacketICMPv4(discoverySession.Zone())
|
2018-08-31 10:05:49 +00:00
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-05 05:59:34 +00:00
|
|
|
buf := gopacket.NewSerializeBuffer()
|
|
|
|
|
2018-08-31 10:05:49 +00:00
|
|
|
LOOP:
|
|
|
|
for _, targetHost := range targetHosts {
|
2018-09-05 05:59:34 +00:00
|
|
|
h := discoverySession.DiscoveredHost(targetHost.String())
|
|
|
|
if nil != h && "" != h.Mac {
|
|
|
|
mac, err := net.ParseMAC(h.Mac)
|
|
|
|
if nil != err {
|
|
|
|
icmp4Packet.Eth.DstMAC = nil
|
|
|
|
} else {
|
|
|
|
icmp4Packet.Eth.DstMAC = mac
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nil == icmp4Packet.Eth.DstMAC {
|
|
|
|
icmp4Packet.Eth.DstMAC = net.HardwareAddr{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
|
|
|
|
}
|
|
|
|
|
|
|
|
icmp4Packet.IP.DstIP = targetHost
|
|
|
|
icmp4Packet.IP.Id = uint16(rand.Intn(UINT16))
|
|
|
|
|
|
|
|
icmp4Packet.ICMPv4.Id = uint16(rand.Intn(0xffff))
|
|
|
|
icmp4Packet.ICMPv4.Seq = uint16(rand.Intn(0xffff))
|
|
|
|
|
|
|
|
if err := gopacket.SerializeLayers(buf, icmp4Packet.Opts, icmp4Packet.Eth, icmp4Packet.IP, icmp4Packet.ICMPv4); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue LOOP
|
|
|
|
}
|
|
|
|
if err := ps.WritePacketData(buf.Bytes()); err != nil {
|
|
|
|
log.Print(err)
|
2018-08-31 10:05:49 +00:00
|
|
|
continue LOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2018-09-04 19:12:14 +00:00
|
|
|
case <-time.After(time.Microsecond * 100):
|
2018-09-05 05:59:34 +00:00
|
|
|
case <-stopChan:
|
|
|
|
return nil
|
2018-08-31 10:05:49 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-05 05:59:34 +00:00
|
|
|
|
2018-08-31 10:05:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlePacketICMP4(zone *omd.Zone, targetHosts []net.IP, hosts map[string]*omd.Host, packet gopacket.Packet) *omd.Host {
|
|
|
|
_ethLayer := packet.Layer(layers.LayerTypeEthernet)
|
|
|
|
ethLayer := _ethLayer.(*layers.Ethernet)
|
|
|
|
|
|
|
|
_ip4Layer := packet.Layer(layers.LayerTypeIPv4)
|
|
|
|
ip4Layer := _ip4Layer.(*layers.IPv4)
|
|
|
|
|
|
|
|
_icmp4Layer := packet.Layer(layers.LayerTypeICMPv4)
|
|
|
|
icmp4Layer := _icmp4Layer.(*layers.ICMPv4)
|
|
|
|
|
|
|
|
if icmp4Layer.TypeCode != layers.ICMPv4TypeEchoReply {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := ip4Layer.SrcIP
|
|
|
|
if _, ok := hosts[ip.String()]; ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
isTarget := false
|
|
|
|
for _, h := range targetHosts {
|
|
|
|
if h.Equal(ip) {
|
|
|
|
isTarget = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isTarget {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
h := &omd.Host{
|
|
|
|
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
|
|
|
|
Address: ip.String(),
|
|
|
|
Mac: net.HardwareAddr(ethLayer.SrcMAC).String(),
|
|
|
|
Zone: zone,
|
2018-09-04 20:57:18 +00:00
|
|
|
DiscoveredBy: "ICMP",
|
2018-08-31 10:05:49 +00:00
|
|
|
DiscoveredDate: omu.NowPtr(),
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts[ip.String()] = h
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2018-09-05 05:59:34 +00:00
|
|
|
type PacketICMPv4 struct {
|
|
|
|
Eth *layers.Ethernet
|
|
|
|
IP *layers.IPv4
|
|
|
|
ICMPv4 *layers.ICMPv4
|
|
|
|
Opts gopacket.SerializeOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func makePacketICMPv4(zone *omd.Zone) (*PacketICMPv4, error) {
|
|
|
|
packetICMPv4 := &PacketICMPv4{}
|
|
|
|
|
|
|
|
srcIP := net.ParseIP(zone.Address)
|
|
|
|
if nil == srcIP {
|
|
|
|
return nil, fmt.Errorf("IP(%s) of zone is not valid", zone.Address)
|
|
|
|
}
|
|
|
|
|
|
|
|
srcMac, err := net.ParseMAC(zone.Mac)
|
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
packetICMPv4.Eth = &layers.Ethernet{
|
|
|
|
SrcMAC: srcMac,
|
|
|
|
EthernetType: layers.EthernetTypeIPv4,
|
|
|
|
}
|
|
|
|
|
|
|
|
packetICMPv4.IP = &layers.IPv4{
|
|
|
|
SrcIP: srcIP,
|
|
|
|
Flags: DF,
|
|
|
|
Version: 4,
|
|
|
|
TTL: 64,
|
|
|
|
Protocol: layers.IPProtocolICMPv4,
|
|
|
|
}
|
|
|
|
|
|
|
|
packetICMPv4.ICMPv4 = &layers.ICMPv4{
|
|
|
|
TypeCode: layers.CreateICMPv4TypeCode(layers.ICMPv4TypeEchoRequest, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
packetICMPv4.Opts = gopacket.SerializeOptions{
|
|
|
|
ComputeChecksums: true,
|
|
|
|
FixLengths: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
return packetICMPv4, nil
|
2018-08-31 10:05:49 +00:00
|
|
|
}
|