-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-WDACFiles.psm1
228 lines (187 loc) · 9.16 KB
/
Get-WDACFiles.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
$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\WDACFileScanner.psm1")) {
Import-Module (Join-Path $PSModuleRoot -ChildPath "SignedModules\Resources\WDACFileScanner.psm1")
} else {
Import-Module (Join-Path $PSModuleRoot -ChildPath "Resources\WDACFileScanner.psm1")
}
if (Test-Path (Join-Path $PSModuleRoot -ChildPath "SignedModules\Resources\Copy-WDACFileScanner.psm1")) {
Import-Module (Join-Path $PSModuleRoot -ChildPath "SignedModules\Resources\Copy-WDACFileScanner.psm1")
} else {
Import-Module (Join-Path $PSModuleRoot -ChildPath "Resources\Copy-WDACFileScanner.psm1")
}
function Get-WDACFiles {
<#
.SYNOPSIS
Scan files to extract WDAC info and format it the same way as Matthew Graber's WDACAuditing module
.DESCRIPTION
This module checks for the presence of the WDACFileScanner.psm1 module (located in the resources folder of WDAC-Framework), and if it is
not present, it will copy it to the PS modules folder on the remote machine.
On the remote machine, the Get-SystemDriver command is used to grab information from files (and they don't actually have to be driver files, they
can be UserMode binaries as well -- just provide the UserPEs flag).
The information is formatted just like the result of Get-WDACEvents.
If the -NoScript flag is provided, no file scanning for scripts or MSI files is performed
Author: Nathan Jepson
License: MIT License
.PARAMETER Audit
"Indicates that this cmdlet searches the Code Integrity Audit log for drivers. It does not perform a full system scan."
Source: https://learn.microsoft.com/en-us/powershell/module/configci/get-systemdriver
.PARAMETER NoScript
When specified, this cmdlet doesn't return MSIs or Scripts.
.PARAMETER MSIorScript
Specify when you only want to select MSI or script file events (the opposite of the NoScript parameter.)
.PARAMETER NoShadowCopy
"Indicates that the Volume Snapshot Service (VSS) does not make a shadow copy of the disk while the scan runs. This parameter could cause an incomplete scan for a system that is running.
If a scan fails due to VSS errors caused by low disk space on the target drive, this cmdlet prompts you to specify this parameter."
Source: https://learn.microsoft.com/en-us/powershell/module/configci/get-systemdriver
.PARAMETER OmitPaths
"Specifies an array of paths that this cmdlet omits from the scan. We recommend that you omit C:\Windows.old."
Source: https://learn.microsoft.com/en-us/powershell/module/configci/get-systemdriver
.PARAMETER PathToCatroot
"Specifies the path of the CatRoot folder. Specify this parameter to scan a remote or mounted drive."
Source: https://learn.microsoft.com/en-us/powershell/module/configci/get-systemdriver
.PARAMETER ScanPath
The directory you are scanning.
.PARAMETER ScriptFileNames
Not specified on MS documentation.
.PARAMETER UserPEs
Allow for the cmdlet to scan UserMode files (highly recommended in many scenarios)
.PARAMETER RemoteMachine
The remote machines you would like to scan the designated ScanPath on
.PARAMETER SkipModuleCheck
Skip the check for whether the WDACFileScanner.psm1 module is on the remote machine (faster)
#>
[CmdletBinding()]
param (
[switch]$Audit,
[switch]$NoScript,
[switch]$MSIorScript,
[switch]$NoShadowCopy,
[string[]]$OmitPaths,
[string]$PathToCatroot,
[string]$ScanPath,
[switch]$ScriptFileNames,
[switch]$UserPEs,
[string[]]$RemoteMachine,
[switch]$SkipModuleCheck
)
begin {
if ($ThisIsASignedModule) {
Write-Verbose "The current file is in the SignedModules folder."
}
if ($NoScript -and $MSIorScript) {
throw "Cannot provide both MSIorScript parameter and NoScript parameters (contradictory)"
}
$Signed = $false
if (Test-Path (Join-Path $PSModuleRoot -ChildPath ".\SignedModules\Resources\WDACFileScanner.psm1")) {
$Signed = $true
}
if ($Signed) {
$ModulePath = ".\SignedModules\Resources\WDACFileScanner.psm1"
} else {
$ModulePath = ".\Resources\WDACFileScanner.psm1"
}
$PEFilesResults = @()
$MSIorScriptFilesResults = @()
$GetWDACFilesScriptBlock = {
Param(
$Audit,
$NoScript,
$NoShadowCopy,
$OmitPaths,
$PathToCatroot,
$ScanPath,
$ScriptFileNames,
$UserPEs,
$IsVerbose
)
try {
Import-Module -Name "WDACFileScanner"
Get-SystemDriversModified -Audit:$Audit -NoScript:$NoScript -NoShadowCopy:$NoShadowCopy -OmitPaths $OmitPaths -PathToCatroot $PathToCatroot -ScanPath $ScanPath -ScriptFileNames:$ScriptFileNames -UserPEs:$UserPEs -Verbose:$IsVerbose -ErrorAction Stop
} catch {
if ($IsVerbose) {
Write-Warning ($_ | Format-List -Property * | Out-String)
}
return $null
}
}
}
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."
}
if ((-not $SkipModuleCheck) -and $RemoteMachine) {
try {
Copy-WDACFileScanner -RemoteMachine $RemoteMachine -PSModuleRoot $PSModuleRoot -ModulePath $ModulePath -Verbose:$VerbosePreference -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 file information for WDAC from local machine."
$PEFilesResults,$MSIorScriptFilesResults = PowerShell {
Param(
$Audit,
$NoScript,
$NoShadowCopy,
$OmitPaths,
$PathToCatroot,
$ScanPath,
$ScriptFileNames,
$UserPEs,
$ModulePath,
$PSModuleRoot,
$IsVerbose
)
try {
Import-Module (Join-Path $PSModuleRoot -ChildPath $ModulePath)
Get-SystemDriversModified -Audit:$Audit -NoScript:$NoScript -NoShadowCopy:$NoShadowCopy -OmitPaths $OmitPaths -PathToCatroot $PathToCatroot -ScanPath $ScanPath -ScriptFileNames:$ScriptFileNames -UserPEs:$UserPEs -Verbose:$IsVerbose -ErrorAction Stop
} catch {
Write-Error ($_ | Format-List -Property * | Out-String)
return $null
}
} -args $Audit.ToBool(),$NoScript.ToBool(),$NoShadowCopy.ToBool(),$OmitPaths,$PathToCatroot,$ScanPath,$ScriptFileNames.ToBool(),$UserPEs.ToBool(),$ModulePath,$PSModuleRoot,$VerbosePreference
} else {
Write-Verbose "Extracting file information for WDAC from remote machines(s)."
$sess = New-PSSession -ComputerName $RemoteMachine -ErrorAction SilentlyContinue
$PEFilesResults,$MSIorScriptFilesResults = Invoke-Command -Session $sess -ScriptBlock $GetWDACFilesScriptBlock -ArgumentList $Audit,$NoScript,$NoShadowCopy,$OmitPaths,$PathToCatroot,$ScanPath,$ScriptFileNames,$UserPEs,$VerbosePreference
}
}
end {
if ($sess) {
$sess | Remove-PSSession
}
if ($NoScript) {
return $PEFilesResults
} elseif ($MSIorScript) {
return $MSIorScriptFilesResults
} else {
$result = @()
foreach ($WDACFileInfo in $PEFilesResults) {
$result += $WDACFileInfo
}
foreach ($WDACFileInfo in $MSIorScriptFilesResults) {
$result += $WDACFileInfo
}
return $result
}
}
}
Export-ModuleMember -Function Get-WDACFiles