hacktricks/binary-exploitation/libc-heap/large-bin-attack.md

88 lines
5.2 KiB
Markdown
Raw Permalink Normal View History

2024-05-17 15:37:03 +00:00
# Large Bin Attack
2024-07-18 16:04:36 +00:00
{% hint style="success" %}
2024-07-18 16:14:56 +00:00
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2024-05-17 15:37:03 +00:00
2024-07-18 16:04:36 +00:00
<details>
2024-05-17 15:37:03 +00:00
2024-07-18 16:04:36 +00:00
<summary>Support HackTricks</summary>
2024-05-17 15:37:03 +00:00
2024-07-18 16:04:36 +00:00
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
2024-05-17 15:37:03 +00:00
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
2024-07-18 16:04:36 +00:00
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2024-05-17 15:37:03 +00:00
</details>
2024-07-18 16:04:36 +00:00
{% endhint %}
2024-05-17 15:37:03 +00:00
## Basic Information
2024-06-12 15:23:07 +00:00
For more information about what is a large bin check this page:
{% content-ref url="bins-and-memory-allocations.md" %}
[bins-and-memory-allocations.md](bins-and-memory-allocations.md)
{% endcontent-ref %}
2024-05-17 15:37:03 +00:00
It's possible to find a great example in [**how2heap - large bin attack**](https://github.com/shellphish/how2heap/blob/master/glibc\_2.35/large\_bin\_attack.c).
2024-06-12 15:23:07 +00:00
Basically here you can see how, in the latest "current" version of glibc (2.35), it's not checked: **`P->bk_nextsize`** allowing to modify an arbitrary address with the value of a large bin chunk if certain conditions are met.
2024-05-17 15:37:03 +00:00
In that example you can find the following conditions:
* A large chunk is allocated
* A large chunk smaller than the first one but in the same index is allocated
2024-06-12 15:23:07 +00:00
* Must be smalled so in the bin it must go first
2024-05-17 15:37:03 +00:00
* (A chunk to prevent merging with the top chunk is created)
* Then, the first large chunk is freed and a new chunk bigger than it is allocated -> Chunk1 goes to the large bin
* Then, the second large chunk is freed
* Now, the vulnerability: The attacker can modify `chunk1->bk_nextsize` to `[target-0x20]`
* Then, a larger chunk than chunk 2 is allocated, so chunk2 is inserted in the large bin overwriting the address `chunk1->bk_nextsize->fd_nextsize` with the address of chunk2
2024-06-12 15:23:07 +00:00
{% hint style="success" %}
There are other potential scenarios, the thing is to add to the large bin a chunk that is **smaller** than a current X chunk in the bin, so it need to be inserted just before it in the bin, and we need to be able to modify X's **`bk_nextsize`** as thats where the address of the smaller chunk will be written to.
{% endhint %}
2024-05-17 15:37:03 +00:00
2024-06-12 15:23:07 +00:00
This is the relevant code from malloc. Comments have been added to understand better how the address was overwritten:
{% code overflow="wrap" %}
2024-05-17 15:37:03 +00:00
```c
/* if smaller than smallest, bypass loop below */
assert (chunk_main_arena (bck->bk));
if ((unsigned long) (size) < (unsigned long) chunksize_nomask (bck->bk))
{
fwd = bck; // fwd = p1
bck = bck->bk; // bck = p1->bk
victim->fd_nextsize = fwd->fd; // p2->fd_nextsize = p1->fd (Note that p1->fd is p1 as it's the only chunk)
victim->bk_nextsize = fwd->fd->bk_nextsize; // p2->bk_nextsize = p1->fd->bk_nextsize
fwd->fd->bk_nextsize = victim->bk_nextsize->fd_nextsize = victim; // p1->fd->bk_nextsize->fd_nextsize = p2
}
```
2024-06-12 15:23:07 +00:00
{% endcode %}
2024-05-17 15:37:03 +00:00
This could be used to **overwrite the `global_max_fast` global variable** of libc to then exploit a fast bin attack with larger chunks.
2024-06-12 15:23:07 +00:00
You can find another great explanation of this attack in [**guyinatuxedo**](https://guyinatuxedo.github.io/32-largebin\_attack/largebin\_explanation0/index.html).
2024-07-11 13:10:36 +00:00
### Other examples
* [**La casa de papel. HackOn CTF 2024**](https://7rocky.github.io/en/ctf/other/hackon-ctf/la-casa-de-papel/)
* Large bin attack in the same situation as it appears in [**how2heap**](https://github.com/shellphish/how2heap/blob/master/glibc\_2.35/large\_bin\_attack.c).
* The write primitive is more complex, because `global_max_fast` is useless here.
* FSOP is needed to finish the exploit.
2024-07-18 16:04:36 +00:00
{% hint style="success" %}
2024-07-18 16:14:56 +00:00
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2024-05-17 15:37:03 +00:00
2024-07-18 16:04:36 +00:00
<details>
2024-05-17 15:37:03 +00:00
2024-07-18 16:04:36 +00:00
<summary>Support HackTricks</summary>
2024-05-17 15:37:03 +00:00
2024-07-18 16:04:36 +00:00
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
2024-05-17 15:37:03 +00:00
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
2024-07-18 16:04:36 +00:00
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2024-05-17 15:37:03 +00:00
</details>
2024-07-18 16:04:36 +00:00
{% endhint %}