Okay, let's start. My name is Alfredo Ortega. I work as an explorer writer for Core Security. This talk will be about the OpenBSD bug. If you don't understand something that I am saying, just ask me and I will repeat. Okay. This is a bug that I discovered a couple of months ago. Well, like four months ago. And it's an interesting bug, and the exploit is interesting too. And got pretty much publicity, so I'm going to explain it. Okay. Oh. Let's start talking about how I discovered the bug. Okay. This is the factory. Okay. This is the history. I was assigned to do a research about a patch in the OpenBSD kernel, named the number eight patch, a reliability fix. This was, in fact, a hang of the kernel, an infinite loop in the kernel, caused by fragmented ICMP packets. So I give my boss command and tell me if I could reproduce this bug. So I started to trying to build a fuzzer. Do you know what a fuzzer is? It's a little program to send malformer packets and try to train to break the system to reproduce the bug. So... It really didn't... I really didn't know it. I didn't have very much information about the bug, so the only thing that I could do is to start trying and studying the patch, trying to make a couple of amended packets, and send it to a OpenBSD system, trying to make it to break, to lock in an infinite loop. So the fuzzer is a very simple program. I made it in Python. It only makes two packets. From the IPv6 protocol. The IPv6 protocol is, as you know, a very new protocol. It's not very much used. And it's got a lot of bugs. I will be presenting some this bug, and in the end maybe I will do some live demonstration about another vulnerabilities. I spent about two days trying to build in malformer packets, thinking how I could break the system and in the end I... In a looky packet, managed to break the system. I sent it to an OpenBSD virtual machine and the OpenBSD kernel just hung up. But it was not an infinite loop like the patch was seeing. It was a complete hang up. The system just hung up and threw a stack trace of the bug. I was thinking, this really is not the same bug that I was trying to find. Maybe it's a new bug. So the thing that I did was to download the latest version of OpenBSD kernel. It was at the time the 4.1 OpenBSD kernel. And trying to attack against that kernel. The attacks worked anyway. So in my hands I did have what is called a zero-day or unpatched vulnerability. In the time I was thinking it was a service and maybe it could result in code execution, but I was not really sure about it. So I reported to my boss. My boss reported to OpenBSD team and got fixed in about two days. They were very fast and fixed the bug. But they reported as a danger of service, of a reliability fix. And I was thinking that this was not a reliability fix. It really was an exploitable bug because of the stack trace. So I started researching how to exploit this bug. Let's see why the bug was hanging in the kernel. Okay, these are, let's see. In the, in the, a lot of Unix systems, the network packets are stored in a structure named mbufs. The mbufs are little packets of memory of a fixed length. They are about 256 bytes long. They are ordered in a linked list in memory and they store network packets and... inside them. And if the network packet is too big to fit inside a mbuf, the mbuf got an external memory buffer. But the failure of the kernel was that the kernel was overwriting a lot of mbuf buffers in the way that it's showing there. Instead of writing into the first mbuf, mbuf1, the Open VSD game was overwriting a lot of these buffers. And not only the buffer but they either information inside the buffers and the couple of pointer to link the buffers to link touring. So when the Open V flooded system was trying to free one of these packets, it made it morning operation, that is operationist exchange of two pointers to fresh them. And then when with the node between them. And because of the pointer was overwriting with garbage, at the time it was garbage, the system was hanging, was making an illegal operation, and it totally hanged. This seemed like a very exploitable bug, because when you overwrite your pointers, you can use a very common technique, namely the mirror copy, or four-byte write. When you exchange your pointers and you can control the pointers, you can manage to write four bytes of data in any place of the memory. So I think if I am overwriting a linked list, it's almost sure that it is exploitable. So I started to disassemble the OpenBCD kernel to see if I could exploit the bug. Maybe it's not shown very well, but here is a disassembly of the function that was hanging. The function that was hanging was a free function. I see in the down, the free end function was crashing. Here. The end-free end function was crashed. That is the function that free an end-buff. So I disassembled this function. That is the result. I must say that the OpenBCD kernel is an open source kernel, and I really did have the C code of the source code of that function, but I am really more used to see it this way, to see it in assembly. To analyze the function in assembly, it's more easy for me to see it this way. The function started in the orange block, and we can see, if we could see, that the packet to free is stored in the register. I must clarify that this disassembler is for the Pentium and 32 bits only. OpenBCD run in assembly. There are a lot of architectures, but this exploit is only for Pentium at 32 bits. In the start, this function placed the packet to be freed in the one register, in the ASI register, and then it made a lot of operation using this register of reference. The point the kernel was breaking is making yellow. You can't see it very well, but it exchanges the pointers here. In the block in yellow, the pointers of the linked list are being exchanged. Of course, the linked list was overwriting, and it was trying to write in a position of memory that did not exist, so it broke exactly in the yellow block. I say, okay. Let's try to reach the yellow block with controller pointers, so I can write in any place of the memory. If I can write a value in any place of the memory, I can do a lot of things, like make my user process root, or open, I don't know, change the permission of the new process. There are not very much things that I could do, but some things, it could be done. It will be exploitable this way. While I was analyzing this, I realized something. I realized that in the next block, in the block next to them, to the yellow block, was a call in the referencing a pointer. It was getting a pointer from the block here in the arrow, in this part. Let's see. In this part, it's moving a value from the packet inside the EX register, and then, in the next block, it's jumping to this EX register. In fact, it's calling them, but it's the same as jumping. I could send a value here inside the packet, and make that open VCD jump exactly what I want, so I can control the flow execution. In a much easier way than trying to do the other trick of the yellow block, that writing in some part of the memory, it's much easier to jump to any place that I want, because I could send my code, and place my code, or my shell code, in some place of the memory, and make the kernel jump it, and I will get total control of the kernel. Of course, I will gain execution inside the kernel. It is not the ideal situation. The ideal situation was to gain control in a user process, because kernel exploit is much difficult to do than a user exploit, because in a user exploit, you got the kernel to do whatever you want. You can use the kernel to open a file or to open a connection. It's much easier. The kernel is made for that. You... When you do a user exploit... When... Only... A hundred instructions, you can make the machine do what you want. It's a very small exploit. But in kernel mode, you really don't have a kernel. You don't have an operative system to make use of. So it's almost like you are your own operative system. Anyway, I was gaining execution inside the kernel, and I was executing my call. So I told the OpenBSD people that, in fact, this bug was exploitable. I still didn't have the exploit. The exploit took me about a couple of weeks to make because it was a very difficult exploit, and I was helped by a lot of people from kernel security. But anyway, now I am in the situation that I can't jump to any place that I want in the memory. But where I want to jump? Of course, I cannot jump to the same kernel because the same kernel is not my call. I want to jump to my call. So I can't overtake the system. First, I must place the code in memory in some way. Well, in this transparency, there are markers, the source code in C, the equivalent of that function. Here's the assembly. And here's the part of the source code that makes the call. It's a little difficult to understand. It's marked in red. It's marked. It's the part of the source code that jumps to the reference pointer. As you can see, it's much easier to understand in assembly than it is in C. It's a lot of micro-expansions. I never will realize the bug looking only to the source code. Okay, that's better. Okay, in this drawing, you can see how the fragmented packets of ICMPv6 are placed in memory. In the right, we have the linker list of mbuf or mbuf chain with the correspondent headers. And the headers of the mbuf are pointer to the next mbuf, pointer to the last mbuf, pointer to the free function. And the packet that places in exactly that way in memory are overwriting a couple of mbufers, or mbuf. And the arrow here, this arrow marks the exact position of where the pointer to jump is placed in memory. So I must place a pointer here. This pointer will overwrite the free function. This pointer will overwrite the free function inside the header, and this function we call the pointer that I place there, here. Here it says trampoline. You can't really see it, but it says trampoline. Trampoline is a name for a position of memory that I want to jump. I want to jump to a... Trampoline. So I put this pointer in the header of the mbuf in exact position. You can be not even one byte... One byte and disalign. It must be exactly aligned to the pointer. But it's something very easy to do. Okay, let's continue. One more. Yes. Okay. Thanks. All right. If I could jump right to the machine andrà back. Oh, well, we can now jump to any place on the memory that we want. But where is the sharecall located? We know that the sharecall is located here. Here it say sharecall is placed next in the same packet because it's very easy to put the sharecode in the same packet that gives the одного jump over. row. So we got this portion of memory, we got our code. The shared code or the code that we want to execute. But we don't know where this is placed because the mbuf, the dynamic, align places and it could be in any place of the kernel memory. It's not in a fixed position, it will change. It will change from run to run and from kernel to kernel. But we know one thing. We know that the shared code is pointed to the ASI register. The ASI register points exactly where our shared code begins. Why is that? Well, because that is how the function was compiled. We remember that the packet in the beginning of the function was pointed to ASI, was pointed here. So if the packet is pointed to ASI and we do an immediate jump to ASI, the packet is pointed to somewhere that we want. We must jump to some place in memory that jumps back to the position of the ASI register. It's like a bouncing. We bounce in a point of the kernel memory that have an intrusion that jumps back to the ASI register. We must find a position of the kernel. Up. Down. Up. This is the kernel. We must find a position of the kernel that got an intrusion that jumps back to the ASI register. This intrusion is a very small intrusion. They are no more than two bytes long and it's placed thousands of times in the kernel. So it's very easy to find one intrusion. It could be in any place of the kernel. It only must be there and be executable. So we find one intrusion, it could be any, we find one intrusion that jumps to the ASI register. And we simply jump there. So we jump in this intrusion. That intrusion jumps back to our shellcode and we are executing our shellcode now. We have complete control of the system. I was thinking, okay, game over. It's over. But it's not over. Because as I was seeing, the kernel, you really can't do anything in kernel mode. I can't even open a file. I can't open a connection to the internet. Because I am running in kernel. And it's a very tricky, this is a really tricky part, to make the exploit work in kernel mode. Because it must be like a mini-operative system. It should be. Okay, there are a lot of ways to do this, but this is the way that we implement it and this way it really works pretty fine. In kernel mode, there are not... There are a lot of many things that you can do, but you can do very easily hook interruption. You can get the interruption table of the Pentium processor and you can hook, you can make that one interruption to assume to you, and you instead jump to the original interruption. So what Arwas did was to hook this particular interruption, the system call, to operate the system. Communicate with the user. The user process using an interruption in Unix. When in Windows, it's more or less similar, but in Unix, the interruption number used is always the 80 interruption. So I must hook this interruption, the 80 interruption, in the same way that the old element in a style resident program from DOS. In DOS, there were a kind of program, maybe you remember that. That hooked the... The interruption, the timer interruption, or the keyboard interruption, and in that way, it's still resident in memory. This is the same technique, but we hooked the system call interruption. This is the original system call. We got a user process here. The user process make a system call, I don't know, open a file, or kernel open an internal connection, and the kernel open the internal connection and returns to the user in the next instruction. And here is... Here is how the hook is made. The system process, the user process, sorry, jump to the hook that I have installed in the memory, and the hook make something that I want, and then return to the original system call, and the system call is made and returned to the user. So the user really don't know if he don't realize that it's calling my code. He thinks that it's calling the kernel, but it's not calling the kernel, it's calling my shellcode in memory. Okay, so it's calling my shellcode. What we do in this situation? Okay, now we can see every system call of the system. We can see every process and every operation of all the process of the system. This is a very useful thing. So I implemented... This is the cellular call of the operation of the hook, the interruption hook. Okay. Okay. Okay. First, we don't want to affect any user process, because we will... In this way, we will hack the machine. We... And not every user process are useful to us. Only the privileged user process are useful... The user process with a privilege of root. Because if we take over a process with no privileges, it will be very useful. We want to take over a user process. process with root privileges. So first we want to see if the process calling us is root. Of course, this is a very hard thing to do because we cannot simply get the process ID or the user's ID of the process because, again, we are in kernel mode. We must do a lot of tricks to do this. First, in user mode, you use a system call called getPid or getProcessID. Just simply have that. That's a system call, returns a number, and that number says if you are root or not. But now we must do a little magic here. We first search for this variable in the kernel memory because the kernel can be any kernel. It can be an OpenBSD 3 kernel or 4 or can be a custom component. And this variable, the current process variable, in fact it's an array, it's not a variable, could be any place in the memory. So we must search it. We must search through all the memory searching for patterns, for a specific pattern of memory that tells us where this variable is placed. These patterns are present in every OpenBSD version from 10 years ago and are very easy to find. Once that we got this current process variable, we can see inside this variable, in fact it's a structure, it's not a variable, what is the user ID of the process. So now we got the user ID of the process. And if the user ID is zero, the process is root and we can't take over this process. So if the process is root, we must attack this process. So we know that the process has full privilege of the system and it's much easier to execute code in user land or user mode than in kernel mode. So we got a process that this root is calling us. We must take over this process. First, we must execute code in this process. We could just overwrite the return pointer and the return pointer of the process should be in... Because we are kernel, we can overwrite any place in the memory. We could just overwrite the return address, but this is not a very good thing to do because maybe this process, the process that are calling us, the binary that are calling us, is the libc. The libc is a library that is very important in the system. We can't just overwrite the libc because the g system will crash within, because every process are using the libc. We cannot overwrite the binary. We must place our shellcode in another place. A good place to put our shellcode is the stack because the stack is one for every process and it's very easy to reserve memory on the stack, put our shellcode there, and execute our shellcode on the stack. But there is a problem with that approach. That is this protection. Okay. This protection is a very good protection that OpenBSD got that says that no place in the memory that is writable is executable. The memory can be or writable or executable but not bought. So that is exactly what we want to do. We want to write in the stack and execute in that place. So the OpenBSD kernel will not let us do that. We want to disable this protection. This protection can be disabled because we are kernel and we can modify every register of the Pentium processor because we've got complete control. So how we disable this protection? We extend the selectors. The selectors, OpenBSD implemented this protection, changing the selectors and making some selectors shorter, some selectors larger. And the space between the selectors is very large. The specific selector that is affecting that don't let us to execute our code in the stack is this selector, the code selector, of course. We must extend this selector. We extend this anyway. This is not very important, but this selector is very important. We must extend the code selector, or CS, to let us execute code in the stack. This extending the selector can be modified. It can be made in a user process, and user process can extend selector, but it don't have permissions to do that. But we are in kernel, so we can extend the selector. Once this protection is down, we simply copy the user mode in the stack and execute that. We modify the return address to the pointer of the stack, and when the system can restore, it will return to the stack. Instead of returning to the binary image of the process, it will return to the stack. And we'll execute there perfectly, because this protection is down. So now we are executing our shellcode in a user process, and it's much easier to do things in that place, like open a connection or, I don't know, adding a user, writing a file. I mean, kernel. Because now we are a user, and we can make use of the kernel. In the end, of course, we must restore the original interruption vector, because if we don't do this, every root process will be taken over, and we don't want that, because we want to take over only one process. We only need that. We don't need to take over all the root process of the machine. So we restore or remove the hook, restore the original interruption vector, and that is, now we got one process, the first process that are calling us, root, and executing all code. This process could be anyone. It can be the send mail process. It can be the Apache, I don't know how we say it, Apache web server. In fact, I was seeing that every time I send the exploit, the send mail process was the first that was taken over. Send mail make a lot of system calls, and it's root, and it's every, almost every time is the send mail process is the first that is attacked by this exploit. And if we are not root, if some other process call the kernel and yes, not root, we just continue with the original system call, and we don't modify it, because we are really done is interested in attack, in attacking a process. If we are not root, that is not root. Okay. This is a diagram. We got a little problem here. There should be a line here, and a line here too. This is a map of the memory of the Pentium 32-bit processor, and how OpenBSD managed it. We got four gigabytes long, and the OpenBSD system reserves the first part of the memory, and the memory. for executing binary code here. So the only executable code can be placed here, in this small part of the memory. This code will grow a little. It is viable, can extend a little further. But only this place of the memory is executable. And this place of the memory is not executable, but it's writable. Here is the data memory, the heap and the stack. So this is the operation that we do. We extend the code selector that only reached here originally, and we extend it from two here, all the way to the wall memory, so we can execute every code in this area, too. We also extend the data segment, just because. It's not really necessary. Well, so a little assembler. This is the implementation of the serial code that I was showing. It's maybe a little, this, it use some instruction that are not commonly used in user mode, like this one and this one, that store global description table and store local description table, because the selector are stored in these tables that the painting processor use to store the selector, let's say. So we first store the selector in some place in the memory, could be any place. We choose to store it in the memory, so in the ISP, in the stack itself. This section of the code are a lot of operation used to assemble the selector, because the selectors are not contiguous in memory, are a little, I don't know, like disassembled at some place in one byte, and some bytes later are another part of the selector. I don't know why this is. This is the Intel engineers, they made it that way, I don't know why, because, but you must assemble the selector using a lot of boolean operations, and these two final instructions extend the selectors, place it only a value in a specific place of the memory, then we must load again the selector. Here is not the part that loads the selector back into the painting processor. It's not showed, but it's next. Okay, but we are not really finishing it yet. I was saying this exploit is a little complex, because OpenBSD is a clever process system, and in the next context switch, we'll restore the selectors. So, if we are happily executing in the stack, but if the process, our context switches, switches back, the process will crash, because the protection, this protection W, WX or X, oh, this is bad, WX or X, will be enabled again, and the process will crash. The execution of the checker will be detected by the operating system. So, we must try to get a portion of memory that are execute, that are writeable and executable forever. How we do that? We can use two, we got two options to do that. In fact, we maybe got more than two options, but these are the two options that I think of. The first one is to use another system call, because I am executing the user mode, I can call any system call that I want, and I call this one, that is named and protect. This system call is used to give any portion of memory, any permission that I want. So, I just unprotect all the stack. I say to the operating system that I know I want to execute calling the stack using this. And the operating system just will make the whole stack executable, like it was in Windows 95, and will be executable forever. We now do a couple of things to fix the original process, because we don't want the same my process of the web server process to crash. We want it to, to make a new process, and to keep the original process attack to functioning, to work okay, to work normally. So, we first fork the process, make a clone of the process, and now we got two processes that are the same. In one of those processes, we fix all the stack, we fix all the things that we overwrite, and continues to execute normally. So, if we, if we attack the same my process, the same my process, we continue to execute, like if nothing was happening. And the forked process will be executing our code forever. Forever in a secure position of the memory, because we unprotected that memory, and it's over. Now we got our root processor executing our share code. There is another way to do this. That is to do another function call, that is called a map, or memory map. We just ask the system to give us a special person of memory that is not in the stack, but is writable and executable. So, we just copy or share code in that new portion of memory, and execute from there. And in the next context switch, there is nothing that can stop our execution, because it's a, it's a reserver, and a portion of memory. Okay, and this is the end of the exploit. We are now, got our root processor executing our code in user mode. This is a sample of what is, what looks like this exploit. Here is a terminal of OpenBSD, executing and asking for a login prompt. And I'm sending here the couple of packets. Inside the share code, I have inserted a breakpoint of an interruption tree. So, this blue part here, in fact, is the debugger of, the kernel debugger of OpenBSD. The DDB is called. We can see, maybe you can see here that it's not hanging. The kernel has stopped it, because it's a breakpoint. It's not a crash. Here, in the, in here, we ask the debugger to continue to execution. Okay. And here, the OpenBSD system is continuing with normal execution. But now, it's hooked. Now, we got all code hooked in the interruption, interruption 80. Now, I would like to do a live demo of this. I hope it will work. It may be not work, but to make it a little more interesting, I'm gonna do a live demo. . Sure. . . . . . . . . . Okay, now we have launched a virtual machine with the OpenBSD 4.0 Operative System, and we're going to launch another virtual machine using the Linux Operative System that will send the package to this virtual machine. The virtual machines are connected together with a host-only network. It's like a virtual network between the two virtual machines. Maybe we're going to have some problems with the resolution, I hope not. I think we're going to have some problems with the resolution. Could you please make the screen a little smaller? Because I can't see the top of the screen. I can't move the window. Okay, now the top, please. There you go. Let's see. That is. In the right side. In the right side is the shell code, the writing is in Python language, and I'm going to launch the shell code in the other window here. Okay, here we can see that the OpenBSD kernel has stopped because the attack works, and it stopped in a breakpoint because inside the shell code. I have placed it as a breakpoint, and we can ask the debugger just to continue with the execution, and the kernel, of course, will be hooked with our shell code. Now, the OpenBSD kernel has a hook in the interruption, and it's working perfectly normal, like if nothing has happened. But in fact, our shell code is inside the interruption vector. Okay. That was one of the live demonstration, but researching about the IPv6 protocol, the IPv6 protocol is not very much used, and it's active by default in a lot of operating systems, and that is a receipt for a lot of failures and a lot of bugs and vulnerabilities. I will show another ones that I have discovered as a part of my research. Okay, using virtual machines. We will continue with this. These couple of vulnerabilities are for the Linux operating system. Clink. Clink. Clink. Clink. Clink. So, come on! Clink. Clink. Clink. Clink. Clink. We're going to use the Ubuntu distribution, it's one of the most used distribution of Linux. Okay, this is a Linux kernel, the latest one, the 2.6.22 kernel, and it's a custom kernel, kernel this one, okay, it's a custom kernel and I will send a couple of ICMP packets to him to see how he'll respond. Oh, here we can see this is Linux asking for a login. Sorry, I get confused, that was not the attack. Okay. Now we can see that the Linux kernel is hanged. This is because we have sent another couple of IPv6 packets, it's another protocol. But the Linux kernel you can see is hanged. I'm going to show you one thing. Maybe you can see the lights are blanking here. This is... This is how a Linux kernel, a Linux kernel for protection is show with, is Linux is hanged very, very few times, but when this hangs, it just blink the caps lock key. It's very strange to see the Linux kernel hanged, but it hangs sometimes. This is a denial of service attack, and in fact, it's not a server-based, so it's patched. It's patched, but it's in the very latest version of the Linux that has come out. Okay. I don't know, a couple of weeks ago, but it's not backported. You mean, you, maybe you have a Linux system over here. I'm going to attack a completely actualized Ubuntu system to see if they have patched this vulnerability. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay, we're going to launch the last, the same library, the same packets. Okay. Well great, the Ubuntu system survived perfectly. But we're going to launch the first attack, there's some other attack, oh damn. So you got there a full patched Ubuntu system that is still have a vulnerability. All because the IPv6 was activated by default. So this talk is over now, but I want you to say only one thing, that is if you got a Unix system or a Linux system. And you got IP activated and you don't use it, please deactivate it, it's pretty easy to do. Because you can see, there are very bad people out there. And they can do things like this. So thank you. If you have any questions, you can ask them now. I don't know. What portion of the packet are you invading the shell for? What portion of the packet are you invading the shell for? Well, I... The ICMP packet has a data part that is supposed to be replicated in the answer of the ICMP packet. And I placed simply in that portion of the packet. It's a big packet, like a 1,000 byte length. And the shell code in fact is something like, I don't know, 700 bytes. It fits perfectly in the second ICMP payload. It's in that part. I could send the exploit in a lot of other ways, like, I don't know, floating the system with shell codes. The system will place the shell code any place in the memory. Maybe you're going to fill the memory with the shell code. And I can't just jump anywhere in the memory because the shell code surely will be there. Because if I send, I don't know, 10 megabytes of traffic to the OpenBSD system, it's going to put it in some way. In some way. Some place in the memory. Okay. If there is no more question, thank you very much. And that's all. Thank you for coming.