Debian Codegraph @

modules/any_proxy/any_proxy.go

raw symbols SCIP
1 package any_proxy
2
3 import (
4 "fmt"
5 "github.com/bettercap/bettercap/firewall"
6 "github.com/bettercap/bettercap/session"
7 "github.com/evilsocket/islazy/str"
8 "strconv"
9 "strings"
10 )
11
12 type AnyProxy struct {
13 session.SessionModule
14 // not using map[int]*firewall.Redirection to preserve order
15 ports []int
16 redirections []*firewall.Redirection
17 }
18
19 func NewAnyProxy(s *session.Session) *AnyProxy {
20 mod := &AnyProxy{
21 SessionModule: session.NewSessionModule("any.proxy", s),
22 }
23
24 mod.AddParam(session.NewStringParameter("any.proxy.iface",
25 session.ParamIfaceName,
26 "",
27 "Interface to redirect packets from."))
28
29 mod.AddParam(session.NewStringParameter("any.proxy.protocol",
30 "TCP",
31 "(TCP|UDP)",
32 "Proxy protocol."))
33
34 mod.AddParam(session.NewStringParameter("any.proxy.src_port",
35 "80",
36 "",
37 "Remote port to redirect when the module is activated, "+
38 "also supported a comma separated list of ports and/or port-ranges."))
39
40 mod.AddParam(session.NewStringParameter("any.proxy.src_address",
41 "",
42 "",
43 "Leave empty to intercept any source address."))
44
45 mod.AddParam(session.NewStringParameter("any.proxy.dst_address",
46 session.ParamIfaceAddress,
47 "",
48 "Address where the proxy is listening."))
49
50 mod.AddParam(session.NewIntParameter("any.proxy.dst_port",
51 "8080",
52 "Port where the proxy is listening."))
53
54 mod.AddHandler(session.NewModuleHandler("any.proxy on", "",
55 "Start the custom proxy redirection.",
56 func(args []string) error {
57 return mod.Start()
58 }))
59
60 mod.AddHandler(session.NewModuleHandler("any.proxy off", "",
61 "Stop the custom proxy redirection.",
62 func(args []string) error {
63 return mod.Stop()
64 }))
65
66 return mod
67 }
68
69 func (mod *AnyProxy) Name() string {
70 return "any.proxy"
71 }
72
73 func (mod *AnyProxy) Description() string {
74 return "A firewall redirection to any custom proxy."
75 }
76
77 func (mod *AnyProxy) Author() string {
78 return "Simone Margaritelli <[email protected]>"
79 }
80
81 func (mod *AnyProxy) Configure() error {
82 var err error
83 var srcPorts string
84 var dstPort int
85 var iface string
86 var protocol string
87 var srcAddress string
88 var dstAddress string
89
90 if mod.Running() {
91 return session.ErrAlreadyStarted(mod.Name())
92 } else if err, iface = mod.StringParam("any.proxy.iface"); err != nil {
93 return err
94 } else if err, protocol = mod.StringParam("any.proxy.protocol"); err != nil {
95 return err
96 } else if err, dstPort = mod.IntParam("any.proxy.dst_port"); err != nil {
97 return err
98 } else if err, srcAddress = mod.StringParam("any.proxy.src_address"); err != nil {
99 return err
100 } else if err, dstAddress = mod.StringParam("any.proxy.dst_address"); err != nil {
101 return err
102 }
103
104 if err, srcPorts = mod.StringParam("any.proxy.src_port"); err != nil {
105 return err
106 } else {
107 var ports []int
108 // srcPorts can be a single port, a list of ports or a list of ranges, or a mix.
109 for _, token := range str.Comma(str.Trim(srcPorts)) {
110 if p, err := strconv.Atoi(token); err == nil {
111 // simple case, integer port
112 ports = append(ports, p)
113 } else if strings.Contains(token, "-") {
114 // port range
115 if parts := strings.Split(token, "-"); len(parts) == 2 {
116 if from, err := strconv.Atoi(str.Trim(parts[0])); err != nil {
117 return fmt.Errorf("invalid start port %s: %s", parts[0], err)
118 } else if from < 1 || from > 65535 {
119 return fmt.Errorf("port %s out of valid range", parts[0])
120 } else if to, err := strconv.Atoi(str.Trim(parts[1])); err != nil {
121 return fmt.Errorf("invalid end port %s: %s", parts[1], err)
122 } else if to < 1 || to > 65535 {
123 return fmt.Errorf("port %s out of valid range", parts[1])
124 } else if from > to {
125 return fmt.Errorf("start port should be lower than end port")
126 } else {
127 for p := from; p <= to; p++ {
128 ports = append(ports, p)
129 }
130 }
131 } else {
132 return fmt.Errorf("can't parse '%s' as range", token)
133 }
134 } else {
135 return fmt.Errorf("can't parse '%s' as port or range", token)
136 }
137 }
138
139 // after parsing and validation, create a redirection per source port
140 mod.ports = ports
141 mod.redirections = nil
142 for _, port := range mod.ports {
143 redir := firewall.NewRedirection(iface,
144 protocol,
145 port,
146 dstAddress,
147 dstPort)
148
149 if srcAddress != "" {
150 redir.SrcAddress = srcAddress
151 }
152
153 mod.redirections = append(mod.redirections, redir)
154 }
155 }
156
157 if !mod.Session.Firewall.IsForwardingEnabled() {
158 mod.Info("Enabling forwarding.")
159 mod.Session.Firewall.EnableForwarding(true)
160 }
161
162 for _, redir := range mod.redirections {
163 if err := mod.Session.Firewall.EnableRedirection(redir, true); err != nil {
164 return err
165 }
166 mod.Info("applied redirection %s", redir.String())
167 }
168
169 return nil
170 }
171
172 func (mod *AnyProxy) Start() error {
173 if err := mod.Configure(); err != nil {
174 return err
175 }
176
177 return mod.SetRunning(true, func() {})
178 }
179
180 func (mod *AnyProxy) Stop() error {
181 for _, redir := range mod.redirections {
182 mod.Info("disabling redirection %s", redir.String())
183 if err := mod.Session.Firewall.EnableRedirection(redir, false); err != nil {
184 return err
185 }
186 }
187 return mod.SetRunning(false, func() {})
188 }