dmarc-check 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dmarc_check/__init__.py +3 -0
- dmarc_check/__main__.py +5 -0
- dmarc_check/action.py +96 -0
- dmarc_check/checks/__init__.py +47 -0
- dmarc_check/checks/dkim.py +162 -0
- dmarc_check/checks/dmarc.py +135 -0
- dmarc_check/checks/mtasts.py +167 -0
- dmarc_check/checks/mx.py +70 -0
- dmarc_check/checks/spf.py +302 -0
- dmarc_check/checks/tlsrpt.py +43 -0
- dmarc_check/cli.py +56 -0
- dmarc_check/dns_client.py +116 -0
- dmarc_check/engine.py +182 -0
- dmarc_check/findings.py +111 -0
- dmarc_check/github.py +89 -0
- dmarc_check/https.py +74 -0
- dmarc_check/py.typed +0 -0
- dmarc_check/report.py +74 -0
- dmarc_check-1.0.0.dist-info/METADATA +277 -0
- dmarc_check-1.0.0.dist-info/RECORD +23 -0
- dmarc_check-1.0.0.dist-info/WHEEL +4 -0
- dmarc_check-1.0.0.dist-info/entry_points.txt +2 -0
- dmarc_check-1.0.0.dist-info/licenses/LICENSE +21 -0
dmarc_check/__init__.py
ADDED
dmarc_check/__main__.py
ADDED
dmarc_check/action.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""Entry point for the GitHub Action (``python -m dmarc_check.action``).
|
|
2
|
+
|
|
3
|
+
Inputs arrive as ``DMARC_CHECK_*`` environment variables set by action.yml.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
import secrets
|
|
10
|
+
import sys
|
|
11
|
+
from collections.abc import Mapping
|
|
12
|
+
|
|
13
|
+
from .dns_client import DnsResolver, Resolver
|
|
14
|
+
from .engine import EXIT_USAGE, UsageError, parse_fail_on, parse_list, run, validate_domain
|
|
15
|
+
from .github import GitHubClient, GitHubError, sync_issue
|
|
16
|
+
from .https import PolicyFetcher
|
|
17
|
+
from .report import to_json, to_markdown, to_text
|
|
18
|
+
|
|
19
|
+
DEFAULT_ISSUE_TITLE = "dmarc-check: mail authentication problems"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _escape(value: str) -> str:
|
|
23
|
+
return value.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _bool(value: str) -> bool:
|
|
27
|
+
return value.strip().lower() in ("1", "true", "yes", "on")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _append(path: str | None, text: str) -> None:
|
|
31
|
+
if path:
|
|
32
|
+
with open(path, "a", encoding="utf-8") as fh:
|
|
33
|
+
fh.write(text)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _set_output(path: str | None, name: str, value: str) -> None:
|
|
37
|
+
delimiter = f"EOF_{secrets.token_hex(8)}"
|
|
38
|
+
_append(path, f"{name}<<{delimiter}\n{value}\n{delimiter}\n")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def main(
|
|
42
|
+
env: Mapping[str, str] | None = None,
|
|
43
|
+
resolver: Resolver | None = None,
|
|
44
|
+
fetcher: PolicyFetcher | None = None,
|
|
45
|
+
client: GitHubClient | None = None,
|
|
46
|
+
) -> int:
|
|
47
|
+
env = os.environ if env is None else env
|
|
48
|
+
try:
|
|
49
|
+
domains = [validate_domain(d) for d in parse_list([env.get("DMARC_CHECK_DOMAINS", "")])]
|
|
50
|
+
if not domains:
|
|
51
|
+
raise UsageError("the domains input is empty")
|
|
52
|
+
fail_on = parse_fail_on(env.get("DMARC_CHECK_FAIL_ON", "error"))
|
|
53
|
+
except UsageError as exc:
|
|
54
|
+
print(f"::error title=dmarc-check::{_escape(str(exc))}")
|
|
55
|
+
return EXIT_USAGE
|
|
56
|
+
|
|
57
|
+
selectors = parse_list([env.get("DMARC_CHECK_DKIM_SELECTORS", "")])
|
|
58
|
+
if resolver is None:
|
|
59
|
+
nameservers = parse_list([env.get("DMARC_CHECK_RESOLVER", "")])
|
|
60
|
+
resolver = DnsResolver(nameservers or None)
|
|
61
|
+
report = run(domains, resolver, selectors, fail_on, fetcher)
|
|
62
|
+
|
|
63
|
+
print(to_text(report))
|
|
64
|
+
for f in report.failing:
|
|
65
|
+
print(f"::error title=dmarc-check {f.id} ({f.domain})::{_escape(f.message)} {f.url}")
|
|
66
|
+
|
|
67
|
+
_append(env.get("GITHUB_STEP_SUMMARY"), to_markdown(report))
|
|
68
|
+
_set_output(env.get("GITHUB_OUTPUT"), "json", to_json(report))
|
|
69
|
+
_set_output(env.get("GITHUB_OUTPUT"), "exit-code", str(report.exit_code))
|
|
70
|
+
|
|
71
|
+
if _bool(env.get("DMARC_CHECK_CREATE_ISSUE", "false")):
|
|
72
|
+
title = env.get("DMARC_CHECK_ISSUE_TITLE", "").strip() or DEFAULT_ISSUE_TITLE
|
|
73
|
+
run_url = ""
|
|
74
|
+
if env.get("GITHUB_RUN_ID"):
|
|
75
|
+
server = env.get("GITHUB_SERVER_URL", "https://github.com")
|
|
76
|
+
run_url = f"{server}/{env.get('GITHUB_REPOSITORY', '')}/actions/runs/{env['GITHUB_RUN_ID']}"
|
|
77
|
+
body = to_markdown(report, heading="## dmarc-check report", footer=False)
|
|
78
|
+
if run_url:
|
|
79
|
+
body += f"\nLast checked by [this workflow run]({run_url}). "
|
|
80
|
+
body += "This issue is updated by dmarc-check and closed automatically when the checks pass.\n"
|
|
81
|
+
try:
|
|
82
|
+
gh = client or GitHubClient(
|
|
83
|
+
env.get("DMARC_CHECK_GITHUB_TOKEN", ""),
|
|
84
|
+
env.get("GITHUB_REPOSITORY", ""),
|
|
85
|
+
env.get("GITHUB_API_URL", "https://api.github.com"),
|
|
86
|
+
)
|
|
87
|
+
print(sync_issue(gh, title, report.failed, body))
|
|
88
|
+
except GitHubError as exc:
|
|
89
|
+
print(f"::error title=dmarc-check issue::{_escape(str(exc))}")
|
|
90
|
+
return max(report.exit_code, 1)
|
|
91
|
+
|
|
92
|
+
return report.exit_code
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
if __name__ == "__main__": # pragma: no cover
|
|
96
|
+
sys.exit(main())
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Individual checks. Each check takes a :class:`Context` and appends findings."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
|
|
7
|
+
from ..dns_client import Resolver
|
|
8
|
+
from ..findings import Finding, Severity
|
|
9
|
+
from ..https import PolicyFetcher
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class Context:
|
|
14
|
+
domain: str
|
|
15
|
+
resolver: Resolver
|
|
16
|
+
fetch_policy: PolicyFetcher
|
|
17
|
+
dkim_selectors: list[str] = field(default_factory=list)
|
|
18
|
+
findings: list[Finding] = field(default_factory=list)
|
|
19
|
+
# Filled in by the MX check and used by MTA-STS.
|
|
20
|
+
mx_hosts: list[str] = field(default_factory=list)
|
|
21
|
+
null_mx: bool = False
|
|
22
|
+
|
|
23
|
+
def add(self, check: str, fid: str, severity: Severity, message: str) -> None:
|
|
24
|
+
# Messages quote DNS data; strip control characters so a record cannot
|
|
25
|
+
# inject lines into logs or GitHub workflow commands.
|
|
26
|
+
clean = "".join(c if c.isprintable() else "?" for c in message)
|
|
27
|
+
self.findings.append(Finding(fid, severity, check, clean, self.domain))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def parse_tags(record: str) -> tuple[list[tuple[str, str]], list[str]]:
|
|
31
|
+
"""Parse a ``k=v; k=v`` tag list (DMARC, DKIM, MTA-STS, TLS-RPT).
|
|
32
|
+
|
|
33
|
+
Returns the tags in order and a list of malformed fragments.
|
|
34
|
+
"""
|
|
35
|
+
tags: list[tuple[str, str]] = []
|
|
36
|
+
bad: list[str] = []
|
|
37
|
+
for part in record.split(";"):
|
|
38
|
+
part = part.strip()
|
|
39
|
+
if not part:
|
|
40
|
+
continue
|
|
41
|
+
key, sep, value = part.partition("=")
|
|
42
|
+
key = key.strip()
|
|
43
|
+
if not sep or not key or any(c.isspace() for c in key):
|
|
44
|
+
bad.append(part)
|
|
45
|
+
continue
|
|
46
|
+
tags.append((key, value.strip()))
|
|
47
|
+
return tags, bad
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"""DKIM key records (RFC 6376) for the selectors the user names."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import binascii
|
|
7
|
+
|
|
8
|
+
from ..findings import Severity
|
|
9
|
+
from . import Context, parse_tags
|
|
10
|
+
|
|
11
|
+
CHECK = "dkim"
|
|
12
|
+
WEAK_BITS = 2048
|
|
13
|
+
MIN_BITS = 1024
|
|
14
|
+
_RSA_OID = bytes.fromhex("2a864886f70d010101") # 1.2.840.113549.1.1.1
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class _DerError(ValueError):
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _tlv(data: bytes, pos: int) -> tuple[int, int, int]:
|
|
22
|
+
"""Return (tag, value_start, value_end) for the DER element at ``pos``."""
|
|
23
|
+
if pos + 2 > len(data):
|
|
24
|
+
raise _DerError("truncated")
|
|
25
|
+
tag = data[pos]
|
|
26
|
+
length = data[pos + 1]
|
|
27
|
+
pos += 2
|
|
28
|
+
if length & 0x80:
|
|
29
|
+
n = length & 0x7F
|
|
30
|
+
if n == 0 or n > 4 or pos + n > len(data):
|
|
31
|
+
raise _DerError("bad length")
|
|
32
|
+
length = int.from_bytes(data[pos : pos + n], "big")
|
|
33
|
+
pos += n
|
|
34
|
+
if pos + length > len(data):
|
|
35
|
+
raise _DerError("truncated")
|
|
36
|
+
return tag, pos, pos + length
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _rsa_modulus_bits(rsa_public_key: bytes) -> int:
|
|
40
|
+
"""Bits of the modulus in a PKCS#1 RSAPublicKey SEQUENCE { n, e }."""
|
|
41
|
+
tag, start, _ = _tlv(rsa_public_key, 0)
|
|
42
|
+
if tag != 0x30:
|
|
43
|
+
raise _DerError("not a sequence")
|
|
44
|
+
tag, nstart, nend = _tlv(rsa_public_key, start)
|
|
45
|
+
if tag != 0x02:
|
|
46
|
+
raise _DerError("modulus is not an integer")
|
|
47
|
+
return int.from_bytes(rsa_public_key[nstart:nend], "big").bit_length()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def rsa_key_bits(der: bytes) -> int:
|
|
51
|
+
"""Return the RSA modulus size of a SubjectPublicKeyInfo or bare RSAPublicKey."""
|
|
52
|
+
tag, start, _ = _tlv(der, 0)
|
|
53
|
+
if tag != 0x30:
|
|
54
|
+
raise _DerError("not a sequence")
|
|
55
|
+
inner_tag, istart, iend = _tlv(der, start)
|
|
56
|
+
if inner_tag == 0x02:
|
|
57
|
+
return _rsa_modulus_bits(der) # PKCS#1 RSAPublicKey
|
|
58
|
+
if inner_tag != 0x30:
|
|
59
|
+
raise _DerError("unexpected structure")
|
|
60
|
+
oid_tag, ostart, oend = _tlv(der, istart)
|
|
61
|
+
if oid_tag != 0x06 or der[ostart:oend] != _RSA_OID:
|
|
62
|
+
raise _DerError("not an RSA key")
|
|
63
|
+
bit_tag, bstart, bend = _tlv(der, iend)
|
|
64
|
+
if bit_tag != 0x03 or bstart >= bend or der[bstart] != 0:
|
|
65
|
+
raise _DerError("bad bit string")
|
|
66
|
+
return _rsa_modulus_bits(der[bstart + 1 : bend])
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def check_dkim(ctx: Context) -> None:
|
|
70
|
+
for selector in ctx.dkim_selectors:
|
|
71
|
+
_check_selector(ctx, selector)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _check_selector(ctx: Context, selector: str) -> None:
|
|
75
|
+
name = f"{selector}._domainkey.{ctx.domain}"
|
|
76
|
+
ans = ctx.resolver.txt(name)
|
|
77
|
+
records = [t for t in ans.records if "p=" in t.replace(" ", "")]
|
|
78
|
+
if not records:
|
|
79
|
+
ctx.add(CHECK, "dkim-not-found", Severity.ERROR, f"No DKIM key at {name}")
|
|
80
|
+
return
|
|
81
|
+
if len(records) > 1:
|
|
82
|
+
ctx.add(
|
|
83
|
+
CHECK,
|
|
84
|
+
"dkim-multiple-records",
|
|
85
|
+
Severity.ERROR,
|
|
86
|
+
f"{len(records)} DKIM key records at {name}; verification is unpredictable",
|
|
87
|
+
)
|
|
88
|
+
return
|
|
89
|
+
tags, bad = parse_tags(records[0])
|
|
90
|
+
values = {k.lower(): v for k, v in tags}
|
|
91
|
+
problems = [f"malformed tag {b!r}" for b in bad]
|
|
92
|
+
if "v" in values and (tags[0][0].lower() != "v" or values["v"] != "DKIM1"):
|
|
93
|
+
problems.append("v= must be DKIM1 and come first")
|
|
94
|
+
if problems:
|
|
95
|
+
ctx.add(
|
|
96
|
+
CHECK,
|
|
97
|
+
"dkim-syntax-error",
|
|
98
|
+
Severity.ERROR,
|
|
99
|
+
f"DKIM record for selector {selector} is invalid: " + "; ".join(problems),
|
|
100
|
+
)
|
|
101
|
+
return
|
|
102
|
+
|
|
103
|
+
if "y" in [f.strip().lower() for f in values.get("t", "").split(":")]:
|
|
104
|
+
ctx.add(
|
|
105
|
+
CHECK,
|
|
106
|
+
"dkim-testing-mode",
|
|
107
|
+
Severity.INFO,
|
|
108
|
+
f"Selector {selector} has t=y (testing): receivers may ignore failures",
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
p = "".join(values.get("p", "").split())
|
|
112
|
+
if not p:
|
|
113
|
+
ctx.add(
|
|
114
|
+
CHECK,
|
|
115
|
+
"dkim-key-revoked",
|
|
116
|
+
Severity.WARN,
|
|
117
|
+
f"Selector {selector} has an empty p= (key revoked)",
|
|
118
|
+
)
|
|
119
|
+
return
|
|
120
|
+
try:
|
|
121
|
+
der = base64.b64decode(p, validate=True)
|
|
122
|
+
except (binascii.Error, ValueError):
|
|
123
|
+
ctx.add(CHECK, "dkim-invalid-key", Severity.ERROR, f"Selector {selector}: p= is not valid base64")
|
|
124
|
+
return
|
|
125
|
+
|
|
126
|
+
key_type = values.get("k", "rsa").lower()
|
|
127
|
+
if key_type == "ed25519":
|
|
128
|
+
if len(der) != 32:
|
|
129
|
+
ctx.add(
|
|
130
|
+
CHECK,
|
|
131
|
+
"dkim-invalid-key",
|
|
132
|
+
Severity.ERROR,
|
|
133
|
+
f"Selector {selector}: Ed25519 key must be 32 bytes, got {len(der)}",
|
|
134
|
+
)
|
|
135
|
+
return
|
|
136
|
+
if key_type != "rsa":
|
|
137
|
+
ctx.add(CHECK, "dkim-invalid-key", Severity.ERROR, f"Selector {selector}: unknown key type k={key_type}")
|
|
138
|
+
return
|
|
139
|
+
try:
|
|
140
|
+
bits = rsa_key_bits(der)
|
|
141
|
+
except _DerError as exc:
|
|
142
|
+
ctx.add(
|
|
143
|
+
CHECK,
|
|
144
|
+
"dkim-invalid-key",
|
|
145
|
+
Severity.ERROR,
|
|
146
|
+
f"Selector {selector}: RSA key could not be parsed ({exc})",
|
|
147
|
+
)
|
|
148
|
+
return
|
|
149
|
+
if bits < MIN_BITS:
|
|
150
|
+
ctx.add(
|
|
151
|
+
CHECK,
|
|
152
|
+
"dkim-key-too-short",
|
|
153
|
+
Severity.ERROR,
|
|
154
|
+
f"Selector {selector}: {bits}-bit RSA key; receivers reject keys under {MIN_BITS} bits",
|
|
155
|
+
)
|
|
156
|
+
elif bits < WEAK_BITS:
|
|
157
|
+
ctx.add(
|
|
158
|
+
CHECK,
|
|
159
|
+
"dkim-weak-key",
|
|
160
|
+
Severity.WARN,
|
|
161
|
+
f"Selector {selector}: {bits}-bit RSA key; use {WEAK_BITS} bits",
|
|
162
|
+
)
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"""DMARC (RFC 7489 / DMARCbis): presence, uniqueness, policy, pct and reporting."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
from ..findings import Severity
|
|
8
|
+
from . import Context, parse_tags
|
|
9
|
+
|
|
10
|
+
CHECK = "dmarc"
|
|
11
|
+
POLICIES = {"none", "quarantine", "reject"}
|
|
12
|
+
KNOWN_TAGS = {"v", "p", "sp", "np", "pct", "rua", "ruf", "adkim", "aspf", "fo", "rf", "ri", "t", "psd"}
|
|
13
|
+
_MAILTO = re.compile(r"^mailto:[^@\s]+@[^@\s]+\.[^@\s]+(?:!\d+[kmgt]?)?$", re.IGNORECASE)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _is_dmarc(txt: str) -> bool:
|
|
17
|
+
first = txt.split(";", 1)[0].replace(" ", "").lower()
|
|
18
|
+
return first == "v=dmarc1"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def check_dmarc(ctx: Context) -> None:
|
|
22
|
+
name = "_dmarc." + ctx.domain
|
|
23
|
+
ans = ctx.resolver.txt(name)
|
|
24
|
+
records = [t for t in ans.records if _is_dmarc(t)]
|
|
25
|
+
if not records:
|
|
26
|
+
ctx.add(CHECK, "dmarc-not-found", Severity.ERROR, f"No DMARC record at {name}")
|
|
27
|
+
return
|
|
28
|
+
if len(records) > 1:
|
|
29
|
+
ctx.add(
|
|
30
|
+
CHECK,
|
|
31
|
+
"dmarc-multiple-records",
|
|
32
|
+
Severity.ERROR,
|
|
33
|
+
f"{len(records)} DMARC records at {name}; receivers ignore all of them",
|
|
34
|
+
)
|
|
35
|
+
return
|
|
36
|
+
|
|
37
|
+
tags, bad = parse_tags(records[0])
|
|
38
|
+
keys = [k.lower() for k, _ in tags]
|
|
39
|
+
values = {k.lower(): v for k, v in tags}
|
|
40
|
+
problems = [f"malformed tag {b!r}" for b in bad]
|
|
41
|
+
dupes = sorted({k for k in keys if keys.count(k) > 1})
|
|
42
|
+
problems += [f"duplicate tag {k}=" for k in dupes]
|
|
43
|
+
for tag in ("adkim", "aspf"):
|
|
44
|
+
if tag in values and values[tag].lower() not in ("r", "s"):
|
|
45
|
+
problems.append(f"{tag}={values[tag]} must be r or s")
|
|
46
|
+
pct: int | None = None
|
|
47
|
+
if "pct" in values:
|
|
48
|
+
try:
|
|
49
|
+
pct = int(values["pct"])
|
|
50
|
+
if not 0 <= pct <= 100:
|
|
51
|
+
raise ValueError
|
|
52
|
+
except ValueError:
|
|
53
|
+
problems.append(f"pct={values['pct']} must be an integer from 0 to 100")
|
|
54
|
+
pct = None
|
|
55
|
+
if problems:
|
|
56
|
+
ctx.add(CHECK, "dmarc-syntax-error", Severity.ERROR, "DMARC record is invalid: " + "; ".join(problems))
|
|
57
|
+
|
|
58
|
+
p = values.get("p", "").lower()
|
|
59
|
+
if "p" not in values:
|
|
60
|
+
ctx.add(CHECK, "dmarc-invalid-policy", Severity.ERROR, "DMARC record has no p= tag")
|
|
61
|
+
elif p not in POLICIES:
|
|
62
|
+
ctx.add(
|
|
63
|
+
CHECK,
|
|
64
|
+
"dmarc-invalid-policy",
|
|
65
|
+
Severity.ERROR,
|
|
66
|
+
f"p={values['p']} is not none, quarantine or reject",
|
|
67
|
+
)
|
|
68
|
+
elif p == "none":
|
|
69
|
+
ctx.add(
|
|
70
|
+
CHECK,
|
|
71
|
+
"dmarc-policy-none",
|
|
72
|
+
Severity.WARN,
|
|
73
|
+
"DMARC policy is p=none: spoofed mail is reported but still delivered",
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
for tag in ("sp", "np"):
|
|
77
|
+
if tag not in values:
|
|
78
|
+
continue
|
|
79
|
+
value = values[tag].lower()
|
|
80
|
+
if value not in POLICIES:
|
|
81
|
+
ctx.add(
|
|
82
|
+
CHECK,
|
|
83
|
+
"dmarc-invalid-policy",
|
|
84
|
+
Severity.ERROR,
|
|
85
|
+
f"{tag}={values[tag]} is not none, quarantine or reject",
|
|
86
|
+
)
|
|
87
|
+
elif value == "none" and p in ("quarantine", "reject"):
|
|
88
|
+
what = "subdomains" if tag == "sp" else "non-existent subdomains"
|
|
89
|
+
ctx.add(
|
|
90
|
+
CHECK,
|
|
91
|
+
"dmarc-subdomain-policy-none",
|
|
92
|
+
Severity.WARN,
|
|
93
|
+
f"{tag}=none leaves {what} unprotected although p={p}",
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
if pct is not None and pct < 100:
|
|
97
|
+
ctx.add(
|
|
98
|
+
CHECK,
|
|
99
|
+
"dmarc-partial-pct",
|
|
100
|
+
Severity.WARN,
|
|
101
|
+
f"pct={pct}: the policy only applies to {pct}% of failing mail",
|
|
102
|
+
)
|
|
103
|
+
if values.get("t", "").lower() == "y":
|
|
104
|
+
ctx.add(
|
|
105
|
+
CHECK,
|
|
106
|
+
"dmarc-testing-mode",
|
|
107
|
+
Severity.WARN,
|
|
108
|
+
"t=y: receivers are asked to apply a less strict policy (testing mode)",
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
if not values.get("rua"):
|
|
112
|
+
ctx.add(
|
|
113
|
+
CHECK,
|
|
114
|
+
"dmarc-no-rua",
|
|
115
|
+
Severity.INFO,
|
|
116
|
+
"No rua= tag: you will not receive aggregate DMARC reports",
|
|
117
|
+
)
|
|
118
|
+
for tag in ("rua", "ruf"):
|
|
119
|
+
for uri in filter(None, (u.strip() for u in values.get(tag, "").split(","))):
|
|
120
|
+
if not _MAILTO.match(uri):
|
|
121
|
+
ctx.add(
|
|
122
|
+
CHECK,
|
|
123
|
+
"dmarc-invalid-report-uri",
|
|
124
|
+
Severity.WARN,
|
|
125
|
+
f"{tag} URI {uri!r} is not a valid mailto: address",
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
unknown = [k for k in dict.fromkeys(keys) if k not in KNOWN_TAGS]
|
|
129
|
+
if unknown:
|
|
130
|
+
ctx.add(
|
|
131
|
+
CHECK,
|
|
132
|
+
"dmarc-unknown-tag",
|
|
133
|
+
Severity.INFO,
|
|
134
|
+
"Unknown DMARC tag(s) ignored by receivers: " + ", ".join(unknown),
|
|
135
|
+
)
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"""MTA-STS (RFC 8461): TXT record, HTTPS policy, mode and MX coverage."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
from ..findings import Severity
|
|
8
|
+
from ..https import MAX_POLICY_BYTES, FetchError
|
|
9
|
+
from . import Context, parse_tags
|
|
10
|
+
|
|
11
|
+
CHECK = "mta-sts"
|
|
12
|
+
MIN_MAX_AGE = 86400
|
|
13
|
+
MAX_MAX_AGE = 31557600
|
|
14
|
+
_ID = re.compile(r"^[A-Za-z0-9]{1,32}$")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _is_sts(txt: str) -> bool:
|
|
18
|
+
return txt.split(";", 1)[0].replace(" ", "") == "v=STSv1"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def mx_matches(pattern: str, host: str) -> bool:
|
|
22
|
+
pattern = pattern.strip().rstrip(".").lower()
|
|
23
|
+
host = host.rstrip(".").lower()
|
|
24
|
+
if pattern.startswith("*."):
|
|
25
|
+
suffix = pattern[1:] # ".example.com"
|
|
26
|
+
return host.endswith(suffix) and "." not in host[: -len(suffix)] and len(host) > len(suffix)
|
|
27
|
+
return host == pattern
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def parse_policy(text: str) -> tuple[dict[str, str], list[str], list[str]]:
|
|
31
|
+
"""Return (fields, mx patterns, problems)."""
|
|
32
|
+
fields: dict[str, str] = {}
|
|
33
|
+
mx: list[str] = []
|
|
34
|
+
problems: list[str] = []
|
|
35
|
+
for line in text.replace("\r\n", "\n").split("\n"):
|
|
36
|
+
if not line.strip():
|
|
37
|
+
continue
|
|
38
|
+
key, sep, value = line.partition(":")
|
|
39
|
+
key, value = key.strip(), value.strip()
|
|
40
|
+
if not sep or not key:
|
|
41
|
+
problems.append(f"malformed line {line.strip()!r}")
|
|
42
|
+
continue
|
|
43
|
+
if key == "mx":
|
|
44
|
+
mx.append(value)
|
|
45
|
+
elif key in fields and key in ("version", "mode", "max_age"):
|
|
46
|
+
problems.append(f"duplicate {key}")
|
|
47
|
+
else:
|
|
48
|
+
fields[key] = value
|
|
49
|
+
if fields.get("version") != "STSv1":
|
|
50
|
+
problems.append("version must be STSv1")
|
|
51
|
+
mode = fields.get("mode")
|
|
52
|
+
if mode not in ("enforce", "testing", "none"):
|
|
53
|
+
problems.append("mode must be enforce, testing or none")
|
|
54
|
+
max_age = fields.get("max_age", "")
|
|
55
|
+
if not max_age.isdigit() or int(max_age) > MAX_MAX_AGE:
|
|
56
|
+
problems.append(f"max_age must be an integer from 0 to {MAX_MAX_AGE}")
|
|
57
|
+
if mode in ("enforce", "testing") and not mx:
|
|
58
|
+
problems.append("no mx: lines")
|
|
59
|
+
return fields, mx, problems
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def check_mtasts(ctx: Context) -> None:
|
|
63
|
+
name = "_mta-sts." + ctx.domain
|
|
64
|
+
ans = ctx.resolver.txt(name)
|
|
65
|
+
records = [t for t in ans.records if _is_sts(t)]
|
|
66
|
+
if not records:
|
|
67
|
+
ctx.add(
|
|
68
|
+
CHECK,
|
|
69
|
+
"mtasts-not-found",
|
|
70
|
+
Severity.INFO,
|
|
71
|
+
f"No MTA-STS record at {name}; inbound TLS can be downgraded",
|
|
72
|
+
)
|
|
73
|
+
return
|
|
74
|
+
if len(records) > 1:
|
|
75
|
+
ctx.add(
|
|
76
|
+
CHECK,
|
|
77
|
+
"mtasts-multiple-records",
|
|
78
|
+
Severity.ERROR,
|
|
79
|
+
f"{len(records)} MTA-STS records at {name}; senders treat this as no policy",
|
|
80
|
+
)
|
|
81
|
+
return
|
|
82
|
+
tags, bad = parse_tags(records[0])
|
|
83
|
+
values = dict(tags)
|
|
84
|
+
if bad or not _ID.match(values.get("id", "")):
|
|
85
|
+
ctx.add(
|
|
86
|
+
CHECK,
|
|
87
|
+
"mtasts-invalid-record",
|
|
88
|
+
Severity.ERROR,
|
|
89
|
+
f"MTA-STS record at {name} needs v=STSv1 and an id= of 1-32 letters/digits",
|
|
90
|
+
)
|
|
91
|
+
return
|
|
92
|
+
|
|
93
|
+
host = "mta-sts." + ctx.domain
|
|
94
|
+
url = f"https://{host}/.well-known/mta-sts.txt"
|
|
95
|
+
try:
|
|
96
|
+
resp = ctx.fetch_policy(host)
|
|
97
|
+
except FetchError as exc:
|
|
98
|
+
ctx.add(CHECK, "mtasts-policy-unavailable", Severity.ERROR, f"Could not fetch {url}: {exc}")
|
|
99
|
+
return
|
|
100
|
+
if 300 <= resp.status < 400:
|
|
101
|
+
ctx.add(
|
|
102
|
+
CHECK,
|
|
103
|
+
"mtasts-policy-unavailable",
|
|
104
|
+
Severity.ERROR,
|
|
105
|
+
f"{url} redirects (HTTP {resp.status}); redirects are not allowed",
|
|
106
|
+
)
|
|
107
|
+
return
|
|
108
|
+
if resp.status != 200:
|
|
109
|
+
ctx.add(CHECK, "mtasts-policy-unavailable", Severity.ERROR, f"{url} returned HTTP {resp.status}")
|
|
110
|
+
return
|
|
111
|
+
media_type = resp.content_type.split(";", 1)[0].strip().lower()
|
|
112
|
+
if media_type != "text/plain":
|
|
113
|
+
ctx.add(
|
|
114
|
+
CHECK,
|
|
115
|
+
"mtasts-invalid-policy",
|
|
116
|
+
Severity.ERROR,
|
|
117
|
+
f"{url} is served as {media_type or 'no content type'}; it must be text/plain",
|
|
118
|
+
)
|
|
119
|
+
return
|
|
120
|
+
if len(resp.body) > MAX_POLICY_BYTES:
|
|
121
|
+
ctx.add(CHECK, "mtasts-invalid-policy", Severity.ERROR, f"{url} is larger than {MAX_POLICY_BYTES} bytes")
|
|
122
|
+
return
|
|
123
|
+
try:
|
|
124
|
+
text = resp.body.decode("utf-8")
|
|
125
|
+
except UnicodeDecodeError:
|
|
126
|
+
ctx.add(CHECK, "mtasts-invalid-policy", Severity.ERROR, f"{url} is not valid UTF-8")
|
|
127
|
+
return
|
|
128
|
+
|
|
129
|
+
fields, patterns, problems = parse_policy(text)
|
|
130
|
+
if problems:
|
|
131
|
+
ctx.add(CHECK, "mtasts-invalid-policy", Severity.ERROR, "MTA-STS policy is invalid: " + "; ".join(problems))
|
|
132
|
+
return
|
|
133
|
+
mode = fields["mode"]
|
|
134
|
+
if mode == "none":
|
|
135
|
+
ctx.add(CHECK, "mtasts-mode-none", Severity.WARN, "MTA-STS policy mode is none (policy withdrawn)")
|
|
136
|
+
return
|
|
137
|
+
if mode == "testing":
|
|
138
|
+
ctx.add(
|
|
139
|
+
CHECK,
|
|
140
|
+
"mtasts-mode-testing",
|
|
141
|
+
Severity.INFO,
|
|
142
|
+
"MTA-STS policy mode is testing: failures are reported, not enforced",
|
|
143
|
+
)
|
|
144
|
+
if int(fields["max_age"]) < MIN_MAX_AGE:
|
|
145
|
+
ctx.add(
|
|
146
|
+
CHECK,
|
|
147
|
+
"mtasts-low-max-age",
|
|
148
|
+
Severity.WARN,
|
|
149
|
+
f"max_age={fields['max_age']} is under one day ({MIN_MAX_AGE})",
|
|
150
|
+
)
|
|
151
|
+
uncovered = [h for h in ctx.mx_hosts if not any(mx_matches(p, h) for p in patterns)]
|
|
152
|
+
if uncovered:
|
|
153
|
+
if mode == "enforce":
|
|
154
|
+
ctx.add(
|
|
155
|
+
CHECK,
|
|
156
|
+
"mtasts-mx-not-covered",
|
|
157
|
+
Severity.ERROR,
|
|
158
|
+
"MX host(s) not covered by the enforced MTA-STS policy, mail to them will fail: "
|
|
159
|
+
+ ", ".join(uncovered),
|
|
160
|
+
)
|
|
161
|
+
else:
|
|
162
|
+
ctx.add(
|
|
163
|
+
CHECK,
|
|
164
|
+
"mtasts-mx-not-covered-testing",
|
|
165
|
+
Severity.WARN,
|
|
166
|
+
"MX host(s) not covered by the MTA-STS policy: " + ", ".join(uncovered),
|
|
167
|
+
)
|
dmarc_check/checks/mx.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""MX records, including null MX (RFC 7505)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ipaddress
|
|
6
|
+
|
|
7
|
+
from ..findings import Severity
|
|
8
|
+
from . import Context
|
|
9
|
+
|
|
10
|
+
CHECK = "mx"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class DomainNotFound(Exception):
|
|
14
|
+
pass
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _is_ip(host: str) -> bool:
|
|
18
|
+
try:
|
|
19
|
+
ipaddress.ip_address(host.strip("[]"))
|
|
20
|
+
except ValueError:
|
|
21
|
+
return False
|
|
22
|
+
return True
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def check_mx(ctx: Context) -> None:
|
|
26
|
+
ans = ctx.resolver.mx(ctx.domain)
|
|
27
|
+
if ans.status == "nxdomain":
|
|
28
|
+
ctx.add("dns", "dns-domain-not-found", Severity.ERROR, f"{ctx.domain} does not exist (NXDOMAIN)")
|
|
29
|
+
raise DomainNotFound(ctx.domain)
|
|
30
|
+
if not ans.records:
|
|
31
|
+
ctx.add(
|
|
32
|
+
CHECK,
|
|
33
|
+
"mx-not-found",
|
|
34
|
+
Severity.WARN,
|
|
35
|
+
f"No MX records on {ctx.domain}; senders fall back to its A/AAAA record. "
|
|
36
|
+
"Publish a null MX (0 .) if the domain receives no mail",
|
|
37
|
+
)
|
|
38
|
+
return
|
|
39
|
+
hosts = [host for _, host in ans.records]
|
|
40
|
+
if "" in hosts:
|
|
41
|
+
if len(hosts) > 1:
|
|
42
|
+
ctx.add(
|
|
43
|
+
CHECK,
|
|
44
|
+
"mx-null-with-others",
|
|
45
|
+
Severity.ERROR,
|
|
46
|
+
"Null MX (0 .) is published together with other MX records",
|
|
47
|
+
)
|
|
48
|
+
else:
|
|
49
|
+
ctx.null_mx = True
|
|
50
|
+
ctx.add(
|
|
51
|
+
CHECK,
|
|
52
|
+
"mx-null",
|
|
53
|
+
Severity.INFO,
|
|
54
|
+
f"{ctx.domain} publishes a null MX and accepts no mail; MTA-STS and TLS-RPT are skipped",
|
|
55
|
+
)
|
|
56
|
+
return
|
|
57
|
+
for host in hosts:
|
|
58
|
+
if not host:
|
|
59
|
+
continue
|
|
60
|
+
if _is_ip(host):
|
|
61
|
+
ctx.add(CHECK, "mx-target-is-ip", Severity.ERROR, f"MX target {host} is an IP address, not a host name")
|
|
62
|
+
continue
|
|
63
|
+
ctx.mx_hosts.append(host)
|
|
64
|
+
if ctx.resolver.addresses(host).status != "ok":
|
|
65
|
+
ctx.add(
|
|
66
|
+
CHECK,
|
|
67
|
+
"mx-target-unresolvable",
|
|
68
|
+
Severity.ERROR,
|
|
69
|
+
f"MX host {host} has no A or AAAA record",
|
|
70
|
+
)
|