Skip to content

Instantly share code, notes, and snippets.

@mertcangokgoz
Created August 31, 2022 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mertcangokgoz/6c7c18bbc1b7da7fab2f328783edb017 to your computer and use it in GitHub Desktop.
Save mertcangokgoz/6c7c18bbc1b7da7fab2f328783edb017 to your computer and use it in GitHub Desktop.
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)
@staticmethod
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
@staticmethod
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
@staticmethod
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):
assets.append(item)
return assets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment