probe/discovery/protocol/udp/matcher/matcher4.go
crusader dee29f34d9 ing
2018-09-05 04:12:14 +09:00

162 lines
3.7 KiB
Go

package matcher
import (
"encoding/json"
"fmt"
"log"
"net"
"strconv"
"sync/atomic"
"time"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
omu "git.loafle.net/overflow/model/util"
osm "git.loafle.net/overflow/service_matcher-go"
"git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/internal/matcher"
"git.loafle.net/overflow_scanner/probe/internal/pcap"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
func scanV4(discoverySession session.DiscoverySession, targetHost *omd.Host) error {
ps := discoverySession.PCapScanner()
if nil == ps {
return fmt.Errorf("Cannot retain pcap instance")
}
udpChan := ps.OpenUDP(targetHost.Address)
defer func() {
go ps.CloseUDP(targetHost.Address, udpChan)
}()
timerStopped := make(chan struct{})
go func() {
ports := make(map[int]*omd.Port)
var delay atomic.Value
delay.Store(false)
ticker := time.NewTicker(time.Millisecond * 500)
for {
select {
case packet, ok := <-udpChan:
if !ok {
// olog.Logger().Debug("Discovery: udp channel is closed")
return
}
delay.Store(true)
if p := handlePacketUDP4(discoverySession, targetHost, ports, packet); nil != p {
discoverySession.AddPort(p)
}
case <-ticker.C:
if false == delay.Load().(bool) {
ticker.Stop()
timerStopped <- struct{}{}
return
}
delay.Store(false)
}
}
}()
if err := sendUDP4(discoverySession, ps, targetHost); nil != err {
log.Printf("sendUDP4 %v", err)
return nil
}
select {
case <-timerStopped:
return nil
case <-discoverySession.StopChan():
return nil
}
}
func sendUDP4(discoverySession session.DiscoverySession, ps pcap.PCapScanner, host *omd.Host) error {
dp := discoverySession.DiscoverPort()
ip := net.ParseIP(host.Address)
if nil == ip {
return fmt.Errorf("IP(%s) of host is not valid", host.Address)
}
matchers := matcher.GetUDPMatchers()
conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0})
if err != nil {
return err
}
defer func() {
conn.Close()
}()
for _, _matcher := range matchers {
INNER_LOOP:
for portNumber := dp.FirstScanRange; portNumber < dp.LastScanRange; portNumber++ {
if nil != dp.ExcludePorts {
for _, exPortNumber := range dp.ExcludePorts {
if portNumber == exPortNumber {
continue INNER_LOOP
}
}
}
if !_matcher.IsSend(portNumber) {
continue INNER_LOOP
}
matchCtx := osm.NewMatchCtx(host.Address, portNumber)
addr := &net.UDPAddr{IP: ip.To4(), Port: portNumber}
for i := 0; i < _matcher.PacketCount(matchCtx); i++ {
p := _matcher.Packet(matchCtx, i)
if _, err := conn.WriteToUDP(p.Buffer, addr); err != nil {
log.Print("UDP write error", err)
}
}
select {
case <-time.After(time.Microsecond * 100):
}
}
}
return nil
}
func handlePacketUDP4(discoverySession session.DiscoverySession, host *omd.Host, ports map[int]*omd.Port, packet gopacket.Packet) *omd.Port {
ipLayer := packet.Layer(layers.LayerTypeIPv4)
if ipLayer.(*layers.IPv4).SrcIP.String() == host.Zone.Address {
return nil
}
dp := discoverySession.DiscoverPort()
if net := packet.NetworkLayer(); net == nil {
} else if udpLayer := packet.Layer(layers.LayerTypeUDP); udpLayer == nil {
} else if udp, ok := udpLayer.(*layers.UDP); ok {
// srcIP := ipLayer.(*layers.IPv4).SrcIP
port := int(udp.SrcPort)
if _, ok := ports[port]; ok || !dp.Contains(port) {
return nil
}
p := &omd.Port{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumUDP),
PortNumber: json.Number(strconv.Itoa(port)),
UDPLayer: udpLayer,
DiscoveredDate: omu.NowPtr(),
}
p.Host = host
ports[port] = p
return p
}
return nil
}