It's about time, so I'm going to go ahead and get started. My name is Chris Eagle. I am on the staff at a place called the Naval Postgraduate School in Monterey, and today I'm going to talk about sort of attacking obfuscated code using IDA Pro as the analysis tool. Go through the introduction, talk about the operation of this tool that I'm going to introduce, and then we'll do some demos of the tool and work. First thing, I highly recommend that you move closer to the screens, not in, because I guess we have two screens, so you can move out. But I'm going to be doing some demos in IDA Pro, and you cannot change the font size in IDA. So we're going to get what we get, and if you're up close, you'll probably see the stuff, and if you're in the back, you're going to probably strain your eyes or miss the finer details. I'll try and talk through it all. If you want to stay back there, that's fine. Okay. So a little bit about IDA Pro. IDA Pro, for those who don't know, hopefully most of you all are familiar with it, is perhaps one of the premier reverse engineering tools that's out there. It's a disassembler that can handle many families of assembly language, understands many executable file formats, and automates a lot of analysis tasks. It really makes life easy on the reverse engineering front. It's a Windows tool, so forgive me if you're not a Windows fan, but that's why I'm in. I'm in Windows today, so I can run IDA. As I understand it, and there's probably smarter people out here, they are working on a Linux, at least, analysis engine. Maybe not the GUI side, but it may be running on the Linux side of the house in the near future, or not so near future. It depends on when it is time to get to it. So what I'm going to talk about today is a plug-in that I've developed, and for lack of anything better, it's this x86 emulator plug-in. And what it does is it allows you to do some emulated execution of instructions within IDA. So basically, you're looking at the disassembly in IDA. Only x86 is all I ever intend to work on. But one of the things I find useful when I'm reverse engineering, A, I end up doing a lot of hand tracing through code. What is this code doing? Keeping track of register values and whatnot. And I had always wished I had something that would do it for me. Just step, step, step. Boom, you know, what's in EAX now? Okay, well, you can do emulated execution. You can see the code right there in front of you. Let somebody else execute it, and that's what the plug-in does. Okay, it's written in C++. It's a visual C++ project at the moment, and you can get it for free out at SourceForge right now. I think the latest release of the tool is also on the conference CD. Again, you know, why I did it, hand tracing through assembly language of pain. So, anti-reverse engineering techniques, which is why I really developed this thing, work very hard to try to obfuscate the code. Okay, so whether they're doing really unusual things, whether they're obfuscating the code path, jumping to the middle of instructions, self-modifying code, all this kind of stuff. If it's self-modifying code, IDA is just going to show you the original unmodified code. Okay, so how are you going to get at the underlying code once it's been unrolled at runtime? Yeah, you're never going to do it in IDA unless you modify it. Modify the stuff yourself. You can do it with IDA scripts, or I've taken an angle of doing it with a plug-in. So, you know, there's a lot of reasons you might obfuscate code, and I'm not going to get into the merits of any of them, but a big place you see this stuff these days is in virtually any worm or virus that comes rolling around the Internet is usually obfuscated in some way. Okay, there's a couple of examples there. UPS. PDX is pretty common. TLOC. ASPAC. In fact, IDA itself is protected with ASPAC. So if you want to reverse engineer IDA, you've got to get through the ASPAC protection. In the Linux world, TESO released BurnEye a few years ago, and there's another tool out there called Shiva. We don't see worms protected with this stuff so much, but it does the same sort of thing, right? It obfuscates code and makes it difficult to see what's underneath. So whether it's hostile code or not, you still have to fight through this, this layer just to see what might be underneath. So the reason to do this is to get at the underlying executable. The last thing you want to do is spend your time reverse engineering the obfuscation piece. Most of these things start up, you have this obfuscation piece. The first thing that's going to happen at runtime is the deobfuscation runs initially that reconstitutes the original binary in some form and then passes control off to that binary. Now, Shiva takes a little bit of time to do that, but it's not going to happen. It's a little different approach. But essentially, you've got to unprotect the binary before you can ever pass execution to it. So we want to get at the stuff underneath. Again, worms, viruses, so it's nice to be able to get at that stuff quickly so that you can turn around and the antivirus vendors develop in signatures or what have you. Maybe you want to know if there's hidden functionality, backdoor functionality in the worm or the virus, and you want to get at it for further analysis of the code. I mean, it's easy to see that. This thing propagates. It's easy to see that a worm propagates through a particular vulnerability, perhaps. But the more detailed analysis tells you whether there's some time-delayed feature in there like Code Red had that was going to do the denial of service on the White House or whatever it was, or whether there's any other backdoor features that aren't apparent in the worm's propagation mechanism. And so that's the challenge, and that's why I developed the plug-ins. It's to work through in IDA, without running a binary, a malicious piece of code, to get to the real reverse engineering, which was what is the underlying code doing. So the way this thing works is, in IDA Pro, for those who have never used it, I'll go through some demos, but you load a binary up into IDA. It does a lot of analysis, decides what type of executable it is. Is it, say, a Linux ELF? Is it a Windows PE executable? Parses all the headers, parses the different segments and so on, and then does the disassembly. So it goes to the entry point, basically, starts from there and takes it apart, disassembles, recognizes functions, recognizes stack-declared locals, recognizes function parameters, and so on. And it makes a lot of annotations. It's really, really nice. And what it's doing all along the way is it's taking basically every byte out of that binary image off your disk and sticking it in a database. So IDA builds a database, and then you as a reverse engineer are interacting with a database. You're not really interacting with a binary anymore. You're making annotations into a database, and that's all it is. Every byte is marked it's executable or it's data. You can reformat things, but IDA is essentially a pretty front end to their database, and you can make annotations and keep working from there. So it doesn't run any code, although the new version has a Windows debugger built into it, which I've never really played with. But that doesn't help you if you want to look at Linux code anyway. Okay, obfuscated code gets tougher, because all you're going to see is the de-obfuscation piece, because that's the only executable piece that sits on the file. Everything else is data from an immediate perspective. You have the de-obfuscator sitting there. That's what's going to run. You have all the data that's been obfuscated in some way, whether it's been just encrypted or encoded or what have you. And so IDA will show you the de-obfuscation code, the obfuscator portion, but everything else is just going to group together as data. Okay, hard to reverse engineer that stuff. And usually you're only going to get sensible output in IDA for the entry point function, and then it's going to start falling apart. And if they do things like jumping in the middle of instructions, IDA really can't follow that. It's got to find some instruction boundaries. It can't show you the same instruction twice split in the middle of an instruction for different instruction starts or so on. So, you know, IDA, we've got to work through that. And if you're going to use IDA to reverse engineer, we've got to get through that easily. You can do it manually, and it's kind of a pain. Okay, anybody who's done it can tell you. Now, the plug-in that I developed has two pieces. It's a user interface piece, and I'm not a user interface person, so you can laugh at it all you want. That's fine. And I will generally chalk up, you know, quick and dirty user interface hacks, you know, as ugly. And it got the job done for me. And I'm happy with that. And I can work on that in the future. But the user interface is all supposed to be Windows GUI-specific, you know, code. Okay, it throws up the dialog boxes that this emulator needs and so on. And then there is an x86 emulator piece, okay, which handles instruction fetching and decoding and executing and so on. And it interacts with the database to pull out different bytes, you know, instruction bytes and so on. And the emulator piece is mostly platform independent. I'm trying to keep it platform independent so that you can actually do, you can pull it outside and do stand-alone emulation. And you can actually integrate it into your own sort of stand-alone unrollers, okay, which is what I did. And there's reference to a talk about reverse engineering Shiva, which was this Linux protector. And I took the emulator out of this, just the emulator code, and used it in the stand-alone to create a stand-alone unwrapper for Shiva. So that's why I'm trying to keep the emulator separate, so I could pull it aside and run it, say, on Linux, if I wanted to. But what it's really designed to do is execute a single instruction at a time and then pass control back over to the GUI side, so you can step through this code. And, you know, we can talk on the side on why I didn't go find any other. There's plenty of x86 emulators out there, and I'll talk to you offline if you want to know why I did my own, okay, sort of reinvented the wheel there. But the integration, you know, is really important. Integration is what's sort of new here. So the GUI gives you this console. If you're in IDA, and we'll see samples here, it throws up this console. And it's sort of like a pseudo-debugging console, okay? It's going to show you register displays, and you can drill in, and you can get at the segment registers if you really want to. It's going to show you the only memory display it has at the moment is the stack display, which sits down there at the bottom. Over to the right, you have some control buttons. We can step through code. We can just jump over code or jump to a specific code. There's a specific spot in the code, which is just resetting the instruction pointer. You can skip over code, just, you know, skip a statement entirely if you don't want to descend into a subroutine, or more importantly, in dynamic-linked code, IDA is only loaded up the binary, but you don't have access to any of the DLLs, okay? So if you're going to descend, if you see that the code makes a call to a library function, you don't have the disassembled code to that function, we can't execute it. So you can just skip the call entirely. Of course, you have to fudge it. You have to fudge the results in EAX, perhaps, in order to be able to continue. You can skip forward, set the cursor somewhere in the future, and run the cursor, and cross your fingers that you get there, because if you don't get there, you've basically locked up the emulator, okay, while it's fetching from who knows where. Okay, that's, you know, an improvement that can be made. And then hide just makes it go away. For manipulating stack data, the push data button down towards the bottom, it actually lets you push your own data onto the stack, which is kind of nice. If you want to just run one file, you can do that. If you want to just run one function out of the program, then you know it needs some arguments. Okay, push the arguments on the stack, and then start stepping through the program. And the program will retrieve the arguments off of the stack. And I'll explain the memory layout, memory architecture of the plugin in just a moment. So using this thing is, it's brought up in IDA with Alt F8, and this hopefully will all become clear when I start working through demos. Wherever you have the cursor, when you bring up the emulator, that's where EIP is going to be set. So you can, you'll start stepping. If you want to, from wherever the cursor is. And then you just, you step and go, okay? And the plugin will interact with IDA. It'll, you know, fetch a byte out of the IDA database, which it's going to assume is code, because you told it to, you told it to step there. The emulator then decodes the instruction, fetches more bytes from IDA, if it's a multi-byte instruction, and then modifies all the registers accordingly. On that control panel that you just saw, the registers will all update as you step through the code, okay? And every time you step, the emulator also tells IDA, okay, this is going to be code, because EIP is pointing at this thing. So the emulator will tell IDA, now reorganize whatever is at this location to look like code. So if you're jumping into the middle of instructions, then the emulator will automatically reorganize your IDA display to mark the new beginning of an instruction. It undefines the old instructions. It unselects the new instruction and redefines a new instruction at the current EIP location. And I'll show you a lot of this sort of code reorganizing, jumping to the middle of the instructions in some of the demos that are coming up. This just describes, well, the run to cursor. I don't do breakpoints yet, okay, but in theory, you could have a list of breakpoints. Just go to any address in IDA and then add a breakpoint into the emulator and then run until you got there. That's really not easy to do. All you have to do is monitor EIP. The plugin supplies its own stack. Remember, IDA is just a binary image that sits on the disk. Okay, so that's a lot of code and some static data, but your heap doesn't sit in that file and your stack doesn't sit in that file. So if you want to really run through any code, you're going to need perhaps a heap and at least a stack, okay, available as you fetch instructions. I mean, push anything and that's got to go somewhere. You're not going to push that data into the IDA database. Okay, because that's really not what's sitting on your file. So the emulator provides a stack and it looks at memory references and sorts them out based on where are we retrieving from, where are we writing to, where are we reading from, makes a decision. Is this code? Am I going to go fetch this out of the IDA database? Is it stack? Am I going to go throw it in the emulated stack? Is it out in the emulated heap? Okay, so the plugin supplies both of those two things. It requires some manual setup. It works. Limitations on a plugin, it's slow. It's emulated execution through a database. A, we've got to do all this database interaction to fetch things and B, we're emulating every instruction. So don't think that you're going to get high performance execution of these executables. But the real goal is to just get through the obfuscation, which should be done fairly quickly and leave you to reverse engineer the underlying binary. Okay. Why not follow calls into dynamically linked functions? Because IDA doesn't load that code up. It doesn't load up DLL. There's nothing more I can say about that. But if you're calling, say, printf, we don't have the disassembled version of printf to descend into. So that's a limitation on this thing. And IDA won't load them up at the same time. Can't follow system calls into static and linked executables. Say you're looking at a Linux binary. And you're doing some int80s. You descend down to an int80. It's not going to follow that system call. So you're going to have to fake the results and throw something into EAX and press on if you can. So the emulator memory, I sort of went over the layout of this. Code gets fetched from the IDA database. So that's database interaction. And then the other references are resolved to either the emulated heap or the emulated stack. Every memory reference is checked. So we could actually output information about, you know, where are we reading to and writing from, or vice versa, or whatever that was supposed to be said. In the heap, you could add sort of valgrind-type analysis. You can actually observe that you're flowing out the end of a heap-allocated block. You could potentially see some heap overflows in that way. Because we can mark every access to and from the heap. Although it's just a toy implementation of a heap algorithm. It could. It could be replaced by any of the actual heap algorithms, or heap management algorithms that are in use in the world. Memory layout you control by bringing up a little dialog box. And you can set your stack top. You can set your stack size. You can set your heap base address. You can set a heap size. And it'll just go grab that memory. It's not very smart about growing the stack if it needs to, or growing the heap anymore if it needs to. But if you set reasonable limits. In here, you should be able to accomplish what my primary goal here, which is unrolling the obfuscated code. Emulated stack operations all go to the stack. So pushes and pops and things like that all interact with the stack. The stack contents are displayed down in that bottom scrolling window. Currently I don't have, the heap was one of the last things I added in. I don't have a way to display heap memory at the moment. Okay. You can always display code. You can always display code. In IDA. You can scroll through IDA and see what the code is looking like. You can scroll through the stack window and see what the stack is looking like. But I do need to come up with a more generic way of looking through memory so you can see the state of the heap at any moment. Let's see. And you do have the capability of pushing data onto the stack sort of outside of program control. Let's just throw some data on there. Grab some stack space if you want to. Push some parameters. And then step into a function. Okay. All right. Looks like this. If you want to push some data, you can throw some data up in the, it brings up a dialog box, asks you what the data that you want to push, puts your numbers in there. It's kind of like calling a function. Okay. The stuff gets pushed in right to left order. So you put on the parameters just like you want to call a function. Okay. And then the stack, the stuff gets jammed onto the stack and the stack pointer gets updated accordingly. Okay. So that's all sort of integrated. Emulated heap is just a real simple linked list memory allocator. It doesn't do inline control information. So if you do heap overwrites, if you see any of that stuff, or if you were able to look at heap memory, you wouldn't see links to the next free block, next empty block, any of that stuff. It's not in memory. It's just a simple way to satisfy memory requests that are made by the binary being looked at. It can detect access outside of allocated blocks though. So in theory, you could watch and see if the heap overflow is going to take place. Function hooking. So while it doesn't descend into dynamically linked libraries, it turned out that one of the unpackers I was looking at did want to call a malloc-type function. And so I was out of luck. I couldn't get any farther unless I could implement the malloc. So A, I had to implement a heap, and B, I had to hook the heap alloc call that was being made in this Windows binary. So I added sort of function hooking, and I've mimicked a few, very few, library calls that I found I needed to get through this one particular protection. And what happens, so then if it's going to call heap alloc, if you see, hey, at this location it's going to call heap alloc, you can hook that call to heap alloc and divert it into our emulated heap. Heap allocator. And so any time it says call heap alloc, then you jump out to the emulated heap and it returns you a pointer to a block of memory the size that you requested. It looks into the stack as you've set it up for that call. And so if it occurs in a loop or as it occurs over and over again, now we're allocating memory without having to have a library function available. And you can also just run the functions if you want to. If you just want to run the functions, you can do that. If you want to run malloc and you want to have a block of, say, 1K, you can just push the number on the stack and then just run malloc and you'll get a pointer back in EAX. And then you can use that pointer manually later on if you know you need a block, a pointer to a buffer to pass into, say, some other function. But the automatic function hooking is a little more interesting, and we'll see that in one of the demos coming up. A manual function hooking just looks like this. It's not really manual function hooking. It's just sort of running a function. Choose a function. If you want to run, make sure you push the parameters on the stack, and then just select the function that you want to run off the dropdown, and it'll run the function, pull the parameters, and give you a result in EAX. So you can sort of play along. No change to EIP. We're sort of executing that function outside of the binary for administrative purposes, like to grab a large block of memory out in the heap that we may use later on. Automatic function hooking works like this. We go choose an address. Actually, I think I changed the way this is implemented. But what we would do is we set the address of the function. So here you see a place where it does a call to malloc. We're going to change that address so that not just this call, but every other call to malloc would get diverted into our whatever function we hook in its place over here, malloc in this case. There's only those five. Five functions were implemented, because that's all I needed. And again, I'm going to demo that coming up. Another place I had to struggle was there is a – some of these unpackers like to generate exceptions for sort of anti-debugging purposes. So a lot of the Windows unpackers will throw exceptions left and right. Divide by zero, they'll throw in threes. They do a lot of strange stuff. And so to get through those, the emulator has to – the emulator has to do a lot of this to handle these exceptions in a manner that the binary expects. Well, these are Windows binaries, and they're expecting to get sort of the Windows-structured exception handling data thrown on the stack and then jump to the exception handler. So I threw that in there. The emulator will do that. If it's a PE binary, then the emulator will try to catch exceptions. And when it sees the exception, it'll push all the stack data required for an exception. So if it's an exception handler in Windows, follow the exception handler links through FS colon zero and try to transfer the control to the exception handler, which then can play with all the data, all the saved registers that are thrown up on the stack before returning control to the binary. And we'll work through that a little bit. And it only recognizes a couple things. It does the divide by zero right now in three single-stepping, the trace flag being turned on. The debug registers, which is, again, because T-Lock insisted on doing all that stuff. The emulator program has to set up an exception handler, obviously, to use all of this stuff. And then the emulator creates the data structures and throws them on the stack before transferring control to the exception handler. Okay, so demos. We'll start off with UPX because it's the easiest, and it's the most likely demo to succeed. It's a pretty common obfuscator. It's not terribly sophisticated. Because all it really is is a compressor. It just compresses the binary, throws a decompression stub on the front. And so when you run it, it decompresses and transfers control back to the original binary. It's very straightforward. They don't make any attempt to obfuscate themselves, obfuscate the decompression algorithm. And in fact, if you don't take measures to prevent it, UPX will. Unroll a UPX protected binary. Okay, UPX will reverse itself at the command line. Okay, so there are other tools out there that will tweak a UPX-packed binary so that it at least breaks the UPX part where you can't just use UPX to unwrap UPX, a UPX protected binary. But it's really simple. There's a simple, straightforward loop. There's really no tricks involved, and it's really no problem for the plug-in. And the only place that I wish... you know, I could do better is the import table. All these things, after they get done deobfuscating, they need to go back and rebuild the import tables. Okay, they have to sort of do their own linking because all that stuff has been hidden from the operating system linker when the program is loaded off the disk. So I don't rebuild the import table here, but it's not too tough to do, either with a script or manually. So let me see if I can get out of this. So I'm going to go ahead and find a UPX binary on the list here. Bear with me. That's not going to get any bigger. Come on. Hmm. I'm lost. I'm locked up. Okay, this thing is a Slackbot IRC Trojan, and we'll just call it malware here. We load it up in IDA. It correctly identifies it as a Windows PE. It tells us that the import segment seems to have been destroyed, which it was because the thing was compressed. And we let it open up, and the resolution on this thing is so low that I hope we don't have problems with the demo. The emulator is going to take up the whole screen, I'm afraid. Okay, so IDA gives you a names window over here, any names that are recognized in the program, and we see a few. There are a couple key imports that function, or some unpacked. Backers need, you know, the standard sort of Windows stuff, load library, get proc address. Over here, you get a strings window, and mostly we see that it's full of garbage. Okay, so there are not a lot of recognized strings right now in this binary. Okay, the strings windows is helpful in de-obfuscating something, because usually if you're successful in de-obfuscating something, you will unravel, strings will become available. Okay, so it's sort of a hint that you're maybe on the right track. So we're set up to go here at the entry point of this program. This is going to be really tough with this resolution. I should try to remove all these window, or these toolbars. See if I gain some more space. Yeah, we're not getting very far. Let me bring up the plug-in, see what we get. Takes up the whole damn screen. Can we get any better resolution out of these monitors? Nope. Okay. Okay, we'll run it way down here. We'll start stepping through code. Okay, you can see, so I hit the step button. The cursor advances over here in IDA. Okay, we're on the next instruction. Okay, we see some stack activity has taken place. It was a push all instruction, so the stack has grown. ESP is updated and so on. And we can step, and you'll see that the registers change and so on. Okay, the stack is growing. Okay, we're working our way down through this thing. But it's going to be a lot easier if I just descend. And you can see it's very straightforward. The code paths don't appear to be obfuscated. Okay, we can see all the loops. I don't know if you all can see these branching things. They're kind of in a bad shade of gray over there on the side. It's just control paths in IDA that display the branching. So we work our way down to a point where we encounter this call instruction. And why is that dangerous at this point? Because we don't know what's going to get called. Okay, so as I walk through this stuff, a call to code whose address I don't know right here because it's going to be dependent on whatever ESI ends up holding at this point, that's sort of a danger point for me. I don't want to run any farther than that. For all I know, it could be a library function. And in this case, it actually is. So I'm going to back up to this point right beside it. Before I move on. Before the loop. Right here. We're going to get back, maybe... Okay, back to the emulator window. And I'm just going to run it to the cursor. And you saw it thinking there for a second. It ran all the way down to here. Okay, I know this is terribly exciting. And the question is, did we get anything done? Well... This loop, it turns out... And I won't go into it. You still have to do some reverse engineering. But this loop is what rebuilds the import table. Okay, after UPX has decompressed it. So we're going to skip all that because I don't have get proc address available. Okay, so we're not going to be able to rebuild the import table. So we'll run down here to the end. Okay, these last couple of instructions. Pop all the registers off the stack. And then jump up to some other location. Okay, well this is... At that point, the code is done. So I'm going to jump down there. It changes the EIP to that cursor location over here. We step through a couple locations. The EIP will make a big jump. Okay, and now I'm somewhere else. I'm way up here. Okay, you can see where it's pointing. And... Everything appears to be... It doesn't look like it's code. Okay. But IDA hasn't been asked to reformat this yet. Okay, this is code that used to be data. Okay, the stub is transferring control to the newly unpacked code. Okay, we've transformed the code in IDA through database interactions. And as soon as I step once... Okay. The emulator tells IDA, hey, reorganize that. That's code. Okay, and many of you will recognize that's a standard function prolog. Okay, right there. And we can in fact... Working with IDA now, let's turn that into a function. Okay, and things start to look a little bit more interesting. And at this point, we can actually start reverse engineering the program. And ultimately down here, we call main. This is a Windows binary. But one of the interesting things that happened when I stepped into that function, when I called it code, IDA does more analysis. And you can see it's reanalyzed the whole program at this point. And it's showing up a lot of strings that were not there before. Okay. It shows those are the same old imports that we had before. Okay. But we have a lot more information to work with. And if we go back and rework the strings window, which does not get automatically updated. Okay, we still see a lot of garbage over there. Okay, if we ask it to rescan for strings. Okay. Okay, now we have a whole lot of other stuff. Okay, somewhere. Let's see. Okay. And so you see some of the static strings that were previously hidden. Okay. And these are all slack bot strings. You see IRC type stuff and so on. Okay. So that's UPX. And as I said, I really wish I could make this thing not so small. I mean, this thing is too big and I don't think I can resize it. So we're going to hide that. We'll get out of UPX. Open something different. Okay. And clearly you could save that and continue working. And now it becomes a reverse engineering problem, a reverse engineering slack bot. Okay. Which that happens to be. And so, you know, but you're beyond the obfuscation. You can get onto the meat of your work. Okay. So next we'll take a look at ASPAC. See if I can do this right. Okay. So this is a GAO bot. It's another IRC type trojan. Comes along in a lot of emails. And same problem, broken imports. And this code is a lot more challenging. Okay. IDA gives you about three statements. And if we go look at this, in fact, we see that the first instruction, well, after the push A, we're going to call into the middle of another instruction. Okay. We can go up to this location. But you see how it says location plus three? Where am I? Okay. Because 455007 is right here. Okay. But after the call instruction, remember, IDA is just parsing instructions. The next instruction, right, the call takes some data. The data is an address. And the next instruction boundary would be this jump near pointer 458251. 45825, whatever. Okay. The next instruction that IDA can show us isn't even going to be executed. Okay. Because this thing says the next statement to be executed should be at 45500A, right? Okay. Which is in the middle of that jump instruction. Because the jump spans 007 to 00C, right? Okay. So how does the emulator handle that? What happens then? What am I doing? ASPAC, right? Okay. So I've got to consult my cheat sheet and make sure I don't blow the demo. Okay. Oh, come on. Okay. ASPAC is one of these tools that needs a heap. So the first thing I need to do, and why I don't do it automatically, don't ask me, but is we need to set up a heap. And I'm not a Windows programmer, so I don't know where Windows heaps live. Okay. But that's good enough for me. Okay. It's outside the programmer space, and it's outside the stack space, and it'll work for our purposes here. Okay. So we have a stack, and we have a heap. Okay. And that's what ASPAC's going to need. Yeah. This one doesn't need it. Okay. So I'm going to step through the code. Okay. Make sure that I'm probably not where I want to be. Okay. I need to get up here. I need to jump to that location. So we start execution from there. 455001. Everybody sees that? Okay. Hopefully. And we're just going to start stepping. And I'll push this way down, and maybe we'll see the way that the emulator handles this sort of code. So we just did the push all. You see the cursor jump down. The next thing we're going to do is the call to the middle of the instruction. So we hit enter. We do that. Okay. And you see it reorganize the code. The jump went away. Okay. The first three bytes of the jump are now just anonymous data right here. Okay. The call was reformatted. Okay. It's no longer 07 plus 3. It is now correctly 00A. Okay. And the next few instructions at the point that we just jumped to are reformatted as code. Okay. So we can continue to step through these. And we work our way through. And it's just going to keep going wherever it needs to go. Here's another call into the middle of an instruction, 13 plus 1. Okay. It reformats. Okay. So now we're down to pop EBP. Okay. Continue to step, step. And I've got to be careful here and see where we are. Okay. We're up here at 35. And you can see I have, again, what I consider one of these danger points coming up. Okay. A call to who knows where. Right. Something that's completely reliant on whatever EBP happens to be at the moment. Okay. So I'm going to step again. Okay. So we're going to push EAX and call this function. So whatever parameter I need to sit in EAX, which I can examine, I can see it's 455441, whatever that means. Okay. And I could use that address in IDA. I could go examine that address and see what is the parameter to this function. Okay. And I can also just do a computation. Here's EBP at 455013. Okay. And I could add this F4D to that. And we can figure out where are we going. Okay. Well, I did it in advance, fortunately. And we are going to, I think, let's see, 455. I blew that. Okay. 455F60, okay, is what the math works out to. If we take EBP and we add on F4D or whatever that is, that's where we're going. So in IDA, we'll jump down there and see what it is. Okay. Well, that's the import for this unpacker's get module handle. Okay. So the unpacker is trying to get a module handle. Great. So we're going to skip that function call. All right. Let me see where we are. Okay. I'm just going to skip these two statements. I don't care about them. I can't call get module handle from this emulator. Okay. So I'm going to come down here and skip two statements. I'm going to skip the push. I'm going to skip the call. I'm going to skip moving EAX because it doesn't hold anything anyway. Okay. Skip the move EAX to EDI. And we're going to continue. Okay. And so at this point, we see the same sort of situation coming up. Okay. We're right here. It's going to do EBP plus F49. Okay. I have the same problem. So the emulator doesn't fix everything. We still have to do a little bit of investigation. But where are we going this time? Well, F49 is just four less than F4D. Okay. So we can jump down to roughly the same spot. Okay. And see that four less than get module handle is get proc address. Okay. So now it's going to call get proc address. Okay. Well, what proc does it want? What procedure is it trying to get the address of? So we jump back. And where am I? 4A. Okay. So we're right here. Okay. So we need to get EAX and EBX will be our two parameters to get proc address. Okay. But EAX is just the module handle. So the interesting thing here with EBX should tell us the procedure I'm getting the address for. All right. So as long as I load up EBX here by stepping. Okay. EBX is 455071. Okay. And we go over here and scroll down a little bit to 455071, which happens to be right here. And if we reformat it, you see they're trying to get the address of virtual alloc. And very shortly, they'll try to get the address of virtual free. Okay. And so, you know, this motivated me to develop the heap. And we'll continue from here. But we're going to skip that. We don't care. Okay. Well, now we do care. Because this is where function hooking gets interesting or comes into play. Okay. So here's where we are. We need to – no, we're not there yet. Okay. We're up here. Getting ready to call get proc address. So we're going to skip it. Don't care. Okay. At this point, back from get proc address, though, I have what should be the address for virtual alloc. Okay. And what I'm going to do here is I'm going to hook something. Okay. So the address of get proc or virtual alloc should be an EAX, right? Okay. So I'm just going to throw in a one there for kicks. Okay. So something that's out of range, out of bounds. Okay. And then what I'm going to go do over here on the emulator is I am going to hook a function. Okay. Program location one. Okay. And the available functions. There we go. Virtual alloc. Okay. So if we ever call one, okay, we're going to jump aside and call the emulated virtual alloc. Okay. So I trick get proc address. Okay. We're going to shove a one. We're going to set a one in its sort of import for virtual alloc there. Okay. And whenever we come back, now as I step through the code, okay, we'll save the one as a result of get proc address. Okay. And in the future when we see a call to one, we'll step aside and we'll allocate some memory. Okay. So same thing down here. F49, F49. We're going to call get proc address again. What are we calling though? Look at EBX. Okay. EBX is now 07E, which is virtual free. Okay. So same thing. Okay. I want to now skip the push there. Skip the call there. And here I'm just going to give it a different address. So two. Okay. Make up any, you know, it could be anything you want. Okay. And I'm going to hook that function. Program location two is going to get virtual free. Okay. And all that does is set it aside. So now the emulator will step aside into the emulated heat memory, heat management functions. Okay. And now we can move forward. Okay. We still haven't deobfuscated much of anything. Okay. So we need to, hopefully we're at 66 and we start stepping through there. Okay. We're down to here. Step, step. That gets us down below. Okay. And we're working our way down here. Okay. And now, let's see. Okay. Here's an example right here. 54D, that's virtual alloc. Okay. That's where they stored the address of virtual alloc. And if I step one more time. Okay. Watch EAX. Okay. I grabbed some heat memory. I know that was exciting. Okay. But remember I set the heap up at C0000, right? So I just asked for some memory off the heap and it gave me a block right at the start of the heap. Okay. And if we keep stepping. Okay. I think it's going to come up. And you see that it asked for 1800 bytes. 1800 hex, right? Okay. Whatever that works out to. Okay. One and a half K. Okay. You see down here, we're going to call it again. Okay. So we can continue to step. Okay. We're going to call virtual alloc again. And you can see EAX. Can you ever see EAX at the bottom there? Barely. Okay. C1804. Okay. So it's basically 1800 hex bytes into the heap and you can sort of get the feel that indeed we're allocating memory out of this toy heap that I've built. Okay. So now we're on a roll, sort of. Okay. So we keep stepping through. We've got the two functions hooked that are of interest to us, virtual alloc and virtual free. And then the way this works is, and I have a bad angle on this, is we're looking for loops. I shouldn't have done that. So now I'm just stepping through. And it's got all these functions and I don't really want to step into them. Every time you have a function that gets called, an IDA that's sub here, subroutine. Okay. You sort of have to descend into it and verify that it's not going to make any library calls. Okay. When you're doing this for the first time. You don't want to get hung up and sent out to fetch from the middle of nowhere. Okay. So we work our way through this function and you can see a loop that's formed here. Okay. We don't need it, I guess. Yeah, we do. Okay. So we're going around and around this loop. I don't want to do that. How am I doing on time? Got a ways to go, right? Okay. So we'll run to cursor. What's that? Still have a half hour? Plenty of time to screw up. Okay. So what I'm going to do is I'm going to look at all. I've been through all these functions. You're going to have to trust me. Okay. I'm going to work my way down and we're just going to run out to the function return here. Okay. So we're going to run through all these functions. Okay. If we get hourglass cursor for a while, then the demo's gone south. But we'll step out of there, continue stepping. Okay. Again, I'm not going to step into that. I need more screen real estate. Okay. Okay. We've worked our way out of that function. Okay. Don't want to go into that either. And the idea is at this point, cross your fingers. Okay. Good. It came back. Okay. Run to cursor is a dangerous thing, right? Because you have to hit that address. Okay. If you don't ever hit that address, you're not going to get control back from the emulator. But fortunately it worked today. It's been known not to. And we work our way down. Okay. We get out of this function. Don't let me step into any more functions. It takes too long. Okay. Now we're in a trouble spot, right? Call EDI. Well, that might not be so bad. I don't know. Let's see what happens. Oh, that's okay. That's a little trick they have. Setting up more stuff. At some point, at any rate, we get to a point where – I've lost my addresses. This thing has been unrolled enough that the original binary will sort of start poking out. Okay. And the trick is really – the point that I'm making on this one is that with the heap emulation, you can get through all this stuff. It's just a matter of stepping a little farther. And at some point, we're going to come up against them rebuilding their import table, if I recall correctly. Let's see. Okay. I think I'm in some sort of loop over here. Yes, I am. I keep jumping back to the top. So what I want to do is work my way down, try and find where this loop rolls out, which, again, is a dangerous thing. And if I don't get my cursor back, we'll just jump to a new demo. Okay. Reorganize some code. Keep on going. Okay. Not sure where we're going there. So – well, these are memory allocators, if I recall. Yeah, we want to get out of this business. So at some point, what I would like to get to – and we're sort of back up to the top of that too – is I find the end of this loop, which is difficult with this screen. Okay. All right. All right. All right. Ideally, what you want to do is sort of find the end of a loop that you know is safe to run through and then hit run to cursor. Without any dangerous calls in the middle. And, again, I'm going to gamble right here. And we made it. And what I'm going to do is try to – is cheat and see if maybe we're somewhere useful. Okay. With no strings. No strings to speak of. Although there's – let's see what we get. Okay. And now you can see a variety of other strings have come into play. Okay. Which means that we've probably got good code. You can see some IRC-type stuff in here. Okay. So the way to do this is then you want to try to find the end of that loop. You want to try to find the control transfer point where you leave the – the obfuscation part and you jump to the original code. And then you're going to – then you're able to start your reverse engineering. Okay. But it's there. Okay. And hopefully the strings convince you it's there. We shouldn't have these strings unless we actually correctly decrypted the binary. Okay. So I'll leave it at that and move on to the next one. Any questions from anybody? Let's start over here. . A lot of these things will do anti-debugging type things where if they detect the presence of a debugger, they'll shut down. Okay. My personal – I don't like running malicious code even in a debugger in a sandbox. Okay. There are ways – you can run some of these things like in a sandbox environment and then use a tool like Lord PE. Okay. Just grab the memory image and then you can just debug the memory image. Okay. Or you can reverse engineer the memory image because IDA will take that Lord PE dump and load it up just fine. Okay. But there are other tools. Again, what really motivated the development of this thing was Shiva. Okay. Which runs on Linux. And we can jump over – and this will also do Linux binaries. Okay. So it's x86 code in general. So we can run Linux binaries in an emulated fashion on IDA running on Windows and get at those as well. So there are people that probably would prefer to do it in a debugger. Okay. I prefer not to run hostile code. And so that's what it buys me anyway. Okay. There was another question back here. . No. But that's a good thing to put on the to-do list. Yeah. Yeah. It's definitely worth saving state at some point. Okay. So that you could get back into the binary and sort of resume wherever you left off. Okay. But it is not in there right now. Okay. Any other questions? Okay. TLOC we'll do next. Okay. Which is – so this is a so big virus. Okay. And like everything else, it's got broken imports. And we got one instruction out of that. Okay. Although it just jumps up here. And then starts doing crazy stuff. Okay. Like jumping to the middle of instructions. Okay. Pretty common stuff. So we'll go back down here to the start location. And bring up the emulator. Don't need a heap in this case. But I do need a threat environment block. Okay. Because this thing is going to generate exceptions. Okay. So the way I do that is I'll just – steal some space down here at the end of the stack. So basically that memory is mine. Okay. Those, you know, whatever 16 – you know, those 64 bytes I just pushed on the stack. Everything gets pushed as a 4 byte int. So I just push 64 bytes on the stack. That memory belongs to me. Because as far as the program is concerned, the stack pointer is starting at FFC0 right there. Okay. So I can play around and point – I can put the FS base up in that space that belongs to me. Okay. I need at least the first part of that, the first two words of that, so that I can link the exception handlers. Okay. So we'll stick FS base up there. And I'll just stick it at D0. Okay. So, you know, that's the setup for this thing. Okay. Because TLOC wants to set up exception handlers. Okay. So it's going to write to FS colon 0. So we – that's got to be writable. It's got to be something that we can see. Okay. So that we can build the chain of exception handlers. Now I don't have a default exception handler or anything like that to go by. But with TLOC we're okay. Okay. So we start stepping over here. See how big I can make that. Okay. And again, it's going to reformat everything. Okay. And you'll notice that – well, you'll see some strange disassembly like this VX decal and so on. But that will end up going away as we reformat all the code. And what I need to do is get over here. Okay. So we just keep working our way through. And TLOC's pretty well behaved except for the exception handlers. And here's a loop that it's in. So we're going to want to jump down here to the end of the loop. You've got to be very careful how far you jump. Okay. When you do this stuff. Yeah, I want to jump past the end of the loop. Okay. But it turns out that this loop is unraveling all of these bytes right immediately after it. Okay. So if I chose this instruction right here, that might not be the start of an instruction in the future. Okay. So you've got to be – and if I said run to cursor, then I'm off in Never Never Land, right? So you've got to be pretty careful. So I'll choose the first instruction right after the loop. I mean, that's going to be the fall through when this JG fails. Okay. And I'll take that. Regardless of whether the JCXZ gets reformatted into another instruction, at least the boundary's going to fall at 3C right here. So we run to cursor. Okay. And you'll see that VXD call actually will go away as the code gets reformatted. You've just got to follow along. I apologize if I'm going too fast through the stepping and it doesn't make sense. I want to make sure I get all the demos in. But the things I'm looking for are calls to library functions that I don't want to follow. Anything that looks like it's going to do a fetch that isn't going to work out. Okay. So here you go. You can see TLOC's starting to set up an exception handler. Okay. Zeroes out ECX, writes to FS colon zero right here. Okay. After, well, it's done some strange stuff. Okay. But it's setting up the exception handler. Okay. Skips a breakpoint. Div ECX at this point is a divide by zero. Okay. And causes, let's see if we can see some stack growth here. FF70. So the stack is really sitting right here. The divide by zero, notice that the stack jumps all the way down to C44. Okay. Because the emulator's jammed all that SCH stuff on top of the stack. Okay. It recognized that exception occurred. Okay. And it's jumped us. Okay. If we step one more time, maybe not. Okay. Next demo. Next demo. That didn't work out. I'm not sure what I did there. But you can see EIP down here at 0004. Okay. So I'll come back to that one. Okay. Something went awry right there. It's usually this ice BP screws me up. I should have skipped it. Okay. So one failed demo out of three. Okay. Moving on. Okay. Burn eye. Burn eye. I'll try to get that. So the first one is a Linux obfuscator, and it can operate in a couple different ways. Okay. Meaningless declarations and so on. And in its simplest form, it's really not difficult to get through. Okay. They can add password protection on top of it. Okay. In which case, you know, we can't go anywhere unless we supply the password, because they use it to decrypt. They actually encrypt the binary. Okay. So you really can't get too far in. There's other tools that will do burn eye on the FLI. from the command line. So this is just an example of running through Linux code in IDA. It's x86 code, so we don't really care what platform it's from. And so we bring it up. Alt F8. Don't really need much here. And we start running. And a loop becomes evidence very quickly. Okay, and again, in its simplest form, it really doesn't do much. You can see this loop that works right there. And I'm just going to run it all the way down to here. Okay. In theory, I should show you that we have no useful strings at the moment. There's kind of a bunch of garbage in here. Right? Come back over here. Run the cursor. Okay. Go back to the strings window. Rescan. Okay, and now we see that, indeed, this was a TASO burn-eye encrypted binary. And you start to see some of these strings that have popped out. Okay. Now, this takes further reversing. It takes the burn-eye itself to figure out, where do I go next? Because burn-eye does a little bit different. It actually embeds the entire protected binary as a piece of data in here. And then it's going to go through some loading. It's going to push it all over into memory. It doesn't transfer control directly to the start function. Okay. It loads it up. It acts as a loader now for the embedded binary. Okay. So we would have to go find that binary in here. And it's, again, you know, being able to work through this doesn't necessarily relieve you from reverse engineering responsibilities. So, you know, the way burn-eye works is you've got the embedded binary. So we can go look for the ELF headers. Okay. So if we go over here in hex view, okay, what I want to do is find 454C46, which is ELF. Okay. Okay. In hex. And we can search through it. And it occurs here. Okay. And, you know, because I happen to know this binary and the way burn-eye works is this is not the embedded binary yet. Okay. We want to go find it again. Okay. And we find ELF again down here. Okay. And it turns out that this is the embedded binary. And we want to go over to the view window sort of follows along. And this looks like all a bunch of data. Okay. Because IDA couldn't understand what it was initially. But we've unrolled it. We've actually changed all of this. We need to undefine all of this stuff. And I'm probably going to go have to search again. Okay. You know. Okay. Okay. So that takes me down to where I am in my hex view. Back over here. Scroll a little further. Okay. And right here is the start of the ELF header. Okay. For the embedded. Protected binary. And a way to sort of yank that out of IDA. Or the way that I do it anyway is. Figure out where I put the emulator. Okay. And. I built in a dump routine. So we can just dump from the database. And the arbitrary range. Okay. So we dump. It automatically chooses current cursor location. Okay. Over to the end of the file. Okay. And I'm going to go ahead. And I'm going to save it as three. Because I've already done it a couple times. Save. Okay. So we just basically took the embedded binary. Dumped it out to disk. And then if we were to go. We won't save this one. Go yank this thing off the disk. Okay. IDA recognizes it as an ELF. Doesn't complain that anything's broken. Okay. And throws this in at the start function. Okay. But you can see all the names. That are defined. And now we can reverse the embedded ELF. That was formerly protected. Okay. And then. Last one. Doing okay. Ten minutes. Okay. It's Shiva. Okay. Shiva is the whole story in and of itself. Okay. Shiva is. It really. Takes a lot of interesting steps. At protecting a binary. A. It obfuscates. Just like all the other things that we've seen. Okay. But instead of. Just embedding it. A deobfuscation stub. With the protected binary. Shiva actually comes with a pretty complex. Runtime environment. Okay. So. It. It tacks on a runtime environment. With the protected binary. And this runtime environment. Is. Responsible. For. Sort of demand. Decrypting. Blocks of the protected binary. Okay. On the fly. So. No. So the protected binary. Is never. Deep. Fully decrypted in memory. At any one time. Okay. And. You know. That's. That's the big thing. Right. If you. If you run these things in debuggers. Or. If you just let them run on the fly. Your goal. Your hope is. Well. I'll just let it run. And then once it's steady state. I'll dump the process out of memory. Okay. But Shiva. Keeps you from doing that. Because. The entire thing. Again. Is never decrypted. In fact. No more than about a third of it. Is decrypted at any one time. And they do it. Question. I know of no Windows. Encrypters. That'll do that. Yeah. The way it works. Is that it just. It fills the process space. With CC hex. Which is an int three. Okay. And. It puts small blocks. Of the decrypted binary. Into that process space. And so. You're going to run off the end of a block. And you're going to hit an int three. Which it handles. Okay. And then sees. Okay. Where am I? What's EIP right now? Because it's P tracing the binary too. Right. There's a lot of overhead. And. Then it goes. And grabs the. The next portion of the program. After overwriting some other portion of the program. Decrypts the next portion. And then allows execution to resume. Okay. That's. That's. That's. One of the things it does. So. You have to be able to get at all of those things. And. So. You had the initial deobfuscation. And then. You had this runtime. Time encryption piece. That you have to get past. Which means that there are embedded keys. In this runtime. Because that. That stuff is actually encrypted. So you have to do some key recovery. And that's part of the reverse engineering of Shiva. Which gets. Again. Pretty complicated. Okay. And Shiva is all about. You know. Obfuscated code paths. It does that all over the place. So. We can bring this up. And we don't. Need anything special. And again. You know. We're running through a Linux binary on Windows. So that's kind of nice. And it becomes a matter of. Just stepping on through here. And the idea is to recognize some loops. And this stuff will reorganize itself. So let me get back over here. And I lost the emulator. And hopefully we can see some loops forming. And that's the idea. You know. IDA is pretty nice about. Showing you branching and everything. So we can see. There is in fact a loop over here. With again more screen. We would see that. So right here. We want to run the cursor. Okay. And then we want to continue stepping. Until we find some more loops. Here are three of them that Shiva forms. At some point. I think that was the bottom. Okay. Lose all my addressing information. Okay. So we'll go over here. Run the cursor. I just screwed something up. Okay. So we'll go over here. So I'm going to do. Okay, we're on the cursor, we're going to continue looking for the next loop. This is getting pretty dry, I know. You get the idea, hopefully. I can talk more about Shiva afterwards, because I think we're going to end up running out of time. I'd rather field any questions that are out there, and if there are none, I'll cut you loose. Question over here again? What would you do to build a better obfuscator? What would I do to build a better obfuscator? Shiva is a good step in the right direction. There's actually some tricks in Shiva that are far more advanced. They actually do instruction replacement. They'll scan the binary that they're trying to protect, and they have a couple different types of instructions that they'll emulate, because they're p-tracing that binary. They'll go through that binary, and they'll replace them with int3s. Those int3s don't cause a new block to be loaded. They drop over. They say, okay. That's an int3, because that instruction, we're going to emulate that instruction. They go over and emulate the instruction. There are about six different instructions that they emulate, and only in a few places. What makes it tricky is, even if you did manage to get the entire binary decrypted in memory at one time, or you were able to watch memory, and through the process of one third of it being loaded, after enough time, you should have seen all of the blocks loaded. Say, okay. Now I got the whole thing. Well, you don't have the whole thing. You don't have the whole thing. Because they've inserted these int3s randomly throughout the program. What you don't get, even if you capture all of those blocks, is what is supposed to be in place of those int3s, which they save aside as a list of ... The int3 at location one, two, three, four is supposed to be a push EAX. When they hit that int3, they jump over, because they're p-tracing. They run the push EAX. They manipulate ESP. They manipulate memory. Okay. Then they return control back to the binary. So it's really tricky stuff. I don't know if Neil's here or not, but probably not. Oh, there you are. Okay. Here's what I would say, is you emulate all these instructions. I say there's five of them. Okay. You have a broader class of instructions that you emulate, because it's really ... You have to spend time reverse engineering Shiva to know what instructions are being emulated. Then you can ... Your emulation list is fixed. You can go, you can pull out the emulation list, and then you can repair them. After you've done enough reverse engineering, you know what all the replacements are supposed to do. You can walk the emulation list and replace the CCs with what is supposed to be there. Okay. So if you have a broader class of emulations that you do, then you choose a subset for each new binary. Maybe you could do 20 emulations. For each new binary, you only pick a random five. So that every binary is ... So there's no way to know ahead of time what emulations are going to be used. I always know ahead of time what emulations you're going to use right now. Okay. But if I don't know ahead of time what emulations you're going to use, then I have to spend a significant amount of time just to recover your emulation list, and then figure out, for each new binary, what is that emulation doing? Okay. So that's what I ... I think that the techniques that Shiva uses would make things very difficult. If they were incorporated on the Windows side, it would be among the best sort of obfuscators that are out there. The SEH stuff is kind of a pain to work through, but it's doable. But the Shiva stuff makes ... Not only do you have to reverse, get through the obfuscation, but then you have to reverse the way Shiva works, because it's the Shiva runtime that's controlling everything. Right? And so you have to deal with the Shiva runtime just to be able to see the other obfuscated code. Okay. Halvar. . I can barely hear you. There are a lot of commercial encryptors that do that? . . . . . . . Yeah, that's the way to defeat this kind of thing, is that you make ... Even after the obfuscation is done, you've got to do so much reverse engineering that this still doesn't help you so much. Okay. Any other questions? Okay. I guess, oh, wait, one over here. So how did you learn more about Shiva? How many calls? How do I know? Well, I went to Neil's talk last year here where he talked about Shiva, and they presented it a couple of times. So there is a presentation on Shiva. And then I gave a presentation on reversing Shiva. So you can go get that. And in the process of developing that one, then that's what motivated sort of development of this. And so I tried to – the development that's gone on on this project has been to make it more generic, to handle more classes of obfuscators. Yes. The plug-in, I've been using it since 4.5 and forward. You just have to compile it with the matching SDK. Okay. Yeah. Yeah, the plug-in is free open source. The URL is in the slides. It's available at SourceForge. Any other questions? Okay. Thank you very much. Thank you.