probe/discovery/protocol/tcp/syn/synv4.go

219 lines
4.6 KiB
Go
Raw Normal View History

2018-08-31 14:00:08 +00:00
package syn
2018-08-31 10:46:52 +00:00
import (
"fmt"
"log"
"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-28 08:50:57 +00:00
func scanV4(discoverySession session.DiscoverySession, targetHost *omd.Host) (err error) {
2018-09-04 18:58:33 +00:00
ps := discoverySession.PCapScanner()
if nil == ps {
2018-09-28 08:50:57 +00:00
err = fmt.Errorf("Cannot retain pcap instance")
return
2018-08-31 10:46:52 +00:00
}
tcpChan := ps.OpenTCP(targetHost.Address)
defer func() {
2018-09-12 06:06:15 +00:00
ps.CloseTCP(targetHost.Address, tcpChan)
2018-08-31 10:46:52 +00:00
}()
2018-09-28 07:13:45 +00:00
timerStopped := make(chan struct{}, 0)
2018-09-04 18:58:33 +00:00
stopChan := make(chan struct{})
defer close(stopChan)
2018-09-28 08:50:57 +00:00
var sendComplete atomic.Value
sendComplete.Store(false)
2018-08-31 10:46:52 +00:00
go func() {
2018-09-28 07:13:45 +00:00
defer func() {
close(timerStopped)
}()
2018-08-31 10:46:52 +00:00
ports := make(map[int]*omd.Port)
var delay atomic.Value
delay.Store(false)
2018-09-28 10:00:48 +00:00
ticker := time.NewTicker(time.Millisecond * 1000)
2018-09-28 07:13:45 +00:00
defer func() {
ticker.Stop()
}()
2018-09-28 03:02:48 +00:00
2018-08-31 10:46:52 +00:00
for {
select {
case packet, ok := <-tcpChan:
if !ok {
return
}
delay.Store(true)
if p := handlePacketTCP4(discoverySession, targetHost, ports, packet); nil != p {
2018-09-17 14:18:49 +00:00
go discoverySession.AddPort(omm.ToMetaDiscovererType(omm.MetaDiscovererTypeEnumTCPSyn), p, nil)
2018-08-31 10:46:52 +00:00
}
case <-ticker.C:
2018-09-28 08:50:57 +00:00
if false == sendComplete.Load().(bool) {
continue
}
2018-08-31 10:46:52 +00:00
if false == delay.Load().(bool) {
return
}
delay.Store(false)
}
}
}()
2018-09-28 09:49:02 +00:00
go func() {
if err = sendTCP4(discoverySession, ps, targetHost, stopChan); nil != err {
log.Printf("sendTCP %v", err)
return
}
sendComplete.Store(true)
}()
2018-08-31 10:46:52 +00:00
select {
case <-timerStopped:
return nil
2018-09-03 10:33:20 +00:00
case <-discoverySession.StopChan():
return nil
2018-08-31 10:46:52 +00:00
}
}
2018-09-04 18:58:33 +00:00
func sendTCP4(discoverySession session.DiscoverySession, ps pcap.PCapScanner, host *omd.Host, stopChan chan struct{}) error {
2018-08-31 10:46:52 +00:00
dp := discoverySession.DiscoverPort()
tcpPacket, err := makePacketPortTCP4(host)
if nil != err {
return err
}
buf := gopacket.NewSerializeBuffer()
Loop:
for portNumber := dp.FirstScanRange; portNumber < dp.LastScanRange; portNumber++ {
if nil != dp.ExcludePorts {
for _, exPortNumber := range dp.ExcludePorts {
if portNumber == exPortNumber {
continue Loop
}
}
}
tcpPacket.TCP.DstPort = layers.TCPPort(portNumber)
if err := tcpPacket.TCP.SetNetworkLayerForChecksum(tcpPacket.IP); err != nil {
return err
}
if err := gopacket.SerializeLayers(buf, tcpPacket.Opts, tcpPacket.Eth, tcpPacket.IP, tcpPacket.TCP); err != nil {
return err
}
2018-09-28 08:46:05 +00:00
2018-08-31 10:46:52 +00:00
if err := ps.WritePacketData(buf.Bytes()); err != nil {
return err
}
select {
2018-09-28 08:46:05 +00:00
case <-time.After(time.Microsecond * 100):
2018-09-04 18:58:33 +00:00
case <-stopChan:
return nil
2018-08-31 10:46:52 +00:00
}
2018-09-28 09:46:46 +00:00
}
2018-09-28 08:46:05 +00:00
2018-08-31 10:46:52 +00:00
return nil
}
func handlePacketTCP4(discoverySession session.DiscoverySession, host *omd.Host, ports map[int]*omd.Port, packet *layers.TCP) *omd.Port {
if nil == packet || packet.DstPort != 60000 {
return nil
}
dp := discoverySession.DiscoverPort()
if packet.SYN && packet.ACK {
port := int(packet.SrcPort)
if _, ok := ports[port]; ok || !dp.Contains(port) {
return nil
}
// olog.Logger().Debug("Discovery", zap.String("ip", host.Address), zap.Int("port", port))
2018-09-12 04:55:51 +00:00
p := omd.NewPort(
host,
omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
port,
)
p.DiscoveredDate = omu.NowPtr()
2018-08-31 10:46:52 +00:00
ports[port] = p
return p
}
return nil
}
type PortPacketTCP4 struct {
Eth *layers.Ethernet
IP *layers.IPv4
TCP *layers.TCP
Opts gopacket.SerializeOptions
//PacketConn net.PacketConn
}
func makePacketPortTCP4(host *omd.Host) (*PortPacketTCP4, error) {
packetTCP := &PortPacketTCP4{}
srcIP := net.ParseIP(host.Zone.Address)
if nil == srcIP {
return nil, fmt.Errorf("IP(%s) of zone is not valid", host.Zone.Address)
}
dstIP := net.ParseIP(host.Address)
if nil == dstIP {
return nil, fmt.Errorf("IP(%s) of host is not valid", host.Address)
}
srcMac, err := net.ParseMAC(host.Zone.Mac)
if nil != err {
return nil, err
}
dstMac, err := net.ParseMAC(host.Mac)
if nil != err {
return nil, err
}
packetTCP.Eth = &layers.Ethernet{
SrcMAC: srcMac,
DstMAC: dstMac,
EthernetType: layers.EthernetTypeIPv4,
}
packetTCP.IP = &layers.IPv4{
SrcIP: srcIP,
DstIP: dstIP,
Version: 4,
TTL: 64,
Protocol: layers.IPProtocolTCP,
}
packetTCP.TCP = &layers.TCP{
SrcPort: 60000,
DstPort: 0, // will be incremented during the scan
SYN: true,
Seq: 0,
}
packetTCP.Opts = gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
}
return packetTCP, nil
}