Prefix oracle
The supplied solver tries one candidate character at a time, encoding it as hexadecimal and appending it to the known prefix. Each guess uses a fresh connection, authenticates, then submits the accumulated hex string.
The script treats a response containing F as a rejection. Otherwise it keeps the candidate and advances to the next character.
Solver
This version keeps the original guessing logic while taking the challenge endpoint and token from environment variables:
import os
import socket
import string
host = os.environ["CHALLENGE_HOST"]
port = int(os.environ.get("CHALLENGE_PORT", "9000"))
token = os.environ["CHALLENGE_TOKEN"].encode()
alphabet = string.ascii_lowercase + string.ascii_uppercase + string.digits + "{} "
prefix = ""
while True:
for char in alphabet:
candidate = prefix + format(ord(char), "x")
with socket.create_connection((host, port), timeout=10) as sock:
sock.recv(1024)
sock.sendall(token + b"\n")
sock.recv(1024)
sock.sendall(candidate.encode("ascii") + b"\n")
response = sock.recv(1024)
if b"F" not in response:
prefix = candidate
print(prefix, flush=True)
if char == "}":
print(bytes.fromhex(prefix).decode())
raise SystemExit
break
else:
raise RuntimeError("No candidate accepted; check the oracle response")
Run it with the original challenge’s host, port and connection token. The recovered hex prefix can be decoded with bytes.fromhex(prefix).decode().
The original file warns to stop if 61 keeps repeating and then decode the hex. It does not contain a recovered flag. Its outer-loop comparison also mixes hex(ord("}")) (with an 0x prefix) and candidates without that prefix; the version above stops when the recovered character is }.