247CTF // Administrative ORM - Predictable Tokens, Debug Leaks, and One Shared Connection

247CTF // Administrative ORM - Predictable Tokens, Debug Leaks, and One Shared Connection

Category: crypto prediction, information disclosure, thread-safety

Stack: PHP, MySQL (uuid()), custom ORM, pymysql


STEPS
  1. Read the debug page: it exposes the reset timestamp, clock sequence, and an ifconfig block with the MAC
  2. Recognize MySQL uuid() as v1 - timestamp + clock_seq + node are the entire key schedule
  3. Trigger a rotation with an invalid code - the server regenerates the code and refreshes the timestamp leak
  4. Reconstruct the UUID offline per RFC 4122: 100ns-interval timestamp, version and variant bits, leaked clock_seq and MAC
  5. Submit the reconstructed code - if sub-microsecond skew misses, the failed attempt mints a fresh code with an exact timestamp; repeat (self-correcting)
  6. With the password reset, fetch the flag

1. The target

A user-management API: /update_password (reset-code flow), /get_flag

(bcrypt password check for admin), and a /statistics "TODO: remove before

production" debug page. The reset code is generated with MySQL uuid().

2. The core bug: v1 UUIDs are identifiers, not secrets

MySQL's uuid() implements UUIDv1:


| time_low (32) | time_mid (16) | time_hi (12+ver) | clock_seq (14) | node (48) |

  • timestamp: 100-nanosecond intervals since 1582-10-15, set at generation.
  • clock_seq: a per-server 14-bit value, initialized once at startup.
  • node: the MAC address of the host (or a random multicast value if no NIC).

All three components leak through the debug page: an embedded ifconfig gives the

MAC; clock_sequence is printed; last_reset gives the generation timestamp at

apparently nanosecond precision. Reconstruct per RFC 4122:


uuid_time  = (ns_since_epoch // 100) + 0x01b21dd213814000

time_low   = uuid_time & 0xffffffff

time_mid   = (uuid_time >> 32) & 0xffff

time_hi    = ((uuid_time >> 48) & 0x0fff) | 0x1000   # version 1

clock_seq  = ((cs >> 8) | 0x80, cs & 0xff)           # variant bits

uuid       = f"{tl:08x}-{tm:04x}-{th:04x}-{csh:02x}{csl:02x}-{node:012x}"

Submit it as the reset code before anyone else rotates the token.

3. The self-correcting oracle

The best property of this bug is that failed guesses are fuel. Every invalid attempt

*regenerates* the code and refreshes the last_reset timestamp. The loop is:

  1. 1. POST an invalid code -> server sets reset_code = uuid(T1), last_reset = T1.
  2. 2. Read /statistics -> harvest T1, clock_seq, MAC.
  3. 3. Reconstruct uuid(T1), submit.
  4. 4. If reconstruction is off by a microsecond (clock skew between uuid()'s internal

read and the debug hook's timestamp), the guess fails - but the failure mints

a fresh code with a fresh, *exactly recorded* T2. Converges in two rounds.

You never need to guess across a time window; you always guess the *immediately

preceding* generation, whose timestamp is known to the nanosecond.

4. The secondary bug: one connection, many threads

The ORM keeps a single pymysql connection in the app config and every request

thread shares it. pymysql connections are not thread-safe: two threads issuing

execute/fetchone concurrently interleave packets on one socket. The observable

outcomes, both seen live:

  • Result-swapped queries. A /get_flag fetch can return the row another

thread's /update_password query produced, or vice versa. With careful request

pairing this is itself an exploitation primitive (authenticate with someone

else's result set), though the UUID route is cleaner.

  • Protocol desync -> total death. A partial interleave corrupts the MySQL

protocol state; every subsequent request on the connection throws. There is no

reconnect logic, so the app stays dead until the worker restarts. This is how

aggressive concurrency testing on a shared instance becomes a denial of service

- a real finding to report, not just a CTF footnote.

The fix is a connection pool with per-checkout semantics, or a lock around the

connection plus reconnect-on-error. "One global connection" is a pattern that only

works in a world of one request at a time - i.e., never.

5. Discovery path

  1. 1. Inventory the debug surface. /statistics self-describes as temporary;

treat every field as an oracle. clock_sequence + ifconfig + last_reset is

the whole key schedule of uuidv1.

  1. 2. Confirm rotation semantics. Send one bad code; watch the debug fields move.

Confirm good codes stop the rotation (else the attack window never stabilizes).

  1. 3. Reconstruct offline against a known sample - validate your RFC-4122 math on

the token you *don't* need before burning attempts on the one you do.

  1. 4. Converge. Round 1 fails on skew; round 2 lands. Then /get_flag with the

new password.

6. What this teaches

  • v1 UUIDs are a keyed construction, not a random one. Timestamp + seq + node

is the entire key schedule, and real deployments leak all three via logs, debug

pages, and container metadata. Use random_bytes(16) (v4) for anything secret.

  • Failure-driven rotation is an oracle amplifier. When a wrong guess mints a

fresh secret *and* reveals its parameters, you get a chosen-plaintext game

instead of a guessing game. Design review question: "what does a failed attempt

reveal?"

  • Shared connections are shared fate. Thread-unsafe resources held globally

are both an exploit primitive (result swapping) and a reliability bomb

(permanent desync). Either is a valid bug report on its own.

Comments

Popular Posts