Use After Free Vulnerability, Attack and Fix
$ git clone https://github.com/NVombat/<insert_repo_name>.git
- use_after_free.c -> Contains vulnerable code with the use-after-free vulnerability that can be exploited
- use_after_free_patched.c -> Contains the patched code where the use-after-free vulnerablity has been patched
- exploit1.sh -> Attack script 1
- exploit2.sh -> Attack script 2
- Makefile -> File for compiling the c files into their respective binaries
- heap_overflow.c -> An example of a heap overflow attack
- Run the Makefile:
$ make
- Try to exploit the program yourself:
$ ./use_after_free
- Run attack scripts to exploit the program:
$ ./exploit1.sh [OR] ./exploit2.sh
- Try to exploit the patched version yourself:
$ ./use_after_free_patched
- Clean up:
$ make clean
When memory is freed, the heap manager puts this memory into bins (Fast Bins and Regular Bins). Our code is concerned only with the Fast Bin which is used for smaller memory chunks. When memory is freed, it is put into the Fast Bin and is readily available for use by any other program or process however, if a memory chunk is freed and a request to allocate the same amount of memory is made after the freeing of the chunk, the freed memory is given to the program. The problem arises because the pointer to that freed memory chunk was never made NULL thus it still points to that memory chunk which has now been assigned to some other variable, program or process. The "dangling" pointer will take the value of whatever is stored in the newly allocated memory chunk.
The easiest way to fix this vulnerability is to ensure that once memory is freed, the pointer to that memeory is made to be NULL. This does not allow for dangling pointers.
- Select option 1 to allocate a chunk of memory to store the username
- Select option 2 to allocate a chunk of memory to store the password
- Free the chunks of memory using option 3.
- Select option 2 to allocate a chunk of memory for the password so that it can rewrite the contents of the previously allocated username chunk too.
- Use option 4 to log in and successfully exploit the vulnerability
OR
- Select option 1 to allocate a chunk of memory to store the username
- Select option 2 to allocate a chunk of memory to store the password
- Free the chunks of memory using option 3.
- Select option 4 to allocate the chunks of memory to temp_uname and temp_pwd and then enter "root" as the username
- A successful heap overflow can be performed using these commands:
$ make heap_overflow
$ ./heap_overflow AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAls
$ make clean
- This will execute the
ls
command however that can be replaced with any command of the attackers choice - The
uname
andcmd
variables are allocated 64 byte chunks next to each other in the heap. - When executing the above mentioned command, the
uname
variable is filled withA's
and theseA's
overflow to the next memory chunk which iscmd
thus overwriting the hard-coded and intended command. - This causes the program to execute the command injected by the attacker