Skip to content

Instantly share code, notes, and snippets.

Created August 31, 2022 03:12
What would you like to do?
Simple SPF Checker via dnspython
import dns.resolver # pip install dnspython
class SpfCheck:
def __init__(self, domain: str) -> None:
self.domain = domain
self.spf_record = self.get_spf_record(domain)
self.parsed_spf = self.get_assets(self.spf_record)
self.ip_address = self.enumerate_ips(self.spf_record)
def get_spf_record(domain: str) -> str:
records = dns.resolver.resolve(
domain, "TXT"
for record in records:
record = record.strings[0].decode("utf-8")
if record.startswith("v=spf1"):
return record
def enumerate_ips(spf_record: str) -> list:
ips = []
spf_values = spf_record.split(" ")
for item in spf_values:
if item.startswith("ip4:"):
ips.append(item.replace("ip4:", ""))
elif item.startswith("ip6:"):
ips.append(item.replace("ip6:", ""))
return ips
def get_assets(spf_record: str) -> list:
assets = []
spf_values = spf_record.split(" ")
# List of all mechanisms as part of SPF standard except for ip4: and ip6:
mechanisms = ['ptr:', 'include:', 'a:', 'include:', 'mx:', 'exists:', 'all', 'redirect=']
for item in spf_values:
# Check for SPF mechanisms in each part of SPF record
if any(mechanism in item for mechanism in mechanisms):
return assets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment