All right. Next speaker is Zoukirin. He's going to be talking. So let's please give him a big DEF CON welcome. Come on. ZOUKIRIN ZOUKIRIN Thank you. Hello and welcome to my talk on ARM Exploitation Techniques. Thank you, crowd. I love you. Hello and welcome to my talk on ARM Exploitation Techniques. Thank you again. Can you hear me? You in the back? All right. We got it. Hello and welcome to my talk on ARM Exploitation Techniques. I actually said it three times now. In this talk, I will cover exploitation technique on both ARM and x86 architecture from scratch, actually. So if you've never done it before, it's the time to start, actually. This is my first DEF CON ever. So I'm really excited to be here with you. I'm really happy to attend this great conversation, this great event. Well, I had a buddy who had to go yesterday. He couldn't stay today. So I promised him I'll take a photo of you while you scream DEF CON. So if you can do that for me, that would be awesome. Like five, four, three, two, one. Yeah. I'm going to post it online. I'm on that blog right there. So you can see yourself. I'm Isaac Avram. My nickname is Zook. You can call me Zook if you love me. I work for Samsung. I'm a researcher there. I do some neat research for them. I really like it. I'm also a partner at PIA. PIA is a Pre-Incident Assessment. Just penetration testing, reverse engineering, custom stuff like that. So you can follow me on Twitter under that account. It's actually a sticker name I borrowed the name from. That's the blog. The full presentation and the paper, which will have, like, more technical details, will be there. The updated one, not the one in the city. It's quite not updated. It hasn't got any Android info. So the updated one is going to be there in a few hours. For any requests, talks, whatever. Dirty offers. That's my e-mail. Feel free. Well, like I said, the presentation is not enough. You need, if you really want, like, it's nice to know exploitation techniques and stuff. If you want, like, real more technical details, you can see the paper. It's on the city, but it's also will be in the website, the updated one. Will be there in that website. All right. So that's what's going to be actually in this talk. We're going to start from the very beginning. And all right. Someone just told me to speak louder. So everybody can hear me? Right there? All right. Cool. Like, what's the calling convention? Why ARM exploitation is different from x86? What we need to adjust in order to make our exploit work from x86? x86 on ARM. Some local attacks, remote attacks, and finally a demo on this Android phone. All right. Let's do it. So that's the best skills I can get in PowerPoint, because all right. If you don't like it, just leave it, because I can't get any better in PowerPoint. Anyway. Why would someone want to hack? Why would someone want to hack a mobile phone or an ARM device or whatever? Let's stay on mobile phones. There's various reasons you want to do that. One of them is it's just like a regular computer these days. You can use it for botnets. You can make calls, send SMSs. That's someone who wants to attack from remote. You can just charge them. You can do whatever you want. It's a computer that can make calls. So that's it. Also, applications are very common these days. So if you download a malicious application for, let's say, a weather, and it actually wants to use your Bluetooth or send e-mails from you and stuff like that, and also it only requests in the manifest just permissions for the, I don't know, nothing. And then it exploits some system APIs to escalation and just use the Bluetooth model or your Internet or your calls. Yeah. And there's also the user itself that would want to hack a mobile phone. You would want to do that to reveal some stuff that the mobile operator didn't allow him to do. You can see that in iPhones. A lot of people do that. All right. So after the very beginning, we'll cover the status of Buffalo Flows on x86. These days, you can see on x86.many.com. It's really hard, actually, to exploit these days on x86 architecture. The stack and heap is not executable. You've got stack cookies. ASLR is very, very most of the places are randomized very good. It's really hard to bypass, especially if you've got all of the vectors. On ARM, it's quite different. The architecture is different. So you have to adjust stuff. It's not fully working. Also, the stack and heap, they're not executable on most devices these days. But you can see ASLR. Most of the time, there is a PAX patch for a Linux platform a few months ago. So now you can see ASLR being implemented, but it's not mainline yet. So let's cover x86.red2libc attack. I'm just beginning from here. From scratch. If you want to exploit stack overflow on ARM architecture, on x86 architecture, it will be something like that. It's very easy to... Can you see my mouse pointer? All right. It's very easy to exploit. You just put the buffer here. Let's say you want to slash pin slash sh to execute. It's just very easy. You put the buffer here. You override the EBP. It doesn't really matter, actually. Because it's... It's... It's good for frame faking and more advanced attacks. But in this case, it's not. You just override the EBP with the system function address. You can also put return address and just push parameter on the stack. So you put your parameter in the payload like that. It's very easy. I think x86 was meant to be hacked. I mean, look at that. You just can push parameters through the stack. In the same payload, I think someone up there wanted us to hack the x86 to make it easier somehow. All right. So for the demo, like I said, we would want to use the system function. Just we'll execute our command and we want to execute slash pin slash sh. All right. It will not work on ARM. Like that. It wasn't meant to be hacker friendly like x86. I think that's my opinion. So yeah, you know, just don't. Also why wouldn't it work on ARM? Because if it worked, we wouldn't have this presentation right here. And it has different calling conventions. And it works differently because ARM assembly works differently. And we just want to cover that. So we're going to the technical part. Actually when I edited this presentation yesterday, I wanted to add this girl. I want to do a break before the technical details. Just put girls with tits or something so you can have a break before I'm going to technical details. But I decided to remove it because there are some girls in the crowd. So that's I'll get killed or something. All right. So this is the basics of ARM assembly. That's what actually you need to know for this presentation. You can also always become better with that one. The ARM assembly is unlike x86 assembly. In x86, if you want to push parameter, you can do it through the stack. In ARM, you just need to put it in the registers. So let's say if I wanted to put slash BNSH in ‑‑ to push it somehow, I would need to put it on R0. And I can do it through the payload like that. So that's less soccer friendly. Also there is ‑‑ the registers can be edited. Like PC is similar to EIP. It's the instruction pointer. And you can edit it as is. Not like x86. You can actually move address to PC and it will be the next instruction. What you also need to know is ‑‑ well. R0 to R3 are the most important registers. The rest ‑‑ R13 is the stack pointer and LR, link register, is R14. R15 is, like I said, the EIP of ARM. It's PC. So let's start it exploiting. So read to libc overwrites the return address and pass parameters to a vulnerable function. But we can't pass parameters on the vulnerable function. Because we need to store it. On the R0 register, for instance, in this one. So we're quite screwed. And we can only override existing variables from local function. So it usually depends on which function you're ‑‑ has the vulnerability in it. And we can also override the PC, the program counter. So that's what we'll use in order to get control of the ‑‑ full control of the application. So there's no really read to libc attack on ARM. We'll have to make some adjustments. So, all right. Why is it actually passable? So when entering a vulnerable function, the push parameter is usually ‑‑ it's not most ‑‑ it's not all cases, but it's most of them. The R14 is being popped into the PC at the end of the function. So if you can override the R14. At the end of the function. it will be being popped to the PC, the R15, so you get control of the code. If you can control it, we can own it, actually. So let's do it like a POC in a program that is very hacker friendly. It gets the PC, right? It gets the buffer here and the stack pointer is pointing to the beginning of our buffer. It's very convenient. Our zero is pointing to the beginning of the buffer, also very convenient. So let's just put a command here. And after we override the PC, just we'll put the system address. address of the function. So we'll see PS is being I'm actually on my screen. It's mirrored. You can see PS function is being executed. But that's a lie because that is very convenient hacking friendly program that I made especially for that. So that's not a real life scenario. Anyway, that's the POC that it can work. Okay. Let's face it. It's not real life scenario. In order to have that scenario, we need a function that returns void, that after the overflow nothing happens. So R0 will not be deleted. And it should be small. There's another problem with that. The buffer should be small, like 16 bytes in order to have a free space of the system function not using the same. Stack pointer space. So it will not override each other. So there is actually almost no chance in the world for that to happen. And if it happened to you, you're very lucky. It's like bringing the jackpot in Wheel of Fortune outside. So, yeah, you don't want that scenario. All right. So what we need actually for a successful hack, we need several stuff. So let's do it. We need parameter adjustments and variable adjustments. Parameter adjustments will be just register adjustments, R0 to R3. We must modify that in order to continue exploiting and do whatever we want. We also would like to get back the control to PC. So after we execute some command, we won't still control the flow of the buffer. And we also might want to do a stack lifting because, like I said, you don't have to come back. You don't have to come back. You don't have to come down on the buffer to be 16 bytes or if it's a longer buffer, you're screwed. You don't want to do that. In the 16 bytes, you just get four bytes of buffer that you can execute. And that's not enough unless it's a local attack. So, like, everybody is just making attacking names. So we just made a new one. Because it's ‑‑ I don't know. I don't really put a lot of thought into it. A lot of that. But it's just a mix of a lot of stuff. Return area to programming, stack lifting, parameter adjustments, and that's it. I mean, it's not really a name, just like a joke. Like everybody in the security community just make up names. So this is for Zook, but don't tell anyone, right? So I want to put my name outside. So how can we control the R0, R1? You're probably fascinated about that. So, like, amazing. Fascinating. R0. Yeah. All right. So we'll actually jump into a POP instruction. We'll do return‑oriented programming. There have been several return‑oriented programming in Black Hat this year and I think in DEF CON also. Very interesting. You can put that with this attack and do some more complex attacks. You can do actually whatever you want with that. So we'll need to jump into a POP instruction. Which also POPs the PC. Or we can control it later. And we can control the R0 or maybe more. All right? So I just looked like a quick regular expression on libc. And you can see, like, LDM is like POP in Intel. It's similar. I don't want to get into details what's different. But you can see it's POPing R0 and R1. So if we jump right here, we're going to be able to pop R0 and R1. We'll put any values that we want in there. Also you can see the stack pointer is lifting itself by 12 bytes. So there will be actually R0, R1 and extra four bytes. And then you can see POP PC. So we get four bytes of junk. So what we want to do after the buffer, we got AAAs. R4, R11. Well, it depends how it's compiled. Then we'll be jumping actually it's a mistake here. It's not 44, it's 48. So we'll be jumping here to the LDM instruction. And then we'll POP R0. We'll put it inside the slash bin slash SH address. R1 is not really matter for that one. But it's just four bytes. And then we got four bytes of junk. It doesn't really matter what you put there. And then we'll call system. So that's the original idea of how you can control the R0 and R1 instructions. But that's only good for local attackers. So let's ‑‑ we can do some more complex return‑oriented programming payloads. But let's just keep it that way in the system function. It's very easy. So if we can control R0 to point to a relative path to be in the beginning of our buffer, we can actually put any command. Let's say we want to put like netcat with reverse shell, we can do that. If R0 is pointing to the beginning of our buffer, we can actually put any command. So what we need in this one is actually to have, like ‑‑ R0 to point over to the relative caller to the beginning of the path. And then we'll call system. But like I said before, we need the size of the buffer to be a certain type. So it will not work. Because it's ‑‑ for short buffers you only get the words of unwritten commands. For long buffer, you get none. So that's not enough. We have to stack lift. So that's not enough. We have to stack lift. that's another thing in our attack we need to remember. Sometimes we just got to stack lift ourselves in order to execute the full payload without being controlled by the lack of the creators of libc and the one who compiled it. So our commands are actually very easy, very nice for exploitation. There are several variations of the same stuff. You can do many instructions. It's much more better than x86 in my opinion. So let's see what we can do in that one. We can abuse the current stack pointer. We can just lift ourselves and then just go back. All right? So let's just a quick look. Wprintf function epilogue. You can see that you can pop LR if we jump right here. We'll jump right here. And then the SP adds to itself 16 bytes. And then the next instruction will be BXLR. BX means branch. It checks the first bit. But in this case, just say it's like a call, advanced call. So like x86 advanced call. So we'll get our stack lifted by 16 bytes just by coming to that one. And then we'll put a new instruction. Right here where we want to jump next time. So we can jump again and again and again and lift our stack. You only need to lift our stack 384 bytes so the system will not override ourselves and we can execute whatever payload we want. So if you do that, our payload will look something like that. Like netcat, reverse shell. And that one is actually very important because it ignores the rest of the command until a null byte. So then as long as it's zero, we'll just write our zero here. It goes back and forth, back and forth until we've done enough of the stack. And execute. And then when we finish, we'll just go to our zero adjustment. Maybe that was a better idea to just put it right here. But anyway, we'll go to our zero adjustments. Again, our one adjustment, four bytes of junk. And then we'll call system. And we can execute whatever we want, whatever size we want of the buffer. Any hotmen? So another interesting part to adjust parameters will be I just seen it on m count epilogue. You can see you can pop any R0 to R3, whatever you want, and R11, LR, and then you branch to LR. So you can pop and adjust multiple parameters in the same jump, in the same return oriented in the same widget of this one. What we want to do next would be as a remote attackers would be actually to enable the stack and execute whatever we want. That can be easily done by calling and protect and changing the our place in the memory to be executable and then we'll have to jump on it. There is a good section of that. That's on perk magazine, the alphanumeric arm shell codes. So yesterday I was very bored. It was like 3 a.m. and well, that's the Android device I'm going to demo on that one. Actually we went pretty fast. It's cool. So let's see. Let's see if we can get a shell out of Android application. There are several limitations on Android devices. First of all, you don't want it to be like a Dalvik application. You want it to be a native Android application, for instance, something that had been compiled with NDK. NDK is the native development kit for Android. And well, it's actually pretty hard to compile dynamically. There's lots of tutorials in the Internet, but it took some time. It's not user-friendly. And also Android libc had been compiled differently. The binsh is actually nothing in Android. There is slash system slash binsh. They recompiled their libc. And they did ‑‑ I don't know if they did it by mistake or by luck, but like by mistake or they really meant to do it. But libc of Android is much more harder to exploit because it has no pop R0 instructions and stuff like that. So you have to be a little bit more complicated to do that. But it's the same technique. So it works on every ARM architecture. So we don't get things on R0 immediately. But we can do that. And the slash binsh is actually on a different path. But, for instance, the system function is being implemented that it executes execve function with the full path of the binsh in regular libc. So it has to do that also. So they just modify the variable and they just put system slash binsh. So that string should also be in the same library. Yeah. So we're cool with that. All right. So don't worry. It's all the same. Also here in Android. We'll get the slash system binsh to R0. And we'll not pop R0, but we'll do a trick to do that. As you can see that, I hope that some people can see the trick here. I'll give you like ten seconds to think about it before I say what it is. It's the malinfo function, just a function in Android libc that I took from that one. And you can see that you can pop to ‑‑ that's a pop instruction if you're not familiar with ARM architecture. So you can actually pop to R4 and get the PC back. So let's do that. We'll pop to R4 instruction. So we can control the R4 register. All right. So that's the first step we want to do. So after we control the R4 register and we control the PC, which means the next instruction, we might want to go here. So we just control the R4 instruction and then we move the R4 to R0. And then we can put some more four bytes of junk on R4 and we can go to system. So our payload will look like that. If that's ‑‑ If that thing is the LDM R4, we'll just put this slash system slash bin SH address in R4. And then we'll jump ‑‑ like you can see, it's four bytes offset between that one. So we'll just jump to the first instruction. And that will move the R4 to R0. And then we can put some more four bytes of junk. That will be on 10 bytes. The next R4, the second phase of this attack, and then we'll go to system. So well, I'm quite ‑‑ I did it too hurry. I was too hurry to do that conversation. So let's show the demo then. So what I did here was actually compiling ‑‑ yeah, I can read it ‑‑ a native application for Android. Just to run C application on Android. What it do is just calling a vulnerable function. That's after the function. So you're not supposed to see that if the payload had succeeded or the program had crashed. Because after the function, we'd get a segmentation fault. So we go to the stack overflow, which means stack overflow, and we got two buffers, a small buffer and a big buffer. What we do is actually read from that place, slash SD card, slash buffer. And we'll just put the stuff in it on the small buffer via memcpy. I used memcpy because there was actually ‑‑ I think there was a null in one of the parameters. So they were lucky on this version. But it was an honest mistake by them, I think. So I used memcpy to copy the buffer to the small buffer. The small buffer is 16 bytes. And the big buffer is, I don't know, 256. So what should happen if we succeed in this one is actually getting a shell. I put a ‑‑ my shell code is on ‑‑ on that place. And it has the same technique I described here. So what we're going to do is put AAAs and then we're going to adjust the R4. We're going to add the R4. We're going to jump again. We're going to just R4 to slash system slash bin SH. We're going to jump again to the move instruction. So the R0 will contain ‑‑ so the R0 will contain our parameter. And then we'll jump back and do the system. All right. So let's do it. Okay. That's this device right here. It's the G1, I think. The only Android device I had on my desk. Let me find it. All right. I got a little bit drunk yesterday. No, not really. So that's our function ‑‑ that's our binary. It has the same technique. It's just a C code. It's just a C code. earlier. It was a nightmare to compile it on Android. But here we go. I'm going to execute it and we're going to get a shell. So we got our shell, like you can see. And if you can see what executed is the buff one executed a shell that executed a shell. So we made it. We won. Victory. All right. I feel like dumb. All right. Thank you. And so we actually made it. And when we're going to exit the binary, we're supposed to see a segmentation fault and it's not going to get into the printf that after the function. Because the path has been ‑‑ the path of the execution has been altered. So we're going to exit out of the shell and if I'm not joking with you, it's supposed to see a segmentation fault. So we just stop it and now ‑‑ all right. So we did it. We got a shell. That was the least complex one. But this stuff works. So that's the whole point of the talk, right? So you can do whatever you want, actually, on Android. So I'm about to finish right now. I did it a bit earlier. You might want to catch some other talk anyway. The buffers on ARM are actually a real threat. We need some more security mechanism set up on that one. And like fully ASLR on a main line, we want to do that. And if you're writing an application for ARM, don't count on it being like unexploitable because it can be exploitable, like you can see. Sometimes it can be even easier. Sometimes it's harder. It's not a hacker's heaven like x86, but it can be done. If you want to prevent it, we want not a single unrandomized static code and stack cookies and like the most vectors we can to prevent buffer overflows. The better. If anyone got questions, I'll be at Q&A 114. You can download the full talk and the paper, like I said. And if anyone wants a business card or something, I got like a full pack here. Anyway, special thanks to some guys, Ilan from Samsung Israel and Moshe Verd and Matthew Carpenter from Matthew from InGuardians. Helped me a lot. Some references and that's it. All right. Thank you. Thank you. Thank you. Thank you.