| 1 | package tcp_proxy |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "net" |
| 7 | "sync" |
| 8 | |
| 9 | "github.com/bettercap/bettercap/firewall" |
| 10 | "github.com/bettercap/bettercap/session" |
| 11 | |
| 12 | "github.com/robertkrimen/otto" |
| 13 | ) |
| 14 | |
| 15 | type TcpProxy struct { |
| 16 | session.SessionModule |
| 17 | Redirection *firewall.Redirection |
| 18 | localAddr *net.TCPAddr |
| 19 | remoteAddr *net.TCPAddr |
| 20 | tunnelAddr *net.TCPAddr |
| 21 | listener *net.TCPListener |
| 22 | script *TcpProxyScript |
| 23 | } |
| 24 | |
| 25 | func NewTcpProxy(s *session.Session) *TcpProxy { |
| 26 | mod := &TcpProxy{ |
| 27 | SessionModule: session.NewSessionModule("tcp.proxy", s), |
| 28 | } |
| 29 | |
| 30 | mod.AddParam(session.NewIntParameter("tcp.port", |
| 31 | "443", |
| 32 | "Remote port to redirect when the TCP proxy is activated.")) |
| 33 | |
| 34 | mod.AddParam(session.NewStringParameter("tcp.address", |
| 35 | "", |
| 36 | session.IPv4Validator, |
| 37 | "Remote address of the TCP proxy.")) |
| 38 | |
| 39 | mod.AddParam(session.NewStringParameter("tcp.proxy.address", |
| 40 | session.ParamIfaceAddress, |
| 41 | session.IPv4Validator, |
| 42 | "Address to bind the TCP proxy to.")) |
| 43 | |
| 44 | mod.AddParam(session.NewIntParameter("tcp.proxy.port", |
| 45 | "8443", |
| 46 | "Port to bind the TCP proxy to.")) |
| 47 | |
| 48 | mod.AddParam(session.NewStringParameter("tcp.proxy.script", |
| 49 | "", |
| 50 | "", |
| 51 | "Path of a TCP proxy JS script.")) |
| 52 | |
| 53 | mod.AddParam(session.NewStringParameter("tcp.tunnel.address", |
| 54 | "", |
| 55 | "", |
| 56 | "Address to redirect the TCP tunnel to (optional).")) |
| 57 | |
| 58 | mod.AddParam(session.NewIntParameter("tcp.tunnel.port", |
| 59 | "0", |
| 60 | "Port to redirect the TCP tunnel to (optional).")) |
| 61 | |
| 62 | mod.AddHandler(session.NewModuleHandler("tcp.proxy on", "", |
| 63 | "Start TCP proxy.", |
| 64 | func(args []string) error { |
| 65 | return mod.Start() |
| 66 | })) |
| 67 | |
| 68 | mod.AddHandler(session.NewModuleHandler("tcp.proxy off", "", |
| 69 | "Stop TCP proxy.", |
| 70 | func(args []string) error { |
| 71 | return mod.Stop() |
| 72 | })) |
| 73 | |
| 74 | return mod |
| 75 | } |
| 76 | |
| 77 | func (mod *TcpProxy) Name() string { |
| 78 | return "tcp.proxy" |
| 79 | } |
| 80 | |
| 81 | func (mod *TcpProxy) Description() string { |
| 82 | return "A full featured TCP proxy and tunnel, all TCP traffic to a given remote address and port will be redirected to it." |
| 83 | } |
| 84 | |
| 85 | func (mod *TcpProxy) Author() string { |
| 86 | return "Simone Margaritelli <[email protected]>" |
| 87 | } |
| 88 | |
| 89 | func (mod *TcpProxy) Configure() error { |
| 90 | var err error |
| 91 | var port int |
| 92 | var proxyPort int |
| 93 | var address string |
| 94 | var proxyAddress string |
| 95 | var scriptPath string |
| 96 | var tunnelAddress string |
| 97 | var tunnelPort int |
| 98 | |
| 99 | if mod.Running() { |
| 100 | return session.ErrAlreadyStarted(mod.Name()) |
| 101 | } else if err, address = mod.StringParam("tcp.address"); err != nil { |
| 102 | return err |
| 103 | } else if err, proxyAddress = mod.StringParam("tcp.proxy.address"); err != nil { |
| 104 | return err |
| 105 | } else if err, proxyPort = mod.IntParam("tcp.proxy.port"); err != nil { |
| 106 | return err |
| 107 | } else if err, port = mod.IntParam("tcp.port"); err != nil { |
| 108 | return err |
| 109 | } else if err, tunnelAddress = mod.StringParam("tcp.tunnel.address"); err != nil { |
| 110 | return err |
| 111 | } else if err, tunnelPort = mod.IntParam("tcp.tunnel.port"); err != nil { |
| 112 | return err |
| 113 | } else if err, scriptPath = mod.StringParam("tcp.proxy.script"); err != nil { |
| 114 | return err |
| 115 | } else if mod.localAddr, err = net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", proxyAddress, proxyPort)); err != nil { |
| 116 | return err |
| 117 | } else if mod.remoteAddr, err = net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", address, port)); err != nil { |
| 118 | return err |
| 119 | } else if mod.tunnelAddr, err = net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", tunnelAddress, tunnelPort)); err != nil { |
| 120 | return err |
| 121 | } else if mod.listener, err = net.ListenTCP("tcp", mod.localAddr); err != nil { |
| 122 | return err |
| 123 | } |
| 124 | |
| 125 | if scriptPath != "" { |
| 126 | if err, mod.script = LoadTcpProxyScript(scriptPath, mod.Session); err != nil { |
| 127 | return err |
| 128 | } else { |
| 129 | mod.Debug("script %s loaded.", scriptPath) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if !mod.Session.Firewall.IsForwardingEnabled() { |
| 134 | mod.Info("enabling forwarding.") |
| 135 | mod.Session.Firewall.EnableForwarding(true) |
| 136 | } |
| 137 | |
| 138 | mod.Redirection = firewall.NewRedirection(mod.Session.Interface.Name(), |
| 139 | "TCP", |
| 140 | port, |
| 141 | proxyAddress, |
| 142 | proxyPort) |
| 143 | |
| 144 | mod.Redirection.SrcAddress = address |
| 145 | |
| 146 | if err := mod.Session.Firewall.EnableRedirection(mod.Redirection, true); err != nil { |
| 147 | return err |
| 148 | } |
| 149 | |
| 150 | mod.Debug("applied redirection %s", mod.Redirection.String()) |
| 151 | |
| 152 | return nil |
| 153 | } |
| 154 | |
| 155 | func (mod *TcpProxy) doPipe(from, to net.Addr, src *net.TCPConn, dst io.ReadWriter, wg *sync.WaitGroup) { |
| 156 | defer wg.Done() |
| 157 | |
| 158 | buff := make([]byte, 0xffff) |
| 159 | for { |
| 160 | n, err := src.Read(buff) |
| 161 | if err != nil { |
| 162 | if err.Error() != "EOF" { |
| 163 | mod.Warning("read failed: %s", err) |
| 164 | } |
| 165 | return |
| 166 | } |
| 167 | b := buff[:n] |
| 168 | |
| 169 | if mod.script != nil { |
| 170 | ret := mod.script.OnData(from, to, b, func(call otto.FunctionCall) otto.Value { |
| 171 | mod.Debug("onData dropCallback called") |
| 172 | src.Close() |
| 173 | return otto.Value{} |
| 174 | }) |
| 175 | |
| 176 | if ret != nil { |
| 177 | nret := len(ret) |
| 178 | mod.Info("overriding %d bytes of data from %s to %s with %d bytes of new data.", |
| 179 | n, from.String(), to.String(), nret) |
| 180 | b = make([]byte, nret) |
| 181 | copy(b, ret) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | n, err = dst.Write(b) |
| 186 | if err != nil { |
| 187 | mod.Warning("write failed: %s", err) |
| 188 | return |
| 189 | } |
| 190 | |
| 191 | mod.Debug("%s -> %s : %d bytes", from.String(), to.String(), n) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | func (mod *TcpProxy) handleConnection(c *net.TCPConn) { |
| 196 | defer c.Close() |
| 197 | |
| 198 | mod.Info("got a connection from %s", c.RemoteAddr().String()) |
| 199 | |
| 200 | // tcp tunnel enabled |
| 201 | if mod.tunnelAddr.IP.To4() != nil { |
| 202 | mod.Info("tcp tunnel started ( %s -> %s )", mod.remoteAddr.String(), mod.tunnelAddr.String()) |
| 203 | mod.remoteAddr = mod.tunnelAddr |
| 204 | } |
| 205 | |
| 206 | remote, err := net.DialTCP("tcp", nil, mod.remoteAddr) |
| 207 | if err != nil { |
| 208 | mod.Warning("error while connecting to remote %s: %s", mod.remoteAddr.String(), err) |
| 209 | return |
| 210 | } |
| 211 | defer remote.Close() |
| 212 | |
| 213 | wg := sync.WaitGroup{} |
| 214 | wg.Add(2) |
| 215 | |
| 216 | // start pipeing |
| 217 | go mod.doPipe(c.RemoteAddr(), mod.remoteAddr, c, remote, &wg) |
| 218 | go mod.doPipe(mod.remoteAddr, c.RemoteAddr(), remote, c, &wg) |
| 219 | |
| 220 | wg.Wait() |
| 221 | } |
| 222 | |
| 223 | func (mod *TcpProxy) Start() error { |
| 224 | if err := mod.Configure(); err != nil { |
| 225 | return err |
| 226 | } |
| 227 | |
| 228 | return mod.SetRunning(true, func() { |
| 229 | mod.Info("started ( x -> %s -> %s )", mod.localAddr.String(), mod.remoteAddr.String()) |
| 230 | |
| 231 | for mod.Running() { |
| 232 | conn, err := mod.listener.AcceptTCP() |
| 233 | if err != nil { |
| 234 | mod.Warning("error while accepting TCP connection: %s", err) |
| 235 | continue |
| 236 | } |
| 237 | |
| 238 | go mod.handleConnection(conn) |
| 239 | } |
| 240 | }) |
| 241 | } |
| 242 | |
| 243 | func (mod *TcpProxy) Stop() error { |
| 244 | |
| 245 | if mod.Redirection != nil { |
| 246 | mod.Debug("disabling redirection %s", mod.Redirection.String()) |
| 247 | if err := mod.Session.Firewall.EnableRedirection(mod.Redirection, false); err != nil { |
| 248 | return err |
| 249 | } |
| 250 | mod.Redirection = nil |
| 251 | } |
| 252 | |
| 253 | return mod.SetRunning(false, func() { |
| 254 | mod.listener.Close() |
| 255 | }) |
| 256 | } |