"""Altumptic outside-in check of crypto exchange websites. For each domain: the home page's security headers (as served to a desktop browser), /.well-known/security.txt, which TLS versions the server accepts, and CAA / DNSSEC from public DNS (Cloudflare DNS-over-HTTPS). Nothing intrusive: one GET per URL, TLS handshakes, DNS lookups. """ import json, subprocess, sys, datetime, re, urllib.request, urllib.error, ssl DOMAINS = sys.argv[1:] UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0 Safari/537.36" def curl_headers(url): # -L follows redirects; -D - dumps every response's headers; we keep the last block p = subprocess.run(["curl", "-s", "-L", "-m", "25", "-A", UA, "-H", "Accept: text/html,application/xhtml+xml", "-H", "Accept-Language: en-US,en;q=0.9", "-D", "-", "-o", "NUL", "-w", "\n__CODE__%{http_code} %{url_effective}", url], capture_output=True, text=True, encoding="utf-8", errors="replace") out = p.stdout m = re.search(r"__CODE__(\d+) (\S+)", out) code, final = (m.group(1), m.group(2)) if m else ("000", url) blocks = [b for b in re.split(r"\r?\n\r?\n", out.split("__CODE__")[0]) if b.strip().startswith("HTTP/")] hdr = {} if blocks: for line in blocks[-1].splitlines()[1:]: if ":" in line: k, v = line.split(":", 1) k = k.strip().lower() hdr[k] = (hdr[k] + ", " + v.strip()) if k in hdr else v.strip() return int(code), final, hdr def body(url): p = subprocess.run(["curl", "-s", "-L", "-m", "20", "-A", UA, "-w", "\n__CODE__%{http_code} %{content_type}", url], capture_output=True, text=True, encoding="utf-8", errors="replace") m = re.search(r"__CODE__(\d+) (.*)$", p.stdout.strip()) text = p.stdout.split("\n__CODE__")[0] return (int(m.group(1)) if m else 0), (m.group(2) if m else ""), text def tls_accepts(host, flag): # OpenSSL 3 refuses TLS 1.0/1.1 locally at the default security level; lower it so we # measure what the server accepts, not what our client allows. extra = ["-cipher", "DEFAULT@SECLEVEL=0"] if flag in ("-tls1", "-tls1_1") else [] p = subprocess.run(["openssl", "s_client", "-connect", f"{host}:443", "-servername", host, flag, *extra], input="", capture_output=True, text=True, timeout=25) out = p.stdout + p.stderr # "New, TLSv1.2, Cipher is ECDHE-..." means the handshake completed; "(NONE)" means it was refused return re.search(r"New, (TLSv[\d.]+|SSLv3), Cipher is (?!\(NONE\))", out) is not None def doh(name, rtype): req = urllib.request.Request(f"https://cloudflare-dns.com/dns-query?name={name}&type={rtype}&do=1", headers={"Accept": "application/dns-json"}) with urllib.request.urlopen(req, timeout=20) as r: return json.load(r) def caa_for(domain): # CAA is inherited from the parent if the name has none j = doh(domain, "CAA") return [a["data"] for a in j.get("Answer", []) if a.get("type") == 257] results = [] for d in DOMAINS: r = {"domain": d, "checked": datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%MZ")} try: code, final, h = curl_headers(f"https://{d}/") r.update(status=code, final=final) hsts = h.get("strict-transport-security", "") ma = re.search(r"max-age=(\d+)", hsts) r["hsts"] = hsts r["hsts_max_age"] = int(ma.group(1)) if ma else 0 r["hsts_subdomains"] = "includesubdomains" in hsts.lower() r["hsts_preload"] = "preload" in hsts.lower() r["csp"] = bool(h.get("content-security-policy")) r["csp_report_only"] = bool(h.get("content-security-policy-report-only")) and not r["csp"] csp = h.get("content-security-policy", "") r["frame_ancestors"] = "frame-ancestors" in csp.lower() r["xfo"] = h.get("x-frame-options", "") r["nosniff"] = h.get("x-content-type-options", "").lower() == "nosniff" r["referrer_policy"] = h.get("referrer-policy", "") r["permissions_policy"] = bool(h.get("permissions-policy")) r["server"] = h.get("server", "") except Exception as e: r["error_headers"] = str(e) try: sc, ctype, txt = body(f"https://{d}/.well-known/security.txt") ok = sc == 200 and "html" not in ctype.lower() and re.search(r"(?im)^contact:", txt) is not None r["securitytxt"] = ok if ok: exp = re.search(r"(?im)^expires:\s*(\S+)", txt) r["securitytxt_expires"] = exp.group(1) if exp else "" r["securitytxt_contact"] = re.findall(r"(?im)^contact:\s*(\S+)", txt)[:3] else: r["securitytxt_status"] = f"{sc} {ctype}" except Exception as e: r["error_securitytxt"] = str(e) for name, flag in (("tls1_0", "-tls1"), ("tls1_1", "-tls1_1"), ("tls1_3", "-tls1_3")): try: r[name] = tls_accepts(d, flag) except Exception as e: r[name] = None try: r["caa"] = caa_for(d) a = doh(d, "A") r["dnssec"] = bool(a.get("AD")) except Exception as e: r["error_dns"] = str(e) results.append(r) print(json.dumps(r), flush=True)