Debian Codegraph @

modules/net_sniff/net_sniff.go

raw symbols SCIP
1 package net_sniff
2
3 import (
4 "fmt"
5 "time"
6
7 "github.com/bettercap/bettercap/session"
8
9 "github.com/google/gopacket"
10 "github.com/google/gopacket/layers"
11 )
12
13 type Sniffer struct {
14 session.SessionModule
15 Stats *SnifferStats
16 Ctx *SnifferContext
17 pktSourceChan chan gopacket.Packet
18
19 fuzzActive bool
20 fuzzSilent bool
21 fuzzLayers []string
22 fuzzRate float64
23 fuzzRatio float64
24 }
25
26 func NewSniffer(s *session.Session) *Sniffer {
27 mod := &Sniffer{
28 SessionModule: session.NewSessionModule("net.sniff", s),
29 Stats: nil,
30 }
31
32 mod.SessionModule.Requires("net.recon")
33
34 mod.AddParam(session.NewBoolParameter("net.sniff.verbose",
35 "false",
36 "If true, every captured and parsed packet will be sent to the events.stream for displaying, otherwise only the ones parsed at the application layer (sni, http, etc)."))
37
38 mod.AddParam(session.NewBoolParameter("net.sniff.local",
39 "false",
40 "If true it will consider packets from/to this computer, otherwise it will skip them."))
41
42 mod.AddParam(session.NewStringParameter("net.sniff.filter",
43 "not arp",
44 "",
45 "BPF filter for the sniffer."))
46
47 mod.AddParam(session.NewStringParameter("net.sniff.regexp",
48 "",
49 "",
50 "If set, only packets matching this regular expression will be considered."))
51
52 mod.AddParam(session.NewStringParameter("net.sniff.output",
53 "",
54 "",
55 "If set, the sniffer will write captured packets to this file."))
56
57 mod.AddParam(session.NewStringParameter("net.sniff.source",
58 "",
59 "",
60 "If set, the sniffer will read from this pcap file instead of the current interface."))
61
62 mod.AddHandler(session.NewModuleHandler("net.sniff stats", "",
63 "Print sniffer session configuration and statistics.",
64 func(args []string) error {
65 if mod.Stats == nil {
66 return fmt.Errorf("No stats yet.")
67 }
68
69 mod.Ctx.Log(mod.Session)
70
71 return mod.Stats.Print()
72 }))
73
74 mod.AddHandler(session.NewModuleHandler("net.sniff on", "",
75 "Start network sniffer in background.",
76 func(args []string) error {
77 return mod.Start()
78 }))
79
80 mod.AddHandler(session.NewModuleHandler("net.sniff off", "",
81 "Stop network sniffer in background.",
82 func(args []string) error {
83 return mod.Stop()
84 }))
85
86 mod.AddHandler(session.NewModuleHandler("net.fuzz on", "",
87 "Enable fuzzing for every sniffed packet containing the specified layers.",
88 func(args []string) error {
89 return mod.StartFuzzing()
90 }))
91
92 mod.AddHandler(session.NewModuleHandler("net.fuzz off", "",
93 "Disable fuzzing",
94 func(args []string) error {
95 return mod.StopFuzzing()
96 }))
97
98 mod.AddParam(session.NewStringParameter("net.fuzz.layers",
99 "Payload",
100 "",
101 "Types of layer to fuzz."))
102
103 mod.AddParam(session.NewDecimalParameter("net.fuzz.rate",
104 "1.0",
105 "Rate in the [0.0,1.0] interval of packets to fuzz."))
106
107 mod.AddParam(session.NewDecimalParameter("net.fuzz.ratio",
108 "0.4",
109 "Rate in the [0.0,1.0] interval of bytes to fuzz for each packet."))
110
111 mod.AddParam(session.NewBoolParameter("net.fuzz.silent",
112 "false",
113 "If true it will not report fuzzed packets."))
114
115 return mod
116 }
117
118 func (mod Sniffer) Name() string {
119 return "net.sniff"
120 }
121
122 func (mod Sniffer) Description() string {
123 return "Sniff packets from the network."
124 }
125
126 func (mod Sniffer) Author() string {
127 return "Simone Margaritelli <[email protected]>"
128 }
129
130 func (mod Sniffer) isLocalPacket(packet gopacket.Packet) bool {
131 ip4l := packet.Layer(layers.LayerTypeIPv4)
132 if ip4l != nil {
133 ip4, _ := ip4l.(*layers.IPv4)
134 if ip4.SrcIP.Equal(mod.Session.Interface.IP) || ip4.DstIP.Equal(mod.Session.Interface.IP) {
135 return true
136 }
137 } else {
138 ip6l := packet.Layer(layers.LayerTypeIPv6)
139 if ip6l != nil {
140 ip6, _ := ip6l.(*layers.IPv6)
141 if ip6.SrcIP.Equal(mod.Session.Interface.IPv6) || ip6.DstIP.Equal(mod.Session.Interface.IPv6) {
142 return true
143 }
144 }
145 }
146 return false
147 }
148
149 func (mod *Sniffer) onPacketMatched(pkt gopacket.Packet) {
150 if mainParser(pkt, mod.Ctx.Verbose) {
151 mod.Stats.NumDumped++
152 }
153 }
154
155 func (mod *Sniffer) Configure() error {
156 var err error
157
158 if mod.Running() {
159 return session.ErrAlreadyStarted(mod.Name())
160 } else if err, mod.Ctx = mod.GetContext(); err != nil {
161 if mod.Ctx != nil {
162 mod.Ctx.Close()
163 mod.Ctx = nil
164 }
165 return err
166 }
167
168 return nil
169 }
170
171 func (mod *Sniffer) Start() error {
172 if err := mod.Configure(); err != nil {
173 return err
174 }
175
176 return mod.SetRunning(true, func() {
177 mod.Stats = NewSnifferStats()
178
179 src := gopacket.NewPacketSource(mod.Ctx.Handle, mod.Ctx.Handle.LinkType())
180 mod.pktSourceChan = src.Packets()
181 for packet := range mod.pktSourceChan {
182 if !mod.Running() {
183 mod.Debug("end pkt loop (pkt=%v filter='%s')", packet, mod.Ctx.Filter)
184 break
185 }
186
187 now := time.Now()
188 if mod.Stats.FirstPacket.IsZero() {
189 mod.Stats.FirstPacket = now
190 }
191 mod.Stats.LastPacket = now
192
193 isLocal := mod.isLocalPacket(packet)
194 if isLocal {
195 mod.Stats.NumLocal++
196 }
197
198 if mod.fuzzActive {
199 mod.doFuzzing(packet)
200 }
201
202 if mod.Ctx.DumpLocal || !isLocal {
203 data := packet.Data()
204 if mod.Ctx.Compiled == nil || mod.Ctx.Compiled.Match(data) {
205 mod.Stats.NumMatched++
206
207 mod.onPacketMatched(packet)
208
209 if mod.Ctx.OutputWriter != nil {
210 mod.Ctx.OutputWriter.WritePacket(packet.Metadata().CaptureInfo, data)
211 mod.Stats.NumWrote++
212 }
213 }
214 }
215 }
216
217 mod.pktSourceChan = nil
218 })
219 }
220
221 func (mod *Sniffer) Stop() error {
222 return mod.SetRunning(false, func() {
223 mod.Debug("stopping sniffer")
224 if mod.pktSourceChan != nil {
225 mod.Debug("sending nil")
226 mod.pktSourceChan <- nil
227 mod.Debug("nil sent")
228 }
229 mod.Debug("closing ctx")
230 mod.Ctx.Close()
231 mod.Debug("ctx closed")
232 })
233 }