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
189 changes: 189 additions & 0 deletions powershell/tests/functions/Test-MtCaGap.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
Describe 'Test-MtCaGap (MT.1036)' {
BeforeAll {
Import-Module $PSScriptRoot/../../Maester.psd1 -Force
. "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1"
# The native-test function is not exported; reach it the same way the other native tests do.
Use-MtModuleFunction -Name Test-MtCaGap

$script:BreakGlassUser = '11111111-1111-1111-1111-111111111111'
$script:AgentIdentity = '22222222-2222-2222-2222-222222222222'
$script:AgentUser = '33333333-3333-3333-3333-333333333333'

# Builds a Conditional Access policy the way Graph beta returns it.
function Get-TestPolicy {
param(
[string]$Name,
[string]$State = 'enabled',
[string[]]$IncludeUsers = @(),
[string[]]$ExcludeUsers = @(),
[string[]]$IncludeAgentIdServicePrincipals,
[string[]]$ExcludeAgentIdServicePrincipals,
[string[]]$IncludeAgentUsers,
[string[]]$ExcludeAgentUsers
)
$clientApplications = $null
if ($PSBoundParameters.ContainsKey('IncludeAgentIdServicePrincipals') -or $PSBoundParameters.ContainsKey('ExcludeAgentIdServicePrincipals')) {
$clientApplications = [pscustomobject]@{
includeServicePrincipals = @()
excludeServicePrincipals = @()
includeAgentIdServicePrincipals = @($IncludeAgentIdServicePrincipals)
excludeAgentIdServicePrincipals = @($ExcludeAgentIdServicePrincipals)
}
}
$agents = $null
if ($PSBoundParameters.ContainsKey('IncludeAgentUsers') -or $PSBoundParameters.ContainsKey('ExcludeAgentUsers')) {
$agents = [pscustomobject]@{
includeAgentUsers = @($IncludeAgentUsers)
excludeAgentUsers = @($ExcludeAgentUsers)
agentFilter = $null
}
}
[pscustomobject]@{
id = [guid]::NewGuid().Guid
displayName = $Name
state = $State
conditions = [pscustomobject]@{
users = [pscustomobject]@{
includeUsers = @($IncludeUsers)
excludeUsers = @($ExcludeUsers)
includeGroups = @()
excludeGroups = @()
includeRoles = @()
excludeRoles = @()
}
applications = [pscustomobject]@{ includeApplications = @('All'); excludeApplications = @() }
clientApplications = $clientApplications
agents = $agents
locations = $null
platforms = $null
}
}
}
}

BeforeEach {
$script:TestResult = $null
Mock -ModuleName Maester Add-MtTestResultDetail { $script:TestResult = $Result }
# Names resolve by ID; anything not listed fails to resolve.
Mock -ModuleName Maester Invoke-MtGraphRequest {
switch ($UniqueId) {
'11111111-1111-1111-1111-111111111111' { return [pscustomobject]@{ displayName = 'breakglass1' } }
'22222222-2222-2222-2222-222222222222' { return [pscustomobject]@{ displayName = 'Contoso Agent' } }
'33333333-3333-3333-3333-333333333333' { return [pscustomobject]@{ displayName = 'Contoso Agent User' } }
default { throw 'Not found' }
}
}
}

It 'passes when nothing is excluded' {
Mock -ModuleName Maester Get-MtConditionalAccessPolicy { Get-TestPolicy -Name 'All users MFA' -IncludeUsers 'All' }

Test-MtCaGap | Should -BeTrue
$script:TestResult | Should -Match 'All excluded objects seem to have a fallback'
}

It 'reports an excluded user with no fallback (existing behaviour)' {
Mock -ModuleName Maester Get-MtConditionalAccessPolicy { Get-TestPolicy -Name 'All users MFA' -IncludeUsers 'All' -ExcludeUsers $script:BreakGlassUser }

Test-MtCaGap | Should -BeFalse
$script:TestResult | Should -Match '(?m)^ User: breakglass1$'
$script:TestResult | Should -Match "Excluded in policy 'All users MFA'"
}

It 'reports an excluded agent identity with no fallback' {
Mock -ModuleName Maester Get-MtConditionalAccessPolicy {
Get-TestPolicy -Name 'Block risky agents' -IncludeUsers 'None' -IncludeAgentIdServicePrincipals 'All' -ExcludeAgentIdServicePrincipals $script:AgentIdentity
}

Test-MtCaGap | Should -BeFalse
$script:TestResult | Should -Match 'The following agent identity objects did not have a fallback'
$script:TestResult | Should -Match '(?m)^ Agent identity: Contoso Agent$'
$script:TestResult | Should -Match "Excluded in policy 'Block risky agents'"
# Agent identities are looked up as service principals.
Should -Invoke Invoke-MtGraphRequest -ModuleName Maester -ParameterFilter { $RelativeUri -eq 'servicePrincipals' -and $UniqueId -eq '22222222-2222-2222-2222-222222222222' }
}

It 'passes when an excluded agent identity is included by ID in another enabled policy' {
Mock -ModuleName Maester Get-MtConditionalAccessPolicy {
Get-TestPolicy -Name 'Block risky agents' -IncludeUsers 'None' -IncludeAgentIdServicePrincipals 'All' -ExcludeAgentIdServicePrincipals $script:AgentIdentity
Get-TestPolicy -Name 'Contoso agent policy' -IncludeUsers 'None' -IncludeAgentIdServicePrincipals $script:AgentIdentity -ExcludeAgentIdServicePrincipals @()
}

Test-MtCaGap | Should -BeTrue
}

It 'reports an excluded agent user with no fallback' {
Mock -ModuleName Maester Get-MtConditionalAccessPolicy {
Get-TestPolicy -Name 'Block risky agent users' -IncludeUsers 'None' -IncludeAgentUsers 'All' -ExcludeAgentUsers $script:AgentUser
}

Test-MtCaGap | Should -BeFalse
$script:TestResult | Should -Match 'The following agent user objects did not have a fallback'
$script:TestResult | Should -Match '(?m)^ Agent user: Contoso Agent User$'
$script:TestResult | Should -Match "Excluded in policy 'Block risky agent users'"
# Agents' user accounts are looked up as users.
Should -Invoke Invoke-MtGraphRequest -ModuleName Maester -ParameterFilter { $RelativeUri -eq 'users' -and $UniqueId -eq '33333333-3333-3333-3333-333333333333' }
}

It 'passes when an excluded agent user is included by ID in another enabled policy' {
Mock -ModuleName Maester Get-MtConditionalAccessPolicy {
Get-TestPolicy -Name 'Block risky agent users' -IncludeUsers 'None' -IncludeAgentUsers 'All' -ExcludeAgentUsers $script:AgentUser
Get-TestPolicy -Name 'Contoso agent user policy' -IncludeUsers 'None' -IncludeAgentUsers $script:AgentUser -ExcludeAgentUsers @()
}

Test-MtCaGap | Should -BeTrue
}

It 'does not count an include of All as a fallback for an excluded agent' {
Mock -ModuleName Maester Get-MtConditionalAccessPolicy {
Get-TestPolicy -Name 'Block risky agents' -IncludeUsers 'None' -IncludeAgentIdServicePrincipals 'All' -ExcludeAgentIdServicePrincipals $script:AgentIdentity
Get-TestPolicy -Name 'All agents MFA' -IncludeUsers 'None' -IncludeAgentIdServicePrincipals 'All' -ExcludeAgentIdServicePrincipals @()
}

Test-MtCaGap | Should -BeFalse
$script:TestResult | Should -Match '(?m)^ Agent identity: Contoso Agent$'
}

It 'does not count a service principal or agent user include as a fallback for an excluded agent identity' {
# An agent identity is a service principal, but the check keeps the three ID lists apart.
Mock -ModuleName Maester Get-MtConditionalAccessPolicy {
Get-TestPolicy -Name 'Block risky agents' -IncludeUsers 'None' -IncludeAgentIdServicePrincipals 'All' -ExcludeAgentIdServicePrincipals $script:AgentIdentity
$sp = Get-TestPolicy -Name 'Workload identity policy' -IncludeUsers 'None'
$sp.conditions.clientApplications = [pscustomobject]@{ includeServicePrincipals = @($script:AgentIdentity); excludeServicePrincipals = @() }
$sp
Get-TestPolicy -Name 'Agent user policy' -IncludeUsers 'None' -IncludeAgentUsers $script:AgentIdentity -ExcludeAgentUsers @()
}

Test-MtCaGap | Should -BeFalse
$script:TestResult | Should -Match '(?m)^ Agent identity: Contoso Agent$'
}

It 'ignores agent exclusions in policies that are not enabled' {
Mock -ModuleName Maester Get-MtConditionalAccessPolicy {
Get-TestPolicy -Name 'Report-only agents' -State 'enabledForReportingButNotEnforced' -IncludeUsers 'None' -IncludeAgentIdServicePrincipals 'All' -ExcludeAgentIdServicePrincipals $script:AgentIdentity
Get-TestPolicy -Name 'Off agent users' -State 'disabled' -IncludeUsers 'None' -IncludeAgentUsers 'All' -ExcludeAgentUsers $script:AgentUser
}

Test-MtCaGap | Should -BeTrue
}

It 'shows the ID when an agent name cannot be resolved' {
Mock -ModuleName Maester Get-MtConditionalAccessPolicy {
Get-TestPolicy -Name 'Block risky agents' -IncludeUsers 'None' -IncludeAgentIdServicePrincipals 'All' -ExcludeAgentIdServicePrincipals '44444444-4444-4444-4444-444444444444'
}

Test-MtCaGap | Should -BeFalse
$script:TestResult | Should -Match 'Agent identity: 44444444-4444-4444-4444-444444444444 \(Unable to resolve GUID\)'
}

It 'reports user and agent gaps together' {
Mock -ModuleName Maester Get-MtConditionalAccessPolicy {
Get-TestPolicy -Name 'All users MFA' -IncludeUsers 'All' -ExcludeUsers $script:BreakGlassUser
Get-TestPolicy -Name 'Block risky agent users' -IncludeUsers 'None' -IncludeAgentUsers 'All' -ExcludeAgentUsers $script:AgentUser
}

Test-MtCaGap | Should -BeFalse
$script:TestResult | Should -Match '(?m)^ User: breakglass1$'
$script:TestResult | Should -Match '(?m)^ Agent user: Contoso Agent User$'
}
}
5 changes: 5 additions & 0 deletions tests/maester/entra/Test.MT.1036.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ Checks for gaps in Conditional Access policies, by looking for excluded objects
in another Conditional Access policy. Instead of looking at the historical sign-ins to find gaps, we try to spot possibly
overlooked exclusions which do not have a fallback.

