-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSet-AuditRule.ps1
326 lines (278 loc) · 14.3 KB
/
Set-AuditRule.ps1
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
function Set-AuditRule
{
<#
.SYNOPSIS
Sets an access control entry (ACE) on a system access control list (SACL) of a file, registry or ad object security descriptor.
.PARAMETER RegistryPath
Path of the registry securable object
.PARAMETER FilePath
Path of the file securable object
.PARAMETER AdObjectPath
Path of the Ad securable object
.PARAMETER WellKnownSidType
Commonly used Security Identifier. We leverage the parameter attribute called ArgumentCompleter to add tab completion values.
These values are obtained from the System.Security.Principal.WellKnownSidType enum.
Examples:
- WorldSid -> Indicates a SID that matches everyone.
- NetworkSid -> Indicates a SID for a network account. This SID is added to the process of a token when it logs on across a network.
- BuiltinAdministratorsSid -> Indicates a SID that matches the administrator account.
- AccountDomainAdminsSid -> Indicates a SID that matches the account domain administrator group.
- AccountDomainUsersSid -> Indicates a SID that matches the account domain users group.
.PARAMETER Rights
Specifies the types of access attempts to monitor. Access control rights that can be applied to a registry, file or ad objects.
These values are served dynamically from the following Enums: System.Security.AccessControl.RegistryRights, System.Security.AccessControl.FileSystemRights and System.DirectoryServices.ActiveDirectoryRights.
.PARAMETER InheritanceFlag
Inheritance flags specify the semantics of inheritance for access control entries (ACEs).
These values are served dynamically from the following Enums: System.DirectoryServices.ActiveDirectorySecurityInheritance and System.Security.AccessControl.InheritanceFlags.
.PARAMETER PropagationFlags
Specifies how Access Control Entries (ACEs) are propagated to child objects. These flags are significant only if inheritance flags are present.
These values are serverd dynamically from the following Enum: System.Security.AccessControl.PropagationFlags.
.PARAMETER AuditFlags
Specifies the conditions for auditing attempts to access a securable object. Success or Failure.
These values are served dynamically from the following Enum: System.Security.AccessControl.AuditFlags.
.NOTES
Author: Roberto Rodriguez (@Cyb3rWard0g)
License: GPL-3.0
Reference:
- @adbertram - https://www.enowsoftware.com/solutions-engine/bid/185867/Powershell-Upping-your-Parameter-Validation-Game-with-Dynamic-Parameters-Part-II
- https://social.technet.microsoft.com/Forums/ie/en-US/b012f66e-08d1-46d2-b659-6ee004e721c0/powershell-to-set-sacl-on-files?forum=ITCG
- http://giuoco.org/security/configure-file-and-registry-auditing-with-powershell/
- https://medium.com/@cryps1s/detecting-windows-endpoint-compromise-with-sacls-cd748e10950
- https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.1#argumentcompleter-attribute
- https://docs.microsoft.com/en-us/dotnet/api/system.security.principal.wellknownsidtype?view=net-5.0
- https://docs.microsoft.com/en-us/windows/win32/secauthz/sid-strings
.EXAMPLE
PS > Get-Acl -Path HKLM:\SYSTEM\CurrentControlSet\Services\Sysmondrv\Parameters\ -Audit | fl
Path : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Sysmondrv\Parameters\
Owner : BUILTIN\Administrators
Group : DESKTOP-WARDOG\None
Access : BUILTIN\Administrators Allow FullControl
..
...
Audit :
Sddl : O:BAG:...
PS > Set-AuditRule -RegistryPath HKLM:\SYSTEM\CurrentControlSet\Services\Sysmondrv\Parameters\ -WellKnownSidType WorldSid -Rights ReadKey,QueryValues -InheritanceFlags None -PropagationFlags None -AuditFlags Success
PS > Get-Acl -Path HKLM:\SYSTEM\CurrentControlSet\Services\Sysmondrv\Parameters\ -Audit | fl
Path : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Sysmondrv\Parameters\
Owner : BUILTIN\Administrators
Group : DESKTOP-WARDOG\None
Access : BUILTIN\Administrators Allow FullControl
..
...
Audit : Everyone Success ReadKey
Sddl : O:BAG:...S:AI(AU;SA;KR;;;WD)
.EXAMPLE
PS > Get-Acl -Path C:\tools\test.txt -Audit | fl
Path : Microsoft.PowerShell.Core\FileSystem::C:\tools\test.txt
Owner : RIVENDELL\cbrown
Group :
Access : BUILTIN\Administrators Allow FullControl
NT AUTHORITY\SYSTEM Allow FullControl
BUILTIN\Users Allow ReadAndExecute, Synchronize
NT AUTHORITY\Authenticated Users Allow Modify, Synchronize
Audit :
Sddl : O:S-1-5...
PS > Set-AuditRule -FilePath C:\tools\test4.txt.txt -WellKnownSidType WorldSid -Rights Read,Modify -InheritanceFlags None -PropagationFlags None -AuditFlags Success
PS > Get-Acl -Path C:\tools\test.txt -Audit | fl
Path : Microsoft.PowerShell.Core\FileSystem::C:\tools\test.txt
Owner : RIVENDELL\cbrown
Group :
Access : BUILTIN\Administrators Allow FullControl
NT AUTHORITY\SYSTEM Allow FullControl
BUILTIN\Users Allow ReadAndExecute, Synchronize
NT AUTHORITY\Authenticated Users Allow Modify, Synchronize
Audit : Everyone Success Modify
Sddl : O:S-1-5... S:AI(AU;SA;CCDCLCSWRPWPLOCRSDRC;;;WD)
.EXAMPLE
PS > Enter-PSSession MORDORDC -Credential theshire\pgustavo
[MORDORDC]: PS > Import-Module activedirectory
[MORDORDC]: PS > Get-Acl -Path 'AD:\CN=Domain Admins,CN=Users,DC=theshire,DC=local' -Audit | fl
[MORDORDC]: PS > Set-AuditRule -AdObjectPath 'AD:\CN=Domain Admins,CN=Users,DC=theshire,DC=local' -WellKnownSidType WorldSid -Rights GenericRead -InheritanceFlags None -AuditFlags Success
[MORDORDC]: PS > Get-Acl -Path 'AD:\CN=Domain Admins,CN=Users,DC=theshire,DC=local' -Audit | fl
#>
[CmdletBinding(DefaultParameterSetName='NoParam')]
param
(
[Parameter(Position=0,Mandatory=$true,ParameterSetname='RegistryAudit')]
[ValidateScript({Test-Path $_})]
[string]$RegistryPath,
[Parameter(Position=0,Mandatory=$true,ParameterSetname='FileAudit')]
[ValidateScript({Test-Path $_})]
[string]$FilePath,
[Parameter(Position=0,Mandatory=$true,ParameterSetname='AdObjectAudit')]
[string]$AdObjectPath,
[Parameter(Position=1,Mandatory=$true)]
[ArgumentCompleter( {
param (
$CommandName,
$ParameterName,
$WordToComplete,
$CommandAst,
$FakeBoundParameters
)
[System.Security.Principal.WellKnownSidType].DeclaredMembers | Where-object { $_.IsStatic } | Select-Object -ExpandProperty name | Where-object {$_ -like "$wordToComplete*"}
})]
[String]$WellKnownSidType
)
DynamicParam {
if ($PSCmdlet.ParameterSetName -eq 'AdObjectAudit')
{
$ParamOptions = @(
@{
'Name' = 'Rights';
'Mandatory' = $true;
'ValidateSetOptions' = ([System.DirectoryServices.ActiveDirectoryRights]).DeclaredMembers | Where-object { $_.IsStatic } | Select-Object -ExpandProperty name
},
@{
'Name' = 'InheritanceFlags';
'Mandatory' = $true;
'ValidateSetOptions' = ([System.DirectoryServices.ActiveDirectorySecurityInheritance]).DeclaredMembers | Where-object { $_.IsStatic } | Select-Object -ExpandProperty name
},
@{
'Name' = 'AuditFlags';
'Mandatory' = $true;
'ValidateSetOptions' = ([System.Security.AccessControl.AuditFlags]).DeclaredMembers | Where-object { $_.IsStatic } | Select-Object -ExpandProperty name
},
@{
'Name' = 'AttributeGUID';
'Mandatory' = $false;
}
)
$DomainSidArray = ("AccountAdministratorSid","AccountGuestSid","AccountKrbtgtSid","AccountDomainAdminsSid","AccountDomainUsersSid","AccountDomainGuestsSid","AccountComputersSid","AccountControllersSid","AccountCertAdminsSid","AccountSchemaAdminsSid","AccountEnterpriseAdminsSid","AccountPolicyAdminsSid","AccountRasAndIasServersSid")
if ($DomainSidArray.Contains($WellKnownSidType))
{
$DomainSidOption = @{
'Name' = 'DomainSid';
'Mandatory' = $true
}
$ParamOptions = @($DomainSidOption) + $ParamOptions
}
}
else
{
If ($PSCmdlet.ParameterSetName -eq 'RegistryAudit'){$AccessRights = [System.Security.AccessControl.RegistryRights]}
If ($PSCmdlet.ParameterSetName -eq 'FileAudit'){$AccessRights = [System.Security.AccessControl.FileSystemRights]}
$ParamOptions = @(
@{
'Name' = 'Rights';
'Mandatory' = $true;
'ValidateSetOptions' = ($AccessRights).DeclaredMembers | Where-object { $_.IsStatic } | Select-Object -ExpandProperty name
},
@{
'Name' = 'InheritanceFlags';
'Mandatory' = $true;
'ValidateSetOptions' = ([System.Security.AccessControl.InheritanceFlags]).DeclaredMembers | Where-object { $_.IsStatic } | Select-Object -ExpandProperty name
},
@{
'Name' = 'PropagationFlags';
'Mandatory' = $true;
'ValidateSetOptions' = ([System.Security.AccessControl.PropagationFlags]).DeclaredMembers | Where-object { $_.IsStatic } | Select-Object -ExpandProperty name
},
@{
'Name' = 'AuditFlags';
'Mandatory' = $true;
'ValidateSetOptions' = ([System.Security.AccessControl.AuditFlags]).DeclaredMembers | Where-object { $_.IsStatic } | Select-Object -ExpandProperty name
}
)
}
$RuntimeParamDic = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
foreach ($Param in $ParamOptions) {
$RuntimeParam = New-DynamicParam @Param
$RuntimeParamDic.Add($Param.Name, $RuntimeParam)
}
return $RuntimeParamDic
}
begin {
$PsBoundParameters.GetEnumerator() | ForEach-Object { New-Variable -Name $_.Key -Value $_.Value -ea 'SilentlyContinue'}
}
process
{
try
{
if ($DomainSid)
{
$IdentityReference = New-Object System.Security.Principal.SecurityIdentifier([System.Security.Principal.WellKnownSidType]$WellKnownSidType, [System.Security.Principal.SecurityIdentifier]$DomainSid)
}
else
{
$IdentityReference = New-Object System.Security.Principal.SecurityIdentifier([System.Security.Principal.WellKnownSidType] $WellKnownSidType,$Null)
}
if ($PSCmdlet.ParameterSetName -eq 'AdObjectAudit')
{
if ($AttributeGUID)
{
$AuditRuleObject = New-Object System.DirectoryServices.ActiveDirectoryAuditRule($IdentityReference,$Rights,$AuditFlags,[guid]$AttributeGUID, $InheritanceFlags,[guid]'00000000-0000-0000-0000-000000000000')
}
else {
$AuditRuleObject = New-Object System.DirectoryServices.ActiveDirectoryAuditRule($IdentityReference,$Rights,$AuditFlags,[guid]'00000000-0000-0000-0000-000000000000', $InheritanceFlags,[guid]'00000000-0000-0000-0000-000000000000')
}
$path = $AdObjectPath
}
else
{
If($PSCmdlet.ParameterSetName -eq 'RegistryAudit')
{
$AuditRule = "System.Security.AccessControl.RegistryAuditRule"
$Path = $RegistryPath
}
If($PSCmdlet.ParameterSetName -eq 'FileAudit')
{
$AuditRule = "System.Security.AccessControl.FileSystemAuditRule"
$Path = $FilePath
}
$AuditRuleObject = New-Object $AuditRule($IdentityReference,$Rights,$InheritanceFlags,$PropagationFlags,$AuditFlags)
}
$Acl = Get-Acl $Path -Audit
Write-Verbose "[+] Old ACL: $($Acl | Format-List | Out-String)"
Write-Verbose "[+] Adding ACE to SACL: $($AuditRuleObject | Out-String)"
$Acl.SetAuditRule($AuditRuleObject)
Set-Acl $Path $Acl
Write-Verbose "[+] New ACL: $($Acl | Format-List | Out-String)"
}
catch
{
Write-Error $_.Exception.Message
}
}
}
function New-DynamicParam {
[CmdletBinding()]
[OutputType('System.Management.Automation.RuntimeDefinedParameter')]
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Name,
[Parameter(Mandatory=$false)]
[array]$ValidateSetOptions,
[Parameter()]
[switch]$Mandatory = $false,
[Parameter()]
[switch]$ValueFromPipeline = $false,
[Parameter()]
[switch]$ValueFromPipelineByPropertyName = $false
)
$Attrib = New-Object System.Management.Automation.ParameterAttribute
$Attrib.Mandatory = $Mandatory.IsPresent
$Attrib.ValueFromPipeline = $ValueFromPipeline.IsPresent
$Attrib.ValueFromPipelineByPropertyName = $ValueFromPipelineByPropertyName.IsPresent
# Create AttributeCollection object for the attribute
$Collection = new-object System.Collections.ObjectModel.Collection[System.Attribute]
# Add our custom attribute
$Collection.Add($Attrib)
# Add Validate Set
if ($ValidateSetOptions)
{
$ValidateSet= new-object System.Management.Automation.ValidateSetAttribute($Param.ValidateSetOptions)
$Collection.Add($ValidateSet)
}
# Create Runtime Parameter
if ($Param.Name -eq 'Rights' -or $Param.Name -eq 'AuditFlags')
{
$DynParam = New-Object System.Management.Automation.RuntimeDefinedParameter($Param.Name, [array], $Collection)
}
else
{
$DynParam = New-Object System.Management.Automation.RuntimeDefinedParameter($Param.Name, [string], $Collection)
}
$DynParam
}