Skip to content

Instantly share code, notes, and snippets.

@nickumia-reisys
Created March 13, 2023 16:41
Show Gist options
  • Save nickumia-reisys/7163dc0ad44d9ec14a3d84a1d7bf4974 to your computer and use it in GitHub Desktop.
Save nickumia-reisys/7163dc0ad44d9ec14a3d84a1d7bf4974 to your computer and use it in GitHub Desktop.
import json
import xmltodict
with open('dmarc.xml', 'rb') as d:
dmarc = xmltodict.parse(d)
success = 0
fail = 0
for i in range(len(dmarc['feedback']['record'])):
source = dmarc['feedback']['record'][i]['row']['source_ip']
dkim = dmarc['feedback']['record'][i]['row']['policy_evaluated']['dkim']
spf = dmarc['feedback']['record'][i]['row']['policy_evaluated']['spf']
spf_specific = dmarc['feedback']['record'][i]['auth_results']['spf']['result']
domain = dmarc['feedback']['record'][i]['identifiers']['header_from']
dkim_trail = [j['domain'] for j in dmarc['feedback']['record'][i]['auth_results']['dkim']] # if j['result'] == 'fail']
spf_trail = dmarc['feedback']['record'][i]['auth_results']['spf']['domain']
count = int(dmarc['feedback']['record'][i]['row']['count'])
if dkim != 'pass' or spf != 'pass':
fail += count
print(source, '\tdkim: ', dkim, '\tspf: ', spf_specific, domain, dkim_trail, spf_trail)
else:
success += count
print(source, '\tdkim: ', dkim, '\tspf: ', spf_specific, domain)
print("Success Rate %d/%d = %.2f" % (success, (success+fail), success/(success+fail)))
@nickumia-reisys
Copy link
Author

Assuming dmarc report is saved to dmarc.xml

pip install xml2dict
python dmarc.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment