-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetdkimselector
More file actions
executable file
·75 lines (66 loc) · 2.19 KB
/
getdkimselector
File metadata and controls
executable file
·75 lines (66 loc) · 2.19 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python3
#
# WARNING: Script is now developed in the keymonitor repository and will soon be removed here:
# https://github.com/badkeys/keymonitor/blob/main/dkim/getdkimselector
import argparse
import email.parser
import mailbox
import os
allheaders = [
"DKIM-Signature",
"ARC-Seal",
"ARC-Message-Signature",
"X-Google-DKIM-Signature",
"X-Original-DKIM-Signature",
"DomainKey-Signature",
]
def parse_mail(em):
for xheader in allheaders:
sigs = em.get_all(xheader)
if sigs is None:
continue
for header in sigs:
sig = str(header)
sig = sig.translate({ord(c): None for c in "\n\r\t "})
dkim = {}
for f in sig.split(";"):
fs = f.split("=", 2)
if len(fs) != 2:
continue
dkim[fs[0]] = fs[1]
if args.verbose:
print(sig)
print(dkim)
if ("d" not in dkim) or ("s" not in dkim):
# looks invalid
continue
if (dkim["s"] == "") or (dkim["s"].isascii() is False):
continue
newhost = f"{dkim['s']}._domainkey.{dkim['d']}"
if newhost not in hosts:
hosts.add(newhost)
print(newhost)
ap = argparse.ArgumentParser()
ap.add_argument("paths", nargs="+")
ap.add_argument("-v", "--verbose", action="store_true")
ap.add_argument("-m", "--mbox", action="store_true")
args = ap.parse_args()
hosts = set()
for path in args.paths:
for root, _, files in os.walk(path):
for name in files:
fn = os.path.join(root, name)
if args.mbox:
mb = mailbox.mbox(fn)
try:
for m in mb.iteritems():
parse_mail(m[1])
except UnicodeDecodeError:
pass
else: # maildir mode
with open(fn, "rb") as fp:
try:
parse_mail(email.message_from_binary_file(fp))
except Exception as e:
print(f"Something went wrong while parsing {fn}")
print(e)