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.
The art of exploitation (nice book by the way) depends on the given security mechanism that has been implemented. The binary or the memory can be protected in certain ways. We need to identify and if possible circumvent these mechanism. We use the checksec extension in gef after attaching to the miniweb process to check them.
gef➤ checksec
[+] checksec for '/tmp/gef/410//usr/bin/miniweb'
Canary : No
NX : No
PIE : No
Fortify : No
RelRO : No
So we will not go through all of them, this task ist left for you. It is a beginner challenge and therefore it makes sense that everything is turned off. But we will enable some of them, or let's say pretend that they are enabled to make it a bit more challenging.
2.1 Security Flags / Mechanism
We will discuss some common security mechanism, how they behave and if we cirumvent them.
2.1.1 No Execute (NX) Bit
The Internet of Things (IoT) devices I have analyzed so far mostly have the NX bit set. This will make the stack non-executable. Placing the shellcode onto the stack and jumping to it will end up in a crash. The shellocde consists of assembly instructions that try to achieve a special task like opening a reverse shell or executing a system command (execv).
It can be circumvented calling mprotect to make the stack executable again. But this will be handled in another blog post. The idea behind ROPchains is using existing code in the binary and libraries. Therefore we do not need to place the shellcode onto the stack and they are also executable because it is code that gets used by the program. With ROPchains you can do a lot of stuff. We can overwrite variables, executing arbitrary functions or executing a system command. We will use it to build our final exploit in Part 3.
2.1.2 Position Independent Code (PIE)
The Position Independent Code (PIE) is also not set. You can find more about this feature here. Many devices have ASLR still completely turned off which makes it very easy for an attacker to exploit this target. However even if ASLR has been turned on, the effectiveness is limited/gone if is missing the PIE feature. This is also a common case.
But there is also a problem. This feature often just affects the memory layout of the binary itself. Shared libraries will (normally) have this flag set and get randomized. We have to build our ROPchain from gadgets out of the main binary. Based on the memory layout we will often/always(?) encounter that the addresses in this binary contain a Null Byte character in the highes byte. We solve one issue but created another one :)
However if the vulnerability exists within a functionality that stop at the Null Byte we might be able to call at leasat one gadget. This can be enough to bypass an authorization check or maybe execute a system command (magic gadget) but we have to dig deeper.
2.1.3 Adress Space Layout Randomization (ASLR)
The Adress Space Layout Randomization is a feature handled by the linux kernel. You can find the config for you system here: .
/proc/sys/kernel/randomize_va_space
$ cat /proc/sys/kernel/randomize_va_space
0
------------------------------------------
Values:
0 means this feature is disabled
1 randomizes the stack and libraries
2 also randomizes the data segments
This is one of the hardest security feature to bypass - if PIE is also active. Everytime the application is launched, the addresses will be randomly aligned. Even in ROPchains where we use existing code we have to hardcode the values into our exploit. If the base address now changes, we jump to a wrong gadget and the exploit fails.
I just know a few possibilties to deal with this security feature and one of them is leaking a valid address from the stack to recalculate the base address. This could be done with a format string vulnerability. It will be discussed in a separate blog post because we this is not the case here.
The other one is trying to brute force the address but this got some dependencies. If the program crashes after every attempt, it will reduces the amount of attempts that can be performed significantly. If the process is forking another process and this contains the vulnerability we get lucky and can try to brute force the base address because the offsets will stay the same. We stick to the same address and wait until the program "randomly" aligned it on the same address. Randomness on IoT devices is often not a really big deal. On most devices I have encountered the ASLR feature, the randomness has been within three bytes. We pick one address we have identifed before and stick to it as shown below. With this approach we might be able to compromise other devices too.
+-------------+
|parent libc: |
|0x40de1000 |
+-------------+
| +-------------+
|_____________| child libc: |
| | 0x40280000 |
| +-------------+
|
| +-------------+
|_____________| child libc: |
| | 0x401b8000 | <-- Got it. Exploit will now successfully run.
| +-------------+ We can establish some sort of persistence.
| The exploit could enable telnet.
| +-------------+
|_____________| child libc: |
| | 0x40e24000 |
| +-------------+
[...]
We will enable ASLR in some kind of "bonus" special where I explain this approach in a bit more detail. We do not want to make it too complexe for now and have this featured turned off.
2.1.4 Bad Characters
Beside the security mechanism of the binary and memory we have to deal with a common behavior named bad characters. Our exploit will be delivered through an vsprintf function. It is reading bytes until a Null Byte character (0x00) appears. By having such a special character in our exploit chain, the reading stops at this position and the rest of it would be cut off. The exploit will not be complete and fails. If the Null Byte character is as the end of our exploit we are good to go.
Beside having the vsprintf function we should also think about other processes that are involved during the exploitation. In this case we are transferring the exploit over an HTTP request. The RFC gives us a guideline on which characters will be allowed and which one will break our request. Luckily we can sometimes circumvent this behavior with a simple URL encoding of our exploit. It depends on when the server is decoding the request.
2.2 Possible Real World Device - Settings
To make this a bit more challenging we assume that the NX bit is enabled. This denies the execution of the shellcode directly on the stack. We will have ASLR turned off for now and enable it within another blog post. Many devices will have set ASLR at least to 1. The other security features are IMHO not very common for Internet of Things (IoT) devices.
2.2.1 What are we trying to achieve?
Our goal is to perform a remote code execution. We want to exploit the stack based buffer overflow in a certain way to execute arbitrary commands on the system. We can achieve this task by jumping to system() within libc. We need to provide our command as an argument in the register r0. The calling convention on ARM has been already discussed in Part 1.
2.2.2 Finding the address of System()
The xinfo command displays all the information known togef about the specific address. We use it to search for the system() address with the following command.
gef➤ xinfo system
──────────────────────────────── xinfo: 0x10d88 ────────────────────────────────
Page: 0x00010000 → 0x00014000 (size=0x4000)
Permissions: r-x
Pathname: /usr/bin/miniweb
Offset (from page): 0xd88
Inode: 187
Segment: .plt (0x00010ce4-0x00011004)
Symbol: system@plt
Here I made mistake which took me quite a while. The address of system is located at 0x10d88. I first assumed that this is the offset within the libc library because the base address of libc is at 0x40000000. But the xinfo points it out pretty clearly, that the page where it has found this address is within the page of 0x0001000. If we verify this with vmmap command, we can see that this is within the miniweb binary. The command vmmap displays the entire memory space mapping.
Start End Offset Perm Path
0x00010000 0x00014000 0x00000000 r-x /usr/bin/miniweb
0x00023000 0x00026000 0x00003000 rwx /usr/bin/miniweb
0x40000000 0x40064000 0x00000000 r-x /lib/libc.so
0x40064000 0x40065000 0x00000000 r-x [sigpage]
0x40073000 0x40074000 0x00063000 r-x /lib/libc.so
0x40074000 0x40075000 0x00064000 rwx /lib/libc.so
0x40075000 0x40077000 0x00000000 rwx
0x40078000 0x40089000 0x00000000 r-x /lib/libgcc_s.so.1
0x40089000 0x4008a000 0x00009000 rwx /lib/libgcc_s.so.1
0xbefdf000 0xbf000000 0x00000000 rwx [stack]
0xffff0000 0xffff1000 0x00000000 r-x [vectors]
Using a system reference in the miniweb binary is causing at least two problems for us right now. The first one is, that it contains a Null Byte at the beginning of this post. This would not be the case if we are using the reference in libc 0x40000000. The second problem is when we enable ASLR and PIE later on. It would be much easier to build a ROPchain within one library and the respective offsets. So why do we get the system reference in the miniweb binary and not the reference in libc?
2.2.2.1 Procedure Linkage Table (PLT)
The Procedure Linkage Table looks up the addresses in the .got.plt section. If the address has not been filled into it, the linker is triggered to look it up. This is the case when the function (in this case system) has not been executed yet. If it has been executed before the address will be directly available. If I am wrong on that than please correct me! :)
We can check this with gdb and the following command.
gef➤ x/4i 0x10d88
0x10d88 <system@plt>: add r12, pc, #0, 12
0x10d8c <system@plt+4>: add r12, r12, #73728 ; 0x12000
0x10d90 <system@plt+8>: ldr pc, [r12, #3704]! ; 0xe78
0x10d94 <feof@plt>: add r12, pc, #0, 12
We examine the next four instructions (x/4i) at the address 0x10d88. Here we can see it is referencing the address to the .plt section. A quick check to get the one within libc is executing the system function manually. We provide values to the register r0 and pc as shown below.
gef➤ set $r0="/sbin/reboot"
gef➤ set $pc=0x10d88
gef➤ n
[ Legend: Modified register | Code | Heap | Stack | String ]
───────────────────────────────────────────────────────────────────────────── registers ────
$r0 : 0x00025df0 → "/sbin/reboot"
$r1 : 0xbefffe4c → 0x400755b4 → 0x00000001
$r2 : 0xbefffe48 → 0x00000010
[...]
$r10 : 0xbefffec4 → 0xbeffff97 → "/usr/bin/miniweb"
$r11 : 0xbefffe94 → 0x400241f0 → 0xebffb862
$r12 : 0x00010d90 → 0xe5bcfe78
$sp : 0xbefffcdc → 0x00000000
$lr : 0x4004e470 → 0xe2577006
$pc : 0x00010d8c → 0xe28cca12
$cpsr: [negative ZERO CARRY overflow interrupt fast thumb]
───────────────────────────────────────────────────────────────────────────────── stack ────
0xbefffcdc│+0x0000: 0x00000000 ← $sp
0xbefffce0│+0x0004: 0x00000000
0xbefffce4│+0x0008: 0x40075564 → 0x00000000
0xbefffce8│+0x000c: 0x0000011d
0xbefffcec│+0x0010: 0x4004e470 → 0xe2577006
0xbefffcf0│+0x0014: 0xbefffe48 → 0x00000010
0xbefffcf4│+0x0018: 0x00000000
0xbefffcf8│+0x001c: 0x00000000
──────────────────────────────────────────────────────────────────────────── code:arm:ARM ────
0x10d80 <tolower@plt+4> add r12, r12, #73728 ; 0x12000
0x10d84 <tolower@plt+8> ldr pc, [r12, #3712]! ; 0xe80
0x10d88 <system@plt+0> add r12, pc, #0, 12
→ 0x10d8c <system@plt+4> add r12, r12, #73728 ; 0x12000
0x10d90 <system@plt+8> ldr pc, [r12, #3704]! ; 0xe78
0x10d94 <feof@plt+0> add r12, pc, #0, 12
0x10d98 <feof@plt+4> add r12, r12, #73728 ; 0x12000
0x10d9c <feof@plt+8> ldr pc, [r12, #3696]! ; 0xe70
0x10da0 <malloc@plt+0> add r12, pc, #0, 12
───────────────────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "miniweb", stopped, reason: SINGLE STEP
───────────────────────────────────────────────────────────────────────────────── trace ────
[#0] 0x10d8c → system@plt()
[#1] 0x4004e470 → subs r7, r7, #6
─────────────────────────────────────────────────────────────────────────────────
0x00010d8c in system@plt ()
By stepping into we reach the libc reference which can be verified by checking the address space (0x40000000):
0x4003aeac add sp, sp, #20
0x4003aeb0 pop {pc} ; (ldr pc, [sp], #4)
0x4003aeb4 ; <UNDEFINED> instruction: 0xfffff5ac
→ 0x4003aeb8 push {r4, r5, r6, r7, r8, r9, r10, lr}
0x4003aebc sub sp, sp, #1040 ; 0x410
0x4003aec0 sub sp, sp, #8
0x4003aec4 add r5, sp, #292 ; 0x124
0x4003aec8 mov r10, r0
0x4003aecc mov r2, #140 ; 0x8c
─────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "miniweb", stopped, reason: SINGLE STEP
───────────────────────────────────────────────────────────────────── trace ────
[#0] 0x4003aeb8 → push {r4, r5, r6, r7, r8, r9, r10, lr}
[#1] 0x4004e470 → subs r7, r7, #6
────────────────────────────────────────────────────────────────────────────────
0x4003aeb8 in ?? ()
We will take this address because we are referencing all upcoming gadgets to the base address of the libc library and it also does not contain Null Byte characters.
In my first solution on this challenge I took the address containing the Null Byte character which made it unintentional harder but I learned a lot of new stuff and managed to circumvent this Null Byte character. I will describe the whole approach in Part 4 of this series but for now we keep doing it the simple and "right" way.
2.3 ROP Chain on ARM
Returned Oriented Programming (ROP) means that we are using existing code, chained together with jump (return) instruction to compose our variables and functions in a certain way. Ok this might be my own interpretation and is definitely not the original definition about ROP but it describes what ROP is about. We are looking for certain instructions like mov r0, sp that contains a jump instruction afterwards. Therefore we can use another gadget and perform an additional task like jumping to system().
We may be not able to execute our shellcode directly on the stack based on the NX bit but we can use the existing code to achieve the same. We need to setup the r0 register, that it points to our string. If we have done this, we need to jump to the system() function.
2.3.1 A Fictional ROP Example
Let's do some examples which explain the whole process a bit better. We would like to create an exploit that executes a reboot command. We could place the command onto the stack, move it into r0 with a mov r0, sp instruction and jump to the address of system which has been also pushed onto the stack.
0x40014104 mov r0, sp; pop {pc}
This single code snipped is doing all of this in one gadget. We are moving the command which gets stored on the stack into r0 and fetch the next four bytes (address of system to place them into pc. The program is now resolving the address and jumping to it which gives us system("/sbin/reboot"). But how do these values get onto the stack?
2.3.2 Stack Alignment
To align the stack in this way we place the address of system() (e.g. 0x40080104) and our arbitrary command /sbin/reboot after our buffer of As and overwrite the actual returning address with the address of this gadget (0x40014104).
http://192.168.../AAAAAAAAAAAA | 0x40014104 | 0x10080104 | /sbin/reboot
2.3.3 How it works
The buffer is filled with As until we reach our offset and overwrite the "return" address. We jump to our gadget and get the stack layout as follow:
Registers:
r0 = 0x0
pc = 0x40014108 (next instruction)
sp = beffxxxxxx --> "0x10080104"
Gadget: 0x40014104 mov r0, sp; pop {pc}
Stack:
+---------------------+ High beffxxxxxx
sp >| 0x10080104 |
|_____________________|
| /sbin/reboot |
|_____________________|
| ????????? |
|_____________________|
| |
| |
+---------------------+ Low
So the next instruction mov r0, sp will move the sp into r0. For now, r0 points to the address 0x10080104. Now we execute pop {pc}. This will take the next four bytes from the stack and moves them into pc. In ARM mode every instruction is four bytes long (two bytes in THUMB).
Registers:
r0 = "/sbin/reboot"
pc = 0x10080104 (fictional address of system)
sp = beffxxxxxx --> "/sbin/reboot"
Gadget: 0x40014104 mov r0, sp; pop {pc}
Stack:
+---------------------+ High beffxxxxxx
| --- popped --- |
|_____________________|
sp >| /sbin/reboot |
|_____________________|
| ????????? |
|_____________________|
| |
| |
+---------------------+ Low
The sp is now pointing to our command and r0 is holding a reference to it and therefore also pointing to the command. The pop {pc} instruction is now executing the next instruction at address 0x10080104 which did get popped from the stack before. We jump to this address and execute the command. Done! :)
The only problem here is, we will not find such a code snippet (gadget) which does all of this at the same time. But we will find many other gadgets, that do parts of this job. What we have to do is like solving a puzzle. We simply take several snippets and combine them until we achieve the same behavior.
2.3.4 A real ROP Example
Another one. The following gadget is pretty similar to the one we had before but there is of course a major difference.
0x40014110 mov r0, sp; blx r3;
After setting up the r0 register, the program is jumping to the address stored in r3, but which address is stored in r3? Well we would have to debug and check it but even than it is most likely not the address of system() :)
Therefore we cannot just simple use this gadget alone. We have to chain it together with another one that gives us control over r3 like this one here.
0x40010120 pop {r0, r1, r2, r3, pc}
This instruction takes the first four bytes from the stack and moves them into r0. The value will be popped from the stack and the next four bytes gets loaded into r1. This will be repeated for r2, r3 and the pc. So if we add 20 bytes (each register takes four) after our offset, we have control over the values in all these registers. We can write whatever we want to them (ok it is limited to 4 bytes and exclude bad characters).
But we just need to write certain values into the r3 and pc register? What do we write into r0, r1 and r2? If we care about process continuation we have to restore the original values later on. But we do not care for now. The only thing beside bad characters is that we do not overwrite other values which have been setup by us before. We can fill them with junk values like 0x41414141. It is just important that these values are four bytes long. So putting it all together we get the following:
GADGET1: 0x40010120 pop {r0, r1, r2, r3, pc}
GADGET2: 0x40014110 mov r0, sp; blx r3;
We write the address of the system() into r3 and the address of GADGET2 into pc. The register r0, r1 and r2 will be filled with junk. We cannot leave them just empty because the stack is removing always four bytes from it with a pop instruction. We have the same approach as in the other example but here we had to setup r3 before because within GADGET2 we jump to it (blx r3).
http://192../AAA | 0x40010120 | 0x41414141 0x42424242 0x43434343 0x40080104 0x40014110 | /sbin/reboot
The return address is now 0x40010120 which takes the next 20 bytes into the registers r0,r1,r2,r3,pc and popping these values from the stack.
2.3.4.1 Before the execution of: 0x40010120 pop {r0, r1, r2, r3, pc}
Registers:
r0 = 0x0
r1 = 0x0
r2 = 0x0
r3 = 0x0
pc = 0x40010124 (next instruction)
sp = beffxxxxxx --> "0x80010d88" (random value)
GADGET1: 0x40010120 pop {r0, r1, r2, r3, pc}
Stack:
+---------------------+ High beffxxxxxx
sp > | 0x41414141 |
|_____________________|
| 0x42424242 |
|_____________________|
| 0x43434343 |
|_____________________|
| 0x40080104 |
|_____________________|
| 0x40014110 |
|_____________________|
| /sbin/reboot |
|_____________________|
| ????????? |
|_____________________|
| ????????? |
|_____________________|
| |
| |
+---------------------+ Low
2.3.4.2 After the execution of: 0x40010120 pop {r0, r1, r2, r3, pc}
Registers:
r0 = 0x41414141 (does not matter --> will be later overwritten mov r0, sp)
r1 = 0x42424242 (does not matter)
r2 = 0x43434343 (does not matter)
r3 = 0x40080104 (does not matter anymore)
pc = 0x40080104 (Address of System - fictional)
sp = beffxxxxxx --> "/sbin/reboot"
GADGET2: 0x40014110 mov r0, sp; blx r3;
Stack:
+---------------------+ High beffxxxxxx
| --- popped --- |
|_____________________|
| --- popped --- |
|_____________________|
| --- popped --- |
|_____________________|
| --- popped --- |
|_____________________|
| --- popped --- |
|_____________________|
sp >| /sbin/reboot |
|_____________________|
| ????????? |
|_____________________|
| ????????? |
|_____________________|
| |
| |
+---------------------+ Low
Now in GADGET2 we move the sp into r0 and jump to the value stored in r3 which is the fictional address of system 0x4008010.
I hope this quick introduction gives you a better understanding on how ROP chains work and how we can set them up. Part3 is finally the exploitation part. We will create our ROP chain step by step and develop a python script which performs the remote code execution in an easy way.