$ cat writeup.md…
$ cat writeup.md…
fortid2026
Task: a flag is Koblitz-encoded to EC points, then each point is run through the Dragon-Crypto dragon_render turtle routine with a secret key; output is the turtles' end coordinates. Solution: exploit a shared-turtle bug where heading persists across calls so each point's x shift is size*ux_i with ux_i small and fully determined by the iteration count; simulate the deterministic turn sequence, pin size from valid-Koblitz-x constraints on the large points, and decode using x only.
Hic Sunt Dracones
English summary: The handout contains chall.py, patch.py, output.txt and a Dockerfile. The Dockerfile clones awnonbhowmik/Dragon-Crypto (commit 046ad8d) and applies patch.py. chall.py encodes the flag onto an elliptic curve with Koblitz encoding, pushes each point through the library's "dragon curve" turtle renderer using a random private key, and prints the end coordinates of every rendered point. The goal is to recover the flag (format FortID{...}) from output.txt.
p with p % 4 == 3 and random curve parameters a, b are chosen.koblitz_encoder(flag, a, b, p) maps every plaintext character m (its ord) to an EC point (x_m, y_m) where x_m = m*k + j with k = 20 and j the first value in 1..19 that yields a point on the curve y² = x³ + a·x + b (mod p).private_key = (size, iterations, angle, a, b) with:
size = randrange(1<<127, 1<<128) — a huge ~128-bit integer,iterations = randrange(10, 15) — i.e. one of {10,11,12,13,14},angle = randrange(2) * 180 — i.e. 0 or 180.dragon.dragon_render(point, private_key), which drives a turtle to draw a dragon curve and returns the turtle's end coordinates (x_end, y_end).point_to_character(encrypted) → "X" + x-values joined by "X" followed by "Y" + y-values joined by "Y".patch.py edits koblitz.py to:
p (instead of the library's hardcoded p = 751), andpow(n, (p+1)//4, p), which is a valid square root when p % 4 == 3.The patch is only there to make a 256-bit prime feasible; it does not change the structural weaknesses below.
koblitz_decoder computes char = floor((x - 1) / 20) and ignores y entirely. So the entire Y...Y half of the output is irrelevant — we only need to recover each point's x-coordinate well enough to decode.
chall.py installs a single HeadlessTurtle object into dragon.ob once, and it is shared across every dragon_render call. Inside dragon_render:
ob.penup() ob.goto(x_start, y_start) # resets ONLY x and y ob.pendown() ob.left(angle) # heading accumulates ... # forward/left/right over the deterministic step sequence
goto() resets x, y but never resets heading. Therefore the turtle's heading accumulates across successive points instead of starting fresh each time.
size * ux_iturn_sequence_generator(private_key) depends only on iterations (it is fully deterministic). Every forward move translates the turtle by a fixed size along the current axis-aligned heading (0/90/180/270). So for point i:
x_end_i = x_m_i + size * ux_i
where ux_i is a small integer (net forward steps along the x-axis, positive at heading 0, negative at heading 180). Because the heading carries over between points, ux_i alternates: for this instance half the points had ux_i = 0 and half had ux_i = ±128.
Since x_m_i (a valid Koblitz x) is small — at most a few thousand — while size is ~128 bits:
ux_i = 0 give x_end_i = x_m_i directly (decode immediately), andux_i = ±128 give a huge x_end_i, from which size can be pinned.Parse output.txt with dragon.character_to_point to get (x_end, y_end) pairs; keep only the x list. (78 points here: 39 small, 39 huge.)
Emulate the shared turtle per-point unit x-displacement. For a candidate iterations, build the deterministic step sequence once and walk it for each point with a single persistent heading variable (initialized once, not reset between points), tracking only the net x displacement in units of size (forward at heading 0 → +1, at heading 180 → -1; l/r adjust heading by ±90). This yields the ux_i vector without knowing size.
Identify iterations. Try each iterations ∈ {10,...,14}. Only iterations = 14 produces a ux_i pattern consistent with a single feasible size (see next step); the others fail.
Pin size. For any huge point, size = (x_end_i − x_m_i) / ux_i. We do not know x_m_i, but it must be a small valid Koblitz x. Enumerate small x_m on one huge point to list candidate sizes, then require that for all points simultaneously x_m_i = x_end_i − size·ux_i lands in the valid small range [1, 3000] and decodes to a printable character. This uniquely pins size.
Decode. For every point, x_m = x_end − size·ux, then char = floor((x_m − 1)/20). Concatenate to recover the flag (FortID{REDACTED}).
#!/usr/bin/env python3 import sys, types, math # Stub the turtle module and load the (patched) Dragon-Crypto library. sys.modules["turtle"] = types.ModuleType("turtle") sys.path.insert(0, "dragon-crypto") import dragon out = open("output.txt").read().strip() points = dragon.character_to_point(out) # [(x_end, y_end), ...] xs = [p[0] for p in points] N = len(points) K = 20 def simulate(iterations, angle0): """Per-point net x displacement in units of `size`, with a SINGLE persistent heading shared across all points (the challenge bug).""" heading = 0 pk = (1, iterations, angle0, 0, 0) steps = dragon.turn_sequence_generator(pk)[:-1] units = [] for _ in range(N): heading += angle0 dx = 0 for s in steps: if s == 'f': h = heading % 360 if h == 0: dx += 1 elif h == 180: dx -= 1 elif s == 'r': heading -= 90 elif s == 'l': heading += 90 units.append(dx) return units def recover(iterations, angle0): ux = simulate(iterations, angle0) # Pick any point with |ux|>0 to enumerate candidate sizes. i0 = next(i for i in range(N) if ux[i] != 0) cands = {(xs[i0] - xm) // ux[i0] for xm in range(1, 3000) if (xs[i0] - xm) % ux[i0] == 0} for size in cands: chars = [] ok = True for i in range(N): x_m = xs[i] - size * ux[i] if not (1 <= x_m <= 3000): ok = False; break chars.append(chr(math.floor((x_m - 1) / K))) if ok and all(32 <= ord(c) < 127 for c in chars): return size, "".join(chars) return None for iterations in range(10, 15): for angle0 in (0, 180): res = recover(iterations, angle0) if res: size, flag = res print(f"iterations={iterations} angle={angle0} size={size}") print("FLAG:", flag) # FortID{REDACTED} raise SystemExit
Use this approach when:
awnonbhowmik/Dragon-Crypto) to a specific commit and patches it — inspect the patch and the library's state model, not just the math.turtle module is stubbed by a HeadlessTurtle whose state object is created once and shared — a classic shared-mutable-state bug where only position is reset and heading/direction persists across calls.floor((x - 1) / k) — y is never needed, so you only have to recover x-coordinates.plaintext + size·ux with a fixed-magnitude ux).size) can be pinned by requiring that subtracting size·ux_i lands every point in a small valid range — a single simultaneous constraint uniquely fixes the key.$ cat /etc/motd
Liked this one?
Pro unlocks every complete writeup and expanded API access. $9/mo.
$ cat pricing.md$ grep --similar