I'm going to talk a little bit about anti-reverse engineering techniques. My name is Jan Nugent. I'm from Germany. So what's a little bit special about this talk is the fact that the techniques I'm going to present here have been taken from a real-world DRAM system. So I was running into some legal issues with that, so I need to actually strip some details out of the talk. But I have also a few slides on that later, so let's just have a look at the outline. We start pretty smooth with a short introduction on DRAMs in general. And as I said, I will have a few slides on the legal issues with that. Chapter 2, actually this is the correct slide. Chapter 2 is a little bit more technical. We'll see some details on exceptions. We have a little bit more information handling on the Windows on operating system level. And we need this background knowledge in order to understand all the techniques I'm going to present in Chapter 3. And Chapter 3 actually is probably the main part of the talk and deals with various anti-reverse engineering techniques and how to overcome them. And finally, we have a demo, and that's it. So, obviously, there... There are some legal issues with this talk, and that's due to the fact that publishing DRAM research is probably pretty dangerous and considered to be illegal in most countries. At least, there's a huge legal uncertainty. So I went through the EFF, and especially Jennifer Granig, who gives me some very good insights and some very good advice. Thanks, Jennifer, if you see this. So we were discussing how we could... how we could modify the talk in order to lower the overall risk for me. And Jennifer actually told me that there's some kind of loophole in the DMCA, which allows you to publish your DRAM research if you do so-called encryption research, though there have to be fulfilled some additional issues with that. But this is still too dangerous for me, so I decided to go for another solution. And we discussed... the various solutions which would be fitted here. And as a consequence, I modified the talk in a way that, I skipped all the details about this key setup of the decryption algorithm. And additionally, I also won't reveal the very identity after the DRAM encryption. So this is kind of a black... black box talk. So, what this is about, it's about showing some not so common and reverse engineering techniques techniques from a real-world example, and I'm also going to show how to defeat these techniques. And it's obviously not on how to hack, insert your DRM here, or, it's also not about writing decryption tools. So, what's a DRM anyway? DRM means digital restriction, no. DRM means digital rights management. Some people say it means digital restriction management. So, what it actually does is it restricts the user in accessing the content, and this is achieved by encrypting the content. So, the DRM actually controls access to the key, and whenever you access the content, it's being decrypted online by the DRM system. The key is often bound to the user or hardware in order to prevent copying of content between different users. So, whenever you change your hardware, it's very likely that you need to acquire a new license. And, depending on the implementation, there might be various types of keys, such as media keys, hardware keys, player keys, and so on. And, just an obvious, pretty obvious side note is that every mainly software-driven DRM actually can be broken. So, let's define our strategy we would use to attack such a system. It's pretty obvious that the ultimate key, DRM, is really up to the user. goal is to find the decryption algorithm and the associated key setup. And if you can understand these, then obviously you have a technique to decrypt any content. So the obvious approach probably would be to, from a debugger perspective, would be to set some breakpoints on common system I.O.L. APIs like, under Windows, like create file, read file, or memory map files. And then you would probably set a breakpoint on memory access on the file buffer. And whenever this breakpoint would trigger, this would mean that it's either a copy operation or you're probably very near to the actual decryption algorithm. Okay so, since this talk would be over at this time if the DRAM system would allow this, we see that the DRAM system actually prevents this strategy by blocking the debug registers. So we need to, we need to carry out some fancy strategies in order to still be sufficient. Okay, a probably, a likely strategy would be to use code coverage. Code coverage in this sense means that you let the program run online while recording execution of base basic blocks or functions, and you recall all these break point hits and do some calculations with the sets of hits, so you would ultimately find, pretty easily find DRM-related code. Due to a few anti-reverse engineering techniques, code coverage is not possible in this case, but on the other hand, it gives some good starting points. Okay, so summing up, our strategy is to use code coverage and apply the debug registers and the hardware break points to find the decryption code. Okay, so exception handling under Windows, namely SEH, works on a per-thread basis. This means every thread actually has a list of registered exception handlers, which is pointed to by the user. This is our FS0, and whenever there's an exception triggered in the current running thread, the operating system walks this list and calls every thread until it finds one which responds to be able to handle the exception. So, as we see, an exception handler basically has two choices. On the one hand, it can say, okay, I'll handle the exception and return to the operating system, and in this moment, the operating system will stop list walking and schedule the thread again. On the other hand, the thread can decide to refuse to handle the exception, and in this case, the operating system will continue to walk the list of exception handlers. Okay, so what we see here is the signature of an exception handler, and probably the most important parameters are the exception record as well as the context. So, what does this mean? The exception record actually contains information on the exception itself, like the exception code, whereas the context is a pointer to the thread's context of the faulting thread, so the handler actually has the possibility to modify the context in a way such as that it fixes what caused the exception in the first place. And the list structure is depicted by the image. In the end of the list, you have always the operating system handler, which jumps in if there's no handler which could handle the exception, and this usually means that the process is terminated and Dr. Watson shows up or whatever. Okay, so the SEH in a more detailed version, whenever there's an exception triggered, the operating system actually dispatches into kernel mode, and the interrupt descriptor table is used to carry out all the program logic which clumps together the exception record and the context pointer, and this information is then passed down to user mode again, and the first code being executed in user mode actually is inside NTDLL, and the very procedure is a user exception dispatcher. And from this procedure, all the SEH list walking, logic, as previously outlined, is triggered. And in this case, handler one would be able to fix the exception and return to the NTDLL procedure, which would finally use NT-continue, which applies the possibly modified context to the thread. Okay, in this view, it was pretty much simplified, but it's sufficient for our analysis because we don't need concepts like stack unwinding, collided unwinds, and so on. And important to note, a handler can also decide not to return, for example, in C++, it's pretty common that the context is actually modified. Okay, so the DRAM protection actually deals with two major techniques to scare off reverse engineers. Okay, so the DRAM protection actually deals with two major techniques to scare off reverse engineers. It's control flow obfuscation on the one hand, and on the other hand, it's anti-debugging tricks. For control flow obfuscation, the DRAM system uses a vast amount of fake exceptions to interrupt control flow at runtime, and these handlers, the exception handlers, which react on these fake exceptions actually use logic to change the thread context. So, what you have is an exception handler, since it's able to modify the thread context, it can, for example, change the instruction pointer and the program resumes at a totally different location. Additionally, there are many, there are many call tables in order to stop disassemblers from successfully performing cross-referencing, and there are two major And there are two major, and two reversing techniques that we can use to stop disassemblers from successfully performing cross-referencing. For reverse engineering techniques, on the one hand, it's tremble lines, which I have a few slides on, and it's the P-code machine, which is used to carry out the actual decryption algorithm. Okay, for anti-debugging, we have some very basic checks. That is, the very common PEB flag check, or scanning APIs for interrupt-free opcodes. And in addition, you have special files containing code which is uncompressed at runtime, and this, again, is nothing very fancy, because this has been around for some years. But on the other hand, the usage of debug registers actually is a little bit harder to overcome, which we will see in a few seconds. And finally, the system also uses fake exceptions to detect a possibly attached debugger. Okay, so tremble lines are, in this case, is called, is code which is copied at runtime to a randomized location, and AudiTSC is used as a seed for a random number generator to place the destination of the tremble line. And when the tremble line has been copied, execution is resumed from the destination. Okay, so the actual control flow, which is changed via fake exceptions, in this case, since it's a fake exception, we need to have some unique exception identifier, and in this case, it's a single-step exception, and this also possibly interferes with an attached debugger. And the exception handler actually modifies the instruction pointer based on some debug register values, and this is exactly the reason why the debug registers have been blocked. So, because instruction pointer, the instruction pointer, and the instruction pointer, have been blocked, actually depends on values of the debug registers. So you cannot easily use BPMs in your debugger because that would interfere with the DRAM system. Okay, so let's see some details about the Trample Line control flow. In this situation, Trample Line A wants to initiate a control flow change to Trample Line B. And it's important to note that the control flow entirely depends on jumps and exceptions. That means there's no such thing like a call to a Trample Line because everything goes through exceptions. That means there's really no direct control flow between A and B because everything goes through the exception handlers. And therefore, we have a call hierarchy emulation because if you only jump to a Trample Line, you cannot, the Trample Line, being jumped to cannot just return because there's no return address on the stack. So Trample Line A copies the Trample Line 0 and jumps to it. Trample Line 0 is just a immediate Trample Line which is used to put the destination Trample Line, in this case, Trample Line B, on an internal call stack emulation. And this is, as I said, this is needed because there is no direct call between A and B. A and B, so you have to kind of emulate this nested calls and return operations. Okay, and Trampl.0 actually, sorry, Trampl.0 actually pushes the destination Trampl.1 on the internal stack, and after this, it copies the next Trampl.1 again to a random location, and Trampl.1, in this case, installs an SEH frame, which is used to handle a fake exception, which is waste by this common code. First of all, the E-flex register is pushed to the stack, and the trap flag is owed in, and finally it's applied, so upon the next instruction, a single step exception is triggered, and the, the aforementioned SEH handler is invocated by the operating system, and so the exception handler now carries out all the logic, which is involved in, in the return and call emulation. So what it basically does is it changes the instruction pointer based on the debug register values, and again, it clears the trap flag bit, removes the SEH frame, and cleans up the stack. And finally, execution resumes at Trampl.1, too, which in turn copies the destination Trampl.1 and jumps to it. So, what we saw here is this is the mechanism which is used to emulate a call between A and B. The return, so whenever Trampl.1 B wants to return again to Trampl.1 A, it would do this in a similar way. That is, again, use Trampl.0, 1, and trigger some fake exception, and the handler would then modify the internal stack representation, clean up the stack, and return to the middle of Trampl.1 A. Okay, so the debug registers are used in a special way, whereas debug register 0 and 6 are zeroed out, because they aren't used at all. Debug register 1 contains a pointer to a shared stack area, but it's not used at all. Which is used to pass data between Trampl.1s. In this case, that would mean DL1 points to a location which is used to exchange data between Trampl.1 A and B. So, therefore, you have some kind of parameter emulation. DL2 actually holds the Trampl.1 address used to perform the return emulation, and DL3 holds the address of the starting Trampl.1, that is Trampl.0. And, to confuse a possibly attached debugger, DR7 is used to turn hardware breakpoints on and off very frequently. So, what's the impact on reverse engineering? It's pretty annoying to debug such a concept, because Trampl.1s always jitter around in memory, and it's pretty hard to actually recognize repeating code patterns, because everything is copied at random addresses, and your disassembler or debugger has, it's difficult for the debugger or the disassembler to recognize function boundaries. So, as I said, since the control flow actually depends on the debug registers, we don't have a possibility to use our BPM or BPX strategy, as I pointed in the first part. On the other hand, we have also no call stack, as I said, due to the call emulation. So, it's pretty hard to kind of backtracing from a nested procedure call, and also we cannot, it's not so easy for the disassembler to say, OK, procedure A calls procedure B, because there are no call instructions which are directly used. And this means we have really very few cross-referencing, information. formation. And additionally, finally, absence of return instruction confuses a disassembler because it's hard to guess function boundaries. Okay, on the other hand, once we understood this call emulation mechanism, we get a perfect call stack, which is good because usually when debugging without debugging symbols, we don't get a perfect call stack. Okay, so how can we actually ease the impact of the trample on mechanism? An idea would be to actually fix the trample on addresses in memory. And we can do this by writing a kernel mode driver. And this driver then would actually turn our DTSC instruction into a privilege instruction by setting the timestamp disable flag in 0.4. And this means whenever this instruction is executed in use case, it will be executed in use case. And this means whenever this instruction is executed in user mode, a general protection fault is waste. And again, then all these exception handling, which outlines in the second chapter, jumps in. And as I said, the control flow upon this general protection fault would then jump to the interrupt descriptor table, which we would hook. So whenever the RDTSC instruction would be executed, we would gain control in our driver. And what we could do is we could just disable the randomization by just returning 0 if we come from user mode and if the instruction actually was RDTSC. So we need to disassemble the memory where the instruction would cause the general protection fault. In all other cases, we just jump to the original handler. Okay, so and this actually works. But by using this technique, we can fix the trample lines and this makes it a lot more easy to understand the trample line mechanism because you have, it's easier for the reverse engineer then to see repeating code patterns because everything doesn't jitter around anymore. Okay, some words on the debug registers. The debug registers are used by the DRM system, as I said, for various storage. And so the debugger cannot use the hardware breakpoints anymore. In addition, the context is actually set by our Windows API, set flat context. Okay, so obviously, since we said we wanted to have BPMs for our strategy, we need to solve this in a way. Okay, what we can do is we can use APIs. We can use a set API hooking in user land space to hook into the set or get the context APIs in order to redirect any modification attempts, read attempt to our internal storage and fake all the values the DRM system actually expects to see. As a consequence, the DRM system would then not be able anymore to modify the debug registers. And this is good. because that's what we ultimately want to achieve. So there's a problem with this because hardware breakpoints still won't work. And the reason for this is that we actually have two different thread contexts. On the one hand, we have the kernel mode thread context, which is maintained by the operating system. And on the other hand, we have, in this situation, we would have our internal storage of context records. So how can we solve this? We could, for example, hook KAU, the exception dispatcher. If you might recall, KAU, the exception dispatcher, actually is the first code being executed in user mode. So a reimplemented version would actually check if the current exception is a fake exception, that is, if it's of type single step. And if that's the case, it would pass a fake context with the fake debug register values previously set by the thread context API to the handler. So the handler would actually see the correct values it expects. And on return, that is when the handler returns to our KAU, the exception dispatcher, reimplementation, we would need to merge the modifications made by the handler into the real context back. And after this, we would then apply the final context again via NT-continue, as the original KAU, the exception dispatcher does. Okay. So here are a few more details. How does this work? In the upper image, you see the real context in kernel mode, which is maintained by the operating system. And in the lower part, there's our hooked version, our MGLT thread context. And at one, you see that the DRM system frequently uses set and get thread context API calls in order to modify the context. And all of these are redirected, and they're all connected. To our emulated context. So upon triggering an exception, the operating system would again call our KAU, the exception dispatcher, because we hooked it. And what we would do then is, we would plug in the fake debug register values, which are green, and merge them with the original values passed down from kernel mode by the operating system. So, we would then call the exception handler and passing our fake context, and the exception handler would then do all the fancy stuff and return, and the exception handler actually modifies some of the general purpose registers like the ESP, because it's cleaning up the stack, and some other stuff. Especially of course, the instruction pointers. So, we need to merge those two versions and sync them back, And finally, let the operating system apply this modified context. So, the summary after these countermeasures is the DRAM system actually cannot modify our debug registers anymore, which is due to our API hook. And in addition, the exception handler of the DRAM system gets its expected values because they are fed from our internal storage. So, this means we can really now use the hardware breakpoints for our analysis. I made the implementation available as an IDA plug-in. Maybe you would want to check it out. Okay, so the final protection mechanism, which seems to be probably the hardest one, is the use of a P-code machine. So, what's a P-code machine? A P-code machine is... some kind of virtual machine which is embedded inside the DRAM system. And in this case, this virtual machine is stack-based, which means that all parameters are pushed on a virtualized stack, which is maintained by the P-code machine. And you have roughly 256 opcodes, and the data are represented as ASMPs, which is an ASN1, an ASN1 format. And the P-code machine actually allows programs running inside it to allocate memory from the host machine, whereas the host machine in this case is just the program where the P-code machine is embedded in. Actually, the opcode set is split in two sets. On the one hand, you have the high-level opcodes, and these are used to... for example, loads opcode files. The DRAM system actually has files which nearly only contain opcodes. So, you have opcodes to reload additional functionality in opcode files. And you can also call into these opcode modules. And it's interesting to note that the music decoding actually is handled by this way. The other part of the set actually contains a lot of opcodes. It contains low-level opcodes, which means it's emulated the virtual CPU. That is, you have simple arithmetic instructions like add, subtract, and so on, and also instructions to handle the internal virtual machine call stack. Okay, a few words on the opcode module files. These are special files which contain opcodes for the P-code machine. Some of them have a mixed... have some amount of mixed code that is native and opcodes. And these files are actually decompressed at one time by using, I guess it was Gzip. On the other hand, these files are rather plain because there are no PE files, so there's no import address table, no sections, and so on. But, there's a relocation table because these opcode modules might call into some... into a fixed amount of imports, like, for example, ms-visual-c++ one time. So, there needs to be some relocation information in these opcode files. Okay, so now, how are the opcodes executed? Every module actually has a random pool, which is used to randomize the assignment between... between opcode and associated handler. And, this just means that every opcode file has a totally different opcode. For example, say, Edition or DecryptMusic. And it's just to further increase the protection. So, as I said, the machine actually has a built-in pseudorandom number generator. And, additionally, the data intel leaf, the time that the machine'sğin... with the opcodes, which are just there to confuse the reverse engineer. It's just garbage data inserted between opcodes. And this data is actually parsed via ASN1. So, the impact on reverse engineering, why is this difficult? Because you need to understand the machine itself before you can even start to understand the opcodes, which are contained in the files of the main program. And as I said, due to the randomization, we have a different meaning for each opcode on a per-module basing, on a per-module basis. And due to ASN1 parsing, which is quite complex, this still even more increases. So, debugging is difficult because you have, and so to say, you have a low signal-to-noise ratio because a PCOD machine actually looks like a very big loop, which has a very large switch statement inside a loop,, and this is even lower due to our opcode scrambling with the randomization. And this is actually the graph in IDAR of the PCOD machine, and on the lower left, you see the entry point, and in the middle, there are the sixth, the blue lines. These are all the opcode handlers. Okay, so what strategies could be developed for you? to attack such a system. Probably the most expensive strategy would be to just what a custom disassembler. But the problem with this is that we have really many handlers, in this case it was 256. And also you have mixed handlers, as I said, those two sets with the native and the opcode handlers. So you would have to analyze all the complex high-level handlers up front in order to make a meaningful disassembly. Okay, and for the disassembler to work, we would also need to reassemble randomization, the descrambling of opcodes, the garbage instruction, and ASN1 pausing, so that's really kind of expensive. Another strategy, which I would call the brute force strategy would be to just let the debugger single-step via a debugger script until the key is actually written to memory. This is pretty slow because single-stepping is a pretty expensive operation because you have so many context switches. But on the other hand, you will definitely reach the code which writes the key to the memory, but it's not so cool because it doesn't seem to be very clever. So what's the cool strategy? The cool strategy, that would be probably to use a CPU emulation like PyEMU or x86EMU for IDA to emulate all the instructions involved in the PCOD machine. So essentially, you would kind of defeat virtualization with virtualization. And this is also a very fast and flexible solution because obviously you can control every aspect due to the emulation. Okay, and the final strategy I've been using in this is, well, it's pretty lazy because we use what we already have. And the trick here is that, as I previously outlined, the machine actually has a mechanism allowing programs running inside it to allocate memory in the host machine. So what we can do is we know that due to our breakpoint, on memory access, for example, we might know that it's a DES algorithm, and since the key schedule size of DES is 80 hex, we can just pass, we could just set a breakpoint, a conditional breakpoint, in the memory allocation routine, which would fire whenever there's an allocation of size 80. Okay, and so whenever we reach this point, we could then use our reclined debug registers to set a breakpoint on memory access in the allocated memory, and whenever this memory has been written, we know, okay, there would be one break in the key setup algorithm. And from there, we could, under the assumption that the decryption algorithm is pretty close, we could backtrace from there and have everything. So, pretty disappointing in this case, was that the decryption and the key setup are contained in native code, that is, some of the high-level handlers actually contained the code to decrypt and calculate the key to decrypt the content. Okay, so the key setup algorithm, here it is. So, a little bit obfuscated due to some legal issues. It basically works by hashing some files using different hash algorithms, and finally, the encryption key is then made of some XO operation. It turns out that the key is different for every music file. Okay, and as I said, it's a DES algorithm, and the interrupt, no, not interrupt, the initialization vector comes from the DRM file. So, now we're going to see a demo. Nope. Okay. So, maybe I'll say a few quick words on the demo. Since I need to, I cannot actually reveal the identity of the very DRM, so I need to do some kind of black box demo. I can show you how a decryption, I can show you how a decryption works. But I cannot show you how the encryption process works, because that would obviously reveal the very identity of the DRM. So, I prepared a DRM-protected file. And I can encrypt it. Okay. And due to the fact that the DRM system is not working, actually registers direct show filters, I can use any player to play back the unprotected content. So by using the Windows Media Player, I don't reveal the identity. So. Okay, so I placed. So, to give you a good indication why this is, that this is not a fake, I started MATLAB with the script, and it just reads in both files as a vector, and puts the histogram on screen. So what you see on the left is the DRM file, which is pretty much equally distributed, and this is a pretty hard indication for the file content being encrypted. And on the right side, you see the plain file, which has been decrypted, so it's absolutely not equally distributed anymore. Okay, so I'm well aware that this could still be fake, but I can't give you a real proof, because it's just too dangerous. Okay, so the conclusion is, overall it's a pretty good protection, but it has a lot of problems. There's some major design issues associated with it, because we could bypass the whole p-code machine by just reacquiring our breakpoints on memory access. And there are also some other weaknesses, like for example, the very weak anti-debugging mechanisms. And of course, there's much room for improvements. The most obvious one would probably be to transform more native code to the p-code, so to avoid that the actual decryption process is a high-level handler, which is pretty useless. And you could also make the p-code machine more complex. You could do some nesting. You could have a p-code machine inside a p-code machine, inside, and so on. Or you could also have polymorphic handlers, a self-modifying machine, so the sky really is the limit. And well, as I said, the debugger detection is very weak and just uses some old techniques. Another one which could be used to improve the protection would be to use debug registers actually for their purpose. That is, the system could just let controls all depend on BPMs. So, in this way, we would have no chance of emulating any debug registers, because they would be used, but the protection itself. And well, I don't know why the guys developing this protection didn't do this. I don't know. Okay, so that's pretty much it. Thanks for your attention, and bye.