In this first post about buffer overflow in ARM, we are going to take a look at how to cause an overflow in the ARM binary and build an exploit in this specific architecture. To demonstrate the POC itself we are going to used source code from protostar and modified it a little bit to make it more interesting.
But before we are going to the juicy stuff let me tell you some little bit of background of the ARM architecture.
Background:
ARM is basically just another processor architecture like intel x86 and MIPS but unlike these two architectures, ARM machine becoming more and more common this day if you notice your phone is actually is using ARM architecture including router and not to mention IoT.why ARM is chosen to be used in IoT (embedded technology) and mobile phone?
simple! ARM is a resource constraint and doesn't require a lot of CPU power to execute a task. ARM is a RISC (Reduced instruction set Computing) processor this means it has fewer instructions and more general-purpose register this is actually equivalent to More power but less resource to compute
Before you start :
I suggest you take a look at azeria-labs website to get the concept of the ARM assembly to follow this tutorial and if you are already familiar with memory corruption, you only need a couple of days to finish all of the materials.Setting up the environment:
To do exploitation in ARM assembly of course we need to have an ARM machine. There are two ways you can accomplish this: first, you can buy a raspberry pi and install raspbian inside or the second approach is to use qemu emulator, follow this tutorial to set up your machine. If you successfully set up the qemu it will show like this.Let's begin:
The vulnerable code will be like this. Basically is similar to the protostar challenge, you need to change the variable value to 1337
Compile the following source code with the following specification:
Once it is done, let's loaded into our gdb:
~# gdb stack0
~# disassemble main
You don't need to care about the first three instructions since that is just a function prologue. This is just telling how the compiler set up the stack for the program to run so nothing interesting will come from there
in the fourth instruction you can see that:
mov r3, #99
(put a breakpoint and examine the register before and after the assignment)
this is equivalent to the variable assignment in the modified variable so the value of 99 is going to be the first stored in r3 and then after it stored in r3 it will move again to the location of r11-8. remember str instruction is moved from left to right
(proceed to the next instruction and we can see that the r3 value is stored in the r11-8)
after the assignment of the modified variable, the next three instruction will used to set up the stack for the gets function
Don't worry this is just basically just to set how long is the buffer that needs to take for the function input and then store the location of the size to the r0 register, in ARM storing value to r0 consider as the first parameter of the function.
Let's say we put some A char so we can easily spot the char input in the stack region.
Now, this is where the interesting part is coming together, remember that our value of the assigned "modified" variable is stored in the r11-8 and now it is loaded again to r3 and surprise surprise the value of "1337" that will be compared will be put to the r2 register.
Let's take a look at how the stack is arranged
Cool so our input is stored at 0xbeffef74 and the "modified" variable is stored at the 0xbeffefb4, since the program get the input using gets function that is commonly known as unsafe method we can just overflow the stack of the who holde the modified variable to become "1337".
simple math: 0xbeffefb4 - 0xbeffef74 => 64 offset for use to overwrite the variable
so create a python script like this:
(run it again in gdb as the input)
examine the stack region and wallaaaa we are able to replace the value of the variable.
(continue the execution again)
We also can use this payload outside the gdb :)
Cool :) we just get our first buffer overflow in ARM.
That's all folks! hope you enjoy this post see you in the next series
Comments
Post a Comment