Debian Codegraph @

modules/syn_scan/syn_scan_reader.go

raw symbols SCIP
1 package syn_scan
2
3 import (
4 "sync/atomic"
5
6 "github.com/bettercap/bettercap/network"
7
8 "github.com/google/gopacket"
9 "github.com/google/gopacket/layers"
10
11 "github.com/evilsocket/islazy/async"
12 )
13
14 type OpenPort struct {
15 Proto string `json:"proto"`
16 Banner string `json:"banner"`
17 Service string `json:"service"`
18 Port int `json:"port"`
19 }
20
21 func (mod *SynScanner) onPacket(pkt gopacket.Packet) {
22 if pkt == nil || pkt.Data() == nil {
23 return
24 }
25
26 var eth layers.Ethernet
27 var ip4 layers.IPv4
28 var ip6 layers.IPv6
29 var tcp layers.TCP
30
31
32
33 isIPv6 := false
34 foundLayerTypes := []gopacket.LayerType{}
35 parser := gopacket.NewDecodingLayerParser(
36 layers.LayerTypeEthernet,
37 &eth,
38 &ip4,
39 &tcp,
40 )
41
42 err := parser.DecodeLayers(pkt.Data(), &foundLayerTypes)
43 if err != nil {
44 // try ipv6
45 parser := gopacket.NewDecodingLayerParser(
46 layers.LayerTypeEthernet,
47 &eth,
48 &ip6,
49 &tcp,
50 )
51 err = parser.DecodeLayers(pkt.Data(), &foundLayerTypes)
52 if err != nil {
53 return
54 }
55 isIPv6 = true
56 }
57
58 if tcp.DstPort == synSourcePort && tcp.SYN && tcp.ACK {
59 atomic.AddUint64(&mod.stats.openPorts, 1)
60
61 port := int(tcp.SrcPort)
62
63 openPort := &OpenPort{
64 Proto: "tcp",
65 Port: port,
66 Service: network.GetServiceByPort(port, "tcp"),
67 }
68
69 var host *network.Endpoint
70
71 from := ""
72
73 if isIPv6 {
74 from = ip6.SrcIP.String()
75 if ip6.SrcIP.Equal(mod.Session.Interface.IPv6) {
76 host = mod.Session.Interface
77 } else if ip6.SrcIP.Equal(mod.Session.Gateway.IPv6) {
78 host = mod.Session.Gateway
79 } else {
80 host = mod.Session.Lan.GetByIp(from)
81 }
82 } else {
83 from = ip4.SrcIP.String()
84
85 if ip4.SrcIP.Equal(mod.Session.Interface.IP) {
86 host = mod.Session.Interface
87 } else if ip4.SrcIP.Equal(mod.Session.Gateway.IP) {
88 host = mod.Session.Gateway
89 } else {
90 host = mod.Session.Lan.GetByIp(from)
91 }
92 }
93
94 if host != nil {
95 ports := host.Meta.GetOr("ports", map[int]*OpenPort{}).(map[int]*OpenPort)
96 if _, found := ports[port]; !found {
97 ports[port] = openPort
98 }
99 host.Meta.Set("ports", ports)
100 }
101
102 mod.bannerQueue.Add(async.Job(grabberJob{from, openPort}))
103
104 NewSynScanEvent(from, host, port).Push()
105 }
106 }