-
Notifications
You must be signed in to change notification settings - Fork 36
/
NtUtils.Security.pas
844 lines (689 loc) · 21.2 KB
/
NtUtils.Security.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
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
unit NtUtils.Security;
{
Base functions for working with Security Descriptors.
}
interface
uses
Ntapi.WinNt, Ntapi.ntseapi, Ntapi.Versions, NtUtils, NtUtils.Security.Acl;
type
TSecurityDescriptorData = record
Control: TSecurityDescriptorControl;
Owner, Group: ISid;
Dacl, Sacl: IAcl;
class function Create(
Control: TSecurityDescriptorControl = 0;
Dacl: IAcl = nil;
Sacl: IAcl = nil;
Owner: ISid = nil;
Group: ISid = nil
): TSecurityDescriptorData; static;
end;
TSecurityQueryFunction = function (
[Access(OBJECT_READ_SECURITY)] const hxObject: IHandle;
SecurityInformation: TSecurityInformation;
out xMemory: ISecurityDescriptor
): TNtxStatus;
TSecuritySetFunction = function (
[Access(OBJECT_WRITE_SECURITY)] const hxObject: IHandle;
SecurityInformation: TSecurityInformation;
[in] SD: PSecurityDescriptor
): TNtxStatus;
// Capture a copy of a security descriptor
function RtlxCaptureSecurityDescriptor(
[in] SourceSD: PSecurityDescriptor;
out SdData: TSecurityDescriptorData
): TNtxStatus;
// Allocate a new self-relative security descriptor
function RtlxAllocateSecurityDescriptor(
const SD: TSecurityDescriptorData;
out xMemory: ISecurityDescriptor
): TNtxStatus;
{ Object Security: Query }
// Query a security of an generic object
function RtlxQuerySecurityObject(
[Access(OBJECT_READ_SECURITY)] const hxObject: IHandle;
Method: TSecurityQueryFunction;
SecurityInformation: TSecurityInformation;
out SD: TSecurityDescriptorData
): TNtxStatus;
// Query DACL of an generic object
function RtlxQueryDaclObject(
[Access(READ_CONTROL)] const hxObject: IHandle;
Method: TSecurityQueryFunction;
[MayReturnNil] out Dacl: IAcl
): TNtxStatus;
// Query SACL of an generic object
function RtlxQuerySaclObject(
[Access(ACCESS_SYSTEM_SECURITY)] const hxObject: IHandle;
Method: TSecurityQueryFunction;
[MayReturnNil] out Sacl: IAcl;
SecurityInformation: TSecurityInformation = SACL_SECURITY_INFORMATION
): TNtxStatus;
// Query owner of a generic object
function RtlxQueryOwnerObject(
[Access(READ_CONTROL)] const hxObject: IHandle;
Method: TSecurityQueryFunction;
out Owner: ISid
): TNtxStatus;
// Query primary group of a generic object
function RtlxQueryGroupObject(
[Access(READ_CONTROL)] const hxObject: IHandle;
Method: TSecurityQueryFunction;
out PrimaryGroup: ISid
): TNtxStatus;
// Query mandatory label of a generic object
function RtlxQueryLabelObject(
[Access(READ_CONTROL)] const hxObject: IHandle;
Method: TSecurityQueryFunction;
out LabelRid: TIntegrityRid;
out Policy: TMandatoryLabelMask
): TNtxStatus;
// Query trust label of a generic object
[MinOSVersion(OsWin81)]
function RtlxQueryTrustObject(
[Access(READ_CONTROL)] const hxObject: IHandle;
Method: TSecurityQueryFunction;
out TrustType: TSecurityTrustType;
out TrustLevel: TSecurityTrustLevel;
out AccessMask: TAccessMask
): TNtxStatus;
{ Object Security: Set }
// Set a security on an generic object
function RtlxSetSecurityObject(
[Access(OBJECT_WRITE_SECURITY)] const hxObject: IHandle;
Method: TSecuritySetFunction;
SecurityInformation: TSecurityInformation;
const SD: TSecurityDescriptorData
): TNtxStatus;
// Set DACL on an generic object
function RtlxSetDaclObject(
[Access(WRITE_DAC)] const hxObject: IHandle;
Method: TSecuritySetFunction;
const Dacl: IAcl;
SecurityInformation: TSecurityInformation = DACL_SECURITY_INFORMATION
): TNtxStatus;
// Set SACL on an generic object
function RtlxSetSaclObject(
[Access(ACCESS_SYSTEM_SECURITY)] const hxObject: IHandle;
Method: TSecuritySetFunction;
const Sacl: IAcl;
SecurityInformation: TSecurityInformation = SACL_SECURITY_INFORMATION
): TNtxStatus;
// Set owner on an generic object
function RtlxSetOwnerObject(
[Access(WRITE_OWNER)] const hxObject: IHandle;
Method: TSecuritySetFunction;
const Owner: ISid
): TNtxStatus;
// Set primary group on an generic object
function RtlxSetGroupObject(
[Access(WRITE_OWNER)] const hxObject: IHandle;
Method: TSecuritySetFunction;
const PrimaryGroup: ISid
): TNtxStatus;
// Set mandatory label on an generic object
function RtlxSetLabelObject(
[Access(WRITE_OWNER)] const hxObject: IHandle;
Method: TSecuritySetFunction;
LabelRid: TIntegrityRid;
Policy: TMandatoryLabelMask
): TNtxStatus;
// Set trust label on an generic object
[MinOSVersion(OsWin81)]
function RtlxSetTrustObject(
[Access(WRITE_DAC)] const hxObject: IHandle;
Method: TSecuritySetFunction;
TrustType: TSecurityTrustType;
TrustLevel: TSecurityTrustLevel;
AccessMask: TAccessMask
): TNtxStatus;
{ Denying DACL }
// Craft a DACL that denies everything
function RtlxAllocateDenyingDacl: IAcl;
// Craft a security descriptor that denies everything
function RtlxAllocateDenyingSd: ISecurityDescriptor;
{ SDDL }
// Parse a textual definition of a security descriptor
function AdvxSecurityDescriptorFromSddl(
const SDDL: String;
out SecDesc: ISecurityDescriptor
): TNtxStatus;
// Parse a textual definition of a security descriptor and capture the result
function AdvxSecurityDescriptorDataFromSddl(
const SDDL: String;
out SD: TSecurityDescriptorData
): TNtxStatus;
// Construct a textual definition of a security descriptor
function AdvxSecurityDescriptorToSddl(
[in] SecDesc: PSecurityDescriptor;
SecurityInformation: TSecurityInformation;
out SDDL: String
): TNtxStatus;
// Construct a textual definition of a captured security descriptor
function AdvxSecurityDescriptorDataToSddl(
const SD: TSecurityDescriptorData;
SecurityInformation: TSecurityInformation;
out SDDL: String
): TNtxStatus;
// Convert an ACE to SDDL
function AdvxAceToSddl(
const Ace: TAceData;
out AceSDDL: String
): TNtxStatus;
// Parse a SDDL of an ACE
function AdvxAceFromSddl(
const AceSDDL: String;
IsAccessAce: Boolean;
out Ace: TAceData
): TNtxStatus;
// Convert a condition of a callback ACE to SDDL
function AdvxAceConditionToSddl(
[opt] const AceCondition: IMemory;
out ConditionString: String
): TNtxStatus;
// Convert a SDDL condition string to a binary form suitable for callback ACEs
function AdvxAceConditionFromSddl(
const ConditionString: String;
[MayReturnNil] out AceCondition: IMemory
): TNtxStatus;
implementation
uses
Ntapi.ntrtl, Ntapi.ntstatus, Ntapi.WinBase, NtUtils.SysUtils,
NtUtils.Security.Sid, DelphiUtils.AutoObjects;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
class function TSecurityDescriptorData.Create;
begin
Result.Control := Control;
Result.Owner := Owner;
Result.Group := Group;
Result.Dacl := Dacl;
Result.Sacl := Sacl;
end;
function RtlxCaptureSecurityDescriptor;
var
Revision: Cardinal;
Sid: PSid;
Acl: PAcl;
Defaulted, Present: Boolean;
begin
if not RtlValidSecurityDescriptor(SourceSD) then
begin
Result.Location := 'RtlValidSecurityDescriptor';
Result.Status := STATUS_INVALID_SECURITY_DESCR;
Exit;
end;
// Control flags
Result.Location := 'RtlGetControlSecurityDescriptor';
Result.Status := RtlGetControlSecurityDescriptor(SourceSD, SdData.Control,
Revision);
if not Result.IsSuccess then
Exit;
// Owner
Sid := nil;
Result.Location := 'RtlGetOwnerSecurityDescriptor';
Result.Status := RtlGetOwnerSecurityDescriptor(SourceSD, Sid, Defaulted);
if not Result.IsSuccess then
Exit;
if Assigned(Sid) then
Result := RtlxCopySid(Sid, SdData.Owner);
// Primary group
Sid := nil;
Result.Location := 'RtlGetGroupSecurityDescriptor';
Result.Status := RtlGetGroupSecurityDescriptor(SourceSD, Sid, Defaulted);
if not Result.IsSuccess then
Exit;
if Assigned(Sid) then
Result := RtlxCopySid(Sid, SdData.Group);
// DACL
Acl := nil;
Result.Location := 'RtlGetDaclSecurityDescriptor';
Result.Status := RtlGetDaclSecurityDescriptor(SourceSD, Present, Acl,
Defaulted);
if not Result.IsSuccess then
Exit;
if Assigned(Acl) then
Result := RtlxCaptureAcl(SdData.Dacl, Acl);
// SACL
Acl := nil;
Result.Location := 'RtlGetSaclSecurityDescriptor';
Result.Status := RtlGetSaclSecurityDescriptor(SourceSD, Present, Acl,
Defaulted);
if Result.IsSuccess and Assigned(Acl) then
Result := RtlxCaptureAcl(SdData.Sacl, Acl);
end;
function RtlxAllocateSecurityDescriptor;
const
SE_CONTROL_CUSTOM = High(TSecurityDescriptorControl)
and not SE_OWNER_DEFAULTED and not SE_GROUP_DEFAULTED
and not SE_DACL_PRESENT and not SE_DACL_DEFAULTED
and not SE_SACL_PRESENT and not SE_SACL_DEFAULTED
and not SE_SELF_RELATIVE;
var
SecDesc: TSecurityDescriptor;
BufferSize: Cardinal;
begin
Result.Location := 'RtlCreateSecurityDescriptor';
Result.Status := RtlCreateSecurityDescriptor(@SecDesc,
SECURITY_DESCRIPTOR_REVISION);
if not Result.IsSuccess then
Exit;
// Owner
Result.Location := 'RtlSetOwnerSecurityDescriptor';
Result.Status := RtlSetOwnerSecurityDescriptor(@SecDesc, Auto.RefOrNil<PSid>(
SD.Owner), BitTest(SD.Control and SE_OWNER_DEFAULTED));
if not Result.IsSuccess then
Exit;
// Primary group
Result.Location := 'RtlSetGroupSecurityDescriptor';
Result.Status := RtlSetGroupSecurityDescriptor(@SecDesc, Auto.RefOrNil<PSid>(
SD.Group), BitTest(SD.Control and SE_GROUP_DEFAULTED));
if not Result.IsSuccess then
Exit;
// DACL
Result.Location := 'RtlSetDaclSecurityDescriptor';
Result.Status := RtlSetDaclSecurityDescriptor(@SecDesc,
BitTest(SD.Control and SE_DACL_PRESENT), Auto.RefOrNil<PAcl>(SD.Dacl),
BitTest(SD.Control and SE_DACL_DEFAULTED));
if not Result.IsSuccess then
Exit;
// SACL
Result.Location := 'RtlSetSaclSecurityDescriptor';
Result.Status := RtlSetSaclSecurityDescriptor(@SecDesc,
BitTest(SD.Control and SE_SACL_PRESENT), Auto.RefOrNil<PAcl>(SD.Sacl),
BitTest(SD.Control and SE_SACL_DEFAULTED));
if not Result.IsSuccess then
Exit;
// Control flags
Result.Location := 'RtlSetControlSecurityDescriptor';
Result.Status := RtlSetControlSecurityDescriptor(@SecDesc, SD.Control and
SE_CONTROL_CUSTOM, SD.Control and SE_CONTROL_CUSTOM);
if not Result.IsSuccess then
Exit;
BufferSize := RtlLengthSecurityDescriptor(@SecDesc);
IMemory(xMemory) := Auto.AllocateDynamic(BufferSize);
Result.Location := 'RtlMakeSelfRelativeSD';
Result.Status := RtlMakeSelfRelativeSD(@SecDesc, xMemory.Data, BufferSize);
end;
{ Object Security }
function RtlxQuerySecurityObject;
var
xMemory: ISecurityDescriptor;
begin
Result := Method(hxObject, SecurityInformation, xMemory);
if Result.IsSuccess then
Result := RtlxCaptureSecurityDescriptor(xMemory.Data, SD);
end;
function RtlxQueryDaclObject;
var
SD: TSecurityDescriptorData;
begin
Result := RtlxQuerySecurityObject(hxObject, Method, DACL_SECURITY_INFORMATION,
SD);
if Result.IsSuccess then
Dacl := SD.Dacl;
end;
function RtlxQuerySaclObject;
var
SD: TSecurityDescriptorData;
begin
Result := RtlxQuerySecurityObject(hxObject, Method, SecurityInformation,
SD);
if Result.IsSuccess then
Sacl := SD.Sacl;
end;
function RtlxQueryOwnerObject;
var
SD: TSecurityDescriptorData;
begin
Result := RtlxQuerySecurityObject(hxObject, Method,
OWNER_SECURITY_INFORMATION, SD);
if not Result.IsSuccess then
Exit;
if not Assigned(SD.Owner) then
begin
Result.Location := 'RtlxQueryOwnerObject';
Result.Status := STATUS_INVALID_SID;
Exit;
end;
Owner := SD.Owner;
end;
function RtlxQueryGroupObject;
var
SD: TSecurityDescriptorData;
begin
Result := RtlxQuerySecurityObject(hxObject, Method,
GROUP_SECURITY_INFORMATION, SD);
if not Result.IsSuccess then
Exit;
if not Assigned(SD.Group) then
begin
Result.Location := 'RtlxQueryGroupObject';
Result.Status := STATUS_INVALID_SID;
Exit;
end;
PrimaryGroup := SD.Group;
end;
function RtlxQueryLabelObject;
var
SD: TSecurityDescriptorData;
Ace: TAceData;
i: Integer;
begin
Result := RtlxQuerySecurityObject(hxObject, Method,
LABEL_SECURITY_INFORMATION, SD);
if not Result.IsSuccess then
Exit;
for i := 0 to Pred(RtlxSizeAcl(SD.Sacl).AceCount) do
begin
Result := RtlxGetAce(SD.Sacl, i, Ace);
if not Result.IsSuccess then
Exit;
if Ace.AceType <> SYSTEM_MANDATORY_LABEL_ACE_TYPE then
Continue;
// Skip inherit-only ACEs
if BitTest(Ace.AceFlags and INHERIT_ONLY_ACE) then
Continue;
// The system only takes the first entry into account
LabelRid := RtlxRidSid(Ace.SID, SECURITY_MANDATORY_UNTRUSTED_RID);
Policy := Ace.Mask;
Exit;
end;
Result.Location := 'RtlxQueryLabelObject';
Result.Status := STATUS_NOT_FOUND;
end;
function RtlxQueryTrustObject;
var
SD: TSecurityDescriptorData;
Ace: TAceData;
SubAuthorities: TArray<Cardinal>;
i: Integer;
begin
Result := RtlxQuerySecurityObject(hxObject, Method,
PROCESS_TRUST_LABEL_SECURITY_INFORMATION, SD);
if not Result.IsSuccess then
Exit;
for i := 0 to Pred(RtlxSizeAcl(SD.Sacl).AceCount) do
begin
Result := RtlxGetAce(SD.Sacl, i, Ace);
if not Result.IsSuccess then
Exit;
if Ace.AceType <> SYSTEM_PROCESS_TRUST_LABEL_ACE_TYPE then
Continue;
// Skip inherit-only ACEs
if BitTest(Ace.AceFlags and INHERIT_ONLY_ACE) then
Continue;
// The system only takes the first entry into account
AccessMask := Ace.Mask;
SubAuthorities := RtlxSubAuthoritiesSid(Ace.SID);
if Length(SubAuthorities) >= SECURITY_PROCESS_TRUST_AUTHORITY_RID_COUNT then
begin
TrustType := SubAuthorities[Pred(High(SubAuthorities))];
TrustLevel := SubAuthorities[High(SubAuthorities)];
end
else
begin
TrustType := SECURITY_PROCESS_PROTECTION_TYPE_NONE_RID;
TrustLevel := SECURITY_PROCESS_PROTECTION_LEVEL_NONE_RID;
end;
Exit;
end;
Result.Location := 'RtlxQueryTrustObject';
Result.Status := STATUS_NOT_FOUND;
end;
function RtlxSetSecurityObject;
var
xMemory: ISecurityDescriptor;
begin
Result := RtlxAllocateSecurityDescriptor(SD, xMemory);
if Result.IsSuccess then
Result := Method(hxObject, SecurityInformation, xMemory.Data);
end;
function RtlxSetDaclObject;
begin
Result := RtlxSetSecurityObject(hxObject, Method, SecurityInformation,
TSecurityDescriptorData.Create(SE_DACL_PRESENT, Dacl));
end;
function RtlxSetSaclObject;
begin
Result := RtlxSetSecurityObject(hxObject, Method, SecurityInformation,
TSecurityDescriptorData.Create(SE_SACL_PRESENT, nil, Sacl));
end;
function RtlxSetOwnerObject;
begin
Result := RtlxSetSecurityObject(hxObject, Method, OWNER_SECURITY_INFORMATION,
TSecurityDescriptorData.Create(0, nil, nil, Owner));
end;
function RtlxSetGroupObject;
begin
Result := RtlxSetSecurityObject(hxObject, Method, GROUP_SECURITY_INFORMATION,
TSecurityDescriptorData.Create(0, nil, nil, nil, PrimaryGroup));
end;
function RtlxSetLabelObject;
var
Sacl: IAcl;
begin
Result := RtlxBuildAcl(Sacl, [
TAceData.New(SYSTEM_MANDATORY_LABEL_ACE_TYPE, 0, Policy,
RtlxMakeSid(SECURITY_MANDATORY_LABEL_AUTHORITY, [LabelRid])
)
]);
if not Result.IsSuccess then
Exit;
Result := RtlxSetSecurityObject(hxObject, Method, LABEL_SECURITY_INFORMATION,
TSecurityDescriptorData.Create(SE_SACL_PRESENT, nil, Sacl));
end;
function RtlxSetTrustObject;
var
Sacl: IAcl;
begin
Result := RtlxBuildAcl(Sacl, [
TAceData.New(SYSTEM_PROCESS_TRUST_LABEL_ACE_TYPE, 0, AccessMask,
RtlxMakeSid(SECURITY_PROCESS_TRUST_AUTHORITY, [TrustType, TrustLevel])
)
]);
if not Result.IsSuccess then
Exit;
Result := RtlxSetSecurityObject(hxObject, Method,
PROCESS_TRUST_LABEL_SECURITY_INFORMATION,
TSecurityDescriptorData.Create(SE_SACL_PRESENT, nil, Sacl));
end;
{ Denying DACL }
function RtlxAllocateDenyingDacl;
begin
if not RtlxBuildAcl(Result, [
TAceData.New(ACCESS_DENIED_ACE_TYPE, 0, GENERIC_ALL,
RtlxMakeSid(SECURITY_CREATOR_SID_AUTHORITY,
[SECURITY_CREATOR_OWNER_RIGHTS_RID]))
]).IsSuccess then
Result := nil;
end;
function RtlxAllocateDenyingSd;
begin
if not RtlxAllocateSecurityDescriptor(TSecurityDescriptorData.Create(
SE_DACL_PRESENT, RtlxAllocateDenyingDacl), Result).IsSuccess then
Result := nil;
end;
{ SDDL }
type
TAutoLocalMem = class (TCustomAutoMemory, IMemory, IAutoPointer, IAutoReleasable)
procedure Release; override;
end;
procedure TAutoLocalMem.Release;
begin
if Assigned(FData) then
LocalFree(FData);
FData := nil;
inherited;
end;
function AdvxSecurityDescriptorFromSddl;
var
pSD: PSecurityDescriptor;
Size: Cardinal;
begin
Size := 0;
Result.Location := 'ConvertStringSecurityDescriptorToSecurityDescriptorW';
Result.Win32Result := ConvertStringSecurityDescriptorToSecurityDescriptorW(
PWideChar(SDDL), SECURITY_DESCRIPTOR_REVISION, pSD, @Size);
if Result.IsSuccess then
IMemory(SecDesc) := TAutoLocalMem.Capture(pSD, Size);
end;
function AdvxSecurityDescriptorDataFromSddl;
var
SecDesc: ISecurityDescriptor;
begin
Result := AdvxSecurityDescriptorFromSddl(SDDL, SecDesc);
if not Result.IsSuccess then
Exit;
Result := RtlxCaptureSecurityDescriptor(SecDesc.Data, SD);
end;
function AdvxSecurityDescriptorToSddl;
var
Buffer: PWideChar;
BufferDeallocator: IAutoReleasable;
Size: Cardinal;
begin
Size := 0;
Result.Location := 'ConvertSecurityDescriptorToStringSecurityDescriptorW';
Result.Win32Result := ConvertSecurityDescriptorToStringSecurityDescriptorW(
SecDesc, SECURITY_DESCRIPTOR_REVISION, SecurityInformation, Buffer, @Size);
if not Result.IsSuccess then
Exit;
BufferDeallocator := AdvxDelayLocalFree(Buffer);
SDDL := RtlxCaptureString(Buffer, Size);
end;
function AdvxSecurityDescriptorDataToSddl;
var
SecDesc: ISecurityDescriptor;
begin
Result := RtlxAllocateSecurityDescriptor(SD, SecDesc);
if not Result.IsSuccess then
Exit;
Result := AdvxSecurityDescriptorToSddl(SecDesc.Data, SecurityInformation,
SDDL);
end;
function AdvxAceToSddl;
var
SecDesc: TSecurityDescriptorData;
Acl: IAcl;
begin
// Make an ACL with this ACE
Result := RtlxBuildAcl(Acl, [Ace]);
if not Result.IsSuccess then
Exit;
// Construct a security descriptor definition
if Ace.AceType in AccessAces then
SecDesc := TSecurityDescriptorData.Create(SE_DACL_PRESENT, Acl)
else if Ace.AceType in SystemAces then
SecDesc := TSecurityDescriptorData.Create(SE_SACL_PRESENT, nil, Acl)
else
begin
// Unrecognized type
Result.Location := 'AdvxAceToSddl';
Result.Status := STATUS_INVALID_ACL;
Exit;
end;
// Convert it to SDDL
Result := AdvxSecurityDescriptorDataToSddl(SecDesc,
AceSecurityInformation[Ace.AceType], AceSDDL);
if not Result.IsSuccess then
Exit;
// Extract the ACE portion
if not RtlxPrefixStripString('D:', AceSDDL) and
not RtlxPrefixStripString('S:', AceSDDL) then
begin
AceSDDL := '';
Result.Location := 'AdvxAceToSddl';
Result.Status := STATUS_UNSUCCESSFUL;
end;
end;
function AdvxAceFromSddl;
var
SD: TSecurityDescriptorData;
Acl: IAcl;
begin
// Make a security descriptor SDDL with one ACE
if IsAccessAce then
Result := AdvxSecurityDescriptorDataFromSddl('D:' + AceSDDL, SD)
else
Result := AdvxSecurityDescriptorDataFromSddl('S:' + AceSDDL, SD);
if not Result.IsSuccess then
Exit;
// Extract either DACL or SACL
if IsAccessAce and BitTest(SD.Control and SE_DACL_PRESENT) and
Assigned(SD.Dacl) and (RtlxSizeAcl(SD.Dacl).AceCount = 1) then
Acl := SD.Dacl
else if not IsAccessAce and BitTest(SD.Control and SE_SACL_PRESENT) and
Assigned(SD.Sacl) and (RtlxSizeAcl(SD.Sacl).AceCount = 1) then
Acl := SD.Sacl
else
begin
Result.Location := 'AdvxAceFromSddl';
Result.Status := STATUS_UNSUCCESSFUL;
Exit;
end;
// Extract the ACE in its binary form
Result := RtlxGetAce(Acl, 0, Ace);
end;
function AdvxAceConditionToSddl;
var
AceCopy: TAceData;
begin
if not Assigned(AceCondition) then
begin
ConditionString := '';
Exit(NtxSuccess);
end;
// Make an ACE with a known SDDL representation except for the condition part
AceCopy := Default(TAceData);
AceCopy.AceType := ACCESS_ALLOWED_CALLBACK_ACE_TYPE;
AceCopy.Mask := GENERIC_ALL;
AceCopy.SID := RtlxMakeSid(SECURITY_NT_AUTHORITY,
[SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS]);
AceCopy.ExtraData := AceCondition;
// Convert the ACE to SDDL
Result := AdvxAceToSddl(AceCopy, ConditionString);
if not Result.IsSuccess then
Exit;
// Extract the condition
if not RtlxPrefixStripString('(XA;;GA;;;BU;(', ConditionString) or
not RtlxSuffixStripString('))', ConditionString) then
begin
ConditionString := '';
Result.Location := 'AdvxAceConditionToSddl';
Result.Status := STATUS_UNSUCCESSFUL;
end;
end;
function AdvxAceConditionFromSddl;
var
SD: TSecurityDescriptorData;
Ace: TAceData;
begin
if ConditionString = '' then
begin
AceCondition := nil;
Exit(NtxSuccess);
end;
// Make a security descriptor with one callback ACE
Result := AdvxSecurityDescriptorDataFromSddl('D:(XA;;GA;;;BU;(' +
ConditionString + '))', SD);
if not Result.IsSuccess then
Exit;
if BitTest(SD.Control and SE_DACL_PRESENT) and Assigned(SD.Dacl) and
(RtlxSizeAcl(SD.Dacl).AceCount = 1) then
begin
// Extract the ACE in its binary form
Result := RtlxGetAce(SD.Dacl, 0, Ace);
if not Result.IsSuccess then
Exit;
if Assigned(Ace.ExtraData) then
begin
AceCondition := Ace.ExtraData;
Exit;
end;
end;
Result.Location := 'AdvxAceConditionFromSDDL';
Result.Status := STATUS_UNSUCCESSFUL;
end;
end.