| 1 | package http_proxy |
| 2 | |
| 3 | import ( |
| 4 | "github.com/bettercap/bettercap/session" |
| 5 | |
| 6 | "github.com/evilsocket/islazy/str" |
| 7 | ) |
| 8 | |
| 9 | type HttpProxy struct { |
| 10 | session.SessionModule |
| 11 | proxy *HTTPProxy |
| 12 | } |
| 13 | |
| 14 | func NewHttpProxy(s *session.Session) *HttpProxy { |
| 15 | mod := &HttpProxy{ |
| 16 | SessionModule: session.NewSessionModule("http.proxy", s), |
| 17 | proxy: NewHTTPProxy(s, "http.proxy"), |
| 18 | } |
| 19 | |
| 20 | mod.AddParam(session.NewIntParameter("http.port", |
| 21 | "80", |
| 22 | "HTTP port to redirect when the proxy is activated.")) |
| 23 | |
| 24 | mod.AddParam(session.NewStringParameter("http.proxy.address", |
| 25 | session.ParamIfaceAddress, |
| 26 | session.IPv4Validator, |
| 27 | "Address to bind the HTTP proxy to.")) |
| 28 | |
| 29 | mod.AddParam(session.NewIntParameter("http.proxy.port", |
| 30 | "8080", |
| 31 | "Port to bind the HTTP proxy to.")) |
| 32 | |
| 33 | mod.AddParam(session.NewBoolParameter("http.proxy.redirect", |
| 34 | "true", |
| 35 | "Enable or disable port redirection with iptables.")) |
| 36 | |
| 37 | mod.AddParam(session.NewStringParameter("http.proxy.script", |
| 38 | "", |
| 39 | "", |
| 40 | "Path of a proxy JS script.")) |
| 41 | |
| 42 | mod.AddParam(session.NewStringParameter("http.proxy.injectjs", |
| 43 | "", |
| 44 | "", |
| 45 | "URL, path or javascript code to inject into every HTML page.")) |
| 46 | |
| 47 | mod.AddParam(session.NewStringParameter("http.proxy.blacklist", "", "", |
| 48 | "Comma separated list of hostnames to skip while proxying (wildcard expressions can be used).")) |
| 49 | |
| 50 | mod.AddParam(session.NewStringParameter("http.proxy.whitelist", "", "", |
| 51 | "Comma separated list of hostnames to proxy if the blacklist is used (wildcard expressions can be used).")) |
| 52 | |
| 53 | mod.AddParam(session.NewBoolParameter("http.proxy.sslstrip", |
| 54 | "false", |
| 55 | "Enable or disable SSL stripping.")) |
| 56 | |
| 57 | mod.AddHandler(session.NewModuleHandler("http.proxy on", "", |
| 58 | "Start HTTP proxy.", |
| 59 | func(args []string) error { |
| 60 | return mod.Start() |
| 61 | })) |
| 62 | |
| 63 | mod.AddHandler(session.NewModuleHandler("http.proxy off", "", |
| 64 | "Stop HTTP proxy.", |
| 65 | func(args []string) error { |
| 66 | return mod.Stop() |
| 67 | })) |
| 68 | |
| 69 | mod.InitState("stripper") |
| 70 | |
| 71 | return mod |
| 72 | } |
| 73 | |
| 74 | func (mod *HttpProxy) Name() string { |
| 75 | return "http.proxy" |
| 76 | } |
| 77 | |
| 78 | func (mod *HttpProxy) Description() string { |
| 79 | return "A full featured HTTP proxy that can be used to inject malicious contents into webpages, all HTTP traffic will be redirected to it." |
| 80 | } |
| 81 | |
| 82 | func (mod *HttpProxy) Author() string { |
| 83 | return "Simone Margaritelli <[email protected]>" |
| 84 | } |
| 85 | |
| 86 | func (mod *HttpProxy) Configure() error { |
| 87 | var err error |
| 88 | var address string |
| 89 | var proxyPort int |
| 90 | var httpPort int |
| 91 | var doRedirect bool |
| 92 | var scriptPath string |
| 93 | var stripSSL bool |
| 94 | var jsToInject string |
| 95 | var blacklist string |
| 96 | var whitelist string |
| 97 | |
| 98 | if mod.Running() { |
| 99 | return session.ErrAlreadyStarted(mod.Name()) |
| 100 | } else if err, address = mod.StringParam("http.proxy.address"); err != nil { |
| 101 | return err |
| 102 | } else if err, proxyPort = mod.IntParam("http.proxy.port"); err != nil { |
| 103 | return err |
| 104 | } else if err, httpPort = mod.IntParam("http.port"); err != nil { |
| 105 | return err |
| 106 | } else if err, doRedirect = mod.BoolParam("http.proxy.redirect"); err != nil { |
| 107 | return err |
| 108 | } else if err, scriptPath = mod.StringParam("http.proxy.script"); err != nil { |
| 109 | return err |
| 110 | } else if err, stripSSL = mod.BoolParam("http.proxy.sslstrip"); err != nil { |
| 111 | return err |
| 112 | } else if err, jsToInject = mod.StringParam("http.proxy.injectjs"); err != nil { |
| 113 | return err |
| 114 | } else if err, blacklist = mod.StringParam("http.proxy.blacklist"); err != nil { |
| 115 | return err |
| 116 | } else if err, whitelist = mod.StringParam("http.proxy.whitelist"); err != nil { |
| 117 | return err |
| 118 | } |
| 119 | |
| 120 | mod.proxy.Blacklist = str.Comma(blacklist) |
| 121 | mod.proxy.Whitelist = str.Comma(whitelist) |
| 122 | |
| 123 | error := mod.proxy.Configure(address, proxyPort, httpPort, doRedirect, scriptPath, jsToInject, stripSSL) |
| 124 | |
| 125 | // save stripper to share it with other http(s) proxies |
| 126 | mod.State.Store("stripper", mod.proxy.Stripper) |
| 127 | |
| 128 | return error |
| 129 | } |
| 130 | |
| 131 | func (mod *HttpProxy) Start() error { |
| 132 | if err := mod.Configure(); err != nil { |
| 133 | return err |
| 134 | } |
| 135 | |
| 136 | return mod.SetRunning(true, func() { |
| 137 | mod.proxy.Start() |
| 138 | }) |
| 139 | } |
| 140 | |
| 141 | func (mod *HttpProxy) Stop() error { |
| 142 | mod.State.Store("stripper", nil) |
| 143 | return mod.SetRunning(false, func() { |
| 144 | mod.proxy.Stop() |
| 145 | }) |
| 146 | } |