-
Notifications
You must be signed in to change notification settings - Fork 29
/
smbclient.c
1772 lines (1460 loc) · 62.2 KB
/
smbclient.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
/* smbclient.c
* By Ron
* Created August 26, 2008
*
* (See LICENSE.txt)
*/
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include "buffer.h"
#include "crypto.h"
#include "memory.h"
#include "nameservice.h"
#include "select_group.h"
#include "smbsession.h"
#include "smb.h"
#include "tcp.h"
#include "time.h"
#include "smbclient.h"
/* Grabbed the list of protocols from Windows 2000. */
/*static char *protocols[] = {"PC NETWORK PROGRAM 1.0", "LANMAN1.0", "Windows for Workgroups 3.1a", "LM1.2X002", "LANMAN2.1", "NT LM 0.12", (char*)0};*/
static char *protocols[] = {"NT LM 0.12", (char*)0};
/* Local function prototypes. */
static uint32_t smbclient_msrpc_parse(SMBCLIENT_t *smbclient, SMB_t *smb, size_t parameter_position, uint16_t parameter_size, size_t data_position, uint16_t data_size);
static uint32_t smbclient_parse_srvsvc_netshareenumall(buffer_t *data, NETSHAREENUMALL_t *result);
static uint32_t smbclient_parse_samr_connect4(buffer_t *data, CONNECT4_t *result);
static uint32_t smbclient_parse_samr_enumdomains(buffer_t *data, ENUMDOMAINS_t *result);
static uint32_t smbclient_parse_samr_lookupdomain(buffer_t *data, LOOKUPDOMAIN_t *result);
static uint32_t smbclient_parse_samr_opendomain(buffer_t *data, OPENDOMAIN_t *result);
static uint32_t smbclient_parse_samr_querydisplayinfo(buffer_t *data, QUERYDISPLAYINFO_t *result, SMBCLIENT_t *smbclient);
static uint32_t smbclient_parse_samr_querydomaininfo2(buffer_t *data, QUERYDOMAININFO2_t *result, SMBCLIENT_t *smbclient);
void parse_SMB_COM_TRANSACTION(SMBCLIENT_t *smbclient, SMB_t *smb)
{
if(smb_is_error(smb))
{
smbclient->protocol_error_callback(smbclient, smb->header.status, smb_get_error_nt(smb));
}
else
{
uint16_t parameter_count, parameter_offset;
uint16_t data_count, data_offset;
uint32_t result;
buffer_read_next_int16(smb->parameters[0].buffer); /* Total word count. */
buffer_read_next_int16(smb->parameters[0].buffer); /* Total data count. */
buffer_read_next_int16(smb->parameters[0].buffer); /* Reserved. */
parameter_count = buffer_read_next_int16(smb->parameters[0].buffer); /* Parameter count. */
parameter_offset = buffer_read_next_int16(smb->parameters[0].buffer); /* Parameter offset. */
buffer_read_next_int16(smb->parameters[0].buffer); /* Parameter displacement. */
data_count = buffer_read_next_int16(smb->parameters[0].buffer); /* Data count. */
data_offset = buffer_read_next_int16(smb->parameters[0].buffer); /* Data offset. */
buffer_read_next_int16(smb->parameters[0].buffer); /* Data displacement. */
buffer_read_next_int8(smb->parameters[0].buffer); /* Setup count. */
buffer_read_next_int8(smb->parameters[0].buffer); /* Reserved. */
/* Convert the parameter offset to the actual parameter offset. */
/* - 0x20 for the header, - 0x01 for the length. */
/* TODO: This isn't right. */
parameter_offset = parameter_offset - 0x20 - 0x01;
/* - 0x20 for the header, - 0x01 for parameter length, the parameter length, and - 0x02 for the data length. */
data_offset = data_offset - 0x20 - 0x01 - buffer_get_length(smb->parameters[0].buffer) - 0x02;
result = smbclient_msrpc_parse(smbclient, smb, parameter_offset, parameter_count, data_offset, data_count);
if(result)
{
smbclient->event_callback(smbclient, SMBCLIENT_MSRPC_ERROR, &result);
}
}
}
void send_SMB_COM_TRANSACTION(SMBCLIENT_t *smbclient, uint16_t function, buffer_t *parameters, buffer_t *data)
{
uint16_t parameter_offset;
uint16_t data_offset;
SMB_t *smb = smb_create(SMB_COM_TRANSACTION, 1, smbclient->uid, smbclient->tid, smbclient->check_signature);
buffer_add_int16(smb->parameters[0].buffer, 0x0000); /* Total parameter count -- the total bytes of parameters. */
buffer_add_int16(smb->parameters[0].buffer, buffer_get_length(data)); /* Total data count -- the total bytes of data. */
buffer_add_int16(smb->parameters[0].buffer, 0x0000); /* Max parameter count. */
buffer_add_int16(smb->parameters[0].buffer, 0x0400); /* Max data count. */
buffer_add_int8(smb->parameters[0].buffer, 0x00); /* Max setup count. */
buffer_add_int8(smb->parameters[0].buffer, 0x00); /* Reserved. */
buffer_add_int16(smb->parameters[0].buffer, 0x0000); /* Flags -- 0 = 2-way transaction, don't disconnect TID. */
buffer_add_int32(smb->parameters[0].buffer, 0x00000000); /* Timeout -- 0 = return immediately. */
buffer_add_int16(smb->parameters[0].buffer, 0x0000); /* Reserved. */
buffer_add_int16(smb->parameters[0].buffer, parameters ? buffer_get_length(parameters) : 0); /* Parameter count -- bytes of 'parameters'. */
/* Save the current location so we can set it when we know the offset. */
parameter_offset = buffer_get_length(smb->parameters[0].buffer);
buffer_add_int16(smb->parameters[0].buffer, 0xFFFF); /* Parameter offset -- distance from start of the packet (set this to a fake value for now). */
buffer_add_int16(smb->parameters[0].buffer, buffer_get_length(data)); /* Data count -- bytes of 'data'. */
/* Save the current location so we can set it when we know the offset. */
data_offset = buffer_get_length(smb->parameters[0].buffer);
buffer_add_int16(smb->parameters[0].buffer, 0xFFFF); /* Data offset -- distance from start of packet (set this to a fake value for now). */
buffer_add_int8(smb->parameters[0].buffer, 0x02); /* Setup count -- The number of "setup words" coming up. */
buffer_add_int8(smb->parameters[0].buffer, 0x00); /* Reserved. */
/* These are the "setup words". */
buffer_add_int16(smb->parameters[0].buffer, function); /* Function */
buffer_add_int16(smb->parameters[0].buffer, smbclient->fid); /* FID. */
/* Add the transaction name. In my experience, '\PIPE\' was the only thing I saw. */
if(smbclient->unicode)
buffer_add_unicode(smb->data[0].buffer, "\\PIPE\\");
else
buffer_add_ntstring(smb->data[0].buffer, "\\PIPE\\");
/* Set the parameter location. We subtract 4 due to the 4-byte NetBIOS header. */
buffer_add_int16_at(smb->parameters[0].buffer, smb_get_length(smb) - 4, parameter_offset);
/* Add the parameters, if there are any. */
if(parameters)
buffer_add_buffer(smb->data[0].buffer, parameters);
/* Set the data location. */
buffer_add_int16_at(smb->parameters[0].buffer, smb_get_length(smb) - 4, data_offset);
/* Add the data buffer. */
buffer_add_buffer(smb->data[0].buffer, data);
smb_send(smb, smbclient->s, &smbclient->sequence, smbclient->mac_key);
smb_destroy(smb);
}
void parse_SMB_COM_NT_CREATE_ANDX(SMBCLIENT_t *smbclient, SMB_t *smb, int index)
{
if(smb_is_error(smb))
{
smbclient->protocol_error_callback(smbclient, smb->header.status, smb_get_error_nt(smb));
}
else
{
uint16_t test;
/* Parameters. */
buffer_read_next_int8(smb->parameters[index].buffer); /* andx command. */
buffer_read_next_int8(smb->parameters[index].buffer); /* andx reserved. */
buffer_read_next_int16(smb->parameters[index].buffer); /* andx offset. */
buffer_read_next_int8(smb->parameters[index].buffer); /* Oplock level. */
smbclient->fid = buffer_read_next_int16(smb->parameters[index].buffer); /* FID. */
buffer_read_next_int32(smb->parameters[index].buffer); /* Create action. */
buffer_read_next_int32(smb->parameters[index].buffer); /* Created (high). */
buffer_read_next_int32(smb->parameters[index].buffer); /* Created (low). */
buffer_read_next_int32(smb->parameters[index].buffer); /* Last access (high). */
buffer_read_next_int32(smb->parameters[index].buffer); /* Last access (low). */
buffer_read_next_int32(smb->parameters[index].buffer); /* Last write (high). */
buffer_read_next_int32(smb->parameters[index].buffer); /* Last write (low). */
buffer_read_next_int32(smb->parameters[index].buffer); /* Change (high). */
buffer_read_next_int32(smb->parameters[index].buffer); /* Change (low). */
buffer_read_next_int32(smb->parameters[index].buffer); /* File attributes. */
buffer_read_next_int32(smb->parameters[index].buffer); /* Allocation size (high). */
buffer_read_next_int32(smb->parameters[index].buffer); /* Allocation size (low). */
buffer_read_next_int32(smb->parameters[index].buffer); /* End of file (high). */
buffer_read_next_int32(smb->parameters[index].buffer); /* End of file (low). */
buffer_read_next_int16(smb->parameters[index].buffer); /* File type. */
test = buffer_read_next_int16(smb->parameters[index].buffer); /* IPC State. */
buffer_read_next_int8(smb->parameters[index].buffer); /* is_directory. */
smbclient->event_callback(smbclient, SMBCLIENT_FILE_CREATED, NULL);
}
}
void send_SMB_COM_NT_CREATE_ANDX(SMBCLIENT_t *smbclient, char *path)
{
SMB_t *smb = smb_create(SMB_COM_NT_CREATE_ANDX, 1, smbclient->uid, smbclient->tid, smbclient->check_signature);
/* Generic ANDX stuff */
buffer_add_int8(smb->parameters[0].buffer, SMB_NO_FURTHER_COMMANDS); /* ANDX: Next command. */
buffer_add_int8(smb->parameters[0].buffer, 0); /* ANDX: Reserved. */
buffer_add_int16(smb->parameters[0].buffer, 0); /* ANDX: Next offset. */
buffer_add_int8(smb->parameters[0].buffer, 0x00); /* Reserved. */
if(smbclient->unicode)
buffer_add_int16(smb->parameters[0].buffer, strlen(path) * 2); /* Length of path. */
else
buffer_add_int16(smb->parameters[0].buffer, strlen(path)); /* Length of path. */
buffer_add_int32(smb->parameters[0].buffer, 0x00000016); /* Create flags. */
buffer_add_int32(smb->parameters[0].buffer, 0x00000000); /* Root FID. */
buffer_add_int32(smb->parameters[0].buffer, 0x0002019f); /* Access mask. */
buffer_add_int32(smb->parameters[0].buffer, 0x00000000); /* Allocation size (high). */
buffer_add_int32(smb->parameters[0].buffer, 0x00000000); /* Allocation size (low). */
buffer_add_int32(smb->parameters[0].buffer, 0x00000000); /* File attributes. */
buffer_add_int32(smb->parameters[0].buffer, 0x00000003); /* Share attributes. */
buffer_add_int32(smb->parameters[0].buffer, 0x00000001); /* Disposition. */
buffer_add_int32(smb->parameters[0].buffer, 0x00400040); /* Create options. */
buffer_add_int32(smb->parameters[0].buffer, 0x00000002); /* Impersonation. */
buffer_add_int8(smb->parameters[0].buffer, 0x01); /* Security flags. */
if(smbclient->unicode)
{
buffer_add_int8(smb->data[0].buffer, 0x00); /* Padding. */
buffer_add_unicode(smb->data[0].buffer, path); /* File name. */
}
else
{
buffer_add_ntstring(smb->data[0].buffer, path); /* File name. */
}
smb_send(smb, smbclient->s, &smbclient->sequence, smbclient->mac_key);
smb_destroy(smb);
}
void parse_SMB_COM_LOGOFF_ANDX(SMBCLIENT_t *smbclient, SMB_t *smb)
{
if(smb_is_error(smb))
{
smbclient->protocol_error_callback(smbclient, smb->header.status, smb_get_error_nt(smb));
}
else
{
smbclient->event_callback(smbclient, SMBCLIENT_SESSION_LOGOFF, NULL);
}
}
void send_SMB_COM_LOGOFF_ANDX(SMBCLIENT_t *smbclient)
{
SMB_t *smb = smb_create(SMB_COM_LOGOFF_ANDX, 1, smbclient->uid, smbclient->tid, smbclient->check_signature);
/* Default ANDX stuff. */
buffer_add_int8(smb->parameters[0].buffer, SMB_NO_FURTHER_COMMANDS); /* ANDX: Next command. */
buffer_add_int8(smb->parameters[0].buffer, 0); /* ANDX: Reserved. */
buffer_add_int16(smb->parameters[0].buffer, 0); /* ANDX: Next offset. */
smb_send(smb, smbclient->s, &smbclient->sequence, smbclient->mac_key);
smb_destroy(smb);
}
void parse_SMB_COM_TREE_DISCONNECT(SMBCLIENT_t *smbclient, SMB_t *smb)
{
if(smb_is_error(smb))
{
smbclient->protocol_error_callback(smbclient, smb->header.status, smb_get_error_nt(smb));
}
else
{
smbclient->event_callback(smbclient, SMBCLIENT_TREE_DISCONNECTED, NULL);
}
}
void send_SMB_COM_TREE_DISCONNECT(SMBCLIENT_t *smbclient)
{
SMB_t *smb = smb_create(SMB_COM_TREE_DISCONNECT, 1, smbclient->uid, smbclient->tid, smbclient->check_signature);
/* Has no parameters or data */
smb_send(smb, smbclient->s, &smbclient->sequence, smbclient->mac_key);
smb_destroy(smb);
}
void parse_SMB_COM_TREE_CONNECT_ANDX(SMBCLIENT_t *smbclient, SMB_t *smb)
{
char type[16];
if(smb_is_error(smb))
{
smbclient->protocol_error_callback(smbclient, smb->header.status, smb_get_error_nt(smb));
}
else
{
if(smbclient->unicode)
buffer_read_next_unicode(smb->data[0].buffer, type, 16);
else
buffer_read_next_ntstring(smb->data[0].buffer, type, 16);
smbclient->tid = smb->header.tid;
smbclient->event_callback(smbclient, SMBCLIENT_TREE_CONNECTED, type);
}
}
void send_SMB_COM_TREE_CONNECT_ANDX(SMBCLIENT_t *smbclient, char *path)
{
SMB_t *smb = smb_create(SMB_COM_TREE_CONNECT_ANDX, 1, smbclient->uid, smbclient->tid, smbclient->check_signature);
/* Default ANDX stuff. */
buffer_add_int8(smb->parameters[0].buffer, SMB_NO_FURTHER_COMMANDS); /* ANDX: Next command. */
buffer_add_int8(smb->parameters[0].buffer, 0); /* ANDX: Reserved. */
buffer_add_int16(smb->parameters[0].buffer, 0); /* ANDX: Next offset. */
buffer_add_int16(smb->parameters[0].buffer, 0); /* Flags. */
buffer_add_int16(smb->parameters[0].buffer, 0); /* Password length (for share-level security). */
/* Data. */
buffer_add_bytes(smb->data[0].buffer, "", 0); /* Password (for share-level security). */
if(smbclient->unicode)
{
buffer_add_int8(smb->data[0].buffer, 0x00); /* Padding. */
buffer_add_unicode(smb->data[0].buffer, path); /* Path. */
buffer_add_ntstring(smb->data[0].buffer, "?????"); /* Type. */
}
else
{
buffer_add_ntstring(smb->data[0].buffer, path); /* Path. */
buffer_add_ntstring(smb->data[0].buffer, "?????"); /* Type. */
}
smb_send(smb, smbclient->s, &smbclient->sequence, smbclient->mac_key);
smb_destroy(smb);
}
void parse_SMB_COM_ECHO(SMBCLIENT_t *smbclient, SMB_t *smb)
{
if(smb_is_error(smb))
{
smbclient->protocol_error_callback(smbclient, smb->header.status, smb_get_error_nt(smb));
}
else
{
printf("SMB Echo returned [%d bytes of data]\n", buffer_get_length(smb->data[0].buffer));
}
}
void send_SMB_COM_ECHO(SMBCLIENT_t *smbclient, uint8_t *data, size_t length, size_t count)
{
SMB_t *smb = smb_create(SMB_COM_ECHO, 1, smbclient->uid, smbclient->tid, smbclient->check_signature);
buffer_add_int16(smb->parameters[0].buffer, count);
buffer_add_bytes(smb->data[0].buffer, data, length);
smb_send(smb, smbclient->s, &smbclient->sequence, smbclient->mac_key);
smb_destroy(smb);
}
/* lm_length and ntlm_length are in/out parameters -- they specify the length of the buffer (should be at least 24 bytes, or 96 for v2), then the actual length is returned in them. */
static void get_logon_hashes_from_password(SMB_LOGONTYPE_t logon, char *password, char *username, char *domain, uint8_t *challenge, uint8_t *lm, uint8_t *ntlm, uint8_t *lm_length, uint8_t *ntlm_length, uint8_t mac_key[40])
{
uint8_t lanman_hash[16];
uint8_t ntlm_hash[16];
/* Verify buffer lengths. */
if(*lm_length < 24 || *ntlm_length < 24)
DIE("Buffers need to be 24-bytes or longer.");
lm_create_hash(password, lanman_hash);
ntlm_create_hash(password, ntlm_hash);
switch(logon)
{
case LOGONTYPE_ANONYMOUS:
*lm_length = 0;
*ntlm_length = 0;
break;
case LOGONTYPE_DEFAULT:
lm_create_response(lanman_hash, challenge, lm);
ntlm_create_response(ntlm_hash, challenge, ntlm);
*lm_length = 24;
*ntlm_length = 24;
/* For default, use the ntlm key (since it's the highest). */
ntlm_create_session_key(ntlm_hash, mac_key);
memcpy(mac_key + 16, ntlm, 24);
break;
case LOGONTYPE_LM:
lm_create_response(lanman_hash, challenge, lm);
*lm_length = 24;
*ntlm_length = 0;
/* For lanman, use the lanman key. */
lm_create_session_key(lanman_hash, mac_key);
memcpy(mac_key + 16, lm, 24);
break;
case LOGONTYPE_NTLM:
ntlm_create_response(ntlm_hash, challenge, lm);
ntlm_create_response(ntlm_hash, challenge, ntlm);
*lm_length = 24;
*ntlm_length = 24;
/* For ntlm, obviously use the ntlm key */
ntlm_create_session_key(ntlm_hash, mac_key);
memcpy(mac_key + 16, ntlm, 24);
break;
case LOGONTYPE_DEFAULTv2:
lmv2_create_response(ntlm_hash, username, domain, challenge, lm, lm_length);
ntlmv2_create_response(ntlm_hash, username, domain, challenge, ntlm, ntlm_length);
/* This function doesn't work, so we use a broken key. */
/* ntlmv2_create_session_key(ntlm_hash, username, domain, ntlm, mac_key);
memcpy(mac_key + 16, ntlm, 24); */
break;
case LOGONTYPE_LMv2:
lmv2_create_response(ntlm_hash, username, domain, challenge, lm, lm_length);
*ntlm_length = 0;
/* This function doesn't work, so we use a broken key. */
/* lmv2_create_session_key(ntlm_hash, username, domain, lm, mac_key);
memcpy(mac_key + 16, lm, 24); */
break;
default:
fprintf(stderr, "Sorry, don't know how to do that logon type!\n\n");
exit(1);
}
}
static void get_logon_hashes_from_hash(SMB_LOGONTYPE_t logon, char *hash, char *username, char *domain, uint8_t *challenge, uint8_t lm[16], uint8_t ntlm[16], uint8_t *lm_length, uint8_t *ntlm_length, uint8_t mac_key[40])
{
uint8_t hash1[16];
uint8_t hash2[16];
int count = string_to_hash(hash, hash1, hash2);
switch(logon)
{
case LOGONTYPE_ANONYMOUS:
*lm_length = 0;
*ntlm_length = 0;
break;
case LOGONTYPE_DEFAULT:
if(count == 0)
{
fprintf(stderr, "Error: invalid hash given, '%s'\n\n", hash);
exit(1);
}
else if(count == 1)
{
fprintf(stderr, "Error: both LM and NTLM hashes are required for 'default' logon type.\n\n");
exit(1);
}
lm_create_response(hash1, challenge, lm);
ntlm_create_response(hash2, challenge, ntlm);
*lm_length = 24;
*ntlm_length = 24;
/* For default, use the ntlm key (since it's the highest). */
ntlm_create_session_key(hash2, mac_key);
memcpy(mac_key + 16, ntlm, 24);
break;
case LOGONTYPE_LM:
if(count == 0)
{
fprintf(stderr, "Error: invalid hash given, '%s'\n\n", hash);
exit(1);
}
else if(count == 2)
{
fprintf(stderr, "Warning: two hashes given, ignoring second for LM login.\n");
}
lm_create_response(hash1, challenge, lm);
*lm_length = 24;
*ntlm_length = 0;
/* For lanman, use the lanman key. */
lm_create_session_key(hash1, mac_key);
memcpy(mac_key + 16, lm, 24);
break;
case LOGONTYPE_NTLM:
if(count == 0)
{
fprintf(stderr, "Error: invalid hash given, '%s'\n\n", hash);
exit(1);
}
else if(count == 2)
{
fprintf(stderr, "Warning: two hashes given, ignoring first for NTLM login.\n");
memcpy(hash1, hash2, 16);
}
ntlm_create_response(hash1, challenge, lm);
ntlm_create_response(hash1, challenge, ntlm);
*lm_length = 24;
*ntlm_length = 24;
/* For ntlm, obviously use the ntlm key */
ntlm_create_session_key(hash1, mac_key);
memcpy(mac_key + 16, ntlm, 24);
break;
case LOGONTYPE_DEFAULTv2:
if(count == 0)
{
fprintf(stderr, "Error: invalid hash given, '%s'\n\n", hash);
exit(1);
}
else if(count == 2)
{
fprintf(stderr, "Warning: two hashes given, ignoring first for v2 login.\n");
memcpy(hash1, hash2, 16);
}
lmv2_create_response(hash1, username, domain, challenge, lm, lm_length);
ntlmv2_create_response(hash1, username, domain, challenge, ntlm, ntlm_length);
/* This function doesn't work, so we use a broken key. */
/* ntlmv2_create_session_key(hash1, username, domain, ntlm, mac_key);
memcpy(mac_key + 16, ntlm, 24); */
break;
case LOGONTYPE_LMv2:
if(count == 0)
{
fprintf(stderr, "Error: invalid hash given, '%s'\n\n", hash);
exit(1);
}
else if(count == 2)
{
fprintf(stderr, "Warning: two hashes given, ignoring first for v2 login.\n");
memcpy(hash1, hash2, 16);
}
lmv2_create_response(hash1, username, domain, challenge, lm, lm_length);
*ntlm_length = 0;
/* This function doesn't work, so we use a broken key. */
/* lmv2_create_session_key(hash1, username, domain, ntlm, mac_key);
memcpy(mac_key + 16, lm, 24); */
break;
default:
fprintf(stderr, "Sorry, don't know how to do that logon type yet!\n\n");
exit(1);
}
}
void send_SMB_COM_SESSION_SETUP_ANDX(SMBCLIENT_t *smbclient, char *domain, char *username, char *password, char *hash, SMB_LOGONTYPE_t logon)
{
SMB_t *smb = smb_create(SMB_COM_SESSION_SETUP_ANDX, 1, smbclient->uid, smbclient->tid, smbclient->check_signature);
uint8_t lanman[24];
uint8_t ntlm[96];
/* The hash lengths will be determined by the user-chosen logon type. */
uint8_t lanman_length = 24;
uint8_t ntlm_length = 96;
/* Set the sequence number to 0 since the session key will shortly be calculated. */
smbclient->sequence = 0;
if(logon == LOGONTYPE_ANONYMOUS)
{
lanman_length = 0;
ntlm_length = 0;
}
else if(hash)
{
get_logon_hashes_from_hash(logon, hash, username, domain, smbclient->encryption_key, lanman, ntlm, &lanman_length, &ntlm_length, smbclient->mac_key);
}
else if(password)
{
get_logon_hashes_from_password(logon, password, username, domain, smbclient->encryption_key, lanman, ntlm, &lanman_length, &ntlm_length, smbclient->mac_key);
}
else
{
fprintf(stderr, "Please set a password with -p or -P!\n\n");
exit(1);
}
/* Parameter section. */
buffer_add_int8(smb_get_parameters(smb, 0), SMB_NO_FURTHER_COMMANDS);
buffer_add_int8(smb_get_parameters(smb, 0), 0); /* Reserved. */
buffer_add_int16(smb_get_parameters(smb, 0), 0); /* AndXOffset. */
buffer_add_int16(smb_get_parameters(smb, 0), 4096); /* Max buffer. */
buffer_add_int16(smb_get_parameters(smb, 0), 1); /* Max Mpx Count. */
buffer_add_int16(smb_get_parameters(smb, 0), 0); /* VC Number. */
buffer_add_int32(smb_get_parameters(smb, 0), smbclient->server_session_key); /* Session key. */
buffer_add_int16(smb_get_parameters(smb, 0), lanman_length); /* ANSI Password Length (LM). */
buffer_add_int16(smb_get_parameters(smb, 0), ntlm_length); /* Unicode Password Length (NTLM). */
buffer_add_int32(smb_get_parameters(smb, 0), 0); /* Reserved. */
buffer_add_int32(smb_get_parameters(smb, 0), smbclient->server_capabilities & (CAP_STATUS32 | CAP_NT_SMBS)); /* Capabilities. */
/* Data section. */
buffer_add_bytes(smb_get_data(smb, 0), lanman, lanman_length);
buffer_add_bytes(smb_get_data(smb, 0), ntlm, ntlm_length);
if(smbclient->unicode)
{
if(logon == LOGONTYPE_ANONYMOUS)
{
buffer_add_int8(smb_get_data(smb, 0), 0x00); /* Padding. */
buffer_add_unicode(smb_get_data(smb, 0), ""); /* Account. */
buffer_add_unicode(smb_get_data(smb, 0), ""); /* Primary Domain. */
}
else
{
buffer_add_unicode(smb_get_data(smb, 0), username); /* Account. */
buffer_add_unicode(smb_get_data(smb, 0), domain); /* Primary Domain. */
}
buffer_add_unicode(smb_get_data(smb, 0), "Windows 95/98/Me/NT/2k/XP"); /* Native OS. */
buffer_add_unicode(smb_get_data(smb, 0), "Native LanMan"); /* Native LAN Manager. */
}
else
{
if(logon == LOGONTYPE_ANONYMOUS)
{
buffer_add_ntstring(smb_get_data(smb, 0), ""); /* Account. */
buffer_add_ntstring(smb_get_data(smb, 0), ""); /* Primary Domain. */
}
else
{
buffer_add_ntstring(smb_get_data(smb, 0), username); /* Account. */
buffer_add_ntstring(smb_get_data(smb, 0), domain); /* Primary Domain. */
}
buffer_add_ntstring(smb_get_data(smb, 0), "Windows 95/98/Me/NT/2k/XP"); /* Native OS. */
buffer_add_ntstring(smb_get_data(smb, 0), "Native LanMan"); /* Native LAN Manager. */
}
smb_send(smb, smbclient->s, &smbclient->sequence, smbclient->mac_key);
smb_destroy(smb);
}
void parse_SMB_COM_SESSION_SETUP_ANDX(SMBCLIENT_t *smbclient, SMB_t *smb, size_t index)
{
uint16_t action;
char OS[1000];
char LANManager[1000];
char Domain[1000];
if(smb_is_error(smb))
{
smbclient->protocol_error_callback(smbclient, smb->header.status, smb_get_error_nt(smb));
}
else
{
buffer_read_next_int8(smb->parameters[index].buffer); /* andx command. */
buffer_read_next_int8(smb->parameters[index].buffer); /* andx reserved. */
buffer_read_next_int16(smb->parameters[index].buffer); /* andx offset. */
action = buffer_read_next_int16(smb->parameters[index].buffer);
/* Save the UID we were given. */
smbclient->uid = smb->header.uid;
/* We're now authenticated! */
smbclient->authenticated = TRUE;
if(smbclient->verbose)
{
if(smbclient->unicode)
{
printf(" -> OS: %s\n", buffer_read_unicode_at(smb->data[index].buffer, 0, OS, 1000));
printf(" -> Lan manager: %s\n", buffer_read_unicode_at(smb->data[index].buffer, 0 + strlen(OS) + 1, LANManager, 1000));
printf(" -> Domain: %s\n", buffer_read_unicode_at(smb->data[index].buffer, 0 + strlen(OS) + 1 + strlen(LANManager) + 1, Domain, 1000));
}
else
{
printf(" -> OS: %s\n", buffer_read_ntstring_at(smb->data[index].buffer, 0, OS, 1000));
printf(" -> Lan manager: %s\n", buffer_read_ntstring_at(smb->data[index].buffer, 0 + strlen(OS) + 1, LANManager, 1000));
printf(" -> Domain: %s\n", buffer_read_ntstring_at(smb->data[index].buffer, 0 + strlen(OS) + 1 + strlen(LANManager) + 1, Domain, 1000));
}
}
if(action & 1)
{
smbclient->event_callback(smbclient, SMBCLIENT_SESSION_SET_UP_GUEST, NULL);
}
else
{
smbclient->event_callback(smbclient, SMBCLIENT_SESSION_SET_UP, NULL);
}
}
}
/* This packet is sent first, as soon as the connection is established, and it contains a list of protocols that the
* client understands.
*
* s is the socket, protocols is a null-terminated list of strings. */
void send_SMB_COM_NEGOTIATE(SMBCLIENT_t *smbclient, char **protocols)
{
size_t i;
SMB_t *smb = smb_create(SMB_COM_NEGOTIATE, 1, smbclient->uid, smbclient->tid, smbclient->check_signature);
/* Parameters: none. */
/* Data: a Null-delimited list of understood protocols (with 0x02s appended) */
i = 0;
while(protocols[i])
{
buffer_add_int8(smb_get_data(smb, 0), 0x02);
buffer_add_ntstring(smb_get_data(smb, 0), protocols[i]);
i++;
}
smb_send(smb, smbclient->s, &smbclient->sequence, smbclient->mac_key);
smb_destroy(smb);
}
/* This is the response from the server. It contains important information like capabilities, the challenge value, etc. */
void parse_SMB_COM_NEGOTIATE(SMBCLIENT_t *smbclient, SMB_t *smb)
{
if(smb_is_error(smb))
{
smbclient->protocol_error_callback(smbclient, smb->header.status, smb_get_error_nt(smb));
}
else
{
uint16_t dialect = buffer_read_next_int16(smb->parameters[0].buffer);
uint8_t security_mode = buffer_read_next_int8(smb->parameters[0].buffer);
uint16_t max_mpx_count = buffer_read_next_int16(smb->parameters[0].buffer);
uint16_t max_number_vcs = buffer_read_next_int16(smb->parameters[0].buffer);
uint32_t max_buffer_size = buffer_read_next_int32(smb->parameters[0].buffer);
uint32_t max_raw_size = buffer_read_next_int32(smb->parameters[0].buffer);
uint32_t session_key = buffer_read_next_int32(smb->parameters[0].buffer);
SMB_CAPABILITIES_t capabilities = buffer_read_next_int32(smb->parameters[0].buffer);
uint32_t system_time_low = buffer_read_next_int32(smb->parameters[0].buffer);
uint32_t system_time_high = buffer_read_next_int32(smb->parameters[0].buffer);
smbclient->system_timezone = buffer_read_next_int16(smb->parameters[0].buffer);
smbclient->encryption_key_length = buffer_read_next_int8(smb->parameters[0].buffer);
/* Convert the system time. */
smbclient->system_time = (((uint64_t)system_time_high) << 32) | system_time_low;
smbclient->system_time_unix = (smbclient->system_time / 10000000LL) - 11644473600LL;
smbclient->server_capabilities = capabilities;
smbclient->server_session_key = session_key;
if(smbclient->verbose > 0)
{
printf("Lowest supported protocol: %s [0x%02x]\n", protocols[dialect], dialect);
printf("Security mode: %d\n", security_mode);
printf(" -> Signatures are %s\n", security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED ? "required" : "optional");
printf(" -> Message signing is %s\n", security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED ? "enabled" : "not enabled");
printf(" -> Plaintext passwords are %s\n", security_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE ? "not required" : "required");
printf(" -> Security is %s-level\n", security_mode & NEGOTIATE_SECURITY_USER_LEVEL ? "user" : "share");
printf("Max multiplex: %d\n", max_mpx_count);
printf("Max virtual circuits: %d\n", max_number_vcs);
printf("Max buffer: %d\n", max_buffer_size);
printf("Max raw size: %d\n", max_raw_size);
printf("Session key: %08x\n", session_key);
printf("Capabilities: %08x", capabilities);
smb_print_capabilities(capabilities);
printf("\n");
printf("System time: %s", ctime((time_t*)&smbclient->system_time_unix));
printf("UTC: %d minutes (%.2f hours)\n", smbclient->system_timezone, (smbclient->system_timezone / 60.0));
printf("Encryption key length: %d\n", smbclient->encryption_key_length);
printf("\n");
}
/* We only support user-level security. */
if(!(security_mode & NEGOTIATE_SECURITY_USER_LEVEL))
{
fprintf(stderr, "Server wants share-level security, we don't support that.\n\n");
exit(1);
}
/* We also only support challenge-response (not plaintext). */
if(!(security_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE))
{
fprintf(stderr, "Server doesn't understand challenge-response authentication, but that's all we can do.\n\n");
exit(1);
}
/* Make sure we were given a challenge. */
if(smbclient->encryption_key_length != 8)
{
fprintf(stderr, "Server didn't give us a 8-byte challenge value (it was %d bytes).\n\n", smbclient->encryption_key_length);
exit(1);
}
buffer_read_next_bytes(smb->data[0].buffer, smbclient->encryption_key, smbclient->encryption_key_length);
buffer_read_next_unicode(smb->data[0].buffer, smbclient->domain_name, 256);
buffer_read_next_unicode(smb->data[0].buffer, smbclient->server_name, 256);
if(smbclient->verbose > 0)
{
printf("Challenge: %x%x%x%x%x%x%x%x\n", smbclient->encryption_key[0], smbclient->encryption_key[1], smbclient->encryption_key[2], smbclient->encryption_key[3], smbclient->encryption_key[4], smbclient->encryption_key[5], smbclient->encryption_key[6], smbclient->encryption_key[7]);
}
smbclient->event_callback(smbclient, SMBCLIENT_PROTOCOL_NEGOTIATED, NULL);
}
}
void send_NETBIOS_SESSION_REQUEST(SMBCLIENT_t *smbclient, char *netbios_name)
{
char *called_name_e = name_encode(netbios_name, "", ' ', NAME_SERVER);
char *calling_name_e = name_encode("WINDOWS", "", ' ', NAME_SERVER);
buffer_t *buffer = buffer_create(BO_NETWORK);
uint8_t data[72]; /* Length is always 72. */
buffer_add_int8(buffer, SESSION_REQUEST);
buffer_add_int8(buffer, 0); /* Flags. */
buffer_add_int16(buffer, 68); /* Length is constant 72, -4 for header. */
buffer_add_ntstring(buffer, called_name_e);
buffer_add_ntstring(buffer, calling_name_e);
buffer_read_next_bytes(buffer, data, 72);
tcp_send(smbclient->s, data, 72);
buffer_destroy(buffer);
safe_free(calling_name_e);
safe_free(called_name_e);
}
void parse_NETBIOS_SESSION_RESPONSE(SMBCLIENT_t *smbclient, uint8_t *data)
{
buffer_t *buffer = buffer_create_with_data(BO_NETWORK, data, 4);
uint8_t response = buffer_read_next_int8(buffer);
if(response != SESSION_POSITIVE_RESPONSE)
smbclient->protocol_error_callback(smbclient, -1, "NetBIOS session rejected, likely due to an incorrect hostname.");
buffer_destroy(buffer);
}
SELECT_RESPONSE_t incoming_callback(void *group, int s, uint8_t *data, size_t length, char *addr, uint16_t port, void *param)
{
SMBCLIENT_t *smbclient = (SMBCLIENT_t*) param;
buffer_t *buffer;
switch(smbclient->state)
{
case RECV_STATE_SESSION_REQUEST:
{
/* Sanity check -- this should never happen with select_group. */
if(length != 4)
smbclient->protocol_error_callback(smbclient, -1, "Received incorrect number of bytes for NetBIOS header.");
/* If this actually returns, we're set. */
parse_NETBIOS_SESSION_RESPONSE(smbclient, data);
/* We start by receiving a header. */
smbclient->state = RECV_STATE_HEADER;
/* NetBIOS Header is 4 bytes. */
select_group_wait_for_bytes((select_group_t*)group, s, 4);
/* Send the initial packet to kick things off. */
send_SMB_COM_NEGOTIATE(smbclient, protocols);
break;
}
case RECV_STATE_HEADER:
{
/* Sanity check -- this should never happen with select_group. */
if(length != 4)
smbclient->protocol_error_callback(smbclient, -1, "Received incorrect number of bytes for NetBIOS header.");
/* The header is 24-bytes in network byte order. */
buffer = buffer_create_with_data(BO_NETWORK, data, 4);
smbclient->current_length = (buffer_read_next_int32(buffer) & 0x00FFFFFF);
buffer_destroy(buffer);
/* Enter body-receiving mode. */
select_group_wait_for_bytes((select_group_t*)group, s, smbclient->current_length);
smbclient->state = RECV_STATE_BODY;
break;
}
case RECV_STATE_BODY:
{
SMB_t *smb;
/* Sanity check -- this should never happen with select_group. */
if(length != smbclient->current_length)
smbclient->protocol_error_callback(smbclient, -1, "Received incorrect number of bytes for NetBIOS body.");
smb = smb_create_from_data(data, length, &smbclient->sequence, smbclient->mac_key, smbclient->authenticated ? smbclient->check_signature : FALSE);
/* Parse out some flags. */
smbclient->error_nt = (smb->header.flags2 & SMB_FLAGS2_32BIT_STATUS) ? TRUE : FALSE;
smbclient->unicode = (smb->header.flags2 & SMB_FLAGS2_UNICODE_STRINGS) ? TRUE : FALSE;
/* Display them, if desired. */
if(smbclient->verbose > 1)
{
printf("Flags: ");
smb_print_flags(smb->header.flags);
printf("\n");
printf("Flags2: ");
smb_print_flags2(smb->header.flags2);
printf("\n");
}
switch(smb->header.command)
{
case SMB_COM_NEGOTIATE:
parse_SMB_COM_NEGOTIATE(smbclient, smb);
break;
case SMB_COM_SESSION_SETUP_ANDX:
parse_SMB_COM_SESSION_SETUP_ANDX(smbclient, smb, 0);
break;
case SMB_COM_ECHO:
parse_SMB_COM_ECHO(smbclient, smb);
break;
case SMB_COM_TREE_CONNECT_ANDX:
parse_SMB_COM_TREE_CONNECT_ANDX(smbclient, smb);
break;
case SMB_COM_TREE_DISCONNECT:
parse_SMB_COM_TREE_DISCONNECT(smbclient, smb);
break;
case SMB_COM_LOGOFF_ANDX:
parse_SMB_COM_LOGOFF_ANDX(smbclient, smb);
break;
case SMB_COM_NT_CREATE_ANDX:
parse_SMB_COM_NT_CREATE_ANDX(smbclient, smb, 0);
break;
case SMB_COM_TRANSACTION:
parse_SMB_COM_TRANSACTION(smbclient, smb);
break;
default:
smbclient->event_callback(smbclient, SMBCLIENT_UNKNOWN_COMMAND, (void*)smb->header.command);
}
smb_destroy(smb);
/* Return to header state. */
select_group_wait_for_bytes((select_group_t*)group, s, 4);
smbclient->state = RECV_STATE_HEADER;
break;
}
default:
DIE("Entered an unknown state.");
}
return SELECT_OK;
}
SELECT_RESPONSE_t timeout_callback(void *group, int s, void *param)
{
return SELECT_OK;
}
SELECT_RESPONSE_t error_callback(void *group, int s, int err, void *param)
{
((SMBCLIENT_t*)param)->connection_error_callback(param, err, strerror(err));
return SELECT_OK;
}
SELECT_RESPONSE_t closed_callback(void *group, int s, void *param)
{
((SMBCLIENT_t*)param)->connection_error_callback(param, 0, "Connection closed.");
return SELECT_OK;
}
SMBCLIENT_t *smbclient_create()
{
SMBCLIENT_t *new_smbclient = safe_malloc(sizeof(SMBCLIENT_t));
memset(new_smbclient, 0, sizeof(SMBCLIENT_t));
return new_smbclient;
}
void smbclient_set_event_callback(SMBCLIENT_t *smbclient, smbclient_event *event_callback)
{
smbclient->event_callback = event_callback;
}
void smbclient_set_protocol_error_callback(SMBCLIENT_t *smbclient, smbclient_protocol_error *protocol_error_callback)
{
smbclient->protocol_error_callback = protocol_error_callback;
}
void smbclient_set_connection_error_callback(SMBCLIENT_t *smbclient, smbclient_connection_error *connection_error_callback)
{
smbclient->connection_error_callback = connection_error_callback;
}
void smbclient_connect(SMBCLIENT_t *smbclient, char *host, uint16_t port, NBBOOL try_other_ports, select_group_t *sg)
{
/* Connect; if it fails, try some other ports. */
int s = tcp_connect(host, port);
if(s == -1 && try_other_ports)
{
if(port != 445)
{
fprintf(stderr, "Connection on port %d failed, trying port 445\n", port);
port = 445;
s = tcp_connect(host, port);
}
if(s == -1)
{
fprintf(stderr, "Connection on port %d failed, trying port 139\n", port);
port = 139;
s = tcp_connect(host, port);
}
}
if(s == -1)
{
fprintf(stderr, "Couldn't connect, sorry!\n\n");
exit(1);