Skip to content

Instantly share code, notes, and snippets.

@chrisjones
Created June 25, 2021 21:31
Show Gist options
  • Save chrisjones/be3cd206717e0c0dedcc3496c7dfd8d5 to your computer and use it in GitHub Desktop.
Save chrisjones/be3cd206717e0c0dedcc3496c7dfd8d5 to your computer and use it in GitHub Desktop.
SPF - DMARC - DKIM scanner
#!/usr/bin/python
## Dependencies
import re
import subprocess, sys
## Variables
selectors = ["selector1", "selector2", "google", "default", "dkim", "k1", "mail", "mxvault" ]
## Functions
def execute_read_and_report(cmd, match_string, label):
## run it ##
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, err) = p.communicate()
p_status = p.wait()
## print results
if re.search(match_string, output.decode('utf-8')):
print(" %s: True" % label)
if label == "DKIM":
print()
exit()
else:
if label != "DKIM":
print(" %s: False" % label)
# get the command line argument, check if it exists
if len(sys.argv) < 2:
print("The domain is missing from the command line.")
exit()
domain = sys.argv[1]
print()
print("Checking domain:", domain)
print("----------------")
## first command to run ##
cmd = "/usr/bin/dig -t TXT +short %s" % domain
execute_read_and_report(cmd, "v=spf1", "SPF")
## second command to run ##
cmd = "/usr/bin/dig -t TXT +short _dmarc.%s" % domain
execute_read_and_report(cmd, "v=DMARC1;", "DMARC")
## third command to run ##
for selector in selectors:
cmd = "/usr/bin/dig -t TXT +short %s._domainkey.%s" % (selector, domain)
execute_read_and_report(cmd, "v=DKIM1;", "DKIM")
print(" %s: False" % "DKIM")
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment