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 240 241
|
package packet_proxy
import (
"fmt"
"plugin"
"context"
"strings"
"syscall"
"time"
"github.com/bettercap/bettercap/core"
"github.com/bettercap/bettercap/session"
nfqueue "github.com/florianl/go-nfqueue/v2"
"github.com/evilsocket/islazy/fs"
)
type PacketProxy struct {
session.SessionModule
chainName string
rule string
queue *nfqueue.Nfqueue
queueNum int
queueCb nfqueue.HookFunc
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),
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.Close()
mod.queue = nil
}
func (mod *PacketProxy) runRule(enable bool) (err error) {
action := "-I"
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),
}...)
mod.Debug("iptables %s", args)
_, err = core.Exec("iptables", args)
return
}
func (mod *PacketProxy) Configure() (err error) {
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 != "" {
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.Attribute) 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)
}
}
}
} else {
mod.Warning("no plugin set")
}
config := nfqueue.Config {
NfQueue: uint16(mod.queueNum),
Copymode: nfqueue.NfQnlCopyPacket,
AfFamily: syscall.AF_INET,
MaxPacketLen: 0xFFFF,
MaxQueueLen: 0xFF,
WriteTimeout: 15 * time.Millisecond,
}
mod.Debug("config: %+v", config)
if err = mod.runRule(true); err != nil {
return
} else if mod.queue, err = nfqueue.Open(&config); err != nil {
return
} else if err = mod.queue.RegisterWithErrorFunc(context.Background(), dummyCallback, func(e error) int {
mod.Error("%v", e)
return -1
}); 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(attribute nfqueue.Attribute) int {
if mod.queueCb != nil {
return mod.queueCb(attribute)
} else {
id := *attribute.PacketID
mod.Info("[%d] %v", id, *attribute.Payload)
mod.queue.SetVerdict(id, nfqueue.NfAccept)
return 0
}
}
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)
})
}
func (mod *PacketProxy) Stop() (err error) {
return mod.SetRunning(false, func() {
mod.runRule(false)
if mod.plugin != nil {
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()
}
}
}
})
}
|