forked from robertdavidgraham/masscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto-smb.c
2089 lines (1902 loc) · 77.9 KB
/
proto-smb.c
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
/*
SMB parser
*/
#include "proto-smb.h"
#include "proto-interactive.h"
#include "unusedparm.h"
#include "masscan-app.h"
#include "crypto-siphash24.h"
#include "string_s.h"
#include "unusedparm.h"
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <stddef.h>
/*
"NT LM 0.12" - Win2k
"SMB 2.002" 0x0202 Vista
"SMB 2.???" 0x02FF Win7, Windows 2008
"PC NETWORK PROGRAM 1.0" MS-DOS
"MICROSOFT NETWORKS 1.03" MS-DOS
"MICROSOFT NETWORKS 3.0" MS-DOS
"LANMAN 1.0" OS/2
"LM1.2X002" OS/2
*/
/*
PC NETWORK PROGRAM 1.0
LANMAN1.0
Windows for Workgroups 3.1a
LM1.2X002
LANMAN2.1
NT LM 0.12
SMB 2.002
SMB 2.???
Samba
XENIX CORE
*/
/*
References:
http://pubs.opengroup.org/onlinepubs/9697999099/toc.pdf
*/
struct SmbParams {
unsigned short command;
unsigned short external_offset;
unsigned char external_length;
unsigned char internal_type;
unsigned short internal_offset;
};
enum InternalType {
IT_uint0,
IT_uint8,
IT_uint16,
IT_uint32,
IT_uint64,
};
struct SmbParams params[] = {
/*
USHORT DialectIndex;
UCHAR SecurityMode;
USHORT MaxMpxCount;
USHORT MaxNumberVcs;
ULONG MaxBufferSize;
ULONG MaxRawSize;
ULONG SessionKey;
ULONG Capabilities;
FILETIME SystemTime;
SHORT ServerTimeZone;
UCHAR ChallengeLength;
*/
{0x72, 0, 2, IT_uint16, offsetof(struct Smb72_Negotiate, DialectIndex)},
{0x72, 2, 1, IT_uint8, offsetof(struct Smb72_Negotiate, SecurityMode)},
//{0x72, 3, 2, IT_uint16, offsetof(struct Smb72_Negotiate, MaxMpxCount)},
//{0x72, 5, 2, IT_uint16, offsetof(struct Smb72_Negotiate, MaxNumberVcs)},
//{0x72, 7, 4, IT_uint32, offsetof(struct Smb72_Negotiate, MaxBufferSize)},
//{0x72, 11, 4, IT_uint32, offsetof(struct Smb72_Negotiate, MaxRawSize)},
{0x72, 15, 4, IT_uint32, offsetof(struct Smb72_Negotiate, SessionKey)},
{0x72, 19, 4, IT_uint32, offsetof(struct Smb72_Negotiate, Capabilities)},
{0x72, 23, 8, IT_uint64, offsetof(struct Smb72_Negotiate, SystemTime)},
{0x72, 31, 2, IT_uint16, offsetof(struct Smb72_Negotiate, ServerTimeZone)},
{0x72, 33, 1, IT_uint8, offsetof(struct Smb72_Negotiate, ChallengeLength)},
{0x73, 6, 2, IT_uint16, offsetof(struct Smb73_Setup, BlobLength)},
{0xFF, 0, 0xFF, IT_uint0, 0},
};
#define memberat(t, s, offset) (t*)((char*)(s)+(offset))
static const char
smb1_hello_template[] = {
0x00, 0x00, 0x00, 0x45, 0xff, 0x53, 0x4d, 0x42,
0x72, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0xc8,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x00,
0xff, 0xff, 0x00, 0x00, 0x00, 0x22, 0x00, 0x02,
0x4e, 0x54, 0x20, 0x4c, 0x4d, 0x20, 0x30, 0x2e,
0x31, 0x32, 0x00, 0x02, 0x53, 0x4d, 0x42, 0x20,
0x32, 0x2e, 0x30, 0x30, 0x32, 0x00, 0x02, 0x53,
0x4d, 0x42, 0x20, 0x32, 0x2e, 0x3f, 0x3f, 0x3f,
0x00
};
static const char
smb1_hello_template_v1[] = {
0x00, 0x00, 0x00, 0x45, 0xff, 0x53, 0x4d, 0x42,
0x72, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0xc8,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x00,
0xff, 0xff, 0x00, 0x00, 0x00, 0x22, 0x00, 0x02,
0x4e, 0x54, 0x20, 0x4c, 0x4d, 0x20, 0x30, 0x2e,
0x31, 0x32, 0x00, 0x02, 0x53, 0x4d, 0x42, 0x20,
0x32, 0x2e, 0x30, 0x30, 0x32, 0x00, 0x02, 0x53,
0x4d, 0x42, 0x20, 0x32, 0x2e, 0x3f, 0x3f, 0x3f,
0x00
};
void smb_set_hello_v1(struct ProtocolParserStream *smb)
{
smb->hello = smb1_hello_template_v1;
smb->hello_length = sizeof(smb1_hello_template_v1);
}
static unsigned char smb1_null_session_setup[] = {
0x00, 0x00, 0x00, 0x7e, 0xff, 0x53, 0x4d, 0x42,
0x73, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0xc0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x00,
0xff, 0xff, 0x01, 0x00, 0x0d, 0xff, 0x00, 0x00,
0x00, 0x04, 0x41, 0x32, 0x00, 0xef, 0x00, 0x53,
0x45, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x5c, 0xc0, 0x80, 0x00, 0x41,
0x00, 0x00, 0x47, 0x00, 0x55, 0x00, 0x45, 0x00,
0x53, 0x00, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00,
0x4d, 0x00, 0x61, 0x00, 0x63, 0x00, 0x20, 0x00,
0x4f, 0x00, 0x53, 0x00, 0x20, 0x00, 0x58, 0x00,
0x20, 0x00, 0x31, 0x00, 0x30, 0x00, 0x2e, 0x00,
0x31, 0x00, 0x33, 0x00, 0x00, 0x00, 0x53, 0x00,
0x4d, 0x00, 0x42, 0x00, 0x46, 0x00, 0x53, 0x00,
0x20, 0x00, 0x33, 0x00, 0x2e, 0x00, 0x32, 0x00,
0x00, 0x00
};
static char smb1_null_session_setup_ex[] = {
0x00, 0x00, 0x00, 0xb8, 0xff, 0x53, 0x4d, 0x42,
0x73, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0xc8,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x00,
0x00, 0x00, 0x01, 0x00, 0x0c, 0xff, 0x00, 0x00,
0x00, 0x04, 0x41, 0x32, 0x00, 0xf1, 0x00, 0xa5,
0x12, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00,
0x00, 0x5c, 0xc0, 0x80, 0x80, 0x7d, 0x00, 0x60,
0x48, 0x06, 0x06, 0x2b, 0x06, 0x01, 0x05, 0x05,
0x02, 0xa0, 0x3e, 0x30, 0x3c, 0xa0, 0x0e, 0x30,
0x0c, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01,
0x82, 0x37, 0x02, 0x02, 0x0a, 0xa2, 0x2a, 0x04,
0x28, 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50,
0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x02, 0x88,
0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x06, 0x01, 0xb0, 0x1d, 0x0f, 0x00, 0x00,
0x00, 0x00, 0x4d, 0x00, 0x61, 0x00, 0x63, 0x00,
0x20, 0x00, 0x4f, 0x00, 0x53, 0x00, 0x20, 0x00,
0x58, 0x00, 0x20, 0x00, 0x31, 0x00, 0x30, 0x00,
0x2e, 0x00, 0x31, 0x00, 0x33, 0x00, 0x00, 0x00,
0x53, 0x00, 0x4d, 0x00, 0x42, 0x00, 0x46, 0x00,
0x53, 0x00, 0x20, 0x00, 0x33, 0x00, 0x2e, 0x00,
0x32, 0x00, 0x00, 0x00
};
char smb2_negotiate_request[] = {
0x00, 0x00, 0x00, 0x6c, 0xfe, 0x53, 0x4d, 0x42,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x04, 0x00,
0x02, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00,
0x17, 0x97, 0x90, 0x40, 0xcd, 0xf0, 0x5e, 0x31,
0x8d, 0xea, 0xef, 0x98, 0xcd, 0xa5, 0x08, 0xda,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 0x02, 0x10, 0x02, 0x00, 0x03, 0x02, 0x03
};
char smb2_null_session_setup[] = {
0x00, 0x00, 0x00, 0xa2, 0xfe, 0x53, 0x4d, 0x42,
0x40, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x02,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x58, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x60, 0x48, 0x06, 0x06,
0x2b, 0x06, 0x01, 0x05, 0x05, 0x02, 0xa0, 0x3e,
0x30, 0x3c, 0xa0, 0x0e, 0x30, 0x0c, 0x06, 0x0a,
0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02,
0x02, 0x0a, 0xa2, 0x2a, 0x04, 0x28, 0x4e, 0x54,
0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00, 0x01, 0x00,
0x00, 0x00, 0x15, 0x82, 0x88, 0x62, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01,
0xb0, 0x1d, 0x0f, 0x00, 0x00, 0x00
};
/*****************************************************************************
*
* ****** WARNING: UGLY HACK !!!! ******
*
* This code is an ugly hack so I can express SMB parameters (word_count)
* headers as a structure instead of writing individual parsers for them.
* This code makes no sense. If you find a bug in it, it's probably worth
* rewriting rather than figure out its convoluted logic. No really, I mean
* this.
*
*****************************************************************************/
static size_t
smb_params_parse(struct SMBSTUFF *smb, const unsigned char *px, size_t offset, size_t max)
{
size_t original_offset = offset;
size_t c;
if (max > offset + (smb->hdr.smb1.param_length - smb->hdr.smb1.param_offset))
max = offset + (smb->hdr.smb1.param_length - smb->hdr.smb1.param_offset);
/* Find the correct header */
for (c=0; params[c].command != smb->hdr.smb1.command && params[c].command != 0xFF; c++)
;
for (; offset < max; offset++, smb->hdr.smb1.param_offset++) {
again:
//printf("\n%u/%u %u\n", (unsigned)smb->hdr.smb1.param_offset, (unsigned)smb->hdr.smb1.param_length, (unsigned)c);
/* If we've gone past our header, just continue consuming bytes */
if (params[c].command != smb->hdr.smb1.command)
continue;
/* If we've gone past the end of this field, goto next field */
if (params[c].external_offset + params[c].external_length <= smb->hdr.smb1.param_offset) {
c++;
goto again;
}
/* Haven't reached the next field yet */
if (params[c].external_offset > smb->hdr.smb1.param_offset)
continue;
//printf("\n%u/%u %u [%02x]\n", (unsigned)smb->hdr.smb1.param_offset, (unsigned)smb->hdr.smb1.param_length, (unsigned)c, px[offset]);
/* Shift the type, because all fields little-endian */
switch (params[c].internal_type) {
case IT_uint0:
default:
break;
case IT_uint8:
{
uint8_t *x = memberat(uint8_t, &smb->parms, params[c].internal_offset);
*x = px[offset];
}
break;
case IT_uint16:
{
uint16_t *x = memberat(uint16_t, &smb->parms, params[c].internal_offset);
//*x <<= 8;
*x |= px[offset] << ((smb->hdr.smb1.param_offset - params[c].external_offset)*8);
}
break;
case IT_uint32:
{
uint32_t *x = memberat(uint32_t, &smb->parms, params[c].internal_offset);
//*x <<= 8;
*x |= px[offset] << ((smb->hdr.smb1.param_offset - params[c].external_offset)*8);
}
break;
case IT_uint64:
{
uint64_t *x = memberat(uint64_t, &smb->parms, params[c].internal_offset);
//*x <<= 8;
*x |= (uint64_t)px[offset] << (uint64_t)((smb->hdr.smb1.param_offset - params[c].external_offset)*8);
}
break;
}
}
/* Return the number of bytes processed */
return offset - original_offset;
}
/*****************************************************************************
*****************************************************************************/
static const unsigned long long TICKS_PER_SECOND = 10000000LL;
static const unsigned long long EPOCH_DIFFERENCE = 11644473600LL;
static time_t
convert_windows_time(long long int filetime)
{
unsigned long long seconds = filetime / TICKS_PER_SECOND;
seconds -= EPOCH_DIFFERENCE;
return (time_t)seconds;
}
/*****************************************************************************
*****************************************************************************/
/*****************************************************************************
*****************************************************************************/
static size_t
smb1_parse_negotiate1(struct SMBSTUFF *smb, const unsigned char *px, size_t offset, size_t max, struct BannerOutput *banout)
{
size_t original_offset = offset;
unsigned state = smb->hdr.smb1.byte_state;
enum {
D_NEGOT_CHALLENGE,
D_NEGOT_DOMAINA_PRE,
D_NEGOT_NAMEA_PRE,
D_NEGOT_DOMAINA,
D_NEGOT_NAMEA,
D_NEGOT_ENDA,
D_NEGOT_DOMAINU1,
D_NEGOT_DOMAINU2,
D_NEGOT_DOMAIN1,
D_NEGOT_DOMAIN2,
D_NEGOT_NAMEU1,
D_NEGOT_NAMEU2,
D_NEGOT_NAME1,
D_NEGOT_NAME2,
D_NEGOT_END,
D_UNKNOWN,
};
if (max > offset + (smb->hdr.smb1.byte_count - smb->hdr.smb1.byte_offset))
max = offset + (smb->hdr.smb1.byte_count - smb->hdr.smb1.byte_offset);
for (;offset<max; offset++)
switch (state) {
case D_NEGOT_CHALLENGE:
if (smb->parms.negotiate.ChallengeLength == 0) {
if (smb->hdr.smb1.flags2 & 0x8000) {
state = D_NEGOT_DOMAINU1;
} else {
state = D_NEGOT_DOMAINA_PRE;
}
offset--;
} else
smb->parms.negotiate.ChallengeLength--;
break;
case D_NEGOT_DOMAINU1:
case D_NEGOT_NAMEU1:
smb->hdr.smb1.unicode_char = px[offset];
state++;
break;
case D_NEGOT_DOMAINU2:
smb->hdr.smb1.unicode_char |= px[offset]<<8;
if (smb->hdr.smb1.unicode_char == 0) {
state = D_NEGOT_NAMEU1;
} else {
banout_append(banout, PROTO_SMB, " domain=", AUTO_LEN);
banout_append_unicode(banout, PROTO_SMB, smb->hdr.smb1.unicode_char);
state++;
}
break;
case D_NEGOT_NAMEU2:
smb->hdr.smb1.unicode_char |= px[offset]<<8;
if (smb->hdr.smb1.unicode_char == 0) {
state = D_NEGOT_END;
} else {
banout_append(banout, PROTO_SMB, " name=", AUTO_LEN);
banout_append_unicode(banout, PROTO_SMB, smb->hdr.smb1.unicode_char);
state++;
}
break;
case D_NEGOT_DOMAIN1:
case D_NEGOT_NAME1:
smb->hdr.smb1.unicode_char = px[offset];
state++;
break;
case D_NEGOT_DOMAIN2:
case D_NEGOT_NAME2:
smb->hdr.smb1.unicode_char |= px[offset]<<8;
if (smb->hdr.smb1.unicode_char == 0) {
state++;
} else {
banout_append_unicode(banout, PROTO_SMB, smb->hdr.smb1.unicode_char);
state--;
}
break;
case D_NEGOT_DOMAINA_PRE:
if (px[offset] == 0) {
state = D_NEGOT_NAMEA_PRE;
} else {
banout_append(banout, PROTO_SMB, " domain=", AUTO_LEN);
banout_append_char(banout, PROTO_SMB, px[offset]);
state = D_NEGOT_DOMAINA;
}
break;
case D_NEGOT_NAMEA_PRE:
if (px[offset] == 0) {
state = D_NEGOT_END;
} else {
banout_append(banout, PROTO_SMB, " name=", AUTO_LEN);
banout_append_char(banout, PROTO_SMB, px[offset]);
state = D_NEGOT_NAMEA;
}
break;
case D_NEGOT_DOMAINA:
case D_NEGOT_NAMEA:
if (px[offset] == 0) {
state++;
} else {
banout_append_char(banout, PROTO_SMB, px[offset]);
}
break;
default:
break;
}
smb->hdr.smb1.byte_state = (unsigned short)state;
smb->hdr.smb1.byte_offset += (unsigned short)(offset - original_offset);
return offset - original_offset;
}
/*****************************************************************************
*****************************************************************************/
static size_t
smb1_parse_setup1(struct SMBSTUFF *smb, const unsigned char *px, size_t offset, size_t max, struct BannerOutput *banout)
{
size_t original_offset = offset;
unsigned state = smb->hdr.smb1.byte_state;
enum {
D_PADDING,
D_OSA1,
D_OSA2,
D_VERSIONA1,
D_VERSIONA2,
D_DOMAINA1,
D_DOMAINA2,
D_ENDA,
D_OSU1,
D_OSU2,
D_OSU3,
D_OSU4,
D_VERSION1,
D_VERSION2,
D_VERSION3,
D_VERSION4,
D_DOMAIN1,
D_DOMAIN2,
D_DOMAIN3,
D_DOMAIN4,
D_UNKNOWN,
};
if (max > offset + (smb->hdr.smb1.byte_count - smb->hdr.smb1.byte_offset))
max = offset + (smb->hdr.smb1.byte_count - smb->hdr.smb1.byte_offset);
for (;offset<max; offset++) {
switch (state) {
case D_PADDING:
if (smb->hdr.smb1.flags2 & 0x8000) {
state = D_OSU1;
} else {
state = D_OSA1;
}
break;
case D_OSA1:
if (px[offset] == 0)
state = D_VERSIONA1;
else {
banout_append(banout, PROTO_SMB, " os=", AUTO_LEN);
banout_append_char(banout, PROTO_SMB, px[offset]);
state = D_OSA2;
}
break;
case D_OSA2:
if (px[offset] == 0)
state = D_VERSIONA1;
else
banout_append_char(banout, PROTO_SMB, px[offset]);
break;
case D_VERSIONA1:
if (px[offset] == 0)
state = D_DOMAINA1;
else {
banout_append(banout, PROTO_SMB, " ver=", AUTO_LEN);
banout_append_char(banout, PROTO_SMB, px[offset]);
state = D_VERSIONA2;
}
break;
case D_VERSIONA2:
if (px[offset] == 0)
state = D_DOMAINA1;
else
banout_append_char(banout, PROTO_SMB, px[offset]);
break;
case D_DOMAINA1:
if (px[offset] == 0)
state = D_UNKNOWN;
else {
banout_append(banout, PROTO_SMB, " domain=", AUTO_LEN);
banout_append_char(banout, PROTO_SMB, px[offset]);
state = D_DOMAINA2;
}
break;
case D_DOMAINA2:
if (px[offset] == 0)
state = D_UNKNOWN;
else
banout_append_char(banout, PROTO_SMB, px[offset]);
break;
case D_OSU1:
case D_OSU3:
case D_VERSION1:
case D_VERSION3:
case D_DOMAIN1:
case D_DOMAIN3:
smb->hdr.smb1.unicode_char = px[offset];
state++;
break;
case D_OSU2:
smb->hdr.smb1.unicode_char |= px[offset]<<8;
if (smb->hdr.smb1.unicode_char == 0)
state = D_VERSION1;
else {
banout_append(banout, PROTO_SMB, " os=", AUTO_LEN);
banout_append_unicode(banout, PROTO_SMB, smb->hdr.smb1.unicode_char);
state = D_OSU3;
}
break;
case D_OSU4:
smb->hdr.smb1.unicode_char |= px[offset]<<8;
if (smb->hdr.smb1.unicode_char == 0)
state = D_VERSION1;
else {
banout_append_unicode(banout, PROTO_SMB, smb->hdr.smb1.unicode_char);
state--;
}
break;
case D_VERSION2:
smb->hdr.smb1.unicode_char |= px[offset]<<8;
if (smb->hdr.smb1.unicode_char == 0)
state = D_DOMAIN1;
else {
banout_append(banout, PROTO_SMB, " ver=", AUTO_LEN);
banout_append_unicode(banout, PROTO_SMB, smb->hdr.smb1.unicode_char);
state = D_VERSION3;
}
break;
case D_VERSION4:
smb->hdr.smb1.unicode_char |= px[offset]<<8;
if (smb->hdr.smb1.unicode_char == 0)
state = D_DOMAIN1;
else {
banout_append_unicode(banout, PROTO_SMB, smb->hdr.smb1.unicode_char);
state--;
}
break;
case D_DOMAIN2:
smb->hdr.smb1.unicode_char |= px[offset]<<8;
if (smb->hdr.smb1.unicode_char == 0)
state = D_UNKNOWN;
else {
banout_append(banout, PROTO_SMB, " domain=", AUTO_LEN);
banout_append_unicode(banout, PROTO_SMB, smb->hdr.smb1.unicode_char);
state = D_DOMAIN3;
}
break;
case D_DOMAIN4:
smb->hdr.smb1.unicode_char |= px[offset]<<8;
if (smb->hdr.smb1.unicode_char == 0)
state = D_UNKNOWN;
else {
banout_append_unicode(banout, PROTO_SMB, smb->hdr.smb1.unicode_char);
state--;
}
break;
default:
break;
}
}
smb->hdr.smb1.byte_state = (unsigned short)state;
smb->hdr.smb1.byte_offset += (unsigned short)(offset - original_offset);
return offset - original_offset;
}
/*****************************************************************************
*****************************************************************************/
static size_t
smb1_parse_setup2(struct SMBSTUFF *smb, const unsigned char *px, size_t offset, size_t max, struct BannerOutput *banout)
{
size_t original_offset = offset;
unsigned state = smb->hdr.smb1.byte_state;
enum {
D_BLOB,
D_PADDING,
D_PADDING2,
D_OSA1,
D_OSA2,
D_VERSIONA1,
D_VERSIONA2,
D_DOMAINA1,
D_DOMAINA2,
D_ENDA,
D_OSU1,
D_OSU2,
D_OSU3,
D_OSU4,
D_VERSION1,
D_VERSION2,
D_VERSION3,
D_VERSION4,
D_DOMAIN1,
D_DOMAIN2,
D_DOMAIN3,
D_DOMAIN4,
D_UNKNOWN,
};
if (max > offset + (smb->hdr.smb1.byte_count - smb->hdr.smb1.byte_offset))
max = offset + (smb->hdr.smb1.byte_count - smb->hdr.smb1.byte_offset);
for (;offset<max; offset++) {
switch (state) {
case D_BLOB:
if (smb->parms.setup.BlobOffset == 0) {
spnego_decode_init(&smb->spnego, smb->parms.setup.BlobLength);
}
{
size_t new_max = max;
if (new_max > offset + smb->parms.setup.BlobLength - smb->parms.setup.BlobOffset)
new_max = offset + smb->parms.setup.BlobLength - smb->parms.setup.BlobOffset;
spnego_decode(&smb->spnego, px+offset, new_max-offset, banout);
smb->parms.setup.BlobOffset += (uint16_t)(new_max-offset);
offset = new_max;
if (smb->parms.setup.BlobLength - smb->parms.setup.BlobOffset == 0) {
offset--;
state = D_PADDING;
}
}
break;
case D_PADDING:
/* If the blog length is odd, then there is no padding. Otherwise,
* there is one byte of padding */
//if (smb->parms.setup.BlobLength & 1)
offset--;
state = D_PADDING2;
break;
case D_PADDING2:
if (smb->hdr.smb1.flags2 & 0x8000) {
state = D_OSU1;
} else {
state = D_OSA1;
}
offset--;
break;
case D_OSA1:
if (px[offset] == 0)
state = D_VERSIONA1;
else {
banout_append(banout, PROTO_SMB, " os=", AUTO_LEN);
banout_append_char(banout, PROTO_SMB, px[offset]);
state = D_OSA2;
}
break;
case D_OSA2:
if (px[offset] == 0)
state = D_VERSIONA1;
else
banout_append_char(banout, PROTO_SMB, px[offset]);
break;
case D_VERSIONA1:
if (px[offset] == 0)
state = D_DOMAINA1;
else {
banout_append(banout, PROTO_SMB, " ver=", AUTO_LEN);
banout_append_char(banout, PROTO_SMB, px[offset]);
state = D_VERSIONA2;
}
break;
case D_VERSIONA2:
if (px[offset] == 0)
state = D_DOMAINA1;
else
banout_append_char(banout, PROTO_SMB, px[offset]);
break;
case D_DOMAINA1:
if (px[offset] == 0)
state = D_UNKNOWN;
else {
banout_append(banout, PROTO_SMB, " domain=", AUTO_LEN);
banout_append_char(banout, PROTO_SMB, px[offset]);
state = D_DOMAINA2;
}
break;
case D_DOMAINA2:
if (px[offset] == 0)
state = D_UNKNOWN;
else
banout_append_char(banout, PROTO_SMB, px[offset]);
break;
case D_OSU1:
case D_OSU3:
case D_VERSION1:
case D_VERSION3:
case D_DOMAIN1:
case D_DOMAIN3:
smb->hdr.smb1.unicode_char = px[offset];
state++;
break;
case D_OSU2:
smb->hdr.smb1.unicode_char |= px[offset]<<8;
if (smb->hdr.smb1.unicode_char == 0)
state = D_VERSION1;
else {
banout_append(banout, PROTO_SMB, " os=", AUTO_LEN);
banout_append_unicode(banout, PROTO_SMB, smb->hdr.smb1.unicode_char);
state = D_OSU3;
}
break;
case D_OSU4:
smb->hdr.smb1.unicode_char |= px[offset]<<8;
if (smb->hdr.smb1.unicode_char == 0)
state = D_VERSION1;
else {
banout_append_unicode(banout, PROTO_SMB, smb->hdr.smb1.unicode_char);
state--;
}
break;
case D_VERSION2:
smb->hdr.smb1.unicode_char |= px[offset]<<8;
if (smb->hdr.smb1.unicode_char == 0)
state = D_DOMAIN1;
else {
banout_append(banout, PROTO_SMB, " ver=", AUTO_LEN);
banout_append_unicode(banout, PROTO_SMB, smb->hdr.smb1.unicode_char);
state = D_VERSION3;
}
break;
case D_VERSION4:
smb->hdr.smb1.unicode_char |= px[offset]<<8;
if (smb->hdr.smb1.unicode_char == 0)
state = D_DOMAIN1;
else {
banout_append_unicode(banout, PROTO_SMB, smb->hdr.smb1.unicode_char);
state--;
}
break;
case D_DOMAIN2:
smb->hdr.smb1.unicode_char |= px[offset]<<8;
if (smb->hdr.smb1.unicode_char == 0)
state = D_UNKNOWN;
else {
banout_append(banout, PROTO_SMB, " domain=", AUTO_LEN);
banout_append_unicode(banout, PROTO_SMB, smb->hdr.smb1.unicode_char);
state = D_DOMAIN3;
}
break;
case D_DOMAIN4:
smb->hdr.smb1.unicode_char |= px[offset]<<8;
if (smb->hdr.smb1.unicode_char == 0)
state = D_UNKNOWN;
else {
banout_append_unicode(banout, PROTO_SMB, smb->hdr.smb1.unicode_char);
state--;
}
break;
default:
break;
}
}
smb->hdr.smb1.byte_state = (unsigned short)state;
smb->hdr.smb1.byte_offset += (unsigned short)(offset - original_offset);
return offset - original_offset;
}
/*****************************************************************************
*****************************************************************************/
static size_t
smb1_parse_negotiate2(struct SMBSTUFF *smb, const unsigned char *px, size_t offset, size_t max, struct BannerOutput *banout)
{
size_t original_offset = offset;
unsigned state = smb->hdr.smb1.byte_state;
UNUSEDPARM(banout); UNUSEDPARM(px);
if (max > offset + (smb->hdr.smb1.byte_count - smb->hdr.smb1.byte_offset))
max = offset + (smb->hdr.smb1.byte_count - smb->hdr.smb1.byte_offset);
for (;offset<max; offset++)
switch (state) {
case 0:
state = 1;
break;
default:
break;
}
smb->hdr.smb1.byte_state = (unsigned short)state;
smb->hdr.smb1.byte_offset += (unsigned short)(offset - original_offset);
return offset - original_offset;
}
/*****************************************************************************
* A default parser for SMBv2 structs. The simplest implementation would be
* to simply skip the "struct_length" bytes. However, we have all this
* extra code to serve as a template for creating additional functions.
*****************************************************************************/
static size_t
smb2_parse_response(struct SMBSTUFF *smb, const unsigned char *px, size_t offset, size_t max, struct BannerOutput *banout)
{
size_t original_offset = offset;
unsigned state = smb->hdr.smb2.state;
UNUSEDPARM(banout);
UNUSEDPARM(px);
if (max > offset + (smb->hdr.smb2.struct_length - smb->hdr.smb2.offset))
max = offset + (smb->hdr.smb2.struct_length - smb->hdr.smb2.offset);
for (;offset<max; offset++)
switch (state) {
default:
break;
}
smb->hdr.smb2.state = (unsigned short)state;
smb->hdr.smb2.offset += (unsigned short)(offset - original_offset);
return offset - original_offset;
}
/*****************************************************************************
*****************************************************************************/
static size_t
smb2_parse_negotiate(struct SMBSTUFF *smb, const unsigned char *px, size_t offset, size_t max, struct BannerOutput *banout)
{
size_t original_offset = offset;
unsigned state = smb->hdr.smb2.state;
enum {
N_SECMOD1, N_SECMOD2,
N_DIALECT1, N_DIALECT2,
N_CONTEXTS1, N_CONTEXTS2,
N_GUID01, N_GUID02, N_GUID03, N_GUID04,
N_GUID05, N_GUID06, N_GUID07, N_GUID08,
N_GUID09, N_GUID10, N_GUID11, N_GUID12,
N_GUID13, N_GUID14, N_GUID15, N_GUID16,
N_CAP1, N_CAP2, N_CAP3, N_CAP4,
N_TRANSACTSIZE1, N_TRANSACTSIZE2, N_TRANSACTSIZE3, N_TRANSACTSIZE4,
N_READSIZE1, N_READSIZE2, N_READSIZE3, N_READSIZE4,
N_WRITESIZE1, N_WRITESIZE2, N_WRITESIZE3, N_WRITESIZE4,
N_TIME1, N_TIME2, N_TIME3, N_TIME4,
N_TIME5, N_TIME6, N_TIME7, N_TIME8,
N_BOOT1, N_BOOT2, N_BOOT3, N_BOOT4,
N_BOOT5, N_BOOT6, N_BOOT7, N_BOOT8,
N_BLOB_OFFSET1, N_BLOB_OFFSET2,
N_BLOB_LENGTH1, N_BLOB_LENGTH2,
};
/*
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Buffer Code | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+-+-+-+-+ +-+-+-+-+
| Server |
+-+-+-+-+ GUID +-+-+-+-+
| |
+-+-+-+-+ +-+-+-+-+
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+-+-+-+-+ Current Time +-+-+-+-+
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+-+-+-+-+ Boot Time +-+-+-+-+
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sec Blob Offset | Sec Blob Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sec Blob ANS.1/DER encoded blob containing supported authentication mechanisms
+-+-+-+-+...
*/
if (max > offset + (smb->hdr.smb2.struct_length - smb->hdr.smb2.offset))
max = offset + (smb->hdr.smb2.struct_length - smb->hdr.smb2.offset);
for (;offset<max; offset++)
switch (state) {
case N_SECMOD1: case N_SECMOD2:
case N_DIALECT1: case N_DIALECT2:
case N_CONTEXTS1:
state++;
break;
case N_CONTEXTS2:
if (!smb->is_printed_guid)
banout_append(banout, PROTO_SMB, " guid=", AUTO_LEN);
state++;
break;
case N_GUID01:
case N_GUID05:
case N_GUID07:
smb->hdr.smb2.number = px[offset];
state++;
break;
case N_GUID02: case N_GUID03: case N_GUID04:
smb->hdr.smb2.number |= px[offset] << (8*(state-N_GUID01));
if (state == N_GUID04 && !smb->is_printed_guid) {
banout_append_hexint(banout, PROTO_SMB, smb->hdr.smb2.number, 8);
banout_append_char(banout, PROTO_SMB, '-');
}
state++;
break;
case N_GUID06:
case N_GUID08:
smb->hdr.smb2.number |= px[offset] << 8;
if (!smb->is_printed_guid) {
banout_append_hexint(banout, PROTO_SMB, smb->hdr.smb2.number, 4);
banout_append_char(banout, PROTO_SMB, '-');
}
state++;
break;
case N_GUID10:
if (!smb->is_printed_guid) {
banout_append_hexint(banout, PROTO_SMB, px[offset], 2);
banout_append_char(banout, PROTO_SMB, '-');
}
state++;
break;
case N_GUID09: case N_GUID11: case N_GUID12:
case N_GUID13: case N_GUID14: case N_GUID15: case N_GUID16:
if (!smb->is_printed_guid)
banout_append_hexint(banout, PROTO_SMB, px[offset], 2);
if (state == N_GUID16)
smb->is_printed_guid = 1;
state++;
break;
case N_CAP1: case N_CAP2: case N_CAP3: case N_CAP4:
case N_TRANSACTSIZE1: case N_TRANSACTSIZE2: case N_TRANSACTSIZE3: case N_TRANSACTSIZE4:
case N_READSIZE1: case N_READSIZE2: case N_READSIZE3: case N_READSIZE4:
case N_WRITESIZE1: case N_WRITESIZE2: case N_WRITESIZE3: case N_WRITESIZE4:
state++;
break;
case N_TIME1: case N_TIME2: case N_TIME3: case N_TIME4: