-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
any_proxy.go
140 lines (113 loc) · 3.35 KB
/
any_proxy.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
package any_proxy
import (
"github.com/bettercap/bettercap/firewall"
"github.com/bettercap/bettercap/session"
)
type AnyProxy struct {
session.SessionModule
Redirection *firewall.Redirection
}
func NewAnyProxy(s *session.Session) *AnyProxy {
mod := &AnyProxy{
SessionModule: session.NewSessionModule("any.proxy", s),
}
mod.AddParam(session.NewStringParameter("any.proxy.iface",
session.ParamIfaceName,
"",
"Interface to redirect packets from."))
mod.AddParam(session.NewStringParameter("any.proxy.protocol",
"TCP",
"(TCP|UDP)",
"Proxy protocol."))
mod.AddParam(session.NewIntParameter("any.proxy.src_port",
"80",
"Remote port to redirect when the module is activated."))
mod.AddParam(session.NewStringParameter("any.proxy.src_address",
"",
"",
"Leave empty to intercept any source address."))
mod.AddParam(session.NewStringParameter("any.proxy.dst_address",
session.ParamIfaceAddress,
session.IPv4Validator,
"Address where the proxy is listening."))
mod.AddParam(session.NewIntParameter("any.proxy.dst_port",
"8080",
"Port where the proxy is listening."))
mod.AddHandler(session.NewModuleHandler("any.proxy on", "",
"Start the custom proxy redirection.",
func(args []string) error {
return mod.Start()
}))
mod.AddHandler(session.NewModuleHandler("any.proxy off", "",
"Stop the custom proxy redirection.",
func(args []string) error {
return mod.Stop()
}))
return mod
}
func (mod *AnyProxy) Name() string {
return "any.proxy"
}
func (mod *AnyProxy) Description() string {
return "A firewall redirection to any custom proxy."
}
func (mod *AnyProxy) Author() string {
return "Simone Margaritelli <evilsocket@gmail.com>"
}
func (mod *AnyProxy) Configure() error {
var err error
var srcPort int
var dstPort int
var iface string
var protocol string
var srcAddress string
var dstAddress string
if mod.Running() {
return session.ErrAlreadyStarted(mod.Name())
} else if err, iface = mod.StringParam("any.proxy.iface"); err != nil {
return err
} else if err, protocol = mod.StringParam("any.proxy.protocol"); err != nil {
return err
} else if err, srcPort = mod.IntParam("any.proxy.src_port"); err != nil {
return err
} else if err, dstPort = mod.IntParam("any.proxy.dst_port"); err != nil {
return err
} else if err, srcAddress = mod.StringParam("any.proxy.src_address"); err != nil {
return err
} else if err, dstAddress = mod.StringParam("any.proxy.dst_address"); err != nil {
return err
}
if !mod.Session.Firewall.IsForwardingEnabled() {
mod.Info("Enabling forwarding.")
mod.Session.Firewall.EnableForwarding(true)
}
mod.Redirection = firewall.NewRedirection(iface,
protocol,
srcPort,
dstAddress,
dstPort)
if srcAddress != "" {
mod.Redirection.SrcAddress = srcAddress
}
if err := mod.Session.Firewall.EnableRedirection(mod.Redirection, true); err != nil {
return err
}
mod.Info("Applied redirection %s", mod.Redirection.String())
return nil
}
func (mod *AnyProxy) Start() error {
if err := mod.Configure(); err != nil {
return err
}
return mod.SetRunning(true, func() {})
}
func (mod *AnyProxy) Stop() error {
if mod.Redirection != nil {
mod.Info("Disabling redirection %s", mod.Redirection.String())
if err := mod.Session.Firewall.EnableRedirection(mod.Redirection, false); err != nil {
return err
}
mod.Redirection = nil
}
return mod.SetRunning(false, func() {})
}