Unlink 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)
When this attack was discovered it mostly allowed a WWW (Write What Where), however, some checks were added making the new version of the attack more interesting more more complex and useless.
Attack doesn't work if tcaches are used (after 2.26)
This attack allows to change a pointer to a chunk to point 3 addresses before of itself. If this new location (surroundings of where the pointer was located) has interesting stuff, like other controllable allocations / stack..., it's possible to read/overwrite them to cause a bigger harm.
If this pointer was located in the stack, because it's now pointing 3 address before itself and the user potentially can read it and modify it, it will be possible to leak sensitive info from the stack or even modify the return address (maybe) without touching the canary
In order CTF examples, this pointer is located in an array of pointers to other allocations, therefore, making it point 3 address before and being able to read and write it, it's possible to make the other pointers point to other addresses. As potentially the user can read/write also the other allocations, he can leak information or overwrite new address in arbitrary locations (like in the GOT).
Some control in a memory (e.g. stack) to create a couple of chunks giving values to some of the attributes.
Stack leak in order to set the pointers of the fake chunk.
There are a couple of chunks (chunk1 and chunk2)
The attacker controls the content of chunk1 and the headers of chunk2.
In chunk1 the attacker creates the structure of a fake chunk:
To bypass protections he makes sure that the field size
is correct to avoid the error: corrupted size vs. prev_size while consolidating
and fields fd
and bk
of the fake chunk are pointing to where chunk1 pointer is stored in the with offsets of -3 and -2 respectively so fake_chunk->fd->bk
and fake_chunk->bk->fd
points to position in memory (stack) where the real chunk1 address is located:
The headers of the chunk2 are modified to indicate that the previous chunk is not used and that the size is the size of the fake chunk contained.
When the second chunk is freed then this fake chunk is unlinked happening:
fake_chunk->fd->bk
= fake_chunk->bk
fake_chunk->bk->fd
= fake_chunk->fd
Previously it was made that fake_chunk->fd->bk
and fake_chunk->bk->fd
point to the same place (the location in the stack where chunk1
was stored, so it was a valid linked list). As both are pointing to the same location only the last one (fake_chunk->bk->fd = fake_chunk->fd
) will take effect.
This will overwrite the pointer to chunk1 in the stack to the address (or bytes) stored 3 addresses before in the stack.
Therefore, if an attacker could control the content of the chunk1 again, he will be able to write inside the stack being able to potentially overwrite the return address skipping the canary and modify the values and points of local variables. Even modifying again the address of chunk1 stored in the stack to a different location where if the attacker could control again the content of chunk1 he will be able to write anywhere.
Note that this was possible because the addresses are stored in the stack. The risk and exploitation might depend on where are the addresses to the fake chunk being stored.
Although it would be weird to find an unlink attack even in a CTF here you have some writeups where this attack was used:
CTF example: https://guyinatuxedo.github.io/30-unlink/hitcon14_stkof/index.html
In this example, instead of the stack there is an array of malloc'ed addresses. The unlink attack is performed to be able to allocate a chunk here, therefore being able to control the pointers of the array of malloc'ed addresses. Then, there is another functionality that allows to modify the content of chunks in these addresses, which allows to point addresses to the GOT, modify function addresses to egt leaks and RCE.
Another CTF example: https://guyinatuxedo.github.io/30-unlink/zctf16_note2/index.html
Just like in the previous example, there is an array of addresses of allocations. It's possible to perform an unlink attack to make the address to the first allocation point a few possitions before starting the array and the overwrite this allocation in the new position. Therefore, it's possible to overwrite pointers of other allocations to point to GOT of atoi, print it to get a libc leak, and then overwrite atoi GOT with the address to a one gadget.
CTF example with custom malloc and free functions that abuse a vuln very similar to the unlink attack: https://guyinatuxedo.github.io/33-custom_misc_heap/csaw17_minesweeper/index.html
There is an overflow that allows to control the FD and BK pointers of custom malloc that will be (custom) freed. Moreover, the heap has the exec bit, so it's possible to leak a heap address and point a function from the GOT to a heap chunk with a shellcode to execute.
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)