diff --git a/powershell/tests/functions/Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 b/powershell/tests/functions/Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 new file mode 100644 index 0000000000..9642f4767f --- /dev/null +++ b/powershell/tests/functions/Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 @@ -0,0 +1,117 @@ +Describe 'MT.1087 public exposed devices with a remotely exploitable CVE' { + BeforeAll { + Import-Module $PSScriptRoot/../../Maester.psd1 -Force + . "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1" + # The check is a native test, so the function is not exported. + Use-MtModuleFunction -Name Test-MtXspmPublicRemotelyExploitableHighExposureDevices + } + + BeforeEach { + $script:TestResult = $null + $script:SkippedBecause = $null + + Mock -ModuleName Maester Get-MtLicenseInformation { return $true } + Mock -ModuleName Maester Add-MtTestResultDetail { + param($Result, $SkippedBecause) + + $script:TestResult = $Result + $script:SkippedBecause = $SkippedBecause + } + } + + It 'skips instead of passing when Defender has no vulnerability data for the tenant' { + # Invoke-MtGraphSecurityQuery returns nothing both when no device matches and when the query + # could not run, so an empty result alone used to be the pass. Zero exposed devices out of + # zero assessed devices is not a pass. + Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { + if ($Query -match 'summarize AssessedDeviceCount') { + return @([pscustomobject]@{ AssessedDeviceCount = 0 }) + } + return @() + } + + $null -eq (Test-MtXspmPublicRemotelyExploitableHighExposureDevices) | Should -BeTrue + $script:SkippedBecause | Should -Be 'NotApplicable' + } + + It 'passes a clean fleet, because the denominator comes from the inventory' { + # The mock honours the semantics of each table instead of matching on the summarize clause: + # a fleet that is assessed but has nothing vulnerable has rows in the inventory and none in + # DeviceTvmSoftwareVulnerabilities. Reading the denominator from the vulnerabilities table + # returned zero here and skipped the tenant that most deserves to pass, so this case fails + # against the previous query. + Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { + # The denominator query is identified by its summarize clause first, because the main + # exposure query also names DeviceTvmSoftwareVulnerabilities. Only then does the table + # decide the answer: inventory has the assessed fleet, the vulnerabilities table has + # nothing because nothing is vulnerable. + if ($Query -match 'summarize AssessedDeviceCount') { + if ($Query -match 'DeviceTvmSoftwareInventory') { return @([pscustomobject]@{ AssessedDeviceCount = 120 }) } + return @([pscustomobject]@{ AssessedDeviceCount = 0 }) + } + return @() + } + + Test-MtXspmPublicRemotelyExploitableHighExposureDevices | Should -BeTrue + $script:TestResult | Should -Match 'Well done' + $script:SkippedBecause | Should -BeNullOrEmpty + } + + It 'passes when the fleet is assessed and no device is exposed' { + Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { + if ($Query -match 'summarize AssessedDeviceCount') { + return @([pscustomobject]@{ AssessedDeviceCount = 120 }) + } + return @() + } + + Test-MtXspmPublicRemotelyExploitableHighExposureDevices | Should -BeTrue + $script:TestResult | Should -Match 'Well done' + $script:SkippedBecause | Should -BeNullOrEmpty + } + + It 'renders no placeholder on the pass path' { + # The markdown accumulator used to be assigned only on the failure path while the -replace + # consumed it either way. + Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { + if ($Query -match 'summarize AssessedDeviceCount') { + return @([pscustomobject]@{ AssessedDeviceCount = 5 }) + } + return @() + } + + $null = Test-MtXspmPublicRemotelyExploitableHighExposureDevices + $script:TestResult | Should -Not -Match '%TestResult%' + } + + It 'fails and lists the devices when any is exposed' { + Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { + if ($Query -match 'summarize AssessedDeviceCount') { + return @([pscustomobject]@{ AssessedDeviceCount = 120 }) + } + return @([pscustomobject]@{ + DeviceName = 'web-01' + MaxEpssScore = 0.72 + MaxCvssScore = 9.8 + CveList = @('CVE-2026-0001', 'CVE-2026-0002') + PublicOpenPortList = @(443, 8080) + }) + } + + Test-MtXspmPublicRemotelyExploitableHighExposureDevices | Should -BeFalse + $script:TestResult | Should -Match 'web-01' + $script:TestResult | Should -Match 'CVE-2026-0001, CVE-2026-0002' + $script:TestResult | Should -Not -Match '%TestResult%' + } + + It 'does not ask for the denominator when devices were already found' { + # The second query is only the tie-breaker for an empty result; a populated result must not + # pay for it. + Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { + @([pscustomobject]@{ DeviceName = 'web-01'; CveList = @('CVE-2026-0003'); PublicOpenPortList = @(443) }) + } + + $null = Test-MtXspmPublicRemotelyExploitableHighExposureDevices + Should -Invoke -ModuleName Maester Invoke-MtGraphSecurityQuery -Times 1 -Exactly + } +} diff --git a/tests/maester/xspm/Test.MT.1087.ps1 b/tests/maester/xspm/Test.MT.1087.ps1 index 281b44d21e..82c3ea8286 100644 --- a/tests/maester/xspm/Test.MT.1087.ps1 +++ b/tests/maester/xspm/Test.MT.1087.ps1 @@ -128,23 +128,47 @@ $Severity = "High" - if ($return -or [string]::IsNullOrEmpty($Devices)) { + if ([string]::IsNullOrEmpty($Devices)) { + # Invoke-MtGraphSecurityQuery returns nothing both when no device matches and when the query + # could not run at all, so an empty result on its own cannot be read as a clean fleet. This + # asks whether Defender assessed anything in this tenant before calling it a pass. + # A summarize with no by clause returns exactly one row even over no input, so the count + # comes back either way. + # + # The source is the software INVENTORY, not DeviceTvmSoftwareVulnerabilities. That table holds + # vulnerabilities that were found, so an onboarded fleet with nothing vulnerable returns zero + # there and the check below would skip the one tenant that most deserves to pass. A device in + # the inventory is a device Defender Vulnerability Management assessed, whether or not it + # turned up anything. + $assessed = Invoke-MtGraphSecurityQuery -Timespan "P1D" -Query @' +DeviceTvmSoftwareInventory +| summarize AssessedDeviceCount = dcount(DeviceId) +'@ + $assessedCount = [int](($assessed | Select-Object -First 1).AssessedDeviceCount) + if ($assessedCount -le 0) { + # No device is onboarded to Defender for Endpoint, or the source could not be read. + # Zero exposed devices out of zero assessed devices is not a pass. + Add-MtTestResultDetail -SkippedBecause NotApplicable + return $null + } + $testResultMarkdown = "Well done. No public exposed devices with high or critical CVE's and high changes of exploitation were found." + $resultMarkdown = "" } else { $testResultMarkdown = "At least one public exposed device with high or critical CVE's and high changes of exploitation was found.`n`n%TestResult%" Write-Verbose "Found $($Devices.Count) public exposed devices with high or critical CVE's and high changes of exploitation." - $result = "| DeviceName | MaxEpssScore | MaxCvssScore | CveList | PublicOpenPortList | `n" - $result += "| --- | --- | --- | --- | --- |`n" + $resultMarkdown = "| DeviceName | MaxEpssScore | MaxCvssScore | CveList | PublicOpenPortList | `n" + $resultMarkdown += "| --- | --- | --- | --- | --- |`n" foreach ($Device in $Devices) { $CveList = $($Device.CveList) -join ', ' # "user1, user2, user3" $PublicOpenPortList = $($Device.PublicOpenPortList) -join ', ' # "user1, user2, user3" - $result += "| $($Device.DeviceName) | $($Device.MaxEpssScore) | $($Device.MaxCvssScore) | $($CveList) | $($PublicOpenPortList) |`n" + $resultMarkdown += "| $($Device.DeviceName) | $($Device.MaxEpssScore) | $($Device.MaxCvssScore) | $($CveList) | $($PublicOpenPortList) |`n" } } - $testResultMarkdown = $testResultMarkdown -replace "%TestResult%", $result + + $testResultMarkdown = $testResultMarkdown -replace "%TestResult%", $resultMarkdown Add-MtTestResultDetail -Result $testResultMarkdown -Severity $Severity - $result = [string]::IsNullOrEmpty($Devices) - return $result + return [string]::IsNullOrEmpty($Devices) }