diff --git a/powershell/internal/checks/cisa/exchange/Test-MtCisaDmarcReport.ps1 b/powershell/internal/checks/cisa/exchange/Test-MtCisaDmarcReport.ps1 index cd026a75ee..a554314b2b 100644 --- a/powershell/internal/checks/cisa/exchange/Test-MtCisaDmarcReport.ps1 +++ b/powershell/internal/checks/cisa/exchange/Test-MtCisaDmarcReport.ps1 @@ -25,11 +25,7 @@ ) $acceptedDomains = Get-MtExo -Request AcceptedDomain - <# Parked domains should have DMARC with reject policy - $sendingDomains = $acceptedDomains | Where-Object {` - -not $_.SendingFromDomainDisabled - } - #> + # Every accepted domain is checked, a parked domain included: MS.EXO.4.4v1 gives no separate rule for one. $expandedDomains = @() foreach($domain in $acceptedDomains){ #This regex does NOT capture for third level domain scenarios diff --git a/powershell/tests/functions/Test-MtCisaSpfDirective.Tests.ps1 b/powershell/tests/functions/Test-MtCisaSpfDirective.Tests.ps1 new file mode 100644 index 0000000000..73f6296119 --- /dev/null +++ b/powershell/tests/functions/Test-MtCisaSpfDirective.Tests.ps1 @@ -0,0 +1,121 @@ +BeforeAll { + . "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1" + # Check functions that became native tests are not exported. + Use-MtModuleFunction -Name Test-MtCisaSpfDirective + + function New-TestSpfRecord { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'Test helper that only builds test data.')] + param( + [string]$DomainName, + # The SPF record, or the text the lookup returns when it finds none. + [string]$Record + ) + + $spfRecord = $Record + $spfLookups = @() + if ($Record -like 'v=spf1*') { + $terms = foreach ($term in ($Record -split '\s+' | Select-Object -Skip 1)) { + $isAll = $term -match '^(?[-~?+]?)all$' + [pscustomobject]@{ + directive = $term + qualifier = if ($isAll) { $Matches.qualifier } else { '' } + mechanism = if ($isAll) { 'all' } elseif ($term -like 'include:*') { 'include' } else { '' } + mechanismTarget = if ($term -like 'include:*') { $term.Substring(8) } else { '' } + modifier = if ($term -like 'redirect=*') { 'redirect' } else { '' } + } + } + $spfRecord = [pscustomobject]@{ record = $Record; terms = @($terms) } + $spfLookups = @($terms | Where-Object { $_.mechanism -eq 'include' } | ForEach-Object { + [pscustomobject]@{ SPFSourceDomain = $_.mechanismTarget; Include = $true } + }) + } + + return [pscustomobject]@{ + domain = $DomainName + spfRecord = $spfRecord + spfLookups = $spfLookups + } + } +} + +Describe 'CISA.MS.EXO.2.2 with ParkedDomains' { + BeforeEach { + $script:acceptedDomains = @() + $script:spfRecords = @{} + Mock -ModuleName Maester Get-MtExo { return $script:acceptedDomains } + Mock -ModuleName Maester Get-MailAuthenticationRecord { + return New-TestSpfRecord -DomainName $DomainName -Record $script:spfRecords[$DomainName] + } + Mock -ModuleName Maester Add-MtTestResultDetail { } + } + + It 'checks a domain that cannot send as a sending domain when it is not listed' { + $script:acceptedDomains = @([pscustomobject]@{ DomainName = 'parked.example'; IsCoexistenceDomain = $false; SendingFromDomainDisabled = $true }) + $script:spfRecords['parked.example'] = 'v=spf1 include:spf.example.net -all' + + Test-MtCisaSpfDirective | Should -BeFalse + + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { $Result -match '\| parked\.example \| ❌ Fail \| No Exchange Online directive \|' } + } + + It 'accepts for a listed parked domain' -ForEach @( + @{ Record = 'v=spf1 -all' } + @{ Record = 'v=spf1 ~all' } + @{ Record = 'v=spf1 include:spf.example.net -all' } + ) { + $script:acceptedDomains = @([pscustomobject]@{ DomainName = 'parked.example'; IsCoexistenceDomain = $false }) + $script:spfRecords['parked.example'] = $Record + + Test-MtCisaSpfDirective -ParkedDomains 'PARKED.example.' | Should -BeTrue + + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { $Result -match '\| parked\.example \| ✅ Pass \| Parked domain: fails all senders \|' } + } + + It 'fails a listed parked domain that publishes ' -ForEach @( + @{ Record = 'v=spf1 ?all'; Reason = 'policy does not end in -all or ~all' } + @{ Record = 'v=spf1 +all'; Reason = 'policy does not end in -all or ~all' } + @{ Record = 'v=spf1 include:spf.protection.outlook.com'; Reason = 'policy does not end in -all or ~all' } + @{ Record = 'Failure to obtain record'; Reason = 'Failure to obtain record' } + ) { + $script:acceptedDomains = @([pscustomobject]@{ DomainName = 'parked.example'; IsCoexistenceDomain = $false }) + $script:spfRecords['parked.example'] = $Record + + Test-MtCisaSpfDirective -ParkedDomains 'parked.example' | Should -BeFalse + + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { $Result -match ([regex]::Escape("| parked.example | ❌ Fail | Parked domain: $Reason |")) } + } + + It 'still asks a sending domain for the Exchange Online directive' { + $script:acceptedDomains = @( + [pscustomobject]@{ DomainName = 'contoso.com'; IsCoexistenceDomain = $false } + [pscustomobject]@{ DomainName = 'parked.example'; IsCoexistenceDomain = $false } + ) + $script:spfRecords['contoso.com'] = 'v=spf1 -all' + $script:spfRecords['parked.example'] = 'v=spf1 -all' + + Test-MtCisaSpfDirective -ParkedDomains 'parked.example' | Should -BeFalse + + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { $Result -match '\| contoso\.com \| ❌ Fail \|' } + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { $Result -match '\| parked\.example \| ✅ Pass \|' } + } + + It 'passes when the sending domain names Exchange Online and the parked domain fails all senders' { + $script:acceptedDomains = @( + [pscustomobject]@{ DomainName = 'contoso.com'; IsCoexistenceDomain = $false } + [pscustomobject]@{ DomainName = 'parked.example'; IsCoexistenceDomain = $false } + ) + $script:spfRecords['contoso.com'] = 'v=spf1 include:spf.protection.outlook.com -all' + $script:spfRecords['parked.example'] = 'v=spf1 -all' + + Test-MtCisaSpfDirective -ParkedDomains 'parked.example' | Should -BeTrue + } + + It 'is not supported when the only domain is parked and DNS is not available' { + $script:acceptedDomains = @([pscustomobject]@{ DomainName = 'parked.example'; IsCoexistenceDomain = $false }) + $script:spfRecords['parked.example'] = 'Unsupported platform, Resolve-DnsName not available' + + Test-MtCisaSpfDirective -ParkedDomains 'parked.example' | Should -BeNullOrEmpty + + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { $SkippedBecause -eq 'NotSupported' } + } +} diff --git a/powershell/tests/functions/Test-MtDkim.Tests.ps1 b/powershell/tests/functions/Test-MtDkim.Tests.ps1 index af59d2de3f..fc8387f904 100644 --- a/powershell/tests/functions/Test-MtDkim.Tests.ps1 +++ b/powershell/tests/functions/Test-MtDkim.Tests.ps1 @@ -45,9 +45,11 @@ Describe 'DKIM checks with no signing configuration' -ForEach @( InitialDomain = $false IsCoexistenceDomain = $false SendingFromDomainDisabled = $true + # Test-MtCisaDkim does not read SendingFromDomainDisabled: MS.EXO.3.1v1 asks for DKIM on every + # domain, so a domain that cannot send is checked like any other. ExpectedResultByCommand = @{ 'Test-MtCisDkim' = $null - 'Test-MtCisaDkim' = $null + 'Test-MtCisaDkim' = $false } } @{ diff --git a/tests/cisa/exchange/Test.CISA.MS.EXO.2.2.md b/tests/cisa/exchange/Test.CISA.MS.EXO.2.2.md index e93baa91f2..7120000f06 100644 --- a/tests/cisa/exchange/Test.CISA.MS.EXO.2.2.md +++ b/tests/cisa/exchange/Test.CISA.MS.EXO.2.2.md @@ -5,6 +5,16 @@ Rationale: An adversary may modify the `FROM` field of an email such that it app Coexistence domains related to Hybrid Configuration Wizard (HCW) are skipped. Production use of coexistence domains is discouraged, and additional controls, such as transport rules, should be used to restrict their use. +Every accepted domain is checked. A domain that sends email must name Exchange Online as an approved sender. A parked domain, one that never sends email, has no approved senders, so it only needs a policy that fails all senders, such as `v=spf1 -all` (`~all` is also accepted, as in MS.EXO.2.2v3). + +Maester does not guess which domains are parked, and turning off **Allow mail to be sent from this domain** in Exchange no longer leaves a domain out of this test. List your parked domains in the `ParkedDomains` global setting, or in this test's `ParkedDomains` parameter: + +```json +{ + "GlobalSettings": { "ParkedDomains": ["contoso-old.com"] } +} +``` + #### Remediation action SPF is not configured through the Exchange admin center, but rather via DNS records hosted by the agency's domain. Thus, the exact steps needed to set up SPF varies from agency to agency. See [Add or edit an SPF TXT record to help prevent email spam (Outlook, Exchange Online) | Microsoft Learn](https://learn.microsoft.com/microsoft-365/admin/get-help-with-domains/create-dns-records-at-any-dns-hosting-provider?view=o365-worldwide#add-or-edit-an-spf-txt-record-to-help-prevent-email-spam-outlook-exchange-online) for more details. diff --git a/tests/cisa/exchange/Test.CISA.MS.EXO.2.2.ps1 b/tests/cisa/exchange/Test.CISA.MS.EXO.2.2.ps1 index 65f90c13b3..cc931a2651 100644 --- a/tests/cisa/exchange/Test.CISA.MS.EXO.2.2.ps1 +++ b/tests/cisa/exchange/Test.CISA.MS.EXO.2.2.ps1 @@ -11,6 +11,11 @@ Returns true if SPF record exists and has at least one directive + .EXAMPLE + Test-MtCisaSpfDirective -ParkedDomains 'contoso-old.com' + + Checks contoso-old.com as a domain that never sends email: its SPF policy must end in -all or ~all + .LINK https://maester.dev/docs/commands/Test-MtCisaSpfDirective #> @@ -32,19 +37,49 @@ )] [CmdletBinding()] [OutputType([bool])] - param() + param( + # Domains that never send email. + [MaesterParameter(Setting = 'ParkedDomains')] + [string[]] $ParkedDomains = @() + ) $acceptedDomains = Get-MtExo -Request AcceptedDomain - $sendingDomains = $acceptedDomains | Where-Object {` - -not $_.SendingFromDomainDisabled - } + # A domain is parked only when it is listed. Exchange's SendingFromDomainDisabled is not read: SCuBA asks + # for an SPF policy on every domain, so a domain that is not listed gets the check for a sending domain. + $parkedDomainNames = @($ParkedDomains | ForEach-Object { "$_".Trim().TrimEnd('.') } | Where-Object { $_ }) $spfRecords = @() - foreach($domain in $sendingDomains){ + foreach($domain in $acceptedDomains){ $spfRecord = Get-MailAuthenticationRecord -DomainName $domain.DomainName -Records SPF $spfRecord | Add-Member -MemberType NoteProperty -Name "pass" -Value "Failed" -ErrorAction Ignore $spfRecord | Add-Member -MemberType NoteProperty -Name "reason" -Value "" -ErrorAction Ignore + if($domain.DomainName -in $parkedDomainNames){ + # A parked domain has no approved senders, so it has no Exchange Online directive to look for. + # MS.EXO.2.2v3 still asks for a policy that fails every other sender: -all or ~all. + $parkedRecord = $spfRecord.spfRecord + # The lookup returns a string when it finds no record or cannot run. + $allTerm = if($parkedRecord -isnot [string]){ + $parkedRecord.terms | Where-Object { $_.mechanism -eq 'all' } | Select-Object -First 1 + } + if($parkedRecord -is [string]){ + if($parkedRecord -like "*not available"){ + $spfRecord.pass = "Skipped" + } + $spfRecord.reason = "Parked domain: $parkedRecord" + }elseif($allTerm -and $allTerm.qualifier -in @('-', '~')){ + $spfRecord.pass = "Passed" + $spfRecord.reason = "Parked domain: fails all senders" + }elseif(-not $allTerm -and $parkedRecord.terms -and $parkedRecord.terms[-1].modifier -eq "redirect"){ + $spfRecord.pass = "Skipped" + $spfRecord.reason = "Parked domain: redirect modifier" + }else{ + $spfRecord.reason = "Parked domain: policy does not end in -all or ~all" + } + $spfRecords += $spfRecord + continue + } + $directives = ($spfRecord.spfLookups | Where-Object {$_.Include}).SPFSourceDomain | Select-Object -Unique $check = "spf.protection.outlook.com" -in $directives -or "outlook.com" -in $directives @@ -91,7 +126,7 @@ if($testResult){ $testResultMarkdown = "Well done. Your tenant's domains have at least 1 directives with specific mechanism targets, review authorized senders for accuracy.`n`n%TestResult%" }else{ - $testResultMarkdown = "Not all Exchange Online accepted domains designate Exchange Online as approved sender.`n`n%TestResult%" + $testResultMarkdown = "Not all Exchange Online accepted domains designate Exchange Online as approved sender or, for a parked domain, fail all senders.`n`n%TestResult%" } $passResult = "✅ Pass" diff --git a/tests/cisa/exchange/Test.CISA.MS.EXO.3.1.md b/tests/cisa/exchange/Test.CISA.MS.EXO.3.1.md index 9edd3e739f..cb2fe5fe8f 100644 --- a/tests/cisa/exchange/Test.CISA.MS.EXO.3.1.md +++ b/tests/cisa/exchange/Test.CISA.MS.EXO.3.1.md @@ -5,22 +5,12 @@ Production use of coexistence domains is discouraged, and additional controls, s Rationale: An adversary may modify the `FROM` field of an email such that it appears to be a legitimate email sent by an agency, facilitating phishing attacks. Enabling DKIM is another means for recipients to detect spoofed emails and verify the integrity of email content. -### Remediation action +Every other accepted domain is checked, including a parked domain that never sends email: MS.EXO.3.1v1 asks for DKIM on all domains and gives no separate rule for one. Turning off **Allow mail to be sent from this domain** in Exchange no longer leaves a domain out of this test, and the `ParkedDomains` global setting does not change it. -#### Option 1: Enable DKIM +### Remediation action To enable DKIM, follow the instructions listed on [Steps to Create, enable and disable DKIM from Microsoft 365 Defender portal | Microsoft Learn](https://learn.microsoft.com/microsoft-365/security/office-365-security/email-authentication-dkim-configure?view=o365-worldwide#steps-to-create-enable-and-disable-dkim-from-microsoft-365-defender-portal). -#### Option 2: Disallow mail to be sent from domain - -If the domain is not used for sending mail, we recommend disabling the ability to send from this domain. This will skip the domain for this particular test. - -1. Sign in to the [**Exchange Admin Center - Accepted Domains**](https://admin.exchange.microsoft.com/#/accepteddomains). -2. Select the domain to disable sending from. -3. Uncheck **Allow mail to be sent from this domain**. - -We recommend doing this for **\*onmicrosoft.com** domains. - ### Related links * [Defender admin center - Email authentication settings](https://security.microsoft.com/authentication?viewid=DKIM) diff --git a/tests/cisa/exchange/Test.CISA.MS.EXO.3.1.ps1 b/tests/cisa/exchange/Test.CISA.MS.EXO.3.1.ps1 index 66f29457ce..71e70f3a8c 100644 --- a/tests/cisa/exchange/Test.CISA.MS.EXO.3.1.ps1 +++ b/tests/cisa/exchange/Test.CISA.MS.EXO.3.1.ps1 @@ -42,11 +42,8 @@ $dkimSigningConfigs = Get-MtExo -Request DkimSigningConfig $acceptedDomains = Get-MtExo -Request AcceptedDomain - <# DKIM record without key for parked domains - $sendingDomains = $acceptedDomains | Where-Object {` - -not $_.SendingFromDomainDisabled - } - #> + # Every accepted domain is checked, a parked domain included: MS.EXO.3.1v1 asks for DKIM on all domains + # and gives no separate rule for a domain that never sends email. $dkimRecords = @() foreach ($domain in $acceptedDomains) { @@ -68,8 +65,8 @@ if (-not $dkimSigningConfig) { $dkimRecord = [PSCustomObject]@{ domain = $domain.DomainName - pass = if ($domain.SendingFromDomainDisabled) { 'Skipped' } elseif ($domain.InitialDomain -eq $true) { 'Passed' } else { 'Failed' } - reason = if ($domain.SendingFromDomainDisabled) { 'Parked domain' } elseif ($domain.InitialDomain -eq $true) { 'Microsoft auto-signs DKIM for the initial onmicrosoft.com domain' } else { 'No DkimSigningConfig found for domain' } + pass = if ($domain.InitialDomain -eq $true) { 'Passed' } else { 'Failed' } + reason = if ($domain.InitialDomain -eq $true) { 'Microsoft auto-signs DKIM for the initial onmicrosoft.com domain' } else { 'No DkimSigningConfig found for domain' } dkimRecord = $null } $dkimRecords += $dkimRecord @@ -100,10 +97,7 @@ $dkimRecord | Add-Member -MemberType NoteProperty -Name "pass" -Value "Failed" $dkimRecord | Add-Member -MemberType NoteProperty -Name "reason" -Value "" - if ($domain.SendingFromDomainDisabled) { - $dkimRecord.pass = 'Skipped' - $dkimRecord.reason = 'Parked domain' - } elseif (-not $dkimSigningConfig.enabled) { + if (-not $dkimSigningConfig.enabled) { $dkimRecord.pass = 'Failed' $dkimRecord.reason = 'DKIM is disabled' } elseif ($dkimRecord.dkimRecord.GetType().Name -eq 'DKIMRecord') { @@ -112,8 +106,6 @@ } else { $dkimRecord.pass = 'Passed' } - } elseif ($domain.DomainName -like '*.onmicrosoft.com') { - $dkimRecord.reason = "Recommendation: Disable sending from domain" } elseif ($dkimRecord.dkimRecord -like "*not available") { $dkimRecord.pass = "Skipped" $dkimRecord.reason = $dkimRecord.dkimRecord diff --git a/tests/cisa/exchange/Test.CISA.MS.EXO.4.2.ps1 b/tests/cisa/exchange/Test.CISA.MS.EXO.4.2.ps1 index 0fb777248d..daa694dc33 100644 --- a/tests/cisa/exchange/Test.CISA.MS.EXO.4.2.ps1 +++ b/tests/cisa/exchange/Test.CISA.MS.EXO.4.2.ps1 @@ -38,11 +38,7 @@ ) $acceptedDomains = Get-MtExo -Request AcceptedDomain - <# Parked domains should have DMARC with reject policy - $sendingDomains = $acceptedDomains | Where-Object {` - -not $_.SendingFromDomainDisabled - } - #> + # Every accepted domain is checked, a parked domain included: MS.EXO.4.2v1 asks for p=reject on all domains. $expandedDomains = @() foreach($domain in $acceptedDomains){ # If it's the coexistence domain (contoso.mail.onmicrosoft.com), skip the 2nd level check. diff --git a/tests/cisa/exchange/Test.CISA.MS.EXO.4.3.ps1 b/tests/cisa/exchange/Test.CISA.MS.EXO.4.3.ps1 index 4604f074fe..814454007e 100644 --- a/tests/cisa/exchange/Test.CISA.MS.EXO.4.3.ps1 +++ b/tests/cisa/exchange/Test.CISA.MS.EXO.4.3.ps1 @@ -45,11 +45,7 @@ ) $acceptedDomains = Get-MtExo -Request AcceptedDomain - <# Parked domains should have DMARC with reject policy - $sendingDomains = $acceptedDomains | Where-Object {` - -not $_.SendingFromDomainDisabled - } - #> + # Every accepted domain is checked, a parked domain included: MS.EXO.4.3v1 gives no separate rule for one. $tldMatch = "^.*\.(?'tld'.*)$" $govDomains = $acceptedDomains | Where-Object {` $_ -imatch $tldMatch|Out-Null; diff --git a/website/src/data/test-builder.json b/website/src/data/test-builder.json index b3877a76d2..2b025d35ea 100644 --- a/website/src/data/test-builder.json +++ b/website/src/data/test-builder.json @@ -18524,6 +18524,11 @@ "type": "int", "tests": 1 }, + { + "name": "ParkedDomains", + "type": "string[]", + "tests": 1 + }, { "name": "PolicyFiltering", "type": "string",