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
|
#include <nt.h>
#include <ntrtl.h>
#include <nturtl.h>
#include <ntsam.h>
#include <ntlsa.h>
#include <windows.h>
#include <lmcons.h>
#include <stdlib.h>
#include <stdio.h>
#include <crypt.h> // logonmsv.h needs this
#include <logonmsv.h> // SSI_SECRET_NAME defined here.
#define TRUST_ENUM_PERF_BUF_SIZE sizeof(LSA_TRUST_INFORMATION) * 1000
// process max. 1000 trusted account records at atime !!
#define NETLOGON_SECRET_NAME L"NETLOGON$"
NTSTATUS
OpenAndVerifyLSA(
IN OUT PLSA_HANDLE LsaHandle,
IN ACCESS_MASK DesiredMask,
IN LPWSTR DomainName,
OUT PPOLICY_PRIMARY_DOMAIN_INFO * ReturnPrimaryDomainInfo OPTIONAL
);
NTSTATUS
AddATrustedDomain(
IN LSA_HANDLE PolicyHandle,
IN PLSA_TRUST_INFORMATION TrustedDomainAccountInfo,
IN LPWSTR TrustedAccountSecret
);
NTSTATUS
DeleteATrustedDomain(
IN LSA_HANDLE PolicyHandle,
IN PLSA_TRUST_INFORMATION TrustedDomainAccountInfo
);
NTSTATUS
MakeNetlogonSecretName(
IN OUT PUNICODE_STRING SecretName
);
VOID
FailureMessage(
IN char *ProcName,
IN NTSTATUS NtStatus
);
VOID
FailureMessage(
IN char *ProcName,
IN NTSTATUS NtStatus
)
{
fprintf( stderr, "NETJOIN: %s failed - Status == %x\n", ProcName, NtStatus );
}
int
_cdecl
main(
int argc,
char *argv[]
)
{
NTSTATUS NtStatus;
HKEY hKey;
WCHAR UnicodeDomainName[ 32 ];
WCHAR UnicodePassword[ 32 ];
DWORD cbUnicodePassword = sizeof( UnicodePassword );
DWORD cbUnicodeDomainName = sizeof( UnicodeDomainName );
DWORD dwType;
DWORD rc;
ACCESS_MASK DesiredAccess;
LSA_HANDLE PolicyHandle = NULL;
PPOLICY_PRIMARY_DOMAIN_INFO PrimaryDomainInfo = NULL;
LSA_ENUMERATION_HANDLE TrustEnumContext = 0;
PLSA_TRUST_INFORMATION TrustEnumBuffer = NULL;
DWORD TrustEnumCount = 0;
//
// Get computer name as the password to use.
//
if (!GetComputerNameW( UnicodePassword, &cbUnicodePassword )) {
fprintf( stderr, "NETJOIN: Unable to read computer name from registry - %u\n", GetLastError() );
exit( 1 );
}
if ((rc = RegOpenKeyW( HKEY_LOCAL_MACHINE,
L"System\\CurrentControlSet\\Services\\LanmanWorkstation\\Parameters",
&hKey
)
) ||
(rc = RegQueryValueExW( hKey,
L"Domain",
NULL,
&dwType,
(LPBYTE)UnicodeDomainName,
&cbUnicodeDomainName
)
)
) {
fprintf( stderr, "NETJOIN: Unable to read domain name from registry - %u\n", rc );
exit( 1 );
}
DesiredAccess = POLICY_VIEW_LOCAL_INFORMATION |
// needed to read domain info and trusted account info
POLICY_TRUST_ADMIN |
// needed to add and delete trust accounts
POLICY_CREATE_SECRET ;
// needed to add and delete secret
NtStatus = OpenAndVerifyLSA( &PolicyHandle,
DesiredAccess,
UnicodeDomainName,
&PrimaryDomainInfo
);
if (!NT_SUCCESS( NtStatus )) {
fprintf( stderr, "NETJOIN: Unable to read domain name from registry - %u\n", GetLastError() );
exit( 1 );
}
//
// now the domain names match and the PrimaryDomainInfo has the SID of the
// domain, we can add trust entry and secret in LSA for this domain.
// Before adding this, clean up old entries.
//
for(;;) {
DWORD i;
PLSA_TRUST_INFORMATION TrustedDomainAccount;
NtStatus = LsaEnumerateTrustedDomains( PolicyHandle,
&TrustEnumContext,
(PVOID *)&TrustEnumBuffer,
TRUST_ENUM_PERF_BUF_SIZE,
&TrustEnumCount
);
if (NtStatus == STATUS_NO_MORE_ENTRIES) {
//
// we are done
//
break;
}
if (NtStatus != STATUS_MORE_ENTRIES) {
if (!NT_SUCCESS( NtStatus )) {
FailureMessage( "LsaEnumerateTrustedDomains", NtStatus );
goto Cleanup;
}
}
//
// delete trusted accounts and the corresponding secrets
//
for( i = 0, TrustedDomainAccount = TrustEnumBuffer;
i < TrustEnumCount;
TrustedDomainAccount++, i++ ) {
NtStatus = DeleteATrustedDomain( PolicyHandle,
TrustedDomainAccount
);
if (!NT_SUCCESS( NtStatus )) {
FailureMessage( "DeleteATrustedDomain", NtStatus );
goto Cleanup;
}
}
if (NtStatus != STATUS_MORE_ENTRIES) {
//
// we have cleaned up all old entries.
//
break;
}
//
// free up used enum buffer
//
if (TrustEnumBuffer != NULL) {
LsaFreeMemory( TrustEnumBuffer );
TrustEnumBuffer = NULL;
}
}
//
// add a new trust for the specified domain
//
NtStatus = AddATrustedDomain( PolicyHandle,
(PLSA_TRUST_INFORMATION) PrimaryDomainInfo,
UnicodePassword
);
if (!NT_SUCCESS( NtStatus )) {
FailureMessage( "AddATrustedDomain", NtStatus );
}
else {
//
// Give LSA a chance to do its thing.
//
Sleep( 10000 );
}
Cleanup:
if (PrimaryDomainInfo != NULL) {
LsaFreeMemory( PrimaryDomainInfo );
}
if (TrustEnumBuffer != NULL) {
LsaFreeMemory( TrustEnumBuffer );
}
if (PolicyHandle != NULL) {
LsaClose( PolicyHandle );
}
if (NT_SUCCESS( NtStatus )) {
fprintf( stderr,
"NETJOIN: Computer == '%ws' joined the '%ws' domain.\n",
UnicodePassword,
UnicodeDomainName
);
return 0;
}
else {
fprintf( stderr,
"NETJOIN: Computer == '%ws' unable to join the '%ws' domain - Status == %08x\n",
UnicodePassword,
UnicodeDomainName,
NtStatus
);
return 1;
}
}
NTSTATUS
OpenAndVerifyLSA(
IN OUT PLSA_HANDLE PolicyHandle,
IN ACCESS_MASK DesiredAccess,
IN LPWSTR DomainName,
OUT PPOLICY_PRIMARY_DOMAIN_INFO * ReturnPrimaryDomainInfo OPTIONAL
)
/*++
Routine Description:
This function opens the local LSA policy and verifies that the LSA is
configured for the workstation. Optionally it returns the primary
domain information that is read form the LSA.
Arguments:
LsaHandle - Pointer to location where the LSA handle will be retured.
DesiredMask - Access mask used to open the LSA.
DomainName - Name of the trusted domain.
ReturnPrimaryDomainInfo - Primary domain info is returned here.
Return Value:
Error code of the operation.
--*/
{
NTSTATUS NtStatus;
PPOLICY_PRIMARY_DOMAIN_INFO PrimaryDomainInfo = NULL;
OBJECT_ATTRIBUTES ObjectAttributes;
DWORD PrimaryDomainNameLength;
LPWSTR PrimaryDomainName = NULL;
//
// open LSA
*PolicyHandle = NULL;
InitializeObjectAttributes( &ObjectAttributes,
NULL,
0,
NULL,
NULL
);
NtStatus = LsaOpenPolicy( NULL,
&ObjectAttributes,
DesiredAccess,
PolicyHandle
);
if (!NT_SUCCESS( NtStatus )) {
FailureMessage( "OpenAndVerifyLSA: LsaOpenPolicy", NtStatus );
return NtStatus;
}
//
// now read primary domain info from LSA.
//
NtStatus = LsaQueryInformationPolicy( *PolicyHandle,
PolicyPrimaryDomainInformation,
(PVOID *) &PrimaryDomainInfo
);
if (!NT_SUCCESS( NtStatus )) {
FailureMessage( "OpenAndVerifyLSA: LsaQueryInformationPolicy", NtStatus );
return NtStatus;
}
//
// compare domain names
//
PrimaryDomainNameLength = PrimaryDomainInfo->Name.Length + sizeof( WCHAR );
PrimaryDomainName = malloc( PrimaryDomainNameLength );
if (PrimaryDomainName == NULL) {
NtStatus = STATUS_NO_MEMORY;
FailureMessage( "OpenAndVerifyLSA: malloc", NtStatus );
goto Cleanup;
}
RtlMoveMemory( PrimaryDomainName,
PrimaryDomainInfo->Name.Buffer,
PrimaryDomainInfo->Name.Length
);
PrimaryDomainName[ PrimaryDomainInfo->Name.Length / sizeof(WCHAR) ] = UNICODE_NULL;
if (_wcsicmp( DomainName, PrimaryDomainName )) {
//
// domain names don't match
//
NtStatus = STATUS_OBJECT_NAME_NOT_FOUND;
FailureMessage( "OpenAndVerifyLSA: wcsicmp", NtStatus );
goto Cleanup;
}
NtStatus = STATUS_SUCCESS;
Cleanup:
if (PrimaryDomainName != NULL) {
free( PrimaryDomainName );
}
if (PrimaryDomainInfo != NULL) {
if (ARGUMENT_PRESENT( ReturnPrimaryDomainInfo ) ) {
if (NT_SUCCESS( NtStatus )) {
*ReturnPrimaryDomainInfo = PrimaryDomainInfo;
}
else {
LsaFreeMemory( PrimaryDomainInfo );
*ReturnPrimaryDomainInfo = NULL;
}
}
else {
LsaFreeMemory( PrimaryDomainInfo );
}
}
if (!NT_SUCCESS( NtStatus ) && *PolicyHandle != NULL) {
//
// close LSA if an error occurred.
//
LsaClose( *PolicyHandle );
*PolicyHandle = NULL;
}
return NtStatus;
}
#if 0
NET_API_STATUS NET_API_FUNCTION
I_NetGetDCList(
IN LPWSTR ServerName OPTIONAL,
IN LPWSTR TrustedDomainName,
OUT PULONG DCCount,
OUT PUNICODE_STRING * DCNames
);
#endif
NTSTATUS
AddATrustedDomain(
IN LSA_HANDLE PolicyHandle,
IN PLSA_TRUST_INFORMATION TrustedDomainAccountInfo,
IN LPWSTR TrustedAccountSecret
)
/*++
Routine Description:
This function adds trusted domain account and a secret for the
corresponding account in LSA. This function does not do any check
before adding this account in LSA.
Arguments:
PolicyHandle - LSA policy handle
TrustedDomainAccountInfo - Pointer to the LSA_TRUST_INFORMATION structure.
TrustedAccountSecret - Pointer to the secret for the trusted domain
account.
Return Value:
Error code of the operation.
--*/
{
NTSTATUS NtStatus;
LSA_HANDLE TrustedDomainHandle = NULL;
DWORD DCCount;
PUNICODE_STRING DCNames = NULL;
TRUSTED_CONTROLLERS_INFO TrustedControllersInfo;
UNICODE_STRING SecretName = {0, 0, NULL};
LSA_HANDLE SecretHandle = NULL;
UNICODE_STRING CurrentSecretValue;
DWORD UnicodeDomainNameLength;
LPWSTR UnicodeDomainName = NULL;
NtStatus = LsaCreateTrustedDomain( PolicyHandle,
TrustedDomainAccountInfo,
TRUSTED_SET_CONTROLLERS | DELETE,
&TrustedDomainHandle
);
if (!NT_SUCCESS( NtStatus )) {
FailureMessage( "AddATrustedDomain: LsaCreateTrustedDomain", NtStatus );
return NtStatus;
}
//
// Determine the DC List. This list will be stored in trusted domain
// account.
//
// Specify the server name NULL, the domain name is the primary domain
// of this workstation and so it must be listening DC announcements.
//
UnicodeDomainNameLength = TrustedDomainAccountInfo->Name.Length +
sizeof(WCHAR);
UnicodeDomainName = malloc( UnicodeDomainNameLength );
if (UnicodeDomainName == NULL) {
NtStatus = STATUS_NO_MEMORY;
FailureMessage( "AddATrustedDomain: malloc", NtStatus );
goto Cleanup;
}
RtlMoveMemory( UnicodeDomainName,
TrustedDomainAccountInfo->Name.Buffer,
TrustedDomainAccountInfo->Name.Length
);
UnicodeDomainName[ (UnicodeDomainNameLength / sizeof(WCHAR)) - 1 ] = '\0';
#if 0
if (I_NetGetDCList( NULL,
UnicodeDomainName,
&DCCount,
&DCNames
)
) {
//
// if unable to find the DC list for the specified domain, set
// the Dc list to null and proceed.
//
DCCount = 0;
DCNames = NULL;
}
#else
DCCount = 0;
DCNames = NULL;
#endif
TrustedControllersInfo.Entries = DCCount;
TrustedControllersInfo.Names = DCNames;
//
// set controller info in trusted domain object.
//
NtStatus = LsaSetInformationTrustedDomain( TrustedDomainHandle,
TrustedControllersInformation,
&TrustedControllersInfo
);
if (!NT_SUCCESS( NtStatus )) {
FailureMessage( "AddATrustedDomain: LsaSetInformationTrustedDomain", NtStatus );
goto Cleanup;
}
//
// Add a secret for this trusted account
//
MakeNetlogonSecretName( &SecretName );
NtStatus = LsaCreateSecret( PolicyHandle,
&SecretName,
SECRET_SET_VALUE,
&SecretHandle
);
if (!NT_SUCCESS( NtStatus )) {
FailureMessage( "AddATrustedDomain: LsaCreateSecret", NtStatus );
goto Cleanup;
}
RtlInitUnicodeString( &CurrentSecretValue, TrustedAccountSecret );
NtStatus = LsaSetSecret( SecretHandle,
&CurrentSecretValue,
&CurrentSecretValue
);
Cleanup:
if (DCNames != NULL) {
free( DCNames );
}
if (UnicodeDomainName != NULL) {
free( UnicodeDomainName );
}
if (SecretHandle != NULL) {
if (!NT_SUCCESS( NtStatus)) {
//
// since we are not successful completely to create the trusted
// account, delete it.
//
LsaDelete( SecretHandle );
}
else {
LsaClose( SecretHandle );
}
}
if (TrustedDomainHandle != NULL) {
if (!NT_SUCCESS( NtStatus)) {
//
// since we are not successful completely to create the trusted
// account, delete it.
//
LsaDelete( TrustedDomainHandle );
}
else {
LsaClose( TrustedDomainHandle );
}
}
return NtStatus;
}
NTSTATUS
DeleteATrustedDomain(
IN LSA_HANDLE PolicyHandle,
IN PLSA_TRUST_INFORMATION TrustedDomainAccountInfo
)
/*++
Routine Description:
This function deletes a trusted domain account and the corresponding
secret from LSA. This function however does not check any conditions
before deleting this account.
Arguments:
PolicyHandle - LSA policy handle
TurstedDoaminAccountInfo - Pointer to the LSA_TRUST_INFORMATION structure.
Return Value:
Error code of the operation.
--*/
{
NTSTATUS NtStatus;
LSA_HANDLE TrustedDomainHandle = NULL;
LSA_HANDLE SecretHandle = NULL;
UNICODE_STRING SecretName = { 0, 0, NULL };
MakeNetlogonSecretName( &SecretName );
//
// open trusted domain account secret
//
NtStatus = LsaOpenSecret(
PolicyHandle,
&SecretName,
DELETE,
&SecretHandle );
if (NtStatus != STATUS_OBJECT_NAME_NOT_FOUND) {
if (!NT_SUCCESS( NtStatus )) {
FailureMessage( "DeleteATrustedDomain: LsaOpenSecret", NtStatus );
goto Cleanup;
}
LsaDelete( SecretHandle );
}
//
// open trusted domain account
//
NtStatus = LsaOpenTrustedDomain(
PolicyHandle,
TrustedDomainAccountInfo->Sid,
DELETE,
&TrustedDomainHandle );
if (!NT_SUCCESS( NtStatus )) {
FailureMessage( "DeleteATrustedDomain: LsaOpenTrustedDomain", NtStatus );
goto Cleanup;
}
LsaDelete( TrustedDomainHandle );
Cleanup:
return NtStatus;
}
NTSTATUS
MakeNetlogonSecretName(
IN OUT PUNICODE_STRING SecretName
)
/*++
Routine Description:
This function makes a secret name that is used for the netlogon.
Arguments:
SecretName - Pointer to a unicode structure in which the netlogon
secret name will be returned.
Return Value:
NERR_Success;
--*/
{
SecretName->Length = wcslen(SSI_SECRET_NAME) * sizeof(WCHAR);
SecretName->MaximumLength = SecretName->Length + 2;
SecretName->Buffer = SSI_SECRET_NAME;
return STATUS_SUCCESS;
}
|