-
Notifications
You must be signed in to change notification settings - Fork 36
/
NtUtils.Lsa.pas
791 lines (631 loc) · 20.7 KB
/
NtUtils.Lsa.pas
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
unit NtUtils.Lsa;
{
This module allows interoperation with Local Security Authority for managing
privilege and logon rights assignment.
}
interface
uses
Ntapi.WinNt, Ntapi.ntlsa, Ntapi.ntseapi, NtUtils;
const
POLICY_ENUMERATE_ACCOUNTS_WITH_RIGHT = POLICY_LOOKUP_NAMES or
POLICY_VIEW_LOCAL_INFORMATION;
type
ILsaHandle = NtUtils.IHandle;
TPrivilegeDefinition = record
Name: String;
LocalValue: TLuid;
end;
TLogonRightRec = record
Value: TSystemAccess;
IsAllowedType: Boolean;
Name, Description: String;
end;
{ --------------------------------- Policy ---------------------------------- }
// Open LSA for desired access
function LsaxOpenPolicy(
out hxPolicy: ILsaHandle;
DesiredAccess: TLsaPolicyAccessMask;
[opt] const SystemName: String = ''
): TNtxStatus;
// Make sure the policy handle is provided
function LsaxpEnsureConnected(
var hxPolicy: ILsaHandle;
DesiredAccess: TLsaPolicyAccessMask
): TNtxStatus;
// Query policy information
function LsaxQueryPolicy(
[Access(POLICY_VIEW_LOCAL_INFORMATION or
POLICY_VIEW_AUDIT_INFORMATION)] const hxPolicy: ILsaHandle;
InfoClass: TPolicyInformationClass;
out xBuffer: IAutoPointer
): TNtxStatus;
// Set policy information
function LsaxSetPolicy(
[Access(POLICY_TRUST_ADMIN or POLICY_AUDIT_LOG_ADMIN or
POLICY_SET_AUDIT_REQUIREMENTS or POLICY_SERVER_ADMIN or
POLICY_SET_DEFAULT_QUOTA_LIMITS)] const hxPolicy: ILsaHandle;
InfoClass: TPolicyInformationClass;
[in] Buffer: Pointer
): TNtxStatus;
{ --------------------------------- Accounts -------------------------------- }
// Open an account from LSA database
function LsaxOpenAccount(
out hxAccount: ILsaHandle;
const AccountSid: ISid;
DesiredAccess: TLsaAccountAccessMask;
[opt, Access(POLICY_VIEW_LOCAL_INFORMATION)] hxPolicy: ILsaHandle = nil
): TNtxStatus;
// Add an account to LSA database
function LsaxCreateAccount(
out hxAccount: ILsaHandle;
const AccountSid: ISid;
[opt, Access(POLICY_CREATE_ACCOUNT)] hxPolicy: ILsaHandle = nil;
DesiredAccess: TLsaAccountAccessMask = ACCOUNT_ALL_ACCESS
): TNtxStatus;
// Delete account from LSA database
function LsaxDeleteAccount(
[Access(_DELETE or ACCOUNT_VIEW)] const hxAccount: ILsaHandle
): TNtxStatus;
// Enumerate account in the LSA database
function LsaxEnumerateAccounts(
out Accounts: TArray<ISid>;
[opt, Access(POLICY_VIEW_LOCAL_INFORMATION)] hxPolicy: ILsaHandle = nil
): TNtxStatus;
// Enumerate privileges assigned to an account
function LsaxEnumeratePrivilegesAccount(
[Access(ACCOUNT_VIEW)] const hxAccount: ILsaHandle;
out Privileges: TArray<TPrivilege>
): TNtxStatus;
// Enumerate privileges assigned to an account using its SID
function LsaxEnumeratePrivilegesAccountBySid(
const AccountSid: ISid;
out Privileges: TArray<TPrivilege>
): TNtxStatus;
// Assign privileges to an account
function LsaxAddPrivilegesAccount(
[Access(ACCOUNT_ADJUST_PRIVILEGES)] const hxAccount: ILsaHandle;
const Privileges: TArray<TPrivilege>
): TNtxStatus;
// Revoke privileges to an account
function LsaxRemovePrivilegesAccount(
[Access(ACCOUNT_ADJUST_PRIVILEGES)] const hxAccount: ILsaHandle;
RemoveAll: Boolean;
[opt] const Privileges: TArray<TPrivilege>
): TNtxStatus;
// Assign & revoke privileges from an account in a single operation
function LsaxManagePrivilegesAccount(
const AccountSid: ISid;
RemoveAll: Boolean;
[opt] const Add: TArray<TPrivilege>;
[opt] const Remove: TArray<TPrivilege>
): TNtxStatus;
// Query logon rights of an account
function LsaxQueryRightsAccount(
[Access(ACCOUNT_VIEW)] const hxAccount: ILsaHandle;
out SystemAccess: TSystemAccess
): TNtxStatus;
// Query logon rights of an account using its SID
function LsaxQueryRightsAccountBySid(
const AccountSid: ISid;
out SystemAccess: TSystemAccess
): TNtxStatus;
// Set logon rights of an account
function LsaxSetRightsAccount(
[Access(ACCOUNT_ADJUST_SYSTEM_ACCESS)] const hxAccount: ILsaHandle;
SystemAccess: TSystemAccess
): TNtxStatus;
// Set logon rights of an account using its SID
function LsaxSetRightsAccountBySid(
const AccountSid: ISid;
SystemAccess: TSystemAccess
): TNtxStatus;
// Retrieve the list accounts that have a logon right or a privileges
[RequiresAdmin]
function LsaxEnumerateAccountsWithRightOrPrivilege(
out Accounts: TArray<ISid>;
const RightOrPrivilegeName: String;
[opt, Access(POLICY_ENUMERATE_ACCOUNTS_WITH_RIGHT)] hxPolicy: ILsaHandle = nil
): TNtxStatus;
{ -------------------------------- Privileges ------------------------------- }
// Enumerate all privileges on the system
function LsaxEnumeratePrivileges(
out Privileges: TArray<TPrivilegeDefinition>;
[opt, Access(POLICY_VIEW_LOCAL_INFORMATION)] hxPolicy: ILsaHandle = nil
): TNtxStatus;
// Convert a numerical privilege value to internal name
function LsaxQueryPrivilege(
const Luid: TLuid;
out Name: String;
out DisplayName: String;
[opt, Access(POLICY_LOOKUP_NAMES)] hxPolicy: ILsaHandle = nil
): TNtxStatus;
// Get the minimal integrity level required to use a specific privilege
function LsaxQueryIntegrityPrivilege(
const Luid: TLuid
): TIntegrityRid;
{ ------------------------------- Logon Process ----------------------------- }
// Establish a connection to LSA without verification
function LsaxConnectUntrusted(
out hxLsaConnection: ILsaHandle
): TNtxStatus;
// Establish a connection to LSA with verification
[RequiredPrivilege(SE_TCB_PRIVILEGE, rpAlways)]
function LsaxRegisterLogonProcess(
out hxLsaConnection: ILsaHandle;
const Name: AnsiString
): TNtxStatus;
// Find an authentication package by name
function LsaxLookupAuthPackage(
out PackageId: Cardinal;
const PackageName: AnsiString;
[opt] hxLsaConnection: ILsaHandle = nil
): TNtxStatus;
{ --------------------------------- Security -------------------------------- }
// Query security descriptor of a LSA object
function LsaxQuerySecurityObject(
[Access(OBJECT_READ_SECURITY)] const LsaHandle: ILsaHandle;
Info: TSecurityInformation;
out SD: ISecurityDescriptor
): TNtxStatus;
// Set security descriptor on a LSA object
function LsaxSetSecurityObject(
[Access(OBJECT_WRITE_SECURITY)] const LsaHandle: ILsaHandle;
Info: TSecurityInformation;
[in] SD: PSecurityDescriptor
): TNtxStatus;
implementation
uses
Ntapi.ntdef, Ntapi.ntstatus, Ntapi.NtSecApi, Ntapi.ntrtl, NtUtils.Tokens.Misc,
NtUtils.Security.Sid, DelphiUtils.AutoObjects;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
type
TLsaAutoHandle = class(TCustomAutoHandle, ILsaHandle, IAutoReleasable)
procedure Release; override;
end;
TLsaAutoPointer = class(TCustomAutoPointer, IAutoPointer, IAutoReleasable)
procedure Release; override;
end;
TLsaAutoMemory = class(TCustomAutoMemory, IMemory, IAutoPointer, IAutoReleasable)
procedure Release; override;
end;
{ Common & Policy }
procedure TLsaAutoHandle.Release;
begin
if FHandle <> 0 then
LsaClose(FHandle);
FHandle := 0;
inherited;
end;
procedure TLsaAutoPointer.Release;
begin
if Assigned(FData) then
LsaFreeMemory(FData);
FData := nil;
inherited;
end;
procedure TLsaAutoMemory.Release;
begin
if Assigned(FData) then
LsaFreeMemory(FData);
FData := nil;
inherited;
end;
function LsaxDelayFreeMemory(
[in] Buffer: Pointer
): IAutoReleasable;
begin
Result := Auto.Delay(
procedure
begin
LsaFreeMemory(Buffer);
end
);
end;
function LsaxOpenPolicy;
var
ObjAttr: TObjectAttributes;
SystemNameStr: TLsaUnicodeString;
hPolicy: TLsaHandle;
begin
Result := RtlxInitUnicodeString(SystemNameStr, SystemName);
if not Result.IsSuccess then
Exit;
InitializeObjectAttributes(ObjAttr);
Result.Location := 'LsaOpenPolicy';
Result.LastCall.OpensForAccess(DesiredAccess);
Result.Status := LsaOpenPolicy(SystemNameStr.RefOrNil, ObjAttr, DesiredAccess,
hPolicy);
if Result.IsSuccess then
hxPolicy := TLsaAutoHandle.Capture(hPolicy);
end;
function LsaxpEnsureConnected;
begin
if not Assigned(hxPolicy) then
Result := LsaxOpenPolicy(hxPolicy, DesiredAccess)
else
Result := NtxSuccess;
end;
function LsaxQueryPolicy;
var
Buffer: Pointer;
begin
Result.Location := 'LsaQueryInformationPolicy';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.LastCall.Expects(ExpectedPolicyQueryAccess(InfoClass));
Result.Status := LsaQueryInformationPolicy(HandleOrDefault(hxPolicy),
InfoClass, Buffer);
if Result.IsSuccess then
xBuffer := TLsaAutoPointer.Capture(Buffer);
end;
function LsaxSetPolicy;
begin
Result.Location := 'LsaSetInformationPolicy';
Result.LastCall.UsesInfoClass(InfoClass, icSet);
Result.LastCall.Expects(ExpectedPolicySetAccess(InfoClass));
Result.Status := LsaSetInformationPolicy(HandleOrDefault(hxPolicy), InfoClass,
Buffer);
end;
{ Accounts }
function LsaxOpenAccount;
var
hAccount: TLsaHandle;
begin
Result := LsaxpEnsureConnected(hxPolicy, POLICY_VIEW_LOCAL_INFORMATION);
if not Result.IsSuccess then
Exit;
Result.Location := 'LsaOpenAccount';
Result.LastCall.OpensForAccess(DesiredAccess);
Result.LastCall.Expects<TLsaPolicyAccessMask>(POLICY_VIEW_LOCAL_INFORMATION);
Result.Status := LsaOpenAccount(hxPolicy.Handle, AccountSid.Data,
DesiredAccess, hAccount);
if Result.IsSuccess then
hxAccount := TLsaAutoHandle.Capture(hAccount);
end;
function LsaxCreateAccount;
var
hAccount: TLsaHandle;
begin
Result := LsaxpEnsureConnected(hxPolicy, POLICY_CREATE_ACCOUNT);
if not Result.IsSuccess then
Exit;
Result.Location := 'LsaCreateAccount';
Result.LastCall.OpensForAccess(DesiredAccess);
Result.LastCall.Expects<TLsaPolicyAccessMask>(POLICY_CREATE_ACCOUNT);
Result.Status := LsaCreateAccount(hxPolicy.Handle, AccountSid.Data,
DesiredAccess, hAccount);
if Result.IsSuccess then
hxAccount := TLsaAutoHandle.Capture(hAccount);
end;
function LsaxDeleteAccount;
begin
Result.Location := 'LsaDelete';
Result.LastCall.Expects<TLsaAccountAccessMask>(_DELETE or ACCOUNT_VIEW);
Result.Status := LsaDelete(HandleOrDefault(hxAccount));
end;
function LsaxEnumerateAccounts;
var
EnumContext: TLsaEnumerationHandle;
Buffer: PSidArray;
BufferDeallocator: IAutoReleasable;
Count, i: Integer;
begin
Result := LsaxpEnsureConnected(hxPolicy, POLICY_VIEW_LOCAL_INFORMATION);
if not Result.IsSuccess then
Exit;
EnumContext := 0;
Result.Location := 'LsaEnumerateAccounts';
Result.LastCall.Expects<TLsaPolicyAccessMask>(POLICY_VIEW_LOCAL_INFORMATION);
Result.Status := LsaEnumerateAccounts(hxPolicy.Handle, EnumContext, Buffer,
MAX_PREFERRED_LENGTH, Count);
if not Result.IsSuccess then
Exit;
BufferDeallocator := LsaxDelayFreeMemory(Buffer);
SetLength(Accounts, Count);
for i := 0 to High(Accounts) do
begin
Result := RtlxCopySid(Buffer{$R-}[i]{$IFDEF R+}{$R+}{$ENDIF}, Accounts[i]);
if not Result.IsSuccess then
Break;
end;
end;
function LsaxEnumeratePrivilegesAccount;
var
Buffer: PPrivilegeSet;
BufferDeallocator: IAutoReleasable;
i: Integer;
begin
Result.Location := 'LsaEnumeratePrivilegesOfAccount';
Result.LastCall.Expects<TLsaAccountAccessMask>(ACCOUNT_VIEW);
Result.Status := LsaEnumeratePrivilegesOfAccount(HandleOrDefault(hxAccount),
Buffer);
if not Result.IsSuccess then
Exit;
BufferDeallocator := LsaxDelayFreeMemory(Buffer);
SetLength(Privileges, Buffer.PrivilegeCount);
for i := 0 to High(Privileges) do
Privileges[i] := Buffer.Privilege{$R-}[i]{$IFDEF R+}{$R+}{$ENDIF};
end;
function LsaxEnumeratePrivilegesAccountBySid;
var
hxAccount: ILsaHandle;
begin
Result := LsaxOpenAccount(hxAccount, AccountSid, ACCOUNT_VIEW);
if Result.IsSuccess then
Result := LsaxEnumeratePrivilegesAccount(hxAccount, Privileges);
end;
function LsaxAddPrivilegesAccount;
begin
Result.Location := 'LsaAddPrivilegesToAccount';
Result.LastCall.Expects<TLsaAccountAccessMask>(ACCOUNT_ADJUST_PRIVILEGES);
Result.Status := LsaAddPrivilegesToAccount(HandleOrDefault(hxAccount),
NtxpAllocPrivilegeSet(Privileges).Data);
end;
function LsaxRemovePrivilegesAccount;
begin
Result.Location := 'LsaRemovePrivilegesFromAccount';
Result.LastCall.Expects<TLsaAccountAccessMask>(ACCOUNT_ADJUST_PRIVILEGES);
Result.Status := LsaRemovePrivilegesFromAccount(HandleOrDefault(hxAccount),
RemoveAll, NtxpAllocPrivilegeSet(Privileges).Data);
end;
function LsaxManagePrivilegesAccount;
var
hxAccount: ILsaHandle;
begin
if (Length(Add) = 0) and (Length(Remove) = 0) and not RemoveAll then
Exit(NtxSuccess);
// Try to open the account
Result := LsaxOpenAccount(hxAccount, AccountSid, ACCOUNT_ADJUST_PRIVILEGES);
// If there is no such account
if Result.Matches(STATUS_OBJECT_NAME_NOT_FOUND, 'LsaOpenAccount') then
begin
// No account - no privileges - nothing to remove
if Length(Add) = 0 then
Exit(NtxSuccess);
// We need to add the account to LSA database to assign privileges
Result := LsaxCreateAccount(hxAccount, AccountSid, nil,
ACCOUNT_ADJUST_PRIVILEGES);
end;
// Add privileges
if Result.IsSuccess and (Length(Add) > 0) then
Result := LsaxAddPrivilegesAccount(hxAccount, Add);
// Remove privileges
if Result.IsSuccess and (RemoveAll or (Length(Remove) > 0)) then
Result := LsaxRemovePrivilegesAccount(hxAccount, RemoveAll, Remove);
end;
function LsaxQueryRightsAccount;
begin
Result.Location := 'LsaGetSystemAccessAccount';
Result.LastCall.Expects<TLsaAccountAccessMask>(ACCOUNT_VIEW);
Result.Status := LsaGetSystemAccessAccount(HandleOrDefault(hxAccount),
SystemAccess);
end;
function LsaxQueryRightsAccountBySid;
var
hxAccount: ILsaHandle;
begin
Result := LsaxOpenAccount(hxAccount, AccountSid, ACCOUNT_VIEW);
if Result.IsSuccess then
Result := LsaxQueryRightsAccount(hxAccount, SystemAccess);
end;
function LsaxSetRightsAccount;
begin
Result.Location := 'LsaSetSystemAccessAccount';
Result.LastCall.Expects<TLsaAccountAccessMask>(ACCOUNT_ADJUST_SYSTEM_ACCESS);
Result.Status := LsaSetSystemAccessAccount(HandleOrDefault(hxAccount),
SystemAccess);
end;
function LsaxSetRightsAccountBySid;
var
hxAccount: ILsaHandle;
begin
Result := LsaxOpenAccount(hxAccount, AccountSid,
ACCOUNT_ADJUST_SYSTEM_ACCESS);
// Add the account to the LSA database if necessary
if Result.Matches(STATUS_OBJECT_NAME_NOT_FOUND, 'LsaOpenAccount') then
begin
// No account - no rights - nothing to revoke
if SystemAccess = 0 then
Exit(NtxSuccess);
Result := LsaxCreateAccount(hxAccount, AccountSid, nil,
ACCOUNT_ADJUST_SYSTEM_ACCESS);
end;
if not Result.IsSuccess then
Exit;
Result := LsaxSetRightsAccount(hxAccount, SystemAccess);
end;
function LsaxEnumerateAccountsWithRightOrPrivilege;
var
Buffer: PLsaEnumerationInformation;
BufferDeallocator: IAutoReleasable;
NameStr: TLsaUnicodeString;
Count: Cardinal;
i: Integer;
begin
Result := LsaxpEnsureConnected(hxPolicy, POLICY_ENUMERATE_ACCOUNTS_WITH_RIGHT);
if not Result.IsSuccess then
Exit;
Result := RtlxInitUnicodeString(NameStr, RightOrPrivilegeName);
if not Result.IsSuccess then
Exit;
// Retrieve the list of accounts
Result.Location := 'LsaEnumerateAccountsWithUserRight';
Result.LastCall.Expects<TLsaPolicyAccessMask>(
POLICY_ENUMERATE_ACCOUNTS_WITH_RIGHT);
Result.Status := LsaEnumerateAccountsWithUserRight(hxPolicy.Handle, NameStr,
Buffer, Count);
if Result.Status = STATUS_NO_MORE_ENTRIES then
begin
// No accounts
Accounts := nil;
Exit(NtxSuccess);
end;
if not Result.IsSuccess then
Exit;
// Save account SIDs
BufferDeallocator := LsaxDelayFreeMemory(Buffer);
SetLength(Accounts, Count);
for i := 0 to High(Accounts) do
begin
Result := RtlxCopySid(Buffer{$R-}[i]{$IFDEF R+}{$R+}{$ENDIF}, Accounts[i]);
if not Result.IsSuccess then
Exit;
end;
end;
{ Privileges }
function LsaxEnumeratePrivileges;
var
EnumContext: TLsaEnumerationHandle;
Count, i: Integer;
Buffer: PPolicyPrivilegeDefinitionArray;
BufferDeallocator: IAutoReleasable;
begin
Result := LsaxpEnsureConnected(hxPolicy, POLICY_VIEW_LOCAL_INFORMATION);
if not Result.IsSuccess then
Exit;
EnumContext := 0;
Result.Location := 'LsaEnumeratePrivileges';
Result.LastCall.Expects<TLsaPolicyAccessMask>(POLICY_VIEW_LOCAL_INFORMATION);
Result.Status := LsaEnumeratePrivileges(hxPolicy.Handle, EnumContext, Buffer,
MAX_PREFERRED_LENGTH, Count);
if not Result.IsSuccess then
Exit;
BufferDeallocator := LsaxDelayFreeMemory(Buffer);
SetLength(Privileges, Count);
for i := 0 to High(Privileges) do
begin
Privileges[i].Name := Buffer{$R-}[i]{$IFDEF R+}{$R+}{$ENDIF}.Name.ToString;
Privileges[i].LocalValue := Buffer{$R-}[i]{$IFDEF R+}{$R+}{$ENDIF}.LocalValue;
end;
end;
function LsaxQueryPrivilege;
var
NameBuffer, DisplayNameBuffer: PLsaUnicodeString;
NameBufferDeallocator, DisplayNameBufferDeallocator: IAutoReleasable;
LangId: SmallInt;
begin
Result := LsaxpEnsureConnected(hxPolicy, POLICY_LOOKUP_NAMES);
if not Result.IsSuccess then
Exit;
// Get name based on LUID
Result.Location := 'LsaLookupPrivilegeName';
Result.LastCall.Expects<TLsaPolicyAccessMask>(POLICY_LOOKUP_NAMES);
Result.Status := LsaLookupPrivilegeName(hxPolicy.Handle, Luid, NameBuffer);
if not Result.IsSuccess then
Exit;
NameBufferDeallocator := LsaxDelayFreeMemory(NameBuffer);
Name := NameBuffer.ToString;
// Get description based on name
Result.Location := 'LsaLookupPrivilegeDisplayName';
Result.LastCall.Expects<TLsaPolicyAccessMask>(POLICY_LOOKUP_NAMES);
Result.Status := LsaLookupPrivilegeDisplayName(hxPolicy.Handle, NameBuffer^,
DisplayNameBuffer, LangId);
if not Result.IsSuccess then
Exit;
DisplayNameBufferDeallocator := LsaxDelayFreeMemory(DisplayNameBuffer);
DisplayName := DisplayNameBuffer.ToString;
end;
function LsaxQueryIntegrityPrivilege;
begin
// Some privileges require a specific integrity level to be enabled.
// The ones that require more than Medium also trigger UAC to split logon
// sessions. The following data is gathered by experimenting and should be
// maintained in sync with Windows behavior when new privileges are
// introduced.
case TSeWellKnownPrivilege(Luid) of
// Ten of them require High
SE_CREATE_TOKEN_PRIVILEGE,
SE_TCB_PRIVILEGE,
SE_TAKE_OWNERSHIP_PRIVILEGE,
SE_LOAD_DRIVER_PRIVILEGE,
SE_BACKUP_PRIVILEGE,
SE_RESTORE_PRIVILEGE,
SE_DEBUG_PRIVILEGE,
SE_IMPERSONATE_PRIVILEGE,
SE_RELABEL_PRIVILEGE,
SE_DELEGATE_SESSION_USER_IMPERSONATE_PRIVILEGE:
Result := SECURITY_MANDATORY_HIGH_RID;
// Three of them does not require anything
SE_CHANGE_NOTIFY_PRIVILEGE,
SE_UNDOCK_PRIVILEGE,
SE_INCREASE_WORKING_SET_PRIVILEGE:
Result := SECURITY_MANDATORY_UNTRUSTED_RID;
else
// All other require Medium
Result := SECURITY_MANDATORY_MEDIUM_RID;
end;
end;
{ Logon process }
type
TLsaAutoConnection = class(TCustomAutoHandle, ILsaHandle, IAutoReleasable)
procedure Release; override;
end;
procedure TLsaAutoConnection.Release;
begin
if FHandle <> 0 then
LsaDeregisterLogonProcess(FHandle);
FHandle := 0;
inherited;
end;
function LsaxConnectUntrusted;
var
hLsaConnection: TLsaHandle;
begin
Result.Location := 'LsaConnectUntrusted';
Result.Status := LsaConnectUntrusted(hLsaConnection);
if Result.IsSuccess then
hxLsaConnection := TLsaAutoConnection.Capture(hLsaConnection);
end;
function LsaxRegisterLogonProcess;
var
hLsaConnection: TLsaHandle;
NameStr: TLsaAnsiString;
Reserved: Cardinal;
begin
Result := RtlxInitAnsiString(NameStr, Name);
if not Result.IsSuccess then
Exit;
Result.Location := 'LsaRegisterLogonProcess';
Result.LastCall.ExpectedPrivilege := SE_TCB_PRIVILEGE;
Result.Status := LsaRegisterLogonProcess(NameStr, hLsaConnection, Reserved);
if Result.IsSuccess then
hxLsaConnection := TLsaAutoConnection.Capture(hLsaConnection);
end;
function LsaxLookupAuthPackage;
var
PackageNameStr: TNtAnsiString;
begin
if not Assigned(hxLsaConnection) then
begin
Result := LsaxConnectUntrusted(hxLsaConnection);
if not Result.IsSuccess then
Exit;
end;
Result := RtlxInitAnsiString(PackageNameStr, PackageName);
if not Result.IsSuccess then
Exit;
Result.Location := 'LsaLookupAuthenticationPackage';
Result.LastCall.Parameter := String(PackageName);
Result.Status := LsaLookupAuthenticationPackage(hxLsaConnection.Handle,
PackageNameStr, PackageId);
end;
function LsaxQuerySecurityObject;
var
Buffer: PSecurityDescriptor;
begin
Result.Location := 'LsaQuerySecurityObject';
Result.LastCall.Expects(SecurityReadAccess(Info));
Result.Status := LsaQuerySecurityObject(HandleOrDefault(LsaHandle), Info,
Buffer);
if Result.IsSuccess then
IMemory(SD) := TLsaAutoMemory.Capture(Buffer,
RtlLengthSecurityDescriptor(Buffer));
end;
function LsaxSetSecurityObject;
begin
Result.Location := 'LsaSetSecurityObject';
Result.LastCall.Expects(SecurityWriteAccess(Info));
Result.Status := LsaSetSecurityObject(HandleOrDefault(LsaHandle), Info, SD);
end;
end.