Hopefully this will work. Okay, so what I'm going to talk about is root kits and also ways to detect root kits. I've developed a freeware tool called VICE that does that. So if you came to see a bunch of hookers taken away by some policemen, you're in the wrong room. I know, I know, the limo got broke down, you know, whatever. So as he said, my name is Fusinop. If you came to Black Hat, you know I'm also Jamie Butler. It's in your program. That's no secret. I'm at rootkit.com. Everything I discuss here you can download from rootkit.com. But wait until Monday or Tuesday. Tuesday of next week. The source code for all the rootkit stuff I will talk about is on rootkit.com. So you can have that and change it or whatever. VICE, the freeware tool, currently is downloadable in binary format. So what we're going to do, we're going to talk about hooking and how a lot of really lame rootkits hook and user land and so forth. Then we're going to talk about the tool VICE to detect it. Then we're going to talk about direct kernel object manipulation, which is incorporated into FU and a demonstration. Pardon my voice today. It's pretty dry. So just we'll cover a few general overview of an operating system. This talk applies mainly to Win32, but you could also apply all the rootkit technologies and concepts to a Linux or Spark or whatever. So in Win32... In a Win32 type of environment, there's user land, and that's supported by kernel32.dll and ntdll.dll. So whenever you write a program, that's the libraries you link to, and you use the functionalities exported by those DLLs. Once those DLLs get a hold of your request, they eventually call down to the kernel through some means we'll talk about in a moment. The kernel is what implements the low-level functionality. So it's the kernel doing all the work, kernel32 and everything is just basically a pass-through. So, tax scenario. Rootkits are not about how to exploit a box. There's been other talks here, FX and Alvar, and other talks, Black Hat, experts at Shellcode and so forth. So I won't talk about how to gain access. I'll talk about how to get access the first time. What a rootkit is for is to maintain access. So you want to hide your presence. You want a remote command and control channel that is undetectable by IDSs and by the kernel itself, and maybe intrusion prevention systems. Rootkits will, in the most part, for the ones I write, they'll exist in the kernel. So they act as part of the kernel. And we'll talk about why. And all device drivers acting as kernel can be really bad. Until recently, most rootkits were nothing more than a Trojan program. This is probably like an 80s thing. They would replace PS or LS or some of the other commands on the file system, so that when an administrator would run the command, they would get a Trojan version of the executable. Of course, companies would come along and, for the large part, solve this problem. And now, rootkits evolved to filter data by hooking functions in memory. This way, they could achieve the same results. By hooking a function, you can change what that function returned. So, for instance, if you're asking for a list of processes on the machine, by hooking, you can filter out your rogue process that you do not want displayed. Hooking can be done in multiple ways. There aren't many different places. The import address table is one. And I think we'll have a diagram of that in a moment. You can hook the system call table. You can hook the scripted table. Really, this is only a few places you could hook. There's quite a multitude. So, this is just a diagram. I don't have a light pen, so sorry. I can't point out the relevant features on the slide. But this is... The green represents... The big green box represents a program that's running. And it needs functionality from another DLL. So, it has stubs in its code called the import address table. When the loader loads the program into memory, it will set the appropriate function address for these functions that are in other DLLs in this IAT table. And that's a blow-up there. They're either exported by function name or by ordinal. And the 1-1-2-2-3-3-4-4 represents the address in a different DLL that that function would actually be located at. So, this is the normal control flow. Code in your program goes... And the application goes through the IAT. The IAT calls into some DLL, such as kernel 32, etc. You can... You can inject, though, a DLL into another process. There are a couple different means. We won't cover those. But they're well documented. And it's also rootkit.com. There's at least three different rootkits that use DLL injection to hook things. So, in this case, the rootkit is changing the IAT entry so that the program control flow changes to it instead of the original DLL. Now, here's an example of a system call hook. So, on the... I guess, my left... There's the system calls being made. That's an application asking the operating system to do something on its behalf. It passes through that magic barrier there. Now, it's in kernel mode. In kernel mode, there's a function called... In the kernel... In TS kernel, there's a function called... Chaos system service. Chaos system service will resolve the number of the function you're trying to call. So, for instance, like... ZW create key for creating a registry key. Maybe that's function number 3B. It uses that as an index into the system service descriptor table. And that just contains function pointers. Each... There's a blowup... Blowup of each table entry. And they're merely function pointers that are indexed by call number. The function is represented here. So, you go through the call table to the function in question with this control flow. Now, if you have a rootkit, obviously, you can divert that control flow to yourself. This is the same as a userland IAT hook. More or less, it's just a little bit more complicated. Because not as many people seem to know how to write kernel code. And can get some things wrong. This is an example of a more advanced technique where... After going through the system call table, it still goes to the correct function. Which is represented there at the top. However, the function's been hooked by an immediate jump. And the immediate jump jumps to the rootkit. And then the rootkit has control once again. This is a lot more advanced technique. It's sometimes hard to get correct. Quick overview again in case... This is an animated slide that represents it maybe a little bit better. So, kernel32.dll runs for a while. This function represents create file w. To create a file in the file system. It eventually calls into ntdll. Calling the function ntcreatefile. That runs for a while. And then it calls into the system. So, at each one of those places on the slide. You could have hooked at the beginning of the function. You could have hooked at where the function made the call. You could hook the IATs, etc. Once you're in the kernel. On Linux, the interrupt for requesting service of the kernel. Is an interrupt baby. On Windows, it's nt2e. So, if you hit nt2e. Then that's when you'll jump to a chaos system service. Which will resolve the number of the system call that you wanted. And also where your user parameters are and so forth. Just for reference. In the EAX register is the system call number. In this case, it would correspond to ntcreatefile. And in EDX is a pointer to the user supplied parameters. So, once in the system call table. Again, it's just an address. And it jumps off to the real function in question. This slide is an anime. But you could have hooked ntcreatefile. By overwriting push EBP. Move ESP into EBP. And XOR. You would have to at least overwrite that many. Maybe the push also. Because a jump is a 5 byte instruction. It's at least 5 bytes. So, you have to overwrite at least the first 5 bytes of the function. So, on rootkit.com there's a whole lot of rootkits. That are user mode rootkits. And user mode rootkits aren't the way to go if you want to write a rootkit. Because they're fairly trivial to find. By hooking the IET and so forth. They leave a really big footprint that you can look for. Because you know the range of the DLL and so forth. So, you can look for jumps outside of an acceptable range. And then flag those. So, vice was to take care of that. Also, hooking even in the curls. Usually it's a little bit old school now. It's been around for at least 6 years. Probably in public source stuff. And maybe in the Linux world even longer than that. I'm not familiar with that. So, vice detects kernel hooks. 132 API hooks. And inline function patching. What I mean by that is when you would overwrite the first 5 bytes of a function. That's inline function patching. Also, the not only slide is. If the driver. Device drivers in memory have a table of function pointers also. So, there's. Whenever you talk to a driver. You're talking through our request packet. So, like when you use netstat to list all the available ports. You're talking actually under the hood to tcpip.sys. And you're passing it rp underscore mj underscore query. That's one of the ERPs you'll pass down to the driver. Well, there's a function pointer within the driver object. That describes which function handles. Or the address of the function that handles that particular ERP. See, you can also hook there. However, I haven't currently come across any public root kits. That do that in the Windows world. And I'm doubting it's done much in the Linux world. I'm not sure of the format. The LKM structure. Okay. So, I'll try to get to a device demonstration now. And show you how it works. And that you can use it when you go home. Okay, well, it's on my key. I'll try to show it in a minute if I have extra time. The key's not installed right now. So, to save the day, I have backup slides. So, here's Vice versus Hacker Defender. Hacker Defender is a very good root kit that's on rootkit.com. I believe it was written by Holy Father. And he's very good. He's very knowledgeable. I've discussed rootkit stuff with him in email. Here's the vice output. I'm not sure if it's completely readable. But all these slides should be on your CD also. The things there with the embers glue. Little icon. You'll see it's hooking. There's something hooking kernel32.dll. And actually, that turns out to be, in this case, way up top of the screen, is NTDLL. So, you might say. Well, NTDLL is not a rootkit. That's correct. But the way it's used in the system, it actually hooks other Windows APIs occasionally. And so, therefore, it is a hook. And that's what vice catches. So, the thing with the Swiss Army knife. A little icon. That's all the functions that vice called Hacker Defender hooking. When possible, vice will output where on the file system you can find the hooking module in question. So, that will allow you to go to the file system and delete that bad DLL or whatever off of the system. In this case, it couldn't resolve Hacker Defender. Vice is, you know, a project still in the works. And maybe in the future, we will be able to resolve that on the file system. So, you can see it hooking things like create processes. And so on and so forth. It also gives you the address and memory. Vice does. So, if you wanted to go and reverse engineer it yourself. And figure out exactly what their function does. You can do this. Here's vice versus vinequish. Vinequish is another popular rootkit at rootkit.com. And, enough said about that. How many read KDM's paper on FRAC 62? Anyone? Okay. He stated that vice could not catch his rootkit called MT Illusion. Which is an entirely userland rootkit. This was correct at the time he tested. There was a bug in vice. And vice had not had any development on it since it was first written in mid-April. However, now that bug is fixed. And as you can see, it catches MT Illusion. What's noteworthy here is not only did it catch MT Illusion. It also lists where on the file system it is. So you can go delete it. So that was sort of the rootkit spin and hooking. We talked about how hooking is detectable. And now we're going to go into a little bit more of what consumers are currently doing. In the world of, you know, trying to prevent viruses. Trying to prevent rootkits and so on and so forth. Everyone's running to third party personal firewalls. And also host-based intrusion prevention systems. So what are HIDS? HIDS are host-based intrusion detection system. HIP's being host-based intrusion prevention system. What are they there for? They're there to detect what processes are running on the system. Maybe if a new process is started. Block access, you know, to things like netcat and so forth from running. What files are created or deleted or modified. That may be important to a HIP's. Like don't let anyone write to the WinMT or Windows system 32 directory. Because that's probably going to be a bad thing. HIP's also keep track of network connections made. And possibly deny those connections. And they try to do a little bit with privilege escalation. And they look for buffer overflows and so forth. However, for a lot of these different pieces of functionality. HIP's and the current security products rely on the operating system itself. So if you lie to the operating system, you lie to the HIP's. Here's some different functionality that's built into the operating system itself. For instance, today they allow HIP's to work. If it allows the HIP's to work, it can also probably... You could use this as a starting point to where to look to try to unlink a HIP's. For instance, in your kernel. So you can register OS provided callback functions. For instance, if you want to be notified every time a process is created. You can register an address to be called in that case. And the kernel will do so. That works for threads also. Images loaded into memory. There's all these different pieces that HIP's use to detect new things coming into the kernel. Or coming into the process listing. You can hook these... You could remove... These are nothing more than a linked list of functions to call. You could remove a HIP's product from that linked list probably. Without a whole lot of problems. Some HIP's actually hook the system call table. You have to do that especially on Windows if you want to... If you want to filter on registry key access. Because there's no easily... There's a userland API that you can call for a callback when a registry key changes. Or any of its children. But it doesn't provide you much useful details. It just says, hey, something changed. And... You would have to reparse all the keys to figure out exactly what it was that changed. And therefore you'd have to sort of keep state like before and after. That's not very useful. So HIP's for the most part just hook the system call table directly. HIP's can hook userland API functions. How many saw the FRAC article on bypassing third party Windows buffer overflow protection? Okay, that was done by myself and two other anonymous co-authors. Who I'd like to thank. And they know who they are. So anyway... Some products were hooking the userland APIs. And we wrote some shellcode and so forth that totally bypassed their implementation of buffer overflow protection at the API level. And then you can query the kernel of course. So if you want to... Know what network connections are open, you can query the kernel and it'll tell you. Well, there's a problem with a lot of these HIP's designs. And it goes back to the FRAC article really. It requires that they be on the execution path of any malicious code. And they have to know where that execution path would be ahead of time. So... There's no reason that... We have to use the APIs in the kernel or even in userland. So the ones that we know that they're watching, we don't use those APIs. And I'll introduce Deacom in a second, which uses absolutely no APIs to make its changes. It may use some APIs once in a while to gather information, but... This is really benign information that would never be blocked anyway. So let's talk about... Operating system design quickly. Intel has four privilege levels, or four rings as you see here demonstrated. However, Microsoft and other OS vendors do not use all four of these rings. They only use two. There's ring zero of course, which is completely privilege code. Which is the kernel itself for device drivers or LKMs. Then there's ring three. Ring three is all applications. So when you're running in ring three... For instance... Ring three is just ring three. Like the HIPS doesn't have any more privilege than you do at that point. If it's only implemented at ring three. Which is the case of some of these 132 buffer overflow protection mechanisms. Ring zero, you have access to all of kernel memory. So everything that the kernel uses... Like objects to keep track. The kernel is more or less a... You know, it's a big accounting arm. So it's keeping track of all the processes. All the ports and so forth. And it does this in structures that it calls... That Microsoft calls objects. So these are kernel objects, not to be confused with C++ objects. So once you have this low separation of powers... There's only two privilege levels. Every single device driver... Has access to all of these accounting objects... That the kernel has access to. Therefore, you can modify it just as much as the kernel can. You just have to be very careful how you do so. You have to understand what the structures are. How to find them. What they do. And how the kernel expects them to look. So if you don't understand those important details... You may modify an object in memory and cause a blue screen. There are so many... There are third-party device drivers. I mean, there's your printer drivers. Your fax drivers. All these different things. And there really should be different levels of protection between the two. Between the kernel and the drivers. So the next generation of rootkit technology... Is DCOM. It's a play on, obviously, Microsoft's terminology. But in this case, direct kernel object manipulation. So what does it do? It modifies objects... Directly in memory. Never going through APIs. So there's well-known APIs to... For instance, add a SID to a token in a Windows process. Well, first of all, the kernel is going to see if you have the appropriate privilege to add that SID. And it may stop you there. Also, the HIPs or other third-parties may wrap around that function flow. The function control flow. And try to intercept that call. And limit you there. But if you write just directly to the memory, there's nothing to stop you. So you can use DCOM to hide a process. Add a privilege to a token. Add groups to tokens. A cool application is to fake out the Windows Event Viewer. So that if a user... I should say, if a process spawns another process... Such as Netcat. If the administrator has detailed process logging turned on... In the Windows Event Viewer, they will see... An event that says that administrator... Or whoever you're logged in as... You know, just started... CMD or RegEdit32 or whatever. Well... If you change your token... To look like someone else's... Just by changing a couple bytes... Then... The administrator will be thrown off. Because it'll look like... You can make it look like system did it. So system just spawned Netcat. Now obviously that's going to be a red flag to any... Halfway competent administrator. However, it's not going to point at possibly... The user account that you subverted. It's going to be a red herring that they're going to have to chase... For a couple weeks probably before they really figure out what happened. And maybe they'll never figure it out. So... Another thing of... Decon, you could use it to hide ports. Currently the... FU rootkit does not hide ports. But you can do it. So... Obviously the implications of hidden processes. The hidden process can have... Potentially full control of the system. There's no accountability. It defeats... The... Heads and hips type of paradigm. And... It can skew the results of forensics. It all depends on how... How advanced your forensics are. But you only have to... Be... Overcome the slowest... Forensic tools. For the most case. So how would you go about hiding a process in the Windows kernel? Here's a... Here's a picture of how the objects look. In memory. The... The kernel has a... Processor control block. It's represented there at the top. Within there is three key pointers. One is to the current thread. One is to the next thread. And one is to the idle thread. So you need to find... Your current thread. Because that's going to lead you to your current process. So you go to the processor control block. Follow that pointer to the current thread. That's represented there by the E-thread block. That's sort of in the middle of the diagram. The E-thread block... Or... Object contains a K-thread object. Completely embedded within it. At the beginning of the E-thread object. And that contains a pointer. That leads to the E-process block. Every process in a Windows... Operating system has to have at least one thread. And it has an E-process block. So... These structures are... For the most part undocumented. Some of them now are becoming a little bit more documented. However they change a lot between versions of service packs. And also major versions of the object. In the operating system. So you have to do... At first when I did this... Two years ago. It was all reverse engineering. Now you can use WinDebug. Load up the symbols appropriately. For whatever version of the kernel you're using. Type... DT... Space... MT... Bang... Underscore... Object name. And if you don't know what the object name is. You can wildcard it. To try to narrow it down. I will tell you that not all the object numbers are documented. Like Microsoft may just say. Oh it's a... It's a P void. And so you don't know what it actually leads to. And of course that takes a bit more time to figure out. So within the E-process block. There's a... There's a list entry structure. It contains two pointers. The first one is called a Flink. And the second one is called a Blink. A Flink is a forward link. And a Blink is a backward link. So this list of E-processes. If you would just keep following the Flinks. It's completely circular. Also if you just keep following. Well you'd have to do like. Dereference Blink. And then add four. To get to the link behind you. And keep following the chain. That way. You could go backwards through the list also. So if you want to know how to process. The kernel processor control block. Is located at a fixed address. Also if you don't trust that. Because I haven't verified that since 2000. You can use the FS register. To always get the address. Of that kernel. Kernel processor control block. Also. Within there we talked about. That you can get the current thread running. And there's the assembly command. Well. You would just move that into like. EAX the FS colon. Bracket 124. And that would get you to. The E thread. The E thread takes you to the K thread. And the K thread takes you to the E process block. So to hide a process. All you need to do is. Change the E process block in front of you. To point to the E process block behind you. And the E process block behind you. To point to the E process block in front of you. And I think. I have a demonstration of that. Or a graphic. So here. We don't have a pointer. But we're trying to hide the current process. So if you just. It's the E process block. That's in the center. So you would want to modify the flink. Behind us. To point to the flink in front of us. And that's represented there. Once you do that. Half the game is done. Now you have to change the. The blink in front of us. To point to the flink behind us. Like that. So you just tell your neighbors. Hey. You know. Point around us. So why does this continue to run? Well it turns out. That the. The scheduler in Windows is extremely advanced. And. It's thread based. But however. The reporting mechanisms. Are process based. So you can edit this E process block. And all your threads. Will get scheduled. Because they're off in their little thread queues. And so they'll continue. To run. I'll just cover this. Really quickly. Because Linux isn't my strong point. And I haven't looked at it since it came out. With the new threading model. In Linux 2.6. But within the Linux kernel. You could do a similar thing. However there's sort of a gotcha at the end. The Linux kernel contains. So each task struct represents a process. A task struct contains. A linked list. With the pointers. Previous task and next task. That you can also follow around the circle. And it contains. Two other vital pointers. Which are previous run. And next run. To determine the list. Or the ordering of when to schedule a process. So. How to process. You just remove. It from the previous task. And the next task. Like demonstrated in the e-process block. Exact same concept. You leave next run and previous run alone. Does anybody know what happens. When you get this far? What? Speak up. Okay. The whole process freezes. So. Nothing. Your process does not continue to run. And the reason being. Is because. The way that. Processes get scheduled time. On a Linux 2.4 kernel. In which case. The scheduler. Walks around the previous run and next run. List. So it follows the next. I'm sorry. Follows the previous task. Or the next task list. Walks around this linked list. Based upon. It calculates a goodness value. For the process. And based upon the goodness value. It assigns it an order. Or the number of jiffies and so forth. In which to run. Or to get CPU scheduling. So that's. When it puts it into previous run and next run. The ordering there. Well if you're never in the. In the next task listing. Then you'll never get your. Goodness value calculated. You'll never get scheduled again. Because. The scheduler can't see you. This is probably all different in the 2.6. With the threading model. There was a root kit on. After I hit this problem. There was a root kit. That I found online. That was open source. It was famous morgy. I believe is the name. And this person had come across. The exact same problem I was having. And the way they were going to fix it. Was to patch the scheduler. So what this. But the. Solution did was. The inert process is always. The first process that has no. No parent process. So the parent processor. Or pointer parent process. In the task struct is null. Well they modified that. To point to the hidden task struct. That way they modify their scheduler. To always go there. And look. At these hidden task structs. And calculate a goodness value for them. But if you're going through all that effort. There would probably be better ways. To hide your process. It should be noted. That you can't just change these listings. Arbitrarily. And not expect some problems. These lists especially. In a multi threaded environment. In a CPU especially environment. These lists are shared resources. So you have to. Use some kind of. Synchronization objects. To try to get access to these lists. If you want to completely modify them. Or modify them in a completely safe manner. In Windows. If you want to modify the list of. Active processes. There's a non exported symbol. Called PSP active process mutex. The kernel every time. Grabs that mutex. And once it has it. Then it can modify the list. PS loaded module resource. Is to guard the list of device drivers. Problem is obviously as I said. These symbols are not exported. So we have to find another way. To come up with the synchronization. One way you could do it. Is you could hard code these addresses. Of these mutexes. And that would vary a lot. Between each version of the OS. And maybe. Potentially each reboot. Could also. Have an effect here. This isn't probably the best solution. Another way you could go about it. Is to try to search. For a known pattern in memory. You know that there's going to be. Certain functions within the kernel. That have to access these mutexes. In order to operate safely. So you could do a search. And. Sherry Sparks. From the University of Central Florida. Has worked with me on this. She wrote all the code to search for the. Pattern matches in memory. In order to gain synchronization. Another technique. That we're playing around with. Is trying to raise every. You can raise your. The dispatch level. On the processor. Or the IRQ. Level on the processor. To dispatch level. Then you can never get swapped out. You'll continue to run. Until you give up control. Because you run at the same. Priority more or less. Or the same level. As the actual scheduler itself. However you can only raise. One call to this function. Only raises it for that particular CPU. So you would have to. Find another way. To raise it on every single CPU. And we think we may have. A solution for that. But we haven't coded it up. To test it yet. So on to token manipulation. I have the privilege. Of some process. You can go and manipulate the token. Would be something like SE debug privilege. Or SE shutdown. Or SE load driver. For instance. You can also add groups to a token. The groups represent the SIDs. That that process belongs to. Also. The first SID. That token. Is the owner of the process. That's important. And we'll talk about it in a moment. And by altering some of these fields. You can make it process. Look like someone else launched it. So how is this possible? You have to understand the structure. Of the tokens a bit. There's a static portion. And there's a variable length portion. Within the static portion. Obviously it's a fixed size. There's important things such as session ID. There's a pointer to user and groups. There's a pointer to the privileges. And there's the number of privileges. And groups. So we'll need to modify these things. And also alter the variable length. Within the variable length portion. Because a token can have. Different numbers of privileges. And so forth. It may not be the same for every token. They're put in the variable length portion. The same for users and groups. And restricted SIDs. I won't cover restricted SIDs. It was something that was added in the Windows 2000. Kernel I believe. And I haven't seen much use of it. But you could restrict certain. Certain users from performing certain actions. With restricted SIDs. So one of the problems I ran into. In the FURootkit was. You cannot just grow a token in memory. In other words. You cannot just write to the end of it. Because you don't know what's below your token. There could be. Especially for FU. It doesn't take into account. The restricted SIDs. So you would sort of just write over the restricted SIDs. Which probably isn't a good idea. Also you don't know what's at the end. There could be another process token. So on and so forth. The memory may not be valid. You could cause a page fault. Or potentially a blue screen. And so I went about this. Or a way to solve this. Was to allocate memory. And that turned out not to work. But I did was allocate enough memory. To put the new privileges. And the new SIDs in. And then I just changed the pointers. And the static portion of the token. To point to that region of memory. And that worked for the current process. But if that process ever spawns. A child process. The way that SEP duplicate token works. It tries to create. It takes the parent process. As well as the child process. As a token. And tries to create that in the child process. So it's an inheritance. But the way it works. It actually has a subtraction in there. That I can't figure out. They assume they know. Where the token is in memory. Or what's right below it. And so on and so forth. So they know some things about the token structure. That I don't. And that prevented this mallocking from working. If they would have just followed the pointers. Which seemed like the obvious way. For me to do it. They wouldn't have had any. Or I wouldn't have had any trouble. When I tried to do it using that method. So the way. The way I went about this. Was to try to. There's a lot of privileges within a token. That are disabled. By default. And so if they're not even turned on. Then I probably don't need them. So let's just throw them away. And I used this method. Just to free up some. Slack space within the token. So I can write some valuable information there.