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
|
/*
Unix SMB/CIFS implementation.
RPC pipe client
Copyright (C) Guenther Deschner 2009
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "rpcclient.h"
#include "../librpc/gen_ndr/ndr_winreg_c.h"
#include "../librpc/gen_ndr/ndr_misc.h"
static WERROR cmd_winreg_enumkeys(struct rpc_pipe_client *cli,
TALLOC_CTX *mem_ctx, int argc,
const char **argv)
{
NTSTATUS status;
WERROR werr;
struct policy_handle handle;
uint32_t enum_index = 0;
struct winreg_StringBuf name;
struct dcerpc_binding_handle *b = cli->binding_handle;
if (argc < 2) {
printf("usage: %s [name]\n", argv[0]);
return WERR_OK;
}
status = dcerpc_winreg_OpenHKLM(b, mem_ctx,
NULL,
SEC_FLAG_MAXIMUM_ALLOWED,
&handle,
&werr);
if (!NT_STATUS_IS_OK(status)) {
return ntstatus_to_werror(status);
}
if (!W_ERROR_IS_OK(werr)) {
return werr;
}
ZERO_STRUCT(name);
name.name = argv[1];
name.length = strlen_m_term_null(name.name)*2;
name.size = name.length;
status = dcerpc_winreg_EnumKey(b, mem_ctx,
&handle,
enum_index,
&name,
NULL,
NULL,
&werr);
if (!NT_STATUS_IS_OK(status)) {
return ntstatus_to_werror(status);
}
if (!W_ERROR_IS_OK(werr)) {
return werr;
}
return WERR_OK;
}
/****************************************************************************
****************************************************************************/
static WERROR pull_winreg_Data(TALLOC_CTX *mem_ctx,
const DATA_BLOB *blob,
union winreg_Data *data,
enum winreg_Type type)
{
enum ndr_err_code ndr_err;
ndr_err = ndr_pull_union_blob(blob, mem_ctx, data, type,
(ndr_pull_flags_fn_t)ndr_pull_winreg_Data);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
return WERR_GEN_FAILURE;
}
return WERR_OK;
}
/****************************************************************************
****************************************************************************/
static void display_winreg_data(const char *v,
enum winreg_Type type,
uint8_t *data,
uint32_t length)
{
size_t i;
union winreg_Data r;
DATA_BLOB blob = data_blob_const(data, length);
WERROR result;
result = pull_winreg_Data(talloc_tos(), &blob, &r, type);
if (!W_ERROR_IS_OK(result)) {
return;
}
switch (type) {
case REG_DWORD:
printf("%-20s: REG_DWORD: 0x%08x\n", v, r.value);
break;
case REG_SZ:
printf("%-20s: REG_SZ: %s\n", v, r.string);
break;
case REG_BINARY: {
char *hex = hex_encode_talloc(NULL,
r.binary.data, r.binary.length);
size_t len;
printf("%-20s: REG_BINARY:", v);
len = strlen(hex);
for (i=0; i<len; i++) {
if (hex[i] == '\0') {
break;
}
if (i%40 == 0) {
putchar('\n');
}
putchar(hex[i]);
}
TALLOC_FREE(hex);
putchar('\n');
break;
}
case REG_MULTI_SZ:
printf("%-20s: REG_MULTI_SZ: ", v);
for (i=0; r.string_array[i] != NULL; i++) {
printf("%s ", r.string_array[i]);
}
printf("\n");
break;
default:
printf("%-20ss: unknown type 0x%02x:\n", v, type);
break;
}
}
static WERROR cmd_winreg_querymultiplevalues_ex(struct rpc_pipe_client *cli,
TALLOC_CTX *mem_ctx, int argc,
const char **argv, bool multiplevalues2)
{
NTSTATUS status;
WERROR werr;
struct policy_handle handle, key_handle;
struct winreg_String key_name = { 0, };
struct dcerpc_binding_handle *b = cli->binding_handle;
struct QueryMultipleValue *values_in, *values_out;
uint32_t num_values;
uint8_t *buffer = NULL;
uint32_t i;
if (argc < 2) {
printf("usage: %s [key] [value1] [value2] ...\n", argv[0]);
return WERR_OK;
}
status = dcerpc_winreg_OpenHKLM(b, mem_ctx,
NULL,
SEC_FLAG_MAXIMUM_ALLOWED,
&handle,
&werr);
if (!NT_STATUS_IS_OK(status)) {
return ntstatus_to_werror(status);
}
if (!W_ERROR_IS_OK(werr)) {
return werr;
}
key_name.name = argv[1];
status = dcerpc_winreg_OpenKey(b, mem_ctx,
&handle,
key_name,
0, /* options */
SEC_FLAG_MAXIMUM_ALLOWED,
&key_handle,
&werr);
if (!NT_STATUS_IS_OK(status)) {
return ntstatus_to_werror(status);
}
if (!W_ERROR_IS_OK(werr)) {
return werr;
}
num_values = argc-2;
values_in = talloc_zero_array(mem_ctx, struct QueryMultipleValue, num_values);
if (values_in == NULL) {
return WERR_NOT_ENOUGH_MEMORY;
}
values_out = talloc_zero_array(mem_ctx, struct QueryMultipleValue, num_values);
if (values_out == NULL) {
return WERR_NOT_ENOUGH_MEMORY;
}
for (i=0; i < num_values; i++) {
values_in[i].ve_valuename = talloc_zero(values_in, struct winreg_ValNameBuf);
if (values_in[i].ve_valuename == NULL) {
return WERR_NOT_ENOUGH_MEMORY;
}
values_in[i].ve_valuename->name = talloc_strdup(values_in[i].ve_valuename, argv[i+2]);
values_in[i].ve_valuename->length = strlen_m_term_null(values_in[i].ve_valuename->name)*2;
values_in[i].ve_valuename->size = values_in[i].ve_valuename->length;
}
if (multiplevalues2) {
uint32_t offered = 0, needed = 0;
status = dcerpc_winreg_QueryMultipleValues2(b, mem_ctx,
&key_handle,
values_in,
values_out,
num_values,
buffer,
&offered,
&needed,
&werr);
if (!NT_STATUS_IS_OK(status)) {
return ntstatus_to_werror(status);
}
if (W_ERROR_EQUAL(werr, WERR_MORE_DATA)) {
offered = needed;
buffer = talloc_zero_array(mem_ctx, uint8_t, needed);
if (buffer == NULL) {
return WERR_NOT_ENOUGH_MEMORY;
}
status = dcerpc_winreg_QueryMultipleValues2(b, mem_ctx,
&key_handle,
values_in,
values_out,
num_values,
buffer,
&offered,
&needed,
&werr);
if (!NT_STATUS_IS_OK(status)) {
return ntstatus_to_werror(status);
}
if (!W_ERROR_IS_OK(werr)) {
return werr;
}
}
} else {
uint32_t buffer_size = 0xff;
buffer = talloc_zero_array(mem_ctx, uint8_t, buffer_size);
if (buffer == NULL) {
return WERR_NOT_ENOUGH_MEMORY;
}
status = dcerpc_winreg_QueryMultipleValues(b, mem_ctx,
&key_handle,
values_in,
values_out,
num_values,
buffer,
&buffer_size,
&werr);
if (!NT_STATUS_IS_OK(status)) {
return ntstatus_to_werror(status);
}
if (!W_ERROR_IS_OK(werr)) {
return werr;
}
}
for (i=0; i < num_values; i++) {
if (buffer) {
display_winreg_data(values_in[i].ve_valuename->name,
values_out[i].ve_type,
buffer + values_out[i].ve_valueptr,
values_out[i].ve_valuelen);
}
}
return WERR_OK;
}
static WERROR cmd_winreg_querymultiplevalues(struct rpc_pipe_client *cli,
TALLOC_CTX *mem_ctx, int argc,
const char **argv)
{
return cmd_winreg_querymultiplevalues_ex(cli, mem_ctx, argc, argv, false);
}
static WERROR cmd_winreg_querymultiplevalues2(struct rpc_pipe_client *cli,
TALLOC_CTX *mem_ctx, int argc,
const char **argv)
{
return cmd_winreg_querymultiplevalues_ex(cli, mem_ctx, argc, argv, true);
}
static WERROR cmd_winreg_enumval(struct rpc_pipe_client *cli,
TALLOC_CTX *mem_ctx, int argc,
const char **argv)
{
NTSTATUS status;
WERROR werr, ignore;
struct dcerpc_binding_handle *b = cli->binding_handle;
struct policy_handle parent_handle, handle;
uint32_t enum_index = 0;
if (argc < 1 || argc > 3) {
printf("usage: %s [name]\n", argv[0]);
return WERR_OK;
}
status = dcerpc_winreg_OpenHKLM(b, mem_ctx,
NULL,
SEC_FLAG_MAXIMUM_ALLOWED,
&parent_handle,
&werr);
if (!NT_STATUS_IS_OK(status)) {
return ntstatus_to_werror(status);
}
if (!W_ERROR_IS_OK(werr)) {
return werr;
}
if (argc >= 2) {
struct winreg_String keyname;
ZERO_STRUCT(keyname);
keyname.name = argv[1];
status = dcerpc_winreg_OpenKey(b, mem_ctx,
&parent_handle,
keyname,
0,
SEC_FLAG_MAXIMUM_ALLOWED,
&handle,
&werr);
if (!NT_STATUS_IS_OK(status)) {
return ntstatus_to_werror(status);
}
if (!W_ERROR_IS_OK(werr)) {
return werr;
}
} else {
handle = parent_handle;
}
do {
struct winreg_ValNameBuf name;
enum winreg_Type type = REG_NONE;
uint32_t size = 0, length = 0;
struct winreg_EnumValue r;
name.name = "";
name.size = 1024;
r.in.handle = &handle;
r.in.enum_index = enum_index;
r.in.name = &name;
r.in.type = &type;
r.in.size = &size;
r.in.length = &length;
r.in.value = talloc_array(mem_ctx, uint8_t, size);
if (r.in.value == NULL) {
werr = WERR_NOT_ENOUGH_MEMORY;
goto done;
}
r.out.name = &name;
r.out.type = &type;
r.out.size = &size;
r.out.length = &length;
r.out.value = r.in.value;
status = dcerpc_winreg_EnumValue_r(b, mem_ctx, &r);
if (!NT_STATUS_IS_OK(status)) {
werr = ntstatus_to_werror(status);
goto done;
}
werr = r.out.result;
if (W_ERROR_EQUAL(werr, WERR_MORE_DATA)) {
*r.in.size = *r.out.size;
r.in.value = talloc_zero_array(mem_ctx, uint8_t, *r.in.size);
if (r.in.value == NULL) {
werr = WERR_NOT_ENOUGH_MEMORY;
goto done;
}
status = dcerpc_winreg_EnumValue_r(b, mem_ctx, &r);
if (!NT_STATUS_IS_OK(status)) {
werr = ntstatus_to_werror(status);
goto done;
}
werr = r.out.result;
}
if (!W_ERROR_IS_OK(r.out.result)) {
goto done;
}
printf("%02d: ", enum_index++);
display_winreg_data(r.out.name->name,
*r.out.type,
r.out.value,
*r.out.size);
} while (W_ERROR_IS_OK(werr));
done:
if (argc >= 2) {
dcerpc_winreg_CloseKey(b, mem_ctx, &handle, &ignore);
}
dcerpc_winreg_CloseKey(b, mem_ctx, &parent_handle, &ignore);
return werr;
}
/* List of commands exported by this module */
struct cmd_set winreg_commands[] = {
{
.name = "WINREG",
},
{
.name = "winreg_enumkey",
.returntype = RPC_RTYPE_WERROR,
.ntfn = NULL,
.wfn = cmd_winreg_enumkeys,
.table = &ndr_table_winreg,
.rpc_pipe = NULL,
.description = "Enumerate Keys",
.usage = "",
},
{
.name = "querymultiplevalues",
.returntype = RPC_RTYPE_WERROR,
.ntfn = NULL,
.wfn = cmd_winreg_querymultiplevalues,
.table = &ndr_table_winreg,
.rpc_pipe = NULL,
.description = "Query multiple values",
.usage = "",
},
{
.name = "querymultiplevalues2",
.returntype = RPC_RTYPE_WERROR,
.ntfn = NULL,
.wfn = cmd_winreg_querymultiplevalues2,
.table = &ndr_table_winreg,
.rpc_pipe = NULL,
.description = "Query multiple values",
.usage = "",
},
{
.name = "winreg_enumval",
.returntype = RPC_RTYPE_WERROR,
.ntfn = NULL,
.wfn = cmd_winreg_enumval,
.table = &ndr_table_winreg,
.rpc_pipe = NULL,
.description = "Enumerate Values",
.usage = "",
},
{
.name = NULL,
},
};
|