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