This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
auditpol.cs
362 lines (302 loc) · 16.1 KB
/
auditpol.cs
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
// Source: https://david-homer.blogspot.com/2016/08/document-windows-advanced-audit-policy.html
// To Compile:
// C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:exe /out:auditpol.exe auditpol.cs
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// Provides management functions of the advanced audit policy (audit policy subcategory settings).
/// </summary>
public class AdvancedAuditPolicyWrapper
{
private static void PrintUsage()
{
Console.WriteLine(@"Enumerates the audit policy configuration of the current host. Requires administrative privileges.
USAGE:
auditpol.exe [/?]");
}
public static void Main(string[] args)
{
try
{
// Parse arguments
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
switch (arg.ToUpper())
{
case "/?":
PrintUsage();
return;
}
}
string auditInfo;
Console.WriteLine("System audit policy");
Console.WriteLine("Category/Subcategory Setting");
foreach (string category_guid_str in GetCategoryIdentifiers())
{
Console.WriteLine(GetCategoryDisplayName(category_guid_str));
foreach (string subcat_guid_str in GetSubCategoryIdentifiers(category_guid_str))
{
Console.Write(" " + String.Format("{0,-40}", GetSubCategoryDisplayName(subcat_guid_str)));
try
{
auditInfo = GetSystemPolicy(subcat_guid_str).AuditingInformation.ToString();
// Post-process to make the output match the native tool
if (auditInfo == "None")
{
auditInfo = "No Auditing";
}
auditInfo = auditInfo.Replace(", ", " and ");
Console.WriteLine(auditInfo);
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message.Trim());
}
}
}
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message.Trim());
}
finally
{
Console.WriteLine("\nDONE");
}
}
/// <summary>
/// Initializes a new instance of the CENTREL.XIA.Management.AdvancedAuditPolicyWrapper class.
/// </summary>
public AdvancedAuditPolicyWrapper()
{
}
/// <summary>
/// The AuditEnumerateCategories function enumerates the available audit-policy categories.
/// </summary>
/// <param name="ppAuditCategoriesArray">A pointer to a single buffer that contains both an array of pointers to GUID structures and the structures themselves. </param>
/// <param name="pCountReturned">A pointer to the number of elements in the ppAuditCategoriesArray array.</param>
/// <returns>A System.Boolean value that indicates whether the function completed successfully.</returns>
/// <remarks>https://msdn.microsoft.com/en-us/library/windows/desktop/aa375636(v=vs.85).aspx</remarks>
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool AuditEnumerateCategories(out IntPtr ppAuditCategoriesArray, out uint pCountReturned);
/// <summary>
/// The AuditLookupCategoryName function retrieves the display name of the specified audit-policy category.
/// </summary>
/// <param name="pAuditCategoryGuid">A pointer to a GUID structure that specifies an audit-policy category.</param>
/// <param name="ppszCategoryName">The address of a pointer to a null-terminated string that contains the display name of the audit-policy category specified by the pAuditCategoryGuid function.</param>
/// <returns>A System.Boolean value that indicates whether the function completed successfully.</returns>
/// <remarks>https://msdn.microsoft.com/en-us/library/windows/desktop/aa375687(v=vs.85).aspx</remarks>
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool AuditLookupCategoryName(ref Guid pAuditCategoryGuid, out StringBuilder ppszCategoryName);
/// <summary>
/// The AuditEnumerateSubCategories function enumerates the available audit-policy subcategories.
/// </summary>
/// <param name="pAuditCategoryGuid">The GUID of an audit-policy category for which subcategories are enumerated. If the value of the bRetrieveAllSubCategories parameter is TRUE, this parameter is ignored.</param>
/// <param name="bRetrieveAllSubCategories">TRUE to enumerate all audit-policy subcategories; FALSE to enumerate only the subcategories of the audit-policy category specified by the pAuditCategoryGuid parameter.</param>
/// <param name="ppAuditSubCategoriesArray">A pointer to a single buffer that contains both an array of pointers to GUID structures and the structures themselves. The GUID structures specify the audit-policy subcategories available on the computer.</param>
/// <param name="pCountReturned">A pointer to the number of audit-policy subcategories returned in the ppAuditSubCategoriesArray array.</param>
/// <returns>A System.Boolean value that indicates whether the function completed successfully.</returns>
/// <remarks>https://msdn.microsoft.com/en-us/library/windows/desktop/aa375648(v=vs.85).aspx</remarks>
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool AuditEnumerateSubCategories(ref Guid pAuditCategoryGuid, bool bRetrieveAllSubCategories, out IntPtr ppAuditSubCategoriesArray, out uint pCountReturned);
/// <summarThe AuditLookupSubCategoryName function retrieves the display name of the specified audit-policy subcategory. y>
/// The AuditLookupSubCategoryName function retrieves the display name of the specified audit-policy subcategory.
/// </summary>
/// <param name="pAuditSubCategoryGuid">A pointer to a GUID structure that specifies an audit-policy subcategory.</param>
/// <param name="ppszSubCategoryName">The address of a pointer to a null-terminated string that contains the display name of the audit-policy subcategory specified by the pAuditSubCategoryGuid parameter.</param>
/// <returns>A System.Boolean value that indicates whether the function completed successfully.</returns>
/// <remarks>https://msdn.microsoft.com/en-us/library/windows/desktop/aa375693(v=vs.85).aspx</remarks>
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool AuditLookupSubCategoryName(ref Guid pAuditSubCategoryGuid, out StringBuilder ppszSubCategoryName);
/// <summary>
/// The AuditFree function frees the memory allocated by audit functions for the specified buffer.
/// </summary>
/// <param name="buffer">A pointer to the buffer to free.</param>
/// <remarks>https://msdn.microsoft.com/en-us/library/windows/desktop/aa375654(v=vs.85).aspx</remarks>
[DllImport("advapi32.dll")]
private static extern void AuditFree(IntPtr buffer);
/// <summary>
/// The AuditQuerySystemPolicy function retrieves system audit policy for one or more audit-policy subcategories.
/// </summary>
/// <param name="pSubCategoryGuids">A pointer to an array of GUID values that specify the subcategories for which to query audit policy. </param>
/// <param name="PolicyCount">The number of elements in each of the pSubCategoryGuids and ppAuditPolicy arrays.</param>
/// <param name="ppAuditPolicy">A pointer to a single buffer that contains both an array of pointers to AUDIT_POLICY_INFORMATION structures and the structures themselves. </param>
/// <returns>https://msdn.microsoft.com/en-us/library/windows/desktop/aa375702(v=vs.85).aspx</returns>
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool AuditQuerySystemPolicy(Guid pSubCategoryGuids, uint PolicyCount, out IntPtr ppAuditPolicy);
/// <summary>
/// Gets the GUIDs of the audit categories.
/// </summary>
/// <returns>The GUIDs of the audit categories on the local machine.</returns>
private static StringCollection GetCategoryIdentifiers()
{
StringCollection identifiers = new StringCollection();
IntPtr buffer;
uint categoryCount;
bool success = AuditEnumerateCategories(out buffer, out categoryCount);
if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); }
for (int i = 0, elemOffs = (int)buffer; i < categoryCount; i++)
{
Guid guid = (Guid)Marshal.PtrToStructure((IntPtr)elemOffs, typeof(Guid));
identifiers.Add(Convert.ToString(guid));
elemOffs += Marshal.SizeOf(typeof(Guid));
}
AuditFree(buffer);
return identifiers;
}
/// <summary>
/// Returns the display name of the audit category with the specified GUID.
/// </summary>
/// <param name="guid">The GUID of the category for which the display name should be returned.</param>
/// <returns>The display name of the category - for example "Account Management".</returns>
private static String GetCategoryDisplayName(String guid)
{
return GetCategoryDisplayName(new Guid(guid));
}
/// <summary>
/// Returns the display name of the audit category with the specified GUID.
/// </summary>
/// <param name="guid">The GUID of the category for which the display name should be returned.</param>
/// <returns>The display name of the category - for example "Account Management".</returns>
private static String GetCategoryDisplayName(Guid guid)
{
StringBuilder buffer = new StringBuilder();
bool success = AuditLookupCategoryName(ref guid, out buffer);
if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); }
if (buffer == null)
{
throw new ArgumentException(String.Format("Category Display Name Not Found for {0}", guid));
}
String categoryDisplayName = buffer.ToString();
buffer = null;
return categoryDisplayName;
}
/// <summary>
/// Gets the GUIDs of the audit subcategories of the specified category.
/// </summary>
/// <param name="guid">The GUID of the category for which the subcategories should be returned.</param>
/// <returns>The GUIDs of the audit subcategories for the specified category.</returns>
private static StringCollection GetSubCategoryIdentifiers(String categoryGuid)
{
return GetSubCategoryIdentifiers(new Guid(categoryGuid));
}
/// <summary>
/// Gets the GUIDs of the audit subcategories of the specified category.
/// </summary>
/// <param name="guid">The GUID of the category for which the subcategories should be returned.</param>
/// <returns>The GUIDs of the audit subcategories for the specified category.</returns>
private static StringCollection GetSubCategoryIdentifiers(Guid categoryGuid)
{
StringCollection identifiers = new StringCollection();
IntPtr buffer;
uint subCategoryCount;
bool success = AuditEnumerateSubCategories(ref categoryGuid, false, out buffer, out subCategoryCount);
if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); }
for (int i = 0, elemOffs = (int)buffer; i < subCategoryCount; i++)
{
Guid guid = (Guid)Marshal.PtrToStructure((IntPtr)elemOffs, typeof(Guid));
identifiers.Add(Convert.ToString(guid));
elemOffs += Marshal.SizeOf(typeof(Guid));
}
AuditFree(buffer);
return identifiers;
}
/// <summary>
/// Returns the display name of the audit subcategory with the specified GUID.
/// </summary>
/// <param name="guid">The GUID of the subcategory for which the display name should be returned.</param>
/// <returns>The display name of the subcategory - for example "Audit Credential Validation".</returns>
private static String GetSubCategoryDisplayName(String guid)
{
return GetSubCategoryDisplayName(new Guid(guid));
}
/// <summary>
/// Returns the display name of the audit subcategory with the specified GUID.
/// </summary>
/// <param name="guid">The GUID of the subcategory for which the display name should be returned.</param>
/// <returns>The display name of the subcategory - for example "Audit Credential Validation".</returns>
private static String GetSubCategoryDisplayName(Guid guid)
{
StringBuilder buffer = new StringBuilder();
bool success = AuditLookupSubCategoryName(ref guid, out buffer);
if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); }
String subCategoryDisplayName = buffer.ToString();
buffer = null;
return subCategoryDisplayName;
}
/// <summary>
/// Gets the audit policy configured for the specified subcategory GUID.
/// </summary>
/// <param name="subCategoryGuid">The GUID of the subcategory for which the policy should be returned.</param>
/// <returns>Returns an AUDIT_POLICY_INFORMATION that contains information about the policy.</returns>
private static AUDIT_POLICY_INFORMATION GetSystemPolicy(String subCategoryGuid)
{
return GetSystemPolicy(new Guid(subCategoryGuid));
}
/// <summary>
/// Gets the audit policy configured for the specified subcategory GUID.
/// </summary>
/// <param name="subCategoryGuid">The GUID of the subcategory for which the policy should be returned.</param>
/// <returns>Returns an AUDIT_POLICY_INFORMATION that contains information about the policy.</returns>
private static AUDIT_POLICY_INFORMATION GetSystemPolicy(Guid subCategoryGuid)
{
StringCollection identifiers = new StringCollection();
IntPtr buffer;
bool success = AuditQuerySystemPolicy(subCategoryGuid, 1, out buffer);
if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); }
AUDIT_POLICY_INFORMATION policyInformation = new AUDIT_POLICY_INFORMATION();
try
{
policyInformation = (AUDIT_POLICY_INFORMATION)Marshal.PtrToStructure(buffer, typeof(AUDIT_POLICY_INFORMATION));
AuditFree(buffer);
}
catch
{
throw new Exception("ERROR 5: Insufficient privileges");
}
return policyInformation;
}
}
/// <summary>
/// The AUDIT_POLICY_INFORMATION structure specifies a security event type and when to audit that type.
/// </summary>
/// <remarks>https://msdn.microsoft.com/en-us/library/windows/desktop/aa965467(v=vs.85).aspx</remarks>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct AUDIT_POLICY_INFORMATION
{
/// <summary>
/// A GUID structure that specifies an audit subcategory.
/// </summary>
public Guid AuditSubCategoryGuid;
/// <summary>
/// A set of bit flags that specify the conditions under which the security event type specified by the AuditSubCategoryGuid and AuditCategoryGuid members are audited.
/// </summary>
public AUDIT_POLICY_INFORMATION_TYPE AuditingInformation;
/// <summary>
/// A GUID structure that specifies an audit-policy category.
/// </summary>
public Guid AuditCategoryGuid;
}
/// <summary>
/// Represents the auditing type.
/// </summary>
[Flags]
public enum AUDIT_POLICY_INFORMATION_TYPE
{
/// <summary>
/// Do not audit the specified event type.
/// </summary>
None = 0,
/// <summary>
/// Audit successful occurrences of the specified event type.
/// </summary>
Success = 1,
/// <summary>
/// Audit failed attempts to cause the specified event type.
/// </summary>
Failure = 2,
}