Fast Bin Attack
Last updated
Last updated
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
For more information about what is a fast bin check this page:
Bins & Memory AllocationsBecause the fast bin is a singly linked list, there are much less protections than in other bins and just modifying an address in a freed fast bin chunk is enough to be able to allocate later a chunk in any memory address.
As summary:
You can find a full example in a very well explained code from https://guyinatuxedo.github.io/28-fastbin_attack/explanation_fastbinAttack/index.html:
If it's possible to overwrite the value of the global variable global_max_fast
with a big number, this allows to generate fast bin chunks of bigger sizes, potentially allowing to perform fast bin attacks in scenarios where it wasn't possible previously. This situation useful in the context of large bin attack and unsorted bin attack
CTF https://guyinatuxedo.github.io/28-fastbin_attack/0ctf_babyheap/index.html:
It's possible to allocate chunks, free them, read their contents and fill them (with an overflow vulnerability).
Consolidate chunk for infoleak: The technique is basically to abuse the overflow to create a fake prev_size
so one previous chunks is put inside a bigger one, so when allocating the bigger one containing another chunk, it's possible to print it's data an leak an address to libc (main_arena+88
).
Overwrite malloc hook: For this, and abusing the previous overlapping situation, it was possible to have 2 chunks that were pointing to the same memory. Therefore, freeing them both (freeing another chunk in between to avoid protections) it was possible to have the same chunk in the fast bin 2 times. Then, it was possible to allocate it again, overwrite the address to the next chunk to point a bit before __malloc_hook
(so it points to an integer that malloc thinks is a free size - another bypass), allocate it again and then allocate another chunk that will receive an address to malloc hooks.
Finally a one gadget was written in there.
CTF https://guyinatuxedo.github.io/28-fastbin_attack/csaw17_auir/index.html:
There is a heap overflow and use after free and double free because when a chunk is freed it's possible to reuse and re-free the pointers
Libc info leak: Just free some chunks and they will get a pointer to a part of the main arena location. As you can reuse freed pointers, just read this address.
Fast bin attack: All the pointers to the allocations are stored inside an array, so we can free a couple of fast bin chunks and in the last one overwrite the address to point a bit before this array of pointers. Then, allocate a couple of chunks with the same size and we will get first the legit one and then the fake one containing the array of pointers. We can now overwrite this allocation pointers to make the GOT address of free
point to system
and then write "/bin/sh"
in chunk 1 to then call free(chunk1)
which instead will execute system("/bin/sh")
.
CTF https://guyinatuxedo.github.io/33-custom_misc_heap/csaw19_traveller/index.html
Another example of abusing a one byte overflow to consolidate chunks in the unsorted bin and get a libc infoleak and then perform a fast bin attack to overwrite malloc hook with a one gadget address
CTF https://guyinatuxedo.github.io/33-custom_misc_heap/csaw18_alienVSsamurai/index.html
After an infoleak abusing the unsorted bin with a UAF to leak a libc address and a PIE address, the exploit of this CTF used a fast bin attack to allocate a chunk in a place where the pointers to controlled chunks were located so it was possible to overwrite certain pointers to write a one gadget in the GOT
You can find a Fast Bin attack abused through an unsorted bin attack:
Note that it's common before performing fast bin attacks to abuse the free-lists to leak libc/heap addresses (when needed).
Robot Factory. BlackHat MEA CTF 2022
We can only allocate chunks of size greater than 0x100
.
Overwrite global_max_fast
using an Unsorted Bin attack (works 1/16 times due to ASLR, because we need to modify 12 bits, but we must modify 16 bits).
Fast Bin attack to modify the a global array of chunks. This gives an arbitrary read/write primitive, which allows to modify the GOT and set some function to point to system
.
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)