247CTF // The Flag API - Blind SQLi on an Exact Budget

247CTF // The Flag API - Blind SQLi on an Exact Budget

Category: blind SQL injection, query economics

Stack: PHP, SQLite, bcrypt


STEPS
  1. Mint one fresh token - failed logins never rotate the target password, only other endpoints do
  2. Build the probe: admin' AND SUBSTR(password,i,1) IN (...);-- - a boolean oracle on each hex digit
  3. Spend exactly 4 requests per character (128 total): four IN-lists ask the 8s/4s/2s/1s bit-membership questions
  4. Fire all 128 probes concurrently against the single token - order is irrelevant, index bookkeeping matters
  5. Assemble bits MSB-first into hex digits, sanity-check length 32
  6. Submit the recovered password to the flag endpoint

1. The target

A login endpoint taking username, password, and an api key; an admin account

whose password is a random 32-hex string; tokens that die after exactly 128

requests. bcrypt on the password check kills timing tricks on that field - so the

injection surface and the budget shape the whole attack.

2. The bug

String-concatenated SQL in the login path:


SELECT * FROM users WHERE username='<username>' AND password='<password>'

The response differs measurably between "no such row" and "row exists, wrong

password" - but the cleaner signal is which *form* of invalid you get back

("invalid credentials" vs "invalid api key"). Either way, <username> is a

boolean oracle: inject a tautology and the query shape changes predictably.

Two properties make the oracle ideal:

  1. 1. Boolean and stable. AND SUBSTR(password,i,1) IN (...) is answered by the

same two responses every time.

  1. 2. Non-destructive. Failed logins don't rotate the target password - only

get_token and get_flag do. All 128 probes can run against one fixed secret.

3. The extraction design: one bit per request

The hint - *"same character set and length as the flag (32-hex)"* - is a budget

statement. 128 requests ? 32 characters = exactly 4 requests per character, which

is the information-theoretic minimum for one hex digit (4 bits). The probe set:

requestasksbits recovered
... IN ('8','9','a','b','c','d','e','f')is bit3 (8s place) set?1
... IN ('4','5','6','7','c','d','e','f')is bit2 (4s place) set?1
... IN ('2','3','6','7','a','b','e','f')is bit1 (2s place) set?1
... IN ('1','3','5','7','9','b','d','f')is bit0 (1s place) set?1

The IN-list is just the set of hex digits with that bit set - no binary-search

branching, four flat membership tests per character, MSB first. The assembled

value is int(bits, 2) rendered as one hex digit.

This is the deep trick of the challenge: **design the query set so the number of

requests equals the information content, then let concurrency do the rest.** A

linear scan (16 candidates per character) needs ~270 requests on average - over

budget. A 4-probe bit decomposition needs exactly 128, every time, no variance.

4. Execution

  1. 1. Mint one token. Note that calibration traffic (wrong guesses on other endpoints)

spent some of the budget on earlier tokens - start clean.

  1. 2. Fire all 128 probes concurrently against that token (keep-alive sessions,

modest thread count - 16 parallel is plenty; the server is the bottleneck).

Each probe is independent; order doesn't matter, only the index bookkeeping.

  1. 3. Assemble digits; a final get_flag with the recovered password is the 129th

request - which is why the margin matters: any wasted probe costs a whole new

token *and* a password rotation. The bit-decomposition is exactly budgeted;

there's no slack for retries.

  1. 4. On the (rare) off-by-one from a race or truncated response: detect via a

final-length sanity check (32 hex chars), and if the assembled password fails,

re-extract only the characters whose probes had anomalous latency or errors.

5. Why the other surfaces don't matter

  • bcrypt timing: useless - the comparison happens only after the SQLi oracle

already leaked the password.

  • get_flag rotation-on-failure: a trap for brute-force instincts. The

challenge wants you to *measure* which endpoints are destructive and route

around them.

  • The api-key check order: validating the key *after* the password lookup is

the only reason a wrong-password login returns a distinguishable message with a

valid key - ordering bugs in auth pipelines are oracles too.

6. What this teaches

  • Parameterized queries close the class. Every layer of this attack - the

tautology bypass, the bit-oracle, the budget fitting - evaporates with prepared

statements. The writeup exists because one line used string interpolation.

  • Rate limits are design constraints, not walls. A 128-request budget with 128

bits of entropy is a tightrope, and the solution is to make each request carry

exactly one bit. When you can't have more requests, demand more from each one.

  • Concurrency is a first-class tool in blind extraction. 128 sequential probes

at 100ms each is 13 seconds; 128 concurrent probes is about one second. The

server's per-request cost doesn't change - your wall clock does.

  • Non-destructive oracles are worth cataloging. Before designing the extraction,

enumerate what each endpoint does to the secret on failure. "Nothing" is the

green light for long extractions.

Comments

Popular Posts