Genuine samples found (3 of the 107 NO SAMPLE):
- 005 (.3gx) via DuckDuckGo: real 3GPP2 file (ftyp brand 3g2a), +magic fix to ftyp3g2
- 373 (.m4v) via DuckDuckGo: complete 1.85 MB .m4v (ftyp brand M4V ), NOT truncated
- 544 (.php4) via Wayback (archived task page -> live file): genuine PHP source; derivation set to text
200 (.EXS) partial -> pass: real EXS24 decode from spec (big-endian SOBT header, 6 fields)
False positives caught and removed by strict validation:
- 51 .axf (Markdown spec), 52 .b (CSS), 130 .dba (SQL .bak), 168 .ecc (404 text),
190 .esd (31-byte text fragment), 545 .phr (SVG image), 719 .taz (Intel HEX),
774 .vdi (QEMU image), 24 .amf (explanatory text, again), 701 .svc (Wayback
capture truncated: 5 MB of 138 MB original), 789 .vrb (HTML page with BOM)
Validation hardened: truncation-at-cap check, no \d{4,} exception in
looks_like_document, UTF-8 BOM stripping, existing files re-validated.
Task 55 (.BAK) deliberately not sampled (generic ext, layout mismatch).
Still no genuine sample: 024 .amf (binary AMF3, no public sample), 701 .svc,
789 .vrb, and ~100 other obscure/proprietary formats.
419 lines
18 KiB
Python
419 lines
18 KiB
Python
#!/usr/bin/env python3
|
|
"""ddg_fetch.py — download and validate DuckDuckGo candidate sample files.
|
|
|
|
Reads tools/ddg_candidates.json, tries each candidate URL, and keeps the
|
|
first genuine sample per task.
|
|
|
|
A candidate URL is either a direct file link or a page. For a page, all
|
|
hrefs ending in the target extension (or containing it + "download")
|
|
are tried as direct file links.
|
|
|
|
A download is genuine only if:
|
|
- non-empty, <= 50 MiB, and not an HTML/JS/PDF error page (magic check);
|
|
- ./sfile identifies it as the target task (identified_task == task),
|
|
OR its magic matches the table magic for that task (extension-only
|
|
formats whose sample sfile cannot confirm by magic alone).
|
|
|
|
Genuine samples are saved as samples/task-NNN<ext> (lowercase ext) so
|
|
tests/run_tests.py picks them up. Bad URLs are appended to
|
|
tools/bad_page_urls.json. A report goes to tools/ddg_fetch_report.json.
|
|
|
|
Usage:
|
|
python3 tools/ddg_fetch.py [candidates.json]
|
|
"""
|
|
import json
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
import urllib.parse
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
CAND = sys.argv[1] if len(sys.argv) > 1 else os.path.join(ROOT, "tools", "ddg_candidates.json")
|
|
SAMPLES = os.path.join(ROOT, "samples")
|
|
SFILE = os.path.join(ROOT, "sfile")
|
|
BAD = os.path.join(ROOT, "tools", "bad_page_urls.json")
|
|
REPORT = os.path.join(ROOT, "tools", "ddg_fetch_report.json")
|
|
UA = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36"
|
|
MAX_BYTES = 50 * 1024 * 1024
|
|
|
|
# Tasks deliberately not sampled (documented judgment calls — do not re-fetch).
|
|
# 55 (.BAK): 50 MB SQL Server backup; .BAK is a generic extension and the
|
|
# table field layout does not match the real MSSQLBAK header, so the
|
|
# decode would be misleading. Left honestly not-found.
|
|
SKIP_TASKS = {55}
|
|
SKIP_DOMAINS = {
|
|
"duckduckgo.com", "www.duckduckgo.com", "google.com", "www.google.com",
|
|
"bing.com", "www.bing.com", "wikipedia.org", "en.wikipedia.org",
|
|
"youtube.com", "www.youtube.com",
|
|
"stackoverflow.com", "www.stackoverflow.com", "reddit.com", "www.reddit.com",
|
|
"quora.com", "www.quora.com", "amazon.com", "www.amazon.com",
|
|
}
|
|
# NOTE: github.com is deliberately NOT skipped — blob URLs are rewritten
|
|
# to raw.githubusercontent.com (see github_raw) and downloaded directly.
|
|
|
|
# Real-world extension aliases: the file on disk often uses a sibling
|
|
# extension to the table ext (e.g. 3GPP2 files are .3g2, not .3gx).
|
|
ALIASES = {
|
|
5: [".3g2", ".3gp"],
|
|
373: [".mp4"],
|
|
}
|
|
|
|
# URL path suffixes that are pages, not files.
|
|
PAGE_SUFFIXES = (".html", ".htm", ".php", ".asp", ".aspx", ".jsp", ".cgi", "/")
|
|
|
|
|
|
def github_raw(url):
|
|
"""github.com/<owner>/<repo>/blob/<branch>/<path> ->
|
|
raw.githubusercontent.com/<owner>/<repo>/<branch>/<path>.
|
|
Returns the raw URL, or None if not a blob URL."""
|
|
m = re.match(r"https?://(?:www\.)?github\.com/([^/]+)/([^/]+)/blob/([^/]+)/(.+)$", url)
|
|
if not m:
|
|
return None
|
|
owner, repo, branch, path = m.groups()
|
|
return f"https://raw.githubusercontent.com/{owner}/{repo}/{branch}/{path}"
|
|
|
|
|
|
def is_direct_file_url(url):
|
|
"""True if the URL looks like a direct file (not a web page)."""
|
|
if url.startswith("https://raw.githubusercontent.com/"):
|
|
return True
|
|
path = urllib.parse.urlparse(url).path.lower()
|
|
if not path or path.endswith(PAGE_SUFFIXES):
|
|
return False
|
|
# A path with a file-like extension (not a page) is a direct file.
|
|
return bool(re.search(r"\.[a-z0-9]{1,5}$", path))
|
|
|
|
|
|
def run(cmd, timeout=60):
|
|
try:
|
|
r = subprocess.run(cmd, capture_output=True, timeout=timeout)
|
|
return r.returncode, r.stdout, r.stderr
|
|
except Exception:
|
|
return 1, b"", b""
|
|
|
|
|
|
def fetch(url, path, max_bytes=MAX_BYTES, timeout=45):
|
|
"""curl a URL to a file. Returns True if a non-empty file was written."""
|
|
rc, _, _ = run(["curl", "-sL", "--max-time", str(timeout),
|
|
"--max-filesize", str(max_bytes), "-A", UA, "-o", path, url],
|
|
timeout=timeout + 15)
|
|
return os.path.exists(path) and os.path.getsize(path) > 0
|
|
|
|
|
|
def junk_reason(path, ext):
|
|
"""Same junk rules as tests/run_tests.py, plus JS and PDF."""
|
|
try:
|
|
sz = os.path.getsize(path)
|
|
except OSError:
|
|
return "missing"
|
|
if sz == 0:
|
|
return "empty file"
|
|
with open(path, "rb") as fh:
|
|
head = fh.read(256)
|
|
low = head.lstrip().lower()
|
|
if low.startswith(b"404") or b"404 page not found" in low[:64] or b"404 not found" in low[:64]:
|
|
return "404 error page"
|
|
if low.startswith(b"<!doctype") or low.startswith(b"<html") or low.startswith(b"<head"):
|
|
if ext.lower() not in (".html", ".htm", ".xhtml", ".shtml"):
|
|
return "HTML error page"
|
|
if low.startswith(b"%pdf"):
|
|
if ext.lower() != ".pdf":
|
|
return "PDF document, not a sample"
|
|
if low.startswith(b"//") or b"function(" in head[:256] and ext.lower() == ".js":
|
|
return "JavaScript, not a sample"
|
|
if head[:2] == b"PK" and ext.lower() not in (".zip",):
|
|
# A ZIP where the format is not ZIP-based is suspicious, but ZIP
|
|
# containers are legitimate for many formats — let sfile decide.
|
|
pass
|
|
return None
|
|
|
|
|
|
def sfile_identifies(path, task, formats):
|
|
"""Run ./sfile on the file. Returns (identified_task, decoded, output)."""
|
|
rc, out, _ = run([SFILE, path], timeout=30)
|
|
text = out.decode(errors="replace")
|
|
m = re.search(r"— task (\d+)", text)
|
|
identified = int(m.group(1)) if m else None
|
|
has_table = bool(re.search(r"offset\s+size\s+field", text))
|
|
nfields = len(re.findall(r"^\s+\d+\s+\d+\s+\S+", text, re.M))
|
|
decoded = bool(has_table and nfields >= 1) or bool(re.search(r"Document type:", text))
|
|
return identified, decoded, text
|
|
|
|
|
|
def looks_like_document(path):
|
|
"""True if the file is clearly a text document (Markdown, CSS, JS,
|
|
HTML, JSON, XML) rather than a binary sample. Used to reject false
|
|
positives for formats with no magic, where sfile can only identify
|
|
by extension and would accept any file with the right suffix."""
|
|
try:
|
|
with open(path, "rb") as fh:
|
|
head = fh.read(512)
|
|
except OSError:
|
|
return True
|
|
if not head:
|
|
return True
|
|
# Strip a UTF-8 BOM so an HTML page saved with one is still detected.
|
|
if head.startswith(b"\xef\xbb\xbf"):
|
|
head = head[3:]
|
|
low = head.lstrip().lower()
|
|
if low.startswith((b"<html", b"<!doctype", b"<head", b"<?xml")):
|
|
return True
|
|
if low.startswith(b"# ") or low.startswith(b"#\n"):
|
|
return True # Markdown heading
|
|
if low.startswith(b"/*") or b"{\n" in head[:200] and b":" in head[:200] and b";" in head[:200]:
|
|
return True # CSS
|
|
if low.startswith((b"function ", b"const ", b"var ", b"import ", b"//")):
|
|
return True # JS / code
|
|
if low.startswith((b"{", b"[")) and b'"' in head[:200]:
|
|
return True # JSON
|
|
# Mostly-printable ASCII that reads like prose/code, not binary data.
|
|
# No minimum length: even a short pure-ASCII fragment (e.g. a 31-byte
|
|
# "--format documentation" snippet) is a text file, not a binary sample.
|
|
# No digit-sequence exception: a text document that happens to contain
|
|
# a long number (or a hex dump) is still a text document, not a binary
|
|
# sample. Text formats are exempt upstream, so this only fires for
|
|
# binary/no-magic formats.
|
|
try:
|
|
text = head.decode("ascii")
|
|
except UnicodeDecodeError:
|
|
return False
|
|
printable = sum(1 for c in text if c.isprintable() or c in "\n\r\t")
|
|
if printable / max(len(text), 1) > 0.95 and b"\x00" not in head:
|
|
return True
|
|
return False
|
|
|
|
|
|
def content_ok(path, task, formats, source_url=None):
|
|
"""Strict validation for a candidate sample.
|
|
- Source check: if the source URL's filename extension differs from
|
|
the target ext AND is itself a known format extension, the file is
|
|
a different format, not this one (e.g. a .bak file offered as .dba).
|
|
- Format has a magic: the file's bytes MUST match it AND sfile must
|
|
identify it as this task (magic + extension disambiguation).
|
|
- Format has no magic: sfile must identify it as this task AND the
|
|
file must not be an obvious wrong-kind text document (for binary
|
|
formats; text formats are exempt).
|
|
Returns (ok, reason)."""
|
|
# Truncation: a file exactly at the download cap was cut off by curl
|
|
# and is incomplete (e.g. a 50 MiB .m4v whose mdat claims 71 MB).
|
|
try:
|
|
if os.path.getsize(path) >= MAX_BYTES:
|
|
return False, "truncated at download size cap"
|
|
except OSError:
|
|
return False, "missing"
|
|
fm = formats.get(str(task), {})
|
|
if source_url:
|
|
src_ext = os.path.splitext(urllib.parse.urlparse(source_url).path)[1].lower()
|
|
target_ext = (fm.get("ext") or "").lower()
|
|
if src_ext and target_ext and src_ext != target_ext:
|
|
known = { (f.get("ext") or "").lower() for f in formats.values() }
|
|
if src_ext in known:
|
|
return False, f"source file is {src_ext}, not {target_ext}"
|
|
identified, decoded, _ = sfile_identifies(path, task, formats)
|
|
if identified != task:
|
|
return False, f"sfile identified task {identified}, not {task}"
|
|
if fm.get("magic"):
|
|
if not table_magic_matches(path, task, formats):
|
|
return False, "magic bytes do not match table magic"
|
|
return True, "magic+extension"
|
|
if fm.get("derivation") != "text" and looks_like_document(path):
|
|
return False, "looks like a text document, not a binary sample"
|
|
return True, "extension-only"
|
|
"""Run ./sfile on the file. Returns (identified_task, decoded, output)."""
|
|
rc, out, _ = run([SFILE, path], timeout=30)
|
|
text = out.decode(errors="replace")
|
|
m = re.search(r"— task (\d+)", text)
|
|
identified = int(m.group(1)) if m else None
|
|
has_table = bool(re.search(r"offset\s+size\s+field", text))
|
|
nfields = len(re.findall(r"^\s+\d+\s+\d+\s+\S+", text, re.M))
|
|
decoded = bool(has_table and nfields >= 1) or bool(re.search(r"Document type:", text))
|
|
return identified, decoded, text
|
|
|
|
|
|
def table_magic_matches(path, task, formats):
|
|
"""Does the file's leading bytes match the table magic for the task?"""
|
|
fm = formats.get(str(task), {})
|
|
magic = fm.get("magic")
|
|
off = fm.get("magic_off", 0) or 0
|
|
if not magic:
|
|
return False
|
|
try:
|
|
raw = bytes.fromhex(magic)
|
|
except ValueError:
|
|
return False
|
|
try:
|
|
with open(path, "rb") as fh:
|
|
fh.seek(off)
|
|
head = fh.read(len(raw))
|
|
except OSError:
|
|
return False
|
|
return head == raw
|
|
|
|
|
|
def extract_file_links(page_bytes, base_url, ext, task=None):
|
|
"""All hrefs in an HTML page that look like a direct file of this ext.
|
|
Accepts the table ext, its real-world aliases, and any href with
|
|
'download' in it that ends in a file-like (non-page) extension."""
|
|
text = page_bytes.decode(errors="replace")
|
|
ext_l = ext.lower()
|
|
accept = {ext_l}
|
|
for a in (ALIASES.get(task) or []):
|
|
accept.add(a.lower())
|
|
links = []
|
|
for m in re.finditer(r'''href\s*=\s*["']([^"']+)["']''', text, re.I):
|
|
href = m.group(1).strip()
|
|
if not href or href.startswith(("#", "javascript:", "mailto:")):
|
|
continue
|
|
full = urllib.parse.urljoin(base_url, href)
|
|
path = urllib.parse.urlparse(full).path.lower()
|
|
if any(path.endswith(a) for a in accept):
|
|
links.append(full)
|
|
elif "download" in full.lower() and is_direct_file_url(full):
|
|
links.append(full)
|
|
# Dedupe, preserve order.
|
|
seen, out = set(), []
|
|
for u in links:
|
|
if u not in seen:
|
|
seen.add(u)
|
|
out.append(u)
|
|
return out[:8]
|
|
|
|
|
|
def main():
|
|
cand = json.load(open(CAND))
|
|
formats = json.load(open(os.path.join(ROOT, "src", "formats.json")))
|
|
results = json.load(open(os.path.join(ROOT, "tests", "results.json")))
|
|
bad = json.load(open(BAD)) if os.path.exists(BAD) else {}
|
|
report = {}
|
|
if os.path.exists(REPORT):
|
|
report = json.load(open(REPORT))
|
|
|
|
tasks = sorted((int(k) for k in cand if k != "_special"))
|
|
for t in tasks:
|
|
key = str(t)
|
|
if t in SKIP_TASKS:
|
|
report[key] = {"status": "skipped",
|
|
"reason": "deliberately not sampled (see SKIP_TASKS)"}
|
|
continue
|
|
entry = cand[key]
|
|
ext = entry.get("ext", "")
|
|
if not ext:
|
|
fm = formats.get(key, {})
|
|
ext = (fm.get("ext") or "").lower()
|
|
# Already have a sample on disk? Re-validate it with the current
|
|
# strict logic (an earlier weak-validation run may have kept a
|
|
# false positive). Only trust it if it passes; otherwise delete it
|
|
# and try to find a genuine one.
|
|
fname = "task-%03d%s" % (t, ext.lower())
|
|
fpath = os.path.join(SAMPLES, fname)
|
|
if os.path.exists(fpath) and os.path.getsize(fpath) > 0:
|
|
ok, reason = content_ok(fpath, t, None)
|
|
if ok:
|
|
report[key] = {"status": "exists", "file": fname}
|
|
continue
|
|
os.remove(fpath)
|
|
print(" task %d: existing %s failed validation (%s) — removed"
|
|
% (t, fname, reason), flush=True)
|
|
if report.get(key, {}).get("status") == "kept":
|
|
continue
|
|
# Candidate URLs, deduped, in query order.
|
|
urls = []
|
|
seen = set()
|
|
for q, r in entry.get("queries", {}).items():
|
|
for res in r.get("results", []):
|
|
u = res.get("url", "")
|
|
if u and u not in seen:
|
|
seen.add(u)
|
|
urls.append(u)
|
|
# Drop known-bad domains and URLs already recorded bad for this task.
|
|
bad_urls = set(bad.get(key, {}).get("urls", []))
|
|
urls = [u for u in urls if u not in bad_urls
|
|
and urllib.parse.urlparse(u).netloc.lower() not in SKIP_DOMAINS]
|
|
kept = None
|
|
tried_bad = []
|
|
for url in urls[:12]:
|
|
# Temp file keeps the TARGET extension (leading dot hides it
|
|
# from the test runner) so sfile's extension disambiguation
|
|
# sees the right ext when validating the candidate.
|
|
tmp = os.path.join(SAMPLES, ".ddg_tmp_" + fname)
|
|
# GitHub blob page -> raw file URL (direct download).
|
|
raw = github_raw(url)
|
|
if raw:
|
|
url = raw
|
|
# Direct file link? (any file-like URL, not just the table ext —
|
|
# content validation decides whether it is the right format.)
|
|
direct = is_direct_file_url(url)
|
|
if direct:
|
|
if not fetch(url, tmp):
|
|
tried_bad.append((url, "download failed/empty"))
|
|
continue
|
|
else:
|
|
# Page: fetch and extract direct file links.
|
|
page = os.path.join(SAMPLES, fname + ".ddg_page")
|
|
if not fetch(url, page, max_bytes=5 * 1024 * 1024, timeout=30):
|
|
tried_bad.append((url, "page fetch failed"))
|
|
continue
|
|
with open(page, "rb") as fh:
|
|
links = extract_file_links(fh.read(), url, ext, task=t)
|
|
os.path.exists(page) and os.remove(page)
|
|
if not links:
|
|
tried_bad.append((url, "no direct file link on page"))
|
|
continue
|
|
got = False
|
|
for link in links:
|
|
link_raw = github_raw(link) or link
|
|
if fetch(link_raw, tmp):
|
|
got = True
|
|
url = link_raw
|
|
break
|
|
if not got:
|
|
tried_bad.append((url, "direct links failed to download"))
|
|
continue
|
|
# Validate the downloaded file. Cheap junk filter first, then
|
|
# strict content validation (magic must match when the format
|
|
# has one; no-magic formats must not be a document).
|
|
reason = junk_reason(tmp, ext)
|
|
if reason:
|
|
tried_bad.append((url, reason))
|
|
os.path.exists(tmp) and os.remove(tmp)
|
|
continue
|
|
ok, why = content_ok(tmp, t, formats, source_url=url)
|
|
if ok:
|
|
identified, decoded, _ = sfile_identifies(tmp, t, formats)
|
|
final = os.path.join(SAMPLES, fname)
|
|
os.rename(tmp, final)
|
|
kept = {"status": "kept", "file": fname, "url": url,
|
|
"identified_task": identified, "decoded": decoded,
|
|
"size": os.path.getsize(final), "validation": why}
|
|
break
|
|
tried_bad.append((url, why))
|
|
os.path.exists(tmp) and os.remove(tmp)
|
|
if kept:
|
|
report[key] = kept
|
|
print(f"task {t} [{ext}] KEPT {kept['file']} ({kept['size']} B) via {kept['url'][:80]}", flush=True)
|
|
else:
|
|
report[key] = {"status": "not-found", "tried": len(urls),
|
|
"bad": tried_bad[:12]}
|
|
# Record the bad URLs so they are not re-fetched.
|
|
if tried_bad:
|
|
rec = bad.get(key, {"ext": ext.upper(), "reason": "DDG candidates rejected", "urls": []})
|
|
have = set(rec.get("urls", []))
|
|
for u, _ in tried_bad:
|
|
if u not in have:
|
|
rec.setdefault("urls", []).append(u)
|
|
have.add(u)
|
|
bad[key] = rec
|
|
print(f"task {t} [{ext}] not-found (tried {len(urls)} candidates)", flush=True)
|
|
with open(REPORT, "w") as f:
|
|
json.dump(report, f, indent=1, sort_keys=True)
|
|
with open(BAD, "w") as f:
|
|
json.dump(bad, f, indent=1, sort_keys=True)
|
|
|
|
kept_n = sum(1 for v in report.values() if v.get("status", "").startswith("kept"))
|
|
print(f"DONE kept={kept_n} report -> {REPORT}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|