Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
Describe 'Test-MtCisTrustedNamedLocation' {
BeforeAll {
Import-Module "$PSScriptRoot\..\..\Maester.psd1" -Force
. "$PSScriptRoot\..\helpers\Use-MtModuleFunction.ps1"
# Check functions that became native tests are not exported.
Use-MtModuleFunction -Name Test-MtCisTrustedNamedLocation

function Get-TestIpLocation {
param(
[string] $DisplayName,
[string[]] $CidrAddress,
[switch] $Trusted
)
[pscustomobject]@{
'@odata.type' = '#microsoft.graph.ipNamedLocation'
displayName = $DisplayName
isTrusted = [bool]$Trusted
ipRanges = @(
foreach ($cidr in $CidrAddress) {
[pscustomobject]@{
'@odata.type' = if ($cidr -like '*:*') { '#microsoft.graph.iPv6CidrRange' } else { '#microsoft.graph.iPv4CidrRange' }
cidrAddress = $cidr
}
}
)
}
}
}

BeforeEach {
Mock Add-MtTestResultDetail -ModuleName Maester { }
}

It 'Passes for a trusted location with a public range, without a note' {
$locations = @(Get-TestIpLocation -DisplayName 'Head office' -CidrAddress '8.8.8.0/24' -Trusted)
Mock Invoke-MtGraphRequest -ModuleName Maester { $locations }

Test-MtCisTrustedNamedLocation | Should -BeTrue
Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter {
$Result -like 'Well done.*' -and $Result -notlike '*never match a sign-in*'
}
}

It 'Passes for a trusted location whose ranges are all <Kind>, and lists it' -ForEach @(
@{ Kind = 'private (RFC 1918)'; Cidr = @('10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16') }
@{ Kind = 'documentation ranges'; Cidr = @('203.0.113.0/24', '2001:db8::/32') }
@{ Kind = 'IPv6 unique local'; Cidr = @('fd00::/8') }
) {
$locations = @(Get-TestIpLocation -DisplayName 'Internal networks' -CidrAddress $Cidr -Trusted)
Mock Invoke-MtGraphRequest -ModuleName Maester { $locations }

Test-MtCisTrustedNamedLocation | Should -BeTrue
Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter {
$Result -like 'Well done.*' -and
$Result -like "*| Internal networks | $($Cidr.Count) | $($Cidr.Count) | True |*" -and
$Result -like '*never match a sign-in*- Internal networks*'
}
}

It 'Passes for a trusted location that mixes private and public ranges, without a note' {
$locations = @(Get-TestIpLocation -DisplayName 'Offices' -CidrAddress '192.168.0.0/16', '2001:4860::/32' -Trusted)
Mock Invoke-MtGraphRequest -ModuleName Maester { $locations }

Test-MtCisTrustedNamedLocation | Should -BeTrue
Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter {
$Result -like '*| Offices | 2 | 1 | True |*' -and $Result -notlike '*never match a sign-in*'
}
}

It 'Fails for a public range that is not trusted' {
$locations = @(Get-TestIpLocation -DisplayName 'Partner' -CidrAddress '8.8.4.0/24')
Mock Invoke-MtGraphRequest -ModuleName Maester { $locations }

Test-MtCisTrustedNamedLocation | Should -BeFalse
Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter {
$Result -like 'No IP named location is both trusted and has IP ranges*'
}
}

It 'Fails when only country locations exist' {
$locations = @(
[pscustomobject]@{
'@odata.type' = '#microsoft.graph.countryNamedLocation'
displayName = 'Allowed countries'
countriesAndRegions = @('FR')
}
)
Mock Invoke-MtGraphRequest -ModuleName Maester { $locations }

Test-MtCisTrustedNamedLocation | Should -BeFalse
Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter {
$Result -like 'No IP named locations are defined*'
}
}
}
2 changes: 1 addition & 1 deletion tests/cis/Test.CIS.M365.5.2.2.14.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ At least one IP-based named location with address ranges exists and is marked as

Named locations give Conditional Access a way to tell your offices and known egress addresses from the rest of the internet. Marked as trusted, they feed Microsoft Entra ID Protection and let policies relax or tighten requirements by network.

The test passes when at least one IP named location has ranges and is trusted. Country-based locations are not counted. Only mark a network as trusted when it is under your control.
The test passes when at least one IP named location has ranges and is trusted, as the benchmark asks. The result also lists the trusted locations whose ranges are all private or otherwise non-routable, such as 10.0.0.0/8, 172.16.0.0/12 or 192.168.0.0/16. They meet the benchmark, but they never match a sign-in: Microsoft Entra ID evaluates the public address that a network uses to reach the internet, never the address of a device on the intranet. Country-based locations are not counted. Only mark a network as trusted when it is under your control.

#### Remediation action

Expand Down
69 changes: 60 additions & 9 deletions tests/cis/Test.CIS.M365.5.2.2.14.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

.DESCRIPTION
Checks that at least one IP-based named location in Conditional Access has IP ranges and is marked as trusted, so that policies can treat sign-ins from your known networks differently.
The result also lists the trusted locations whose ranges are all private or otherwise non-routable. They meet the benchmark, but they never match a sign-in: Microsoft Entra ID evaluates the public address that a network uses to reach the internet, never the address of a device on the intranet.
CIS Microsoft 365 Foundations Benchmark v7.0.0 (5.2.2.14, L2)

