-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-WDACEvents.psm1
324 lines (278 loc) · 13.6 KB
/
Get-WDACEvents.psm1
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
$ThisIsASignedModule = $false
if ((Split-Path (Get-Item $PSScriptRoot) -Leaf) -eq "SignedModules") {
$PSModuleRoot = Join-Path $PSScriptRoot -ChildPath "..\"
$ThisIsASignedModule = $true
} else {
$PSModuleRoot = $PSScriptRoot
}
if (Test-Path (Join-Path $PSModuleRoot -ChildPath "SignedModules\Resources\SQL-TrustDBTools.psm1")) {
Import-Module (Join-Path $PSModuleRoot -ChildPath "SignedModules\Resources\SQL-TrustDBTools.psm1")
} else {
Import-Module (Join-Path $PSModuleRoot -ChildPath "Resources\SQL-TrustDBTools.psm1")
}
if (Test-Path (Join-Path $PSModuleRoot -ChildPath "SignedModules\Resources\Copy-WDACAuditing.psm1")) {
Import-Module (Join-Path $PSModuleRoot -ChildPath "SignedModules\Resources\Copy-WDACAuditing.psm1")
} else {
Import-Module (Join-Path $PSModuleRoot -ChildPath "Resources\Copy-WDACAuditing.psm1")
}
function Get-WDACEvents {
<#
.SYNOPSIS
Uses WDACAuditing.psm1 module (h/t Matthew Graeber) to grab Code Integrity events from devices.
.DESCRIPTION
First this checks whether WDACAuditing module is installed on remote machine(s). If not, it is copied to the remote machine(s). (Copied to "C:\Program Files\WindowsPowerShell\Modules").
Then, once the module is in place, either the Get-WDACApplockerScriptMsiEvent or Get-WDACCodeIntegrityEvent functions will be used to pull events (or both).
You can pipe these results to Register-WDACEvents and Approve-WDACRules.
NOTE: If you try to grab too many events at once it will take a while to get results, especially if pulled from more than one machine
Author: Nathan Jepson
License: MIT License
.PARAMETER RemoteMachine
The remote machine(s) to grab code integrity events from. Omit this parameter to grab events from just the local machine.
.PARAMETER SkipModuleCheck
When specified, the script will not check if WDACAuditing module is located on remote machine(s).
.PARAMETER MaxEvents
Specifies the maximum number of events that Get-WDACCodeIntegrityEvent and Get-WDACApplockerScriptMsiEvent return. The default is to return all the events.
.PARAMETER PEEvents
Get non-MSI and non-Script-Based WDAC events (i.e., uses Get-WDACCodeIntegrityEvent). By default this is already enabled.
.PARAMETER MSIorScripts
Get MSI or Script-based events (i.e., uses Get-WDACApplockerScriptMsiEvent). By default this is already enabled.
.PARAMETER ShowAllowedEvents
Parameter for Get-WDACApplockerScriptMsiEvent. See WDACAuditing.psm1 for usage.
.PARAMETER SignerInformation
Parameter for both Get-WDACApplockerScriptMsiEvent and Get-WDACCodeIntegrityEvent. See WDACAuditing.psm1 for usage.
.PARAMETER SinceLastPolicyRefresh
Parameter for both Get-WDACApplockerScriptMsiEvent and Get-WDACCodeIntegrityEvent. See WDACAuditing.psm1 for usage.
.PARAMETER User
Parameter for Get-WDACCodeIntegrityEvent. See WDACAuditing.psm1 for usage.
.PARAMETER Kernel
Parameter for Get-WDACCodeIntegrityEvent. See WDACAuditing.psm1 for usage.
.PARAMETER Audit
Parameter for Get-WDACCodeIntegrityEvent. See WDACAuditing.psm1 for usage.
.PARAMETER Enforce
Parameter for Get-WDACCodeIntegrityEvent. See WDACAuditing.psm1 for usage.
.PARAMETER CheckWhqlStatus
Parameter for Get-WDACCodeIntegrityEvent. See WDACAuditing.psm1 for usage.
.PARAMETER IgnoreNativeImagesDLLs
Parameter for Get-WDACCodeIntegrityEvent. See WDACAuditing.psm1 for usage.
.PARAMETER IgnoreDenyEvents
Parameter for Get-WDACCodeIntegrityEvent. See WDACAuditing.psm1 for usage.
.PARAMETER PolicyGUID
Specify that you only want events associated with a specific WDAC Policy (by GUID)
.PARAMETER PolicyName
Specify that you only want events associated with a specific WDAC Policy (by name)
.EXAMPLE
Get-WDACEvents -MaxEvents 4 -SignerInformation -Verbose
.EXAMPLE
Get-WDACEvents -RemoteMachine PC1,PC2 -SkipModuleCheck
.EXAMPLE
Get-WDACEvents -RemoteMachine PC1 -PEEvents -MaxEvents 2 -SinceLastPolicyRefresh -CheckWhqlStatus
.EXAMPLE
Get-WDACEvents -RemoteMachine PC1,PC2 -MSIorScripts -ShowAllowedEvents -SinceLastPolicyRefresh
#>
[CmdletBinding(DefaultParameterSetName = 'Both')]
param(
[Parameter(ParameterSetName = 'Both')]
[Parameter(ParameterSetName = 'PEEvents')]
[Parameter(ParameterSetName = 'MSIorScripts')]
[ValidateNotNullOrEmpty()]
[string[]]$RemoteMachine,
[Parameter(ParameterSetName = 'Both')]
[Parameter(ParameterSetName = 'PEEvents')]
[Parameter(ParameterSetName = 'MSIorScripts')]
[switch]$SkipModuleCheck,
[Parameter(ParameterSetName = 'Both')]
[Parameter(ParameterSetName = 'PEEvents')]
[Parameter(ParameterSetName = 'MSIorScripts')]
[Int64]$MaxEvents,
[Parameter(ParameterSetName = 'PEEvents')]
[switch]$PEEvents,
[Parameter(ParameterSetName = 'MSIorScripts')]
[switch]$MSIorScripts,
[Parameter(ParameterSetName = 'Both')]
[Parameter(ParameterSetName = 'MSIorScripts')]
[switch]$ShowAllowedEvents,
[Parameter(ParameterSetName = 'Both')]
[Parameter(ParameterSetName = 'PEEvents')]
[Parameter(ParameterSetName = 'MSIorScripts')]
[switch]$SignerInformation,
[Parameter(ParameterSetName = 'Both')]
[Parameter(ParameterSetName = 'PEEvents')]
[Parameter(ParameterSetName = 'MSIorScripts')]
[switch]$SinceLastPolicyRefresh,
[Parameter(ParameterSetName = 'Both')]
[Parameter(ParameterSetName = 'PEEvents')]
[switch]$User,
[Parameter(ParameterSetName = 'Both')]
[Parameter(ParameterSetName = 'PEEvents')]
[switch]$Kernel,
[Parameter(ParameterSetName = 'Both')]
[Parameter(ParameterSetName = 'PEEvents')]
[Parameter(ParameterSetName = 'MSIorScripts')]
[switch]$Audit,
[Parameter(ParameterSetName = 'Both')]
[Parameter(ParameterSetName = 'PEEvents')]
[Parameter(ParameterSetName = 'MSIorScripts')]
[switch]$Enforce,
[Parameter(ParameterSetName = 'Both')]
[Parameter(ParameterSetName = 'PEEvents')]
[switch]$CheckWhqlStatus,
[Parameter(ParameterSetName = 'Both')]
[Parameter(ParameterSetName = 'PEEvents')]
[switch]$IgnoreNativeImagesDLLs,
[Parameter(ParameterSetName = 'Both')]
[Parameter(ParameterSetName = 'PEEvents')]
[switch]$IgnoreDenyEvents,
[Parameter(ParameterSetName = 'Both')]
[Parameter(ParameterSetName = 'PEEvents')]
[string]$PolicyGUID,
[Parameter(ParameterSetName = 'Both')]
[Parameter(ParameterSetName = 'PEEvents')]
[string]$PolicyName
)
begin {
if ($ThisIsASignedModule) {
Write-Verbose "The current file is in the SignedModules folder."
}
if ($PolicyGUID -and $PolicyName) {
throw "Cannot provide both a policy GUID and a policy name."
}
if ($PolicyName) {
if (-not (Find-WDACPolicyByName -PolicyName $PolicyName -ErrorAction Stop)) {
throw "There are no policies by this policy name: $PolicyName in the database."
}
$PolicyGUID = (Get-WDACPolicyByName -PolicyName $PolicyName -ErrorAction Stop).PolicyGUID
}
if ($PolicyGUID) {
if (-not (Find-WDACPolicy -PolicyGUID $PolicyGUID -ErrorAction Stop)) {
throw "There are no policies in the database with this GUID: $PolicyGUID"
}
}
if (($PolicyGUID -or $PolicyName) -and (($MSIorScripts) -or ((-not $MSIorScripts) -and (-not $PEEvents)))) {
Write-Warning "Filtering by policy is not available for MSI or Script events, this parameter has been ignored for these types of event logs."
}
$Signed = $false
if (Test-Path (Join-Path $PSModuleRoot -ChildPath ".\SignedModules\WDACAuditing\WDACAuditing.psm1")) {
$Signed = $true
}
if ($Signed) {
$ModulePath = ".\SignedModules\WDACAuditing\WDACAuditing.psm1"
} else {
$ModulePath = ".\WDACAuditing\WDACAuditing.psm1"
}
$PE_And_MSI = $false
if ( ($PEEvents -and $MSIorScripts) -or ( (-not $PEEvents) -and (-not $MSIorScripts))) {
$PE_And_MSI = $true
}
$PEScriptBlock = {
Param(
$MaxEvents,
$User,
$Kernel,
$Audit,
$Enforce,
$SinceLastPolicyRefresh,
$SignerInformation,
$CheckWhqlStatus,
$IgnoreNativeImagesDLLs,
$IgnoreDenyEvents,
$ImportModule,
$PolicyGUID
)
try {
if ($ImportModule) {
Import-Module -Name "WDACAuditing"
}
Get-WDACCodeIntegrityEvent -MaxEvents $MaxEvents -PolicyGUID $PolicyGUID -User:$User -Kernel:$Kernel -Audit:$Audit -Enforce:$Enforce -SinceLastPolicyRefresh:$SinceLastPolicyRefresh -SignerInformation:$SignerInformation -CheckWhqlStatus:$CheckWhqlStatus -IgnoreNativeImagesDLLs:$IgnoreNativeImagesDLLs -IgnoreDenyEvents:$IgnoreDenyEvents -ErrorAction Stop
} catch {
Write-Verbose $_
return $null
}
}
$MSIorScript_ScriptBlock = {
Param(
$MaxEvents,
$SignerInformation,
$ShowAllowedEvents,
$SinceLastPolicyRefresh,
$ImportModule,
$Audit,
$Enforce
)
try {
if ($ImportModule) {
Import-Module -Name "WDACAuditing"
}
Get-WDACApplockerScriptMsiEvent -MaxEvents $MaxEvents -SignerInformation:$SignerInformation -ShowAllowedEvents:$ShowAllowedEvents -SinceLastPolicyRefresh:$SinceLastPolicyRefresh -Audit:$Audit -Enforce:$Enforce -ErrorAction Stop
} catch {
Write-Verbose $_
return $null
}
}
$PEEventResults = @()
$MSIorScriptEventResults = @()
$ImportModule = $false
if ($RemoteMachine) {
$ImportModule = $true
}
}
process {
try {
#This clears all the "Skipped" properties on apps and msi_or_script so they are all 0 when Approve-WDACRules cmdlet is used
Clear-AllWDACSkipped -ErrorAction Stop
} catch {
Write-Verbose $_
Write-Warning "Could not clear all the `"Skipped`" properties on apps and scripts. Clear attribute manually before running Approve-WDACRules."
}
#Check whether WDACAuditing should be copied if SkipModuleCheck is not set and we are running on remote machines
if (-not $SkipModuleCheck -and $RemoteMachine) {
try {
Copy-WDACAuditing -RemoteMachine $RemoteMachine -PSModuleRoot $PSModuleRoot -ModulePath $ModulePath -ErrorAction Stop
} catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
Write-Error "PowerShell remoting is not available for those devices."
return
} catch {
Write-Verbose $_
}
}
if (-not $RemoteMachine) {
Write-Verbose "Extracting events from local machine."
if ($PE_And_MSI -or $PEEvents) {
$PEEventResults = Invoke-Command -ScriptBlock $PEScriptBlock -ArgumentList $MaxEvents,$User,$Kernel,$Audit,$Enforce,$SinceLastPolicyRefresh,$SignerInformation,$CheckWhqlStatus,$IgnoreNativeImagesDLLs,$IgnoreDenyEvents,$ImportModule,$PolicyGUID
}
if ($PE_And_MSI -or $MSIorScripts) {
$MSIorScriptEventResults = Invoke-Command -ScriptBlock $MSIorScript_ScriptBlock -ArgumentList $MaxEvents,$SignerInformation,$ShowAllowedEvents,$SinceLastPolicyRefresh,$ImportModule,$Audit,$Enforce
}
} else {
Write-Verbose "Extracting events from remote machines(s)."
$sess = New-PSSession -ComputerName $RemoteMachine -ErrorAction SilentlyContinue
if ($sess) {
if ($PE_And_MSI -or $PEEvents) {
$PEEventResults = Invoke-Command -Session $sess -ScriptBlock $PEScriptBlock -ArgumentList $MaxEvents,$User,$Kernel,$Audit,$Enforce,$SinceLastPolicyRefresh,$SignerInformation,$CheckWhqlStatus,$IgnoreNativeImagesDLLs,$IgnoreDenyEvents,$ImportModule,$PolicyGUID
}
if ($PE_And_MSI -or $MSIorScripts) {
$MSIorScriptEventResults = Invoke-Command -Session $sess -ScriptBlock $MSIorScript_ScriptBlock -ArgumentList $MaxEvents,$SignerInformation,$ShowAllowedEvents,$SinceLastPolicyRefresh,$ImportModule,$Audit,$Enforce
}
}
}
}
end {
if ($sess) {
$sess | Remove-PSSession
}
if ($PE_And_MSI) {
$result = @()
foreach ($WDACFileInfo in $PEEventResults) {
$result += $WDACFileInfo
}
foreach ($WDACFileInfo in $MSIorScriptEventResults) {
$result += $WDACFileInfo
}
return $result
} elseif ($PEEvents) {
return $PEEventResults
} elseif ($MSIorScripts) {
return $MSIorScriptEventResults
}
}
}
Export-ModuleMember -Function Get-WDACEvents