all repos — site @ e41ce6841486e8f68f072544f4a65381fa55f67a

source for my site, found at icyphox.sh

pages/blog/rop-on-arm.md (view raw)

  1---
  2template: text.html
  3title: Return Oriented Programming on ARM (32-bit)
  4subtitle: Making stack-based exploitation great again!
  5date: 2019-06-06
  6---
  7
  8Before we start _anything_, you’re expected to know the basics of ARM
  9assembly to follow along. I highly recommend
 10[Azeria’s](https://twitter.com/fox0x01) series on [ARM Assembly
 11Basics](https://azeria-labs.com/writing-arm-assembly-part-1/). Once you’re
 12comfortable with it, proceed with the next bit — environment setup.
 13
 14### Setup
 15
 16Since we’re working with the ARM architecture, there are two options to go
 17forth with: 
 18
 191. Emulate — head over to [qemu.org/download](https://www.qemu.org/download/) and install QEMU. 
 20And then download and extract the ARMv6 Debian Stretch image from one of the links [here](https://blahcat.github.io/qemu/).
 21The scripts found inside should be self-explanatory.
 222. Use actual ARM hardware, like an RPi.
 23
 24For debugging and disassembling, we’ll be using plain old `gdb`, but you
 25may use `radare2`, IDA or anything else, really. All of which can be
 26trivially installed.
 27
 28And for the sake of simplicity, disable ASLR:
 29
 30```shell
 31$ echo 0 > /proc/sys/kernel/randomize_va_space
 32```
 33
 34Finally, the binary we’ll be using in this exercise is [Billy Ellis’](https://twitter.com/bellis1000)
 35[roplevel2](/static/files/roplevel2.c). 
 36
 37Compile it:
 38```sh
 39$ gcc roplevel2.c -o rop2
 40```
 41
 42With that out of the way, here’s a quick run down of what ROP actually is.
 43
 44### A primer on ROP
 45
 46ROP or Return Oriented Programming is a modern exploitation technique that’s
 47used to bypass protections like the **NX bit** (no-execute bit) and **code sigining**.
 48In essence, no code in the binary is actually modified and the entire exploit
 49is crafted out of pre-existing artifacts within the binary, known as **gadgets**.
 50
 51A gadget is essentially a small sequence of code (instructions), ending with
 52a `ret`, or a return instruction. In our case, since we’re dealing with ARM
 53code, there is no `ret` instruction but rather a `pop {pc}` or a `bx lr`.
 54These gadgets are _chained_ together by jumping (returning) from one onto the other
 55to form what’s called as a **ropchain**. At the end of a ropchain,
 56there’s generally a call to `system()`, to acheive code execution.
 57
 58In practice, the process of executing a ropchain is something like this:
 59
 60- confirm the existence of a stack-based buffer overflow
 61- identify the offset at which the instruction pointer gets overwritten
 62- locate the addresses of the gadgets you wish to use
 63- craft your input keeping in mind the stack’s layout, and chain the addresses
 64of your gadgets
 65
 66[LiveOverflow](https://twitter.com/LiveOverflow) has a [beautiful video](https://www.youtube.com/watch?v=zaQVNM3or7k&list=PLhixgUqwRTjxglIswKp9mpkfPNfHkzyeN&index=46&t=0s) where he explains ROP using “weird machines”. 
 67Check it out, it might be just what you needed for that “aha!” moment :)
 68
 69Still don’t get it? Don’t fret, we’ll look at _actual_ exploit code in a bit and hopefully
 70that should put things into perspective.
 71
 72### Exploring our binary
 73
 74Start by running it, and entering any arbitrary string. On entering a fairly
 75large string, say, “A” × 20, we
 76see a segmentation fault occur.
 77
 78![string and segfault](/static/img/string_segfault.png)
 79
 80Now, open it up in `gdb` and look at the functions inside it.
 81
 82![gdb functions](/static/img/gdb_functions.png)
 83
 84There are three functions that are of importance here, `main`, `winner` and 
 85`gadget`. Disassembling the `main` function:
 86
 87![gdb main disassembly](/static/img/gdb_main_disas.png)
 88
 89We see a buffer of 16 bytes being created (`sub	sp, sp, #16`), and some calls
 90to `puts()`/`printf()` and `scanf()`. Looks like `winner` and `gadget` are 
 91never actually called.
 92
 93Disassembling the `gadget` function:
 94
 95![gdb gadget disassembly](/static/img/gdb_gadget_disas.png)
 96
 97This is fairly simple, the stack is being initialized by `push`ing `{r11}`,
 98which is also the frame pointer (`fp`). What’s interesting is the `pop {r0, pc}`
 99instruction in the middle. This is a **gadget**.
100
101We can use this to control what goes into `r0` and `pc`. Unlike in x86 where
102arguments to functions are passed on the stack, in ARM the registers `r0` to `r3`
103are used for this. So this gadget effectively allows us to pass arguments to
104functions using `r0`, and subsequently jumping to them by passing its address
105in `pc`. Neat.
106
107Moving on to the disassembly of the `winner` function:
108
109![gdb winner disassembly](/static/img/gdb_disas_winner.png)
110
111Here, we see a calls to `puts()`, `system()` and finally, `exit()`.
112So our end goal here is to, quite obviously, execute code via the `system()`
113function.
114
115Now that we have an overview of what’s in the binary, let’s formulate a method
116of exploitation by messing around with inputs.
117
118### Messing around with inputs :^)
119
120Back to `gdb`, hit `r` to run and pass in a patterned input, like in the
121screenshot.
122
123![gdb info reg post segfault](/static/img/gdb_info_reg_segfault.png)
124
125We hit a segfault because of invalid memory at address `0x46464646`. Notice
126the `pc` has been overwritten with our input.
127So we smashed the stack alright, but more importantly, it’s at the letter ‘F’.
128
129Since we know the offset at which the `pc` gets overwritten, we can now
130control program execution flow. Let’s try jumping to the `winner` function.
131
132Disassemble `winner` again using `disas winner` and note down the offset
133of the second instruction — `add r11, sp, #4`. 
134For this, we’ll use Python to print our input string replacing `FFFF` with
135the address of `winner`. Note the endianness.
136
137```shell
138$ python -c 'print("AAAABBBBCCCCDDDDEEEE\x28\x05\x01\x00")' | ./rop2
139```
140
141![jump to winner](/static/img/python_winner_jump.png)
142
143The reason we don’t jump to the first instruction is because we want to control the stack
144ourselves. If we allow `push {rll, lr}` (first instruction) to occur, the program will `pop`
145those out after `winner` is done executing and we will no longer control 
146where it jumps to.
147
148So that didn’t do much, just prints out a string “Nothing much here...”. 
149But it _does_ however, contain `system()`. Which somehow needs to be populated with an argument
150to do what we want (run a command, execute a shell, etc.).
151
152To do that, we’ll follow a multi-step process: 
153
1541. Jump to the address of `gadget`, again the 2nd instruction. This will `pop` `r0` and `pc`.
1552. Push our command to be executed, say “`/bin/sh`” onto the stack. This will go into
156`r0`.
1573. Then, push the address of `system()`. And this will go into `pc`.
158
159The pseudo-code is something like this:
160```
161string = AAAABBBBCCCCDDDDEEEE
162gadget = # addr of gadget
163binsh  = # addr of /bin/sh
164system = # addr of system()
165
166print(string + gadget + binsh + system)
167```
168Clean and mean.
169
170
171### The exploit
172
173To write the exploit, we’ll use Python and the absolute godsend of a library — `struct`.
174It allows us to pack the bytes of addresses to the endianness of our choice.
175It probably does a lot more, but who cares.
176
177Let’s start by fetching the address of `/bin/sh`. In `gdb`, set a breakpoint
178at `main`, hit `r` to run, and search the entire address space for the string “`/bin/sh`”:
179
180
181```
182(gdb) find &system, +9999999, "/bin/sh"
183```
184![gdb finding /bin/sh](/static/img/gdb_find_binsh.png)
185
186One hit at `0xb6f85588`. The addresses of `gadget` and `system()` can be
187found from the disassmblies from earlier. Here’s the final exploit code:
188```python
189import struct
190
191binsh = struct.pack("I", 0xb6f85588)
192string = "AAAABBBBCCCCDDDDEEEE"
193gadget = struct.pack("I", 0x00010550)
194system = struct.pack("I", 0x00010538)
195
196print(string + gadget + binsh + system)
197
198```
199Honestly, not too far off from our pseudo-code :)
200
201Let’s see it in action:
202
203![the shell!](/static/img/the_shell.png)
204
205Notice that it doesn’t work the first time, and this is because `/bin/sh` terminates
206when the pipe closes, since there’s no input coming in from STDIN.
207To get around this, we use `cat(1)` which allows us to relay input through it
208to the shell. Nifty trick.
209
210### Conclusion
211
212This was a fairly basic challenge, with everything laid out conveniently. 
213Actual ropchaining is a little more involved, with a lot more gadgets to be chained
214to acheive code execution.
215
216Hopefully, I’ll get around to writing about heap exploitation on ARM too. That’s all for now.