probe/discovery/protocol/tcp/syn/synv4.go
crusader 212d3feb38 ing
2018-10-23 12:09:40 +09:00

232 lines
5.2 KiB
Go

package syn
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"
)
func scanV4(discoverySession session.DiscoverySession, targetHost *omd.Host) (err error) {
ps := discoverySession.PCapScanner()
if nil == ps {
err = fmt.Errorf("Cannot retain pcap instance")
return
}
tcpChan := ps.OpenTCP(targetHost.Address)
defer func() {
ps.CloseTCP(targetHost.Address, tcpChan)
}()
timerStopped := make(chan struct{}, 0)
stopChan := make(chan struct{})
defer close(stopChan)
var sendComplete atomic.Value
sendComplete.Store(false)
go func() {
defer func() {
close(timerStopped)
}()
ports := make(map[int]*omd.Port)
var delay atomic.Value
delay.Store(false)
ticker := time.NewTicker(time.Millisecond * 3000)
defer func() {
ticker.Stop()
}()
for {
select {
case packet, ok := <-tcpChan:
if !ok {
return
}
delay.Store(true)
if p := handlePacketTCP4(discoverySession, targetHost, ports, packet); nil != p {
go discoverySession.AddPort(discovererType, p, nil)
}
case <-ticker.C:
if false == sendComplete.Load().(bool) {
continue
}
if false == delay.Load().(bool) {
return
}
delay.Store(false)
}
}
}()
go func() {
if err = sendTCP4(discoverySession, ps, targetHost, stopChan); nil != err {
log.Printf("sendTCP %v", err)
return
}
sendComplete.Store(true)
}()
select {
case <-timerStopped:
return nil
case <-discoverySession.StopChan():
return nil
}
}
func sendTCP4(discoverySession session.DiscoverySession, ps pcap.PCapScanner, host *omd.Host, stopChan chan struct{}) error {
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
}
}
}
// startTime := time.Now()
tcpPacket.TCP.DstPort = layers.TCPPort(portNumber)
if err := tcpPacket.TCP.SetNetworkLayerForChecksum(tcpPacket.IP); err != nil {
return err
}
// if 80 == portNumber || 41111 == portNumber {
// log.Print("1 SYN IP:", host.Address, " Port:", portNumber, " t:", time.Since(startTime))
// }
if err := gopacket.SerializeLayers(buf, tcpPacket.Opts, tcpPacket.Eth, tcpPacket.IP, tcpPacket.TCP); err != nil {
return err
}
// if 80 == portNumber || 41111 == portNumber {
// log.Print("2 SYN IP:", host.Address, " Port:", portNumber, " t:", time.Since(startTime))
// }
if err := ps.WritePacketData(buf.Bytes()); err != nil {
return err
}
// if 80 == portNumber || 41111 == portNumber {
// log.Print("3 SYN IP:", host.Address, " Port:", portNumber, " t:", time.Since(startTime))
// }
select {
case <-time.After(time.Microsecond * 10):
case <-stopChan:
return nil
}
// if 80 == portNumber || 41111 == portNumber {
// log.Print("4 SYN IP:", host.Address, " Port:", portNumber, " t:", time.Since(startTime))
// }
}
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))
p := omd.NewPort(
host,
omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
port,
)
p.DiscoveredDate = omu.NowPtr()
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
}