> 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/heap0.md).

# Heap0

### Heap0 — PicoCTF Walkthrough | Binary Exploitation

First thing we do is read the source code carefully and understand the memory layout.

The program allocates two buffers on the heap:

* `input_data` → 5 bytes → contains `"pico"`
* `safe_var` → 5 bytes → contains `"bico"`

These allocations happen one after another, so in memory they are placed **adjacent**:

```
[input_data][safe_var]
```

***

### Goal

The win condition is inside `check_win()`:

```
if (strcmp(safe_var, "bico") != 0)
```

So to win:

```
safe_var must NOT be "bico"
```

That means we need to overwrite it.

***

### Vulnerability

The bug is here:

```
scanf("%s", input_data);
```

This reads user input **without any bounds checking**.

Even though `input_data` is only 5 bytes, you can input way more → causing a **heap buffer overflow**.

***

### Memory Insight

At runtime, the program even shows heap addresses:

```
input_data: 0x...2b0
safe_var:   0x...2d0
```

Difference:

```
0x2d0 - 0x2b0 = 0x20 = 32 bytes
```

So:

* 32 bytes → gap between buffers
* After that → `safe_var`

***

### Exploit Idea

We overflow `input_data` until we reach `safe_var`, then overwrite it.

Payload structure:

```
"A" * 32 + "AAAA"
```

Even simpler:

```
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
```

(32+ bytes is enough to corrupt `safe_var`)

***

### Why This Works

* First 32 bytes → fill the gap
* Next bytes → overwrite `safe_var`
* `safe_var != "bico"` → condition becomes true
* Program prints the flag

***

### Exploitation Steps

1. Run the program
2. Choose option `2` (write buffer)
3. Enter payload:

```
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
```

4. Choose option `4` (print flag)

***

### Key Takeaway

Heap overflows are about **adjacent memory corruption**.

Instead of guessing, always calculate:

```
distance = target - source
payload = "A" * distance + overwrite_data
```

That’s it — simple overflow, clean win.


---

# 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/heap0.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.
