So, we're getting ready to start. Welcome to presentation on runtime kernel patching on Mac OS X. My name is Boz Eriksson. Kind of hard to pronounce for you, Americans and non-Swedish people. I'm a security consultant at Swedish company BitSec. And I break stuff for a living. And recently I've been looking into Mac OS X rootkitting techniques. So, that's short about me. We only have 20 minutes, so I'm going to speed this up. But the basic talk is going to look like this. Short intro about rootkits. OSX, BSD, and the ex-new kernel. Runtime kernel patching in general. What makes runtime kernel patching specific for OSX. And I'm going to demonstrate a proof-of-concept rootkit for OSX. Also, some basic detection technique for this. And we'll take questions if we have time. I'm going to start off. So, I'm pretty sure most of you guys know what a rootkit is. Well, basically it's a program for access retention. It's like, not an exploit or a throwing horse. Like, it doesn't grant you access to a box. You don't own something with it. Also, you don't send it to a person and have them run it. It's something you run on a box you own to retain access to it. Typically it requires root access. And it provides stealth techniques like hides files, processes, sockets, and so on. Basic types of rootkits are user-based rootkits and kernel-based rootkits. Those are the main categories. The user-based rootkits are kind of easy to implement, and therefore easy to detect. The kernel ones are a bit harder to implement, and much harder to detect if they are done properly. Since you are at the kernel level of the OS, and you can manipulate pretty much anything you want, so. This is a simple demonstration, just so you get this right. This is when you get the kernel, and you get the rootkit. You get owned. That's the exploit right there. And the bottom line is, that's when you stay owned. That's the rootkit. So, just to get this straight, it's required root access. Example of rootkits. Well, user-based, like, various evil patches to PS, Netstat, LS. And you can do also binary patching, like SSH daemons and stuff. Pretty basic stuff. The kernel ones. There are a few out there. Public, private. We have Phalanx by Rebel, which is a kernel patching rootkit for Linux 2.4. Uses devmem device to patch syscalls. Suckit by SD, which are classic. One of the first public rootkits we used as runtime kernel patching. It's for Linux 2.4. Also, later version, Suckit 2 for 2.6. We have Knottic by SwitchGammonCreed, which is a Linux kernel module for Linux 2.2. And took syscalls by patching the kernel. Via an LKM. And also, we have WeaponX by NEMU, which is, I believe, the first public OSX kernel rootkit. But, unfortunately, it stopped working at OSX 10.3. And there hasn't been any public OSX rootkits since then, I believe. So, short intro on OSX and the XNU kernel. The XNU kernel is the kernel of the OSX operating system. And it's built on both. It's based on BSD and mock technology. Mock is the microkernel which runs in the bottom of the OS. And you also have a kind of BSD layer in there. And they're both responsible for different stuff. This is very abstracted. But the BSD layer is responsible for networking, processors, and POSIX API, and the BSD syscalls. So you can basically do any POSIX stuff on OSX without touching the MACK layer at all. And the MACK layer handles kernel threads, interrupts, memory management, scheduling, and so forth. We don't have the time, I want to dig into this, but this is the basic of it. The XNU, like any modern operating system, supports modules. On Linux you have LKMs, BSD has KLDs, and OSX has KEXs, like kernel extensions, for device drivers and so on. So you can extend the kernel at runtime and add a new code to it. And this has been the primary way to subvert the XNU kernel. Like, the... Rootkit I mentioned, Weapon X, uses kernel extensions to patch it. But I think that's kind of all that's been done. It was a talk last year on Black Hat by Jesse, I can't pronounce his last name. He did the same thing with KEX that works for every new release of OSX. But we're going to skip the KEX part and we're going to runtime patch the kernel. So runtime kernel patching is basically subverting the kernel without the use of kernel. So runtime kernel patching is basically subverting the kernel without the use of kernel. With kernel modules. Because that's like the basic entry point in the kernel for normal rootkits. Like... Yeah, like Weapon X. And we're going to do like hooking system calls to stay hidden and implement various backdoors in the running OS by just patching the kernel directly in memory. And you can also manipulate various kernel structures to do all kinds of crazy stuff if you want to. But I'm going to show you some basic examples. examples. This is basically function hooking for those of you who doesn't know it. It's kind of a man-in-the-middle attack between two functions. Let's say function A calls function B, but instead of function B getting executed, the evil hook function gets executed and then executes function B and passes on the return value back to function A, so it's like a transparent man-in-the-middle attack. Function A sees the result from function B, but various other code can also be executed in the hook. So that's the basic idea of function hooking. Runtime kernel patching is nothing new. Like I said, it's been done. I think the first occurrence was the socket rootkit back in like 99, which did this on Linux. And the basic idea is you allocate kernel memory from user land, you put your evil code in the allocated space, and you redirect a system call or anything. Any other function to this memory area, and then you get profit, because you can do like whatever you want. So normal way to do this, this is for Linux. You find a suitable system call handler, like set host name that is rarely used. You back up the system call handler. You redirect the handler to K malloc, the function in the kernel to allocate memory, and then you execute the system call and K malloc will be executed from user space and you have your memory. And then you restore the system call handler. But that's kind of a lot of work. So we're going to do this a bit easier. OSX is very hacker friendly, by the way, since it provides an API for this. You can use various function exported by the mock layer to do this evil stuff, and all you need is root. So there's a couple of function here, VM read, VM write, and VM allocate. I'm not going to go into this like too deep. But basic, they read, write, and allocate memory in a process. So you're going to see where this is going. This is some basic analogy of the mock kernel. The mock part of the kernel of XNU has tasks, which is a logical representation of an execution environment, fancy speaking. It contains one or more threads. And a thread has its own register and schedule. So we're not going to go too deep into this, because we don't have time. But you have ports. Ports is like a message communication channel. It's used to send messages between processes. That's basically all you need to know for this. So here we have a simple function using this for reading memory. I don't know if you can read that. But you just call task for a PID, and you could specify PID zero. It's the XNU kernel running in the OS X operating system. And what you get there is a port to the kernel, the communication channel. And all you need to do is use this function, VM read, VM write, and VM allocate, and you can manipulate the kernel's memory space. It's that easy. It's literally like ten lines of code to read kernel memory as root. It's pretty awesome if you want to root get a machine. Here's the same function, but for writing kernel memory. Same thing. Task for PID, VM write. You're good to go. Also for allocating. It's just a one-line change. So OS X has a global sys entry array, which holds a structure for each system call available in the kernel. Its maximum is 427. It goes from zero up to there. Each entry has this structure which contains the system call arguments. Also a pointer to the system call handler, which is the function that gets executed when the system call is called. So that's our main target we're going to patch. So to do this, we need to locate the sys entry table and patch the system call handlers. This guy, Landon Fowler, he developed a really nice method of doing this with a kernel extension to enable P-trace for certain Apple applications. His method was like this. It's a little bit different. Since the number of system calls is an exported function in the kernel, you like call n sysent, number of sys entries, you can calculate from that an offset to the system call sys entry table. And that's static. It's 28 plus 4 on I386. So you just add to that, and you get to the sys entry table. I can mention before 10.3, the sys entry table was, in itself, unexported. So you could access it directly from a kernel extension. But it's not anymore. So you can't patch it directly. You need to locate in memory. And that works pretty good for kext. But as I said, we don't want to use kext. So to do this from user land, we just need to locate the number of sys entries in memory. And on OS X, the kernel image is on the file system, called mock kernel in slash. And it contains the n sys entry symbol, which can be resolved by parsing the mock O binary. And you take that symbol plus 32, and there you have it, the sys entry table. So you don't need a kernel extension to do this. And yeah, as I said, I was thinking about doing some Mac O parsing here, but we don't have time, so I'm going to skip it. The kernel is a universal mock O binary with two architectures, I386 and PPC. So just read up on how the file form it is, and you can parse it pretty easy. I even have some tools to do it that I'll give to you later. So basically, the modified function looks like this. It's S2A resolve. It's a library I threw together two resolve symbols from the kernel. So we don't change anything. We just resolve it from the kernel on the disk. So now, we have located the sys entry table, which holds all the system calls in the OS. We can read, write. And allocate kernel memory, which is good. So now, we're just going to get down to the dirty business. So it looks basically like this. We have the sys entry table, which contains all the system calls. Normally, it points to, like, 001E425C. I think that's for one reversion back of the ex-new kernel. It points to open for the open sys call. What we're going to do is we're going to modify that structure and point it to our address of our choosing, not necessarily a dead kernel. But another is where we have this function here, where we then have a function pointer, which we will execute and return. So we can do evil stuff in between, transparent. So I'm going to show you a proof of concept rootkit I threw together for this. It's called Mirage. It's a cheesy name. I know it. But it resolves symbols from the ex-new kernel and hooks system calls using the named functions. It's not detected by its check rootkit. But, I don't know any serious rootkit which is detected by its check rootkit. So demo time. And I'm not going to do this live. I have videos, since I'm a chicken. So basically, we compile the rootkit. We patch the kernel. We install. As you see, PD11 here is visible. It's the directory service. We install the rootkit, patch some sys calls, and PD11 is gone. We uninstall the rootkit. And PD11 is visible again. So that's basically process hiding. Thank you. There's more. We have an open backdoor here. It's kind of messy. I'll take it later if we have time. This is the TCP input hook. The basic idea of this is you send the infected host a trigger packet, and it looks for patterns in the TCP packet, and then executes stuff you want. So on the top here is the infected host, and the lower window is the attacker. We build the rootkit. We install it. Hook some system calls and the TCP input. And then we have the TCP input handler. That's a client for a connect back shell on a, this is the same host, but it could be a remote host. And I just send a packet to port 1337, and there you have it. Root prompt. And as you see, we're root. You don't need to connect to the port, actually. I just used netcat because it was easy. You can do that with any kind of trigger, like in the TCP header. Really easy. You just need to modify it. So detection. Well, how do you detect these things, like if you've been infected? It's pretty easy. You just compare the C-Center table in memory to a known state, right? But I think everyone in here knows that that's just not the case every time, because there's like a million ways to bypass this comparison. But I'm going to show you it anyway, because it works. It works. But any serious attacker would not make this work. So yeah, it's not that easy. But remember, number of system calls available is 427, and that was 0x 1ab and hex. And the original C-Center table is that number of C-Centers plus 32. So we can look at the mock kernel image and see our highlight there. We have the number of C-Centers and 32 bytes ahead. Yeah. sysentry table. So all we need to do is we copy the kernel image into a buffer. We find the offset to the nsys symbol, and we add 32 bytes to that offset and return a pointer to that position. And there we have our sysentry table from disk, which is sort of unknown state. It can be modified. An attacker could hook open call to the kernel image and whatnot not to make you read another sysentry table, but it's simple and it works. So here's the code for it. Just open the kernel image and do a for loop basically with a pointer and look for patterns. So if you're on that you should find it. Demo time. Fingers crossed. So we build the rootkit and the detector, so. Okay. We patch the kernel. And you see PD11 is invisible again. We run the detector. It's two modes, scan and restore. First we scan. It detects two hooked system calls. And we run restore. Two restored system calls successfully. And PD11 is visible again. So that's basically it about detection. But as I said, this is not reality. It works in a lab, but you shouldn't rely on this. We could go back and do the other demos we had time. This is basically an open hook for the open syscall, which will take a path and a flag. And if you specify a magic flag, it won't be open, but it will be executed as root. So. Now we patch the kernel. In temp there's a file called run me. And we're just a regular user. And the run me file cats the master password file to temp-pwn. The test program here just calls open temp run me with 1337 as the flag. Nothing special. And then exit. it. So we run it and check the file and we have the password file. Not that OS X stores shadows, hashes or something in there, but it's read-only for root. It's an easy way for a local backdoor to be implemented. Here's the other slides. Oh yeah, here. Articles on this subby, if you want to read up to it, you have Abusing Mock on Mock by Nemo, which talks about this a lot deeper and the whole Mock X new technology. It's a great paper. Mock OS X Wars, A New Hope by Nemo, it's also a good paper on Mock OS X stuff. And Developing Mock OS X Kernel Rootkits by myself and Huawei. We talk a little bit about runtime kernel patching, but mostly about kernel extensions. So Mock Hacker's Handbook by Dino and Miller. It's a great Mock book if you love OS X and hacking Mocks. Updated slides will be available on kmem.se. Perhaps some code. There's all already some code there. Symbol resolving code and some other I can't remember, but it's there. So that's all I had to say. You know where to find me. If you have any questions, I don't have any Q&A, so thank you.