00:00:00.067-->00:00:04.438 >>Alright, so, there’s a there’s a bit of a- bit of a story here that I don’t know the full 00:00:04.438-->00:00:09.443 thing, but um you’ll notice that uh either the speaker is tiny or non existent. Which is, 00:00:12.079-->00:00:17.951 which-which he is uh, well he does exist, he just is not here um our speaker um apparently had 00:00:17.951-->00:00:22.956 enough juice to um, to convince us that we will now deliver this presentation in an automated way 00:00:25.792-->00:00:30.530 streamed via icloud dot com. That’s right, you heard that right, we’re going to try to 00:00:30.530-->00:00:35.536 stream an entire presentation from the internet right here on stage. Yeah, so [applause] So, 00:00:37.971-->00:00:42.276 >>To the internet! Alright! >>You guys know about autonomous things, so this is the-this is 00:00:42.276-->00:00:48.315 DefCon’s first autonomous presentation, and we also know what might happen if you get 00:00:48.315-->00:00:53.654 into an autonomous vehicle, that s**t might just completely crash. So, we have no earthly 00:00:53.654-->00:00:58.058 idea what’s going to happen here it really [off mic comment indiscernible] yeah we yeah 00:00:58.058-->00:01:02.996 right we hope that it goes really well, this is the first time we have used icloud dot com 00:01:05.632-->00:01:11.204 as a presentation medium which we think qualifies icloud dot com as a first time speaker and 00:01:11.204-->00:01:16.209 presenter, so therefore, [applause] we’re all in this together here’s to you icloud 00:01:20.113-->00:01:31.024 dot com and without further ado, my my very small co presenter. >>hello everyone this is Sanat. 00:01:31.024-->00:01:36.029 Sorry I couldn’t make it to DefCon, apparently my visa got rejected, so I have to pre 00:01:36.029-->00:01:41.802 record my entire talk. Uh, I hope I can convey the entire content of this presentation 00:01:41.802-->00:02:16.470 today. If you have any question just feel free to reach me out to twitter, my DMs are open 00:02:16.470-->00:02:21.541 >>Hello everyone this is Sanat. Sorry I couldn’t make it to DefCon, apparently my visa got 00:02:21.541-->00:02:26.546 rejected, so I have to pre record my entire talk. Uh, I hope I can convey the entire 00:02:26.546-->00:02:31.551 content of this presentation today. If you have any question just feel free to reach me out 00:02:31.551-->00:02:47.167 to twitter, my DMs are open. So um, I am a 19 year old security engineer at GoRoot based in 00:02:47.167-->00:02:54.841 Berlin, Germany and uh on weekends I play ctfs and dcua, uh ctf team based in Ukraine, I 00:02:54.841-->00:03:00.680 normally solve pwn challenges and sometimes upload solutions to them and images, so now let’s 00:03:00.680-->00:03:14.995 get started on the talk. um. So, regarding heap exploitation, there’ve been a ton of 00:03:14.995-->00:03:20.067 techniques being published like from 2005 uh house of spirit, house of force, like you know 00:03:20.067-->00:03:25.839 allow you to get a chunk anywhere in memory and uh the death started patching most of 00:03:25.839-->00:03:29.743 them but uh the research is still going on and there are a ton of new exploitation 00:03:29.743-->00:03:35.782 techniques coming up like 2016 house of orange it was a challenge in HITCON, 2017 it was 00:03:35.782-->00:03:40.954 house of rabbit we’re using a few Asian ctf’s also so this year I thought maybe I’d 00:03:40.954-->00:03:46.493 research a phantom thing that’s a little more interesting and you know it's applicable to many 00:03:46.493-->00:03:58.972 different scenarios which is why uh this is titled the house of Romans. So. So, like I said this 00:03:58.972-->00:04:02.609 technique is leakless, you just use a series of partial overwrites to achieve a complete 00:04:02.609-->00:04:09.082 RCE binaries are compiled with pie 2 like we don’t really know where the detect section is 00:04:09.082-->00:04:14.821 where we can ROP and the best part of the server is don’t need to send us any data back so even 00:04:14.821-->00:04:21.328 if iFCDF output is closed we can still get a shell and uh, I demonstrated this technique a 00:04:21.328-->00:04:27.000 while ago with a simple UAF, but it seemed to be a pretty severe bug so I thought maybe do it 00:04:27.000-->00:04:33.039 with a very simple bug like an off by one or something to demonstrate its versatility and 00:04:33.039-->00:04:38.111 also to use calloc in certain mallac because calloc memsets at 0, so it makes it a bit more 00:04:38.111-->00:04:49.923 tricky and tough to perform this but yet it’s doable. So as I said the bug is simple off by 00:04:49.923-->00:04:58.598 one and nothing else. So as you can see I just made a simple binary which has 3 functions, 00:04:58.598-->00:05:05.705 malloc, write and free, there’s no print function all it does basically take a size from 00:05:05.705-->00:05:16.049 malloc and then it mallocs it and uh you enter a bunch of a’s and it gets stored on the heap 00:05:16.049-->00:05:27.260 as you can see and it’s truly like a basic skeleton program. So, this is like a basic recap 00:05:27.260-->00:05:33.233 of the how the algorithm works, you free a chunk, it gets added to its appropriate free list, if 00:05:33.233-->00:05:39.339 it’s a fast one it gets added to that single list, if it is a size greater than 0x80 it either 00:05:39.339-->00:05:44.044 gets controlled or it gets added to [indiscernible] free list or a smaller enlargement which are 00:05:44.044-->00:05:51.351 double link this with the FD and BK. And uh in this program uh there is no UAF as you can see 00:05:51.351-->00:05:56.289 the pointer in the array is NULLed out there off by one in the right function so we can 00:05:56.289-->00:06:00.727 change the size of the chunk and we can overlap with the other neighboring chunks and thus can 00:06:00.727-->00:06:18.144 control FD and BK to perform like various sorts of heap attacks. So, this is your xd1 00:06:18.144-->00:06:24.384 size chunk, when your free [a udio cuts] you get some weird pointers here, 00:06:24.384-->00:06:30.023 these are actually the arena pointers, they point to this main arena which is libc symbol, 00:06:30.023-->00:06:35.328 to complete the double linked list or the unsorted list and uh that's it the main arena, the 00:06:35.328-->00:06:41.735 libc symbol much like the system execve malloc hook and all of these stuff so it’s interesting 00:06:41.735-->00:06:51.711 though that malloc is pretty close so but I’ll talk about that more later on. So, uh okay 00:06:51.711-->00:06:56.082 so this is sort of like a refresher to the audience for those of you don’t know what the 00:06:56.082-->00:07:07.193 unsorted bin attack does, is, it allows you to write um a particular libc address to 00:07:07.193-->00:07:13.199 anywhere you want. Now you can’t really control what you want to write but it’s uh the address 00:07:13.199-->00:07:21.508 and main arena. More importantly, it’s like a libc address so. And then there are 00:07:21.508-->00:07:26.479 fastbin chunks they are size less than 0x80, their head point is stored in the main arena and 00:07:26.479-->00:07:32.018 offset is determined by respective size. If you free two chunks of the same size then 00:07:32.018-->00:07:37.090 they are in the same free list. And it makes it easy, really easy to exploit them because 00:07:37.090-->00:07:43.296 it’s a singly linked list so less checks, and uh you should find a particular size alignment 00:07:43.296-->00:07:59.913 for fastbin free list which matches the size and then it’s like game over so. So, the next 00:07:59.913-->00:08:05.385 one you see the off by one in action this is sort of like the attack plan. So, I have these 00:08:05.385-->00:08:12.559 0x71, 21 and D1 sort of like these chunks, malloc in the heap. Nothing fishy here, just 00:08:12.559-->00:08:20.200 like I made a bunch of allocs and my plan is to use the second 0x71 to overflowing to, 00:08:20.200-->00:08:36.783 overflowing to the 0x21 size chunk and uh make it something like 0xe1 and then of course 00:08:36.783-->00:08:44.424 with a fake size header at plus 0xe1, so that we do, we don’t feel any malloc extortions and 00:08:44.424-->00:08:52.432 we bypass all of them. And uh, as you can see with this, we overlap 0x71, 21 and 0xD1 chunk 00:08:52.432-->00:09:11.017 with our e1 chunk. So we could free them and we could actually control 3 chunks. So this one 00:09:11.017-->00:09:19.259 looks like the 0xe1 chunk when we return it for a 0xd1 side it overlaps the 0x71 and 21 and uh 00:09:19.259-->00:09:26.132 there’s our fake size header of 0xe1, and the one thing that we’ll make sure is that at plus 00:09:26.132-->00:09:31.805 0xe1 there’s a good side like here you can see there’s a malloc 0x21, this is really 00:09:31.805-->00:09:37.243 important otherwise you will fail a malloc extortion, especially when you try to free 00:09:37.243-->00:09:51.191 it, so um. So now this is you, we have control of fdf of fastbin free list so now we need 00:09:51.191-->00:09:58.898 to think where we want to attack. So, the best way to attack is, we just seen is to 00:09:58.898-->00:10:05.271 find these loops, the addresses or stack addresses because they start with 0x7f and uh so we can 00:10:05.271-->00:10:11.511 use the 0x71 freelist and make it point to somewhere near this, uh how this works is basically 00:10:11.511-->00:10:19.219 we take a libc address and uh we shift it by 5 bytes, so uh, when you shift it, and it sort of 00:10:19.219-->00:10:26.226 becomes like 0x000007f, which is a valid size for a 0x71 freelist, so you actually fool 00:10:26.226-->00:10:33.333 malloc into believing that that is a valid fastbin freelist on the heap. And, and the 00:10:33.333-->00:10:42.208 consecutive 0x71 allegation would actually return you that chunk, so. Just in case you 00:10:42.208-->00:10:47.547 didn’t understand this part um, this is how it works. So you take a libc address and if you 00:10:47.547-->00:10:53.086 have a null keyword in front of it then you can just shift it and you can see in diagram it 00:10:53.086-->00:10:57.724 just keeps shifting, shifting, shifting until you reference 0xmf and the memory that you 00:10:57.724-->00:11:04.430 have to make it point too is something, something, something 9ad0, if your going something, 00:11:04.430-->00:11:15.608 something something 9ad5, so that’s where you actually make a point. I chose malloc hook 00:11:15.608-->00:11:20.180 because malloc hook is in a place which is surrounded by a bunch of libc addresses and a 00:11:20.180-->00:11:25.385 bunch of NULLs, if you’ve ever seen this. And uh, I think it’s possible free hook too, which I 00:11:25.385-->00:11:30.456 also discuss later uh with an undetermined parameter you can actually get to free hook and 00:11:30.456-->00:11:36.396 free hook gives you more control over RDI and the argument. But, nonetheless, malloc hook also 00:11:36.396-->00:11:44.537 works to get a shell, so. I mean we’ve seen this in a ton of CTF challenges and writes like just 00:11:44.537-->00:11:49.742 point the FD there and get it but we don’t know the libc address so we can’t really set 00:11:49.742-->00:11:55.815 it to somewhere near malloc hook. So we need to find an address which somewhere points 00:11:55.815-->00:12:02.021 close to it and then maybe we could do a partial overwrite to it. That’s like the basic idea 00:12:02.021-->00:12:12.799 is [audio cuts out] To do a series of 4 partial overwrites, some in the heap some in the 00:12:12.799-->00:12:20.139 libc in order to put a libc address, and in order to get an allocation in malloc hook and 00:12:20.139-->00:12:25.511 get your shell. I mean it’s all of our partial overwrites because we’re assuming that the 00:12:25.511-->00:12:44.230 program does not bring any data back. Just to show you like if you have like bacd in one run 00:12:44.230-->00:12:50.570 you found out to GBD and uh so basically in your script you quote backslash xcd and then 00:12:50.570-->00:12:57.443 backslash xa and that capital x would be what you boot you could set it to b, you could set it to 00:12:57.443-->00:13:04.717 1, 2,3, 4, anything you want, that's the thing we have to boot. So imagine we have this 00:13:04.717-->00:13:12.025 scenario like I said, we have 0xd1 getting overwrite by 0xd1 and uh so now I just free like 00:13:12.025-->00:13:23.236 two 0x71 chunk, and the FD points there, and um what you see here is where you fool 00:13:23.236-->00:13:34.180 malloc is, instead of making point here, we make it point to somewhere at the top like you 00:13:34.180-->00:13:40.420 know that 0xd1 chunk. So, what we basically do is you tell malloc hey after the next 0x71, 00:13:40.420-->00:13:47.126 think that the free list is not down there but up here and then we use the overlap thing to 00:13:47.126-->00:13:55.334 change the size of 0xd1 to 0x71 so now, we make malloc think that that’s a 0x71 chunk because 00:13:55.334-->00:14:03.376 of course we just changed the size, and once more we even made a thing that the 0x7ff7 address 00:14:03.376-->00:14:08.848 thing is actually an f3 pointer to the next 0x7 on the freelist. Of course, it’s just a pointer 00:14:08.848-->00:14:17.457 to the main arena, but we can do a partial overwrite of the lower 2 bytes of this address, and 00:14:17.457-->00:14:31.537 make it something like point close to or above malloc hook and uh of course the lower three 00:14:31.537-->00:14:36.476 numbers all will be the same so we just need to move the one byte 16 probability that I 00:14:36.476-->00:14:48.221 talked about before. This actually turned out to be a really big problem, because once 00:14:48.221-->00:14:55.294 I overlap in order to get control of that overlap I need to return it. If calloc is the 00:14:55.294-->00:15:00.500 only way to return it then you could just memset the entire region to 0. What that means is 00:15:00.500-->00:15:07.840 basic-I’m going to talk about the calloc bypass, actually I found this while looking at the 00:15:07.840-->00:15:18.818 source code of calloc during uh a ctf [audio cuts] called RC3 ctf um, it was a real good in 00:15:18.818-->00:15:29.595 ctf um so anyways um the bypass is that calloc uh if it returns uh sort of memory that from the 00:15:29.595-->00:15:35.935 mmap, then it think that it’s already zeroed out and it does not do the operation of nulling 00:15:35.935-->00:15:42.542 it out this is more of efficiency thing because you expect uh, a memory region that 00:15:42.542-->00:15:50.249 will return by mmap to be zeroed out, so this is the fact that we exploit is, we set the lower 00:15:50.249-->00:15:57.456 nibble uh off every chunk to 0xf so basically mmap it and we’ll make calloc believe that this 00:15:57.456-->00:16:05.331 chunk is not part of the heap, it’s part of an mmap segment so calloc will be like okay I don’t 00:16:05.331-->00:16:11.304 need to null it out then and we’ll still have the arena pointers there. So, for example 00:16:11.304-->00:16:21.814 we have 0x71 chunk, so it’s not 0x71 now it has to be 0x7f. This is cool for fast bins but when 00:16:21.814-->00:16:26.252 you come to unsorted bins, you have to you have to be sure if you have like 0x91, it should be 00:16:26.252-->00:16:33.860 0x9f, but you have to also make sure that you allocate exactly 0x9f because uh like you know, 00:16:33.860-->00:16:39.999 unsorted chunks have sort of this previous size thing in the next chunks header, so if you 00:16:39.999-->00:16:45.204 allocate a size less than that then you would have a size conflict but it would check the 00:16:45.204-->00:16:50.243 previous size. So if you allocate exactly the same size, then it will use up the entire 00:16:50.243-->00:16:59.452 unsorted bin and it won’t check the previous size so you bypass that check. Again, like this is 00:16:59.452-->00:17:04.156 just simple demonstration, a lot shorter than the exploit is, this is what it looks like, this 00:17:04.156-->00:17:08.327 is what it should look like 0x9f and then you have the arena pointers to a free chunk and a 00:17:08.327-->00:17:18.905 calloc and uh I get the same chunk back but as you can see, all the memory is not nulled 00:17:18.905-->00:17:23.409 out, it’s still at the arena pointer so I can partially overwrite that. Of course, you 00:17:23.409-->00:17:29.582 want a more detailed analysis, I posted it on my g-gist as part of the solution in that 00:17:29.582-->00:17:40.159 particular challenge in our RC3. So this is the first partial overwrite where we have uh, uh, 00:17:40.159-->00:17:46.432 0x9 on the free list and we want to make it point somewhere near malloc hook, so we overwrite the 00:17:46.432-->00:17:52.238 lower 2 bytes, with a boot for the 4 bits, and uh the next allocation for 0x71 return. The 00:17:52.238-->00:17:59.045 second partial overwrite is when you want to make malloc believe that the 0xd1 chunk that we are 00:17:59.045-->00:18:05.751 foraging is actually part of the 0x71 free list. And to do that you need to use an already made 00:18:05.751-->00:18:11.958 0x71 free list and make it point to the 0xd1 free list. And for that, you need to do a partial 00:18:11.958-->00:18:20.933 overwrite in the heap. So like for example, I have a pointer here to 1398190 and, but instead 00:18:20.933-->00:18:34.080 I need to make it point, 1398110, the one which I just overwrote with the pointer to 00:18:34.080-->00:18:42.021 malloc hook. So now, malloc thinks after the 13982d0 the next chunk in the freelist is 00:18:42.021-->00:18:48.961 1398 double 1-->0 which a) is a 0xd1 chunk which I changed the size and b)points to malloc 00:18:48.961-->00:18:58.771 hook. Of course, even after we reach malloc hook, we can’t ROP, we can’t do stat preview thing, 00:18:58.771-->00:19:02.675 we can’t build ROP chains in heap, we don’t really know the doc text and we can’t leak it 00:19:02.675-->00:19:16.956 so. So, here’s the third part while writing an libc address in malloc hook itself. So the third 00:19:16.956-->00:19:23.696 step in our attack vector is to do an unsorted bin attack on malloc hook. So, this is a free 00:19:23.696-->00:19:29.635 unsorted bin list, the FD and BK both point to main arena, because there's only one, and uh 00:19:29.635-->00:19:40.780 we overwrite it with uh actually we partially overwrite the lower 2 bytes again with um the 00:19:40.780-->00:19:47.586 address of malloc hook minus 0x10, so you don’t even need to boot this time because you 00:19:47.586-->00:19:57.396 already booted the address to um malloc. Now we’re down to the 4th and final partial overwrite 00:19:57.396-->00:20:05.738 right, which is we have a libc address in malloc hook, and we have a chunk close to it now all 00:20:05.738-->00:20:10.676 we need to do is partially overwrite the libc address that we just showed on malloc hook 00:20:10.676-->00:20:18.217 and make it so that it points to system or the magic gadget or any libc function that you want 00:20:18.217-->00:20:26.725 to call. So of course, this is what it looked like before, malloc hook was 0, after it look 00:20:26.725-->00:20:32.832 like the address of main arena. Of course if you try the malloc here then what it will do is it 00:20:32.832-->00:20:38.204 will think the main arena is an address and it will try to execute shell code as there 00:20:38.204-->00:20:43.943 which of course will make it crash, so, you’ve got to do the partial overwrite and then call 00:20:43.943-->00:20:50.149 malloc. The best way to trigger a shell is to basically use the magic gadget and then trigger a 00:20:50.149-->00:20:58.758 double 3, which sets up the and correctly fulfills the stack when strings required to uh call 00:20:58.758-->00:21:11.837 the magic gadget appropriately um. >>Was that as awkward for you guys as it was for me? 00:21:11.837-->00:21:15.908 [laughter] cause it was real awkward for me. Thank you all for staying, and how about that 00:21:15.908-->00:21:21.747 for the people that came in and were probably very confused uh let's give uh I guess powerpoint 00:21:21.747-->00:00:00.000 and 3 clicks for no apparent reason to advance a slide a big round of applause. [applause]