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
|
/*
Unix SMB/CIFS implementation.
DsGetNCChanges replication test
Copyright (C) Stefan (metze) Metzmacher 2005
Copyright (C) Brad Henry 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 "lib/cmdline/popt_common.h"
#include "librpc/gen_ndr/ndr_drsuapi_c.h"
#include "libcli/cldap/cldap.h"
#include "libcli/ldap/ldap_client.h"
#include "torture/torture.h"
#include "torture/ldap/proto.h"
#include "libcli/auth/libcli_auth.h"
#include "lib/crypto/crypto.h"
#include "auth/credentials/credentials.h"
#include "libcli/auth/libcli_auth.h"
#include "auth/gensec/gensec.h"
struct DsSyncBindInfo {
struct dcerpc_pipe *pipe;
struct drsuapi_DsBind req;
struct GUID bind_guid;
struct drsuapi_DsBindInfoCtr our_bind_info_ctr;
struct drsuapi_DsBindInfo28 our_bind_info28;
struct drsuapi_DsBindInfo28 peer_bind_info28;
struct policy_handle bind_handle;
DATA_BLOB dce_key;
DATA_BLOB gen_key;
struct samr_Password nthash;
};
struct DsSyncLDAPInfo {
struct ldap_connection *conn;
};
struct DsSyncTest {
struct dcerpc_binding *drsuapi_binding;
const char *ldap_url;
const char *site_name;
const char *domain_dn;
/* what we need to do as 'Administrator' */
struct {
struct cli_credentials *credentials;
struct DsSyncBindInfo drsuapi;
struct DsSyncLDAPInfo ldap;
} admin;
/* what we need to do as the new dc machine account */
struct {
struct cli_credentials *credentials;
struct DsSyncBindInfo drsuapi;
struct drsuapi_DsGetDCInfo2 dc_info2;
struct GUID invocation_id;
struct GUID object_guid;
} new_dc;
/* info about the old dc */
struct {
struct drsuapi_DsGetDomainControllerInfo dc_info;
} old_dc;
};
static struct DsSyncTest *test_create_context(TALLOC_CTX *mem_ctx)
{
NTSTATUS status;
struct DsSyncTest *ctx;
struct drsuapi_DsBindInfo28 *our_bind_info28;
struct drsuapi_DsBindInfoCtr *our_bind_info_ctr;
const char *binding = lp_parm_string(-1, "torture", "binding");
ctx = talloc_zero(mem_ctx, struct DsSyncTest);
if (!ctx) return NULL;
status = dcerpc_parse_binding(ctx, binding, &ctx->drsuapi_binding);
if (!NT_STATUS_IS_OK(status)) {
printf("Bad binding string %s\n", binding);
return NULL;
}
ctx->drsuapi_binding->flags |= DCERPC_SIGN | DCERPC_SEAL;
ctx->ldap_url = talloc_asprintf(ctx, "ldap://%s/", ctx->drsuapi_binding->host);
/* ctx->admin ...*/
ctx->admin.credentials = cmdline_credentials;
our_bind_info28 = &ctx->admin.drsuapi.our_bind_info28;
our_bind_info28->supported_extensions = 0xFFFFFFFF;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
our_bind_info28->site_guid = GUID_zero();
our_bind_info28->u1 = 0;
our_bind_info28->repl_epoch = 1;
our_bind_info_ctr = &ctx->admin.drsuapi.our_bind_info_ctr;
our_bind_info_ctr->length = 28;
our_bind_info_ctr->info.info28 = *our_bind_info28;
GUID_from_string(DRSUAPI_DS_BIND_GUID, &ctx->admin.drsuapi.bind_guid);
ctx->admin.drsuapi.req.in.bind_guid = &ctx->admin.drsuapi.bind_guid;
ctx->admin.drsuapi.req.in.bind_info = our_bind_info_ctr;
ctx->admin.drsuapi.req.out.bind_handle = &ctx->admin.drsuapi.bind_handle;
/* ctx->new_dc ...*/
ctx->new_dc.credentials = cmdline_credentials;
our_bind_info28 = &ctx->new_dc.drsuapi.our_bind_info28;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
if (lp_parm_bool(-1,"dssync","xpress",False)) {
our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
}
our_bind_info28->site_guid = GUID_zero();
our_bind_info28->u1 = 508;
our_bind_info28->repl_epoch = 0;
our_bind_info_ctr = &ctx->new_dc.drsuapi.our_bind_info_ctr;
our_bind_info_ctr->length = 28;
our_bind_info_ctr->info.info28 = *our_bind_info28;
GUID_from_string(DRSUAPI_DS_BIND_GUID_W2K3, &ctx->new_dc.drsuapi.bind_guid);
ctx->new_dc.drsuapi.req.in.bind_guid = &ctx->new_dc.drsuapi.bind_guid;
ctx->new_dc.drsuapi.req.in.bind_info = our_bind_info_ctr;
ctx->new_dc.drsuapi.req.out.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
ctx->new_dc.invocation_id = ctx->new_dc.drsuapi.bind_guid;
/* ctx->old_dc ...*/
return ctx;
}
static BOOL _test_DsBind(struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncBindInfo *b)
{
NTSTATUS status;
BOOL ret = True;
struct event_context *event = NULL;
const struct samr_Password *nthash;
status = dcerpc_pipe_connect_b(ctx,
&b->pipe, ctx->drsuapi_binding,
&dcerpc_table_drsuapi,
credentials, event);
if (!NT_STATUS_IS_OK(status)) {
printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
return False;
}
status = dcerpc_drsuapi_DsBind(b->pipe, ctx, &b->req);
if (!NT_STATUS_IS_OK(status)) {
const char *errstr = nt_errstr(status);
if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
errstr = dcerpc_errstr(ctx, b->pipe->last_fault_code);
}
printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
ret = False;
} else if (!W_ERROR_IS_OK(b->req.out.result)) {
printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
ret = False;
}
ZERO_STRUCT(b->peer_bind_info28);
if (b->req.out.bind_info) {
switch (b->req.out.bind_info->length) {
case 24: {
struct drsuapi_DsBindInfo24 *info24;
info24 = &b->req.out.bind_info->info.info24;
b->peer_bind_info28.supported_extensions= info24->supported_extensions;
b->peer_bind_info28.site_guid = info24->site_guid;
b->peer_bind_info28.u1 = info24->u1;
b->peer_bind_info28.repl_epoch = 0;
break;
}
case 28:
b->peer_bind_info28 = b->req.out.bind_info->info.info28;
break;
}
}
dcerpc_fetch_session_key(b->pipe, &b->dce_key);
gensec_session_key(b->pipe->conn->security_state.generic_state, &b->gen_key);
nthash = cli_credentials_get_nt_hash(credentials, NULL);
if (nthash) b->nthash = *nthash;
if (lp_parm_bool(-1,"dssync","print_pwd_blobs",False)) {
DEBUG(0,("DCERPC session key:\n"));
dump_data(0, b->dce_key.data, b->dce_key.length);
DEBUG(0,("GENSEC session key:\n"));
dump_data(0, b->gen_key.data, b->gen_key.length);
DEBUG(0,("CREDENTIALS nthash:\n"));
dump_data(0, b->nthash.hash, sizeof(b->nthash.hash));
}
return ret;
}
static BOOL test_LDAPBind(struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncLDAPInfo *l)
{
NTSTATUS status;
BOOL ret = True;
status = torture_ldap_connection(ctx, &l->conn, ctx->ldap_url);
if (!NT_STATUS_IS_OK(status)) {
printf("failed to connect to LDAP: %s\n", ctx->ldap_url);
return False;
}
printf("connected to LDAP: %s\n", ctx->ldap_url);
status = torture_ldap_bind_sasl(l->conn, credentials);
if (!NT_STATUS_IS_OK(status)) {
printf("failed to bind to LDAP:\n");
return False;
}
printf("bound to LDAP.\n");
return ret;
}
static BOOL test_GetInfo(struct DsSyncTest *ctx)
{
NTSTATUS status;
struct drsuapi_DsCrackNames r;
struct drsuapi_DsNameString names[1];
BOOL ret = True;
struct cldap_socket *cldap = cldap_socket_init(ctx, NULL);
struct cldap_netlogon search;
r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
r.in.level = 1;
r.in.req.req1.codepage = 1252; /* western european */
r.in.req.req1.language = 0x00000407; /* german */
r.in.req.req1.count = 1;
r.in.req.req1.names = names;
r.in.req.req1.format_flags = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
r.in.req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
r.in.req.req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
names[0].str = talloc_asprintf(ctx, "%s\\", lp_workgroup());
status = dcerpc_drsuapi_DsCrackNames(ctx->admin.drsuapi.pipe, ctx, &r);
if (!NT_STATUS_IS_OK(status)) {
const char *errstr = nt_errstr(status);
if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
errstr = dcerpc_errstr(ctx, ctx->admin.drsuapi.pipe->last_fault_code);
}
printf("dcerpc_drsuapi_DsCrackNames failed - %s\n", errstr);
return False;
} else if (!W_ERROR_IS_OK(r.out.result)) {
printf("DsCrackNames failed - %s\n", win_errstr(r.out.result));
return False;
}
ctx->domain_dn = r.out.ctr.ctr1->array[0].result_name;
ZERO_STRUCT(search);
search.in.dest_address = ctx->drsuapi_binding->host;
search.in.acct_control = -1;
search.in.version = 6;
status = cldap_netlogon(cldap, ctx, &search);
if (!NT_STATUS_IS_OK(status)) {
const char *errstr = nt_errstr(status);
ctx->site_name = talloc_asprintf(ctx, "%s", "Default-First-Site-Name");
printf("cldap_netlogon() returned %s. Defaulting to Site-Name: %s\n", errstr, ctx->site_name);
} else {
ctx->site_name = talloc_steal(ctx, search.out.netlogon.logon5.client_site);
printf("cldap_netlogon() returned Client Site-Name: %s.\n",ctx->site_name);
printf("cldap_netlogon() returned Server Site-Name: %s.\n",search.out.netlogon.logon5.server_site);
}
return ret;
}
static void choose_confounder_v01(TALLOC_CTX *mem_ctx,
struct DsSyncBindInfo *b,
struct drsuapi_DsReplicaObjectIdentifier *id,
uint32_t rid,
const DATA_BLOB *buffer,
uint32_t confounder_len,
DATA_BLOB *confounder,
DATA_BLOB *enc_buffer)
{
*confounder = data_blob_talloc(mem_ctx, buffer->data, confounder_len);
*enc_buffer = data_blob_talloc(mem_ctx, buffer->data+confounder_len, buffer->length - confounder_len);
}
static void choose_confounder_v02(TALLOC_CTX *mem_ctx,
struct DsSyncBindInfo *b,
struct drsuapi_DsReplicaObjectIdentifier *id,
uint32_t rid,
const DATA_BLOB *buffer,
uint32_t confounder_len,
DATA_BLOB *confounder,
DATA_BLOB *enc_buffer)
{
*confounder = data_blob_talloc(mem_ctx, buffer->data + buffer->length - confounder_len, confounder_len);
*enc_buffer = data_blob_talloc(mem_ctx, buffer->data, buffer->length - confounder_len);
}
static const struct {
uint32_t len;
void (*fn)(TALLOC_CTX *mem_ctx,
struct DsSyncBindInfo *b,
struct drsuapi_DsReplicaObjectIdentifier *id,
uint32_t rid,
const DATA_BLOB *buffer,
uint32_t confounder_len,
DATA_BLOB *confounder,
DATA_BLOB *enc_buffer);
} choose_confounder_fns[] = {
{
.len = 4,
.fn = choose_confounder_v01,
},
{
.len = 8,
.fn = choose_confounder_v01,
},
{
.len = 12,
.fn = choose_confounder_v01,
},
{
.len = 16,
.fn = choose_confounder_v01,
},
{
.len = 4,
.fn = choose_confounder_v02,
},
{
.len = 8,
.fn = choose_confounder_v02,
},
{
.len = 12,
.fn = choose_confounder_v02,
},
{
.len = 16,
.fn = choose_confounder_v02,
},
};
static void choose_session_key_v01(TALLOC_CTX *mem_ctx,
struct DsSyncBindInfo *b,
struct drsuapi_DsReplicaObjectIdentifier *id,
uint32_t rid,
const DATA_BLOB *buffer,
DATA_BLOB *session_key)
{
*session_key = data_blob_talloc(mem_ctx, b->dce_key.data, b->dce_key.length);
}
static void choose_session_key_v02(TALLOC_CTX *mem_ctx,
struct DsSyncBindInfo *b,
struct drsuapi_DsReplicaObjectIdentifier *id,
uint32_t rid,
const DATA_BLOB *buffer,
DATA_BLOB *session_key)
{
*session_key = data_blob_talloc(mem_ctx, b->gen_key.data, b->gen_key.length);
}
static const struct {
void (*fn)(TALLOC_CTX *mem_ctx,
struct DsSyncBindInfo *b,
struct drsuapi_DsReplicaObjectIdentifier *id,
uint32_t rid,
const DATA_BLOB *buffer,
DATA_BLOB *session_key);
} choose_session_key_fns[] = {
{
.fn = choose_session_key_v01,
},
{
.fn = choose_session_key_v02,
},
};
static void create_enc_key_v01(TALLOC_CTX *mem_ctx,
struct DsSyncBindInfo *b,
struct drsuapi_DsReplicaObjectIdentifier *id,
uint32_t rid,
const DATA_BLOB *buffer,
const DATA_BLOB *confounder,
const DATA_BLOB *session_key,
DATA_BLOB *_enc_key)
{
struct MD5Context md5;
DATA_BLOB enc_key;
enc_key = data_blob_talloc(mem_ctx, NULL, 16);
MD5Init(&md5);
MD5Update(&md5, confounder->data, confounder->length);
MD5Update(&md5, session_key->data, session_key->length);
MD5Final(enc_key.data, &md5);
*_enc_key = enc_key;
}
static void create_enc_key_v02(TALLOC_CTX *mem_ctx,
struct DsSyncBindInfo *b,
struct drsuapi_DsReplicaObjectIdentifier *id,
uint32_t rid,
const DATA_BLOB *buffer,
const DATA_BLOB *confounder,
const DATA_BLOB *session_key,
DATA_BLOB *_enc_key)
{
struct MD5Context md5;
DATA_BLOB enc_key;
enc_key = data_blob_talloc(mem_ctx, NULL, 16);
MD5Init(&md5);
MD5Update(&md5, session_key->data, session_key->length);
MD5Update(&md5, confounder->data, confounder->length);
MD5Final(enc_key.data, &md5);
*_enc_key = enc_key;
}
static const struct {
void (*fn)(TALLOC_CTX *mem_ctx,
struct DsSyncBindInfo *b,
struct drsuapi_DsReplicaObjectIdentifier *id,
uint32_t rid,
const DATA_BLOB *buffer,
const DATA_BLOB *confounder,
const DATA_BLOB *session_key,
DATA_BLOB *_enc_key);
} create_enc_key_fns[] = {
{
.fn = create_enc_key_v01,
},
{
.fn = create_enc_key_v02,
},
};
static void do_decryption_v01(TALLOC_CTX *mem_ctx,
struct DsSyncBindInfo *b,
struct drsuapi_DsReplicaObjectIdentifier *id,
uint32_t rid,
const DATA_BLOB *buffer,
const DATA_BLOB *enc_key,
const DATA_BLOB *enc_buffer,
DATA_BLOB *_plain_buffer)
{
DATA_BLOB plain_buffer;
plain_buffer = data_blob_talloc(mem_ctx, enc_buffer->data, enc_buffer->length);
arcfour_crypt_blob(plain_buffer.data, plain_buffer.length, enc_buffer);
*_plain_buffer = plain_buffer;
}
static const struct {
void (*fn)(TALLOC_CTX *mem_ctx,
struct DsSyncBindInfo *b,
struct drsuapi_DsReplicaObjectIdentifier *id,
uint32_t rid,
const DATA_BLOB *buffer,
const DATA_BLOB *enc_key,
const DATA_BLOB *enc_buffer,
DATA_BLOB *plain_buffer);
} do_decryption_fns[] = {
{
.fn = do_decryption_v01,
},
};
static DATA_BLOB decrypt_blob(TALLOC_CTX *mem_ctx,
struct DsSyncBindInfo *b,
struct drsuapi_DsReplicaObjectIdentifier *id,
uint32_t rid,
const DATA_BLOB *buffer)
{
uint32_t conf_i;
uint32_t skey_i;
uint32_t ekey_i;
uint32_t crypt_i;
for (conf_i = 0; conf_i < ARRAY_SIZE(choose_confounder_fns); conf_i++) {
DATA_BLOB confounder;
DATA_BLOB enc_buffer;
choose_confounder_fns[conf_i].fn(mem_ctx, b, id, rid, buffer,
choose_confounder_fns[conf_i].len,
&confounder, &enc_buffer);
for (skey_i = 0; skey_i < ARRAY_SIZE(choose_session_key_fns); skey_i++) {
DATA_BLOB session_key;
choose_session_key_fns[skey_i].fn(mem_ctx, b, id, rid, buffer,
&session_key);
for (ekey_i = 0; ekey_i < ARRAY_SIZE(create_enc_key_fns); ekey_i++) {
DATA_BLOB enc_key;
create_enc_key_fns[ekey_i].fn(mem_ctx, b, id, rid, buffer,
&confounder, &session_key,
&enc_key);
for (crypt_i = 0; crypt_i < ARRAY_SIZE(do_decryption_fns); crypt_i++) {
DATA_BLOB plain_buffer;
do_decryption_fns[crypt_i].fn(mem_ctx, b, id, rid, buffer,
&enc_key, &enc_buffer,
&plain_buffer);
DEBUGADD(0,("c[%u] s[%u] e[%u] d[%u] len[%u]:\n",
conf_i, skey_i, ekey_i, crypt_i,
plain_buffer.length));
dump_data(0, confounder.data, confounder.length);
dump_data(0, session_key.data, session_key.length);
dump_data(0, enc_key.data, enc_key.length);
dump_data(0, enc_buffer.data, enc_buffer.length);
dump_data(0, plain_buffer.data, plain_buffer.length);
}
}
}
}
return data_blob(NULL, 0);
}
static void test_analyse_objects(struct DsSyncTest *ctx,
struct drsuapi_DsReplicaObjectListItemEx *cur)
{
if (!lp_parm_bool(-1,"dssync","print_pwd_blobs",False)) {
return;
}
for (; cur; cur = cur->next_object) {
const char *dn;
struct dom_sid *sid = NULL;
uint32_t rid = 0;
BOOL dn_printed = False;
uint32_t i;
if (!cur->object.identifier) continue;
dn = cur->object.identifier->dn;
if (cur->object.identifier->sid.num_auths > 0) {
sid = &cur->object.identifier->sid;
rid = sid->sub_auths[sid->num_auths - 1];
}
for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
const char *name = NULL;
DATA_BLOB *enc_data = NULL;
DATA_BLOB plain_data;
struct drsuapi_DsReplicaAttribute *attr;
attr = &cur->object.attribute_ctr.attributes[i];
switch (attr->attid) {
case DRSUAPI_ATTRIBUTE_dBCSPwd:
name = "dBCSPwd";
break;
case DRSUAPI_ATTRIBUTE_unicodePwd:
name = "unicodePwd";
break;
case DRSUAPI_ATTRIBUTE_ntPwdHistory:
name = "ntPwdHistory";
break;
case DRSUAPI_ATTRIBUTE_lmPwdHistory:
name = "lmPwdHistory";
break;
case DRSUAPI_ATTRIBUTE_supplementalCredentials:
name = "supplementalCredentials";
break;
case DRSUAPI_ATTRIBUTE_priorValue:
name = "priorValue";
break;
case DRSUAPI_ATTRIBUTE_currentValue:
name = "currentValue";
break;
case DRSUAPI_ATTRIBUTE_trustAuthOutgoing:
name = "trustAuthOutgoing";
break;
case DRSUAPI_ATTRIBUTE_trustAuthIncoming:
name = "trustAuthIncoming";
break;
case DRSUAPI_ATTRIBUTE_initialAuthOutgoing:
name = "initialAuthOutgoing";
break;
case DRSUAPI_ATTRIBUTE_initialAuthIncoming:
name = "initialAuthIncoming";
break;
default:
continue;
}
if (attr->value_ctr.num_values != 1) continue;
if (!attr->value_ctr.values[0].blob) continue;
enc_data = attr->value_ctr.values[0].blob;
ZERO_STRUCT(plain_data);
plain_data = decrypt_blob(ctx, &ctx->new_dc.drsuapi,
cur->object.identifier, rid,
enc_data);
if (!dn_printed) {
DEBUG(0,("DN: %s\n", dn));
dn_printed = True;
}
DEBUGADD(0,("ATTR: %s enc.length=%lu plain.length=%lu\n",
name, (long)enc_data->length, (long)plain_data.length));
dump_data(0, enc_data->data, enc_data->length);
if (plain_data.length) {
dump_data(0, plain_data.data, plain_data.length);
}
}
}
}
static BOOL test_FetchData(struct DsSyncTest *ctx)
{
NTSTATUS status;
BOOL ret = True;
int i, y = 0;
uint64_t highest_usn = 0;
const char *partition = NULL;
struct drsuapi_DsGetNCChanges r;
struct drsuapi_DsReplicaObjectIdentifier nc;
struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
int32_t out_level = 0;
struct GUID null_guid;
struct dom_sid null_sid;
struct {
int32_t level;
} array[] = {
/* {
5
},
*/ {
8
}
};
ZERO_STRUCT(null_guid);
ZERO_STRUCT(null_sid);
partition = lp_parm_string(-1, "dssync", "partition");
if (partition == NULL) {
partition = ctx->domain_dn;
printf("dssync:partition not specified, defaulting to %s.\n", ctx->domain_dn);
}
highest_usn = lp_parm_int(-1, "dssync", "highest_usn", 0);
for (i=0; i < ARRAY_SIZE(array); i++) {
printf("testing DsGetNCChanges level %d\n",
array[i].level);
r.in.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
r.in.level = &array[i].level;
switch (*r.in.level) {
case 5:
nc.guid = null_guid;
nc.sid = null_sid;
nc.dn = partition;
r.in.req.req5.destination_dsa_guid = ctx->new_dc.invocation_id;
r.in.req.req5.source_dsa_invocation_id = null_guid;
r.in.req.req5.naming_context = &nc;
r.in.req.req5.highwatermark.tmp_highest_usn = highest_usn;
r.in.req.req5.highwatermark.reserved_usn = 0;
r.in.req.req5.highwatermark.highest_usn = highest_usn;
r.in.req.req5.uptodateness_vector = NULL;
r.in.req.req5.replica_flags = 0;
if (lp_parm_bool(-1,"dssync","compression",False)) {
r.in.req.req5.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
}
if (lp_parm_bool(-1,"dssync","neighbour_writeable",True)) {
r.in.req.req5.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
}
r.in.req.req5.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
| DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
| DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
| DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
;
r.in.req.req5.max_object_count = 133;
r.in.req.req5.max_ndr_size = 1336770;
r.in.req.req5.unknown4 = 0;
r.in.req.req5.h1 = 0;
break;
case 8:
nc.guid = null_guid;
nc.sid = null_sid;
nc.dn = partition;
/* nc.dn can be set to any other ad partition */
r.in.req.req8.destination_dsa_guid = ctx->new_dc.invocation_id;
r.in.req.req8.source_dsa_invocation_id = null_guid;
r.in.req.req8.naming_context = &nc;
r.in.req.req8.highwatermark.tmp_highest_usn = highest_usn;
r.in.req.req8.highwatermark.reserved_usn = 0;
r.in.req.req8.highwatermark.highest_usn = highest_usn;
r.in.req.req8.uptodateness_vector = NULL;
r.in.req.req8.replica_flags = 0;
if (lp_parm_bool(-1,"dssync","compression",False)) {
r.in.req.req8.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
}
if (lp_parm_bool(-1,"dssync","neighbour_writeable",True)) {
r.in.req.req8.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
}
r.in.req.req8.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
| DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
| DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
| DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
;
r.in.req.req8.max_object_count = 402;
r.in.req.req8.max_ndr_size = 402116;
r.in.req.req8.unknown4 = 0;
r.in.req.req8.h1 = 0;
r.in.req.req8.unique_ptr1 = 0;
r.in.req.req8.unique_ptr2 = 0;
r.in.req.req8.mapping_ctr.num_mappings = 0;
r.in.req.req8.mapping_ctr.mappings = NULL;
break;
}
printf("Dumping AD partition: %s\n", nc.dn);
for (y=0; ;y++) {
int32_t _level = 0;
ZERO_STRUCT(r.out);
r.out.level = &_level;
if (*r.in.level == 5) {
DEBUG(0,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
(long long)r.in.req.req5.highwatermark.tmp_highest_usn,
(long long)r.in.req.req5.highwatermark.highest_usn));
}
if (*r.in.level == 8) {
DEBUG(0,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
(long long)r.in.req.req8.highwatermark.tmp_highest_usn,
(long long)r.in.req.req8.highwatermark.highest_usn));
}
status = dcerpc_drsuapi_DsGetNCChanges(ctx->new_dc.drsuapi.pipe, ctx, &r);
if (!NT_STATUS_IS_OK(status)) {
const char *errstr = nt_errstr(status);
if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
errstr = dcerpc_errstr(ctx, ctx->new_dc.drsuapi.pipe->last_fault_code);
}
printf("dcerpc_drsuapi_DsGetNCChanges failed - %s\n", errstr);
ret = False;
} else if (!W_ERROR_IS_OK(r.out.result)) {
printf("DsGetNCChanges failed - %s\n", win_errstr(r.out.result));
ret = False;
}
if (ret == True && *r.out.level == 1) {
out_level = 1;
ctr1 = &r.out.ctr.ctr1;
} else if (ret == True && *r.out.level == 2) {
out_level = 1;
ctr1 = r.out.ctr.ctr2.ctr.mszip1.ctr1;
}
if (out_level == 1) {
DEBUG(0,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
(long long)ctr1->new_highwatermark.tmp_highest_usn,
(long long)ctr1->new_highwatermark.highest_usn));
test_analyse_objects(ctx, ctr1->first_object);
if (ctr1->new_highwatermark.tmp_highest_usn > ctr1->new_highwatermark.highest_usn) {
r.in.req.req5.highwatermark = ctr1->new_highwatermark;
continue;
}
}
if (ret == True && *r.out.level == 6) {
out_level = 6;
ctr6 = &r.out.ctr.ctr6;
} else if (ret == True && *r.out.level == 7
&& r.out.ctr.ctr7.level == 6
&& r.out.ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP) {
out_level = 6;
ctr6 = r.out.ctr.ctr7.ctr.mszip6.ctr6;
}
if (out_level == 6) {
DEBUG(0,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
(long long)ctr6->new_highwatermark.tmp_highest_usn,
(long long)ctr6->new_highwatermark.highest_usn));
test_analyse_objects(ctx, ctr6->first_object);
if (ctr6->new_highwatermark.tmp_highest_usn > ctr6->new_highwatermark.highest_usn) {
r.in.req.req8.highwatermark = ctr6->new_highwatermark;
continue;
}
}
break;
}
}
return ret;
}
static BOOL test_FetchNT4Data(struct DsSyncTest *ctx)
{
NTSTATUS status;
BOOL ret = True;
int i, y = 0;
uint64_t highest_usn = 0;
const char *partition = NULL;
struct drsuapi_DsGetNT4ChangeLog r;
int32_t out_level = 0;
struct GUID null_guid;
struct dom_sid null_sid;
DATA_BLOB cookie;
ZERO_STRUCT(null_guid);
ZERO_STRUCT(null_sid);
ZERO_STRUCT(cookie);
ZERO_STRUCT(r);
r.in.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
r.in.level = 1;
r.in.req.req1.unknown1 = lp_parm_int(-1, "dssync", "nt4-1", 3);
r.in.req.req1.unknown2 = lp_parm_int(-1, "dssync", "nt4-2", 0x00004000);
while (1) {
r.in.req.req1.length = cookie.length;
r.in.req.req1.data = cookie.data;
status = dcerpc_drsuapi_DsGetNT4ChangeLog(ctx->new_dc.drsuapi.pipe, ctx, &r);
if (!NT_STATUS_IS_OK(status)) {
const char *errstr = nt_errstr(status);
if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
errstr = dcerpc_errstr(ctx, ctx->new_dc.drsuapi.pipe->last_fault_code);
}
printf("dcerpc_drsuapi_DsGetNT4ChangeLog failed - %s\n", errstr);
ret = False;
} else if (!W_ERROR_IS_OK(r.out.result)) {
printf("DsGetNT4ChangeLog failed - %s\n", win_errstr(r.out.result));
ret = False;
} else if (r.out.level != 1) {
printf("DsGetNT4ChangeLog unknown level - %u\n", r.out.level);
ret = False;
} else if (NT_STATUS_IS_OK(r.out.info.info1.status)) {
} else if (NT_STATUS_EQUAL(r.out.info.info1.status, STATUS_MORE_ENTRIES)) {
cookie.length = r.out.info.info1.length1;
cookie.data = r.out.info.info1.data1;
continue;
} else {
printf("DsGetNT4ChangeLog failed - %s\n", nt_errstr(r.out.info.info1.status));
ret = False;
}
break;
}
return ret;
}
BOOL torture_rpc_dssync(struct torture_context *torture)
{
BOOL ret = True;
TALLOC_CTX *mem_ctx;
struct DsSyncTest *ctx;
mem_ctx = talloc_init("torture_rpc_dssync");
ctx = test_create_context(mem_ctx);
ret &= _test_DsBind(ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
ret &= test_LDAPBind(ctx, ctx->admin.credentials, &ctx->admin.ldap);
ret &= test_GetInfo(ctx);
ret &= _test_DsBind(ctx, ctx->new_dc.credentials, &ctx->new_dc.drsuapi);
ret &= test_FetchData(ctx);
ret &= test_FetchNT4Data(ctx);
return ret;
}
|