http_proxy.go 3.9 KB

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