-
Notifications
You must be signed in to change notification settings - Fork 15
/
Invoke-DumpMDEConfig.ps1
347 lines (306 loc) · 16.1 KB
/
Invoke-DumpMDEConfig.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
function Invoke-DumpMDEConfig {
param (
[switch]$TableOutput,
[string]$TableOutputFile = "MDEConfig.txt",
[switch]$CSVOutput
)
Write-Host "[+] Dumping Defender Excluded Paths"
Query-ExclusionPaths -TableOutput:$TableOutput -TableOutputFile:$TableOutputFile -CSVOutput:$CSVOutput
Write-Host "[+] Dumping Enabled ASR Rules"
Query-RegASRRules -TableOutput:$TableOutput -TableOutputFile:$TableOutputFile -CSVOutput:$CSVOutput
Write-Host "[+] Dumping Allowed Threats"
Query-AllowedThreats -TableOutput:$TableOutput -TableOutputFile:$TableOutputFile -CSVOutput:$CSVOutput
Write-Host "[+] Dumping Defender Protection History"
Query-ProtectionHistory -TableOutput:$TableOutput -TableOutputFile:$TableOutputFile -CSVOutput:$CSVOutput
Write-Host "[+] Dumping Exploit Guard Protection History"
Query-ExploitGuardProtectionHistory -TableOutput:$TableOutput -TableOutputFile:$TableOutputFile -CSVOutput:$CSVOutput
Write-Host "[+] Dumping Windows Firewall Exclusions"
Query-FirewallExclusions -TableOutput:$TableOutput -TableOutputFile:$TableOutputFile -CSVOutput:$CSVOutput
if ($TableOutput) {
Write-Host "[+] Defender Config Dumped to $TableOutputFile"
}
}
function Get-ASRRuleDescriptions {
return @{
"56a863a9-875e-4185-98a7-b882c64b5ce5" = "Block Exploit of Vulnerable Signed Drivers"
"7674ba52-37eb-4a4f-a9a1-f0f9a1619a2c" = "Prevent Adobe Reader from creating child processes"
"d4f940ab-401b-4efc-aadc-ad5f3c50688a" = "Prevent all Office applications from creating child processes"
"9e6c4e1f-7d60-472f-ba1a-a39ef669e4b2" = "Block stealing credentials from the Windows Local Security Authority (lsass.exe) Subsystem"
"be9ba2d9-53ea-4cdc-84e5-9b1eeee46550" = "Block executable content from email client and webmail"
"01443614-cd74-433a-b99e-2ecdc07bfc25" = "Block executable files unless they meet a prevalence, age, or trusted list criterion"
"5beb7efe-fd9a-4556-801d-275e5ffc04cc" = "Block execution of potentially hidden scripts"
"d3e037e1-3eb8-44c8-a917-57927947596d" = "Block JavaScript or VBScript from launching downloaded executable content"
"3b576869-a4ec-4529-8536-b80a7769e899" = "Block Office applications from creating executable content"
"75668c1f-73b5-4cf0-bb93-3ecf5cb7cc84" = "Prevent Office applications from injecting code into other processes"
"26190899-1602-49e8-8b27-eb1d0a1ce869" = "Block Office Communication Application from Creating Child Processes"
"e6db77e5-3df2-4cf1-b95a-636979351e5b" = "Block persistence via WMI event subscription"
"d1e49aac-8f56-4280-b9ba-993a6d77406c" = "Block Process Creations from PSExec and WMI Commands"
"33ddedf1-c6e0-47cb-833e-de6133960387" = "Block computer restarting in safe mode (preview)"
"b2b3f03d-6a65-4f7b-a9c7-1c7ef74a9ba4" = "Block untrusted and unsigned processes running from USB"
"c0033c00-d16d-4114-a5a0-dc9b3a7d2ceb" = "Block the use of copied or imitated system utilities (preview)"
"a8f5898e-1dc8-49a9-9878-85004b8a61e6" = "Block the creation of web shells for servers"
"92e97fa1-2edf-4476-bdd6-9dd0b4dddc7b" = "Block Win32 API Calls from Office Macros"
"c1db55ab-c21a-4637-bb3f-a12568109d35" = "How to use advanced ransomware protection"
}
}
function Query-ExclusionPaths {
param (
[switch]$TableOutput,
[string]$TableOutputFile,
[switch]$CSVOutput
)
try {
$logName = "Microsoft-Windows-Windows Defender/Operational"
$query = "*[System[Provider[@Name='Microsoft-Windows-Windows Defender'] and (EventID=5007)]]"
$events = Get-WinEvent -LogName $logName -FilterXPath $query
$exclusionPaths = foreach ($event in $events) {
$message = $event.Message
if ($message -match "HKLM\\SOFTWARE\\Microsoft\\Windows Defender\\Exclusions\\Paths\\([^\s]+)") {
[PSCustomObject]@{
Path = $matches[1]
TimeCreated = $event.TimeCreated
}
}
}
if ($TableOutput) {
$subject = "[+] Exclusion Paths:"
$subject | Out-File $TableOutputFile -Append
$exclusionPaths | Format-Table -AutoSize | Out-File $TableOutputFile -Append
} elseif ($CSVOutput) {
Write-Host "[+] Dumped Exclusion Paths to ExclusionPaths.csv"
$exclusionPaths | Export-Csv -Path "ExclusionPaths.csv" -NoTypeInformation
} else {
foreach ($path in $exclusionPaths) {
$path | Format-List
}
}
} catch {
Write-Error "Failed to query exclusion paths: $_"
}
}
function Query-RegASRRules {
param (
[switch]$TableOutput,
[string]$TableOutputFile,
[switch]$CSVOutput
)
try {
$logName = "Microsoft-Windows-Windows Defender/Operational"
$query = "*[System[Provider[@Name='Microsoft-Windows-Windows Defender'] and (EventID=5007)]]"
$events = Get-WinEvent -LogName $logName -FilterXPath $query
$asrDescriptions = Get-ASRRuleDescriptions
$asrRules = foreach ($event in $events) {
$message = $event.Message
if ($message -match "HKLM\\SOFTWARE\\Microsoft\\Windows Defender\\Windows Defender Exploit Guard\\ASR\\Rules\\([0-9A-Fa-f-]+)") {
$asrRuleId = $matches[1].ToLower()
$description = if ($asrDescriptions.ContainsKey($asrRuleId)) { $asrDescriptions[$asrRuleId] } else { "Unknown ASR Rule" }
[PSCustomObject]@{
RuleId = $asrRuleId
Description = $description
TimeCreated = $event.TimeCreated
}
}
}
if ($TableOutput) {
$subject = "[+] Enabled ASR Rules:"
$subject | Out-File $TableOutputFile -Append
$asrRules | Format-Table -AutoSize | Out-String -Width 4096 | Out-File $TableOutputFile -Append
} elseif ($CSVOutput) {
Write-Host "[+] Dumped Enabled ASR Rules to ASRRules.csv"
$asrRules | Export-Csv -Path "ASRRules.csv" -NoTypeInformation
} else {
foreach ($rule in $asrRules) {
$rule | Format-List
}
}
} catch {
Write-Error "Failed to query ASR rules: $_"
}
}
function Query-AllowedThreats {
param (
[switch]$TableOutput,
[string]$TableOutputFile,
[switch]$CSVOutput
)
try {
$logName = "Microsoft-Windows-Windows Defender/Operational"
$query = "*[System[(EventID=1117 or EventID=5007)]]"
$events = Get-WinEvent -LogName $logName -FilterXPath $query
$threatDetails = @{}
$allowedThreats = @()
foreach ($event in $events) {
$message = $event.ToXml()
$eventId = $event.Id
if ($eventId -eq 1117) {
$threatId = Select-String -InputObject $message -Pattern 'threatid=(.+?)&' | ForEach-Object { $_.Matches.Groups[1].Value }
$toolName = Select-String -InputObject $message -Pattern "<Data Name='Threat Name'>(.+?)</Data>" | ForEach-Object { $_.Matches.Groups[1].Value }
$path = Select-String -InputObject $message -Pattern "<Data Name='Path'>(.+?)</Data>" | ForEach-Object { $_.Matches.Groups[1].Value }
if ($threatId) {
$threatDetails[$threatId] = @{
ToolName = $toolName
Path = $path
}
}
} elseif ($eventId -eq 5007) {
$newValue = Select-String -InputObject $message -Pattern "<Data Name='New Value'>(.+?)</Data>" | ForEach-Object { $_.Matches.Groups[1].Value }
if ($newValue -and $newValue -match "HKLM\\SOFTWARE\\Microsoft\\Windows Defender\\Threats\\ThreatIDDefaultAction" -and $newValue.EndsWith("= 0x6")) {
$threatId = Select-String -InputObject $newValue -Pattern "ThreatIDDefaultAction\\(.+?) " | ForEach-Object { $_.Matches.Groups[1].Value }
if ($threatDetails[$threatId]) {
$timeCreated = Select-String -InputObject $message -Pattern "<TimeCreated SystemTime='(.+?)'" | ForEach-Object { $_.Matches.Groups[1].Value }
$timeCreatedUtc = [datetime]::ParseExact($timeCreated, "yyyy-MM-ddTHH:mm:ss.fffffffK", $null, [System.Globalization.DateTimeStyles]::AssumeUniversal).ToUniversalTime()
$allowedThreats += [PSCustomObject]@{
ThreatID = $threatId
ToolName = $threatDetails[$threatId].ToolName
Path = $threatDetails[$threatId].Path
TimeCreated = $timeCreatedUtc
}
}
}
}
}
if ($TableOutput) {
$subject = "[+] Allowed Threats:"
$subject | Out-File $TableOutputFile -Append
$allowedThreats | Format-Table -AutoSize | Out-File $TableOutputFile -Append
} elseif ($CSVOutput) {
Write-Host "[+] Dumped Allowed Threats to AllowedThreats.csv"
$allowedThreats | Export-Csv -Path "AllowedThreats.csv" -NoTypeInformation
} else {
foreach ($threat in $allowedThreats) {
$threat | Format-List
}
}
} catch {
Write-Error "Failed to query allowed threats: $_"
}
}
function Query-ProtectionHistory {
param (
[switch]$TableOutput,
[string]$TableOutputFile,
[switch]$CSVOutput
)
try {
$logName = "Microsoft-Windows-Windows Defender/Operational"
$query = "*[System[(EventID=1117)]]"
$events = Get-WinEvent -LogName $logName -FilterXPath $query
$protectionHistory = foreach ($event in $events) {
$message = $event.ToXml()
$eventId = $event.Id
if ($eventId -eq 1117) {
$threatName = Select-String -InputObject $message -Pattern "<Data Name='Threat Name'>(.+?)</Data>" | ForEach-Object { $_.Matches.Groups[1].Value }
$severityName = Select-String -InputObject $message -Pattern "<Data Name='Severity Name'>(.+?)</Data>" | ForEach-Object { $_.Matches.Groups[1].Value }
$categoryName = Select-String -InputObject $message -Pattern "<Data Name='Category Name'>(.+?)</Data>" | ForEach-Object { $_.Matches.Groups[1].Value }
$path = Select-String -InputObject $message -Pattern "<Data Name='Path'>(.+?)</Data>" | ForEach-Object { $_.Matches.Groups[1].Value }
$actionName = Select-String -InputObject $message -Pattern "<Data Name='Action Name'>(.+?)</Data>" | ForEach-Object { $_.Matches.Groups[1].Value }
$timeCreated = Select-String -InputObject $message -Pattern "<TimeCreated SystemTime='(.+?)'" | ForEach-Object { $_.Matches.Groups[1].Value }
$timeCreatedUtc = [datetime]::ParseExact($timeCreated, "yyyy-MM-ddTHH:mm:ss.fffffffK", $null, [System.Globalization.DateTimeStyles]::AssumeUniversal).ToUniversalTime()
[PSCustomObject]@{
ThreatName = $threatName
Severity = $severityName
Category = $categoryName
Path = $path
ActionTaken = $actionName
TimeCreated = $timeCreatedUtc
}
}
}
if ($TableOutput) {
$subject = "[+] Protection History:"
$subject | Out-File $TableOutputFile -Append
$protectionHistory | Format-Table -AutoSize | Out-String -Width 4096 | Out-File $TableOutputFile -Append
} elseif ($CSVOutput) {
Write-Host "[+] Dumped Protection History to ProtectionHistory.csv"
$protectionHistory | Export-Csv -Path "ProtectionHistory.csv" -NoTypeInformation
} else {
foreach ($history in $protectionHistory) {
$history | Format-List
}
}
} catch {
Write-Error "Failed to query protection history: $_"
}
}
function Query-ExploitGuardProtectionHistory {
param (
[switch]$TableOutput,
[string]$TableOutputFile,
[switch]$CSVOutput
)
try {
$logName = "Microsoft-Windows-Windows Defender/Operational"
$query = "*[System[(EventID=1121)]]"
$events = Get-WinEvent -LogName $logName -FilterXPath $query -ErrorAction SilentlyContinue
$asrDescriptions = Get-ASRRuleDescriptions
$exploitGuardHistory = foreach ($event in $events) {
$message = $event.Message
$asrRuleId = if ($message -match "ID: ([0-9A-Fa-f-]+)") { $matches[1] } else { $null }
$detectionTime = if ($message -match "Detection time: (.+?)\s") { [datetime]$matches[1] } else { $null }
$user = if ($message -match "User: (.+?)\s") { $matches[1] } else { $null }
$path = if ($message -match "Path: (.+?)\s") { $matches[1] } else { $null }
$processName = if ($message -match "Process Name: (.+?)\s") { $matches[1] } else { $null }
$targetCommandline = if ($message -match "Target Commandline: (.+?)\s") { $matches[1] } else { $null }
$description = if ($asrRuleId -and $asrDescriptions.ContainsKey($asrRuleId)) { $asrDescriptions[$asrRuleId] } else { "Unknown ASR Rule" }
[PSCustomObject]@{
RuleId = $asrRuleId
Description = $description
DetectionTime = $detectionTime
User = $user
Path = $path
ProcessName = $processName
TargetCommandline = $targetCommandline
}
}
if ($TableOutput) {
$subject = "[+] Exploit Guard Protection History:"
$subject | Out-File $TableOutputFile -Append
$exploitGuardHistory | Format-Table -AutoSize | Out-String -Width 4096 | Out-File $TableOutputFile -Append
} elseif ($CSVOutput) {
Write-Host "[+] Dumped Exploit Guard Protection History to ExploitGuardProtectionHistory.csv"
$exploitGuardHistory | Export-Csv -Path "ExploitGuardProtectionHistory.csv" -NoTypeInformation
} else {
foreach ($history in $exploitGuardHistory) {
$history | Format-List
}
}
} catch {
Write-Error "Failed to query exploit guard protection history: $_"
}
}
function Query-FirewallExclusions {
param (
[switch]$TableOutput,
[string]$TableOutputFile = "FirewallExclusions.txt",
[switch]$CSVOutput
)
$logName = "Microsoft-Windows-Windows Firewall With Advanced Security/Firewall"
$query = "*[System[(EventID=2099 or EventID=2097)]]"
$events = Get-WinEvent -LogName $logName -FilterXPath $query
$firewallExclusions = foreach ($event in $events) {
$message = $event.Message
$action = [regex]::Match($message, 'Action:\s*(.+)').Groups[1].Value.Trim()
if ($action -eq "Allow") {
[PSCustomObject]@{
RuleID = [regex]::Match($message, 'Rule ID:\s*(.+)').Groups[1].Value.Trim()
RuleName = [regex]::Match($message, 'Rule Name:\s*(.+)').Groups[1].Value.Trim()
ApplicationPath = [regex]::Match($message, 'Application Path:\s*(.+)').Groups[1].Value.Trim()
Direction = [regex]::Match($message, 'Direction:\s*(.+)').Groups[1].Value.Trim()
Action = $action
TimeCreated = $event.TimeCreated
}
}
}
if ($TableOutput) {
$subject = "[+] Firewall Exclusions:"
$subject | Out-File $TableOutputFile -Append
$firewallExclusions | Format-Table -AutoSize | Out-String -Width 4096 | Out-File $TableOutputFile -Append
} elseif ($CSVOutput) {
Write-Host "[+] Dumped Firewall Exclusions to FirewallExclusions.csv"
$firewallExclusions | Export-Csv -Path "FirewallExclusions.csv" -NoTypeInformation
} else {
foreach ($exclusion in $firewallExclusions) {
$exclusion | Format-List
}
}
}