247CTF // Signed, Not Encrypted

247CTF // Signed, Not Encrypted - Flask Session Cookie Disclosure

Category: crypto-misuse,???? (information disclosure)

Stack: Python, Flask (itsdangerous-signed session cookie)


STEPS
  1. Send GET /flag with no parameters - the app answers "Incorrect secret key!" but sets a session cookie
  2. Split the cookie on . - three segments: payload, timestamp, signature
  3. Base64url-decode the first segment (restore = padding)
  4. If the result starts with eJ it is zlib-compressed - decompress before parsing the JSON
  5. Unwrap the flag value - Flask serializes bytes objects as an inner base64 string inside the JSON
  6. The flag field is present in plaintext - no secret key ever needed

1. The target


@app.route("/flag")

def index():

    secret_key = secret_key_to_int(request.args['secret_key']) \

        if 'secret_key' in request.args else None

    session['flag'] = flag          # (1) unconditional

    if secret_key == app.config['SECRET_KEY']:   # (2) the "real" check

        return session['flag']

    else:

        return "Incorrect secret key!"

A 24-byte os.urandom secret key, an integer comparison against it, and a stern

"Incorrect secret key!" for anyone who guesses wrong.

2. The bug, in two layers

Layer 1 - the business-logic hole. Line (1) runs before line (2). The flag is

committed to the session for *every* visitor, correct key or not. The conditional

only decides whether the flag is *also* returned in the response body. Anything

that touches the session cookie - which Flask sets on the response regardless -

carries the flag out the door.

Layer 2 - the crypto misunderstanding. Flask's session cookie is a

signed, unencrypted container:


cookie = base64url( payload ) + "." + base64url( timestamp ) + "." + HMAC-SHA1( signature )

payload = zlib( json )      # compressed when beneficial

The signature proves nobody *tampered* with the payload. It does nothing to hide

it. base64 is an encoding, zlib is an encoding - neither is a cipher. The payload

is plaintext under two reversible dresses.

The secret_key_to_int comparison is a decoy engineered to look like the puzzle:

in Python 3, int == bytes is never True, so the "correct" branch is

unreachable by design. Every minute spent reasoning about the comparison is a

minute not spent looking at the response headers.

3. The discovery path

  1. 1. Request /flag with no parameters. Status 200. Body: "Incorrect secret

key!". Headers: Set-Cookie: session=eyJ... - the flag left the building

before the check even ran.

  1. 2. Split the cookie on . - three segments: payload, timestamp, signature.

Only the first matters.

  1. 3. Decode. base64url-decode the payload (restore = padding). Two shapes:

- starts with eyJ -> raw JSON ({"flag": ...}), read directly.

- starts with eJ -> zlib-compressed (the eJyr prefix is zlib's magic);

zlib.decompress then parse JSON.

  1. 4. Read the flag. No key, no forgery, no crypto - just decoding.

4. Variants and gotchas

  • The double-encoded object. Flask serializes the flag value as bytes; the

JSON contains a nested base64 wrapper: {"flag":{" b":"<base64>"}}. Decode

twice. This trips people who stop at the first JSON layer and report "the flag

is corrupted".

  • Timestamp segment. The middle segment is the issue time - useful for

diagnosing session rotation, irrelevant to the read.

  • Forgery is not needed and not possible without the key. Resist the reflex:

the goal was to *read*, not to *write*. Challenges that hand you a valid

anonymous token and ask for an admin value are testing whether you notice the

token already differs from yours in exactly one field.

5. Generalized hunting pattern

When a Flask (or Rails, or any signed-cookie) app presents a "guess the secret"

puzzle:

  1. 1. What is in my session *right now*?
  2. 2. Is the protected value placed into client-visible state before the check?
  3. 3. Does the "wrong answer" path still mint a session?

Any "yes" ends the challenge. The signed-cookie primitive - *readable by everyone,

writable by no one without the key* - is routinely mistaken for confidentiality.

6. Defense

  • Never store secrets in client-readable cookies; server-side sessions with an

opaque ID are the default-correct pattern.

  • If client state must carry sensitive data, encrypt (Fernet) in addition to

signing - and audit every session[...] = for pre-auth placement.

  • Treat "the attacker can read but not forge" as a broken assumption in threat

models: values that must stay server-confidential do not belong in a signed

cookie, period.

Comments

Popular Posts