The check compares excluded and included users, groups, roles, applications, service principals, agent identities,
agent users, locations and platforms across all enabled policies. An object counts as having a fallback only when an
enabled policy includes it by its ID (or, for platforms and special values such as `GuestsOrExternalUsers`, by the same
value): an include of `All` does not count.

#### Remediation action

Review the configuration described above.
Expand Down
46 changes: 45 additions & 1 deletion tests/maester/entra/Test.MT.1036.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function Test-MtCaGap {
Mitre = 'T1078.004',
Service = 'Graph',
Author = 'RobbeVandenDaele',
Contributor = ('soulemike', 'weyCC81', 'thomas-s-schmidt')
Contributor = ('soulemike', 'weyCC81', 'thomas-s-schmidt', 'Dfrank77')
)]
[CmdletBinding()]
[OutputType([bool])]
Expand Down Expand Up @@ -129,6 +129,14 @@ function Test-MtCaGap {
[System.Collections.ArrayList]$excludedServicePrincipals = @()
[System.Collections.ArrayList]$includedServicePrincipals = @()
[System.Collections.ArrayList]$differencesServicePrincipals = @()
# Variables related to agent identities (agent identities are service principals, targeted through their own condition)
[System.Collections.ArrayList]$excludedAgentIdentities = @()
[System.Collections.ArrayList]$includedAgentIdentities = @()
[System.Collections.ArrayList]$differencesAgentIdentities = @()
# Variables related to agent users (agents' user accounts, targeted through the agents condition)
[System.Collections.ArrayList]$excludedAgentUsers = @()
[System.Collections.ArrayList]$includedAgentUsers = @()
[System.Collections.ArrayList]$differencesAgentUsers = @()
# Variables related to Locations
[System.Collections.ArrayList]$excludedLocations = @()
[System.Collections.ArrayList]$includedLocations = @()
Expand Down Expand Up @@ -157,6 +165,10 @@ function Test-MtCaGap {
$_.Conditions.Applications.IncludeApplications | ForEach-Object { $includedApplications.Add($_) | Out-Null }
$_.Conditions.ClientApplications.ExcludeServicePrincipals | ForEach-Object { $excludedServicePrincipals.Add($_) | Out-Null }
$_.Conditions.ClientApplications.IncludeServicePrincipals | ForEach-Object { $includedServicePrincipals.Add($_) | Out-Null }
$_.Conditions.ClientApplications.ExcludeAgentIdServicePrincipals | ForEach-Object { $excludedAgentIdentities.Add($_) | Out-Null }
$_.Conditions.ClientApplications.IncludeAgentIdServicePrincipals | ForEach-Object { $includedAgentIdentities.Add($_) | Out-Null }
$_.Conditions.Agents.ExcludeAgentUsers | ForEach-Object { $excludedAgentUsers.Add($_) | Out-Null }
$_.Conditions.Agents.IncludeAgentUsers | ForEach-Object { $includedAgentUsers.Add($_) | Out-Null }
$_.Conditions.Locations.ExcludeLocations | ForEach-Object { $excludedLocations.Add($_) | Out-Null }
$_.Conditions.Locations.IncludeLocations | ForEach-Object { $includedLocations.Add($_) | Out-Null }
$_.Conditions.Platforms.ExcludePlatforms | ForEach-Object { $excludedPlatforms.Add($_) | Out-Null }
Expand All @@ -168,6 +180,8 @@ function Test-MtCaGap {
$_.Conditions.Users.ExcludeRoles + `
$_.Conditions.Applications.ExcludeApplications + `
$_.Conditions.ClientApplications.ExcludeServicePrincipals + `
$_.Conditions.ClientApplications.ExcludeAgentIdServicePrincipals + `
$_.Conditions.Agents.ExcludeAgentUsers + `
$_.Conditions.Locations.ExcludeLocations + `
$_.Conditions.Platforms.ExcludePlatforms
# Create the mapping
Expand All @@ -187,6 +201,8 @@ function Test-MtCaGap {
[System.Collections.ArrayList]$differencesRoles = @(Get-ObjectDifference -excludedObjects $excludedRoles -includedObjects $includedRoles)
[System.Collections.ArrayList]$differencesApplications = @(Get-ObjectDifference -excludedObjects $excludedApplications -includedObjects $includedApplications)
[System.Collections.ArrayList]$differencesServicePrincipals = @(Get-ObjectDifference -excludedObjects $excludedServicePrincipals -includedObjects $includedServicePrincipals)
[System.Collections.ArrayList]$differencesAgentIdentities = @(Get-ObjectDifference -excludedObjects $excludedAgentIdentities -includedObjects $includedAgentIdentities)
[System.Collections.ArrayList]$differencesAgentUsers = @(Get-ObjectDifference -excludedObjects $excludedAgentUsers -includedObjects $includedAgentUsers)
[System.Collections.ArrayList]$differencesLocations = @(Get-ObjectDifference -excludedObjects $excludedLocations -includedObjects $includedLocations)
[System.Collections.ArrayList]$differencesPlatforms = @(Get-ObjectDifference -excludedObjects $excludedPlatforms -includedObjects $includedPlatforms)
Write-Verbose 'Finished searching for gaps in policies.'
Expand All @@ -198,6 +214,8 @@ function Test-MtCaGap {
-and $differencesRoles.Count -eq 0 `
-and $differencesApplications.Count -eq 0 `
-and $differencesServicePrincipals.Count -eq 0 `
-and $differencesAgentIdentities.Count -eq 0 `
-and $differencesAgentUsers.Count -eq 0 `
-and $differencesLocations.Count -eq 0 `
-and $differencesPlatforms.Count -eq 0 `
) {
Expand Down Expand Up @@ -271,6 +289,32 @@ function Test-MtCaGap {
$testResult += Get-RelatedPolicy -Arr $mappingArray -ObjName $Object
}
}
# Add agent identity objects to results
if ($differencesAgentIdentities.Count -ne 0) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
$testResult += "The following agent identity objects did not have a fallback:`n`n"
foreach ($Object in $differencesAgentIdentities) {
try {
$DisplayName = (Invoke-MtGraphRequest -RelativeUri 'servicePrincipals' -ApiVersion v1.0 -UniqueId $Object -ErrorAction Stop).displayName
} catch {
$DisplayName = "${Object} (Unable to resolve GUID)"
}
$testResult += "`n Agent identity: ${DisplayName}`n"
$testResult += Get-RelatedPolicy -Arr $mappingArray -ObjName $Object
}
}
# Add agent user objects to results
if ($differencesAgentUsers.Count -ne 0) {
$testResult += "The following agent user objects did not have a fallback:`n`n"
foreach ($Object in $differencesAgentUsers) {
try {
$DisplayName = (Invoke-MtGraphRequest -RelativeUri 'users' -ApiVersion v1.0 -UniqueId $Object -ErrorAction Stop).displayName
} catch {
$DisplayName = "${Object} (Unable to resolve GUID)"
}
$testResult += "`n Agent user: ${DisplayName}`n"
$testResult += Get-RelatedPolicy -Arr $mappingArray -ObjName $Object
}
}
# Add location objects to results
if ($differencesLocations.Count -ne 0) {
$testResult += "The following location objects did not have a fallback:`n`n"
Expand Down
Loading