DomainHealthChecker.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

<#PSScriptInfo
 
.VERSION 1.3
 
.GUID 829d6e7f-2d4c-40de-9ead-36e508258b89
 
.AUTHOR Martien van Dijk
 
.COMPANYNAME Avantage IT
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 Is your email domain properly protected against abuse, such as email spoofing? This form of abuse can cause (image) damage to an organization. The PowerShell script DomainHealthChecker.ps1 checks the SPF, DKIM and DMARC record of one or more email domains and gives advice if necessary.
 
#>
 

Param()


#Requires -Version 7
#$ErrorActionPreference = "SilentlyContinue"
function DomainHealthChecker {
    [CmdletBinding()]
    param (
        # Check a single domain
        [Parameter(
            Mandatory, ParameterSetName = 'domain',
            ValueFromPipeline = $True,
            ValueFromPipelineByPropertyName = $True,
            HelpMessage = "Specify domain name whose SPF, DMARC and DKIM record should be checked.",
            Position = 1)]
        [string]$Name,

        # Check domains from a file
        [Parameter(
            Mandatory, ParameterSetName = 'file',
            ValueFromPipeline = $True,
            ValueFromPipelineByPropertyName = $True,
            HelpMessage = "Specifies file name which contains a list of domain names.",
            Position = 2)]
        [System.IO.FileInfo]$File,

        # DNS Server to use
        [Parameter(Mandatory = $false,
            Position = 3)]
        [string]$Server = "1.1.1.1"
    )

    begin {

        class SpfDkimDmarc {
            [string]$Name
            [string]$SPFRecord
            [string]$SpfAdvisory
            [string]$DmarcRecord
            [string]$DmarcAdvisory
            [string]$DkimRecord
            [string]$DkimAdvisory
        
            # Constructor: Created the object with the SPF, DMARC and DKIM values
            SpfDkimDmarc (
                [string]$d, 
                [string]$SPF,
                [string]$SpfAdvisory,
                [string]$DMARC,
                [string]$DmarcAdvisory,
                [string]$DKIM,
                [string]$DkimAdvisory
            ) {
                $this.Name = $d
                $this.SPFRecord = $SPF
                $this.SpfAdvisory = $SpfAdvisory
                $this.DmarcRecord = $DMARC
                $this.DmarcAdvisory = $DmarcAdvisory
                $this.DkimRecord = $DKIM
                $this.DkimAdvisory = $DkimAdvisory
            }
        }
    }

    process {
        if ($file) {
            if (-not(Test-Path $file)) {
                Write-error "$($file) does not exist"
                return
            }
        }

        function StartDomainHealthCheck($domain) {

            # Check SPF-record
            $SPF = Resolve-DnsName -type TXT -name $Domain -server $Server -ErrorAction SilentlyContinue | where-object { $_.strings -match "v=spf1" } | Select-Object -ExpandProperty strings
            $SPFCount = ($SPF | Measure-Object).Count
            if ($SPFCount -eq 0) {
                $SpfAdvisory = "No SPF-record found"
            }
            elseif ($SPF -is [array]) {
                $SpfAdvisory = "Domain has more than one SPF-record. One SPF record for one domain. This is explicitly defined in RFC4408"
            }
            elseif ($SPF -notmatch "-all") {
                $SpfAdvisory = "An SPF-record is configured but the policy is not sufficiently strict"
            }
            elseif ($SPF -match "-all") {
                $SpfAdvisory = "An SPF-record is configured and the policy is sufficiently strict."
            }
            elseif ($SPF -match "^?all") {
                $SpfAdvisory = "Your domain has a valid SPF record but your policy is not effective enough."
            }
            Elseif ($SPF -notmatch "v=spf1" -or "all") {
                $SpfAdvisory = "No SPF-record configured."
            }

            # Check DKIM-record
            $DKIM = $null
            $CnameSelector1 = Resolve-DnsName -Type CNAME -Name selector1._domainkey.$Domain -server $Server -ErrorAction SilentlyContinue
            if ($CnameSelector1.NameHost -notcontains "domainkey") {
                $DkimAdvisory = "We couldn't find a DKIM record associated with your domain."
            }
            else {
                $DKIM = Resolve-DnsName -Type TXT -Name $CnameSelector1.namehost -server $Server | Select-Object -ExpandProperty strings
                if ($DKIM -like "") {
                    $DkimAdvisory = "We couldn't find a DKIM record associated with your domain."
                }
                elseif ($DKIM -match "v=DKIM1") {
                    $DkimAdvisory = "DKIM-record is valid."
                } 
            }
        
            # Check DMARC-record
            $DMARC = Resolve-DnsName -type TXT -name _dmarc.$Domain -Server $Server -ErrorAction SilentlyContinue | where-object { $_.strings -match "v=DMARC1" } | Select-Object -ExpandProperty strings
            if ($DMARC -like "") {
                $DmarcAdvisory = "Does not have a DMARC record. This domain is at risk to being abused by phishers and spammers."
            }
            elseif ($DMARC -match "p=quarantine") {
                $DmarcAdvisory = "Domain has a DMARC record and it is set to p=quarantine. To fully take advantage of DMARC, the policy should be set to p=reject."
            }
            elseif ($DMARC -match "p=reject") {
                $DmarcAdvisory = "Domain has a DMARC record and your DMARC policy will prevent abuse of your domain by phishers and spammers."
            }
            elseif ($DMARC -match "p=none") {
                $DmarcAdvisory = "Domain has a valid DMARC record but the DMARC (subdomain) policy does not prevent abuse of your domain by phishers and spammers."
            }

            $ReturnValues = [SpfDkimDmarc]::New($Domain, $SPF, $SpfAdvisory, $DMARC, $DmarcAdvisory, $DKIM, $DkimAdvisory)
            $ReturnValues
        }

        if ($file) {
            foreach ($Domain in (Get-Content -Path $file)) {
                StartDomainHealthCheck -Domain $Domain
            }
        }
        if ($Name) {
            foreach ($Domain in $Name) {
                StartDomainHealthCheck -Domain $Name
            }
        }      
    }
    end {}
}