#!/usr/bin/env python3 """CodeShell.kr - Terminal (Misc, 400p) solver. Each record's `from` field is the parent packet id, so the 687 records form a lineage rooted at the Origin id 000000000000. The plaintext byte of a record is data XOR SHA256(bytes.fromhex(from))[0]. Walking from the target packet back to the Origin and printing in reverse recovers the flag. """ import hashlib import json
defmain(): recs = [json.loads(line) for line inopen(CAPTURE)] by_id = {r["id"]: r for r in recs}
# the id formula is checkable, so verify it before trusting the lineage ok = 0 for r in recs: body = {k: v for k, v in r.items() if k != "id"} h = hashlib.sha256(json.dumps(body, sort_keys=True, separators=(",", ":")).encode()).hexdigest() ok += h[:12] == r["id"] print(f"id formula matches {ok}/{len(recs)}")
cur, lineage = by_id[TARGET], [] whileTrue: lineage.append(cur) if cur["from"] == ORIGIN: break cur = by_id[cur["from"]] print("lineage length:", len(lineage)) print("".join(chr(int(r["data"], 16) ^ mask(r["from"])) for r inreversed(lineage)))