Thanks, everybody, for coming, despite the late hour. This is Owned Over Amateur Radio, and we're going to be talking about remote kernel exploitation. First, a little bit about me. My name's Dan. I am a security consultant at VSR in Boston, mostly doing app and network pen testing, code review, that sort of thing. I've published a few bugs. Mostly, I focus lately on the Linux kernel, and especially on Linux kernel exploitation and mitigation. So what are we going to talk about today? First, I was going to provide some motivation on why I wanted to give this talk and what I hope you guys will get out of it. And next, we'll just dive right into some of the technical details of what are some of the challenges associated with developing fully working remote kernel exploits. Next, we'll take a look at what some of the past work has been in remote kernel exploitation and try to draw some trends there and look at areas that haven't been covered as much. And then we'll get into the body of the talk, which is sort of a case study of an exploit that I wrote for a remote stack overflow in the Linux kernel's implementation of the ROSE amateur radio protocol. And this part of the talk will be in sort of two phases. In the first phase, I'll sort of explain exploitation of the vulnerability. And the second phase, I'll explain the details of the kernel backdoor that I installed during the exploitation phase. And then finally, we'll wrap up taking a look at some future work. I think remote kernel exploits sort of speak for themselves as to why they're useful. It's sort of this keys to the kingdom concept where you have instant remote root access to a machine that you previously had no interaction with. But especially when you compare them with the challenges that are facing you when you're trying to exploit client-side systems like browsers, for example. You know, if you have a browser exploit, you're frequently facing exploit mitigation technologies like ASLR and NX or DEP. And sometimes those require the existence of a second vulnerability to bypass. And at that point, you may be running inside a browser sandbox, for example, as IE9 and Chrome and most recently Safari now provide. So if you can leverage a third vulnerability to escape that sandbox, you may need to escalate privileges using a fourth vulnerability because you're running as an unprivileged user. And that seems like a lot of pain, so I prefer to skip all that. What I hope you guys will get out of this talk, this is not actually an amateur radio talk despite the really misleading title. We're going to talk about enough of amateur radio to understand the vulnerability that I'm using. But really, this is sort of a vehicle for me to talk about exploit development methodology. So hopefully, if you guys don't have any experience writing exploits, you'll hopefully get a sense of what are some of the building blocks that we can use to exploit these kinds of vulnerabilities? What are some of the steps that we need to go through? And I'll also be showing off a bunch of sort of exploitation techniques, which I think is far more interesting and useful than doing a study of an individual bug, because bugs come and go. They get fixed all the time. But exploit techniques and learning how to mitigate them are really defined lots of, you know, lots of bugs. And sort of the whole point of this is to take a look at this exploit and identify what the weak links in the chain are, what are the parts of the exploit that aren't really done so well, so that we can identify what are the easiest ways to protect against these kinds of exploits. So despite their advantages, remote kernel exploits are not trivial, right? And I've sort of identified three key points as to why there are some extra challenges associated with these kinds of exploits. The first of these is that the environment that you're working in is incredibly fragile. If you're dealing with a remote userland exploit, for example, like a web server or an FTP server or something like that, frequently if you fail, if you're dealing with some sort of memory corruption vulnerability and you miscalculate some offsets or your exploit fails for some other reason, frequently you'll crash the application or service. In some cases, it needs to be restarted manually. In others, it'll be restarted automatically. If you're dealing with a service that actually forks child processes for each connection, you may not even crash the service itself. You'll just crash the child process, and you essentially have very minimal consequences for failure and can continue to try to exploit it. In contrast, if you fail at a remote kernel exploit, in nearly every case, the kernel's gonna panic and the box will fall over. And not only will you have lost your chance to exploit that machine. You've lost any chance of doing so with any sense of stealth. It's a little bit noisy to crash an entire machine. The second main obstacle, in my opinion, for these kinds of exploits is that you have very little control over the environment that you're trying to exploit. And this is sort of a shared property of all remote exploits. If you're dealing with a local kernel exploit where you have an unprivileged account on the machine, you have a lot of sort of exploit primitives, sort of building blocks that you can use to make writing exploits easier. For example, it's very easy to trigger the allocation of data structures on the kernel heap. You can open files or sockets or create shared memory segments. And all of these resources have structures on the kernel heap. And you can sort of trigger these allocations in such a way as to massage the heap into a state that's conducive to exploitation. Likewise, you can trigger the calling of function pointers in the kernel by performing operations on these structures that you've allocated. And finally, there's this huge silver platter of information the kernel gives you, if you're a local user, through interfaces like on Linux, the proc file system, that will really help you find out where things are in kernel memory and help target your attacks. If you're dealing with a remote kernel exploit, you don't actually have any of those capabilities right away. But we may see how you can sort of build them up from other exploitation primitives you might have. And the final challenge, in my opinion, with writing these exploits is they frequently occur in what's known as interrupt context. So, you know, 10-second operating system 101, when a process makes a system call, it is executing kernel code in what's known as process context, where the threat of execution has a userland application, a process, associated with it. But in contrast, when you're dealing with asynchronous events, like receiving networking data, the kernel's running in what's known as interrupt context. It doesn't have a process associated with that threat of execution. And this is a very sort of delicate and difficult context to exploit, because the end goal here is usually to execute code in userland, because, you know, a shell is a really nice, convenient way to interact with the system, and that's sort of what everybody wants. So the challenge is, how do we get from this sort of hostile interrupt context environment to executing code in userland? And the answer is, we need to find a way to transition from interrupt context to kernel mode process context, in which we're still running kernel code. But we now have a process associated with us, and then finally from there to actually executing code in userland. So before I get into what I did, I thought it would be prudent to talk about what's been done before. So I studied, I looked up and researched every remote kernel exploit I could find anywhere, and I identified 18 exploits that have been talked about or published for 16 unique vulnerabilities. These were written by 19 people. Nine of them have full public source code. Many of them metasploit modules. Three of them have sort of partial or proof of concept source code, and the rest were sort of discussed in detail at conferences without code. And these exploits cover a wide range of platforms. Solaris and OSX have not had any weaponized remote kernel exploits. That's sort of future work, if you're into that sort of thing. Breaking these down by what operating systems they affect, half of the 16 vulnerabilities were in Windows. Only four of those were actually in the Windows core components. Three of them were in various wireless drivers, and one was in a semantic firewall. Three in NetWare, which I don't think anyone still uses NetWare. Three in various BSDs, and two in Linux, one of which was in a third-party driver. And breaking these down by vulnerability class as to what vulnerability was exploited, a full three-quarters of these, 12 of the 16, were typical stack overflows. And I think this is probably because these vulnerability vulnerabilities are incredibly conducive to exploitation. They're very well understood. The steps of exploiting them are known. Three of these vulnerabilities were heap overflows, which are frequently much more difficult. And then one of these Windows SMB issue was actually an array indexing issue. I'm not sure there's actually enough data to draw meaningful conclusions about trends here, but 2007 was a busy year for kernel exploitation. So just a brief walkthrough of some of what are, in my opinion, the highlights of past work on remote kernel exploits. Barnaby Jack did the first work that I'm aware of. He started in 2004, 2005. And he exploited a remote stack overflow in a semantic firewall application. And he demonstrated a lot of really cool things, especially detailed shellcode examples for how to transition from running in the kernel to actually executing user-land code. And he also demonstrated a kernel backdoor that in many ways was sort of an inspiration for the one I ended up implementing. Next in 2006, Sinan Aren from Immunity, they announced they had Greenapple, which was a remote kernel exploit for Windows. And that's sort of the first commercially available remote kernel exploit that I'm aware of. Also, in 2006, H.D. Moore, Matt Miller, and Johnny Cash published three remote kernel exploits that were allied to Metasploit in various WiFi drivers. 2007, there was an open BSD IPv6 one. vulnerability, which was really awesome. That was the first public remote kernel heap overflow that I'm aware of. They did some nice work on that. And they also bypassed userland NX protection, as in non-executable stack heap, etc. Final highlights. Immunity has a canvas exploit for MS08-2001, which is a Windows IGMPv3 vulnerability. And that was a really difficult to exploit Windows kernel pool overflow that people were questioning whether it was exploitable before they announced they had a reliable exploit. And then finally, in 2009, Scrakiu published an exploit for a Linux kernel vulnerability in the SCTP protocol, which is a remote Linux heap overflow. And I think one of the great contributions from this exploit is he introduced a really neat trick that leverages a 64-bit specific page mapping to easily translate a Linux kernel vulnerability into a Linux kernel vulnerability. And that's a transition from interrupt context to executing code directly in userland. Just sort of drawing some trends from all this work I did reading up on what's been done before. You know, three quarters of these issues were stack overflows, but none of these actually had to contend with having non-executable stacks in the kernel itself. Because the feature wasn't introduced at that point. So my exploit will try to address that. Next, none of the issues, neither of the two issues on Linux actually have a stack overflow. So I'm going to go ahead and talk a little bit about the stack overflows. And I think one of the things that's really interesting about the stack overflows is that they actually were stack overflows in interrupt context, which is sort of a particularly difficult context to exploit things in, because you need to do a bunch of cleanup. Scrakiu and Twiz published an article in Frac 64 called Notes on Kernel Exploitation, or something of that effect. And they sort of described the steps you need to go through to do this, but it was for a vulnerability introduced for the sake of demonstration. And I wanted to provide a real-life example. And the last observation is that six of the 18 apps that were used in the stack overflows were for issues in 802.11 code. So I think wireless drivers could probably use some improvement. And now we've gotten to the good stuff. Before we start talking about how to write this exploit or what the bug is, the target system that I'm going after, I'm using a 32-bit x86 physical address extension kernel. And the PAE component is significant because it means the kernel now has NX support. As in, the kernel stacks, and kernel stack, and kernel stack, and kernel stack, and kernel stack, and the kernel heap, kernel data are all now non-executable. So we can't just execute code on the kernel stack immediately without doing something first. They also, you know, these kernels have userland NX protection as well. So if we're going to somehow introduce code into a user process, we also need to do so in a way that honors page permissions. We can't execute code on a userland stack without modifying its permissions. And then finally, because we're on a 32-bit machine, we can't leverage Scrakiu's trick that was specific to AMD. So I'm going to use a T-64 to make that transition from interrupt context to userland. For actually doing testing, I just had two VMs. I chose Ubuntu 10.04. The attacker is just a desktop machine. The victim is Ubuntu server because that's a PAE build. For debugging, I just use KGDB. And for the actual networking, I was not sitting in my apartment doing, like, with radio equipment driving my girlfriend crazy. I was using BPQ, which is an implementation of AX25 amateur radio over Ethernet. So my VMs can talk to each other without me needing to buy radio hardware or get a license. And just because of the nature of the exploit, it's written entirely in x86 assembly, except for the code that sort of ties it all together and sends it over the wire. So this is the advisory that Debian put out for the vulnerability that I'm going to be exploiting. Dan Rosenberg, first, they spelled my name wrong. They spelled it right in actually five other issues in the same advisory and got it wrong in this one, which is some sort of a mistake. So I'm going to go ahead and assign. Reported two issues in the Linux implementation of the amateur radio x25 PLP protocol. A remote user can cause a denial of service by providing specially crafted facilities fields. This is a pretty interesting denial of service. So just a brief overview of the protocol we're going to be exploiting. Just enough detail to talk about so you understand the vulnerability that I'll be taking advantage of. ROSE is a fairly rarely used amateur radio protocol. And it's a network layer that sits on top of x25, which is sort of a more commonly used packet radio protocol. So in addition to 7 byte x25 addresses, ROSE nodes have 10 digit numeric addresses to identify them. And it supports only static routing and it does so using a Digipeter mechanism where a host can essentially say, all right, I'll accept packets from this x25 call sign and forward them along to this one. So when two hosts issue a connection to each other, they're going to have to do a call sign and forward to each other using ROSE. They exchange what are known as facilities. And this is sort of just a list of supported features for that connection. And one of these facilities, FAC National Digis, allows one host to give the other host a list of Digipeters just so it can figure out its routing stuff. And in the Linux kernel implementation, I noticed that when they went to parse this particular field in the ROSE frame, they read this length value directly from the frame and copied all this Digipeter data without any bounds checking into a statically sized stack. And this is the sad code. You can see at the top line, they're just reading that length value right out of the frame data, using it as an upper bound for this loop that then copies data in, in x25 address size of chunks, which are 7 bytes, into either the destination or source array. And those arrays are living on the stack. And you may also notice that there's a constraint here. The 7th byte of every x25 address is actually anded with a flag that indicates whether it's a source or destination Digipeter. And that means in our payload, we need to obey that constraint. Every 7th byte needs to be consistently greater or less than 0x80. Otherwise, you know, one 7-byte chunk might be copied into one array, and then the next one might be copied into a different array, and it would be very difficult to handle, and it would be a pain. So this sort of just required me to go through the exploit and manually just check that this constraint was satisfied. So now we've got a plan of attack. Um, we've got a vulnerability, and we, we want to actually, you know, exploit it. And the first step is to actually gain control of the instruction pointer, so we can control what the kernel's gonna do. Um, and from that point, we need to actually start executing code. Uh, next, I decided that my exploit would install a remotely triggerable kernel backdoor, because that sounded like fun. And finally, we will have to do some cleanup to, to make everything keep running. So first, triggering the bug was actually really the easy part. I took 10 minutes after I found it. Um, you just, what I did was essentially cannibalize the, the ROSE kernel module that already existed, so that it, whenever I made a connection to someone, it would send out my evil ROSE frame instead of a normal one. Um, and this evil ROSE frame just had this particular facility field that was vulnerable, followed by a too-big length value, and then a bunch of no-op instructions just for the sake of testing to see if I could actually trigger it. Uh, this is what the frame looks like. You have your header, total facility's length, which didn't actually matter, um, a null byte, fact-national is the specific type of facility, fact-national-digis is the vulnerable facility itself, and then this length value, which we're setting to OXFF, which is 255, which will, is more than enough to cause the overflow, and then a bunch of 90s. And so you recompile your ROSE module, and then you make a connection to your vulnerable host, and it causes a stack overflow on the soft IRQ stack, which is an interrupt handler that's receiving this data. Um, you can see on the, in the debugger, you know, our host has, we've overwritten, we've overwritten, we've overwritten, we've overwritten the save-return address on the stack, and then when that vulnerable function returned, it returned to all 90s, which is the, the value that we put there. So now we control the instruction pointer. Um, as most people who've written serious exploits know, um, getting control of the instruction pointer and actually executing code can be separated by a lot of work. Um, traditionally, if it were like 1995, we would just sort of return into shellcode that was stored in our buffer that was copied onto a stack, and then we'd be running code, and that's all you need to do. Uh, the first problem is we don't actually know where the soft IRQ stack lives in memory, because it's allocated at runtime, and it's gonna be different on each boot. Um, that's pretty easily solvable. You just sort of return to this trampoline function that will do like a jump ESP and jump into the stack wherever it is. Um, the second problem, though, is that since we're running a PAE kernel, the soft IRQ stack on which we caused our overflow is non-executable memory. So if we try to return there, the kernel will just crash. And this means we need to employ return-oriented programming. Um, so the basic idea of return-oriented programming is, because we've caused our stack overflow, we now control the return address, where this function's going to go after it return- is finished doing whatever it's doing, and we control a bunch of data on the stack past that saved return address. And we also notice that every return instruction will direct execution to the address that's on the stack, and then increment the stack pointer to the address that's on the stack, and then increment the stack pointer to the next place. So using this, we can actually just chain together little pieces of code at known locations in the kernel, uh, to do essentially arbitrary computation. Um, I say at known locations because this actually does rely on the fact that, uh, you know where stuff is in memory. And this is a-a fairly reasonable assumption in the kernel world. Uh, if you're running a distribution kernel, like Ubuntu or Debian, Fedora, um, they're shipping binary kernels, which means everyone with the latest version of Ubuntu has the same- has the same kernel image in memory, in terms of the code. So it-it's fairly reasonable to assume that if you know something about your target, then you-you essentially know where-where in memory certain instructions live. And there-there's no sort of randomization of the kernel at all. Um, to-to make this a little bit better, you can actually choose these gadgets, these little pieces of code that you're executing in such a way that they're more likely to appear across multiple kernel builds. So now, actually, looking at some code, um, what our route payload actually-what we wanted to do is to actually make the stack that we overflowed executable so that we can run code on it. And the kernel has a really nice convenient function that'll take care of all the hard work for us, called setMemoryX. And setMemoryX just takes two arguments. The first one is an address that you want to mark executable, and the second one is the number of pages from that point that you want to mark. Um, so we're in the kernel, so the calling convention to functions actually has the first three arguments to the functions in registers. So we just need to keep that in mind. Um, so what the ROPS dub does is, first, we actually want to load the stack pointer, which points into the stack that we want to mark executable, into the EAX register, because that's the first argument register to this function. We'll actually, um, align it to a page boundary, because setMemoryX will-will cry a little bit if-if you don't, um, have a lot of time to do that. So, um, the thing is page-aligned. Um, then I just chose arbitrarily, let's mark four pages from that point, um, into our second argument register, which is EDX. Return into setMemoryX, which is just gonna mark the SoftRQ stack executable, and from that point we can just return into a jump ESP instruc-instruction, at which point execution will jump into the SoftRQ stack and start running our code. So at this point, we are running shell code on the SoftRQ stack in kernel mode. Great. Um, but we've got a problem. Um, the amount of data that we could have copied into our overflowed region is limited to 255 bytes, just by nature of the particular-particular bug that we're exploiting. And we've already used a whole bunch of that to get up to the-the saveReturn instruction. Um, we've used more for our route payload. So we're-we're running very low on space, and kernel payloads can be quite big, so we really need to overcome that space constraint. Um, but it's useful to keep that in mind. So, um, we've got a problem. Um, we've got a problem. Um, but it's useful to keep in mind that that's just the part of the packet that copied into the overflow region. We have this rows frame that we sent that can be thousands of bytes big, living somewhere on the kernel heap. Um, and I found experimentally that, um, one of the parent functions to the function that we caused an overflow in actually has a pointer to this-to this rows frame, the entire rows packet that we sent over. So what we can do is, now that we're executing code, all we-all we need to do is walk up the stack, say, is this a kernel pointer? Yes, no, just, uh, heuristically. And if it is, you can follow it and look around in memory, and we'll put a tag in our rows frame so that we can find it. Um, and then, you know, we'll-we'll eventually just find our rows frame on the kernel heap, we'll mark it executable just by calling setMemoryX and then jump into it. So now we've overcome our space constraint. Uh, we can execute arbitrary length payloads running in kernel mode right now. Um, and the goal here is I want to install a kernel backdoor in the ICMP handler, because, uh, I don't know if you've ever done this. It's-it's badass. Um, so just some-some data structures, uh, to sort of explain what I had to do here. It's not very complicated. There's this sort of global array at a known location, you know, because it's-it's in kernel data, um, called the inet protos array. And this just is an array of pointers to these net protocol structures. There's one for sort of each IP protocol, which is redundant. Um, like, IP proto-the-the-there's a TCP one, there's UDP, ICMP. And the first member of this net protocol structure is a handler function pointer that just-that's what gets called when the kernel receives data for that protocol. So if we can overwrite that function pointer for the ICMP protocol, then whenever we receive ICMP data, it will call into our code, and we can-we can move on from there. Um, so hooking ICMP is pretty straightforward. Um, I decided to use the SoftRQ stack itself as a place to sort of put stuff, just for the explanation. So, um, it's a good place to put code, um, that we're gonna need later. It's, um, it's persistent. It's not gonna go away while the kernel's running. And it's a very safe place, because on Linux, kernel stacks are 8 kilobytes big by default, but the bottom 4 kilobytes is guaranteed to never be written into. Otherwise, the optional 4 kilobyte stack version would never work. Um, so anything in the bottom 4 kilobytes of the SoftRQ stack, outside of the metadata at the very bottom of it, is a great place to store data. So what we do is we copy our hook, and we'll explain what that does in the second part when I describe the backdoor, but for now it's just some code that we want to run into the SoftRQ stack just for safekeeping. Um, and then we want to actually install a hook so that execution will be redirected to it. Um, first we notice that the handler that I want to overwrite is in read-only memory, which is sort of a pain. Um, x86 has a nice little way out for this. Um, control register 0 has a-a bit called the write protect bit, and if you flip this bit, then you can essentially write into read-only memory with no problem. Um, which comes in handy. Um, so what we do is we flip this bit, now we can write into read-only memory, we write the address of this hook that we just copied in into the ICMP handler function pointer, and then it looks something like this. Wow, those are really narrow arrows, I guess. Um, so whenever some, you know, whenever our kernel receives ICMP data, it will call that handler function pointer, and that will, execution will go to this hook that we installed, it'll run whatever code we choose to run there, and then presumably when we're done we're going to call the original ICMP function so that ping still works and stuff like that. Finally, the last phase of this exploitation component, um, is to actually make sure that the kernel keeps running, because all this work is-is sort of for nothing if-if you do it and then the kernel just collapses. Um, so the biggest problem here is that, um, the kernel is not running because we've wiped out a pretty big portion of the stack by causing this overflow, um, and if we don't do any cleanup then the kernel is just going to crash. Uh, the first bit of cleanup I needed to do has to do with-with some locks. So at the time of the exploit, um, the rose protocol was holding two spin locks just for mutual exclusion purposes and if we just sort of carried it on as if nothing happened then the rose stack would actually deadlock and-and the kernel would lock up. Um, The problem here is these spin locks live inside the rows kernel module, which is loaded at runtime, so we don't actually know where it is. Fortunately, it's actually pretty easy to find it. There's this global modules variable, which is the head of a linked list of loaded kernel modules. So what we can do is we just follow this linked list until we find the module named rows, and read this sort of module structure that is hanging off this linked list until we find where the data section of the rows module is. Now that we know where the data section of this module is, we can sort of scan it for this byte pattern that represents a distinctive signature of what these spin locks look like in memory, and that's unique, so that will always work. And then once we find them, we can release them by, I think it's incrementing them. The second bit of cleanup I needed to do has to do with the preemption count. So on Linux, every process has this variable called preempt count, which sort of encapsulates a bunch of information related to scheduling. And if the kernel calls an interrupt handler, as it did when our malicious rows frame was received, and returns from that interrupt handler, and the preemption count isn't what it expects it to be, in the past, the scheduler would panic and everything would fall over. More recently, they put in a check where if it's not what it expects it to be, it actually just complains a little bit and fixes it for you, which is really nice for exploitation and saved me a lot of trouble when I was first coming to Linux. Getting this up and running. But for the sake of completeness, we'll avoid that warning, because we don't want anything to be logged. It's really easy to find the preemption count. It just lives in this thread info structure, which is at the base of whatever process it is, kernel stack. So all we need to do is we know where the software queue stack is, we just find the base of it, and then this variable is at a known offset from that base, and we just decrement it appropriately, and that's all we need to do. Finally, we need to do this actual cleanup so the kernel will keep running. So recalling that stacks are sort of divided into frames, which represent function contexts essentially, our overflow has wiped out some number of frames, including all the metadata that's needed to keep things running. So what we need to do is we actually walk up the kernel stack until we match the signature that I prepare ahead of time that represents this is a frame boundary. This is a good place for us to put the stack pointer, and everything will keep running as normal once we return from there. It's sort of as if we magically teleported up to a few parent functions. So let's go over what we've done so far. That's the end of the exploitation phase. At that point, if you send your exploit over, it installs its hook, and the kernel keeps running, and it would never notice anything happening. So what we've done so far. We trigger our overflow. Gain control of the instruction pointer. And then we leverage return-oriented programming to mark the softRQ stack where the overflow took place executable, and then jump into a shell code stub on the softRQ stack. Next because we have this space limitation, we find the entire rows frame on the kernel heap, mark it executable, and jump into it. Next we install our kernel backdoor by hooking the ICMP handler, and then we do some cleanup that we need to do to keep the kernel running. Next we're going to talk about the backdoor. So now at this point, whenever an ICMP packet is received, our hook that we put in kernel memory is going to run. Now we want to talk about what that hook's going to actually do. So the first thing it does is it checks for this sort of arbitrary magic tag that I just made up in the ICMP header. And I decided that I wanted to have two distinct kinds of packets to handle. I wanted to have an install packet, and what I want that to do is sort of cause it to keep shell code that I put in the ICMP packet around for life. So we're going to do that a few weeks later. Just sort of let it sit in kernel memory somewhere. And then I wanted to have a trigger packet that causes that shell code to execute somehow. And this is user land shell code that I want to be dealing with. And these packets should be able to be sent independently. So you can sort of install a payload and come back a week later and cause it to trigger repeatedly. So we need to develop a strategy for how we're going to actually make this happen. The problem here is the ICMP handler that we've hooked is already running. It's also running in interrupt context. And we want to get to user land. We want to execute code in user land. So the first phase, we need to actually transition from interrupt context to kernel mode process context where we're still running kernel code, but it now has a process associated with our execution. In the second phase, we want to actually hijack that process that's now associated with our execution and cause it to execute our user land payload. So for the first phase. We check the magic tag when we receive an ICMP packet and if it's an install packet, then we'll just copy the payload that's in the packet into a safe place and we're going to keep using the software queue stack as a good place to store stuff. If we get a trigger packet, then we need to make that transition to process context. And in my opinion, the easiest way to do so is to hook a system call because whenever a process calls a system call, it will then be executing kernel code in process context, which is where we want to be. How to do this is, as it's been done before, I find the system call table at runtime by issuing an SIDT instruction, which is an x86 instruction that finds the base address of the interrupt descriptor table. And I just index into this table and pull out the handler for int 80, which is system call. So that's sort of the entry code for what happens when you call system call, when you make a system call. I scan this function for a byte pattern indicating it's making a call. It's making a call into another table. And this other table is the system call table. So you do that. Now I know where the system call table is. And the system call table is just sort of like a table of essentially function pointers that represent handlers for what happens when you make a system call, like close or open or et cetera. The system call table on Linux is read only, but we've already figured out how to get around that. We just flip that magic bit in CR0. And that's it. We can write to read only memory. So what we do is we just keep that system call handler, whatever one we want to hook for later, because we're going to need it, and we'll write the address of our hook code into the system call table. So now whenever someone calls that system call, it'll execute our code. Because we want the ICMP stack to keep working, then we just actually call the original ICMP handler that we kept around so the machine still pings. So now we want to move on to phase two. So what we've done is we've copied our userland payload. We want to execute eventually into kernel memory. And some process comes along, and it's going to call the system call that we chose to hook. And what we need to do now is hijack that process to execute our userland code. First of all, we're only really interested in processes that are running with root privileges. It would sort of suck if we went through all this trouble and got a connect back shell as a nobody user. So the challenge is, how do we actually check that this random process that we hijacked, is a root-owned process? And you could do this by actually following various pointers in metadata associated with that process. But each of these structures sort of is unstable. They change a lot between different kernel versions. You'd have to do it heuristically, and it's sort of a pain. It'd be really nice if we could just call getUID, because that's something we know and understand. It's a very simple interface that will immediately tell us what our user ID is. But the question is, is it possible to call system calls from kernel mode? I mean, logically, it doesn't really seem to make sense, because a system call is designed to be an interface that user applications can call to request services from the kernel. So what happens when you make a system call via int80 from kernel mode? I'm just curious, how many of you, if you had to guess, would think that you could do this without a problem? That's pretty good. How many of you think that some issues may arise if you try to make an int80 from kernel mode? How many of you have no clue? I don't know what I'm talking about. Thanks for being honest. It turns out that most system calls will work perfectly fine when you call them from the kernel. And the details of this, you just need to know some x86 trivia. But basically, the problem that I thought might arise has to do with the fact that when you transition from userland to kernel mode, you switch stacks to a kernel mode stack. And I thought that that might screw things up. But this stack switch only happens when you're changing privilege levels. So if you make a system call from kernel mode, you just skip it. You skip the stack switch and use whatever stack is still there. And this is just like any other interrupt that is intra-privilege level. As in, there are lots of interrupts that can be called from ring zero. And there's really no problems here. There are a few exceptions to this rule. Some system calls actually require the stack to be in a certain state using this per-thread register structure. And when you do it from kernel mode, these assumptions aren't really, are violated. So it won't work properly. And these are things like fork, execv, clone. But those are really things we don't really have much of an interest in calling from kernel mode anyway. So now back to the actual challenge. We wanted to see if our process is owned by root. All you need to do is load the EAX register with the syscall number for getuid and call in data. It's really easy. Then you can just check the return code. If it's zero, then we know our process is owned by root and we'll carry on. If it's not zero, then we'll just send that process away. If it's not zero, then we'll just send it back to the original syscall handler that we hooked. So it just does whatever it was intending to do and keeps going. Next we need to actually inject our userland payload into this process that we've now hijacked. So the kernel stack, as a result of that stack switch that I just mentioned, actually has a pointer to the saved userland stack pointer. So when the process enters kernel mode, the CPU pushes the userland stack pointer onto the kernel stack. When it returns from that system call, it knows to put its stack back to where it was. So let's put our payload on the userland stack because we can just read that pointer and know where it is. So what we do is we copy that userland payload that we installed earlier as part of that sort of install packet from kernel memory, wherever we put it, onto this process's userland stack. Because we have a modern system, the userland stack is non-executable memory, so we can't immediately execute code there. But that's easy enough to fix. We'll just call mprotect by loading the appropriate system call number and EAX and arguments and make an int80 from kernel mode to mark the userland stack executable. Now finally, sort of one of the last things we need to do is actually make sure that when this process that we're hijacking returns from the system call that it's in, it runs our code. So in addition to saving the userland stack pointer, the kernel saves the userland instruction pointer on the kernel stack. So that when it returns from the system call, it knows where to go. So we just need to overwrite this pointer on the stack with the address of this userland shell code that we just injected, and then when it returns, it'll run our code in userland. Because we want to do things perfectly and make sure nothing crashes, we actually, after we've done this, we'll jump to the original handler for that system call that we hijacked. So if we hooked close, for example, we want the close to still actually happen. Now the sort of... Okay. So that's the part where you get to use your imagination. Because we set this up this way, you can use whatever userland payload you want. Connect back shell is good. You can use meta-sploit payloads for all I care. One thing that I did do is that, regardless of what userland payload you provide, I prefix it with a stub that makes sure that the process you hijacked keeps running. And this... All this stub does is forks a new process, and then the child runs the shellcode that you injected... Wow, it's rowdy over there. The child runs the shellcode that you injected. and the parent process of the fork will jump to where it was originally going to go. So that, you know, if you're hijacking something important, it will keep doing whatever it was doing and be none the wiser. So now is the real fun part where I get to demo this and pray to the demo gods. So I've got two VMs up and running. Uh, this is awful. The password is password like all my machines. Um, so we're going to set up the rose server stack. This is just a little shell script to create the appropriate interfaces and, and find them. Uh, likewise on the client, just bring up the rose protocol stack. Um, I've aliased call to just make a rose call from this host elite one to the victim host loser one using the rose port associated with this host. So when I type that, um, we've loaded our, our kernel exploit into this attacker machine's, um, kernel. So whenever we make a call, it'll send our exploit over to the victim. So we'll do that and it's done. You don't have to clap yet cause I haven't really proven anything. Um, but you'll notice that absolutely nothing was logged. That's all just residual from bringing up the rose stack and you would have no idea that this happened to you, but that machine is now totally owned. So we will demonstrate the back end of the machine. We're going to show you the back end of the machine. We're going to show you the back door that I just installed as a part of running that exploit. Um, so I'm going to bring up a server that I wrote. This is actually an X 25 connect back shell, which was sort of a pain to write. Um, so this is just a server listening for X 25 connections and this is my little magic ping command that just builds your, your special ICMP packet. I'm going to install this X 25 shell code that I compiled. I'm going to trigger it by hooking syscall six, which is closed. It's just a good choice cause it gets called frequently. And the IP address of the victim. So we will send that packet over and wait a few seconds and pray to the demo gods. And we have a root shell. Now of course, once you've, once you've, um, compromised a host, you need to always check for the zero data text file. Um, living, living in the home directory because you've earned it. Well, that's really difficult to read. Um, well, the point is I also audited the user land utilities associated with this network stack and the X 25 Damon actually has a missing return code check on set you ID. So if you're actually handing out unprivileged shells using X 25, which I'm sure all of you do, um, you can actually make a connection, essentially fork bomb the machine and then make another connection and it'll fail the set you ID call and it will give you a root shell instead of an unprivileged shell. That's sort of a funny bug. All right. Um, this exploit has a lot of things that could use improvement. Uh, first off, the thing that really bothers me is that I hard coded a bunch of things. Um, there are some advantages to using hard coding and this is sort of a toss up between reliability and portability. You know, if you're hard coding something, uh, you're, you're reasonably sure that it's going to work every time on the machine that you targeted it for, uh, versus like a heuristic approach where you're doing signatures or something that will work on a wider variety of hosts, but may fail some of the time, even on hosts that you've tested. Um, on a physical address extension kernel, like the one I targeted, it's, it seems mostly unavoidable, um, that you have to use ROP. Um, so the, the goal is here really just to minimize the number of ROP gadgets that you use and minimize the amount of hard coding of other data structures that you have to do. If you're running a non PAE kernel, the situation is much better. Um, you can actually just get by with a single known instruction, like a jump ESP instruction, for example, because you can jump right into the stack because it's executable. Um, and it may be possible to do something with, with partial overwrites, like overwriting a, a portion of the frame pointer, a stack pointer, or doing some sort of spray approach where you don't even need to know any instructions. So the, the fact that it's PAE makes exploitation much easier. Um, the next sort of limitation, in my opinion, is using this magic little write protect bit is, is fun and easy. Uh, technically speaking, it's not the safest thing in the world. Um, it's a per CPU bit. So if you have a multiprocessor system, there's actually a, a, a small risk of flipping this bit so you can write to read-only memory. And then what if that thread gets scheduled out and scheduled back in on the other processor before you make the write? Then it would try to write to read-only memory and crash. I have never seen or heard of that ever happening. There, that's a very, very small race window. Um, but it, it's possible and it might be worth considering alt, alternative ways to write to read-only memory. Um, it might be possible to just leverage kernel data, uh, kernel functions that already exist that will sort of do this in a safe way. And then the challenge becomes finding those at run time. Now, sort of more general future work on the offensive side. Um, because these exploits really rely on knowing something about your, your target, um, it's, it's, it's, it's, it's, it's, it's, it's a challenge. Um, I think remotely being able to fingerprint a kernel, um, you know, identifying is this a distribution, what distribution is it, what version is it likely to be, um, is really essential in, in sort of gaining the information you need about your target to be able to build your exploit. Um, I'd also like to look at other, other fun packet families for exploitation. Uh, IRDA, Bluetooth, X25, um, are all less tested and probably have plenty of, of fun bugs. And of course finding that, that IP bug that breaks the entire internet is always future work. Um, on the defense side, um, I think it's pretty clear that the weakest link, the weakest link of this exploit is that you need to know things about the kernel. You need to know those, those known instruction locations. Uh, so if we rent, do something like randomizing the kernel base at boot, which is a sort of Linux kernel patch series that I have tinkered with and, and sort of started to try to get upstreamed, um, that really totally prevents all this code reuse. Um, and your exploit no longer works. In the absence of some sort of way of remotely disclosing the kernel memory, which is possible, but I've actually never heard of such a bug. Um, additionally, it's pretty clear that, that the more exotic networking protocols are, are not tested as rigorously and could probably use some work. Um, and next, um, it would be really nice if, if certain functions like set memory X were inlined so that you can't leverage them in return oriented programming. So it's not quite so easy to, to mark things executable. And finally, as sort of long-term work, it would be really good if there were sort of, you know, some sort of policies implemented in the kernel that prevented changing permission, changing page permissions after initialization. And I think the PAX team is actually working on something like this. So now we've got to questions. You guys have any questions? You're all just stunned. Yes? Sorry, what was that? Yeah, the, the bug was found through inspection. I actually had no real radio, amateur radio experience prior to that. I am not a licensed operator. Um, so I thought it was found through auditing. If I hooked getUID in my exploit, um, that would probably cause an infinite loop. Yeah. Because, yeah, because, yeah, exactly. You would, um, your hook would get called, and then you would call getUID in your hook and it would, um, you know, it would, it would, it would, it would, it would, it would, it would, it would, you know, call your hook again. Don't use that one, I guess. Good point. . I'm sorry, would you mind speaking up? . You mean, if I understand, did you, did you mean creating a USB device that would, that would, that would, you know, that sort of exploits itself. You need to get what on the system? Oh, to send the... Like, if you were doing with real radio hardware? How about we talk afterwards? Any other questions? I saw one over there. Can you just disable interrupts? Oh. That would probably help, yeah. For the write protect bit, you mean? Yeah, I think that would probably work, yeah. Good idea. John? No. No, I don't want to marry you, John. You said yes! For information! All right, cool. Thanks, guys.