forked from bettercap/bettercap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet_proxy_linux.go
239 lines (195 loc) · 5.64 KB
/
packet_proxy_linux.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package packet_proxy
import (
"fmt"
"io/ioutil"
golog "log"
"plugin"
"strings"
"syscall"
"github.com/bettercap/bettercap/core"
"github.com/bettercap/bettercap/session"
"github.com/chifflier/nfqueue-go/nfqueue"
"github.com/evilsocket/islazy/fs"
"github.com/evilsocket/islazy/tui"
)
type PacketProxy struct {
session.SessionModule
done chan bool
chainName string
rule string
queue *nfqueue.Queue
queueNum int
queueCb nfqueue.Callback
pluginPath string
plugin *plugin.Plugin
}
// this is ugly, but since we can only pass a function
// (not a struct function) as a callback to nfqueue,
// we need this in order to recover the state.
var mod *PacketProxy
func NewPacketProxy(s *session.Session) *PacketProxy {
mod = &PacketProxy{
SessionModule: session.NewSessionModule("packet.proxy", s),
done: make(chan bool),
queue: nil,
queueCb: nil,
queueNum: 0,
chainName: "OUTPUT",
}
mod.AddHandler(session.NewModuleHandler("packet.proxy on", "",
"Start the NFQUEUE based packet proxy.",
func(args []string) error {
return mod.Start()
}))
mod.AddHandler(session.NewModuleHandler("packet.proxy off", "",
"Stop the NFQUEUE based packet proxy.",
func(args []string) error {
return mod.Stop()
}))
mod.AddParam(session.NewIntParameter("packet.proxy.queue.num",
"0",
"NFQUEUE number to bind to."))
mod.AddParam(session.NewStringParameter("packet.proxy.chain",
"OUTPUT",
"",
"Chain name of the iptables rule."))
mod.AddParam(session.NewStringParameter("packet.proxy.plugin",
"",
"",
"Go plugin file to load and call for every packet."))
mod.AddParam(session.NewStringParameter("packet.proxy.rule",
"",
"",
"Any additional iptables rule to make the queue more selective (ex. --destination 8.8.8.8)."))
return mod
}
func (mod PacketProxy) Name() string {
return "packet.proxy"
}
func (mod PacketProxy) Description() string {
return "A Linux only module that relies on NFQUEUEs in order to filter packets."
}
func (mod PacketProxy) Author() string {
return "Simone Margaritelli <evilsocket@gmail.com>"
}
func (mod *PacketProxy) destroyQueue() {
if mod.queue == nil {
return
}
mod.queue.DestroyQueue()
mod.queue.Close()
mod.queue = nil
}
func (mod *PacketProxy) runRule(enable bool) (err error) {
action := "-A"
if !enable {
action = "-D"
}
args := []string{
action, mod.chainName,
}
if mod.rule != "" {
rule := strings.Split(mod.rule, " ")
args = append(args, rule...)
}
args = append(args, []string{
"-j", "NFQUEUE",
"--queue-num", fmt.Sprintf("%d", mod.queueNum),
"--queue-bypass",
}...)
mod.Debug("iptables %s", args)
_, err = core.Exec("iptables", args)
return
}
func (mod *PacketProxy) Configure() (err error) {
golog.SetOutput(ioutil.Discard)
mod.destroyQueue()
if err, mod.queueNum = mod.IntParam("packet.proxy.queue.num"); err != nil {
return
} else if err, mod.chainName = mod.StringParam("packet.proxy.chain"); err != nil {
return
} else if err, mod.rule = mod.StringParam("packet.proxy.rule"); err != nil {
return
} else if err, mod.pluginPath = mod.StringParam("packet.proxy.plugin"); err != nil {
return
}
if mod.pluginPath == "" {
return fmt.Errorf("The parameter %s can not be empty.", tui.Bold("packet.proxy.plugin"))
} else if !fs.Exists(mod.pluginPath) {
return fmt.Errorf("%s does not exist.", mod.pluginPath)
}
mod.Info("loading packet proxy plugin from %s ...", mod.pluginPath)
var ok bool
var sym plugin.Symbol
if mod.plugin, err = plugin.Open(mod.pluginPath); err != nil {
return
} else if sym, err = mod.plugin.Lookup("OnPacket"); err != nil {
return
} else if mod.queueCb, ok = sym.(func(*nfqueue.Payload) int); !ok {
return fmt.Errorf("Symbol OnPacket is not a valid callback function.")
}
if sym, err = mod.plugin.Lookup("OnStart"); err == nil {
var onStartCb func() int
if onStartCb, ok = sym.(func() int); !ok {
return fmt.Errorf("OnStart signature does not match expected signature: 'func() int'")
} else {
var result int
if result = onStartCb(); result != 0 {
return fmt.Errorf("OnStart returned non-zero result. result=%d", result)
}
}
}
mod.queue = new(nfqueue.Queue)
if err = mod.queue.SetCallback(dummyCallback); err != nil {
return
} else if err = mod.queue.Init(); err != nil {
return
} else if err = mod.queue.Unbind(syscall.AF_INET); err != nil {
return
} else if err = mod.queue.Bind(syscall.AF_INET); err != nil {
return
} else if err = mod.queue.CreateQueue(mod.queueNum); err != nil {
return
} else if err = mod.queue.SetMode(nfqueue.NFQNL_COPY_PACKET); err != nil {
return
} else if err = mod.runRule(true); err != nil {
return
}
return nil
}
// we need this because for some reason we can't directly
// pass the symbol loaded from the plugin as a direct
// CGO callback ... ¯\_(ツ)_/¯
func dummyCallback(payload *nfqueue.Payload) int {
return mod.queueCb(payload)
}
func (mod *PacketProxy) Start() error {
if mod.Running() {
return session.ErrAlreadyStarted(mod.Name())
} else if err := mod.Configure(); err != nil {
return err
}
return mod.SetRunning(true, func() {
mod.Info("started on queue number %d", mod.queueNum)
defer mod.destroyQueue()
mod.queue.Loop()
mod.done <- true
})
}
func (mod *PacketProxy) Stop() (err error) {
return mod.SetRunning(false, func() {
mod.queue.StopLoop()
mod.runRule(false)
var sym plugin.Symbol
if sym, err = mod.plugin.Lookup("OnStop"); err == nil {
var onStopCb func()
var ok bool
if onStopCb, ok = sym.(func()); !ok {
mod.Error("OnStop signature does not match expected signature: 'func()', unable to call OnStop.")
} else {
onStopCb()
}
}
<-mod.done
})
}