forked from bettercap/bettercap
/
http_proxy_base_filters.go
99 lines (83 loc) · 2.21 KB
/
http_proxy_base_filters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package modules
import (
"net/http"
"strings"
"github.com/bettercap/bettercap/core"
"github.com/bettercap/bettercap/log"
"github.com/elazarl/goproxy"
)
func (p *HTTPProxy) onRequestFilter(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
log.Debug("(%s) < %s %s %s%s", core.Green(p.Name), req.RemoteAddr, req.Method, req.Host, req.URL.Path)
redir := p.stripper.Preprocess(req, ctx)
if redir != nil {
// we need to redirect the user in order to make
// some session cookie expire
return req, redir
}
// do we have a proxy script?
if p.Script == nil {
return req, nil
}
// run the module OnRequest callback if defined
jsreq, jsres := p.Script.OnRequest(req)
if jsreq != nil {
// the request has been changed by the script
p.logRequestAction(req, jsreq)
return jsreq.ToRequest(), nil
} else if jsres != nil {
// a fake response has been returned by the script
p.logResponseAction(req, jsres)
return req, jsres.ToResponse(req)
}
return req, nil
}
func (p *HTTPProxy) onResponseFilter(res *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
// sometimes it happens ¯\_(ツ)_/¯
if res == nil {
return nil
}
req := res.Request
log.Debug("(%s) > %s %s %s%s", core.Green(p.Name), req.RemoteAddr, req.Method, req.Host, req.URL.Path)
p.stripper.Process(res, ctx)
// do we have a proxy script?
if p.Script == nil {
return res
}
_, jsres := p.Script.OnResponse(res)
if jsres != nil {
// the response has been changed by the script
p.logResponseAction(res.Request, jsres)
return jsres.ToResponse(res.Request)
}
return res
}
func (p *HTTPProxy) logRequestAction(req *http.Request, jsreq *JSRequest) {
p.sess.Events.Add(p.Name+".spoofed-request", struct {
To string
Method string
Host string
Path string
Size int
}{
strings.Split(req.RemoteAddr, ":")[0],
jsreq.Method,
jsreq.Hostname,
jsreq.Path,
len(jsreq.Body),
})
}
func (p *HTTPProxy) logResponseAction(req *http.Request, jsres *JSResponse) {
p.sess.Events.Add(p.Name+".spoofed-response", struct {
To string
Method string
Host string
Path string
Size int
}{
strings.Split(req.RemoteAddr, ":")[0],
req.Method,
req.Host,
req.URL.Path,
len(jsres.Body),
})
}