Debian Codegraph @

modules/https_proxy/https_proxy.go

raw symbols SCIP
1 package https_proxy
2
3 import (
4 "github.com/bettercap/bettercap/modules/http_proxy"
5 "github.com/bettercap/bettercap/session"
6 "github.com/bettercap/bettercap/tls"
7
8 "github.com/evilsocket/islazy/fs"
9 "github.com/evilsocket/islazy/str"
10 )
11
12 type HttpsProxy struct {
13 session.SessionModule
14 proxy *http_proxy.HTTPProxy
15 }
16
17 func NewHttpsProxy(s *session.Session) *HttpsProxy {
18 mod := &HttpsProxy{
19 SessionModule: session.NewSessionModule("https.proxy", s),
20 proxy: http_proxy.NewHTTPProxy(s, "https.proxy"),
21 }
22
23 mod.AddParam(session.NewIntParameter("https.port",
24 "443",
25 "HTTPS port to redirect when the proxy is activated."))
26
27 mod.AddParam(session.NewStringParameter("https.proxy.address",
28 session.ParamIfaceAddress,
29 session.IPv4Validator,
30 "Address to bind the HTTPS proxy to."))
31
32 mod.AddParam(session.NewIntParameter("https.proxy.port",
33 "8083",
34 "Port to bind the HTTPS proxy to."))
35
36 mod.AddParam(session.NewBoolParameter("https.proxy.redirect",
37 "true",
38 "Enable or disable port redirection with iptables."))
39
40 mod.AddParam(session.NewBoolParameter("https.proxy.sslstrip",
41 "false",
42 "Enable or disable SSL stripping."))
43
44 mod.AddParam(session.NewStringParameter("https.proxy.injectjs",
45 "",
46 "",
47 "URL, path or javascript code to inject into every HTML page."))
48
49 mod.AddParam(session.NewStringParameter("https.proxy.certificate",
50 "~/.bettercap-ca.cert.pem",
51 "",
52 "HTTPS proxy certification authority TLS certificate file."))
53
54 mod.AddParam(session.NewStringParameter("https.proxy.key",
55 "~/.bettercap-ca.key.pem",
56 "",
57 "HTTPS proxy certification authority TLS key file."))
58
59 tls.CertConfigToModule("https.proxy", &mod.SessionModule, tls.DefaultSpoofConfig)
60
61 mod.AddParam(session.NewStringParameter("https.proxy.script",
62 "",
63 "",
64 "Path of a proxy JS script."))
65
66 mod.AddParam(session.NewStringParameter("https.proxy.blacklist", "", "",
67 "Comma separated list of hostnames to skip while proxying (wildcard expressions can be used)."))
68
69 mod.AddParam(session.NewStringParameter("https.proxy.whitelist", "", "",
70 "Comma separated list of hostnames to proxy if the blacklist is used (wildcard expressions can be used)."))
71
72 mod.AddHandler(session.NewModuleHandler("https.proxy on", "",
73 "Start HTTPS proxy.",
74 func(args []string) error {
75 return mod.Start()
76 }))
77
78 mod.AddHandler(session.NewModuleHandler("https.proxy off", "",
79 "Stop HTTPS proxy.",
80 func(args []string) error {
81 return mod.Stop()
82 }))
83
84 mod.InitState("stripper")
85
86 return mod
87 }
88
89 func (mod *HttpsProxy) Name() string {
90 return "https.proxy"
91 }
92
93 func (mod *HttpsProxy) Description() string {
94 return "A full featured HTTPS proxy that can be used to inject malicious contents into webpages, all HTTPS traffic will be redirected to it."
95 }
96
97 func (mod *HttpsProxy) Author() string {
98 return "Simone Margaritelli <[email protected]>"
99 }
100
101 func (mod *HttpsProxy) Configure() error {
102 var err error
103 var address string
104 var proxyPort int
105 var httpPort int
106 var doRedirect bool
107 var scriptPath string
108 var certFile string
109 var keyFile string
110 var stripSSL bool
111 var jsToInject string
112 var whitelist string
113 var blacklist string
114
115 if mod.Running() {
116 return session.ErrAlreadyStarted(mod.Name())
117 } else if err, address = mod.StringParam("https.proxy.address"); err != nil {
118 return err
119 } else if err, proxyPort = mod.IntParam("https.proxy.port"); err != nil {
120 return err
121 } else if err, httpPort = mod.IntParam("https.port"); err != nil {
122 return err
123 } else if err, doRedirect = mod.BoolParam("https.proxy.redirect"); err != nil {
124 return err
125 } else if err, stripSSL = mod.BoolParam("https.proxy.sslstrip"); err != nil {
126 return err
127 } else if err, certFile = mod.StringParam("https.proxy.certificate"); err != nil {
128 return err
129 } else if certFile, err = fs.Expand(certFile); err != nil {
130 return err
131 } else if err, keyFile = mod.StringParam("https.proxy.key"); err != nil {
132 return err
133 } else if keyFile, err = fs.Expand(keyFile); err != nil {
134 return err
135 } else if err, scriptPath = mod.StringParam("https.proxy.script"); err != nil {
136 return err
137 } else if err, jsToInject = mod.StringParam("https.proxy.injectjs"); err != nil {
138 return err
139 } else if err, blacklist = mod.StringParam("https.proxy.blacklist"); err != nil {
140 return err
141 } else if err, whitelist = mod.StringParam("https.proxy.whitelist"); err != nil {
142 return err
143 }
144
145 mod.proxy.Blacklist = str.Comma(blacklist)
146 mod.proxy.Whitelist = str.Comma(whitelist)
147
148 if !fs.Exists(certFile) || !fs.Exists(keyFile) {
149 cfg, err := tls.CertConfigFromModule("https.proxy", mod.SessionModule)
150 if err != nil {
151 return err
152 }
153
154 mod.Debug("%+v", cfg)
155 mod.Info("generating proxy certification authority TLS key to %s", keyFile)
156 mod.Info("generating proxy certification authority TLS certificate to %s", certFile)
157 if err := tls.Generate(cfg, certFile, keyFile, true); err != nil {
158 return err
159 }
160 } else {
161 mod.Info("loading proxy certification authority TLS key from %s", keyFile)
162 mod.Info("loading proxy certification authority TLS certificate from %s", certFile)
163 }
164
165 error := mod.proxy.ConfigureTLS(address, proxyPort, httpPort, doRedirect, scriptPath, certFile, keyFile, jsToInject,
166 stripSSL)
167
168 // save stripper to share it with other http(s) proxies
169 mod.State.Store("stripper", mod.proxy.Stripper)
170
171 return error
172 }
173
174 func (mod *HttpsProxy) Start() error {
175 if err := mod.Configure(); err != nil {
176 return err
177 }
178
179 return mod.SetRunning(true, func() {
180 mod.proxy.Start()
181 })
182 }
183
184 func (mod *HttpsProxy) Stop() error {
185 mod.State.Store("stripper", nil)
186 return mod.SetRunning(false, func() {
187 mod.proxy.Stop()
188 })
189 }