-
Notifications
You must be signed in to change notification settings - Fork 35
/
NtUtils.Lsa.Audit.pas
513 lines (424 loc) · 14.5 KB
/
NtUtils.Lsa.Audit.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
unit NtUtils.Lsa.Audit;
{
This module provides function for working with global and per-user auditing
policy, including token-based overrides.
}
interface
uses
Ntapi.WinNt, Ntapi.NtSecApi, Ntapi.ntseapi, NtUtils, DelphiUtils.AutoObjects,
DelphiApi.Reflection;
type
TAuditCategoryMapping = record
Category: TGuid;
SubCategories: TArray<TGuid>;
end;
TAuditPolicyEntry = record
Category: TGuid;
SubCategory: TGuid;
case Boolean of
False: (Policy: TAuditEventPolicy);
True: (PolicyOverride: TAuditEventPolicyOverride);
end;
TTokenAuditPolicyHelper = record helper for TTokenAuditPolicy
private
function GetSubCategory(Index: Integer): TAuditEventPolicyOverride;
procedure SetSubCategory(Index: Integer; Value: TAuditEventPolicyOverride);
public
property SubCategory[Index: Integer]: TAuditEventPolicyOverride read GetSubCategory write SetSubCategory;
end;
// Enumerate audit categories and their sub categories
function LsaxEnumerateAuditMapping(
out Mapping: TArray<TAuditCategoryMapping>
): TNtxStatus;
// Get a friendly name for an audit category
function LsaxLookupAuditCategoryName(
const Category: TGuid;
out Name: String
): TNtxStatus;
// Get a friendly name for an audit sub category
function LsaxLookupAuditSubCategoryName(
const SubCategory: TGuid;
out Name: String
): TNtxStatus;
// Create an array of empty audit settings
function LsaxCreateEmptyAudit(
out Entries: TArray<TAuditPolicyEntry>
): TNtxStatus;
// Query system-wide audit settings
[Access(AUDIT_QUERY_SYSTEM_POLICY)]
[RequiredPrivilege(SE_AUDIT_PRIVILEGE, rpForBypassingChecks)]
[RequiredPrivilege(SE_SECURITY_PRIVILEGE, rpForBypassingChecks)]
function LsaxQuerySystemAudit(
out Entries: TArray<TAuditPolicyEntry>
): TNtxStatus;
// Set system-wide audit settings
[Access(AUDIT_SET_SYSTEM_POLICY)]
[RequiredPrivilege(SE_SECURITY_PRIVILEGE, rpForBypassingChecks)]
function LsaxSetSystemAudit(
const Entries: TArray<TAuditPolicyEntry>
): TNtxStatus;
// Query per-user audit override settings
[Access(AUDIT_QUERY_USER_POLICY)]
[RequiredPrivilege(SE_AUDIT_PRIVILEGE, rpForBypassingChecks)]
[RequiredPrivilege(SE_SECURITY_PRIVILEGE, rpForBypassingChecks)]
function LsaxQueryUserAudit(
const Sid: ISid;
out Entries: TArray<TAuditPolicyEntry>
): TNtxStatus;
// Set per-user audit override settings
[Access(AUDIT_SET_USER_POLICY)]
[RequiredPrivilege(SE_SECURITY_PRIVILEGE, rpForBypassingChecks)]
function LsaxSetUserAudit(
const Sid: ISid;
const Entries: TArray<TAuditPolicyEntry>
): TNtxStatus;
// Convert per-user audit settings to token audit settings
// Note: the entries must include all sub categories in order returned by
// LsaxCreateEmptyAudit
function LsaxUserAuditToTokenAudit(
const Entries: TArray<TAuditPolicyEntry>
): IMemory<PTokenAuditPolicy>;
// Convert token audit settings to per-user audit settings
function LsaxTokenAuditToUserAudit(
[in] Buffer: PTokenAuditPolicy;
out Entries: TArray<TAuditPolicyEntry>
): TNtxStatus;
// Query the security descriptor that protects the audit policy
[RequiredPrivilege(SE_SECURITY_PRIVILEGE, rpAlways)]
function LsaxQueryAuditSecurity(
[Reserved] const Unused: IHandle;
Info: TSecurityInformation;
out SD: ISecurityDescriptor
): TNtxStatus;
// Set the security descriptor that protects the audit policy
[RequiredPrivilege(SE_SECURITY_PRIVILEGE, rpAlways)]
function LsaxSetAuditSecurity(
[Reserved] const Unused: IHandle;
Info: TSecurityInformation;
[in] SD: PSecurityDescriptor
): TNtxStatus;
// Query the global SACL for a specific object types such as "File" and "Key"
[RequiredPrivilege(SE_SECURITY_PRIVILEGE, rpAlways)]
function LsaxQueryGlobalSacl(
out Acl: IAcl;
const ObjectTypeName: String
): TNtxStatus;
// Query the global SACL for a specific object types such as "File" and "Key"
[RequiredPrivilege(SE_SECURITY_PRIVILEGE, rpAlways)]
function LsaxSetGlobalSacl(
const ObjectTypeName: String;
[in, opt] Acl: PAcl
): TNtxStatus;
implementation
uses
Ntapi.ntrtl, DelphiUtils.Arrays;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
type
TAuditAutoMemory = class(TCustomAutoMemory, IMemory, IAutoPointer, IAutoReleasable)
procedure Release; override;
end;
procedure TAuditAutoMemory.Release;
begin
if Assigned(FData) then
AuditFree(FData);
FData := nil;
inherited;
end;
function LsaxDelayAuditFree(
[in] Buffer: Pointer
): IAutoReleasable;
begin
Result := Auto.Delay(
procedure
begin
AuditFree(Buffer);
end
);
end;
function LsaxEnumerateAuditMapping;
const
// See comments below
FIXUP_ID: array [0..1] of Cardinal = ($0CCE921B, $0CCE9227);
FIXUP_SHIFT: array [0..1] of Integer = (-2, -6);
var
Guids, SubGuids: PGuidArray;
GuidsDeallocator, SubGuidsDeallocator: IAutoReleasable;
Count, SubCount: Cardinal;
TempGuid: TGuid;
i, j, k: Integer;
begin
// Enumerate categories first
Result.Location := 'AuditEnumerateCategories';
Result.Win32Result := AuditEnumerateCategories(Guids, Count);
if not Result.IsSuccess then
Exit;
GuidsDeallocator := LsaxDelayAuditFree(Guids);
SetLength(Mapping, Count);
for i := 0 to High(Mapping) do
Mapping[i].Category := Guids{$R-}[i]{$IFDEF R+}{$R+}{$ENDIF};
for i := 0 to High(Mapping) do
begin
// Enumerate subcategories per category
Result.Location := 'AuditEnumerateSubCategories';
Result.Win32Result := AuditEnumerateSubCategories(@Mapping[i].Category,
False, SubGuids, SubCount);
if not Result.IsSuccess then
Exit;
SubGuidsDeallocator := LsaxDelayAuditFree(SubGuids);
SetLength(Mapping[i].SubCategories, SubCount);
for j := 0 to High(Mapping[i].SubCategories) do
Mapping[i].SubCategories[j] := SubGuids{$R-}[j]{$IFDEF R+}{$R+}{$ENDIF};
end;
// The system stores per-user audit overrides in tokens in compact form (see
// Ntapi.ntseapi.TTokenAuditPolicy) where each byte represents the policy for
// two sub-categories. These sub-categories are grouped by their category,
// but, for unknown reason, their order differs slightly from what you would
// get by flattening the arrays we are about to return. It appears that two
// sub-categories ("Special Logon" and "Other Object Access Events") are
// misplaced by 2 and 6, respectively; the rest matches precisely. Fix the
// order manually here, making the output compatible with TTokenAuditPolicy.
for i := 0 to High(Mapping) do
for j := 0 to High(Mapping[i].SubCategories) do
for k := 0 to High(FIXUP_ID) do
if Mapping[i].SubCategories[j].D1 = FIXUP_ID[k] then
begin
TempGuid := Mapping[i].SubCategories[j];
Delete(Mapping[i].SubCategories, j, 1);
Insert(TempGuid, Mapping[i].SubCategories, j + FIXUP_SHIFT[k]);
end;
end;
function LsaxLookupAuditCategoryName;
var
Buffer: PWideChar;
BufferDeallocator: IAutoReleasable;
begin
Result.Location := 'AuditLookupCategoryNameW';
Result.Win32Result := AuditLookupCategoryNameW(Category, Buffer);
if not Result.IsSuccess then
Exit;
BufferDeallocator := LsaxDelayAuditFree(Buffer);
Name := String(Buffer);
end;
function LsaxLookupAuditSubCategoryName;
var
Buffer: PWideChar;
BufferDeallocator: IAutoReleasable;
begin
Result.Location := 'AuditLookupSubCategoryNameW';
Result.Win32Result := AuditLookupSubCategoryNameW(SubCategory, Buffer);
if not Result.IsSuccess then
Exit;
BufferDeallocator := LsaxDelayAuditFree(Buffer);
Name := String(Buffer);
end;
function LsaxCreateEmptyAudit;
var
Mapping: TArray<TAuditCategoryMapping>;
begin
Result := LsaxEnumerateAuditMapping(Mapping);
if not Result.IsSuccess then
Exit;
// Expand all sub-categories and concatenate them, converting in the process
Entries := TArray.FlattenEx<TAuditCategoryMapping, TAuditPolicyEntry>(
Mapping,
function (const Entry: TAuditCategoryMapping): TArray<TAuditPolicyEntry>
var
i: Integer;
begin
SetLength(Result, Length(Entry.SubCategories));
for i := 0 to High(Result) do
begin
Result[i].Category := Entry.Category;
Result[i].SubCategory := Entry.SubCategories[i];
Result[i].Policy := POLICY_AUDIT_EVENT_UNCHANGED;
end;
end
);
end;
function LsaxQuerySystemAudit;
var
SubCategories: TArray<TGuid>;
Buffer: PAuditPolicyInformationArray;
BufferDeallocator: IAutoReleasable;
i: Integer;
begin
// Retrieve all sub-categories
Result := LsaxCreateEmptyAudit(Entries);
if not Result.IsSuccess then
Exit;
SetLength(SubCategories, Length(Entries));
for i := 0 to High(SubCategories) do
SubCategories[i] := Entries[i].SubCategory;
// Query settings for all of them at once
Result.Location := 'AuditQuerySystemPolicy';
Result.LastCall.Expects<TAuditAccessMask>(AUDIT_QUERY_SYSTEM_POLICY);
Result.LastCall.ExpectedPrivilege := SE_SECURITY_PRIVILEGE;
Result.Win32Result := AuditQuerySystemPolicy(SubCategories,
Length(SubCategories), Buffer);
if not Result.IsSuccess then
Exit;
BufferDeallocator := LsaxDelayAuditFree(Buffer);
SetLength(Entries, Length(SubCategories));
for i := 0 to High(Entries) do
begin
Entries[i].SubCategory := SubCategories[i];
Entries[i].Policy := Buffer{$R-}[i]{$IFDEF R+}{$R+}{$ENDIF}
.AuditingInformation;
end;
end;
function LsaxSetSystemAudit;
var
Audit: TArray<TAuditPolicyInformation>;
i: Integer;
begin
SetLength(Audit, Length(Entries));
for i := 0 to High(Audit) do
begin
Audit[i].AuditSubCategoryGuid := Entries[i].SubCategory;
Audit[i].AuditingInformation := Entries[i].Policy;
// Explicitly convert unchanged to none
if Audit[i].AuditingInformation = POLICY_AUDIT_EVENT_UNCHANGED then
Audit[i].AuditingInformation := POLICY_AUDIT_EVENT_NONE;
end;
Result.Location := 'AuditSetSystemPolicy';
Result.LastCall.Expects<TAuditAccessMask>(AUDIT_SET_SYSTEM_POLICY);
Result.LastCall.ExpectedPrivilege := SE_SECURITY_PRIVILEGE;
Result.Win32Result := AuditSetSystemPolicy(Audit, Length(Audit));
end;
function LsaxQueryUserAudit;
var
SubCategories: TArray<TGuid>;
Buffer: PAuditPolicyInformationArray;
BufferDeallocator: IAutoReleasable;
i: Integer;
begin
// Retrieve all sub-categories
Result := LsaxCreateEmptyAudit(Entries);
if not Result.IsSuccess then
Exit;
SetLength(SubCategories, Length(Entries));
for i := 0 to High(SubCategories) do
SubCategories[i] := Entries[i].SubCategory;
// Query settings for all of them at once
Result.Location := 'AuditQueryPerUserPolicy';
Result.LastCall.Expects<TAuditAccessMask>(AUDIT_QUERY_USER_POLICY);
Result.LastCall.ExpectedPrivilege := SE_SECURITY_PRIVILEGE;
Result.Win32Result := AuditQueryPerUserPolicy(Sid.Data, SubCategories,
Length(SubCategories), Buffer);
if not Result.IsSuccess then
Exit;
BufferDeallocator := LsaxDelayAuditFree(Buffer);
SetLength(Entries, Length(SubCategories));
for i := 0 to High(Entries) do
begin
Entries[i].SubCategory := SubCategories[i];
Entries[i].PolicyOverride := Buffer{$R-}[i]{$IFDEF R+}{$R+}{$ENDIF}
.AuditingInformation;
end;
end;
function LsaxSetUserAudit;
var
i: Integer;
Audit: TArray<TAuditPolicyInformation>;
begin
SetLength(Audit, Length(Entries));
for i := 0 to High(Audit) do
begin
Audit[i].AuditSubcategoryGUID := Entries[i].SubCategory;
Audit[i].AuditingInformation := Entries[i].PolicyOverride;
// Although on read Unchanged means that the audit is disabled, we need to
// explicitly convert it to None on write.
if Audit[i].AuditingInformation = PER_USER_POLICY_UNCHANGED then
Audit[i].AuditingInformation := PER_USER_AUDIT_NONE;
end;
Result.Location := 'AuditSetPerUserPolicy';
Result.LastCall.Expects<TAuditAccessMask>(AUDIT_SET_USER_POLICY);
Result.LastCall.ExpectedPrivilege := SE_SECURITY_PRIVILEGE;
Result.Win32Result := AuditSetPerUserPolicy(Sid.Data, Audit, Length(Audit));
end;
function TTokenAuditPolicyHelper.GetSubCategory;
begin
// TTokenAuditPolicy stores two sub-categories in each byte
// Extract required half of the byte
if Index and 1 = 0 then
Result := PerUserPolicy{$R-}[Index shr 1]{$IFDEF R+}{$R+}{$ENDIF} and $0F
else
Result := PerUserPolicy{$R-}[Index shr 1]{$IFDEF R+}{$R+}{$ENDIF} shr 4;
end;
procedure TTokenAuditPolicyHelper.SetSubCategory;
var
PolicyByte: Byte;
begin
// PER_USER_AUDIT_NONE encodes as zero, other flags remain
Value := Value and $0F;
PolicyByte := PerUserPolicy{$R-}[Index shr 1]{$IFDEF R+}{$R+}{$ENDIF};
// Since each byte stores policies for two sub-categories, we should modify
// only one of them, preserving the other.
if Index and 1 = 0 then
PolicyByte := (PolicyByte and $F0) or Value
else
PolicyByte := (PolicyByte and $0F) or (Value shl 4);
PerUserPolicy{$R-}[Index shr 1]{$IFDEF R+}{$R+}{$ENDIF} := PolicyByte;
end;
function LsaxUserAuditToTokenAudit;
var
i: Integer;
begin
// Compute the size according to Winapi's definition
IMemory(Result) := Auto.AllocateDynamic((Length(Entries) shr 1) + 1);
for i := 0 to High(Entries) do
Result.Data.SubCategory[i] := Entries[i].PolicyOverride;
end;
function LsaxTokenAuditToUserAudit;
var
i: Integer;
begin
Result := LsaxCreateEmptyAudit(Entries);
if not Result.IsSuccess then
Exit;
for i := 0 to High(Entries) do
Entries[i].PolicyOverride := Buffer.SubCategory[i];
end;
function LsaxQueryAuditSecurity;
var
Buffer: PSecurityDescriptor;
begin
Result.Location := 'AuditQuerySecurity';
Result.LastCall.ExpectedPrivilege := SE_SECURITY_PRIVILEGE;
Result.Win32Result := AuditQuerySecurity(Info, Buffer);
if Result.IsSuccess then
IMemory(SD) := TAuditAutoMemory.Capture(Buffer,
RtlLengthSecurityDescriptor(Buffer));
end;
function LsaxSetAuditSecurity;
begin
Result.Location := 'AuditSetSecurity';
Result.LastCall.ExpectedPrivilege := SE_SECURITY_PRIVILEGE;
Result.Win32Result := AuditSetSecurity(Info, SD);
end;
function LsaxQueryGlobalSacl;
var
Buffer: PAcl;
AclInfo: TAclSizeInformation;
begin
Result.Location := 'AuditQueryGlobalSaclW';
Result.LastCall.ExpectedPrivilege := SE_SECURITY_PRIVILEGE;
Result.Win32Result := AuditQueryGlobalSaclW(PWideChar(ObjectTypeName), Buffer);
if not Result.IsSuccess then
Exit;
Result.Location := 'RtlQueryInformationAcl';
Result.LastCall.UsesInfoClass(AclSizeInformation, icQuery);
Result.Status := RtlQueryInformationAcl(Buffer, @AclInfo, SizeOf(AclInfo),
AclSizeInformation);
if Result.IsSuccess then
IMemory(Acl) := TAuditAutoMemory.Capture(Buffer, AclInfo.AclBytesTotal);
end;
function LsaxSetGlobalSacl;
begin
Result.Location := 'AuditSetGlobalSaclW';
Result.LastCall.ExpectedPrivilege := SE_SECURITY_PRIVILEGE;
Result.Win32Result := AuditSetGlobalSaclW(PWideChar(ObjectTypeName), Acl);
end;
end.