// Collects the published DMARC record, if any, for the top N registrable // domains of the Majestic Million list. It reads public DNS only: one TXT query for // _dmarc. per domain, through Cloudflare's public DNS-over-HTTPS // resolver, a few at a time. It sends no mail and contacts no domain's servers. // // Usage: node dmarc-collect.js // // The list ranks some subdomains (play.google.com). DMARC policy is published // on the registrable ("organizational") domain, so each entry is reduced to its // registrable domain with the Public Suffix List (https://publicsuffix.org/, // MPL 2.0) and the first N unique ones are kept, in rank order. // // The Majestic Million is published by Majestic under CC BY 3.0 // (https://majestic.com/reports/majestic-million). The snapshot this writes // keeps rank and domain from it, with that attribution. const fs = require('fs'); const crypto = require('crypto'); const [csvFile, pslFile, nArg, outFile] = process.argv.slice(2); const N = Number(nArg); const RESOLVER = 'https://cloudflare-dns.com/dns-query'; const CONCURRENCY = 6; // parallel queries const PAUSE_MS = 100; // per worker, between queries (about 60 queries a second at most) const buf = fs.readFileSync(csvFile); const csvSha = crypto.createHash('sha256').update(buf).digest('hex'); const lines = buf.toString('utf8').split(/\r?\n/); const header = lines[0].split(','); const iRank = header.indexOf('GlobalRank'); const iDomain = header.indexOf('Domain'); // Public Suffix List rules: normal, wildcard (*.x) and exception (!y.x). const pslBuf = fs.readFileSync(pslFile); const pslSha = crypto.createHash('sha256').update(pslBuf).digest('hex'); const rules = new Set(), wild = new Set(), excep = new Set(); for (const raw of pslBuf.toString('utf8').split(String.fromCharCode(10))) { const l = raw.trim(); if (!l || l.startsWith('//')) continue; if (l.startsWith('!')) excep.add(l.slice(1)); else if (l.startsWith('*.')) wild.add(l.slice(2)); else rules.add(l); } // Length in labels of the public suffix of a name (default rule "*" = 1). function suffixLabels(labels) { let best = 1; for (let i = 0; i < labels.length; i++) { const cand = labels.slice(i).join('.'); if (excep.has(cand)) return labels.length - i - 1; if (rules.has(cand)) best = Math.max(best, labels.length - i); const parent = labels.slice(i + 1).join('.'); if (i + 1 < labels.length && wild.has(parent)) best = Math.max(best, labels.length - i); } return best; } function registrable(name) { const labels = name.split('.'); const n = suffixLabels(labels); return labels.length > n ? labels.slice(labels.length - n - 1).join('.') : null; } const seen = new Set(); const domains = []; let listed = 0, notRegistrable = 0; for (const l of lines.slice(1)) { if (!l || domains.length >= N) continue; const c = l.split(','); listed++; const org = registrable(c[iDomain].toLowerCase()); if (!org) { notRegistrable++; continue; } if (seen.has(org)) continue; seen.add(org); domains.push({ rank: Number(c[iRank]), domain: org, listedAs: c[iDomain].toLowerCase() }); } const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); async function query(name) { for (let attempt = 1; attempt <= 3; attempt++) { try { const res = await fetch(`${RESOLVER}?name=${encodeURIComponent(name)}&type=TXT`, { headers: { accept: 'application/dns-json' } }); if (!res.ok) throw new Error(`HTTP ${res.status}`); const j = await res.json(); // Status: 0 NOERROR, 3 NXDOMAIN, 2 SERVFAIL ... const txt = (j.Answer ?? []).filter((a) => a.type === 16).map((a) => // TXT data arrives as one or more quoted strings; join them as RFC 7208/7489 require. a.data.match(/"((?:[^"\\]|\\.)*)"/g)?.map((s) => s.slice(1, -1).replace(/\\(.)/g, '$1')).join('') ?? a.data); return { status: j.Status, txt, error: null }; } catch (e) { if (attempt === 3) return { status: null, txt: [], error: String(e.message ?? e) }; await sleep(1000 * attempt); } } } (async () => { const records = new Array(domains.length); let next = 0; const started = new Date().toISOString(); async function worker() { while (next < domains.length) { const i = next++; const d = domains[i]; const r = await query(`_dmarc.${d.domain}`); records[i] = { rank: d.rank, domain: d.domain, listedAs: d.listedAs, status: r.status, txt: r.txt, error: r.error }; if ((i + 1) % 500 === 0) console.error(`${i + 1}/${domains.length}`); await sleep(PAUSE_MS); } } await Promise.all(Array.from({ length: CONCURRENCY }, worker)); const snapshot = { description: `Published DMARC TXT records at _dmarc. for the top ${N} domains of the Majestic Million`, list: { name: 'Majestic Million', publisher: 'Majestic-12 Ltd', licence: 'CC BY 3.0, https://creativecommons.org/licenses/by/3.0/', source: 'https://downloads.majestic.com/majestic_million.csv', sha256: csvSha, bytes: buf.length, }, publicSuffixList: { source: 'https://publicsuffix.org/list/public_suffix_list.dat', licence: 'MPL 2.0', sha256: pslSha }, listEntriesRead: listed, listEntriesWithoutRegistrableDomain: notRegistrable, resolver: RESOLVER, collectedFrom: started, collectedTo: new Date().toISOString(), records, }; fs.writeFileSync(outFile, JSON.stringify(snapshot, null, 1) + '\n'); console.error(`done: ${records.length} domains, ${records.filter((r) => r.error).length} lookup errors`); })();