1 00:00:00,000 --> 00:00:00,300 All right. 2 00:00:00,340 --> 00:00:01,220 Next speaker is Zoukirin. 3 00:00:01,300 --> 00:00:01,780 He's going to be talking. 4 00:00:02,040 --> 00:00:04,080 So let's please give him a big DEF CON welcome. 5 00:00:04,360 --> 00:00:04,640 Come on. 6 00:00:07,940 --> 00:00:10,120 ZOUKIRIN ZOUKIRIN Thank you. 7 00:00:11,260 --> 00:00:14,140 Hello and welcome to my talk on ARM Exploitation Techniques. 8 00:00:15,960 --> 00:00:17,260 Thank you, crowd. 9 00:00:17,560 --> 00:00:18,040 I love you. 10 00:00:18,800 --> 00:00:21,780 Hello and welcome to my talk on ARM Exploitation Techniques. 11 00:00:24,740 --> 00:00:25,600 Thank you again. 12 00:00:26,580 --> 00:00:27,400 Can you hear me? 13 00:00:27,480 --> 00:00:28,240 You in the back? 14 00:00:29,640 --> 00:00:30,440 All right. 15 00:00:30,520 --> 00:00:30,980 We got it. 16 00:00:31,980 --> 00:00:35,140 Hello and welcome to my talk on ARM Exploitation Techniques. 17 00:00:35,260 --> 00:00:36,640 I actually said it three times now. 18 00:00:37,800 --> 00:00:45,100 In this talk, I will cover exploitation technique on both ARM and x86 architecture from scratch, 19 00:00:45,220 --> 00:00:45,420 actually. 20 00:00:45,480 --> 00:00:49,780 So if you've never done it before, it's the time to start, actually. 21 00:00:51,260 --> 00:00:54,700 This is my first DEF CON ever. 22 00:00:55,960 --> 00:00:58,040 So I'm really excited to be here with you. 23 00:01:00,220 --> 00:01:04,200 I'm really happy to attend this great conversation, this great event. 24 00:01:05,760 --> 00:01:09,440 Well, I had a buddy who had to go yesterday. 25 00:01:09,700 --> 00:01:10,720 He couldn't stay today. 26 00:01:10,900 --> 00:01:14,620 So I promised him I'll take a photo of you while you scream DEF CON. 27 00:01:14,860 --> 00:01:18,780 So if you can do that for me, that would be awesome. 28 00:01:19,200 --> 00:01:22,640 Like five, four, three, two, one. 29 00:01:25,020 --> 00:01:25,460 Yeah. 30 00:01:26,640 --> 00:01:27,960 I'm going to post it online. 31 00:01:28,020 --> 00:01:29,520 I'm on that blog right there. 32 00:01:29,700 --> 00:01:32,360 So you can see yourself. 33 00:01:34,820 --> 00:01:37,340 I'm Isaac Avram. 34 00:01:37,620 --> 00:01:38,840 My nickname is Zook. 35 00:01:38,940 --> 00:01:40,580 You can call me Zook if you love me. 36 00:01:41,420 --> 00:01:42,740 I work for Samsung. 37 00:01:43,300 --> 00:01:44,460 I'm a researcher there. 38 00:01:45,300 --> 00:01:48,180 I do some neat research for them. 39 00:01:48,280 --> 00:01:48,940 I really like it. 40 00:01:49,200 --> 00:01:50,940 I'm also a partner at PIA. 41 00:01:51,100 --> 00:01:52,420 PIA is a Pre-Incident Assessment. 42 00:01:53,400 --> 00:01:57,720 Just penetration testing, reverse engineering, custom stuff like that. 43 00:01:58,020 --> 00:02:00,860 So you can follow me on Twitter under that account. 44 00:02:01,360 --> 00:02:05,180 It's actually a sticker name I borrowed the name from. 45 00:02:05,560 --> 00:02:06,400 That's the blog. 46 00:02:07,000 --> 00:02:13,420 The full presentation and the paper, which will have, like, more technical details, will 47 00:02:13,420 --> 00:02:14,320 be there. 48 00:02:14,600 --> 00:02:16,560 The updated one, not the one in the city. 49 00:02:16,740 --> 00:02:18,040 It's quite not updated. 50 00:02:18,220 --> 00:02:21,980 It hasn't got any Android info. 51 00:02:22,140 --> 00:02:25,040 So the updated one is going to be there in a few hours. 52 00:02:26,040 --> 00:02:27,720 For any requests, talks, whatever. 53 00:02:28,020 --> 00:02:28,580 Dirty offers. 54 00:02:29,580 --> 00:02:30,320 That's my e-mail. 55 00:02:32,280 --> 00:02:32,880 Feel free. 56 00:02:35,660 --> 00:02:37,760 Well, like I said, the presentation is not enough. 57 00:02:38,380 --> 00:02:43,800 You need, if you really want, like, it's nice to know exploitation techniques and stuff. 58 00:02:44,320 --> 00:02:48,800 If you want, like, real more technical details, you can see the paper. 59 00:02:48,960 --> 00:02:52,340 It's on the city, but it's also will be in the website, the updated one. 60 00:02:53,980 --> 00:02:55,620 Will be there in that website. 61 00:02:59,100 --> 00:02:59,800 All right. 62 00:03:00,900 --> 00:03:03,940 So that's what's going to be actually in this talk. 63 00:03:04,740 --> 00:03:06,600 We're going to start from the very beginning. 64 00:03:08,340 --> 00:03:10,120 And all right. 65 00:03:10,160 --> 00:03:11,780 Someone just told me to speak louder. 66 00:03:11,940 --> 00:03:12,900 So everybody can hear me? 67 00:03:12,960 --> 00:03:13,520 Right there? 68 00:03:14,060 --> 00:03:14,660 All right. 69 00:03:14,680 --> 00:03:14,840 Cool. 70 00:03:17,020 --> 00:03:18,580 Like, what's the calling convention? 71 00:03:18,860 --> 00:03:22,900 Why ARM exploitation is different from x86? 72 00:03:23,960 --> 00:03:28,000 What we need to adjust in order to make our exploit work from x86? 73 00:03:28,020 --> 00:03:28,800 x86 on ARM. 74 00:03:31,240 --> 00:03:35,660 Some local attacks, remote attacks, and finally a demo on this Android phone. 75 00:03:38,240 --> 00:03:38,800 All right. 76 00:03:38,840 --> 00:03:39,300 Let's do it. 77 00:03:42,740 --> 00:03:48,520 So that's the best skills I can get in PowerPoint, because all right. 78 00:03:48,540 --> 00:03:53,820 If you don't like it, just leave it, because I can't get any better in PowerPoint. 79 00:03:56,000 --> 00:03:56,440 Anyway. 80 00:03:56,620 --> 00:03:57,760 Why would someone want to hack? 81 00:03:57,760 --> 00:04:00,640 Why would someone want to hack a mobile phone or an ARM device or whatever? 82 00:04:00,920 --> 00:04:02,380 Let's stay on mobile phones. 83 00:04:03,160 --> 00:04:05,540 There's various reasons you want to do that. 84 00:04:05,900 --> 00:04:10,320 One of them is it's just like a regular computer these days. 85 00:04:10,680 --> 00:04:12,300 You can use it for botnets. 86 00:04:12,580 --> 00:04:15,860 You can make calls, send SMSs. 87 00:04:15,980 --> 00:04:17,900 That's someone who wants to attack from remote. 88 00:04:18,540 --> 00:04:20,440 You can just charge them. 89 00:04:21,460 --> 00:04:22,780 You can do whatever you want. 90 00:04:22,920 --> 00:04:24,400 It's a computer that can make calls. 91 00:04:25,740 --> 00:04:26,660 So that's it. 92 00:04:27,780 --> 00:04:29,980 Also, applications are very common these days. 93 00:04:30,140 --> 00:04:36,240 So if you download a malicious application for, let's say, a weather, and it actually 94 00:04:36,240 --> 00:04:41,380 wants to use your Bluetooth or send e-mails from you and stuff like that, and also it 95 00:04:41,380 --> 00:04:46,820 only requests in the manifest just permissions for the, I don't know, nothing. 96 00:04:47,100 --> 00:04:53,120 And then it exploits some system APIs to escalation and just use the Bluetooth 97 00:04:53,120 --> 00:04:55,900 model or your Internet or your calls. 98 00:04:56,160 --> 00:04:56,300 Yeah. 99 00:04:58,760 --> 00:05:02,620 And there's also the user itself that would want to hack a mobile phone. 100 00:05:03,460 --> 00:05:09,380 You would want to do that to reveal some stuff that the mobile operator didn't allow him 101 00:05:09,380 --> 00:05:09,700 to do. 102 00:05:10,280 --> 00:05:11,840 You can see that in iPhones. 103 00:05:12,140 --> 00:05:13,380 A lot of people do that. 104 00:05:17,300 --> 00:05:18,040 All right. 105 00:05:18,120 --> 00:05:24,180 So after the very beginning, we'll cover the status of Buffalo Flows on x86. 106 00:05:24,740 --> 00:05:27,740 These days, you can see on x86.many.com. 107 00:05:30,380 --> 00:05:35,640 It's really hard, actually, to exploit these days on x86 architecture. 108 00:05:36,140 --> 00:05:38,160 The stack and heap is not executable. 109 00:05:38,760 --> 00:05:39,660 You've got stack cookies. 110 00:05:40,620 --> 00:05:45,580 ASLR is very, very most of the places are randomized very good. 111 00:05:46,220 --> 00:05:49,800 It's really hard to bypass, especially if you've got all of the vectors. 112 00:05:51,000 --> 00:05:52,880 On ARM, it's quite different. 113 00:05:53,220 --> 00:05:54,580 The architecture is different. 114 00:05:54,720 --> 00:05:56,740 So you have to adjust stuff. 115 00:05:56,740 --> 00:05:57,880 It's not fully working. 116 00:05:58,120 --> 00:06:02,640 Also, the stack and heap, they're not executable on most devices these days. 117 00:06:03,420 --> 00:06:05,560 But you can see ASLR. 118 00:06:06,640 --> 00:06:13,380 Most of the time, there is a PAX patch for a Linux platform a few months ago. 119 00:06:13,760 --> 00:06:18,380 So now you can see ASLR being implemented, but it's not mainline yet. 120 00:06:21,400 --> 00:06:24,560 So let's cover x86.red2libc attack. 121 00:06:24,900 --> 00:06:26,720 I'm just beginning from here. 122 00:06:26,720 --> 00:06:27,200 From scratch. 123 00:06:28,600 --> 00:06:36,080 If you want to exploit stack overflow on ARM architecture, on x86 architecture, it will 124 00:06:36,080 --> 00:06:36,780 be something like that. 125 00:06:36,880 --> 00:06:37,740 It's very easy to... 126 00:06:37,740 --> 00:06:39,640 Can you see my mouse pointer? 127 00:06:39,860 --> 00:06:39,980 All right. 128 00:06:40,460 --> 00:06:41,800 It's very easy to exploit. 129 00:06:41,980 --> 00:06:43,340 You just put the buffer here. 130 00:06:43,840 --> 00:06:47,580 Let's say you want to slash pin slash sh to execute. 131 00:06:48,920 --> 00:06:50,080 It's just very easy. 132 00:06:50,200 --> 00:06:51,280 You put the buffer here. 133 00:06:51,760 --> 00:06:53,040 You override the EBP. 134 00:06:53,120 --> 00:06:54,140 It doesn't really matter, actually. 135 00:06:54,300 --> 00:06:55,320 Because it's... 136 00:06:56,220 --> 00:06:56,700 It's... 137 00:06:56,720 --> 00:06:59,240 It's good for frame faking and more advanced attacks. 138 00:06:59,420 --> 00:07:00,540 But in this case, it's not. 139 00:07:01,200 --> 00:07:05,660 You just override the EBP with the system function address. 140 00:07:06,620 --> 00:07:11,100 You can also put return address and just push parameter on the stack. 141 00:07:11,360 --> 00:07:15,800 So you put your parameter in the payload like that. 142 00:07:16,000 --> 00:07:16,660 It's very easy. 143 00:07:16,800 --> 00:07:19,600 I think x86 was meant to be hacked. 144 00:07:21,260 --> 00:07:23,920 I mean, look at that. 145 00:07:24,020 --> 00:07:26,440 You just can push parameters through the stack. 146 00:07:26,440 --> 00:07:36,460 In the same payload, I think someone up there wanted us to hack the x86 to make it easier 147 00:07:36,460 --> 00:07:37,300 somehow. 148 00:07:39,160 --> 00:07:40,160 All right. 149 00:07:40,180 --> 00:07:43,980 So for the demo, like I said, we would want to use the system function. 150 00:07:44,720 --> 00:07:49,180 Just we'll execute our command and we want to execute slash pin slash sh. 151 00:07:52,320 --> 00:07:53,000 All right. 152 00:07:54,100 --> 00:07:55,660 It will not work on ARM. 153 00:07:56,440 --> 00:07:56,820 Like that. 154 00:07:57,000 --> 00:08:00,040 It wasn't meant to be hacker friendly like x86. 155 00:08:00,240 --> 00:08:01,360 I think that's my opinion. 156 00:08:03,770 --> 00:08:06,800 So yeah, you know, just don't. 157 00:08:07,060 --> 00:08:08,820 Also why wouldn't it work on ARM? 158 00:08:08,920 --> 00:08:13,040 Because if it worked, we wouldn't have this presentation right here. 159 00:08:13,400 --> 00:08:17,960 And it has different calling conventions. 160 00:08:19,010 --> 00:08:22,100 And it works differently because ARM assembly works differently. 161 00:08:23,220 --> 00:08:25,960 And we just want to cover that. 162 00:08:28,900 --> 00:08:30,760 So we're going to the technical part. 163 00:08:30,880 --> 00:08:34,900 Actually when I edited this presentation yesterday, I wanted to add this girl. 164 00:08:36,320 --> 00:08:39,000 I want to do a break before the technical details. 165 00:08:39,360 --> 00:08:44,700 Just put girls with tits or something so you can have a break before I'm going to 166 00:08:44,700 --> 00:08:45,340 technical details. 167 00:08:45,580 --> 00:08:48,120 But I decided to remove it because there are some girls in the crowd. 168 00:08:48,300 --> 00:08:51,120 So that's I'll get killed or something. 169 00:08:52,900 --> 00:08:53,780 All right. 170 00:08:53,880 --> 00:08:55,880 So this is the basics of ARM assembly. 171 00:08:55,880 --> 00:08:59,900 That's what actually you need to know for this presentation. 172 00:09:00,180 --> 00:09:03,080 You can also always become better with that one. 173 00:09:05,000 --> 00:09:07,920 The ARM assembly is unlike x86 assembly. 174 00:09:08,280 --> 00:09:12,020 In x86, if you want to push parameter, you can do it through the stack. 175 00:09:13,320 --> 00:09:16,080 In ARM, you just need to put it in the registers. 176 00:09:16,580 --> 00:09:25,780 So let's say if I wanted to put slash BNSH in ‑‑ to push it somehow, I would need 177 00:09:25,880 --> 00:09:26,960 to put it on R0. 178 00:09:27,160 --> 00:09:29,980 And I can do it through the payload like that. 179 00:09:30,700 --> 00:09:32,280 So that's less soccer friendly. 180 00:09:33,720 --> 00:09:36,580 Also there is ‑‑ the registers can be edited. 181 00:09:36,840 --> 00:09:39,320 Like PC is similar to EIP. 182 00:09:39,560 --> 00:09:40,520 It's the instruction pointer. 183 00:09:42,240 --> 00:09:44,860 And you can edit it as is. 184 00:09:45,040 --> 00:09:46,540 Not like x86. 185 00:09:47,100 --> 00:09:52,100 You can actually move address to PC and it will be the next instruction. 186 00:09:53,400 --> 00:09:55,820 What you also need to know is ‑‑ well. 187 00:09:55,820 --> 00:09:59,940 R0 to R3 are the most important registers. 188 00:10:00,500 --> 00:10:07,540 The rest ‑‑ R13 is the stack pointer and LR, link register, is R14. 189 00:10:08,040 --> 00:10:10,640 R15 is, like I said, the EIP of ARM. 190 00:10:10,740 --> 00:10:11,580 It's PC. 191 00:10:13,280 --> 00:10:15,680 So let's start it exploiting. 192 00:10:17,060 --> 00:10:21,640 So read to libc overwrites the return address and pass parameters to a vulnerable function. 193 00:10:21,820 --> 00:10:24,600 But we can't pass parameters on the vulnerable function. 194 00:10:24,680 --> 00:10:25,680 Because we need to store it. 195 00:10:25,820 --> 00:10:28,260 On the R0 register, for instance, in this one. 196 00:10:28,820 --> 00:10:30,200 So we're quite screwed. 197 00:10:32,060 --> 00:10:35,680 And we can only override existing variables from local function. 198 00:10:36,040 --> 00:10:41,180 So it usually depends on which function you're ‑‑ has the vulnerability in it. 199 00:10:41,740 --> 00:10:44,740 And we can also override the PC, the program counter. 200 00:10:44,980 --> 00:10:49,440 So that's what we'll use in order to get control of the ‑‑ full control of the application. 201 00:10:51,480 --> 00:10:55,260 So there's no really read to libc attack on ARM. 202 00:10:55,820 --> 00:10:56,960 We'll have to make some adjustments. 203 00:10:57,520 --> 00:10:59,340 So, all right. 204 00:11:00,640 --> 00:11:02,080 Why is it actually passable? 205 00:11:05,340 --> 00:11:11,240 So when entering a vulnerable function, the push parameter is usually ‑‑ it's not 206 00:11:11,240 --> 00:11:13,900 most ‑‑ it's not all cases, but it's most of them. 207 00:11:14,120 --> 00:11:17,640 The R14 is being popped into the PC at the end of the function. 208 00:11:18,320 --> 00:11:21,360 So if you can override the R14. 209 00:11:24,300 --> 00:11:25,760 At the end of the function. 210 00:11:25,820 --> 00:11:33,200 it will be being popped to the PC, the R15, so you get control of the code. If you can 211 00:11:33,200 --> 00:11:44,360 control it, we can own it, actually. So let's do it like a POC in a program that is very 212 00:11:44,360 --> 00:11:55,180 hacker friendly. It gets the PC, right? It gets the buffer here and the stack pointer 213 00:11:55,180 --> 00:12:00,480 is pointing to the beginning of our buffer. It's very convenient. Our zero is pointing 214 00:12:01,080 --> 00:12:03,460 to the beginning of the buffer, also very convenient. 215 00:12:03,980 --> 00:12:13,520 So let's just put a command here. And after we override the PC, just we'll put the system 216 00:12:13,520 --> 00:12:13,900 address. 217 00:12:13,900 --> 00:12:23,080 address of the function. So we'll see PS is being I'm actually on my screen. It's mirrored. 218 00:12:23,880 --> 00:12:32,860 You can see PS function is being executed. But that's a lie because that is very convenient 219 00:12:33,560 --> 00:12:40,200 hacking friendly program that I made especially for that. So that's not a real life scenario. 220 00:12:40,200 --> 00:12:48,000 Anyway, that's the POC that it can work. Okay. Let's face it. It's not real life scenario. 221 00:12:48,740 --> 00:12:53,600 In order to have that scenario, we need a function that returns void, that after the 222 00:12:53,600 --> 00:13:00,880 overflow nothing happens. So R0 will not be deleted. And it should be small. There's another 223 00:13:00,880 --> 00:13:07,160 problem with that. The buffer should be small, like 16 bytes in order to have a free space 224 00:13:07,780 --> 00:13:09,940 of the system function not using the same. 225 00:13:10,200 --> 00:13:16,700 Stack pointer space. So it will not override each other. So there is actually almost no 226 00:13:16,700 --> 00:13:21,380 chance in the world for that to happen. And if it happened to you, you're very lucky. 227 00:13:21,640 --> 00:13:28,620 It's like bringing the jackpot in Wheel of Fortune outside. So, yeah, you don't want 228 00:13:29,160 --> 00:13:38,320 that scenario. All right. So what we need actually for a successful 229 00:13:40,200 --> 00:13:45,080 hack, we need several stuff. So let's do it. We need parameter adjustments and variable 230 00:13:45,080 --> 00:13:50,480 adjustments. Parameter adjustments will be just register adjustments, R0 to R3. We must 231 00:13:50,480 --> 00:13:56,120 modify that in order to continue exploiting and do whatever we want. We also would like 232 00:13:56,120 --> 00:14:01,960 to get back the control to PC. So after we execute some command, we won't still control 233 00:14:01,960 --> 00:14:08,040 the flow of the buffer. And we also might want to do a stack lifting because, like I 234 00:14:08,040 --> 00:14:10,180 said, you don't have to come back. You don't have to come back. You don't have to come 235 00:14:10,200 --> 00:14:16,140 down on the buffer to be 16 bytes or if it's a longer buffer, you're screwed. You don't 236 00:14:16,140 --> 00:14:23,460 want to do that. In the 16 bytes, you just get four bytes of buffer that you can execute. 237 00:14:23,700 --> 00:14:31,660 And that's not enough unless it's a local attack. So, like, everybody is just making 238 00:14:32,320 --> 00:14:37,460 attacking names. So we just made a new one. Because it's ‑‑ I don't know. I don't 239 00:14:37,460 --> 00:14:40,180 really put a lot of thought into it. 240 00:14:40,180 --> 00:14:44,620 A lot of that. But it's just a mix of a lot of stuff. Return area to programming, stack 241 00:14:45,200 --> 00:14:49,960 lifting, parameter adjustments, and that's it. I mean, it's not really a name, just like 242 00:14:49,960 --> 00:14:56,420 a joke. Like everybody in the security community just make up names. So this is for Zook, but 243 00:14:56,420 --> 00:15:04,760 don't tell anyone, right? So I want to put my name outside. So how can we control the 244 00:15:04,760 --> 00:15:10,100 R0, R1? You're probably fascinated about that. So, like, amazing. 245 00:15:10,100 --> 00:15:17,540 Fascinating. R0. Yeah. All right. So we'll actually jump into a POP instruction. We'll 246 00:15:17,540 --> 00:15:21,540 do return‑oriented programming. There have been several return‑oriented programming 247 00:15:21,540 --> 00:15:30,040 in Black Hat this year and I think in DEF CON also. Very interesting. You can put that 248 00:15:30,620 --> 00:15:36,180 with this attack and do some more complex attacks. You can do actually whatever you 249 00:15:36,180 --> 00:15:39,400 want with that. So we'll need to jump into a POP instruction. 250 00:15:41,060 --> 00:15:49,100 Which also POPs the PC. Or we can control it later. And we can control the R0 or maybe 251 00:15:49,100 --> 00:15:55,320 more. All right? So I just looked like a quick regular expression on libc. And you 252 00:15:55,320 --> 00:16:03,520 can see, like, LDM is like POP in Intel. It's similar. I don't want to get into details 253 00:16:03,520 --> 00:16:10,080 what's different. But you can see it's POPing R0 and R1. So if we jump right here, we're 254 00:16:11,000 --> 00:16:17,720 going to be able to pop R0 and R1. We'll put any values that we want in there. Also you 255 00:16:17,720 --> 00:16:24,860 can see the stack pointer is lifting itself by 12 bytes. So there will be actually R0, 256 00:16:25,040 --> 00:16:31,200 R1 and extra four bytes. And then you can see POP PC. So we get four bytes of junk. 257 00:16:32,360 --> 00:16:39,040 So what we want to do after the buffer, we got AAAs. R4, R11. Well, it depends how it's 258 00:16:39,040 --> 00:16:39,360 compiled. 259 00:16:40,100 --> 00:16:45,020 Then we'll be jumping actually it's a mistake here. It's not 44, it's 48. So we'll be jumping 260 00:16:45,020 --> 00:16:51,960 here to the LDM instruction. And then we'll POP R0. We'll put it inside the slash bin 261 00:16:51,960 --> 00:16:58,040 slash SH address. R1 is not really matter for that one. But it's just four bytes. And 262 00:16:58,040 --> 00:17:02,320 then we got four bytes of junk. It doesn't really matter what you put there. And then 263 00:17:02,320 --> 00:17:08,460 we'll call system. So that's the original idea of how you can control the R0 and R1 264 00:17:08,460 --> 00:17:08,820 instructions. 265 00:17:10,300 --> 00:17:15,920 But that's only good for local attackers. So let's ‑‑ we can do some more complex 266 00:17:16,740 --> 00:17:21,160 return‑oriented programming payloads. But let's just keep it that way in the system 267 00:17:21,160 --> 00:17:22,280 function. It's very easy. 268 00:17:24,240 --> 00:17:30,500 So if we can control R0 to point to a relative path to be in the beginning of our buffer, 269 00:17:30,700 --> 00:17:35,220 we can actually put any command. Let's say we want to put like netcat with reverse shell, 270 00:17:35,720 --> 00:17:39,720 we can do that. If R0 is pointing to the beginning of our buffer, we can actually put any command. 271 00:17:41,000 --> 00:17:47,740 So what we need in this one is actually to have, like ‑‑ R0 to point over to the 272 00:17:47,740 --> 00:17:54,020 relative caller to the beginning of the path. And then we'll call system. But like I said 273 00:17:54,020 --> 00:18:00,720 before, we need the size of the buffer to be a certain type. So it will not work. Because 274 00:18:00,720 --> 00:18:05,340 it's ‑‑ for short buffers you only get the words of unwritten commands. For long 275 00:18:05,340 --> 00:18:06,440 buffer, you get none. 276 00:18:06,980 --> 00:18:09,700 So that's not enough. We have to stack lift. So that's not enough. We have to stack lift. 277 00:18:09,700 --> 00:18:14,380 that's another thing in our attack we need to remember. Sometimes we just got to stack 278 00:18:14,380 --> 00:18:20,180 lift ourselves in order to execute the full payload without being controlled by the lack 279 00:18:20,180 --> 00:18:26,320 of the creators of libc and the one who compiled it. 280 00:18:27,160 --> 00:18:34,560 So our commands are actually very easy, very nice for exploitation. There are several variations 281 00:18:34,560 --> 00:18:44,500 of the same stuff. You can do many instructions. It's much more better than x86 in my opinion. 282 00:18:45,780 --> 00:18:51,480 So let's see what we can do in that one. We can abuse the current stack pointer. We can 283 00:18:51,480 --> 00:18:59,000 just lift ourselves and then just go back. All right? So let's just a quick look. Wprintf 284 00:18:59,000 --> 00:19:04,060 function epilogue. You can see that you can pop LR if we jump right here. We'll jump right 285 00:19:04,060 --> 00:19:04,300 here. 286 00:19:04,300 --> 00:19:12,820 And then the SP adds to itself 16 bytes. And then the next instruction will be BXLR. BX 287 00:19:12,820 --> 00:19:22,000 means branch. It checks the first bit. But in this case, just say it's like a call, advanced 288 00:19:22,000 --> 00:19:25,580 call. So like x86 advanced call. 289 00:19:26,360 --> 00:19:33,480 So we'll get our stack lifted by 16 bytes just by coming to that one. And then we'll 290 00:19:33,480 --> 00:19:34,280 put a new instruction. 291 00:19:35,580 --> 00:19:40,720 Right here where we want to jump next time. So we can jump again and again and again and 292 00:19:40,720 --> 00:19:47,720 lift our stack. You only need to lift our stack 384 bytes so the system will not override 293 00:19:47,720 --> 00:19:50,700 ourselves and we can execute whatever payload we want. 294 00:19:52,300 --> 00:19:58,320 So if you do that, our payload will look something like that. Like netcat, reverse shell. And 295 00:19:58,320 --> 00:20:02,400 that one is actually very important because it ignores the rest of the command until a 296 00:20:02,400 --> 00:20:03,160 null byte. 297 00:20:04,280 --> 00:20:10,400 So then as long as it's zero, we'll just write our zero here. It goes back and forth, back 298 00:20:10,400 --> 00:20:12,500 and forth until we've done enough of the stack. 299 00:20:13,740 --> 00:20:19,180 And execute. And then when we finish, we'll just go to our zero adjustment. Maybe that 300 00:20:19,180 --> 00:20:24,180 was a better idea to just put it right here. But anyway, we'll go to our zero adjustments. 301 00:20:24,520 --> 00:20:29,880 Again, our one adjustment, four bytes of junk. And then we'll call system. And we can 302 00:20:29,880 --> 00:20:33,480 execute whatever we want, whatever size we want of the buffer. 303 00:20:33,780 --> 00:20:34,260 Any hotmen? 304 00:20:37,560 --> 00:20:45,580 So another interesting part to adjust parameters will be I just seen it on m count epilogue. 305 00:20:45,900 --> 00:20:54,240 You can see you can pop any R0 to R3, whatever you want, and R11, LR, and then you branch 306 00:20:54,240 --> 00:20:54,740 to LR. 307 00:20:54,860 --> 00:21:00,140 So you can pop and adjust multiple parameters in the same jump, in the same return oriented 308 00:21:00,140 --> 00:21:04,260 in the same widget of this one. 309 00:21:06,200 --> 00:21:13,780 What we want to do next would be as a remote attackers would be actually to enable the 310 00:21:13,780 --> 00:21:15,340 stack and execute whatever we want. 311 00:21:16,200 --> 00:21:24,340 That can be easily done by calling and protect and changing the our place in the memory to 312 00:21:24,340 --> 00:21:26,760 be executable and then we'll have to jump on it. 313 00:21:27,020 --> 00:21:29,440 There is a good section of that. 314 00:21:29,440 --> 00:21:32,340 That's on perk magazine, the alphanumeric arm shell codes. 315 00:21:37,100 --> 00:21:40,920 So yesterday I was very bored. 316 00:21:41,080 --> 00:21:50,940 It was like 3 a.m. and well, that's the Android device I'm going to demo on that one. 317 00:21:51,080 --> 00:21:52,240 Actually we went pretty fast. 318 00:21:55,320 --> 00:21:55,960 It's cool. 319 00:21:57,460 --> 00:21:58,580 So let's see. 320 00:21:58,580 --> 00:22:02,140 Let's see if we can get a shell out of Android application. 321 00:22:04,000 --> 00:22:07,160 There are several limitations on Android devices. 322 00:22:08,420 --> 00:22:11,420 First of all, you don't want it to be like a Dalvik application. 323 00:22:11,660 --> 00:22:17,500 You want it to be a native Android application, for instance, something that had been compiled 324 00:22:17,500 --> 00:22:18,060 with NDK. 325 00:22:18,940 --> 00:22:22,300 NDK is the native development kit for Android. 326 00:22:23,160 --> 00:22:27,860 And well, it's actually pretty hard to compile dynamically. 327 00:22:27,860 --> 00:22:31,780 There's lots of tutorials in the Internet, but it took some time. 328 00:22:32,000 --> 00:22:33,520 It's not user-friendly. 329 00:22:34,960 --> 00:22:38,580 And also Android libc had been compiled differently. 330 00:22:39,640 --> 00:22:43,220 The binsh is actually nothing in Android. 331 00:22:43,500 --> 00:22:45,600 There is slash system slash binsh. 332 00:22:45,880 --> 00:22:47,500 They recompiled their libc. 333 00:22:48,520 --> 00:22:56,260 And they did ‑‑ I don't know if they did it by mistake or by luck, but like by mistake 334 00:22:56,260 --> 00:22:57,840 or they really meant to do it. 335 00:22:57,860 --> 00:23:06,640 But libc of Android is much more harder to exploit because it has no pop R0 instructions 336 00:23:07,300 --> 00:23:08,100 and stuff like that. 337 00:23:08,200 --> 00:23:15,200 So you have to be a little bit more complicated to do that. 338 00:23:15,380 --> 00:23:16,920 But it's the same technique. 339 00:23:17,140 --> 00:23:19,660 So it works on every ARM architecture. 340 00:23:21,180 --> 00:23:24,680 So we don't get things on R0 immediately. 341 00:23:25,540 --> 00:23:27,280 But we can do that. 342 00:23:27,860 --> 00:23:31,560 And the slash binsh is actually on a different path. 343 00:23:31,900 --> 00:23:38,580 But, for instance, the system function is being implemented that it executes execve 344 00:23:38,580 --> 00:23:44,040 function with the full path of the binsh in regular libc. 345 00:23:44,500 --> 00:23:47,320 So it has to do that also. 346 00:23:48,040 --> 00:23:53,280 So they just modify the variable and they just put system slash binsh. 347 00:23:53,520 --> 00:23:56,600 So that string should also be in the same library. 348 00:23:57,660 --> 00:23:57,840 Yeah. 349 00:23:57,860 --> 00:23:59,680 So we're cool with that. 350 00:24:02,040 --> 00:24:02,700 All right. 351 00:24:02,780 --> 00:24:03,320 So don't worry. 352 00:24:03,700 --> 00:24:04,640 It's all the same. 353 00:24:04,820 --> 00:24:05,920 Also here in Android. 354 00:24:06,860 --> 00:24:10,720 We'll get the slash system binsh to R0. 355 00:24:11,560 --> 00:24:15,220 And we'll not pop R0, but we'll do a trick to do that. 356 00:24:15,680 --> 00:24:20,240 As you can see that, I hope that some people can see the trick here. 357 00:24:21,800 --> 00:24:25,220 I'll give you like ten seconds to think about it before I say what it is. 358 00:24:29,220 --> 00:24:34,840 It's the malinfo function, just a function in Android libc that I took from that one. 359 00:24:35,220 --> 00:24:40,040 And you can see that you can pop to ‑‑ that's a pop instruction if you're not familiar 360 00:24:40,040 --> 00:24:42,600 with ARM architecture. 361 00:24:43,520 --> 00:24:48,040 So you can actually pop to R4 and get the PC back. 362 00:24:48,360 --> 00:24:49,360 So let's do that. 363 00:24:49,640 --> 00:24:51,160 We'll pop to R4 instruction. 364 00:24:51,480 --> 00:24:53,960 So we can control the R4 register. 365 00:24:55,000 --> 00:24:55,200 All right. 366 00:24:55,220 --> 00:24:56,740 So that's the first step we want to do. 367 00:24:57,240 --> 00:25:01,920 So after we control the R4 register and we control the PC, which means the next instruction, 368 00:25:02,680 --> 00:25:05,660 we might want to go here. 369 00:25:08,020 --> 00:25:14,000 So we just control the R4 instruction and then we move the R4 to R0. 370 00:25:14,680 --> 00:25:19,980 And then we can put some more four bytes of junk on R4 and we can go to system. 371 00:25:21,020 --> 00:25:22,860 So our payload will look like that. 372 00:25:23,020 --> 00:25:23,820 If that's ‑‑ 373 00:25:26,700 --> 00:25:38,340 If that thing is the LDM R4, we'll just put this slash system slash bin SH address in R4. 374 00:25:38,580 --> 00:25:44,360 And then we'll jump ‑‑ like you can see, it's four bytes offset between that one. 375 00:25:44,580 --> 00:25:46,880 So we'll just jump to the first instruction. 376 00:25:47,820 --> 00:25:50,380 And that will move the R4 to R0. 377 00:25:50,960 --> 00:25:53,420 And then we can put some more four bytes of junk. 378 00:25:53,560 --> 00:25:55,200 That will be on 10 bytes. 379 00:25:55,220 --> 00:25:59,980 The next R4, the second phase of this attack, and then we'll go to system. 380 00:26:03,220 --> 00:26:06,540 So well, I'm quite ‑‑ I did it too hurry. 381 00:26:06,720 --> 00:26:08,540 I was too hurry to do that conversation. 382 00:26:10,880 --> 00:26:12,280 So let's show the demo then. 383 00:26:15,980 --> 00:26:24,660 So what I did here was actually compiling ‑‑ yeah, I can read it ‑‑ a native application 384 00:26:24,660 --> 00:26:25,580 for Android. 385 00:26:26,040 --> 00:26:27,760 Just to run C application on Android. 386 00:26:28,100 --> 00:26:32,940 What it do is just calling a vulnerable function. 387 00:26:34,140 --> 00:26:35,200 That's after the function. 388 00:26:35,360 --> 00:26:41,020 So you're not supposed to see that if the payload had succeeded or the program had crashed. 389 00:26:41,460 --> 00:26:43,900 Because after the function, we'd get a segmentation fault. 390 00:26:44,500 --> 00:26:52,720 So we go to the stack overflow, which means stack overflow, and we got two buffers, a 391 00:26:52,720 --> 00:26:54,020 small buffer and a big buffer. 392 00:26:55,280 --> 00:27:00,200 What we do is actually read from that place, slash SD card, slash buffer. 393 00:27:00,740 --> 00:27:08,080 And we'll just put the stuff in it on the small buffer via memcpy. 394 00:27:08,480 --> 00:27:13,520 I used memcpy because there was actually ‑‑ I think there was a null in one of the parameters. 395 00:27:13,860 --> 00:27:16,220 So they were lucky on this version. 396 00:27:16,420 --> 00:27:19,020 But it was an honest mistake by them, I think. 397 00:27:20,140 --> 00:27:24,560 So I used memcpy to copy the buffer to the small buffer. 398 00:27:24,660 --> 00:27:26,200 The small buffer is 16 bytes. 399 00:27:26,900 --> 00:27:30,200 And the big buffer is, I don't know, 256. 400 00:27:31,580 --> 00:27:37,340 So what should happen if we succeed in this one is actually getting a shell. 401 00:27:37,680 --> 00:27:43,380 I put a ‑‑ my shell code is on ‑‑ on that place. 402 00:27:43,880 --> 00:27:46,400 And it has the same technique I described here. 403 00:27:47,700 --> 00:27:53,300 So what we're going to do is put AAAs and then we're going to adjust the R4. 404 00:27:54,020 --> 00:27:54,640 We're going to add the R4. 405 00:27:54,660 --> 00:27:55,420 We're going to jump again. 406 00:27:55,940 --> 00:27:59,160 We're going to just R4 to slash system slash bin SH. 407 00:27:59,760 --> 00:28:03,480 We're going to jump again to the move instruction. 408 00:28:03,920 --> 00:28:11,740 So the R0 will contain ‑‑ so the R0 will contain our parameter. 409 00:28:12,020 --> 00:28:14,120 And then we'll jump back and do the system. 410 00:28:15,840 --> 00:28:16,540 All right. 411 00:28:16,580 --> 00:28:17,160 So let's do it. 412 00:28:19,020 --> 00:28:19,540 Okay. 413 00:28:27,240 --> 00:28:29,260 That's this device right here. 414 00:28:29,440 --> 00:28:31,520 It's the G1, I think. 415 00:28:32,200 --> 00:28:34,520 The only Android device I had on my desk. 416 00:28:37,740 --> 00:28:38,340 Let me find it. 417 00:28:53,440 --> 00:28:54,640 All right. 418 00:28:54,660 --> 00:28:55,200 I got a little bit drunk yesterday. 419 00:28:57,400 --> 00:28:58,060 No, not really. 420 00:29:17,660 --> 00:29:19,880 So that's our function ‑‑ that's our binary. 421 00:29:21,700 --> 00:29:22,840 It has the same technique. 422 00:29:23,060 --> 00:29:24,100 It's just a C code. 423 00:29:24,220 --> 00:29:24,640 It's just a C code. 424 00:29:24,660 --> 00:29:30,620 earlier. It was a nightmare to compile it on Android. But here we go. I'm going to execute 425 00:29:30,620 --> 00:29:38,740 it and we're going to get a shell. So we got our shell, like you can see. And if you can 426 00:29:38,740 --> 00:29:45,420 see what executed is the buff one executed a shell that executed a shell. So we made 427 00:29:45,420 --> 00:30:04,340 it. We won. Victory. All right. I feel like dumb. All right. Thank you. And so we actually 428 00:30:04,340 --> 00:30:10,080 made it. And when we're going to exit the binary, we're supposed to see a segmentation 429 00:30:10,080 --> 00:30:14,640 fault and it's not going to get into the printf that after the function. 430 00:30:15,700 --> 00:30:21,320 Because the path has been ‑‑ the path of the execution has been altered. So we're 431 00:30:21,320 --> 00:30:27,720 going to exit out of the shell and if I'm not joking with you, it's supposed to see 432 00:30:27,720 --> 00:30:36,160 a segmentation fault. So we just stop it and now ‑‑ all right. So we did it. We got 433 00:30:36,160 --> 00:30:43,740 a shell. That was the least complex one. But this stuff works. So that's the whole point 434 00:30:43,740 --> 00:30:44,560 of the talk, right? 435 00:30:45,420 --> 00:30:49,080 So you can do whatever you want, actually, on Android. 436 00:30:51,900 --> 00:30:59,280 So I'm about to finish right now. I did it a bit earlier. You might want to catch some 437 00:30:59,280 --> 00:31:07,280 other talk anyway. The buffers on ARM are actually a real threat. We need some more 438 00:31:07,280 --> 00:31:13,760 security mechanism set up on that one. And like fully ASLR on a main line, we want to 439 00:31:13,760 --> 00:31:14,120 do that. 440 00:31:15,420 --> 00:31:21,720 And if you're writing an application for ARM, don't count on it being like unexploitable 441 00:31:21,720 --> 00:31:27,620 because it can be exploitable, like you can see. Sometimes it can be even easier. Sometimes 442 00:31:27,620 --> 00:31:32,800 it's harder. It's not a hacker's heaven like x86, but it can be done. 443 00:31:33,620 --> 00:31:40,440 If you want to prevent it, we want not a single unrandomized static code and stack cookies 444 00:31:40,440 --> 00:31:44,180 and like the most vectors we can to prevent buffer overflows. 445 00:31:45,420 --> 00:31:45,820 The better. 446 00:31:47,020 --> 00:31:57,800 If anyone got questions, I'll be at Q&A 114. You can download the full talk and the paper, 447 00:31:57,920 --> 00:32:01,780 like I said. And if anyone wants a business card or something, I got like a full pack 448 00:32:01,780 --> 00:32:01,960 here. 449 00:32:04,200 --> 00:32:10,660 Anyway, special thanks to some guys, Ilan from Samsung Israel and Moshe Verd and Matthew 450 00:32:10,660 --> 00:32:15,120 Carpenter from Matthew from InGuardians. Helped me a lot. 451 00:32:15,420 --> 00:32:17,360 Some references and that's it. 452 00:32:17,660 --> 00:32:18,520 All right. 453 00:32:19,680 --> 00:32:20,580 Thank you. 454 00:32:20,760 --> 00:32:22,500 Thank you. 455 00:32:22,680 --> 00:32:23,980 Thank you. 456 00:32:23,980 --> 00:32:24,060 Thank you.