.EXAMPLE
Expand All @@ -29,7 +30,8 @@
Control = ('cis-m365:5.2.2.14', 'nist-800-53-r5:AC-3'),
Service = 'Graph',
License = 'AAD_PREMIUM',
Author = 'JeremyFinney-lgtm'
Author = 'JeremyFinney-lgtm',
Contributor = 'julien-ly'
)]
[CmdletBinding()]
[OutputType([bool])]
Expand All @@ -39,24 +41,66 @@
$locations = Invoke-MtGraphRequest -RelativeUri 'identity/conditionalAccess/namedLocations'
$ipLocations = @($locations | Where-Object { $_.'@odata.type' -eq '#microsoft.graph.ipNamedLocation' })

# Microsoft Entra ID evaluates the public address that a network uses to reach the internet, never the address of
# a device on the intranet, so a range in private, special-purpose or otherwise non-routable space can't match a sign-in.
$nonPublicNetworks = @(
'10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', # Private (RFC 1918)
'100.64.0.0/10', # Shared address space (RFC 6598)
'0.0.0.0/8', '127.0.0.0/8', '169.254.0.0/16', # This network, loopback and link-local
'192.0.2.0/24', '198.51.100.0/24', '203.0.113.0/24', # Documentation (RFC 5737)
'198.18.0.0/15', # Benchmarking (RFC 2544)
'224.0.0.0/4', '240.0.0.0/4', # Multicast and reserved
'fc00::/7', 'fe80::/10', '::1/128', '::/128', # IPv6 unique local, link-local, loopback and unspecified
'2001:db8::/32', '3fff::/20', # IPv6 documentation (RFC 3849, RFC 9637)
'ff00::/8' # IPv6 multicast
)
$isNonPublicRange = {
param([string] $Cidr)
$address, $prefix = $Cidr.Split('/')
$ip = $null
if (-not [System.Net.IPAddress]::TryParse($address, [ref]$ip)) { return $false }
$bytes = $ip.GetAddressBytes()
$prefixLength = if ($prefix) { [int]$prefix } else { $bytes.Count * 8 }
foreach ($network in $nonPublicNetworks) {
$networkAddress, $networkPrefix = $network.Split('/')
$networkBytes = [System.Net.IPAddress]::Parse($networkAddress).GetAddressBytes()
# The range lies inside the network when it is at least as narrow and shares the network's prefix bits.
if ($networkBytes.Count -ne $bytes.Count -or $prefixLength -lt [int]$networkPrefix) { continue }
$inNetwork = $true
for ($bit = 0; $bit -lt [int]$networkPrefix; $bit++) {
$mask = 0x80 -shr ($bit % 8)
if (($bytes[$bit -shr 3] -band $mask) -ne ($networkBytes[$bit -shr 3] -band $mask)) {
$inNetwork = $false
break
}
}
if ($inNetwork) { return $true }
}
return $false
}

$report = foreach ($location in $ipLocations) {
$rangeCount = @($location.ipRanges).Count
$ranges = @($location.ipRanges | ForEach-Object { [string]$_.cidrAddress } | Where-Object { $_ })
$nonPublicRangeCount = @($ranges | Where-Object { & $isNonPublicRange $_ }).Count
[pscustomobject]@{
Name = $location.displayName
Ranges = $rangeCount
Trusted = [bool]$location.isTrusted
Pass = $rangeCount -gt 0 -and $location.isTrusted -eq $true
Name = $location.displayName
Ranges = $ranges.Count
NonPublicRanges = $nonPublicRangeCount
Trusted = [bool]$location.isTrusted
Pass = $ranges.Count -gt 0 -and $location.isTrusted -eq $true
}
}
$testResult = @($report | Where-Object { $_.Pass }).Count -gt 0
# Trusted locations that meet the benchmark but can't match any sign-in.
$unmatchable = @($report | Where-Object { $_.Pass -and $_.NonPublicRanges -eq $_.Ranges })

$passText = 'At least one trusted IP named location with IP ranges is defined.'
$failText = if ($ipLocations.Count -eq 0) { 'No IP named locations are defined.' } else { 'No IP named location is both trusted and has IP ranges.' }

$resultMd = "| Named location | IP ranges | Trusted | Result |`n"
$resultMd += "| --- | --- | --- | --- |`n"
$resultMd = "| Named location | IP ranges | Non-public ranges | Trusted | Result |`n"
$resultMd += "| --- | --- | --- | --- | --- |`n"
foreach ($row in $report) {
$resultMd += "| $($row.Name) | $($row.Ranges) | $($row.Trusted) | $(if ($row.Pass) { '✅ Pass' } else { '❌ Fail' }) |`n"
$resultMd += "| $(Get-MtSafeMarkdown $row.Name) | $($row.Ranges) | $($row.NonPublicRanges) | $($row.Trusted) | $(if ($row.Pass) { '✅ Pass' } else { '❌ Fail' }) |`n"
}

if ($testResult) {
Expand All @@ -66,6 +110,13 @@
}
$testResultMarkdown = $testResultMarkdown -replace '%TestResult%', $resultMd

if ($unmatchable.Count -gt 0) {
$unmatchableList = ($unmatchable | ForEach-Object { "- $(Get-MtSafeMarkdown $_.Name)" }) -join "`n"
$testResultMarkdown += "`n`nThe following trusted locations meet the benchmark, but all their ranges are private or otherwise " +
'non-routable, so they never match a sign-in: Microsoft Entra ID evaluates the public address that a network uses ' +
"to reach the internet. Add the public ranges of your egress points.`n`n$unmatchableList"
}

Add-MtTestResultDetail -Result $testResultMarkdown
return $testResult
}
Loading