Rules/MAIL-003.dmarc-enforced.psd1

@{
    Id          = 'MAIL-003'
    Title       = 'DMARC is published and enforced for every custom mail domain'
    Area        = 'EmailAuthDns'
    Severity    = 'High'
    References  = @(
        'CIS Microsoft 365 Foundations Benchmark (DMARC records)'
        'https://learn.microsoft.com/defender-office-365/email-authentication-dmarc-configure'
    )
    Rationale   = 'DMARC tells receiving servers what to do with mail that fails SPF/DKIM. A policy of none only monitors; quarantine or reject actually stops spoofing.'
    Remediation = 'Publish a _dmarc TXT record with at least p=quarantine (ideally p=reject) and rua reporting, after validating SPF and DKIM alignment.'
    Test        = {
        param($Snapshot)
        $customDomains = @($Snapshot.EmailAuthDns | Where-Object { -not $_.isOnMicrosoft })
        # Older snapshots have no isMailDomain field - treat those as mail domains.
        $mailDomains = @($customDomains | Where-Object {
                -not $_.PSObject.Properties['isMailDomain'] -or [bool]$_.isMailDomain
            })
        $nonMailDomains = @($customDomains | Where-Object {
                $_.PSObject.Properties['isMailDomain'] -and -not [bool]$_.isMailDomain
            })
        $notes = @()
        if ($nonMailDomains.Count -gt 0) {
            $notes = @('Not evaluated (no Email service): ' + (@($nonMailDomains | ForEach-Object { $_.domain }) -join ', ') +
                '. Consider publishing DMARC p=reject on non-mail domains as spoofing protection.')
        }
        if ($mailDomains.Count -eq 0) {
            return New-TLRuleResult -Status Pass -Evidence (@('No custom mail domains to check.') + $notes)
        }
        $bad = @($mailDomains | Where-Object {
                -not ($_.dmarc.present -and (@('quarantine', 'reject') -contains [string]$_.dmarc.policy))
            })
        if ($bad.Count -gt 0) {
            New-TLRuleResult -Status Fail -Evidence (@($bad | ForEach-Object {
                        $detail = 'no DMARC record'
                        if ($_.dmarc.present) { $detail = "policy 'p=$($_.dmarc.policy)'" }
                        "$($_.domain): $detail"
                    }) + $notes)
        }
        else {
            New-TLRuleResult -Status Pass -Evidence (@($mailDomains | ForEach-Object { "$($_.domain): p=$($_.dmarc.policy)" }) + $notes)
        }
    }
}