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
|
/*
Unix SMB/CIFS implementation.
dcerpc over SMB2 transport
Copyright (C) Andrew Tridgell 2005
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 "libcli/raw/libcliraw.h"
#include "librpc/gen_ndr/ndr_security.h"
#include "libcli/composite/composite.h"
#include "libcli/smb2/smb2.h"
#include "libcli/smb2/smb2_calls.h"
#include "ioctl.h"
/* transport private information used by SMB2 pipe transport */
struct smb2_private {
struct smb2_handle handle;
struct smb2_tree *tree;
const char *server_name;
};
/*
tell the dcerpc layer that the transport is dead
*/
static void pipe_dead(struct dcerpc_connection *c, NTSTATUS status)
{
c->transport.recv_data(c, NULL, status);
}
/*
this holds the state of an in-flight call
*/
struct smb2_read_state {
struct dcerpc_connection *c;
DATA_BLOB data;
};
/*
called when a read request has completed
*/
static void smb2_read_callback(struct smb2_request *req)
{
struct smb2_private *smb;
struct smb2_read_state *state;
struct smb2_read io;
uint16_t frag_length;
NTSTATUS status;
state = talloc_get_type(req->async.private, struct smb2_read_state);
smb = talloc_get_type(state->c->transport.private, struct smb2_private);
status = smb2_read_recv(req, state, &io);
if (NT_STATUS_IS_ERR(status)) {
pipe_dead(state->c, status);
talloc_free(state);
return;
}
status = data_blob_append(state, &state->data,
io.out.data.data, io.out.data.length);
if (NT_STATUS_IS_ERR(status)) {
pipe_dead(state->c, status);
talloc_free(state);
return;
}
if (state->data.length < 16) {
DEBUG(0,("dcerpc_smb2: short packet (length %d) in read callback!\n",
(int)state->data.length));
pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
talloc_free(state);
return;
}
frag_length = dcerpc_get_frag_length(&state->data);
if (frag_length <= state->data.length) {
DATA_BLOB data = state->data;
struct dcerpc_connection *c = state->c;
talloc_steal(c, data.data);
talloc_free(state);
c->transport.recv_data(c, &data, NT_STATUS_OK);
return;
}
/* initiate another read request, as we only got part of a fragment */
ZERO_STRUCT(io);
io.in.length = MIN(state->c->srv_max_xmit_frag,
frag_length - state->data.length);
if (io.in.length < 16) {
io.in.length = 16;
}
io.in.handle = smb->handle;
req = smb2_read_send(smb->tree, &io);
if (req == NULL) {
pipe_dead(state->c, NT_STATUS_NO_MEMORY);
talloc_free(state);
return;
}
req->async.fn = smb2_read_callback;
req->async.private = state;
}
/*
trigger a read request from the server, possibly with some initial
data in the read buffer
*/
static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLOB *blob)
{
struct smb2_private *smb = c->transport.private;
struct smb2_read io;
struct smb2_read_state *state;
struct smb2_request *req;
state = talloc(smb, struct smb2_read_state);
if (state == NULL) {
return NT_STATUS_NO_MEMORY;
}
state->c = c;
if (blob == NULL) {
state->data = data_blob(NULL, 0);
} else {
state->data = *blob;
talloc_steal(state, state->data.data);
}
ZERO_STRUCT(io);
io.in.handle = smb->handle;
if (state->data.length >= 16) {
uint16_t frag_length = dcerpc_get_frag_length(&state->data);
io.in.length = frag_length - state->data.length;
} else {
io.in.length = 0x2000;
}
req = smb2_read_send(smb->tree, &io);
if (req == NULL) {
return NT_STATUS_NO_MEMORY;
}
req->async.fn = smb2_read_callback;
req->async.private = state;
return NT_STATUS_OK;
}
/*
trigger a read request from the server
*/
static NTSTATUS send_read_request(struct dcerpc_connection *c)
{
return send_read_request_continue(c, NULL);
}
/*
this holds the state of an in-flight trans call
*/
struct smb2_trans_state {
struct dcerpc_connection *c;
};
/*
called when a trans request has completed
*/
static void smb2_trans_callback(struct smb2_request *req)
{
struct smb2_trans_state *state = talloc_get_type(req->async.private,
struct smb2_trans_state);
struct dcerpc_connection *c = state->c;
NTSTATUS status;
struct smb2_ioctl io;
status = smb2_ioctl_recv(req, state, &io);
if (NT_STATUS_IS_ERR(status)) {
pipe_dead(c, status);
return;
}
if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
DATA_BLOB data = io.out.out;
talloc_steal(c, data.data);
talloc_free(state);
c->transport.recv_data(c, &data, NT_STATUS_OK);
return;
}
/* there is more to receive - setup a read */
send_read_request_continue(c, &io.out.out);
talloc_free(state);
}
/*
send a SMBtrans style request, using a named pipe read_write fsctl
*/
static NTSTATUS smb2_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
{
struct smb2_private *smb = talloc_get_type(c->transport.private,
struct smb2_private);
struct smb2_ioctl io;
struct smb2_trans_state *state;
struct smb2_request *req;
state = talloc(smb, struct smb2_trans_state);
if (state == NULL) {
return NT_STATUS_NO_MEMORY;
}
state->c = c;
ZERO_STRUCT(io);
io.in.function = FSCTL_NAMED_PIPE_READ_WRITE;
io.in.handle = smb->handle;
io.in.max_response_size = 0x1000;
io.in.flags = 1;
io.in.out = *blob;
req = smb2_ioctl_send(smb->tree, &io);
if (req == NULL) {
talloc_free(state);
return NT_STATUS_NO_MEMORY;
}
req->async.fn = smb2_trans_callback;
req->async.private = state;
talloc_steal(state, req);
return NT_STATUS_OK;
}
/*
called when a write request has completed
*/
static void smb2_write_callback(struct smb2_request *req)
{
struct dcerpc_connection *c = req->async.private;
if (!NT_STATUS_IS_OK(req->status)) {
DEBUG(0,("dcerpc_smb2: write callback error\n"));
pipe_dead(c, req->status);
}
smb2_request_destroy(req);
}
/*
send a packet to the server
*/
static NTSTATUS smb2_send_request(struct dcerpc_connection *c, DATA_BLOB *blob,
BOOL trigger_read)
{
struct smb2_private *smb = c->transport.private;
struct smb2_write io;
struct smb2_request *req;
if (trigger_read) {
return smb2_send_trans_request(c, blob);
}
ZERO_STRUCT(io);
io.in.handle = smb->handle;
io.in.data = *blob;
req = smb2_write_send(smb->tree, &io);
if (req == NULL) {
return NT_STATUS_NO_MEMORY;
}
req->async.fn = smb2_write_callback;
req->async.private = c;
return NT_STATUS_OK;
}
/*
shutdown SMB pipe connection
*/
static NTSTATUS smb2_shutdown_pipe(struct dcerpc_connection *c)
{
struct smb2_private *smb = c->transport.private;
struct smb2_close io;
struct smb2_request *req;
/* maybe we're still starting up */
if (!smb) return NT_STATUS_OK;
ZERO_STRUCT(io);
io.in.handle = smb->handle;
req = smb2_close_send(smb->tree, &io);
if (req != NULL) {
/* we don't care if this fails, so just free it if it succeeds */
req->async.fn = (void (*)(struct smb2_request *))talloc_free;
}
talloc_free(smb);
return NT_STATUS_OK;
}
/*
return SMB server name
*/
static const char *smb2_peer_name(struct dcerpc_connection *c)
{
struct smb2_private *smb = talloc_get_type(c->transport.private,
struct smb2_private);
return smb->server_name;
}
/*
fetch the user session key
*/
static NTSTATUS smb2_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
{
struct smb2_private *smb = talloc_get_type(c->transport.private,
struct smb2_private);
*session_key = smb->tree->session->session_key;
if (session_key->data == NULL) {
return NT_STATUS_NO_USER_SESSION_KEY;
}
return NT_STATUS_OK;
}
struct pipe_open_smb2_state {
struct dcerpc_connection *c;
struct composite_context *ctx;
};
static void pipe_open_recv(struct smb2_request *req);
struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_connection *c,
struct smb2_tree *tree,
const char *pipe_name)
{
struct composite_context *ctx;
struct pipe_open_smb2_state *state;
struct smb2_create io;
struct smb2_request *req;
ctx = talloc_zero(NULL, struct composite_context);
if (ctx == NULL) goto failed;
ctx->state = COMPOSITE_STATE_IN_PROGRESS;
ctx->event_ctx = talloc_reference(c, c->event_ctx);
state = talloc(ctx, struct pipe_open_smb2_state);
if (state == NULL) goto failed;
state->c = c;
state->ctx = ctx;
ZERO_STRUCT(io);
io.in.access_mask =
SEC_STD_READ_CONTROL |
SEC_FILE_READ_ATTRIBUTE |
SEC_FILE_WRITE_ATTRIBUTE |
SEC_STD_SYNCHRONIZE |
SEC_FILE_READ_EA |
SEC_FILE_WRITE_EA |
SEC_FILE_READ_DATA |
SEC_FILE_WRITE_DATA |
SEC_FILE_APPEND_DATA;
io.in.share_access =
NTCREATEX_SHARE_ACCESS_READ |
NTCREATEX_SHARE_ACCESS_WRITE;
io.in.open_disposition = NTCREATEX_DISP_OPEN;
io.in.create_options =
NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
NTCREATEX_OPTIONS_UNKNOWN_400000;
io.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) ||
(strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
pipe_name += 6;
}
io.in.fname = pipe_name;
req = smb2_create_send(tree, &io);
if (req == NULL) goto failed;
req->async.fn = pipe_open_recv;
req->async.private = state;
return ctx;
failed:
talloc_free(ctx);
return NULL;
}
static void pipe_open_recv(struct smb2_request *req)
{
struct pipe_open_smb2_state *state =
talloc_get_type(req->async.private,
struct pipe_open_smb2_state);
struct composite_context *ctx = state->ctx;
struct dcerpc_connection *c = state->c;
struct smb2_tree *tree = req->tree;
struct smb2_private *smb;
struct smb2_create io;
ctx->status = smb2_create_recv(req, state, &io);
if (!composite_is_ok(ctx)) return;
/*
fill in the transport methods
*/
c->transport.transport = NCACN_NP;
c->transport.private = NULL;
c->transport.shutdown_pipe = smb2_shutdown_pipe;
c->transport.peer_name = smb2_peer_name;
c->transport.send_request = smb2_send_request;
c->transport.send_read = send_read_request;
c->transport.recv_data = NULL;
/* Over-ride the default session key with the SMB session key */
c->security_state.session_key = smb2_session_key;
smb = talloc(c, struct smb2_private);
if (composite_nomem(smb, ctx)) return;
smb->handle = io.out.handle;
smb->tree = talloc_reference(smb, tree);
smb->server_name= strupper_talloc(smb,
tree->session->transport->socket->hostname);
if (composite_nomem(smb->server_name, ctx)) return;
c->transport.private = smb;
composite_done(ctx);
}
NTSTATUS dcerpc_pipe_open_smb2_recv(struct composite_context *c)
{
NTSTATUS status = composite_wait(c);
talloc_free(c);
return status;
}
NTSTATUS dcerpc_pipe_open_smb2(struct dcerpc_connection *c,
struct smb2_tree *tree,
const char *pipe_name)
{
struct composite_context *ctx = dcerpc_pipe_open_smb2_send(c, tree, pipe_name);
return dcerpc_pipe_open_smb2_recv(ctx);
}
/*
return the SMB2 tree used for a dcerpc over SMB2 pipe
*/
struct smb2_tree *dcerpc_smb2_tree(struct dcerpc_connection *c)
{
struct smb2_private *smb = talloc_get_type(c->transport.private,
struct smb2_private);
return smb->tree;
}
|