Hi guys back again with the introduction of Heap Overflow, this time we move to the third challenge of the heap protostar challenge "Heap 2"
Static Analysis:
Before we move into the exploitation, let's take a look at the source code first and do a little bit of analysis- At the start of the main function, we immediately enter an infinite loop and then there is a fgets function that will be used to store your input into the "line" variable. Notice that fgets is a safe function since it has a size limitation that enables the program to allocate enough memory without getting the risk of buffer overflow
- After accepting our input the program will compare the value with 4 string which is "auth ", "reset", "service" and "login". If we choose "auth " options it will allocate memory in the heap for "auth" struct with malloc and then fill the memory with a bunch of zeroes using memset(). Finally, the program will take any data that comes after "auth " and stored it into the heap memory that was just reserved earlier.
- If we choose to input "reset" it will free the heap memory using free() and then back to the loop again
- If we choose to input "service" it will store the input after "service" to the heap memory using strdup().
- Last will be "login" that will check the "auth" value if it contains any data or not. Our goal is to fill the "auth" variable in order to pass heap2 challenge but how do we do that? since this variable is never touched by the program in the first place
Dynamic Analysis:
Let's try to run the program so we know exactly what we are dealing with
from the above figure, you can see that the program tell us where the program store our input (auth and service), for example, the first input will be stored at 0x804c008 and next input will be put to the next 10 bytes

strange things happen when we send "reset" to the program and enter another value, as you can see that both of the variables now store at the same place
what you just witness here is the "use-after-free" vulnerabilities in heap vulnerabilities. According to owasp community guideline(link: https://owasp.org/www-community/vulnerabilities/Using_freed_memory) it is a bad idea to reference memory after it has been freed this could lead to crash or undefined program behavior
"use-after-free" occur when we free() the auth variable by inputting "reset" but the program still use the memory to do some checking

let's load it into GDB to get a closer look
we will put a breakpoint into these two locations so we know how the program fills the heap using our input.



at the first breakpoint, our input is stored at 0x804c008

but do you notice something strange?
take a look at the size of the allocation at the left side (0x00000011) it should allocate memory more than that since struct consist of 32 bytes of char and integer. Why this is happening?
this is happening due to using the same name of a variable across the code that eventually confused the compiler on how it should correctly set the memory for each variable for the program

back again to the debugging let's try to free the memory and then input some value to it again

after we reset the heap memory of "auth" we can see that the program store our value to the previous location and if we do it again-again it will eventually replace the value of "auth-auth" and when enter "login" it will said that we already log in cool :)



but wait how do we know the exact location of "auth-auth", well it's pretty simple, take a look at the last section program.

this piece of assembly code is equivalent to if(auth->auth)
we can see that before comparing the value the program moves the content of the location "eax+0x20" to eax register. this eax register is actually pointing to our "auth" struct and by adding it with 0x20 it actually reference it to the integer "auth" variable inside the struct. We can take a look at the actual address in the following way.

Cool :) we just experience how one poor programming practice can screw up the entire program LOL
Hope you enjoy this
see you at the next blog post
Comments
Post a Comment