Search K
Appearance
Appearance
Other ways to support HackTricks:
malloc_printerr
functionThe attack starts by managing to get the top chunk inside the unsorted bin. This is achieved by calling malloc
with a size greater than the current top chunk size but smaller than mmp_.mmap_threshold
(default is 128K), which would otherwise trigger mmap
allocation. Whenever the top chunk size is modified, it's important to ensure that the top chunk + its size is page-aligned and that the prev_inuse bit of the top chunk is always set.
To get the top chunk inside the unsorted bin, allocate a chunk to create the top chunk, change the top chunk size so that top chunk + size is page-aligned with the prev_inuse bit set. Then allocate a chunk larger than the new top chunk size. Note that free
is never called to get the top chunk into the unsorted bin.
The old top chunk is now in the unsorted bin. Assuming we can read data inside it (possibly due to a vulnerability that also caused the overflow), itโs possible to leak libc addresses from it and get the address of _IO_list_all.
An unsorted bin attack is performed by abusing the overflow to write topChunk->bk->fwd = _IO_list_all - 0x10
. When a new chunk is allocated, the old top chunk will be split, and a pointer to the unsorted bin will be written into _IO_list_all.
The next step involves shrinking the size of the old top chunk to fit into a small bin, specifically setting its size to 0x61. This serves two purposes:
malloc
scans through the unsorted bin and sees this chunk, it will try to insert it into small bin 4 due to its small size. This makes the chunk end up at the head of the small bin 4 list, facilitating the manipulation of the forward and backward pointers.malloc
to perform internal checks. When it checks the size of the false forward chunk, which will be zero, it triggers an error and calls malloc_printerr
.The manipulation of the small bin will allow you to control the forward pointer of the chunk. The overlap with _IO_list_all is used to forge a fake _IO_FILE structure. The structure is carefully crafted to include key fields like _IO_write_base
and _IO_write_ptr
set to values that pass internal checks in libc. Additionally, a jump table is created within the fake structure, where an instruction pointer is set to the address where arbitrary code (e.g., the system
function) can be executed.
To summarize the remaining part of the technique:
The next step involves forging a fake _IO_FILE structure that overlaps with the old top chunk currently in the unsorted bin. The first bytes of this structure are crafted carefully to include a pointer to a command (e.g., "/bin/sh") that will be executed.
Key fields in the fake _IO_FILE structure, such as _IO_write_base
and _IO_write_ptr
, are set to values that pass internal checks in libc. Additionally, a jump table is created within the fake structure, where an instruction pointer is set to the address where arbitrary code can be executed. Typically, this would be the address of the system
function or another function that can execute shell commands.
The attack culminates when a call to malloc
triggers the execution of the code through the manipulated _IO_FILE structure. This effectively allows arbitrary code execution, typically resulting in a shell being spawned or another malicious payload being executed.
Summary of the Attack:
This approach exploits heap management mechanisms, libc information leaks, and heap overflows to achieve code execution without directly calling free
. By carefully crafting the fake _IO_FILE structure and placing it in the right location, the attack can hijack the control flow during standard memory allocation operations. This enables the execution of arbitrary code, potentially resulting in a shell or other malicious activities.
Other ways to support HackTricks: