diff --git a/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 b/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 new file mode 100644 index 0000000000..ae481b7368 --- /dev/null +++ b/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 @@ -0,0 +1,122 @@ +Describe 'Test-MtCisSafeAntiPhishingPolicy' { + BeforeAll { + Import-Module "$PSScriptRoot\..\..\Maester.psd1" -Force + . "$PSScriptRoot\..\helpers\Use-MtModuleFunction.ps1" + Use-MtModuleFunction -Name Test-MtCisSafeAntiPhishingPolicy + + # Every CIS setting is met, so the rule scope decides the result. + $script:compliantSettings = @{ + Enabled = $true + PhishThresholdLevel = 3 + EnableTargetedUserProtection = $true + TargetedUsersToProtect = @('Executive;executive@contoso.com') + EnableOrganizationDomainsProtection = $true + EnableMailboxIntelligence = $true + EnableMailboxIntelligenceProtection = $true + EnableSpoofIntelligence = $true + TargetedUserProtectionAction = 'Quarantine' + TargetedDomainProtectionAction = 'Quarantine' + MailboxIntelligenceProtectionAction = 'Quarantine' + EnableFirstContactSafetyTips = $true + EnableSimilarUsersSafetyTips = $true + EnableSimilarDomainsSafetyTips = $true + EnableUnusualCharactersSafetyTips = $true + HonorDmarcPolicy = $true + } + } + + BeforeEach { + Mock Add-MtTestResultDetail -ModuleName Maester { } + } + + It 'Reports for a rule scoped to ' -ForEach @( + @{ Scope = 'all recipients'; Domains = @(); Groups = @(); Users = @(); Expected = 'Pass' } + @{ Scope = 'every accepted domain'; Domains = @('contoso.com', 'fabrikam.com'); Groups = @(); Users = @(); Expected = 'Pass' } + @{ Scope = 'a group'; Domains = @(); Groups = @('All staff'); Users = @(); Expected = 'Pass' } + @{ Scope = 'some accepted domains'; Domains = @('contoso.com'); Groups = @(); Users = @(); Expected = 'Fail' } + @{ Scope = 'users'; Domains = @(); Groups = @(); Users = @('adele@contoso.com'); Expected = 'Fail' } + @{ Scope = 'some accepted domains and users'; Domains = @('contoso.com'); Groups = @(); Users = @('adele@contoso.com'); Expected = 'Fail' } + @{ Scope = 'every accepted domain and users'; Domains = @('contoso.com', 'fabrikam.com'); Groups = @(); Users = @('adele@contoso.com'); Expected = 'Investigate' } + @{ Scope = 'a group and users'; Domains = @(); Groups = @('All staff'); Users = @('adele@contoso.com'); Expected = 'Investigate' } + ) { + $policy = [pscustomobject](@{ Name = 'Contoso'; Identity = 'Contoso' } + $script:compliantSettings) + $rule = [pscustomobject]@{ AntiPhishPolicy = 'Contoso'; State = 'Enabled'; Priority = 0; RecipientDomainIs = $Domains; SentToMemberOf = $Groups; SentTo = $Users } + Mock Get-MtExo -ModuleName Maester { + switch ($Request) { + 'AntiPhishPolicy' { $policy } + 'AntiPhishRule' { $rule } + 'AcceptedDomain' { [pscustomobject]@{ Name = 'contoso.com' }; [pscustomobject]@{ Name = 'fabrikam.com' } } + } + } + + Test-MtCisSafeAntiPhishingPolicy | Should -Be ($Expected -eq 'Pass') + + $scopeResult = if ($Expected -eq 'Pass') { 'Pass' } else { 'Fail' } + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + [bool]$Investigate -eq ($Expected -eq 'Investigate') -and + $Result -match "\| Contoso \| Enabled matching rule and scope \| [^\r\n]* $scopeResult \|" -and + ($Expected -ne 'Investigate' -or $Result -match 'MC1546447') + } + } + + It 'Fails without investigation when another check also fails' { + $policy = [pscustomobject](@{ Name = 'Contoso'; Identity = 'Contoso' } + $script:compliantSettings) + $policy.PhishThresholdLevel = 1 + $rule = [pscustomobject]@{ AntiPhishPolicy = 'Contoso'; State = 'Enabled'; Priority = 0; RecipientDomainIs = @('contoso.com', 'fabrikam.com'); SentTo = @('adele@contoso.com') } + Mock Get-MtExo -ModuleName Maester { + switch ($Request) { + 'AntiPhishPolicy' { $policy } + 'AntiPhishRule' { $rule } + 'AcceptedDomain' { [pscustomobject]@{ Name = 'contoso.com' }; [pscustomobject]@{ Name = 'fabrikam.com' } } + } + } + + Test-MtCisSafeAntiPhishingPolicy | Should -BeFalse + + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + -not [bool]$Investigate -and $Result.StartsWith('No enabled anti-phishing policy') + } + } + + It 'Passes without investigation when another policy meets every check' { + $executivePolicy = [pscustomobject](@{ Name = 'Executives'; Identity = 'Executives' } + $script:compliantSettings) + $tenantPolicy = [pscustomobject](@{ Name = 'Contoso'; Identity = 'Contoso' } + $script:compliantSettings) + Mock Get-MtExo -ModuleName Maester { + switch ($Request) { + 'AntiPhishPolicy' { $executivePolicy; $tenantPolicy } + 'AntiPhishRule' { + [pscustomobject]@{ AntiPhishPolicy = 'Executives'; State = 'Enabled'; Priority = 0; RecipientDomainIs = @('contoso.com', 'fabrikam.com'); SentTo = @('adele@contoso.com') } + [pscustomobject]@{ AntiPhishPolicy = 'Contoso'; State = 'Enabled'; Priority = 1; RecipientDomainIs = @('contoso.com', 'fabrikam.com') } + } + 'AcceptedDomain' { [pscustomobject]@{ Name = 'contoso.com' }; [pscustomobject]@{ Name = 'fabrikam.com' } } + } + } + + Test-MtCisSafeAntiPhishingPolicy | Should -BeTrue + + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + -not [bool]$Investigate -and $Result.StartsWith('Well done.') + } + } + + It 'Names every policy that needs investigation, in the plural when there are several' { + $executivePolicy = [pscustomobject](@{ Name = 'Executives'; Identity = 'Executives' } + $script:compliantSettings) + $financePolicy = [pscustomobject](@{ Name = 'Finance'; Identity = 'Finance' } + $script:compliantSettings) + Mock Get-MtExo -ModuleName Maester { + switch ($Request) { + 'AntiPhishPolicy' { $executivePolicy; $financePolicy } + 'AntiPhishRule' { + [pscustomobject]@{ AntiPhishPolicy = 'Executives'; State = 'Enabled'; Priority = 0; RecipientDomainIs = @('contoso.com', 'fabrikam.com'); SentTo = @('adele@contoso.com') } + [pscustomobject]@{ AntiPhishPolicy = 'Finance'; State = 'Enabled'; Priority = 1; SentToMemberOf = @('Finance'); SentTo = @('megan@contoso.com') } + } + 'AcceptedDomain' { [pscustomobject]@{ Name = 'contoso.com' }; [pscustomobject]@{ Name = 'fabrikam.com' } } + } + } + + Test-MtCisSafeAntiPhishingPolicy | Should -BeFalse + + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + [bool]$Investigate -and $Result.StartsWith("Anti-phishing policies 'Executives', 'Finance' meet the other CIS recommendations, but their rules") + } + } +} diff --git a/powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 b/powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 new file mode 100644 index 0000000000..6348348d35 --- /dev/null +++ b/powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 @@ -0,0 +1,59 @@ +Describe 'Test-MtCisSafeAttachment' { + BeforeAll { + Import-Module "$PSScriptRoot\..\..\Maester.psd1" -Force + . "$PSScriptRoot\..\helpers\Use-MtModuleFunction.ps1" + Use-MtModuleFunction -Name Test-MtCisSafeAttachment + } + + BeforeEach { + Mock Add-MtTestResultDetail -ModuleName Maester { } + } + + It 'Reports for a custom rule scoped to ' -ForEach @( + @{ Scope = 'all recipients'; Domains = @(); Groups = @(); Users = @(); Expected = 'Pass' } + @{ Scope = 'every accepted domain'; Domains = @('contoso.com', 'fabrikam.com'); Groups = @(); Users = @(); Expected = 'Pass' } + @{ Scope = 'a group'; Domains = @(); Groups = @('All staff'); Users = @(); Expected = 'Pass' } + @{ Scope = 'some accepted domains'; Domains = @('contoso.com'); Groups = @(); Users = @(); Expected = 'Fail' } + @{ Scope = 'users'; Domains = @(); Groups = @(); Users = @('adele@contoso.com'); Expected = 'Fail' } + @{ Scope = 'some accepted domains and users'; Domains = @('contoso.com'); Groups = @(); Users = @('adele@contoso.com'); Expected = 'Fail' } + @{ Scope = 'every accepted domain and users'; Domains = @('contoso.com', 'fabrikam.com'); Groups = @(); Users = @('adele@contoso.com'); Expected = 'Investigate' } + @{ Scope = 'a group and users'; Domains = @(); Groups = @('All staff'); Users = @('adele@contoso.com'); Expected = 'Investigate' } + ) { + $policy = [pscustomobject]@{ Name = 'Contoso'; Identity = 'Contoso'; Enable = $true; Action = 'Block'; QuarantineTag = 'AdminOnlyAccessPolicy' } + $rule = [pscustomobject]@{ Identity = 'Contoso'; SafeAttachmentPolicy = 'Contoso'; State = 'Enabled'; Priority = 0; RecipientDomainIs = $Domains; SentToMemberOf = $Groups; SentTo = $Users } + Mock Get-MtExo -ModuleName Maester { + switch ($Request) { + 'SafeAttachmentPolicy' { $policy } + 'SafeAttachmentRule' { $rule } + 'AcceptedDomain' { [pscustomobject]@{ Name = 'contoso.com' }; [pscustomobject]@{ Name = 'fabrikam.com' } } + } + } + + Test-MtCisSafeAttachment | Should -Be ($Expected -eq 'Pass') + + $scopeResult = if ($Expected -eq 'Pass') { 'Pass' } else { 'Fail' } + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + [bool]$Investigate -eq ($Expected -eq 'Investigate') -and + $Result -match "\| Rule scope \| [^\r\n]* $scopeResult \|" -and + ($Expected -ne 'Investigate' -or $Result -match 'MC1546447') + } + } + + It 'Fails without investigation when another check also fails' { + $policy = [pscustomobject]@{ Name = 'Contoso'; Identity = 'Contoso'; Enable = $false; Action = 'Block'; QuarantineTag = 'AdminOnlyAccessPolicy' } + $rule = [pscustomobject]@{ Identity = 'Contoso'; SafeAttachmentPolicy = 'Contoso'; State = 'Enabled'; Priority = 0; RecipientDomainIs = @('contoso.com', 'fabrikam.com'); SentTo = @('adele@contoso.com') } + Mock Get-MtExo -ModuleName Maester { + switch ($Request) { + 'SafeAttachmentPolicy' { $policy } + 'SafeAttachmentRule' { $rule } + 'AcceptedDomain' { [pscustomobject]@{ Name = 'contoso.com' }; [pscustomobject]@{ Name = 'fabrikam.com' } } + } + } + + Test-MtCisSafeAttachment | Should -BeFalse + + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + -not [bool]$Investigate -and $Result -match 'does not match CIS recommendations' + } + } +} diff --git a/tests/cis/Test.CIS.M365.2.1.4.md b/tests/cis/Test.CIS.M365.2.1.4.md index 99bb765af3..f8e9574b61 100644 --- a/tests/cis/Test.CIS.M365.2.1.4.md +++ b/tests/cis/Test.CIS.M365.2.1.4.md @@ -2,7 +2,7 @@ The Safe Attachments policy helps protect users from malware in email attachments by scanning attachments for viruses, malware, and other malicious content. When an email attachment is received by a user, Safe Attachments will scan the attachment in a secure environment and provide a verdict on whether the attachment is safe or not. -This check evaluates the highest-priority enabled policy in this order: Strict preset, Standard preset, the highest-priority enabled custom rule, then built-in protection. It verifies the associated rule is enabled and reports its recipient scope. Domain-only rules are checked against accepted domains; group membership size is not evaluated and should be confirmed where group targets are used. +This check evaluates the highest-priority enabled policy in this order: Strict preset, Standard preset, the highest-priority enabled custom rule, then built-in protection. It verifies the associated rule is enabled and reports its recipient scope. Domain-only rules are checked against accepted domains; group membership size is not evaluated and should be confirmed where group targets are used. When the selected rule adds users to every accepted domain or to a group and that scope is its only failed check, the result is Investigate: a rule that matches all conditions applies only to those users, rules created or edited after the MC1546447 rollout match any condition, and the rule does not show which logic it uses. Users alone, or users with only some accepted domains, still fail. #### Rationale diff --git a/tests/cis/Test.CIS.M365.2.1.4.ps1 b/tests/cis/Test.CIS.M365.2.1.4.ps1 index 9ed3cc967d..955c537854 100644 --- a/tests/cis/Test.CIS.M365.2.1.4.ps1 +++ b/tests/cis/Test.CIS.M365.2.1.4.ps1 @@ -102,8 +102,15 @@ $acceptedDomains = @(Get-MtExo -Request AcceptedDomain | ForEach-Object { $_.Name }) } $domainsCoverTenant = $acceptedDomains.Count -gt 0 -and @($acceptedDomains | Where-Object { $_ -notin $domainTargets }).Count -eq 0 - $scopePass = $recipientConditions.Count -eq 0 -or $domainsCoverTenant -or $groupTargets.Count -gt 0 + # Users, groups and domains are different condition types. A rule that matches all of them (AND, the logic of existing + # rules) applies only to the listed users. Rules created or edited after the MC1546447 rollout match any of them (OR), + # and the rule does not expose which logic applies, so users added to domain or group targets need investigation. + $scopeDependsOnMatchLogic = $userTargets.Count -gt 0 -and ($domainsCoverTenant -or $groupTargets.Count -gt 0) + $scopePass = $userTargets.Count -eq 0 -and ($recipientConditions.Count -eq 0 -or $domainsCoverTenant -or $groupTargets.Count -gt 0) $scopeDescription = if ($recipientConditions.Count -eq 0) { 'All recipients' } else { $recipientConditions -join ', ' } + if ($scopeDependsOnMatchLogic) { + $scopeDescription += ' (only these users if the rule matches all conditions)' + } if ($recipientExceptions.Count -gt 0) { $scopeDescription += "; exclusions: $($recipientExceptions -join ', ')" } @@ -115,11 +122,15 @@ [pscustomobject]@{ CheckName = 'Rule state'; Value = $selectedRule.State; Pass = $selectedRule.State -eq 'Enabled' } [pscustomobject]@{ CheckName = 'Rule scope'; Value = $scopeDescription; Pass = $scopePass } ) - $testResult = @($safeAttachmentCheckList | Where-Object { -not $_.Pass }).Count -eq 0 + $failedChecks = @($safeAttachmentCheckList | Where-Object { -not $_.Pass }) + $testResult = $failedChecks.Count -eq 0 + $investigate = $scopeDependsOnMatchLogic -and $failedChecks.Count -eq 1 $portalLink = 'https://security.microsoft.com/safeattachmentv2' if ($testResult) { $testResultMarkdown = "Well done. The highest-priority Safe Attachments policy '$($policy.Name)' and its rule match CIS recommendations ($portalLink).`n`n%TestResult%" + } elseif ($investigate) { + $testResultMarkdown = "The highest-priority Safe Attachments policy '$($policy.Name)' meets the other CIS recommendations, but its rule adds users to domain or group targets. If the rule matches all conditions, as rules created before the MC1546447 rollout do until they are edited, it applies only to those users. If it matches any condition, it also covers the domains or groups. The rule does not show which logic applies, so move the users to a separate policy to make the scope explicit ($portalLink).`n`n%TestResult%" } else { $testResultMarkdown = "The highest-priority Safe Attachments policy '$($policy.Name)' or its rule does not match CIS recommendations ($portalLink).`n`n%TestResult%" } @@ -133,6 +144,6 @@ } $testResultMarkdown = $testResultMarkdown -replace '%TestResult%', $resultMd - Add-MtTestResultDetail -Result $testResultMarkdown + Add-MtTestResultDetail -Result $testResultMarkdown -Investigate:$investigate return $testResult } diff --git a/tests/cis/Test.CIS.M365.2.1.7.md b/tests/cis/Test.CIS.M365.2.1.7.md index f23343412c..88eeb953f3 100644 --- a/tests/cis/Test.CIS.M365.2.1.7.md +++ b/tests/cis/Test.CIS.M365.2.1.7.md @@ -2,7 +2,7 @@ By default, Office 365 includes built-in features that help protect users from phishing attacks. Set up anti-phishing polices to increase this protection, for example by refining settings to better detect and prevent impersonation and spoofing attacks. The default policy applies to all users within the organization and is a single view to fine-tune antiphishing protection. Custom policies can be created and configured for specific users, groups or domains within the organization and will take precedence over the default policy for the scoped users. -This check evaluates enabled AntiPhish policies with a matching enabled rule. It checks the policy settings listed below and reports the rule's recipient scope. Group membership size is not evaluated and should be confirmed where group targets are used. +This check evaluates enabled AntiPhish policies with a matching enabled rule. It checks the policy settings listed below and reports the rule's recipient scope. Group membership size is not evaluated and should be confirmed where group targets are used. When no policy passes and a policy fails only on a rule that adds users to every accepted domain or to a group, the result is Investigate: a rule that matches all conditions applies only to those users, rules created or edited after the MC1546447 rollout match any condition, and the rule does not show which logic it uses. Users alone, or users with only some accepted domains, still fail. #### Rationale diff --git a/tests/cis/Test.CIS.M365.2.1.7.ps1 b/tests/cis/Test.CIS.M365.2.1.7.ps1 index ce2f410712..4e5ab2e21b 100644 --- a/tests/cis/Test.CIS.M365.2.1.7.ps1 +++ b/tests/cis/Test.CIS.M365.2.1.7.ps1 @@ -10,7 +10,7 @@ .EXAMPLE Test-MtCisSafeAntiPhishingPolicy - Returns true if at least one anti-phishing policy and its highest-priority enabled matching rule meet all CIS checks. Recipient scope passes with no targets, coverage of every accepted domain, or at least one group target; group membership and exclusions are not validated. + Returns true if at least one anti-phishing policy and its highest-priority enabled matching rule meet all CIS checks. Recipient scope passes with no targets, coverage of every accepted domain, or at least one group target, when the rule names no users; group membership and exclusions are not validated. When no policy passes and a policy fails only on a rule that adds users to every accepted domain or to a group, the result is Investigate: the scope depends on whether the rule matches all or any of its conditions, which the rule does not show (MC1546447). Users alone, or users with only some accepted domains, fail. .LINK https://maester.dev/docs/commands/Test-MtCisSafeAntiPhishingPolicy @@ -57,7 +57,11 @@ $userTargets = @($rule.SentTo | Where-Object { -not [string]::IsNullOrWhiteSpace([string]$_) }) $recipientConditions = @($domainTargets + $groupTargets + $userTargets) $domainScopeCoversTenant = $domainTargets.Count -gt 0 -and $acceptedDomains.Count -gt 0 -and @($acceptedDomains | Where-Object { $_ -notin $domainTargets }).Count -eq 0 - $scopePass = $null -ne $rule -and ($recipientConditions.Count -eq 0 -or $domainScopeCoversTenant -or $groupTargets.Count -gt 0) + # Users, groups and domains are different condition types. A rule that matches all of them (AND, the logic of existing + # rules) applies only to the listed users. Rules created or edited after the MC1546447 rollout match any of them (OR), + # and the rule does not expose which logic applies, so users added to domain or group targets need investigation. + $scopeDependsOnMatchLogic = $null -ne $rule -and $userTargets.Count -gt 0 -and ($domainScopeCoversTenant -or $groupTargets.Count -gt 0) + $scopePass = $null -ne $rule -and $userTargets.Count -eq 0 -and ($recipientConditions.Count -eq 0 -or $domainScopeCoversTenant -or $groupTargets.Count -gt 0) $scopeDescription = if ($null -eq $rule) { 'No enabled matching rule' } elseif ($recipientConditions.Count -eq 0) { @@ -65,6 +69,9 @@ } else { $recipientConditions -join ', ' } + if ($scopeDependsOnMatchLogic) { + $scopeDescription += ' (only these users if the rule matches all conditions)' + } $recipientExceptions = @( $rule.ExceptIfRecipientDomainIs $rule.ExceptIfSentToMemberOf @@ -101,13 +108,22 @@ Name = $policy.Name Checks = $checks Pass = @($checks | Where-Object { -not $_.Pass }).Count -eq 0 + Investigate = $scopeDependsOnMatchLogic -and @($checks | Where-Object { -not $_.Pass }).Count -eq 1 } } $testResult = @($policyReports | Where-Object { $_.Pass }).Count -gt 0 + $investigatePolicies = @(if (-not $testResult) { $policyReports | Where-Object { $_.Investigate } }) $portalLink = 'https://security.microsoft.com/antiphishing' if ($testResult) { $testResultMarkdown = "Well done. An enabled anti-phishing policy and matching rule meet CIS recommendations ($portalLink).`n`n%TestResult%" + } elseif ($investigatePolicies.Count -gt 0) { + $policyList = "'$($investigatePolicies.Name -join "', '")'" + $testResultMarkdown = if ($investigatePolicies.Count -eq 1) { + "Anti-phishing policy $policyList meets the other CIS recommendations, but its rule adds users to domain or group targets. If the rule matches all conditions, as rules created before the MC1546447 rollout do until they are edited, it applies only to those users. If it matches any condition, it also covers the domains or groups. The rule does not show which logic applies, so move the users to a separate policy to make the scope explicit ($portalLink).`n`n%TestResult%" + } else { + "Anti-phishing policies $policyList meet the other CIS recommendations, but their rules add users to domain or group targets. If a rule matches all conditions, as rules created before the MC1546447 rollout do until they are edited, it applies only to those users. If it matches any condition, it also covers the domains or groups. The rules do not show which logic applies, so move the users to separate policies to make the scope explicit ($portalLink).`n`n%TestResult%" + } } else { $testResultMarkdown = "No enabled anti-phishing policy and matching rule meet all CIS recommendations ($portalLink).`n`n%TestResult%" } @@ -123,6 +139,6 @@ } $testResultMarkdown = $testResultMarkdown -replace '%TestResult%', $resultMd - Add-MtTestResultDetail -Result $testResultMarkdown + Add-MtTestResultDetail -Result $testResultMarkdown -Investigate:($investigatePolicies.Count -gt 0) return $testResult }