forked from jajp777/powershell-scripts-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ObjectAccessAudit.ps1
239 lines (218 loc) · 8.27 KB
/
Get-ObjectAccessAudit.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
function Get-ObjectAccessAudit {
[CmdletBinding()]
param (
[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[Alias('PSComputerName','DNSHostName','CN','Hostname')]
[string[]]
$ComputerName,
[string]
$UserName,
[string]
$ObjectName,
[parameter()]
[ValidateSet(
'ACCESS_SYS_SEC','AppendData','DELETE','DeleteChild','Execute',
'READ_CONTROL','ReadAttributes','ReadData','ReadEA','SYNCHRONIZE',
'WRITE_DAC','WRITE_OWNER','WriteAttributes','WriteData','WriteEA'
)]
[string]
$AccessType
) #param
begin {
Write-Verbose -Message 'Determining filter based on parameter binding'
if ($PSBoundParameters.ContainsKey('ComputerName')) {
Write-Debug -Message '$ComputerName parameter was bound at runtime'
} elseif (-not ($PSBoundParameters.ContainsKey('ComputerName'))) {
Write-Debug -Message '$ComputerName parameter was not bound at runtime. Setting it to $env:COMPUTERNAME'
$ComputerName = $env:COMPUTERNAME
} #if $PSBoundParameters.ContainsKey('ComputerName')
if ($PSBoundParameters.ContainsKey('UserName')) {
Write-Debug -Message '$UserName parameter was bound at runtime'
$User = $true
} elseif (-not ($PSBoundParameters.ContainsKey('UserName'))) {
Write-Debug -Message '$UserName parameter was not bound at runtime'
$User = $false
} #if $PSBoundParameters.ContainsKey('UserName')
if ($PSBoundParameters.ContainsKey('ObjectName')) {
$Object = $true
} elseif (-not ($PSBoundParameters.ContainsKey('ObjectName'))) {
$Object = $false
} #if $PSBoundParameters.ContainsKey('ObjectName')
if ($PSBoundParameters.ContainsKey('AccessType')) {
Write-Debug -Message '$AccessType parameter was bound at runtime'
$Access = $true
Write-Debug -Message 'Translating AccessType into AccessMask'
$AccessMask = switch -Exact ($AccessType) {
'ReadData' {'0x1'}
'WriteData' {'0x2'}
'AppendData' {'0x4'}
'ReadEA' {'0x8'}
'WriteEA' {'0x10'}
'Execute' {'0x20'}
'DeleteChild' {'0x40'}
'ReadAttributes' {'0x80'}
'WriteAttributes' {'0x100'}
'DELETE' {'0x10000'}
'READ_CONTROL' {'0x20000'}
'WRITE_DAC' {'0x40000'}
'WRITE_OWNER' {'0x80000'}
'SYNCHRONIZE' {'0x100000'}
'ACCESS_SYS_SEC' {'0x1000000'}
} #$AccessMask
} elseif (-not ($PSBoundParameters.ContainsKey('AccessType'))) {
Write-Debug -Message '$AccessType parameter was not bound at runtime'
$Access = $false
} #if $PSBoundParameters.ContainsKey('AccessType')
if ($User -and $Object -and $Access) {
$QueryString = @"
<QueryList>
<Query Id="0" Path="Security">
<Select Path="Security">
*[System[Provider[@Name='Microsoft-Windows-Security-Auditing'] and (EventID=4663)]]
and
*[EventData[Data[@Name='SubjectUserName']='$UserName']]
and
*[EventData[Data[@Name='ObjectName']="$ObjectName"]]
and
*[EventData[Data[@Name='AccessMask']='$AccessMask']]
</Select>
</Query>
</QueryList>
"@
} elseif ($User -and $Object -and (-not $Access)) {
$QueryString = @"
<QueryList>
<Query Id="0" Path="Security">
<Select Path="Security">
*[System[Provider[@Name='Microsoft-Windows-Security-Auditing'] and (EventID=4663)]]
and
*[EventData[Data[@Name='SubjectUserName']='$UserName']]
and
*[EventData[Data[@Name='ObjectName']="$ObjectName"]]
</Select>
</Query>
</QueryList>
"@
} elseif ($User -and (-not $Object) -and $Access) {
$QueryString = @"
<QueryList>
<Query Id="0" Path="Security">
<Select Path="Security">
*[System[Provider[@Name='Microsoft-Windows-Security-Auditing'] and (EventID=4663)]]
and
*[EventData[Data[@Name='SubjectUserName']='$UserName']]
and
*[EventData[Data[@Name='AccessMask']='$AccessMask']]
</Select>
</Query>
</QueryList>
"@
} elseif ((-not $User) -and $Object -and $Access) {
$QueryString = @"
<QueryList>
<Query Id="0" Path="Security">
<Select Path="Security">
*[System[Provider[@Name='Microsoft-Windows-Security-Auditing'] and (EventID=4663)]]
and
*[EventData[Data[@Name='ObjectName']="$ObjectName"]]
and
*[EventData[Data[@Name='AccessMask']='$AccessMask']]
</Select>
</Query>
</QueryList>
"@
} elseif ($User -and (-not $Object) -and (-not $Access)) {
$QueryString = @"
<QueryList>
<Query Id="0" Path="Security">
<Select Path="Security">
*[System[Provider[@Name='Microsoft-Windows-Security-Auditing'] and (EventID=4663)]]
and
*[EventData[Data[@Name='SubjectUserName']='$UserName']]
</Select>
</Query>
</QueryList>
"@
} elseif ((-not $User) -and $Object -and (-not $Access)) {
$QueryString = @"
<QueryList>
<Query Id="0" Path="Security">
<Select Path="Security">
*[System[Provider[@Name='Microsoft-Windows-Security-Auditing'] and (EventID=4663)]]
and
*[EventData[Data[@Name='ObjectName']="$ObjectName"]]
</Select>
</Query>
</QueryList>
"@
} elseif ((-not $User) -and (-not $Object) -and $Access) {
$QueryString = @"
<QueryList>
<Query Id="0" Path="Security">
<Select Path="Security">
*[System[Provider[@Name='Microsoft-Windows-Security-Auditing'] and (EventID=4663)]]
and
*[EventData[Data[@Name='AccessMask']='$AccessMask']]
</Select>
</Query>
</QueryList>
"@
} elseif ((-not $User) -and (-not $Object) -and (-not $Access)) {
$QueryString = @"
<QueryList>
<Query Id="0" Path="Security">
<Select Path="Security">
*[System[Provider[@Name='Microsoft-Windows-Security-Auditing'] and (EventID=4663)]]
</Select>
</Query>
</QueryList>
"@
} #if ($User -and $Object -and $Access)
$QueryXml = [xml]$QueryString
} #begin
process {
$ComputerName | ForEach-Object {
$EachComputer = $_
if ($EachComputer -match $env:COMPUTERNAME) {
Get-WinEvent -FilterXml $QueryXml | ForEach-Object {
$EventXml = [xml]$_.ToXml()
$XmlData = $null
if ($XmlData = @($EventXml.Event.EventData.Data)) {
for ($i=0; $i -lt $XmlData.Count; $i++) {
$SplatArgs = @{
InputObject = $_
MemberType = "NoteProperty"
Name = "$($XmlData[$i].name)"
Value = "$($XmlData[$i].'#text')"
Force = $true
Passthru = $true
}
$_ = Add-Member @SplatArgs
} #for
} #if
$_
} #ForEach Event
} elseif ($EachComputer -notmatch $env:COMPUTERNAME) {
Get-WinEvent -ComputerName $EachComputer -FilterXml $QueryXml | ForEach-Object {
$EventXml = [xml]$_.ToXml()
$XmlData = $null
if ($XmlData = @($EventXml.Event.EventData.Data)) {
for ($i=0; $i -lt $XmlData.Count; $i++) {
$SplatArgs = @{
InputObject = $_
MemberType = "NoteProperty"
Name = "$($XmlData[$i].name)"
Value = "$($XmlData[$i].'#text')"
Force = $true
Passthru = $true
}
$_ = Add-Member @SplatArgs
} #for
} #if
$_
} #ForEach Event
} #if $EachComputer -match $env:COMPUTERNAME
} #ForEach ComputerName
} #process
end {} #end
} #function Get-ObjectAccessAudit