probe/discovery/protocol/icmp/privileged/icmpv4.go

179 lines
3.5 KiB
Go
Raw Normal View History

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"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
)
func scanV4(discoverySession session.DiscoverySession) error {
targetHosts := discoverySession.TargetHosts()
if nil == targetHosts || 0 == len(targetHosts) {
return nil
}
zone := discoverySession.Zone()
ps, err := pcap.RetainScanner(zone)
if nil != err {
return fmt.Errorf("Cannot retain pcap instance %v", err)
}
defer func() {
pcap.ReleaseScanner(zone)
}()
icmpChan := ps.OpenICMP4()
defer func() {
ps.CloseICMP4(icmpChan)
}()
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 := <-icmpChan:
if !ok {
// logging.Logger().Debugf("icmp channel is closed")
return
}
delay.Store(true)
if h := handlePacketICMP4(zone, targetHosts, hosts, packet); nil != h {
if h != nil {
2018-09-03 05:24:30 +00:00
// log.Print("Host ", h)
2018-09-01 13:09:33 +00:00
discoverySession.AddHost(h)
2018-08-31 10:05:49 +00:00
}
// resultChan <- h
}
case <-ticker.C:
if false == delay.Load().(bool) {
ticker.Stop()
timerStopped <- struct{}{}
return
}
delay.Store(false)
}
}
}()
if err := sendICMP4(ps, zone, targetHosts); nil != err {
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
}
}
func sendICMP4(ps pcap.PCapScanner, zone *omd.Zone, targetHosts []net.IP) error {
conn, err := icmp.ListenPacket("ip4:icmp", "")
if nil != err {
return err
}
defer conn.Close()
msg, err := makeMessageICMP4()
if nil != err {
return err
}
var dst net.IPAddr
LOOP:
for _, targetHost := range targetHosts {
dst.IP = targetHost
if _, err := conn.WriteTo(msg, &dst); nil != err {
continue LOOP
}
timer := time.NewTimer(time.Microsecond * 100)
select {
case <-timer.C:
}
}
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,
DiscoveredDate: omu.NowPtr(),
}
hosts[ip.String()] = h
return h
}
func makeMessageICMP4() ([]byte, error) {
id := rand.Intn(0xffff)
seq := rand.Intn(0xffff)
return (&icmp.Message{
Type: ipv4.ICMPTypeEcho,
Code: 0,
Body: &icmp.Echo{
ID: id,
Seq: seq,
Data: []byte("PING by overFlow"),
},
}).Marshal(nil)
}