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
|
/*
Unix SMB/CIFS implementation.
SMB torture tester - scanning functions
Copyright (C) Andrew Tridgell 2001
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
#include "torture/torture.h"
#include "libcli/libcli.h"
#include "torture/util.h"
#include "libcli/raw/libcliraw.h"
#include "system/filesys.h"
#include "pstring.h"
#define VERBOSE 0
#define OP_MIN 0
#define OP_MAX 100
/****************************************************************************
look for a partial hit
****************************************************************************/
static void trans2_check_hit(const char *format, int op, int level, NTSTATUS status)
{
if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL) ||
NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED) ||
NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED) ||
NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) ||
NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
return;
}
#if VERBOSE
printf("possible %s hit op=%3d level=%5d status=%s\n",
format, op, level, nt_errstr(status));
#endif
}
/****************************************************************************
check for existance of a trans2 call
****************************************************************************/
static NTSTATUS try_trans2(struct smbcli_state *cli,
int op,
uint8_t *param, uint8_t *data,
int param_len, int data_len,
int *rparam_len, int *rdata_len)
{
NTSTATUS status;
struct smb_trans2 t2;
uint16_t setup = op;
TALLOC_CTX *mem_ctx;
mem_ctx = talloc_init("try_trans2");
t2.in.max_param = 64;
t2.in.max_data = smb_raw_max_trans_data(cli->tree, 64);
t2.in.max_setup = 10;
t2.in.flags = 0;
t2.in.timeout = 0;
t2.in.setup_count = 1;
t2.in.setup = &setup;
t2.in.params.data = param;
t2.in.params.length = param_len;
t2.in.data.data = data;
t2.in.data.length = data_len;
status = smb_raw_trans2(cli->tree, mem_ctx, &t2);
*rparam_len = t2.out.params.length;
*rdata_len = t2.out.data.length;
talloc_free(mem_ctx);
return status;
}
static NTSTATUS try_trans2_len(struct smbcli_state *cli,
const char *format,
int op, int level,
uint8_t *param, uint8_t *data,
int param_len, int *data_len,
int *rparam_len, int *rdata_len)
{
NTSTATUS ret=NT_STATUS_OK;
ret = try_trans2(cli, op, param, data, param_len,
sizeof(pstring), rparam_len, rdata_len);
#if VERBOSE
printf("op=%d level=%d ret=%s\n", op, level, nt_errstr(ret));
#endif
if (!NT_STATUS_IS_OK(ret)) return ret;
*data_len = 0;
while (*data_len < sizeof(pstring)) {
ret = try_trans2(cli, op, param, data, param_len,
*data_len, rparam_len, rdata_len);
if (NT_STATUS_IS_OK(ret)) break;
*data_len += 2;
}
if (NT_STATUS_IS_OK(ret)) {
printf("found %s level=%d data_len=%d rparam_len=%d rdata_len=%d\n",
format, level, *data_len, *rparam_len, *rdata_len);
} else {
trans2_check_hit(format, op, level, ret);
}
return ret;
}
/****************************************************************************
check whether a trans2 opnum exists at all
****************************************************************************/
static BOOL trans2_op_exists(struct smbcli_state *cli, int op)
{
int data_len = 0;
int param_len = 0;
int rparam_len, rdata_len;
uint8_t param[1024], data[1024];
NTSTATUS status1, status2;
memset(data, 0, sizeof(data));
data_len = 4;
/* try with a info level only */
param_len = sizeof(param);
data_len = sizeof(data);
memset(param, 0xFF, sizeof(param));
memset(data, 0xFF, sizeof(data));
status1 = try_trans2(cli, 0xFFFF, param, data, param_len, data_len,
&rparam_len, &rdata_len);
status2 = try_trans2(cli, op, param, data, param_len, data_len,
&rparam_len, &rdata_len);
if (NT_STATUS_EQUAL(status1, status2)) return False;
printf("Found op %d (status=%s)\n", op, nt_errstr(status2));
return True;
}
/****************************************************************************
check for existance of a trans2 call
****************************************************************************/
static BOOL scan_trans2(struct smbcli_state *cli, int op, int level,
int fnum, int dnum, int qfnum, const char *fname)
{
int data_len = 0;
int param_len = 0;
int rparam_len, rdata_len;
uint8_t param[1024], data[1024];
NTSTATUS status;
memset(data, 0, sizeof(data));
data_len = 4;
/* try with a info level only */
param_len = 2;
SSVAL(param, 0, level);
status = try_trans2_len(cli, "void", op, level, param, data, param_len, &data_len,
&rparam_len, &rdata_len);
if (NT_STATUS_IS_OK(status)) return True;
/* try with a file descriptor */
param_len = 6;
SSVAL(param, 0, fnum);
SSVAL(param, 2, level);
SSVAL(param, 4, 0);
status = try_trans2_len(cli, "fnum", op, level, param, data, param_len, &data_len,
&rparam_len, &rdata_len);
if (NT_STATUS_IS_OK(status)) return True;
/* try with a quota file descriptor */
param_len = 6;
SSVAL(param, 0, qfnum);
SSVAL(param, 2, level);
SSVAL(param, 4, 0);
status = try_trans2_len(cli, "qfnum", op, level, param, data, param_len, &data_len,
&rparam_len, &rdata_len);
if (NT_STATUS_IS_OK(status)) return True;
/* try with a notify style */
param_len = 6;
SSVAL(param, 0, dnum);
SSVAL(param, 2, dnum);
SSVAL(param, 4, level);
status = try_trans2_len(cli, "notify", op, level, param, data, param_len, &data_len,
&rparam_len, &rdata_len);
if (NT_STATUS_IS_OK(status)) return True;
/* try with a file name */
param_len = 6;
SSVAL(param, 0, level);
SSVAL(param, 2, 0);
SSVAL(param, 4, 0);
param_len += push_string(¶m[6], fname, sizeof(pstring)-7, STR_TERMINATE|STR_UNICODE);
status = try_trans2_len(cli, "fname", op, level, param, data, param_len, &data_len,
&rparam_len, &rdata_len);
if (NT_STATUS_IS_OK(status)) return True;
/* try with a new file name */
param_len = 6;
SSVAL(param, 0, level);
SSVAL(param, 2, 0);
SSVAL(param, 4, 0);
param_len += push_string(¶m[6], "\\newfile.dat", sizeof(pstring)-7, STR_TERMINATE|STR_UNICODE);
status = try_trans2_len(cli, "newfile", op, level, param, data, param_len, &data_len,
&rparam_len, &rdata_len);
smbcli_unlink(cli->tree, "\\newfile.dat");
smbcli_rmdir(cli->tree, "\\newfile.dat");
if (NT_STATUS_IS_OK(status)) return True;
/* try dfs style */
smbcli_mkdir(cli->tree, "\\testdir");
param_len = 2;
SSVAL(param, 0, level);
param_len += push_string(¶m[2], "\\testdir", sizeof(pstring)-3, STR_TERMINATE|STR_UNICODE);
status = try_trans2_len(cli, "dfs", op, level, param, data, param_len, &data_len,
&rparam_len, &rdata_len);
smbcli_rmdir(cli->tree, "\\testdir");
if (NT_STATUS_IS_OK(status)) return True;
return False;
}
BOOL torture_trans2_scan(void)
{
static struct smbcli_state *cli;
int op, level;
const char *fname = "\\scanner.dat";
int fnum, dnum, qfnum;
printf("starting trans2 scan test\n");
if (!torture_open_connection(&cli)) {
return False;
}
fnum = smbcli_open(cli->tree, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
if (fnum == -1) {
printf("file open failed - %s\n", smbcli_errstr(cli->tree));
}
dnum = smbcli_nt_create_full(cli->tree, "\\",
0,
SEC_RIGHTS_FILE_READ,
FILE_ATTRIBUTE_NORMAL,
NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE,
NTCREATEX_DISP_OPEN,
NTCREATEX_OPTIONS_DIRECTORY, 0);
if (dnum == -1) {
printf("directory open failed - %s\n", smbcli_errstr(cli->tree));
}
qfnum = smbcli_nt_create_full(cli->tree, "\\$Extend\\$Quota:$Q:$INDEX_ALLOCATION",
NTCREATEX_FLAGS_EXTENDED,
SEC_FLAG_MAXIMUM_ALLOWED,
0,
NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE,
NTCREATEX_DISP_OPEN,
0, 0);
if (qfnum == -1) {
printf("quota open failed - %s\n", smbcli_errstr(cli->tree));
}
for (op=OP_MIN; op<=OP_MAX; op++) {
if (!trans2_op_exists(cli, op)) {
continue;
}
for (level = 0; level <= 50; level++) {
scan_trans2(cli, op, level, fnum, dnum, qfnum, fname);
}
for (level = 0x100; level <= 0x130; level++) {
scan_trans2(cli, op, level, fnum, dnum, qfnum, fname);
}
for (level = 1000; level < 1050; level++) {
scan_trans2(cli, op, level, fnum, dnum, qfnum, fname);
}
}
torture_close_connection(cli);
printf("trans2 scan finished\n");
return True;
}
/****************************************************************************
look for a partial hit
****************************************************************************/
static void nttrans_check_hit(const char *format, int op, int level, NTSTATUS status)
{
if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL) ||
NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED) ||
NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED) ||
NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL) ||
NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
return;
}
#if VERBOSE
printf("possible %s hit op=%3d level=%5d status=%s\n",
format, op, level, nt_errstr(status));
#endif
}
/****************************************************************************
check for existence of a nttrans call
****************************************************************************/
static NTSTATUS try_nttrans(struct smbcli_state *cli,
int op,
uint8_t *param, uint8_t *data,
int param_len, int data_len,
int *rparam_len, int *rdata_len)
{
struct smb_nttrans parms;
DATA_BLOB ntparam_blob, ntdata_blob;
TALLOC_CTX *mem_ctx;
NTSTATUS status;
mem_ctx = talloc_init("try_nttrans");
ntparam_blob.length = param_len;
ntparam_blob.data = param;
ntdata_blob.length = data_len;
ntdata_blob.data = data;
parms.in.max_param = 64;
parms.in.max_data = smb_raw_max_trans_data(cli->tree, 64);
parms.in.max_setup = 0;
parms.in.setup_count = 0;
parms.in.function = op;
parms.in.params = ntparam_blob;
parms.in.data = ntdata_blob;
status = smb_raw_nttrans(cli->tree, mem_ctx, &parms);
if (NT_STATUS_IS_ERR(status)) {
DEBUG(1,("Failed to send NT_TRANS\n"));
talloc_free(mem_ctx);
return status;
}
*rparam_len = parms.out.params.length;
*rdata_len = parms.out.data.length;
talloc_free(mem_ctx);
return status;
}
static NTSTATUS try_nttrans_len(struct smbcli_state *cli,
const char *format,
int op, int level,
uint8_t *param, uint8_t *data,
int param_len, int *data_len,
int *rparam_len, int *rdata_len)
{
NTSTATUS ret=NT_STATUS_OK;
ret = try_nttrans(cli, op, param, data, param_len,
sizeof(pstring), rparam_len, rdata_len);
#if VERBOSE
printf("op=%d level=%d ret=%s\n", op, level, nt_errstr(ret));
#endif
if (!NT_STATUS_IS_OK(ret)) return ret;
*data_len = 0;
while (*data_len < sizeof(pstring)) {
ret = try_nttrans(cli, op, param, data, param_len,
*data_len, rparam_len, rdata_len);
if (NT_STATUS_IS_OK(ret)) break;
*data_len += 2;
}
if (NT_STATUS_IS_OK(ret)) {
printf("found %s level=%d data_len=%d rparam_len=%d rdata_len=%d\n",
format, level, *data_len, *rparam_len, *rdata_len);
} else {
nttrans_check_hit(format, op, level, ret);
}
return ret;
}
/****************************************************************************
check for existance of a nttrans call
****************************************************************************/
static BOOL scan_nttrans(struct smbcli_state *cli, int op, int level,
int fnum, int dnum, const char *fname)
{
int data_len = 0;
int param_len = 0;
int rparam_len, rdata_len;
uint8_t param[1024], data[1024];
NTSTATUS status;
memset(data, 0, sizeof(data));
data_len = 4;
/* try with a info level only */
param_len = 2;
SSVAL(param, 0, level);
status = try_nttrans_len(cli, "void", op, level, param, data, param_len, &data_len,
&rparam_len, &rdata_len);
if (NT_STATUS_IS_OK(status)) return True;
/* try with a file descriptor */
param_len = 6;
SSVAL(param, 0, fnum);
SSVAL(param, 2, level);
SSVAL(param, 4, 0);
status = try_nttrans_len(cli, "fnum", op, level, param, data, param_len, &data_len,
&rparam_len, &rdata_len);
if (NT_STATUS_IS_OK(status)) return True;
/* try with a notify style */
param_len = 6;
SSVAL(param, 0, dnum);
SSVAL(param, 2, dnum);
SSVAL(param, 4, level);
status = try_nttrans_len(cli, "notify", op, level, param, data, param_len, &data_len,
&rparam_len, &rdata_len);
if (NT_STATUS_IS_OK(status)) return True;
/* try with a file name */
param_len = 6;
SSVAL(param, 0, level);
SSVAL(param, 2, 0);
SSVAL(param, 4, 0);
param_len += push_string(¶m[6], fname, sizeof(pstring), STR_TERMINATE | STR_UNICODE);
status = try_nttrans_len(cli, "fname", op, level, param, data, param_len, &data_len,
&rparam_len, &rdata_len);
if (NT_STATUS_IS_OK(status)) return True;
/* try with a new file name */
param_len = 6;
SSVAL(param, 0, level);
SSVAL(param, 2, 0);
SSVAL(param, 4, 0);
param_len += push_string(¶m[6], "\\newfile.dat", sizeof(pstring), STR_TERMINATE | STR_UNICODE);
status = try_nttrans_len(cli, "newfile", op, level, param, data, param_len, &data_len,
&rparam_len, &rdata_len);
smbcli_unlink(cli->tree, "\\newfile.dat");
smbcli_rmdir(cli->tree, "\\newfile.dat");
if (NT_STATUS_IS_OK(status)) return True;
/* try dfs style */
smbcli_mkdir(cli->tree, "\\testdir");
param_len = 2;
SSVAL(param, 0, level);
param_len += push_string(¶m[2], "\\testdir", sizeof(pstring), STR_TERMINATE | STR_UNICODE);
status = try_nttrans_len(cli, "dfs", op, level, param, data, param_len, &data_len,
&rparam_len, &rdata_len);
smbcli_rmdir(cli->tree, "\\testdir");
if (NT_STATUS_IS_OK(status)) return True;
return False;
}
BOOL torture_nttrans_scan(void)
{
static struct smbcli_state *cli;
int op, level;
const char *fname = "\\scanner.dat";
int fnum, dnum;
printf("starting nttrans scan test\n");
if (!torture_open_connection(&cli)) {
return False;
}
fnum = smbcli_open(cli->tree, fname, O_RDWR | O_CREAT | O_TRUNC,
DENY_NONE);
dnum = smbcli_open(cli->tree, "\\", O_RDONLY, DENY_NONE);
for (op=OP_MIN; op<=OP_MAX; op++) {
printf("Scanning op=%d\n", op);
for (level = 0; level <= 50; level++) {
scan_nttrans(cli, op, level, fnum, dnum, fname);
}
for (level = 0x100; level <= 0x130; level++) {
scan_nttrans(cli, op, level, fnum, dnum, fname);
}
for (level = 1000; level < 1050; level++) {
scan_nttrans(cli, op, level, fnum, dnum, fname);
}
}
torture_close_connection(cli);
printf("nttrans scan finished\n");
return True;
}
/* scan for valid base SMB requests */
BOOL torture_smb_scan(void)
{
static struct smbcli_state *cli;
int op;
struct smbcli_request *req;
NTSTATUS status;
for (op=0x0;op<=0xFF;op++) {
if (op == SMBreadbraw) continue;
if (!torture_open_connection(&cli)) {
return False;
}
req = smbcli_request_setup(cli->tree, op, 0, 0);
if (!smbcli_request_send(req)) {
smbcli_request_destroy(req);
break;
}
usleep(10000);
smbcli_transport_process(cli->transport);
if (req->state > SMBCLI_REQUEST_RECV) {
status = smbcli_request_simple_recv(req);
printf("op=0x%x status=%s\n", op, nt_errstr(status));
torture_close_connection(cli);
continue;
}
sleep(1);
smbcli_transport_process(cli->transport);
if (req->state > SMBCLI_REQUEST_RECV) {
status = smbcli_request_simple_recv(req);
printf("op=0x%x status=%s\n", op, nt_errstr(status));
} else {
printf("op=0x%x no reply\n", op);
smbcli_request_destroy(req);
continue; /* don't attempt close! */
}
torture_close_connection(cli);
}
printf("smb scan finished\n");
return True;
}
|