Skip to content

Instantly share code, notes, and snippets.

@benoit74

benoit74/run.py Secret

Created August 21, 2023 08:33
Show Gist options
  • Save benoit74/35cb8b01d3a6aa4a91ad985a5de9ed57 to your computer and use it in GitHub Desktop.
Save benoit74/35cb8b01d3a6aa4a91ad985a5de9ed57 to your computer and use it in GitHub Desktop.
Dig into SPF records
import pydig
from dataclasses import dataclass
def get_spf_record(domain: str) -> str:
txts = pydig.query(domain, 'TXT')
result = list(filter(lambda x: "v=spf" in x, txts))
if len(result) == 0:
raise Exception(f"SPF not found for {domain}")
elif len(result) > 1:
raise Exception(f"too many SPF found for {domain}")
return result[0]
def get_included_domains(spf_record: str) -> list[str]:
return list(
map(lambda x: x[8:],
filter(lambda x: x.startswith("include:"), spf_record.split(" "))
)
)
@dataclass
class IncludedDomain:
indent: int
domain: str
queries_to_do = []
queries_to_do.append(IncludedDomain(0, "kiwix.org"))
while len(queries_to_do) > 0:
included_domain = queries_to_do.pop()
spf_record = get_spf_record(included_domain.domain)
sub_domains = get_included_domains(spf_record)
records = spf_record.split(" ")
spaces = [" " for x in range(included_domain.indent)]
text = spaces + [f"{included_domain.domain}: {len(records)} instructions, among which {len(sub_domains)} includes"]
print("".join(text))
for sub_domain in sub_domains:
queries_to_do.append(IncludedDomain(included_domain.indent + 1, sub_domain))
# print(get_included_domains(get_spf_record(included_domain.domain)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment