Heap Exploitation
“These courses expects a high standard of professionalism from its students with regard to how security testing is conducted. We expect all students to act in good faith at all times […]”
TL;DR Don’t be mean
gets
/ fgets
strcpy
[..]printf
execution on user-controlled inputtext
region (executable)Leak!
A chunk can be in two states free, or in-use
Note: malloc
returns the address of the payload
*AMP
4 * sizeof(void*)
Freeing chunks need to be fast!
Programs use several different types of ‘bins’ to efficiently store information about free’d memory.
free
'd, chunks are stored in a single bin of varying chunk sizes
The normal bins are divided into 62 small bins (each bin has chunks of the same size), and two large bins (where each large bin has chunks of similar size)
Thread-local Cache
Faster than a global cache!
Arbitrarily sized bins that have a limit of 7 chunks (by default).
If 7 chunks have been free’d, tcache won’t be used
Note: calloc
doesn’t use the tcache
When a chunk is freed, part of its contents is used as metadata… If we tamper with its contents, we can corrupt the linked list! This allows us to control the addresses of a future malloc chunk!
e.g. Modify the forward pointer…
malloc returns our own address!
// Bin: NULL
free(chunk) // Bin: chunk -> (HEAD = NULL)
chunk = 0x41414141 // Bin: chunk -> 0x41414141 -> ???
dummy = malloc(...) // Bin: 0x41414141 -> ???
pwn = malloc(...) ///////// pwn = 0x41414141
Systems check that you haven’t free
'd a memory address twice in a row.
free(a);
free(a);
// free(): double free detected
// Aborted (core dumped)
But… it doesn’t prevent this
free(a);
free(dummy);
free(a);
// ???
When a chunk is freed the second time…
// Bin: NULL
free(chunk); // Bin: chunk -> (HEAD = NULL)
free(chunk); // Bin: chunk -> (HEAD = chunk) -> NULL
puts(chunk.next) /////// chunk
// $$$
or maybe
// Bin: NULL
free(chunk); // Bin: chunk -> (HEAD = NULL)
free(chunk); // Bin: chunk -> (HEAD = chunk) -> NULL
malloc(...) // some val
malloc(...)
malloc(...) // the same val!???
// $$$
vis_heap_chunks
heap
arena
bins
___bins
Double Free
Use After Free
🐏 (s)heap 🐑