ARM64 Reversing and Exploitation Part 2 - Use After Free
In this blog post, we will be exploiting a Use-after-free vulnerability in the vuln binary. The binaries for this and the next article can be found here. This UaF challenge is based on the one used by Protostar
Use-after-free vulnerabilities occurs on the use of heap allocated memory after it has been freed. This can lead to several unexpected behaviours, from a crash to code execution.
Anyways, let’s get started. Copy the vuln binary to your iOS or Corellium device.
Run the binary vuln. You get a message that says “Better luck next time”
Let’s open the binary in Hopper to see what’s going on. Let’s have a look at the main function.
Just like the previous example on Heap Overflow, our objective here is to jump the useafterfree function. For that, we need to pass in the argument uaf
The function then jumps execution to the function useafterfree
The output shows the address of the user and the customerChat object. We see several commands here, however on reversing the function, we find there is another hidden command reset that basically frees the user object.
This can be confirmed by looking at the code itself
We see that the user struct object has an attribute password . This is being checked later on. If the password has three B’s, the user gets logged in.
This is an example of a UaF since the user object can be freed by using the reset command and then calling if(user->password) will basically trigger the UaF.
We can also calculate the size of the user object. The user object is a object of struct currentUser as can be seen in the following line
The size of the user object is 256 + 4 = 260 bytes.
If we can free the user object using reset and then overwrite it with the value BBBB such that we are able to overwrite the password property, we might be able to execute a Use-after-free condition and successfully log in.
Since our objective is to login, so let’s try that by first entering the username command,
Now let’s enter the reset command, this will free the buffer. Now let’s enter the customerChat command followed by the chat and send 260 B’s (so the size of cutomerChat object is the same as than of user object), we keep entering the size of the chat around the same size of the user so that it can take over the memory address of the freed user object.
After some tries, we see that the customerChat address is overlapping the user address, in this case we were able to overwrite the password property of the freed user object with all B’s. And hence entering the login command again gives us a success.
Command in order
- username admin
- customerChat BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB