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,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 <Expected> for a rule scoped to <Scope>' -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")
}
}
}
59 changes: 59 additions & 0 deletions powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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 <Expected> for a custom rule scoped to <Scope>' -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'
}
}
}
2 changes: 1 addition & 1 deletion tests/cis/Test.CIS.M365.2.1.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 14 additions & 3 deletions tests/cis/Test.CIS.M365.2.1.4.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 ', ')"
}
Expand All @@ -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%"
}
Expand All @@ -133,6 +144,6 @@
}

$testResultMarkdown = $testResultMarkdown -replace '%TestResult%', $resultMd
Add-MtTestResultDetail -Result $testResultMarkdown
Add-MtTestResultDetail -Result $testResultMarkdown -Investigate:$investigate
return $testResult
}
2 changes: 1 addition & 1 deletion tests/cis/Test.CIS.M365.2.1.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment thread
julien-ly marked this conversation as resolved.
Resolved

#### Rationale

Expand Down
Loading
Loading