This commit is contained in:
insanity 2018-08-12 20:08:31 +09:00
parent 85a6ffcd7b
commit 26444de39f
9 changed files with 395 additions and 47 deletions

106
pcap/packet.go Normal file
View File

@ -0,0 +1,106 @@
package pcap
import (
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
type PacketType int
const (
PacketTypeUnknown PacketType = iota
PacketTypeARP
PacketTypeTCP
PacketTypeUDP
)
func getPacketType(packet gopacket.Packet) PacketType {
if packet == nil {
return PacketTypeUnknown
}
layer := packet.Layer(layers.LayerTypeARP)
if layer != nil {
return PacketTypeARP
}
layer = packet.Layer(layers.LayerTypeTCP)
if layer != nil {
if _, ok := layer.(*layers.TCP); ok {
return PacketTypeTCP
}
}
layer = packet.Layer(layers.LayerTypeUDP)
if layer != nil {
if _, ok := layer.(*layers.UDP); ok {
return PacketTypeUDP
}
}
return PacketTypeUnknown
}
func handlePacket(ps *pCapScan, packet gopacket.Packet) {
switch getPacketType(packet) {
case PacketTypeARP:
handlePacketARP(ps, packet)
case PacketTypeTCP:
handlePacketTCP(ps, packet)
case PacketTypeUDP:
handlePacketUDP(ps, packet)
default:
}
}
func handlePacketARP(ps *pCapScan, packet gopacket.Packet) {
ps.arpListenerChanMtx.RLock()
defer ps.arpListenerChanMtx.RUnlock()
arpLayer := packet.Layer(layers.LayerTypeARP)
arp := arpLayer.(*layers.ARP)
for _, ch := range ps.arpListenerChans {
ch <- arp
}
}
func handlePacketTCP(ps *pCapScan, packet gopacket.Packet) {
ipLayer := packet.Layer(layers.LayerTypeIPv4)
if nil == ipLayer {
return
}
ip := ipLayer.(*layers.IPv4).SrcIP.String()
ps.tcpListenerChanMtx.RLock()
defer ps.tcpListenerChanMtx.RUnlock()
chs, ok := ps.tcpListenerChans[ip]
if ok {
layer := packet.Layer(layers.LayerTypeTCP)
tcp, _ := layer.(*layers.TCP)
for _, ch := range chs {
ch <- tcp
}
}
}
func handlePacketUDP(ps *pCapScan, packet gopacket.Packet) {
ipLayer := packet.Layer(layers.LayerTypeIPv4)
if nil == ipLayer {
return
}
ip := ipLayer.(*layers.IPv4).SrcIP.String()
ps.udpListenerChanMtx.RLock()
defer ps.udpListenerChanMtx.RUnlock()
chs, ok := ps.udpListenerChans[ip]
if ok {
for _, ch := range chs {
ch <- packet
}
}
}

View File

@ -4,7 +4,7 @@ import (
"sync"
"time"
ocmd "git.loafle.net/overflow/commons-go/model/discovery"
oms "git.loafle.net/overflow/model/scan"
)
var mtx sync.Mutex
@ -14,7 +14,7 @@ func init() {
instances = make(map[string]PCapScanner, 0)
}
func RetainScanner(zone *ocmd.Zone) (PCapScanner, error) {
func RetainScanner(zone *oms.Zone) (PCapScanner, error) {
mtx.Lock()
defer mtx.Unlock()
@ -32,7 +32,7 @@ func RetainScanner(zone *ocmd.Zone) (PCapScanner, error) {
return ps, nil
}
func ReleaseScanner(zone *ocmd.Zone) {
func ReleaseScanner(zone *oms.Zone) {
go func() {
time.Sleep(2 * time.Second)

View File

@ -5,14 +5,15 @@ import (
"sort"
"sync"
oms "git.loafle.net/overflow/model/scan"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
func newPCapScanner(iface string) PCapScanner {
func newPCapScanner(zone *oms.Zone) PCapScanner {
ps := &pCapScan{
iface: iface
zone: zone,
}
return ps
@ -39,7 +40,7 @@ type PCapScanner interface {
type pCapScan struct {
pCapHandle *pcap.Handle
iface string
zone *oms.Zone
arpListenerChanMtx sync.RWMutex
arpListenerChans []chan *layers.ARP

View File

@ -1,9 +1,28 @@
package pcap
import (
"log"
"testing"
"github.com/google/gopacket/pcap"
)
func TestPcap(t *testing.T) {
func TestFindIfaces(t *testing.T) {
devices, err := pcap.FindAllDevs()
if err != nil {
log.Fatal(err)
}
t.Log("Devices found:")
for _, d := range devices {
t.Log("\nName: ", d.Name)
t.Log("Description: ", d.Description)
t.Log("Devices addresses: ", d.Description)
for _, address := range d.Addresses {
t.Log("- IP address: ", address.IP)
t.Log("- Subnet mask: ", address.Netmask)
}
}
}

View File

@ -1,12 +1,7 @@
package ping
import (
"fmt"
"log"
"testing"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
)
func TestPing(t *testing.T) {
@ -29,38 +24,3 @@ func TestPing(t *testing.T) {
}
}
}
func TestPcap(t *testing.T) {
devices, err := pcap.FindAllDevs()
if err != nil {
log.Fatal(err)
}
// Print device information
fmt.Println("Devices found:")
for _, d := range devices {
fmt.Println("\nName: ", d.Name)
fmt.Println("Description: ", d.Description)
fmt.Println("Devices addresses: ", d.Description)
for _, address := range d.Addresses {
fmt.Println("- IP address: ", address.IP)
fmt.Println("- Subnet mask: ", address.Netmask)
}
}
h, err := pcap.OpenLive("{1924FA2B-6927-4BA5-AF43-876C3F8853CE}", 1600, true, pcap.BlockForever)
if err != nil {
t.Error(err)
}
if err = h.SetBPFFilter("tcp"); nil != err {
h.Close()
t.Error(err)
}
packetSource := gopacket.NewPacketSource(h, h.LinkType())
for packet := range packetSource.Packets() {
fmt.Printf("%s", packet.Dump())
}
}

25
scan/host.go Normal file
View File

@ -0,0 +1,25 @@
package scan
import (
"fmt"
oms "git.loafle.net/overflow/model/scan"
"git.loafle.net/overflow_scanner/probe/scan/ipv4"
"git.loafle.net/overflow_scanner/probe/scan/ipv6"
)
func scanHost(zone *oms.Zone, dh *oms.DiscoverHost, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}, stopChan chan struct{}) {
defer func() {
doneChan <- struct{}{}
}()
switch ocmm.ToMetaIPTypeEnum(dh.MetaIPType) {
case ocmm.MetaIPTypeEnumV4:
ipv4.ScanHost(zone, dh, resultChan, errChan, stopChan)
case ocmm.MetaIPTypeEnumV6:
ipv6.ScanHost(zone, dh, resultChan, errChan, stopChan)
default:
errChan <- fmt.Errorf("Discovery: Not supported MetaIPType")
}
}

221
scan/ipv4/host.go Normal file
View File

@ -0,0 +1,221 @@
package ipv4
import (
"fmt"
"net"
"sync/atomic"
"time"
"git.loafle.net/commons/util-go/net/cidr"
omm "git.loafle.net/overflow/model/meta"
oms "git.loafle.net/overflow/model/scan"
omu "git.loafle.net/overflow/model/util"
"git.loafle.net/overflow_scanner/probe/pcap"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
func ScanHost(zone *oms.Zone, dh *oms.DiscoverHost, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
ps, err := pcap.RetainScanner(zone)
if nil != err {
errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err)
return
}
defer func() {
pcap.ReleaseScanner(zone)
}()
cr, err := cidr.NewCIDRRanger(zone.Network)
if nil != err {
errChan <- err
return
}
hostRanges, err := getTargetHostRange(dh, cr)
if nil != err {
errChan <- err
return
}
arpChan := ps.OpenARP()
defer func() {
ps.CloseARP(arpChan)
}()
timerStopped := make(chan struct{})
go func() {
hosts := make(map[string]*oms.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, hostRanges, hosts, packet); nil != h {
resultChan <- h
}
case <-ticker.C:
if false == delay.Load().(bool) {
ticker.Stop()
timerStopped <- struct{}{}
return
}
delay.Store(false)
case <-stopChan:
return
}
}
}()
if err := sendARP(ps, zone, hostRanges, stopChan); nil != err {
errChan <- err
return
}
select {
case <-stopChan:
return
case <-timerStopped:
return
}
}
func sendARP(ps pcap.PCapScanner, zone *oms.Zone, hostRanges []net.IP, stopChan chan struct{}) 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 hostRanges {
arpPacket.DstProtAddress = []byte(targetHost)
// log.Printf("ARP:%v", arpPacket)
gopacket.SerializeLayers(buf, opts, &ethPacket, &arpPacket)
if err := ps.WritePacketData(buf.Bytes()); err != nil {
return err
}
timer := time.NewTimer(time.Microsecond * 100)
select {
case <-stopChan:
return nil
case <-timer.C:
}
}
return nil
}
func handlePacketARP(zone *oms.Zone, hostRanges []net.IP, hosts map[string]*oms.Host, packet *layers.ARP) *oms.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 hostRanges {
if h.Equal(ip) {
inRange = true
break
}
}
if !inRange {
return nil
}
h := &oms.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 getTargetHostRange(dh *oms.DiscoverHost, cr cidr.CIDRRanger) ([]net.IP, error) {
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)
}
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, err := cr.Ranges(firstIP, lastIP, includeIPs, excludeIPs)
if nil != err {
return nil, err
}
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},
}
}

7
scan/ipv4/host_test.go Normal file
View File

@ -0,0 +1,7 @@
package ipv4
import "testing"
func TestHostScan(t *testing.T) {
}

9
scan/ipv6/host.go Normal file
View File

@ -0,0 +1,9 @@
package ipv6
import (
ocmd "git.loafle.net/overflow/commons-go/model/discovery"
)
func ScanHost(zone *ocmd.Zone, dh *ocmd.DiscoverHost, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
}