All right, so there's a bit of a story here that I don't know the full thing, but you'll notice that either the speaker is tiny or non-existent, which is true. Well, he does exist. He is just not here. Our speaker apparently had enough juice to convince us that we will now deliver this presentation in an automated way streamed via iCloud.com. That's right, you heard that right. We're going to try to stream an entire presentation from the Internet right here on stage. Yeah, so you guys know about autonomous things, so this is DEF CON's first autonomous presentation, and we also know what might happen if you get into an autonomous vehicle. That shit might just completely crash. So we have no earthly idea what's going to happen here. We hope it really... Yeah, we... Yeah. Right, we hope that it goes really well. However, there's one thing for sure. This is the first time we have used iCloud.com as a presentation medium, which we think qualifies iCloud.com as a first-time speaker and presenter, so therefore... We're all in this together. Here's to you, iCloud.com. And without further ado, my very small co-presenter, Sanad. Hello, everyone. This is Sanad. Sorry I couldn't make it to DEF CON. Apparently my visa got rejected, so I have to pre-record my entire talk. I hope I can convey the entire content of this presentation through it. If you have any questions, just feel free to reach me out to Twitter. My DMs are open. You can find me on the Facebook page at the link in the video description. Hi, everyone. This is Sanad. Sorry I couldn't make it to DEF CON. Apparently my visa got rejected, so I have to pre-record my entire talk. I hope I can convey the entire content of this presentation through it. intro head. If you have any questions, just feel free to reach me out to Twitter. My DMs are open. So, I'm a 19-year-old security engineer at GoRoot, based in Berlin, Germany, and on weekends I play CTFs in DCUA, a CTF team based in Ukraine. I normally solve pawn challenges and sometimes upload solutions to them in my GIS. So, now let's get started on the talk. So, regarding heap exploitation, there have been a ton of techniques being published, like from 2005, House of Spirit, House of Force, which, you know, allow you to get a chunk anywhere in memory. And the devs started patching most of them, but the resources still going on, and there are a ton of new exploitation techniques coming up, like 2016, House of Orange, it was a challenge in HitCon, 2017, there was House of Rabbit, it was used in a few Asian CTFs also. So, this year I thought maybe I'd research and find something that's a little more interesting and universally applicable to many different scenarios, which is why this is titled House of Romans. So, as I said, this technique is leakless, we just use a seeder of partial overwrites to achieve a complete RCE on binary that are compiled with pi2, like, we don't really know where the .txt section is, so we can't rob, and the best part is the server does not need to send us any data back. So, even if std alpha is closed, we could still get a shell, and I demonstrated this technique earlier with a simple UAF, but it seemed to be a pretty severe bug, so I thought maybe we should try it out. So, let's get started. We do it with a very simple bug, like an off-by-one or something, to demonstrate its versatility, and also to use calloc instead of malloc, because calloc memsets at zero, so it makes it a bit more tricky and tough to perform this, but yet, it's doable. So, as I said, the bug is simple, off-by-one, and nothing else. So, as you can see, I'll just show you how it works. This is made a simple binary, which has three functions, malloc, write, and free. There's no print function. All it does is basically take a site from malloc, and then it mallocs it, and you enter a bunch of a's, and it gets stored on the heap, as you can see, and it's really like a basic skeleton program. So, this is like a basic recap. Of how the algorithm works, if you free a chunk, it gets added to its appropriate free list. If it's a fast bin, it gets added to that single list. If it's a size greater than 0x80, it either gets consolidated or it gets added to an unsorted bin free list, or a small bin, large bin, which are double linked lists with FDBK. And in this program, there is no UOF. As you can see, the pointer and the array is nulled out. There's an off-by-one and a write function, so we can change the size of a chunk, and make it overlap with the other neighboring chunks, and thus gain control of FDBK to perform various sorts of heap attacks. So this is 0xD1 size chunk. When you free, you get some weird pointers here. These are actually the arena pointers. They point to the area. This is the main arena, which is the Lipsy symbol, to complete the double linked list of the unsorted bin list. And as I said, the main arena is the Lipsy symbol, much like the system, execv, malloc hook, and all of these stuff. So it's interesting to know that malloc is pretty close, but I'll talk about that more later on. Okay, and this is just sort of like a refresher to the audience, for those of you who don't know what the unsorted bin attack does. It allows you to write a particular Lipsy address to anywhere you want. Now, you can't really control what you want to write, but it's the address of the main arena. More importantly, it's like a Lipsy address, so. And then there are FASMN chunks, which are cyclized in 0x80. Their head pointers are stored in the main arena. And an offset is determined by their respective size. If you free two chunks of the same size, then they're in the same free list. And it makes it really easy to exploit them, because it's a single linked list, so less checks. And if you find a particular size alignment for FASMN free list, which matches the size, then it's like game over. So, to actually see the off-by-when in action, this is sort of like the attack plan. So, I have these 0x71, 0x21, and 0xD1, sort of like these chunks malloced in the heap. Nothing fishy here, just like I just made a bunch of allocs. And my plan is to use the second 0x21 to over-slow into the 0x21. So, I'm going to use the 0x21 size chunk, and make it something like 0xE1. And then, of course, put a fake size header at the plus 0xE1, so that we don't fail any malloc assertions, and we bypass all of them. And as you can see with this, we overlap 0x71, 0x21, and 0xD1 chunks with our 0xE1 chunk. So, we could free them. And we could actually control freed chunks. So this is what it looks like, this 0xE1 chunk, when we return it for a 0xE1 size, it overlaps the 0x71 and 0x21. And there is our fake size header of 0xA1. And one thing we have to make sure is that at plus 0xA1, there is a good size, like here you can see there is a malloc 0x21. This is really important, otherwise you will fail a malloc assertion, especially when you try to free it. So, now let's assume we have control of the FD of a Phasmin Felis. So now we need to think where we want to attack. So, the best way to attack is, which I've seen is, to find these libc addresses or stack addresses because they usually start with 0x7f. And so we can use this 0x71 free list and make it point to somewhere near this. How this works is basically we take a libc address and we shift it by 5 bytes. So when you shift it, and it sort of becomes like 0x00007f. Which is a valid size for a 0x71 free list. So you actually fool malloc into believing that that is a valid phasmin free list on the heap. And the consecutive 0x71 allegation which actually return you that chunk. So just in case you didn't understand this part, this is how it works. So you take a libc address and if you have a null keyword in front of it, then you can just shift it. You can see in the diagram, it just keeps shifting, shifting, shifting until you reference 0x7f. And the memory that you have to make it point to is something something something 9ad0. It becomes something something something 9ad5. So that's where you actually make it point. I chose malloc hook because malloc hook is in a place which is surrounded by a bunch of libc addresses and a bunch of nulls. If you've ever seen this. And I think it's possible with freehook too, which I'll also discuss later. With an unsorted bin primitive, you can actually get to freehook and freehook gives you more control over rdi and your arguments. But nonetheless, malloc hook also works to get a shell, so. I mean we've seen this in a ton of CTF challenges and write-ups, like just point the fd there and get it. But we don't know the libc address. So we can't really set it to 0x7f. So we need to find an address which somewhere points close to it and then maybe we could do a partial overwrite to it. That's like the basic idea is. To do a series of four partial overwrites, some in the heap, some in the libc, in order to put a libc address and in order to get an allocation near malloc hook and get your shell. I mean it's all about partial overwrites because we're assuming that the program does not print any data back. Just to show you, like if you have like BACD, in one run you found out to gdb. And so basically in your script, you'll call template backslash xcd. And that's it. Then backslash xa and that capital X would be what you boot. You can set it to B, you could set it to 1, 2, 3, 4, anything you want. That's the thing you have to boot. So imagine we have this scenario, like I said, we have 0xd1 getting overlapped by 0xd1. And so now I just field like two 0x71 chunks. And the fd points there. And. But you see here's where we fool malloc is, instead of making a point here, we make it to somewhere at the top, like near that 0xd1 chunk. So what we basically do is we tell malloc, hey, after the next 0x71, think that the field is not down there, but it's rather up here. And then we use the overlap thing to change the size of 0xd1 to 0x71. Now we made malloc think that that's the 0x71 chunk, because of course we just changed size. And what's more, we even made it think that the 0x7fff7 address thing is actually an fd pointer to the next 0x7 on the field list. Of course, it's just a pointer to the main arena. But we can do a partial override of the lower two bytes of this address and make it something like. So. Point close to or above malloc hook and of course the lower 3 nibbles will all be the same so we just need to root the 1 by 16 probability that I talked about before. This actually turned out to be a really big problem because once I overlap in order to get control of that overlap I need to return it. If calloc is the only way to return it then it will just memset the entire region to 0. What that means is basically I'm going to talk about the calloc bypass. Actually I found this while looking at the source code of my code. So calloc during a CTF event called rc3ctf it was a real gradation CTF. So anyways the bypass is that calloc if it returns a sort of memory that's from mmap then it thinks that it's already zeroed out and it does not do the operation of nulling it out. This is more of a... efficiency thing because you expect a memory region that is returned by mmap to be zeroed out. So this is the fact that we exploit is we set the lower nibble of every chunk to 0xf which is basically mmap bit and we'll make calloc believe that this chunk is not part of the heap it's part of an mmap segment. So calloc will be like okay I don't need to null it out then and we'll still have the arena pointers there. So for example you have 0x71 chunk so it's not 0x71 now it has to be 0x7f. This is cool for fast bins but when you come to unsorted bins you have to make sure like if you have like 0x91 it should be 0x9f. But you have to also make sure that you allocate exactly 0x9f. Because like you know unsorted chunks have sort of this previous size thing. In the next chunks header. So if you allocate a size less than that then you would have a size conflict because it will check the previous size. Rather if you allocate exactly the same size then it will use up the entire unsorted bin and it won't check the previous size. So you'll bypass that check. Again like this is just a simple demonstration while I was writing the exploit is this is what it looks like. This is what it should look like 0x9f. And then you have the arena pointers. This is a free chunk. And a calloc. And I get the same chunk back. But as you can see all the memory is not nulled out. It still has the arena pointers. So I can partially overwrite that. Of course if you want a more detailed analysis I have posted it on my gist. As part of the solution for that particular challenge in RC3 CTM. So this is the first one. This is the first partial overwrite. Where we have a 0x71 free list and we want to make it point somewhere near malloc hook. So we overwrite the lower two bytes with a root for the 4 bits. And the next allocation for 0x71 returns. The second partial overwrite is when you want to make malloc believe that the 0xd1 chunk that we are forging is actually part of the 0x71 free list. And to do that you need to use a... ...already made 0x71 free list and make it pointer to 0xd1 free list. And for that you need to do a partial overwrite in the heap. So like for example I have a pointer here to 1398190. And... But instead I need to make it point... ...1398110. The one which I just overwrote with the pointer to malloc hook. So now malloc thinks after the 13982d0 the next chunk in the free list is 1398110. Which A is a 0xd1 chunk which I change the size and B points to malloc hook. Of course even after we reach malloc hook we can't ROP. We can't do stack pivoting. We can't build ROP change on heap. We don't really know the dot text and we can't leak it. So... So here's the third part of our writing an ellipse address in malloc hook itself. So the third step in our tag vector is to do an unsorted bin attack on malloc hook. So this is a freed unsorted bin list. The fdnbk both point to main arena because there's only one. And... We overwrite it with... Actually we partially overwrite the lower two bytes again. With... The address of malloc hook. Minus 0x10. So you don't even need to brute this time. Because if you already bruited the address to... ...malloc hook. Now we're down to the fourth and final partial overwrite. ...overwrite. Which is... We have an ellipse address in malloc hook. And we have a chunk close to it. So now all we need to do is partially overwrite the ellipse address that we just wrote on malloc hook. And make it so that it points to system or the magic gadget or any ellipse function that you want to call. So of course this is what it looked like before. Malloc hook is 0. After it looked like the address of main arena. Of course if you try to malloc here. Then what it'll do is... It'll think the main arena is an address. And it'll try to execute shell codas there. Which of course it'll make it crash. So you gotta do the partial overwrite. And then call malloc. The best way to trigger a shell is basically to use the magic gadget. And then trigger a double free. Which sets up the... And correctly fulfills the stack constraints required to... ...call the magic gadget appropriately. So the first step is to call malloc. And then if you heard the clicking sound. And then the sleep sound. And then the sound is answered. So it's a real time management thing. It's not a good time management trick. But what does it do? So it works. It's a real time management trick. It works. Thank you.