tcp syn scan added

This commit is contained in:
crusader 2018-08-31 19:46:52 +09:00
parent 0e953adb3d
commit fb41fef80c
3 changed files with 370 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package tcp
import (
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
"git.loafle.net/overflow_scanner/probe/discovery/session"
)
func Scan(discoverySession session.DiscoverySession, targetHost *omd.Host) error {
metaIPTypeEnum := omm.ToMetaIPTypeEnum(discoverySession.Zone().MetaIPType)
switch metaIPTypeEnum {
case omm.MetaIPTypeEnumV4:
return scanV4(discoverySession, targetHost)
case omm.MetaIPTypeEnumV6:
return scanV4(discoverySession, targetHost)
}
return nil
}

View File

@ -0,0 +1,207 @@
package tcp
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"
"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) error {
if nil == targetHost || nil == discoverySession.DiscoverPort() {
return nil
}
ps, err := pcap.RetainScanner(targetHost.Zone)
if nil != err {
return fmt.Errorf("Discovery: Cannot retain pcap instance %v", err)
}
defer func() {
go pcap.ReleaseScanner(targetHost.Zone)
}()
tcpChan := ps.OpenTCP(targetHost.Address)
defer func() {
go ps.CloseTCP(targetHost.Address, tcpChan)
}()
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 := <-tcpChan:
if !ok {
// olog.Logger().Debug("Discovery: tcp channel is closed")
return
}
delay.Store(true)
if p := handlePacketTCP4(discoverySession, targetHost, ports, packet); nil != p {
// resultChan <- p
log.Println(p)
}
case <-ticker.C:
if false == delay.Load().(bool) {
ticker.Stop()
timerStopped <- struct{}{}
return
}
delay.Store(false)
}
}
}()
if err := sendTCP4(discoverySession, ps, targetHost); nil != err {
log.Printf("sendTCP %v", err)
return nil
}
select {
case <-timerStopped:
return nil
}
}
func sendTCP4(discoverySession session.DiscoverySession, ps pcap.PCapScanner, host *omd.Host) 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
}
}
}
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
}
if err := ps.WritePacketData(buf.Bytes()); err != nil {
return err
}
timer := time.NewTimer(time.Microsecond * 100)
select {
case <-timer.C:
}
}
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.Port{
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
PortNumber: json.Number(strconv.Itoa(port)),
DiscoveredDate: omu.NowPtr(),
}
p.Host = host
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
}

View File

@ -0,0 +1,143 @@
package tcp
import (
"reflect"
"testing"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
"git.loafle.net/overflow_scanner/probe/discovery/session"
"git.loafle.net/overflow_scanner/probe/discovery/types"
"git.loafle.net/overflow_scanner/probe/internal/pcap"
"github.com/google/gopacket/layers"
)
func Test_scanV4(t *testing.T) {
s := session.NewMockDiscoverySession(
"testRequester",
types.DiscoveryRequestTypeHost,
&omd.Zone{
Network: "192.168.1.0/24",
Iface: "enp3s0",
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Address: "192.168.1.101",
Mac: "44:8a:5b:f1:f1:f3",
},
&omd.DiscoverHost{
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
FirstScanRange: "192.168.1.1",
LastScanRange: "192.168.1.254",
DiscoveryConfig: &omd.DiscoveryConfig{},
DiscoverPort: &omd.DiscoverPort{
FirstScanRange: 1,
LastScanRange: 1024,
IncludeTCP: true,
},
},
)
targetHost := &omd.Host{
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Name: "atGame",
Address: "192.168.1.1",
Mac: "00:11:32:7f:20:61",
Zone: s.Zone(),
}
type args struct {
discoverySession session.DiscoverySession
targetHost *omd.Host
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "1",
args: args{
discoverySession: s,
targetHost: targetHost,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := scanV4(tt.args.discoverySession, tt.args.targetHost); (err != nil) != tt.wantErr {
t.Errorf("scanV4() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_sendTCP4(t *testing.T) {
type args struct {
discoverySession session.DiscoverySession
ps pcap.PCapScanner
host *omd.Host
}
tests := []struct {
name string
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := sendTCP4(tt.args.discoverySession, tt.args.ps, tt.args.host); (err != nil) != tt.wantErr {
t.Errorf("sendTCP4() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_handlePacketTCP4(t *testing.T) {
type args struct {
discoverySession session.DiscoverySession
host *omd.Host
ports map[int]*omd.Port
packet *layers.TCP
}
tests := []struct {
name string
args args
want *omd.Port
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := handlePacketTCP4(tt.args.discoverySession, tt.args.host, tt.args.ports, tt.args.packet); !reflect.DeepEqual(got, tt.want) {
t.Errorf("handlePacketTCP4() = %v, want %v", got, tt.want)
}
})
}
}
func Test_makePacketPortTCP4(t *testing.T) {
type args struct {
host *omd.Host
}
tests := []struct {
name string
args args
want *PortPacketTCP4
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := makePacketPortTCP4(tt.args.host)
if (err != nil) != tt.wantErr {
t.Errorf("makePacketPortTCP4() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("makePacketPortTCP4() = %v, want %v", got, tt.want)
}
})
}
}