requires pip install dkimpy from pprint import pprint import json from

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# requires `pip install dkimpy`
from pprint import pprint
import json
from dkim import load_pk_from_dns
# input:
# {"name":"selector1-contechcorp-net._domainkey.netorg872434.onmicrosoft.com","class":"IN","status":"NOERROR","timestamp":"2020-03-06T13:15:45+01:00","data":{"answers":[{"ttl":3600,"type":"TXT","class":"IN","name":"selector1-contechcorp-net._domainkey.netorg872434.onmicrosoft.com","answer":"v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDPvmtTpFxJ0tDOeS45eeejItc5OGrJJnGwdSyhUrWfhW5vFEbzE3gWds1SvR01ua0Zu0XO3fdKtVFdPOzq51NlFdP+dkSKaD3JsBEX72XhGLJBPesCjFkTpfpW2xEHaek+1zmJ9MjRqpmW3xZhEgFHYYUfsHxHRTGBcO+kFAw83QIDAQAB; n=1024,1449771495,1"}],"additionals":[],"authorities":[],"protocol":"udp","flags":{"response":true,"opcode":0,"authoritative":false,"truncated":false,"recursion_desired":true,"recursion_available":true,"authenticated":false,"checking_disabled":false,"error_code":0}}}
# output:
#{"name":"default._domainkey.tmcoutureireland.com","key":"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxyQKNCDcYk+cX1uKzu2bFUMyt0KfemcQILV8OgXyH4gS1Fu7hvKy5G5Sn98k3RsdQ5QxUGXogZCFRWbD7a1QhESIhaT/XzNdPoV1+4K7yR7wMgYsZfezLQh+W6j1cXNEzOe2zfxido6iHvs/hUsO3S+pHMIYp7tvC2HarrJd9bf9mv77jB1CQYYIGTcivilPKmLwxNrojffdURiq62K8jypVhngBB+Lwcmzt9CqYW6FArUI7mOBMYFSKsBMiNlIBgxOreOf2qDGC6amJshCP99JLMAIxB4bsazfmVXVOHg+6GT75G361unEzpfklTLbF2O6saYG/f/lXYUn19J19ZwIDAQAB","bits":2048,"answer":"v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxyQKNCDcYk+cX1uKzu2bFUMyt0KfemcQILV8OgXyH4gS1Fu7hvKy5G5Sn98k3RsdQ5QxUGXogZCFRWbD7a1QhESIhaT/XzNdPoV1+4K7yR7wMgYsZfezLQh+W6j1cXNEzOe2zfxido6iHvs/hUsO3S+pHMIYp7tvC2HarrJd9bf9mv77jB1CQYYIGTcivilPK\nmLwxNrojffdURiq62K8jypVhngBB+Lwcmzt9CqYW6FArUI7mOBMYFSKsBMiNlIBgxOreOf2qDGC6amJshCP99JLMAIxB4bsazfmVXVOHg+6GT75G361unEzpfklTLbF2O6saYG/f/lXYUn19J19ZwIDAQAB;"}
def iter_records():
with open('var/dkim.json') as inp:
for line in inp:
item = json.loads(line.strip())
for answer in item.get('data', {}).get('answers', []):
if answer.get('type') == 'TXT' and 'v=DKIM1' in answer.get('answer', ''):
yield answer
def main():
for rec in iter_records():
print(rec)
pk = rec['answer'].split('p=')[1].split(';')[0]
pk_data, keysize, ktag, foo = load_pk_from_dns(
rec['name'],
dnsfunc=lambda *x, **y: rec['answer'],
)
res = {
'name': rec['name'],
'bits': keysize,
'answer': rec['answer'],
}
print(res)
print('----')
if __name__ == '__main__':
main()