forked from bettercap/bettercap
/
tcp_proxy.go
250 lines (204 loc) · 6.13 KB
/
tcp_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
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
242
243
244
245
246
247
248
249
250
package tcp_proxy
import (
"fmt"
"io"
"net"
"sync"
"github.com/bettercap/bettercap/firewall"
"github.com/bettercap/bettercap/session"
)
type TcpProxy struct {
session.SessionModule
Redirection *firewall.Redirection
localAddr *net.TCPAddr
remoteAddr *net.TCPAddr
tunnelAddr *net.TCPAddr
listener *net.TCPListener
script *TcpProxyScript
}
func NewTcpProxy(s *session.Session) *TcpProxy {
mod := &TcpProxy{
SessionModule: session.NewSessionModule("tcp.proxy", s),
}
mod.AddParam(session.NewIntParameter("tcp.port",
"443",
"Remote port to redirect when the TCP proxy is activated."))
mod.AddParam(session.NewStringParameter("tcp.address",
"",
session.IPv4Validator,
"Remote address of the TCP proxy."))
mod.AddParam(session.NewStringParameter("tcp.proxy.address",
session.ParamIfaceAddress,
session.IPv4Validator,
"Address to bind the TCP proxy to."))
mod.AddParam(session.NewIntParameter("tcp.proxy.port",
"8443",
"Port to bind the TCP proxy to."))
mod.AddParam(session.NewStringParameter("tcp.proxy.script",
"",
"",
"Path of a TCP proxy JS script."))
mod.AddParam(session.NewStringParameter("tcp.tunnel.address",
"",
"",
"Address to redirect the TCP tunnel to (optional)."))
mod.AddParam(session.NewIntParameter("tcp.tunnel.port",
"0",
"Port to redirect the TCP tunnel to (optional)."))
mod.AddHandler(session.NewModuleHandler("tcp.proxy on", "",
"Start TCP proxy.",
func(args []string) error {
return mod.Start()
}))
mod.AddHandler(session.NewModuleHandler("tcp.proxy off", "",
"Stop TCP proxy.",
func(args []string) error {
return mod.Stop()
}))
return mod
}
func (mod *TcpProxy) Name() string {
return "tcp.proxy"
}
func (mod *TcpProxy) Description() string {
return "A full featured TCP proxy and tunnel, all TCP traffic to a given remote address and port will be redirected to it."
}
func (mod *TcpProxy) Author() string {
return "Simone Margaritelli <evilsocket@gmail.com>"
}
func (mod *TcpProxy) Configure() error {
var err error
var port int
var proxyPort int
var address string
var proxyAddress string
var scriptPath string
var tunnelAddress string
var tunnelPort int
if mod.Running() {
return session.ErrAlreadyStarted(mod.Name())
} else if err, address = mod.StringParam("tcp.address"); err != nil {
return err
} else if err, proxyAddress = mod.StringParam("tcp.proxy.address"); err != nil {
return err
} else if err, proxyPort = mod.IntParam("tcp.proxy.port"); err != nil {
return err
} else if err, port = mod.IntParam("tcp.port"); err != nil {
return err
} else if err, tunnelAddress = mod.StringParam("tcp.tunnel.address"); err != nil {
return err
} else if err, tunnelPort = mod.IntParam("tcp.tunnel.port"); err != nil {
return err
} else if err, scriptPath = mod.StringParam("tcp.proxy.script"); err != nil {
return err
} else if mod.localAddr, err = net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", proxyAddress, proxyPort)); err != nil {
return err
} else if mod.remoteAddr, err = net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", address, port)); err != nil {
return err
} else if mod.tunnelAddr, err = net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", tunnelAddress, tunnelPort)); err != nil {
return err
} else if mod.listener, err = net.ListenTCP("tcp", mod.localAddr); err != nil {
return err
}
if scriptPath != "" {
if err, mod.script = LoadTcpProxyScript(scriptPath, mod.Session); err != nil {
return err
} else {
mod.Debug("script %s loaded.", scriptPath)
}
}
if !mod.Session.Firewall.IsForwardingEnabled() {
mod.Info("enabling forwarding.")
mod.Session.Firewall.EnableForwarding(true)
}
mod.Redirection = firewall.NewRedirection(mod.Session.Interface.Name(),
"TCP",
port,
proxyAddress,
proxyPort)
mod.Redirection.SrcAddress = address
if err := mod.Session.Firewall.EnableRedirection(mod.Redirection, true); err != nil {
return err
}
mod.Debug("applied redirection %s", mod.Redirection.String())
return nil
}
func (mod *TcpProxy) doPipe(from, to net.Addr, src, dst io.ReadWriter, wg *sync.WaitGroup) {
defer wg.Done()
buff := make([]byte, 0xffff)
for {
n, err := src.Read(buff)
if err != nil {
if err.Error() != "EOF" {
mod.Warning("read failed: %s", err)
}
return
}
b := buff[:n]
if mod.script != nil {
ret := mod.script.OnData(from, to, b)
if ret != nil {
nret := len(ret)
mod.Info("overriding %d bytes of data from %s to %s with %d bytes of new data.",
n, from.String(), to.String(), nret)
b = make([]byte, nret)
copy(b, ret)
}
}
n, err = dst.Write(b)
if err != nil {
mod.Warning("write failed: %s", err)
return
}
mod.Debug("%s -> %s : %d bytes", from.String(), to.String(), n)
}
}
func (mod *TcpProxy) handleConnection(c *net.TCPConn) {
defer c.Close()
mod.Info("got a connection from %s", c.RemoteAddr().String())
// tcp tunnel enabled
if mod.tunnelAddr.IP.To4() != nil {
mod.Info("tcp tunnel started ( %s -> %s )", mod.remoteAddr.String(), mod.tunnelAddr.String())
mod.remoteAddr = mod.tunnelAddr
}
remote, err := net.DialTCP("tcp", nil, mod.remoteAddr)
if err != nil {
mod.Warning("error while connecting to remote %s: %s", mod.remoteAddr.String(), err)
return
}
defer remote.Close()
wg := sync.WaitGroup{}
wg.Add(2)
// start pipeing
go mod.doPipe(c.RemoteAddr(), mod.remoteAddr, c, remote, &wg)
go mod.doPipe(mod.remoteAddr, c.RemoteAddr(), remote, c, &wg)
wg.Wait()
}
func (mod *TcpProxy) Start() error {
if err := mod.Configure(); err != nil {
return err
}
return mod.SetRunning(true, func() {
mod.Info("started ( x -> %s -> %s )", mod.localAddr.String(), mod.remoteAddr.String())
for mod.Running() {
conn, err := mod.listener.AcceptTCP()
if err != nil {
mod.Warning("error while accepting TCP connection: %s", err)
continue
}
go mod.handleConnection(conn)
}
})
}
func (mod *TcpProxy) Stop() error {
if mod.Redirection != nil {
mod.Debug("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() {
mod.listener.Close()
})
}