/
events_view_http.go
198 lines (172 loc) · 5.05 KB
/
events_view_http.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package events_stream
import (
"bytes"
"compress/gzip"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/bettercap/bettercap/modules/net_sniff"
"net/url"
"regexp"
"strings"
"github.com/bettercap/bettercap/session"
"github.com/evilsocket/islazy/tui"
)
var (
reJsonKey = regexp.MustCompile(`("[^"]+"):`)
)
func (mod *EventsStream) shouldDumpHttpRequest(req net_sniff.HTTPRequest) bool {
if mod.dumpHttpReqs {
// dump all
return true
} else if req.Method != "GET" {
// dump if it's not just a GET
return true
}
// search for interesting headers and cookies
for name := range req.Headers {
headerName := strings.ToLower(name)
if strings.Contains(headerName, "auth") || strings.Contains(headerName, "token") {
return true
}
}
return false
}
func (mod *EventsStream) shouldDumpHttpResponse(res net_sniff.HTTPResponse) bool {
if mod.dumpHttpResp {
return true
} else if strings.Contains(res.ContentType, "text/plain") {
return true
} else if strings.Contains(res.ContentType, "application/json") {
return true
} else if strings.Contains(res.ContentType, "text/xml") {
return true
}
// search for interesting headers
for name := range res.Headers {
headerName := strings.ToLower(name)
if strings.Contains(headerName, "auth") || strings.Contains(headerName, "token") || strings.Contains(headerName, "cookie") {
return true
}
}
return false
}
func (mod *EventsStream) dumpForm(body []byte) string {
form := []string{}
for _, v := range strings.Split(string(body), "&") {
if strings.Contains(v, "=") {
parts := strings.SplitN(v, "=", 2)
name := parts[0]
value, err := url.QueryUnescape(parts[1])
if err != nil {
value = parts[1]
}
form = append(form, fmt.Sprintf("%s=%s", tui.Green(name), tui.Bold(tui.Red(value))))
} else {
value, err := url.QueryUnescape(v)
if err != nil {
value = v
}
form = append(form, fmt.Sprintf("%s", tui.Bold(tui.Red(value))))
}
}
return "\n" + strings.Join(form, "&") + "\n"
}
func (mod *EventsStream) dumpText(body []byte) string {
return "\n" + tui.Bold(tui.Red(string(body))) + "\n"
}
func (mod *EventsStream) dumpGZIP(body []byte) string {
buffer := bytes.NewBuffer(body)
uncompressed := bytes.Buffer{}
reader, err := gzip.NewReader(buffer)
if err != nil {
return mod.dumpRaw(body)
} else if _, err = uncompressed.ReadFrom(reader); err != nil {
return mod.dumpRaw(body)
}
return mod.dumpRaw(uncompressed.Bytes())
}
func (mod *EventsStream) dumpJSON(body []byte) string {
var buf bytes.Buffer
var pretty string
if err := json.Indent(&buf, body, "", " "); err != nil {
pretty = string(body)
} else {
pretty = string(buf.Bytes())
}
return "\n" + reJsonKey.ReplaceAllString(pretty, tui.Green(`$1:`)) + "\n"
}
func (mod *EventsStream) dumpXML(body []byte) string {
// TODO: indent xml
return "\n" + string(body) + "\n"
}
func (mod *EventsStream) dumpRaw(body []byte) string {
return "\n" + hex.Dump(body) + "\n"
}
func (mod *EventsStream) viewHttpRequest(e session.Event) {
se := e.Data.(net_sniff.SnifferEvent)
req := se.Data.(net_sniff.HTTPRequest)
fmt.Fprintf(mod.output, "[%s] [%s] %s\n",
e.Time.Format(mod.timeFormat),
tui.Green(e.Tag),
se.Message)
if mod.shouldDumpHttpRequest(req) {
dump := fmt.Sprintf("%s %s %s\n", tui.Bold(req.Method), req.URL, tui.Dim(req.Proto))
dump += fmt.Sprintf("%s: %s\n", tui.Blue("Host"), tui.Yellow(req.Host))
for name, values := range req.Headers {
for _, value := range values {
dump += fmt.Sprintf("%s: %s\n", tui.Blue(name), tui.Yellow(value))
}
}
if req.Body != nil {
if req.IsType("application/x-www-form-urlencoded") {
dump += mod.dumpForm(req.Body)
} else if req.IsType("text/plain") {
dump += mod.dumpText(req.Body)
} else if req.IsType("text/xml") {
dump += mod.dumpXML(req.Body)
} else if req.IsType("gzip") {
dump += mod.dumpGZIP(req.Body)
} else if req.IsType("application/json") {
dump += mod.dumpJSON(req.Body)
} else {
dump += mod.dumpRaw(req.Body)
}
}
fmt.Fprintf(mod.output, "\n%s\n", dump)
}
}
func (mod *EventsStream) viewHttpResponse(e session.Event) {
se := e.Data.(net_sniff.SnifferEvent)
res := se.Data.(net_sniff.HTTPResponse)
fmt.Fprintf(mod.output, "[%s] [%s] %s\n",
e.Time.Format(mod.timeFormat),
tui.Green(e.Tag),
se.Message)
if mod.shouldDumpHttpResponse(res) {
dump := fmt.Sprintf("%s %s\n", tui.Dim(res.Protocol), res.Status)
for name, values := range res.Headers {
for _, value := range values {
dump += fmt.Sprintf("%s: %s\n", tui.Blue(name), tui.Yellow(value))
}
}
if res.Body != nil {
// TODO: add more interesting response types
if res.IsType("text/plain") {
dump += mod.dumpText(res.Body)
} else if res.IsType("application/json") {
dump += mod.dumpJSON(res.Body)
} else if res.IsType("text/xml") {
dump += mod.dumpXML(res.Body)
}
}
fmt.Fprintf(mod.output, "\n%s\n", dump)
}
}
func (mod *EventsStream) viewHttpEvent(e session.Event) {
if e.Tag == "net.sniff.http.request" {
mod.viewHttpRequest(e)
} else if e.Tag == "net.sniff.http.response" {
mod.viewHttpResponse(e)
}
}