-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathTest-Assessment.41055.ps1
More file actions
274 lines (244 loc) · 11.6 KB
/
Copy pathTest-Assessment.41055.ps1
File metadata and controls
274 lines (244 loc) · 11.6 KB
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
<#
.SYNOPSIS
Real-time, behavioral, and heuristic protection are enabled on Microsoft Defender Antivirus.
.DESCRIPTION
Evaluates the pinned Microsoft Defender Antivirus Secure Score controls by joining control
profiles to the latest Microsoft Secure Score snapshot and comparing each achieved score with
its maximum score. Controls with missing profiles, controls without applicable devices, and
ignored controls are excluded from the roll-up.
.NOTES
Test ID: 41055
Workshop Task ID: SECOPS-055
Category: Endpoint threat protection
Pillar: SecOps
Required Module: Microsoft.Graph.Authentication
Required Connection: Microsoft Graph
#>
function Test-Assessment-41055 {
[ZtTest(
Category = 'Endpoint threat protection',
CompatibleLicense = ('WINDEFATP', 'MDE_LITE'),
ImplementationCost = 'Low',
Pillar = 'SecOps',
RiskLevel = 'High',
Service = ('Graph'),
SfiPillar = 'Monitor and detect cyberthreats',
TenantType = ('Workforce'),
TestId = 41055,
Title = 'Real-time, behavioral, and heuristic protection are enabled on Microsoft Defender Antivirus',
UserImpact = 'Low'
)]
[CmdletBinding()]
param()
#region Data Collection
Write-PSFMessage '🟦 Start' -Tag Test -Level VeryVerbose
$activity = 'Checking Microsoft Defender Antivirus protection controls in Microsoft Secure Score'
$defenderAntivirusControlIds = @(
'scid_2012', 'scid_91', 'scid_92', 'scid_89', 'scid_90', 'scid_5093', 'scid_6093'
)
$controlProfileError = $null
$secureScoreError = $null
# Q1: Read MDATP Secure Score control profiles, then intersect client-side with pinned IDs.
Write-ZtProgress -Activity $activity -Status 'Getting MDATP Secure Score control profiles'
$mdatpControlProfiles = @()
try {
$controlProfileFilter = "service eq 'MDATP' and (id eq 'scid_2012' or id eq 'scid_91' or id eq 'scid_92' or id eq 'scid_89' or id eq 'scid_90' or id eq 'scid_5093' or id eq 'scid_6093')"
$mdatpControlProfiles = @(Invoke-ZtGraphRequest -RelativeUri 'security/secureScoreControlProfiles' -Filter $controlProfileFilter -ApiVersion beta -ErrorAction Stop)
}
catch {
$controlProfileError = $_
Write-PSFMessage "Failed to retrieve MDATP Secure Score control profiles: $_" -Tag Test -Level Warning
}
# Q2: Read the latest Secure Score snapshot; -DisablePaging returns the wrapper object.
Write-ZtProgress -Activity $activity -Status 'Getting latest Secure Score snapshot'
$latestSecureScore = $null
try {
$secureScoresResponse = Invoke-ZtGraphRequest -RelativeUri 'security/secureScores' -Top 1 -ApiVersion beta -DisablePaging -ErrorAction Stop
$secureScores = @($secureScoresResponse.value)
if ($secureScores.Count -gt 0) {
$latestSecureScore = $secureScores[0]
}
}
catch {
$secureScoreError = $_
Write-PSFMessage "Failed to retrieve latest Secure Score snapshot: $_" -Tag Test -Level Warning
}
#endregion Data Collection
#region Assessment Logic
$passed = $false
$customStatus = $null
foreach ($queryError in @($controlProfileError, $secureScoreError) | Where-Object { $null -ne $_ }) {
if ((Get-ZtHttpStatusCode -ErrorRecord $queryError) -in (401, 403)) {
$params = @{
TestId = '41055'
Title = 'Real-time, behavioral, and heuristic protection are enabled on Microsoft Defender Antivirus'
Status = $false
Result = '⚠️ Microsoft Graph returned HTTP 401 or 403. Verify SecurityEvents.Read.All is granted, Secure Score data is flowing, and at least one MDE device is onboarded.'
CustomStatus = 'Investigate'
}
Add-ZtTestResultDetail @params
return
}
}
$controlProfileById = @{}
foreach ($controlProfile in @($mdatpControlProfiles | Where-Object { $defenderAntivirusControlIds -contains $_.id })) {
if ($null -ne $controlProfile.id -and -not $controlProfileById.ContainsKey($controlProfile.id)) {
$controlProfileById[$controlProfile.id] = $controlProfile
}
}
if ($controlProfileError -or $secureScoreError -or $null -eq $latestSecureScore) {
$params = @{
TestId = '41055'
Title = 'Real-time, behavioral, and heuristic protection are enabled on Microsoft Defender Antivirus'
Status = $false
Result = '⚠️ No Microsoft Defender Antivirus Secure Score control could be evaluated; verify SecurityEvents.Read.All is granted, Secure Score data is flowing, and at least one MDE device is onboarded.'
CustomStatus = 'Investigate'
}
Add-ZtTestResultDetail @params
return
}
$controlScoreByName = @{}
foreach ($controlScore in @($latestSecureScore.controlScores)) {
if ($null -ne $controlScore.controlName -and -not $controlScoreByName.ContainsKey($controlScore.controlName)) {
$controlScoreByName[$controlScore.controlName] = $controlScore
}
}
$evaluationResults = @()
foreach ($controlId in $defenderAntivirusControlIds) {
$controlProfile = if ($controlProfileById.ContainsKey($controlId)) { $controlProfileById[$controlId] } else { $null }
$matchingScore = if ($controlScoreByName.ContainsKey($controlId)) { $controlScoreByName[$controlId] } else { $null }
$latestStateUpdate = @()
if ($null -ne $controlProfile) {
$latestStateUpdate = @($controlProfile.controlStateUpdates | Sort-Object { if ($_.updatedDateTime) { [datetime]$_.updatedDateTime } else { [datetime]::MinValue } } -Descending | Select-Object -First 1)
}
$controlState = if ($latestStateUpdate.Count -gt 0 -and -not [string]::IsNullOrWhiteSpace($latestStateUpdate[0].state)) { $latestStateUpdate[0].state } else { 'N/A' }
$isIgnored = $latestStateUpdate.Count -gt 0 -and $latestStateUpdate[0].state -eq 'ignored'
$score = if ($null -ne $matchingScore -and $null -ne $matchingScore.score) { $matchingScore.score } else { $null }
$maxScore = if ($null -ne $controlProfile -and $null -ne $controlProfile.maxScore) { $controlProfile.maxScore } else { $null }
$scoreValue = $null
$scoreIsNumeric = $false
if ($null -ne $score) {
try {
$scoreValue = [double]$score
$scoreIsNumeric = $true
}
catch { }
}
$maxScoreValue = $null
$maxScoreIsNumeric = $false
if ($null -ne $maxScore) {
try {
$maxScoreValue = [double]$maxScore
$maxScoreIsNumeric = $true
}
catch { }
}
$statusReason = $null
$status = if ($null -eq $controlProfile) {
$statusReason = 'profile not found'
'N/A'
}
elseif ($null -eq $matchingScore) {
$statusReason = 'no applicable devices'
'N/A'
}
elseif ($isIgnored) {
'Skipped'
}
elseif (-not $scoreIsNumeric -or -not $maxScoreIsNumeric) {
'Investigate'
}
elseif ($scoreValue -ge $maxScoreValue) {
'Pass'
}
else {
'Fail'
}
$controlTitle = if ($null -ne $controlProfile -and -not [string]::IsNullOrWhiteSpace($controlProfile.title)) {
$controlProfile.title
}
else {
$controlId
}
$evaluationResults += [PSCustomObject]@{
ControlId = $controlId
ControlTitle = $controlTitle
ActionUrl = if ($null -ne $controlProfile) { $controlProfile.actionUrl } else { $null }
Score = if ($null -ne $score) { $score } else { 'N/A' }
MaxScore = if ($null -ne $maxScore) { $maxScore } else { 'N/A' }
ImplementationStatus = if ($null -ne $matchingScore -and -not [string]::IsNullOrWhiteSpace($matchingScore.implementationStatus)) { $matchingScore.implementationStatus } else { 'N/A' }
State = $controlState
LastModifiedDateTime = if ($null -ne $controlProfile) { $controlProfile.lastModifiedDateTime } else { $null }
Status = $status
StatusReason = $statusReason
}
}
$failedItems = @($evaluationResults | Where-Object Status -eq 'Fail')
$investigateItems = @($evaluationResults | Where-Object Status -eq 'Investigate')
$passedItems = @($evaluationResults | Where-Object Status -eq 'Pass')
if ($failedItems.Count -gt 0) {
$testResultMarkdown = "❌ One or more of real-time, behavioral, or heuristic scanning is disabled.`n`n%TestResult%"
}
elseif ($investigateItems.Count -gt 0) {
$customStatus = 'Investigate'
$testResultMarkdown = "⚠️ One or more Microsoft Defender Antivirus Secure Score controls could not be evaluated.`n`n%TestResult%"
}
elseif ($passedItems.Count -gt 0) {
$passed = $true
$testResultMarkdown = "✅ Real-time, behavioral, and heuristic protection on Microsoft Defender Antivirus are enabled.`n`n%TestResult%"
}
else {
$customStatus = 'Investigate'
$testResultMarkdown = "⚠️ No Microsoft Defender Antivirus Secure Score control could be evaluated; verify SecurityEvents.Read.All is granted, Secure Score data is flowing, and at least one MDE device is onboarded.`n`n%TestResult%"
}
#endregion Assessment Logic
#region Report Generation
$totalCount = $evaluationResults.Count
$countLine = "Total pinned Microsoft Defender Antivirus controls: $totalCount"
$secureScoreLink = '[Microsoft Secure Score](https://security.microsoft.com/securescore)'
$tableRows = ''
foreach ($result in $evaluationResults) {
$statusDisplay = switch ($result.Status) {
'Pass' { '✅ Pass' }
'Fail' { '❌ Fail' }
'Investigate' { '⚠️ Investigate' }
'N/A' { "N/A ($($result.StatusReason))" }
default { 'Skipped' }
}
$lastModified = if ($result.LastModifiedDateTime) { Get-FormattedDate -DateString $result.LastModifiedDateTime } else { 'N/A' }
$safeControlTitle = Get-SafeMarkdown -Text $result.ControlTitle
$controlDisplay = if (-not [string]::IsNullOrWhiteSpace($result.ActionUrl)) {
"[$safeControlTitle]($($result.ActionUrl)) ($($result.ControlId))"
}
elseif ($result.ControlTitle -ne $result.ControlId) {
"$safeControlTitle ($($result.ControlId))"
}
else {
$safeControlTitle
}
$tableRows += "| $controlDisplay | $($result.Score) | $($result.MaxScore) | $($result.ImplementationStatus) | $($result.State) | $lastModified | $statusDisplay |`n"
}
$controlTable = @"
| Control title (id) | Score | Max score | Implementation status | State | Last modified | Status |
| :----------------- | ----: | --------: | :-------------------- | :---- | :------------ | :----- |
$tableRows
"@
$formatTemplate = @'
{0}
{1}
{2}
'@
$mdInfo = $formatTemplate -f $countLine, $secureScoreLink, $controlTable
$testResultMarkdown = $testResultMarkdown -replace '%TestResult%', $mdInfo
#endregion Report Generation
$params = @{
TestId = '41055'
Title = 'Real-time, behavioral, and heuristic protection are enabled on Microsoft Defender Antivirus'
Status = $passed
Result = $testResultMarkdown
}
if ($customStatus) {
$params.CustomStatus = $customStatus
}
Add-ZtTestResultDetail @params
}