All right, everyone. Thanks for coming to this talk. This talk is titled Smashing the Stack with Hydra. We're from Columbia University's Intrusion Detection Systems Lab. My name is Yingbo Song. I'm the middle author. This is Prathap, and our advisor, Sal Stoffel, is sitting down here in the middle row. So before I begin, even though I'm giving this talk, Prathap here did a lot of the work for this, so keep your scorn or praise upon him at the end. All right. So an overview of our project. So Hydra is a new polymorphic shellcode engine for x86 platforms. We designed this to bypass signature, statistical, and emulator-based IDS systems, basically. It does this by integrating several obfuscation techniques into one engine, such as self-cyphering, statistical memory, forking shellcode, and much more. And I'll talk about these in today's talk. So basics that Smashing the Stack. This is from LF1's seminal paper in FRAC. This is the stack frame. When you call a function, you have the EIP, which is the address of the function that called. This function is what we return to. It sets up the local variables. If you don't have bound checking, you can send a simple exploit with a no-op sled, payload, return zone. The return zone overrides the EIP, and when the function calls return, it jumps into a no-op sled and passes into the payload. And you basically lose the execution context of your program to whatever. So just keep in mind what the three default zones look like for a regular shellcode. No-op sled, payload, return zone. And Hydra basically obfuscates all of these sections and adds more. Okay, so polymorphic shellcode. Why do we need polymorphic shellcode? Well, it's because IDS signatures for shellcode is easy to write. For example, you can detect a string of hex 90s, and that's your basic no-op sled. You can look for a bin SH, and that's an indication you're trying to open a shell. So many polymorphic engines use... an encoder to cipher the payload with a random key. But that doesn't work if your decoder is always the same. And IDS can just try and detect the decoder. So the decoder has to be polymorphic. It has to change every time. There's also statistical IDS systems out there that look at byte distributions, multigram byte distributions. There are also new IDS systems being introduced now based on emulation, dynamic emulation. There's an actual sensor on the network in line with your traffic trying to execute all the bytes that come across the network. And people have actually gotten this to work on 100 megabit line rates, which is very impressive. There's also dynamic disassembly-based IDS, which looks... which dynamically disassembles all network traffic and tries to look for large basic blocks. So normal code, normal data should not have large basic blocks when you disassemble them. And if you find large chunks of executable code, large chunks of instructions, it could be an indication of an exploit coming through. So here's a listing of Hydra's features. We have a knob instruction generator, recursive knob sleds. Everything is randomized, including register selection and clearing, randomized multilayer ciphering. We do junk code data insertion, multi-part type decoders. I'll explain what that means. It's basically we take the decoder, break it up into pieces, and insert it into the payload itself. Multigram statistical mimicry. This is machine learning techniques to make your shell code look like normal traffic. Randomized return zone. I'll explain what that is. It's basically jumping into different parts of the knob sled, forking shell code. So there's some recent work now on how do you safely execute an exploit. When you exploit a process, that process typically hangs. It crashes. Your code may run, but it might also throw off an IDS alert. So this is how to safely execute your shell code and have the vulnerable process continue executing. We also do something called time-lock ciphering for anti-emulators. This is a way to do that. It's a way to do anti-emulator and anti-disassembly techniques. I'll talk about that. Alpha-numeric encoding. The shell code can be pushed down to a printable characters range. All right. So knob sled obfuscation. No-ops don't have to be hex-90. If you look at the KLED engine, for example, they use the ASCII characters A to Z. Any of those characters are actually valid no-ops. You can't use them in line as no-ops because they touch the stack and so on. But you can create a group of these. You can use them in line as no-ops because they touch the stack and so on. You can just put them in front of the payload and execute that, and your payload will still be fine. Hydro contains a no-op generator that basically goes out and builds a library of all possible no-op instructions of multiple byte sizes. We do this by setting up code to test. We write code to set up stack and register canary variables. Then we build a sled using a potential no-op instruction. Then we write validation code at the end to check those canary variables. Then we execute that payload, basically. At the end, if that no-op sled worked and all the canary variables are fine at the end, then that instruction can be used as a no-op sled. We use this to find approximately two million no-op instructions that can be used. You're not limited to just hex-90. There's also multi-character no-ops. For example, if you read the original FRAC article by the KLED team, there's something called no-ops. It's called a recursive no-op sled. What you do is you find all one-byte no-op instructions by brute force. There's only 256 possible choices. Then you find two-byte no-ops where the second byte is the first no-op. It doesn't matter if you jump into the first byte or the second byte. They're both no-ops. Larger no-op instructions recursively contain smaller no-ops. This is why it's called a recursive no-op sled. Hydro distinguishes between two types of no-op instructions, basic no-op equivalents, which is what you put in your no-op sled. You put it in front of the payload, and it basically just catches the execution jump and delivers the execution into the payload safely. The second type of no-ops are what we call state-safe no-ops. State-safe no-ops can be inserted between instructions. These are the no-ops that do not leave the stack in an unsafe state. They don't randomly modify memory. Special conditions are put into place when we search for those. We found about two million total no-op equivalent instructions, the ones you can use to build a traditional no-op sled, and about 30,000 state-safe no-ops. You can see the large amount of variation you can generate in a polymorphic engine. You're not just limited to just hex 90. Register clearing operations. We have many different ways of selecting and clearing registers. Most of these methods involved generating random keys, moving the keys around, and then move registers, subtract, register, and so on, as long as it clears a register at the end. It does a lot of various things before that. Hydra provides a large library of such instructions, and a platform to easily add more. For some operations, the random key is generated to further obfuscate the payload. Multi-partite decoding. Hydra generates non-contiguous decoders. In a traditional polymorphic engine, you have your no-op sled, you have your decoder, your payload, and no-op sled will catch the execution jump, pass that into the decoder. The decoder will pass that into the decoder. The decoder will dynamically reverse whatever encoding method you use to obfuscate the payload. But typically decoders are contiguous, they're just one block of code, and those can be detected by IDS sensors if you try hard enough, signatures or statistical methods. So in Hydra, what we did was break up the decoder instructions and then scatter those into the payload itself. The instructions jump between each other while decoding the payload. So you cannot easily write a signature for such a decoder. Currently, only bi-part decoding is implemented, where we have half of the decoder instructions up front and half in the back, and we have the payload in between. And these two decoder portions will just jump between each other. But we plan to add more true multi-partite decoding in the future. So multi-layer ciphering. Multi-layer ciphering is pretty much standard these days for shellcode obfuscation. We've used CLED, Metasploit, even back to the ADM mutate days, people will just XOR the payload with randomly chosen keys. Even back in the mid-90s, people were using 32-bit keys to do encryption on their shellcode payloads, and that works very well against most AV IDS sensors. So multi-layer ciphering is basically the newer way of doing it. You don't just use XOR. There are a lot. You can use any type of instruction, as long as they're reversible. For example, rotate right, rotate left. You have XOR, add, subtract. There are many different types of instructions you can use, as long as the payload is decrypted properly. If you add a key to your payload, you have to subtract it at some point before in the decoder. So if your encoder uses an add, your decoder has to use a subtract. And in Hydra, the cipher order is dynamic. Every time, it's completely random. You don't get the same cipher operations in each run, and you don't get the same keys in each run. And we'll have demos of this at the end. We use 32-bit keys, and it's generated randomly per invocation. Hydra uses six rounds of ciphering by default. I just lost my screen. Hold on a second. Okay. It's back. It's back. Six rounds of ciphering is used by default, but the user can specify a number. Inline junk code insertion. Hydra automatically spaces out your shellcode payload, basically. So you give it your shellcode. It takes that and spreads the instructions apart. In between the instructions, we can insert junk. We can insert arbitrary data, basically. And what we do is we typically insert no op instructions. You can also insert anti-disassembly code, random junk. Or you can use this section for statistical memory attacks, which we'll talk about in a minute. So statistical IDS sensors are the new way of detecting shellcode. Basically they learn statistical models for normal content and try to detect exploits. What happened? I think I just lost my screen again. Oh. Okay. Statistical memory. So Hydra actually uses machine learning based techniques to make shellcode look like normal traffic. Hydra, all you have to do is provide Hydra with instances of normal traffic that you want to mimic. And it will build statistical models for multigram distributions. So what is the frequency of certain character distributions, certain two-by character distribution, three-by, up to five, seven, and so on. And it will take your shellcode, build a model of it, take normal traffic, build a model of it, and it will tweak your shellcode until the two models look very similar. And it uses machine learning techniques to do that. Unfortunately, I don't have time to explain that. But if you want to learn more about it, you can do that. And if you want to know the details of that, just find me afterwards, and I'll point you to some papers. So we do this using Markov chain Monte Carlo. It's basically a machine learning technique to sample, build a distribution, sample from that distribution. And if you recall, we have the junk code insertion feature. When we space out the instructions, we take the statistical memory bytes and put it in those spaced out sections. Randomize. Return. Return zones. So the randomized return address zone is basically a sequence of repeated target addresses. They point to the NOP sled. When you write a simple stack exploit, you're hoping that one of these addresses overrides the IP on the stack. When the function returns, it jumps into your NOP sled. And the basic way to randomize this zone is to just add random offsets to each of the individual address components. It breaks signatures by completely randomizing each component of the address zone. So emulator-based IDS systems are the very new, newly introduced techniques. They exist mostly in the academic communities. What you do here is you basically build a stripped down x86 emulator and dynamically execute all network traffic and look for self-decrypting behavior or large basic blocks. And to defeat this, we basically use something called syscall-based ciphering. Where we exploit some type of OS functionality to grab a key, which is used to decode the main cipher operations. So Hydro uses the time syscall. And our shellcode will actually call, make the time syscall, get the return value back, and use the most significant bits of that result as the key to decode the main cipher operations. So if you recall, we have the main cipher operations, like xor. Those instructions are ciphered based on the key we get from syscall. Okay. If the syscall is not handled as most emulators can't, then the shellcode cannot be decoded properly and it will look like random traffic. But when it gets to the actual host and the OS can handle the syscall, then the proper shellcode is properly decoded and it will run. So this also introduces the concept of a shell life, which is where a user can specify how good, how long of a window this shellcode can run for. Because we use the time syscall, we can specify, say, seven most significant bytes and that gives you five minutes for when this shellcode is good. After five minutes, the shellcode will not decrypt properly and it can't be disassembled. Well, it's hard to disassemble it. So network IDS can emulate all possible syscalls. That's basically the main idea behind why we did this time cipher idea. It bypasses the syscall. It bypasses emulators and it bypasses dynamic disassembly-based methods and it slows down human reverse engineers because it's very hard to figure out exactly where these little tiny mechanisms are. These are very small mechanisms that we put in a few bytes at most. So forking shellcode. This is something that just got recent attention at CanSecWest. Some people from Immunity Inc. presented a talk on this. We did this at the exact same time. We didn't know that they were also working on it. So basically, you have an exploit. You have a target process. You exploit that target process by a stack exploit or a stack overflow or something. And what happens is that the target process will crash. Thanks. The target process will crash and that's not a desirable result. What you want to do is have that process keep going simultaneously, have your exploit execute. Because if the process crash, if it starts doing weird things, then that might alert a sysadmin or some sort of AD sensor. So the solution is that we have mechanisms in Hydra that adds forking features to your shellcode where once the shellcode executes, it immediately forks. The child executes the payload and the parent attempts to recover the exploited process. It tries to repair the stack. It tries to figure out the right return address so that the parent can actually go back to normal execution. And we'll demonstrate all of these at the end. So this feature is very useful. It's kind of hard to get right. Recovery is very hard. Once you exploit the process, you typically lose EIP. So you have to kind of do some disassembly on your target to figure out exactly where the offsets are, the proper offsets are. You have to understand the target address space. But if you know how to do that, then Hydra adds all this forking feature in automatically. So alphanumeric encoding. Most polymorphic engines these days use alphanumeric encoding. It basically just drops. Drops all of your shellcode into printable characters range. We use the alpha 2 encoder. Those functionalities incorporate into Hydra. And we also have the alphanumeric no-op generators. And the alphanumeric no-ops are incorporated into the alpha 2 encoder as well. So the main benefit of using Hydra is that it has all of these different features. They're all modular and they all work together. So this is, if you see on top, that is the traditional shellcode, no-op sled payload return zone. And then you have Hydra shellcode, which is you have this recursive sled, alpha decoder, time-lock cipher, fork, and you have the payloads and the decoders all scattered around. So the goal of this is basically make it impossible to recognize that what you're looking at is shellcode. It's impossible to use the signatures for this. It's impossible to use statistical methods for this or emulator methods for this. Well, maybe not impossible. But very hard. That's the goal of this work. And I'll pass it off to Prathap, who will demo this. All right. I'll be showing three quick demos here. The first one, if you just run Hydra without any command line options, you get a decryption loop. That's the multi-level cipher operations that will be used for encoding the shellcode. And the second one would be... If I... This is a small shell script that runs Hydra three times and shows us the alphanumeric encodings of the shellcode, such as the invocations. We see that this is one payload. And this is the second payload. And this is the third payload. So we see that the content and the length of all the three strings are different. And this is because we're using two levels of polymorphic encodings, first at the binary level and the second one at the alphanumeric level. And the third demo would be multi-threading test. So we have three... This is a small program that we're going to exploit. We have three functions, main, A, and B. So main calls A and A calls B, and B is a function that will be exploited. And the goal of the multi-level, I mean, multi-threaded shellcode is to return back into main, skipping A from B. So... That is, from B, you return back into A, and we should be able to see the in-main string. And this is the vulnerable... This is where we're exploiting the program. This is the shellcode. So I just compiled it already. So we see a shell here. As well as we come into B, we skip A, and we come into main. The reason why we skip A is because the payload... We would actually overwrite the EIP of A. So we just have to get the second last EIP, and that's the reason we come into main. And the third test would be the time lock test. I just invoke Hydra minus seven. So minus T tells Hydra that we need a time-locked shellcode, and seven is a precision. And we get the shellcode here. So we see that the current time is this, and depending on the precision, the time the shellcode expires at this particular time, we would actually want a higher precision, I think. Let me just invoke it again. This is too small. Let me get eight. Yeah. So the time increases now. Okay. Okay. Okay. Yeah. So just copy this. Oh, sorry. I don't see, though. Okay. It's too long. Yeah. All right. So apparently this last demo was a bit hard to pull off. Yeah. The resolution is kind of messing up with the ‑‑ do you have time? Time test. Let's see. This one. Yeah. One moment. Yeah. The screen just turned off. Okay. So ‑‑ Yeah. Basically, like, when you put in an argument for the time-lock shellcode, only within that time frame the shellcode would be executable. Okay. And after that, the decoder loop would fail and ‑‑ All right. Thanks. I think we're out of time. Yeah. We should get off before they drag us off the stage. All right. Thanks. We'll be around. If you want more ‑‑ if you have any questions or want to see a demo, just find us. We'll be walking around.