<# .SYNOPSIS Checks that Exchange Online, DNS and the relay are set up correctly for Innovarro Signatures. .DESCRIPTION Read-only. Prints PASS / WARN / FAIL for: * the outbound connector, inbound connector and transport rule (and its scope) * transport rules that run earlier and stop rule processing * SPF of every sending domain (must allow the relay; includes and redirects are followed) and its DMARC policy * DKIM signing per domain * network connection to the relay on port 25 (Windows only) With -TestRecipient it also validates the outbound connector by sending a test message. .EXAMPLE .\Test-InnovarroSignatures.ps1 .\Test-InnovarroSignatures.ps1 -TestRecipient me@gmail.com .NOTES Innovarro Signatures - https://signatures.innovarro.cloud/support #> [CmdletBinding()] param( [string]$TestRecipient, [string]$RelayHost = 'relay.signatures.innovarro.cloud', [string[]]$RelayIPs = @('102.213.7.195') ) $ErrorActionPreference = 'Stop' if (-not (Get-Command Get-ConnectionInformation -ErrorAction SilentlyContinue)) { throw 'The ExchangeOnlineManagement module is not installed. Run: Install-Module ExchangeOnlineManagement -Scope CurrentUser' } if (-not (Get-ConnectionInformation | Where-Object State -eq 'Connected')) { Connect-ExchangeOnline -ShowBanner:$false } $checks = [System.Collections.Generic.List[object]]::new() function Add-Check($area, $result, $detail) { $checks.Add([pscustomobject]@{ Check = $area; Result = $result; Detail = $detail }) } function Get-Txt([string]$name) { try { if (Get-Command Resolve-DnsName -ErrorAction SilentlyContinue) { return @(Resolve-DnsName -Name $name -Type TXT -DnsOnly -ErrorAction Stop | Where-Object Type -eq 'TXT' | ForEach-Object { $_.Strings -join '' }) } return @(nslookup -type=TXT $name 2>$null | Select-String '"(.*)"' | ForEach-Object { ($_.Matches | ForEach-Object { $_.Groups[1].Value }) -join '' }) } catch { return @() } } function Get-DnsValues([string]$name, [string]$type) { # A / MX lookups for SPF 'a' and 'mx' terms. Needs Resolve-DnsName (Windows); elsewhere these terms count as no match. if (-not (Get-Command Resolve-DnsName -ErrorAction SilentlyContinue)) { return @() } try { $r = Resolve-DnsName -Name $name -Type $type -DnsOnly -ErrorAction Stop | Where-Object Type -eq $type if ($type -eq 'MX') { return @($r | ForEach-Object { $_.NameExchange }) } return @($r | ForEach-Object { $_.IPAddress }) } catch { return @() } } function Test-InCidr([string]$ip, [string]$cidr) { $parts = $cidr.Split('/') $netIp = $null; $addr = $null if (-not [System.Net.IPAddress]::TryParse($parts[0], [ref]$netIp) -or -not [System.Net.IPAddress]::TryParse($ip, [ref]$addr)) { return $false } if ($netIp.AddressFamily -ne $addr.AddressFamily -or $addr.AddressFamily -ne 'InterNetwork') { return $false } $bits = if ($parts.Count -gt 1) { [int]$parts[1] } else { 32 } $a = [System.BitConverter]::ToUInt32(($addr.GetAddressBytes()[3..0]), 0) $n = [System.BitConverter]::ToUInt32(($netIp.GetAddressBytes()[3..0]), 0) if ($bits -eq 0) { return $true } $mask = [uint32]([math]::Pow(2, 32) - [math]::Pow(2, 32 - $bits)) return (($a -band $mask) -eq ($n -band $mask)) } # SPF result for mail from $ip with $domain as sender: pass, fail, softfail, neutral, none or permerror (RFC 7208, # without macros). $state counts DNS lookups across includes (the limit is 10). function Get-SpfResult([string]$domain, [string]$ip, $state) { $recs = @(Get-Txt $domain | Where-Object { $_ -match '^v=spf1(\s|$)' }) if ($recs.Count -eq 0) { return 'none' } if ($recs.Count -gt 1) { $state.Detail = "$domain has more than one SPF record"; return 'permerror' } $redirect = $null foreach ($term in ($recs[0] -split '\s+' | Select-Object -Skip 1)) { if (-not $term) { continue } if ($term -match '^redirect=(.+)$') { $redirect = $Matches[1]; continue } if ($term -match '^[a-z][a-z0-9_.-]*=') { continue } if ($term -notmatch '^([+\-~?]?)([a-z0-9]+)(?::([^/]*))?(/\d+)?$') { $state.Detail = "invalid term '$term'"; return 'permerror' } $q = switch ($Matches[1]) { '-' { 'fail' } '~' { 'softfail' } '?' { 'neutral' } default { 'pass' } } $mech = $Matches[2].ToLower(); $arg = $Matches[3]; $prefix = $Matches[4] if ($mech -in 'include', 'a', 'mx', 'exists', 'ptr') { $state.Lookups++ if ($state.Lookups -gt 10) { $state.Detail = 'more than 10 DNS lookups (SPF limit)'; return 'permerror' } } $hit = $false switch ($mech) { 'all' { $hit = $true } 'ip4' { $hit = Test-InCidr $ip "$arg$prefix" } 'ip6' { $hit = $false } 'include' { $r = Get-SpfResult $arg $ip $state if ($r -eq 'pass') { $hit = $true } elseif ($r -in 'none', 'permerror') { if (-not $state.Detail) { $state.Detail = "include:$arg is broken" }; return 'permerror' } } 'a' { $name = if ($arg) { $arg } else { $domain } $hit = @(Get-DnsValues $name 'A' | Where-Object { Test-InCidr $ip "$_$prefix" }).Count -gt 0 } 'mx' { $name = if ($arg) { $arg } else { $domain } foreach ($h in (Get-DnsValues $name 'MX')) { if (@(Get-DnsValues $h 'A' | Where-Object { Test-InCidr $ip "$_$prefix" }).Count) { $hit = $true; break } } } default { } } if ($hit) { return $q } } if ($redirect) { $state.Lookups++ if ($state.Lookups -gt 10) { $state.Detail = 'more than 10 DNS lookups (SPF limit)'; return 'permerror' } return Get-SpfResult $redirect $ip $state } return 'neutral' } function Get-DmarcPolicy([string]$domain) { $rec = @(Get-Txt "_dmarc.$domain" | Where-Object { $_ -match '^v=DMARC1' }) | Select-Object -First 1 if (-not $rec) { return $null } if ($rec -match '(?:^|;)\s*p\s*=\s*(none|quarantine|reject)') { return $Matches[1].ToLower() } return $null } # --- Connectors ---------------------------------------------------------------------------------------------- $out = Get-OutboundConnector -Identity 'Innovarro Signatures' -ErrorAction SilentlyContinue if (-not $out) { Add-Check 'Outbound connector' 'FAIL' 'Missing - run Install-InnovarroSignatures.ps1' } else { $problems = @() if (-not $out.Enabled) { $problems += 'disabled' } if (-not $out.IsTransportRuleScoped) { $problems += 'not limited to the transport rule (would route ALL mail)' } if (($out.SmartHosts -join ',') -notmatch [regex]::Escape($RelayHost)) { $problems += "smart host is $($out.SmartHosts -join ',')" } if ($out.TlsSettings -notin 'DomainValidation', 'CertificateValidation') { $problems += "TLS setting is $($out.TlsSettings)" } if ($problems) { Add-Check 'Outbound connector' 'FAIL' ($problems -join '; ') } else { Add-Check 'Outbound connector' 'PASS' "-> $RelayHost, TLS $($out.TlsSettings)" } } $in = Get-InboundConnector -Identity 'Innovarro Signatures - return' -ErrorAction SilentlyContinue if (-not $in) { Add-Check 'Inbound connector' 'FAIL' 'Missing - run Install-InnovarroSignatures.ps1' } else { $problems = @() if (-not $in.Enabled) { $problems += 'disabled' } if ($in.ConnectorType -ne 'OnPremises') { $problems += "type is $($in.ConnectorType), must be OnPremises" } if ("$($in.TlsSenderCertificateName)" -notmatch [regex]::Escape($RelayHost)) { $problems += "certificate name is '$($in.TlsSenderCertificateName)'" } if (-not $in.RequireTls) { $problems += 'TLS not required' } if ($problems) { Add-Check 'Inbound connector' 'FAIL' ($problems -join '; ') } else { Add-Check 'Inbound connector' 'PASS' "certificate $RelayHost" } } # --- Transport rule ------------------------------------------------------------------------------------------ $rules = @(Get-TransportRule | Sort-Object Priority) $rule = $rules | Where-Object Name -eq 'Innovarro Signatures' if (-not $rule) { Add-Check 'Transport rule' 'FAIL' 'Missing - run Install-InnovarroSignatures.ps1' } else { $scope = if ($rule.From) { "pilot: $($rule.From -join ', ')" } elseif ($rule.FromMemberOf) { "group: $($rule.FromMemberOf -join ', ')" } else { 'all users' } if ($rule.SentToScope -eq 'NotInOrganization') { $scope += ', external only' } if ($rule.ExceptIfFrom) { $scope += "; excluded: $($rule.ExceptIfFrom -join ', ')" } if ($rule.State -ne 'Enabled') { Add-Check 'Transport rule' 'WARN' "Disabled - mail is not routed through the relay ($scope)" } elseif ($rule.Mode -ne 'Enforce') { Add-Check 'Transport rule' 'WARN' "Mode is $($rule.Mode), must be Enforce" } else { Add-Check 'Transport rule' 'PASS' "Enabled, priority $($rule.Priority), $scope" } if ($rule.ExceptIfHeaderMatchesMessageHeader -ne 'X-Innovarro-Signature') { Add-Check 'Loop prevention' 'FAIL' 'Rule lacks the exception for header X-Innovarro-Signature - re-run Install-InnovarroSignatures.ps1' } else { Add-Check 'Loop prevention' 'PASS' 'Exception for X-Innovarro-Signature present' } $blocking = $rules | Where-Object { $_.Priority -lt $rule.Priority -and $_.StopRuleProcessing -and $_.State -eq 'Enabled' } if ($blocking) { Add-Check 'Earlier rules' 'WARN' "Stop processing before ours (matching mail is not signed): $($blocking.Name -join ', ')" } else { Add-Check 'Earlier rules' 'PASS' 'None stop rule processing' } } # --- DNS: SPF, DMARC and DKIM per sending domain ------------------------------------------------------------------- # Exchange Online checks SPF and DMARC on mail the relay hands back. When the relay adds a signature it removes the # original DKIM signature, so SPF must allow the relay; with DMARC p=reject the mail otherwise bounces (550 5.7.509). $domains = Get-AcceptedDomain | Where-Object { $_.DomainType -eq 'Authoritative' -and $_.DomainName -notlike '*.onmicrosoft.com' } | ForEach-Object { $_.DomainName.ToString() } foreach ($d in $domains) { $spf = @(Get-Txt $d | Where-Object { $_ -match '^v=spf1(\s|$)' }) $dmarc = Get-DmarcPolicy $d $enforcing = $dmarc -in 'reject', 'quarantine' $policy = if ($dmarc) { "DMARC p=$dmarc" } else { 'no DMARC record' } $notAllowed = @() $detail = $null foreach ($ip in $RelayIPs) { $state = [pscustomobject]@{ Lookups = 0; Detail = $null } $r = Get-SpfResult $d $ip $state if ($r -ne 'pass') { $notAllowed += $ip; $detail = "result $r$(if ($state.Detail) { ": $($state.Detail)" })" } } if (-not $spf.Count) { Add-Check "SPF $d" $(if ($enforcing) { 'FAIL' } else { 'WARN' }) "No SPF record found (or DNS lookup not possible here); $policy" } elseif ($notAllowed) { $effect = if ($dmarc -eq 'reject') { 'returned mail will bounce (550 5.7.509)' } elseif ($dmarc -eq 'quarantine') { 'returned mail will go to Junk' } else { 'returned mail fails SPF' } Add-Check "SPF $d" $(if ($enforcing) { 'FAIL' } else { 'WARN' }) "Relay not allowed ($detail) - add ip4:$($notAllowed -join ' ip4:'); $policy, so $effect. Record: $($spf -join ' | ')" } else { Add-Check "SPF $d" 'PASS' "Allows the relay; $policy" } $dkim = Get-DkimSigningConfig -Identity $d -ErrorAction SilentlyContinue if ($dkim -and $dkim.Enabled) { Add-Check "DKIM $d" 'PASS' 'Signing with own domain' } else { Add-Check "DKIM $d" 'WARN' 'Not signing with own domain - run Enable-InnovarroDkim.ps1' } } # --- Network --------------------------------------------------------------------------------------------------- if (Get-Command Test-NetConnection -ErrorAction SilentlyContinue) { $tcp = Test-NetConnection -ComputerName $RelayHost -Port 25 -WarningAction SilentlyContinue if ($tcp.TcpTestSucceeded) { Add-Check 'Relay reachable' 'PASS' "$RelayHost port 25" } else { Add-Check 'Relay reachable' 'WARN' "Could not reach $RelayHost on port 25 from this computer (many networks block outbound 25; Exchange Online is not affected)" } } if ($TestRecipient -and $out) { try { $v = Validate-OutboundConnector -Identity 'Innovarro Signatures' -Recipients $TestRecipient $ok = ($v | Where-Object { -not $_.IsTaskSuccessful }).Count -eq 0 Add-Check 'Connector validation' $(if ($ok) { 'PASS' } else { 'FAIL' }) (($v | ForEach-Object { $_.Detail }) -join ' ') } catch { Add-Check 'Connector validation' 'FAIL' $_.Exception.Message } } $checks | ForEach-Object { $color = @{ PASS = 'Green'; WARN = 'Yellow'; FAIL = 'Red' }[$_.Result] Write-Host ('{0,-5} {1,-28} {2}' -f $_.Result, $_.Check, $_.Detail) -ForegroundColor $color } $fails = @($checks | Where-Object Result -eq 'FAIL').Count Write-Host "`n$fails failure(s). Guides: https://signatures.innovarro.cloud/support" -ForegroundColor $(if ($fails) { 'Red' } else { 'Green' })