Debian Codegraph @

modules/net_sniff/net_sniff_context.go

raw symbols SCIP
1 package net_sniff
2
3 import (
4 "os"
5 "regexp"
6 "time"
7
8 "github.com/bettercap/bettercap/log"
9 "github.com/bettercap/bettercap/network"
10 "github.com/bettercap/bettercap/session"
11
12 "github.com/google/gopacket/pcap"
13 "github.com/google/gopacket/pcapgo"
14
15 "github.com/evilsocket/islazy/tui"
16 )
17
18 type SnifferContext struct {
19 Handle *pcap.Handle
20 Source string
21 DumpLocal bool
22 Verbose bool
23 Filter string
24 Expression string
25 Compiled *regexp.Regexp
26 Output string
27 OutputFile *os.File
28 OutputWriter *pcapgo.Writer
29 }
30
31 func (mod *Sniffer) GetContext() (error, *SnifferContext) {
32 var err error
33
34 ctx := NewSnifferContext()
35
36 if err, ctx.Source = mod.StringParam("net.sniff.source"); err != nil {
37 return err, ctx
38 }
39
40 if ctx.Source == "" {
41 /*
42 * We don't want to pcap.BlockForever otherwise pcap_close(handle)
43 * could hang waiting for a timeout to expire ...
44 */
45 readTimeout := 500 * time.Millisecond
46 if ctx.Handle, err = network.CaptureWithTimeout(mod.Session.Interface.Name(), readTimeout); err != nil {
47 return err, ctx
48 }
49 } else {
50 if ctx.Handle, err = pcap.OpenOffline(ctx.Source); err != nil {
51 return err, ctx
52 }
53 }
54
55 if err, ctx.Verbose = mod.BoolParam("net.sniff.verbose"); err != nil {
56 return err, ctx
57 }
58
59 if err, ctx.DumpLocal = mod.BoolParam("net.sniff.local"); err != nil {
60 return err, ctx
61 }
62
63 if err, ctx.Filter = mod.StringParam("net.sniff.filter"); err != nil {
64 return err, ctx
65 } else if ctx.Filter != "" {
66 err = ctx.Handle.SetBPFFilter(ctx.Filter)
67 if err != nil {
68 return err, ctx
69 }
70 }
71
72 if err, ctx.Expression = mod.StringParam("net.sniff.regexp"); err != nil {
73 return err, ctx
74 } else if ctx.Expression != "" {
75 if ctx.Compiled, err = regexp.Compile(ctx.Expression); err != nil {
76 return err, ctx
77 }
78 }
79
80 if err, ctx.Output = mod.StringParam("net.sniff.output"); err != nil {
81 return err, ctx
82 } else if ctx.Output != "" {
83 if ctx.OutputFile, err = os.Create(ctx.Output); err != nil {
84 return err, ctx
85 }
86
87 ctx.OutputWriter = pcapgo.NewWriter(ctx.OutputFile)
88 ctx.OutputWriter.WriteFileHeader(65536, ctx.Handle.LinkType())
89 }
90
91 return nil, ctx
92 }
93
94 func NewSnifferContext() *SnifferContext {
95 return &SnifferContext{
96 Handle: nil,
97 DumpLocal: false,
98 Verbose: false,
99 Filter: "",
100 Expression: "",
101 Compiled: nil,
102 Output: "",
103 OutputFile: nil,
104 OutputWriter: nil,
105 }
106 }
107
108 var (
109 no = tui.Red("no")
110 yes = tui.Green("yes")
111 yn = map[bool]string{
112 true: yes,
113 false: no,
114 }
115 )
116
117 func (c *SnifferContext) Log(sess *session.Session) {
118 log.Info("Skip local packets : %s", yn[c.DumpLocal])
119 log.Info("Verbose : %s", yn[c.Verbose])
120 log.Info("BPF Filter : '%s'", tui.Yellow(c.Filter))
121 log.Info("Regular expression : '%s'", tui.Yellow(c.Expression))
122 log.Info("File output : '%s'", tui.Yellow(c.Output))
123 }
124
125 func (c *SnifferContext) Close() {
126 if c.Handle != nil {
127 log.Debug("closing handle")
128 c.Handle.Close()
129 log.Debug("handle closed")
130 c.Handle = nil
131 }
132
133 if c.OutputFile != nil {
134 log.Debug("closing output")
135 c.OutputFile.Close()
136 log.Debug("output closed")
137 c.OutputFile = nil
138 }
139 }