-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathemail_security_check.py
More file actions
224 lines (187 loc) · 6.59 KB
/
email_security_check.py
File metadata and controls
224 lines (187 loc) · 6.59 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import json
try:
import dns.resolver
resolver = dns.resolver.Resolver()
resolver.timeout = 2
resolver.lifetime = 2
except ImportError:
print("dnspython is missing, use 'pip install dnspython' to install it.")
from pymisp import MISPAttribute, MISPEvent, MISPObject
misperrors = {"error": "Error"}
mispattributes = {"input": ["domain", "hostname"], "format": "misp_standard"}
moduleinfo = {
"version": "0.2",
"author": "Mihai Saveanu",
"description": "Check email security posture (SPF, DKIM, DMARC, MTA-STS) for a domain.",
"module-type": ["expansion", "hover"],
"name": "Email Security Check",
"logo": "",
"requirements": ["dnspython"],
"features": (
"The module takes a domain or hostname attribute as input and queries DNS"
" for email security records: SPF (TXT), DMARC (_dmarc), DKIM (common selectors),"
" and MTA-STS (_mta-sts). Returns structured MISP attributes with a domain-ip"
" object linking the findings to the queried domain."
),
"references": [
"https://tools.ietf.org/html/rfc7208",
"https://tools.ietf.org/html/rfc7489",
],
"input": "A domain or hostname attribute.",
"output": "Domain-ip MISP object with email security assessment attributes.",
}
moduleconfig = ["custom_resolver"]
DKIM_SELECTORS = [
"default",
"google",
"selector1",
"selector2",
"k1",
"mandrill",
"everlytickey1",
"everlytickey2",
"dkim",
"s1",
"s2",
"mailo",
]
def _query_txt(domain):
try:
answers = resolver.resolve(domain, "TXT")
return [str(rdata).strip('"') for rdata in answers]
except Exception:
return []
def _check_spf(domain):
records = _query_txt(domain)
spf = [r for r in records if r.startswith("v=spf1")]
if spf:
return {"status": "FOUND", "record": spf[0]}
return {"status": "MISSING", "record": None}
def _check_dmarc(domain):
records = _query_txt(f"_dmarc.{domain}")
dmarc = [r for r in records if r.startswith("v=DMARC1")]
if dmarc:
policy = "none"
for part in dmarc[0].split(";"):
part = part.strip()
if part.startswith("p="):
policy = part[2:]
return {"status": "FOUND", "record": dmarc[0], "policy": policy}
return {"status": "MISSING", "record": None, "policy": None}
def _check_dkim(domain):
found = []
for selector in DKIM_SELECTORS:
records = _query_txt(f"{selector}._domainkey.{domain}")
dkim = [r for r in records if "DKIM1" in r or "k=" in r or "p=" in r]
if dkim:
found.append({"selector": selector, "record": dkim[0]})
return found
def _check_mta_sts(domain):
records = _query_txt(f"_mta-sts.{domain}")
sts = [r for r in records if r.startswith("v=STSv1")]
if sts:
return {"status": "FOUND", "record": sts[0]}
return {"status": "MISSING", "record": None}
def handler(q=False):
if q is False:
return False
request = json.loads(q)
if not request.get("attribute") or not request["attribute"].get("type"):
return {"error": "Missing or invalid attribute."}
attribute = request["attribute"]
if attribute["type"] not in mispattributes["input"]:
return {"error": f"Unsupported attribute type: {attribute['type']}"}
domain = attribute["value"]
if request.get("config", {}).get("custom_resolver"):
resolver.nameservers = [request["config"]["custom_resolver"]]
spf = _check_spf(domain)
dmarc = _check_dmarc(domain)
dkim = _check_dkim(domain)
mta_sts = _check_mta_sts(domain)
event = MISPEvent()
initial_attribute = MISPAttribute()
initial_attribute.from_dict(**attribute)
event.add_attribute(**initial_attribute)
domain_obj = MISPObject("domain-ip")
domain_obj.add_attribute("domain", **{"type": "domain", "value": domain})
score = 0
if spf["status"] == "FOUND":
score += 1
domain_obj.add_attribute(
"text",
**{"type": "text", "value": f"SPF: {spf['record']}", "comment": "SPF record", "disable_correlation": True},
)
else:
domain_obj.add_attribute(
"text",
**{"type": "text", "value": "SPF: MISSING", "comment": "SPF record", "disable_correlation": True},
)
if dmarc["status"] == "FOUND":
score += 1
if dmarc["policy"] in ("reject", "quarantine"):
score += 1
domain_obj.add_attribute(
"text",
**{
"type": "text",
"value": f"DMARC: {dmarc['policy']} — {dmarc['record']}",
"comment": "DMARC record and policy",
"disable_correlation": True,
},
)
else:
domain_obj.add_attribute(
"text",
**{"type": "text", "value": "DMARC: MISSING", "comment": "DMARC record", "disable_correlation": True},
)
if dkim:
score += 1
selectors = ", ".join(d["selector"] for d in dkim)
domain_obj.add_attribute(
"text",
**{
"type": "text",
"value": f"DKIM: FOUND ({len(dkim)} selector(s): {selectors})",
"comment": "DKIM selectors found",
"disable_correlation": True,
},
)
else:
domain_obj.add_attribute(
"text",
**{
"type": "text",
"value": "DKIM: NOT FOUND (tested common selectors)",
"comment": "DKIM check",
"disable_correlation": True,
},
)
if mta_sts["status"] == "FOUND":
score += 1
domain_obj.add_attribute(
"text",
**{"type": "text", "value": f"MTA-STS: {mta_sts['record']}", "comment": "MTA-STS record", "disable_correlation": True},
)
else:
domain_obj.add_attribute(
"text",
**{"type": "text", "value": "MTA-STS: MISSING", "comment": "MTA-STS record", "disable_correlation": True},
)
domain_obj.add_attribute(
"text",
**{
"type": "text",
"value": f"Email Security Score: {score}/5",
"comment": "Overall email security posture score",
"disable_correlation": True,
},
)
domain_obj.add_reference(initial_attribute.uuid, "related-to")
event.add_object(**domain_obj)
ev = json.loads(event.to_json())
results = {key: ev[key] for key in ("Attribute", "Object") if key in ev and ev[key]}
return {"results": results}
def introspection():
return mispattributes
def version():
return moduleinfo