1 00:00:00,000 --> 00:00:11,800 Okay, so let's get started. This talk is Covert Debugging. My name is Danny Quist. I'm a co-founder of Offensive Computing, a PhD student at New Mexico Tech. 2 00:00:12,380 --> 00:00:16,620 I do reverse engineering, exploit development, and a member of the CDC NSF. 3 00:00:19,820 --> 00:00:29,340 Hey, how's it going? I'm Val Smith. I'm also a co-founder of Offensive Computing, a due malware analysis and reverse engineering contributor to the Metasploit project. 4 00:00:29,600 --> 00:00:35,240 We're giving a talk later. I also do penetration testing, exploit development, and a member of CDC NSF. 5 00:00:37,640 --> 00:00:44,020 So just real quick, tell you in case you don't know what Offensive Computing is, is we're an online malware analysis website. 6 00:00:44,400 --> 00:00:46,560 We have a blog where we post analyses. 7 00:00:46,620 --> 00:00:47,880 We post analyses about the latest threats. 8 00:00:48,760 --> 00:00:56,220 We have, as far as we know, the largest openly available malware collection on the internet, about 160,000 samples that you can go download. 9 00:00:57,360 --> 00:01:00,680 We provide a couple of services if anybody's curious about that. 10 00:01:00,800 --> 00:01:05,540 We do custom malware analysis, reverse engineering, malware intelligence gathering type stuff. 11 00:01:10,220 --> 00:01:15,640 Okay, so this talk is about debugging software, and it's specifically about debugging malware. 12 00:01:16,620 --> 00:01:17,840 Because it's a powerful tool. 13 00:01:18,040 --> 00:01:22,840 It's the main way that you unpack or expose the internals of the malware. 14 00:01:24,010 --> 00:01:31,640 And so the goal is to trace runtime performance, monitor API calls, and basically get to the dynamic analysis. 15 00:01:31,720 --> 00:01:33,680 Because that means we can get automation. 16 00:01:34,840 --> 00:01:37,780 But malware is getting real good at preventing it. 17 00:01:38,040 --> 00:01:43,320 Val will talk a little bit about some of the methods, just at an overview level. 18 00:01:43,640 --> 00:01:46,000 But basically they detect whether they're being debugged. 19 00:01:46,620 --> 00:01:48,440 They detect whether they're in a virtual machine. 20 00:01:49,000 --> 00:01:52,940 And these are all techniques that were pioneered by legitimate software. 21 00:01:54,820 --> 00:01:56,540 So the talk is going to go like this. 22 00:01:56,620 --> 00:01:59,240 We're going to talk about software armoring or packing techniques. 23 00:01:59,880 --> 00:02:02,420 We're going to talk about some of the covert debugging requirements. 24 00:02:02,860 --> 00:02:08,660 We'll present a one solution for removing software armoring, which is dynamic instrumentation. 25 00:02:09,180 --> 00:02:13,020 And then we'll show our OS page fault-assisted covert debugger. 26 00:02:13,380 --> 00:02:15,460 And then as an application of all this, 27 00:02:15,520 --> 00:02:16,600 we'll apply... 28 00:02:16,620 --> 00:02:19,320 We'll apply it and make a generic unpacker and show you the results. 29 00:02:23,700 --> 00:02:24,140 Okay. 30 00:02:24,400 --> 00:02:28,980 So I'm going to go over the different techniques that software uses to protect itself from being analyzed. 31 00:02:29,380 --> 00:02:30,960 We like to call it software armoring. 32 00:02:31,700 --> 00:02:34,680 And this is going to be kind of a high-level generic talk about this. 33 00:02:34,820 --> 00:02:36,680 And then we'll dig down as we go and show some demos. 34 00:02:37,700 --> 00:02:43,960 So these are the main techniques that you commonly see both legitimate software and malware using 35 00:02:43,960 --> 00:02:45,780 to protect itself from being reverse engineered. 36 00:02:46,620 --> 00:02:49,280 The first most common is called packing or encryption. 37 00:02:49,560 --> 00:02:51,320 This is where they obfuscate the binary. 38 00:02:51,600 --> 00:02:52,940 We'll go into detail on each one. 39 00:02:53,680 --> 00:02:57,500 There's SEH tricks, virtual machine detection, debugger detection, 40 00:02:57,940 --> 00:02:59,500 and something called shifting decode frame. 41 00:03:01,660 --> 00:03:02,180 Okay. 42 00:03:02,240 --> 00:03:07,060 So basically the way that the packing or encoding of a binary works is that 43 00:03:07,060 --> 00:03:12,760 they take the original piece of malware with the normal code where you can see all the assembly instructions 44 00:03:12,760 --> 00:03:16,600 and they pack it or encode it so that when you... 45 00:03:16,620 --> 00:03:21,840 load it up into IDA Pro or whatever tool you use for disassembling, it's basically gibberish. 46 00:03:22,460 --> 00:03:27,360 The reasons for doing this is to make it small, make it hard to analyze, that kind of thing. 47 00:03:28,080 --> 00:03:31,820 So inside of there, there's usually a small decoder stub that knows... 48 00:03:31,820 --> 00:03:36,480 it's a set of assembly instructions that knows how to de-obfuscate the binary and then execute it. 49 00:03:37,800 --> 00:03:41,660 They play all kinds of tricks with portable executables, which is the Windows file format, 50 00:03:41,840 --> 00:03:44,760 to keep you from analyzing them, such as hiding the import table, 51 00:03:45,700 --> 00:03:48,960 obscuring the relocations, encrypting, compressing different parts. 52 00:03:49,260 --> 00:03:51,320 And we'll show a little bit about those things. 53 00:03:53,000 --> 00:03:57,000 So as you can see up there, that's sort of what a normal portable executable file looks like. 54 00:03:57,700 --> 00:04:03,240 To the right, you can see clear assembly instructions that make sense, API calls, that sort of thing. 55 00:04:04,200 --> 00:04:07,360 This is what a packed or obfuscated binary would look like. 56 00:04:07,500 --> 00:04:09,420 It's basically nonsense. 57 00:04:09,600 --> 00:04:13,940 You can't tell anything with a small stub of instructions that start to say this is how you decode this. 58 00:04:14,880 --> 00:04:17,440 So our goal is to get to the decoded part of the binary. 59 00:04:19,300 --> 00:04:24,000 Okay, the next technique that malware uses to protect itself is called virtual machine detection. 60 00:04:24,400 --> 00:04:26,500 And we gave a talk about this at DEF CON last year. 61 00:04:27,060 --> 00:04:29,320 There's a whole bunch of ways to do this and lots of great tools. 62 00:04:29,920 --> 00:04:32,560 We kind of like the single instruction detection method. 63 00:04:32,880 --> 00:04:36,740 This is using various descriptor tables to figure out that you're running in a virtual machine. 64 00:04:38,540 --> 00:04:40,760 Joanna Rutkowska released a great tool called Red Pill. 65 00:04:41,580 --> 00:04:42,380 There's ScoopyDoo. 66 00:04:42,480 --> 00:04:44,740 And then the tool we released last year called OCVM. 67 00:04:44,760 --> 00:04:45,140 It's called OCVM Detect. 68 00:04:46,460 --> 00:04:51,500 So basically, virtual machines try to be efficient and fast and work well. 69 00:04:51,680 --> 00:04:54,040 But they don't necessarily try to be secure or stealthy. 70 00:04:54,060 --> 00:04:54,780 They're not rootkits. 71 00:04:54,840 --> 00:04:55,860 They're just virtual machines. 72 00:04:56,220 --> 00:04:58,720 So they don't fully emulate all the bugs. 73 00:04:58,920 --> 00:05:01,340 There's ways to find signatures that you're being virtualized. 74 00:05:03,840 --> 00:05:07,880 Okay, another common thing that packers like to do is called debugger detection. 75 00:05:08,520 --> 00:05:12,000 Generally when you're analyzing a piece of software, you're going to run it in a debugger 76 00:05:12,000 --> 00:05:13,680 and step through and see what's happening. 77 00:05:14,280 --> 00:05:14,740 Okay. 78 00:05:14,760 --> 00:05:18,880 The easiest way to detect that you're being debugged, there's a Windows API call called 79 00:05:18,880 --> 00:05:19,840 Is Debugger Present? 80 00:05:20,540 --> 00:05:24,940 This checks the process execution block for a magic bit that says, hey, I'm being debugged. 81 00:05:25,440 --> 00:05:27,020 This is easy to hide from. 82 00:05:27,300 --> 00:05:28,900 Oli Debug has a plug-in that does it. 83 00:05:29,400 --> 00:05:31,980 So you can basically say, no, I'm not debugging you. 84 00:05:32,200 --> 00:05:36,320 A lot of pieces of malware will take that and believe it. 85 00:05:37,040 --> 00:05:40,140 One of the better ways to detect that you're being debugged is a timing attack. 86 00:05:41,260 --> 00:05:43,720 Basically you issue the RDTSC instruction. 87 00:05:44,300 --> 00:05:44,740 Okay. 88 00:05:44,760 --> 00:05:47,700 And see, am I executing very slowly? 89 00:05:48,560 --> 00:05:52,060 When you're running in debugger, it's thousands and thousands of times slower than just running 90 00:05:52,060 --> 00:05:52,460 normally. 91 00:05:52,860 --> 00:05:56,420 So this is a good way to detect you're being debugged, and it's pretty effective and hard 92 00:05:56,420 --> 00:05:56,960 to get around. 93 00:05:59,380 --> 00:06:01,980 Okay, so there's a few other methods. 94 00:06:02,360 --> 00:06:04,900 A good one is called breakpoint detection. 95 00:06:05,180 --> 00:06:08,200 This is where you scan for int3 instructions. 96 00:06:08,760 --> 00:06:12,040 Generally a debugger's going to issue lots of breakpoints so that you can analyze at 97 00:06:12,040 --> 00:06:13,040 each point what's going on. 98 00:06:13,040 --> 00:06:15,660 Another thing they do is called checksumming. 99 00:06:15,740 --> 00:06:18,780 This is where they check to see, has my executable been modified? 100 00:06:19,420 --> 00:06:23,020 Are they basically trying to crack me or see what's going on? 101 00:06:24,060 --> 00:06:26,520 There's even hardware debugging detection. 102 00:06:26,920 --> 00:06:30,440 There's CPU flags you can look at and say, oh, yeah, I'm in the middle of being debugged 103 00:06:30,440 --> 00:06:30,760 right now. 104 00:06:31,640 --> 00:06:34,640 People used to use a tool called softice, which has been discontinued. 105 00:06:35,460 --> 00:06:40,280 And it was pretty good for doing debugging of software, but there's a lot of different 106 00:06:40,280 --> 00:06:41,120 ways to detect it. 107 00:06:41,880 --> 00:06:44,060 There's a bounds checker signature that was pretty good. 108 00:06:46,860 --> 00:06:50,460 Okay, so this is sort of related to packing and unpacking. 109 00:06:51,100 --> 00:06:53,720 Some malware uses the structured exception handler. 110 00:06:54,500 --> 00:07:00,500 Basically what this is, is the malware will issue some sort of error, like a divide by 111 00:07:00,500 --> 00:07:06,040 zero or something else that gets passed to the structured exception handler, which says, 112 00:07:06,180 --> 00:07:07,860 okay, what handler takes care of this error? 113 00:07:08,180 --> 00:07:11,100 Well, in the case of malware, the handler is the actual unpacking. 114 00:07:11,120 --> 00:07:16,420 This makes it hard to debug or figure out, because debuggers say, oh, there's an error. 115 00:07:16,560 --> 00:07:17,140 What do I do now? 116 00:07:17,340 --> 00:07:19,140 And they crash or they stop debugging. 117 00:07:20,120 --> 00:07:23,140 There's ways to get around that, too, and we'll talk about some of those later. 118 00:07:23,660 --> 00:07:27,440 But it's a pretty good method for unpacking in a complex way. 119 00:07:30,160 --> 00:07:34,280 Shifting decode frame is one of the hardest things to deal with with obfuscated malware. 120 00:07:34,880 --> 00:07:40,660 Basically this is where each little block of instructions is encoded, and then at runtime, 121 00:07:40,660 --> 00:07:44,900 it's decoded, executed, re-encoded, and the next block comes and gets decoded. 122 00:07:45,180 --> 00:07:50,580 This means that it's very unlikely or unusual for you to have the entire binary sitting 123 00:07:50,580 --> 00:07:53,340 in memory, clear text, decoded. 124 00:07:53,580 --> 00:07:58,980 This is very hard to defeat, and some people are actually implementing it for legitimate 125 00:07:58,980 --> 00:08:01,760 uses like protecting themselves, patch guard, that sort of thing. 126 00:08:03,260 --> 00:08:07,700 The main thing, though, is that if it executes, it can be unpacked. 127 00:08:07,760 --> 00:08:10,640 At some point, the processor needs to execute valid code. 128 00:08:10,640 --> 00:08:11,580 Not gibberish. 129 00:08:11,840 --> 00:08:14,560 And so if you can observe that, you can unpack it. 130 00:08:17,020 --> 00:08:19,380 Okay, so now I'm going to go into manual unpacking. 131 00:08:19,480 --> 00:08:21,960 This is the process for deobfuscating these binaries. 132 00:08:23,460 --> 00:08:27,560 So basically the way an unpacker works, I described it a little bit before. 133 00:08:29,140 --> 00:08:30,460 There's a right to an area of memory. 134 00:08:30,620 --> 00:08:31,400 This is the decode. 135 00:08:31,520 --> 00:08:36,120 That area is then executed, and then there's more rights to memory, sometimes re-encoding 136 00:08:36,620 --> 00:08:37,600 optionally or other things. 137 00:08:38,800 --> 00:08:40,580 Since the processor can only execute. 138 00:08:40,640 --> 00:08:46,420 machine code, you can monitor this unpacking process and get to the instructions after 139 00:08:46,420 --> 00:08:47,260 they've been decoded. 140 00:08:48,500 --> 00:08:50,220 Unpacking is basically a function of timing. 141 00:08:50,460 --> 00:08:55,460 If you can get there at the right time when everything's deobfuscated, you can tell what's 142 00:08:55,460 --> 00:08:56,920 going on, you can dump it to a file. 143 00:08:57,200 --> 00:08:59,780 At some point, it has to be unpacked before it executes. 144 00:09:01,600 --> 00:09:04,780 So there's several stages to unpacking malware. 145 00:09:07,000 --> 00:09:08,520 So I'm going to go through each one briefly. 146 00:09:08,740 --> 00:09:09,780 The first one is. 147 00:09:09,780 --> 00:09:13,220 You typically want to identify what type of packer is being used. 148 00:09:15,080 --> 00:09:18,900 Sometimes you can't because there's custom packers or targeted packers that are used, 149 00:09:19,100 --> 00:09:20,980 but most of the time it's the first step you take. 150 00:09:21,500 --> 00:09:24,900 The next part, and generally one of the harder parts, is finding what's called the original 151 00:09:24,900 --> 00:09:25,500 entry point. 152 00:09:25,880 --> 00:09:31,620 This is the address at which the original binary executes. 153 00:09:31,920 --> 00:09:38,880 So when it's packed, there's an entry point for the packer, and then eventually gets to 154 00:09:38,880 --> 00:09:41,000 the original entry point of the original binary. 155 00:09:42,280 --> 00:09:46,820 Once you've got the entry point, you typically dump the process memory to a file, and then 156 00:09:46,820 --> 00:09:50,840 fix up any problems with the file, make sure it's in the right format, fix the import address 157 00:09:50,840 --> 00:09:54,180 table, and then you should be able to analyze it at this point. 158 00:09:56,160 --> 00:10:00,260 For the first stage, there's a whole bunch of different tools for identifying what type 159 00:10:00,260 --> 00:10:00,800 of packer. 160 00:10:01,740 --> 00:10:06,800 Probably the most common is called PID, and it's a little GUI tool that uses signature-based 161 00:10:07,320 --> 00:10:08,160 scanning of files. 162 00:10:08,160 --> 00:10:12,720 It has a huge database of packers, and it can tell generally what a file's packed with. 163 00:10:13,440 --> 00:10:15,900 The next is called MSFP scan. 164 00:10:16,480 --> 00:10:21,580 This comes as part of the Metasploit framework, and it was modified like last year to do packer 165 00:10:21,580 --> 00:10:23,000 identification similar to PID. 166 00:10:24,400 --> 00:10:29,000 Errol Correra wrote a great tool called PE file, which Danny's contributed a lot of code 167 00:10:29,000 --> 00:10:30,660 to to make it hardened against malware. 168 00:10:32,140 --> 00:10:34,320 So if you get a chance, check that tool out as well. 169 00:10:35,220 --> 00:10:38,020 A couple other things you can do is you can actually manually look at... 170 00:10:38,020 --> 00:10:41,720 In IDA, in the disassembly, look at section names and things like that, and there's often 171 00:10:41,720 --> 00:10:45,380 a hint, like it might say UPEX in there or something like that, and there's a bunch 172 00:10:45,380 --> 00:10:48,800 of other scanners, but the first ones are pretty much the most commonly used. 173 00:10:50,540 --> 00:10:53,120 So this screen shows you kind of what it looks like. 174 00:10:53,280 --> 00:10:58,120 These are various tools for showing how to detect what packer's being used. 175 00:10:58,680 --> 00:11:00,840 The bottom black window is MSFP scan. 176 00:11:01,200 --> 00:11:02,860 There's PID, our website. 177 00:11:05,440 --> 00:11:05,880 Okay. 178 00:11:05,980 --> 00:11:08,000 The next stage of unpacking is... 179 00:11:08,640 --> 00:11:10,440 Finding the original entry point. 180 00:11:11,040 --> 00:11:12,480 Probably the most difficult stage. 181 00:11:12,980 --> 00:11:17,780 There's a bunch of great scripts for Oli debug to do this for a lot of different packers, 182 00:11:17,840 --> 00:11:20,900 but these are very specific to packer type, packer version. 183 00:11:21,460 --> 00:11:23,680 When new versions come out, these scripts don't work. 184 00:11:24,160 --> 00:11:29,200 OpenRCE is probably one of the best repositories for reverse engineering scripts, so definitely 185 00:11:29,200 --> 00:11:29,860 check that out. 186 00:11:30,580 --> 00:11:33,820 There's a lot of other tools, but again, they're signature based, they're not generic. 187 00:11:34,180 --> 00:11:35,880 They're GUI tools, hard to automate. 188 00:11:36,620 --> 00:11:37,900 So that's why we started... 189 00:11:37,900 --> 00:11:40,140 To work on what we'll talk about later. 190 00:11:41,840 --> 00:11:45,600 So this sort of shows the process of finding the original entry point. 191 00:11:46,160 --> 00:11:50,320 Up there at the top, you can see in green a little address highlighted. 192 00:11:50,640 --> 00:11:52,760 That's the original entry point in this particular case. 193 00:11:55,360 --> 00:11:59,980 So once you've identified the entry point, then you're going to dump the process memory 194 00:11:59,980 --> 00:12:03,140 of the process you're trying to get to a file. 195 00:12:03,440 --> 00:12:04,540 There's a lot of tools for this. 196 00:12:04,640 --> 00:12:06,680 Oli dump is a plug-in for Oli debug. 197 00:12:06,960 --> 00:12:07,500 Lord PE. 198 00:12:07,900 --> 00:12:09,160 And you can write your own. 199 00:12:09,420 --> 00:12:10,340 That's what we've done. 200 00:12:10,920 --> 00:12:12,640 This is basically the flow, though. 201 00:12:12,720 --> 00:12:16,100 These four API calls will get you what you need to dump the file. 202 00:12:16,700 --> 00:12:18,660 Basically open process, reprocess memory. 203 00:12:19,000 --> 00:12:19,860 It's pretty simple. 204 00:12:21,880 --> 00:12:25,760 This is a screenshot of Lord PE dumping a particular process to a file. 205 00:12:28,440 --> 00:12:33,320 Okay, one of the final stages is fixing up any problems with the file format, rebuilding 206 00:12:33,320 --> 00:12:34,380 the import address table. 207 00:12:34,700 --> 00:12:36,980 It turns out it's not a hard problem. 208 00:12:37,020 --> 00:12:37,880 But it's a tedious problem. 209 00:12:37,900 --> 00:12:39,740 So there's not very many tools that do it. 210 00:12:40,060 --> 00:12:41,680 The main tool is called import rec. 211 00:12:42,120 --> 00:12:43,340 And it's a GUI tool. 212 00:12:43,540 --> 00:12:44,580 Not very automatable. 213 00:12:44,740 --> 00:12:46,300 But it's probably the best tool out there. 214 00:12:46,580 --> 00:12:49,800 You can also do this by hand with a hex editor, but I wouldn't recommend it. 215 00:12:49,860 --> 00:12:50,360 Take days. 216 00:12:53,280 --> 00:12:58,520 So this is a screenshot of import rec and another tool called revergen, trying to rebuild 217 00:12:58,520 --> 00:13:00,100 the import table on a particular file. 218 00:13:02,680 --> 00:13:06,340 Okay, so at this point, once you've got the OEP, dumped the file. 219 00:13:06,340 --> 00:13:10,440 You should have clean disassembly, something that you can analyze in IDA Pro and see what's 220 00:13:10,440 --> 00:13:10,860 going on. 221 00:13:11,120 --> 00:13:16,080 There should be internal functions visible, strings that make sense, unless they've done 222 00:13:16,080 --> 00:13:17,060 other encoding afterwards. 223 00:13:18,380 --> 00:13:19,840 This process is pretty manual. 224 00:13:19,940 --> 00:13:23,720 It can take, you know, like an average of four hours or so per file. 225 00:13:23,940 --> 00:13:27,600 And if you have 100,000 files you have to unpack, you're never going to get done. 226 00:13:28,340 --> 00:13:30,660 The hardest part is generally finding the original entry point. 227 00:13:32,100 --> 00:13:35,160 Once file's unpacked, this is basically kind of what it'll look like. 228 00:13:36,340 --> 00:13:40,060 All kinds of stuff visible, lots of information, and we're going to show a quick demo. 229 00:13:53,960 --> 00:13:58,600 Okay, so this demo is basically manually unpacking a file that's been packed with UPEX. 230 00:14:01,640 --> 00:14:04,000 First thing we're going to do is identify what packer it is. 231 00:14:04,620 --> 00:14:05,740 This tool, PIDA. 232 00:14:05,760 --> 00:14:06,320 You can see down here. 233 00:14:06,340 --> 00:14:08,400 It shows UPEX 0.89. 234 00:14:09,560 --> 00:14:12,560 And I'm going to prove it's actually a valid file, so I execute it. 235 00:14:12,680 --> 00:14:13,300 Open support. 236 00:14:13,640 --> 00:14:15,080 This is a real piece of malware. 237 00:14:15,160 --> 00:14:16,480 It's a backdoor called NetBull. 238 00:14:20,300 --> 00:14:24,240 Then the next thing we're going to do is open IDA Pro so you can see how broken the analysis 239 00:14:24,240 --> 00:14:25,780 is because of the packer. 240 00:14:28,120 --> 00:14:29,880 So IDA Pro's going to start to analyze it. 241 00:14:29,980 --> 00:14:31,060 It gave us a few errors. 242 00:14:31,920 --> 00:14:33,800 You can see the names window is basically empty. 243 00:14:33,980 --> 00:14:36,120 It says start, but there's not a lot of information there. 244 00:14:36,340 --> 00:14:38,840 There's no internal functions available at all. 245 00:14:39,500 --> 00:14:41,300 The import table is pretty sparse. 246 00:14:41,500 --> 00:14:44,580 Generally this has got a hundred things in it or more. 247 00:14:48,780 --> 00:14:54,340 So usually the next thing you would do is put the file into Oli Debug and load a OEP 248 00:14:54,340 --> 00:14:54,980 finding script. 249 00:14:55,300 --> 00:14:56,880 And that's exactly what we're going to do here. 250 00:14:57,000 --> 00:14:58,960 Oli Debug even detected that something wasn't right. 251 00:14:59,020 --> 00:14:59,420 It was packed. 252 00:15:01,680 --> 00:15:04,080 So we have this script called OEP Finder for UPEX. 253 00:15:04,260 --> 00:15:05,020 We run that. 254 00:15:06,340 --> 00:15:12,180 And you can see the top address there, 40BB08, is the original entry point. 255 00:15:12,320 --> 00:15:17,020 So we're going to go ahead and dump this file, this process to a file so we can try to analyze 256 00:15:17,020 --> 00:15:17,260 it. 257 00:15:19,680 --> 00:15:23,280 Load PE is really nice for doing this, but it's kind of manual. 258 00:15:23,360 --> 00:15:25,380 You can see lots of windows, lots of stuff to click through. 259 00:15:25,460 --> 00:15:26,500 You can't really automate this. 260 00:15:28,460 --> 00:15:31,060 So dump.exe is the name of the file that we dumped. 261 00:15:32,760 --> 00:15:34,540 I'm going to go ahead and rebuild the imports table. 262 00:15:37,660 --> 00:15:42,120 So import rec is going through, scanning the file, trying to see what DLLs are imported, 263 00:15:42,340 --> 00:15:44,280 what functions are being used from the DLLs. 264 00:15:44,440 --> 00:15:49,080 You can kind of see that stuff scrolling by there, like socket, send, etc. 265 00:15:50,840 --> 00:15:53,180 Okay, so we tell import rec to fix the file. 266 00:15:57,440 --> 00:16:01,740 And now we're going to open the dumped, repaired file in IDA Pro, and you should see a totally 267 00:16:01,740 --> 00:16:02,300 different view. 268 00:16:03,260 --> 00:16:03,700 Okay. 269 00:16:04,260 --> 00:16:04,700 Okay. 270 00:16:05,880 --> 00:16:06,320 Okay. 271 00:16:09,520 --> 00:16:11,720 So it's beginning to analyze our file. 272 00:16:15,260 --> 00:16:16,980 Blue up there at the top is good. 273 00:16:17,060 --> 00:16:18,480 That means it's actually finding good code. 274 00:16:18,800 --> 00:16:20,500 So there are the imports tables, fully populated. 275 00:16:20,720 --> 00:16:26,180 You can see like registry, key modifications, can create files, names, windows populated. 276 00:16:27,100 --> 00:16:29,000 There's a bunch of internal functions available. 277 00:16:32,140 --> 00:16:35,040 And here's assembly instructions that make sense. 278 00:16:35,040 --> 00:16:38,160 So this is a lot different than the original view when it was packed. 279 00:16:38,320 --> 00:16:39,780 There's a lot more information we can find there. 280 00:16:41,980 --> 00:16:48,020 So that's the tedious manual process of manually unpacking software. 281 00:16:51,720 --> 00:16:53,060 And one second here. 282 00:17:02,000 --> 00:17:02,740 So what? 283 00:17:02,740 --> 00:17:07,500 But, you know, people know how to manually unpack software, and we need to basically 284 00:17:07,500 --> 00:17:08,540 automate this method. 285 00:17:09,520 --> 00:17:13,680 It takes a lot of work to go through this process, and if you've got a malware collection 286 00:17:13,680 --> 00:17:19,940 of a significant size, you're going to need a lot of time and a lot of patience, which, 287 00:17:20,100 --> 00:17:21,020 you know, is hard to come by. 288 00:17:22,500 --> 00:17:28,280 So the unpacking, this is a very automatable process, and we need a generic way to debug 289 00:17:28,280 --> 00:17:28,520 it. 290 00:17:28,580 --> 00:17:32,240 We need a way to modify this process at a fundamental level. 291 00:17:32,360 --> 00:17:32,720 Okay. 292 00:17:32,740 --> 00:17:38,100 Furthermore, we want it to work generically across all executables and all packed binaries. 293 00:17:38,260 --> 00:17:42,180 We want it to be efficient so that we don't have to spend a lot of computing time doing 294 00:17:42,180 --> 00:17:42,500 this. 295 00:17:43,160 --> 00:17:48,720 We'd like to be as undetectable as we can, and we should make it so that it's extensible 296 00:17:48,720 --> 00:17:50,460 and it can be automated fairly easily. 297 00:17:52,520 --> 00:17:58,980 So again, the algorithm that's been stated in many papers is that you can track written 298 00:17:58,980 --> 00:17:59,420 memory. 299 00:17:59,900 --> 00:18:02,720 If you see any execution inside of that written memory, you can track it. 300 00:18:02,720 --> 00:18:03,820 If you don't see any execution inside of that written memory, there's a good chance 301 00:18:03,820 --> 00:18:09,940 that it's unpacked, because the unpacker has to modify the code and then execute it. 302 00:18:10,160 --> 00:18:14,920 So the two things we have to monitor for are memory writes and executes. 303 00:18:15,640 --> 00:18:17,440 And then again, we want to automate this. 304 00:18:18,240 --> 00:18:24,440 The first example of a good way to do this is dynamic instrumentation, and specifically 305 00:18:25,180 --> 00:18:26,420 Intel's PIN library. 306 00:18:26,880 --> 00:18:29,680 This is a real good dynamic instrumentation library. 307 00:18:29,880 --> 00:18:32,620 It uses a just-in-time component. 308 00:18:32,700 --> 00:18:35,420 It uses a compiler to insert code before instructions. 309 00:18:36,820 --> 00:18:41,860 PIN retains the consistency of the executable, so when it goes to look at itself and inspect 310 00:18:41,860 --> 00:18:46,520 to see if anything's been modified, it can see if there's been any changes to it. 311 00:18:46,640 --> 00:18:47,840 And PIN allows that to go through. 312 00:18:48,560 --> 00:18:50,360 And it gives you good control of execution. 313 00:18:50,820 --> 00:18:52,500 You can do it at the instruction level. 314 00:18:52,740 --> 00:18:58,140 You can do it based on a memory read, memory write, or any other memory access, and at 315 00:18:58,140 --> 00:18:59,100 the basic block level. 316 00:18:59,820 --> 00:19:02,200 The nice thing about PIN is it allows you to... 317 00:19:02,700 --> 00:19:05,780 attach and detach at will, and still keep the program running. 318 00:19:08,460 --> 00:19:11,640 So this is what normal instructions look like. 319 00:19:11,820 --> 00:19:12,940 This is just assembly. 320 00:19:15,540 --> 00:19:21,460 PIN inserts itself before each instruction, provided you tell it to insert your callbacks 321 00:19:21,460 --> 00:19:23,220 before each instruction is executed. 322 00:19:24,020 --> 00:19:28,860 And the end of executable looks like this, is that you have an instruction, you have 323 00:19:28,860 --> 00:19:31,820 the PIN instructions, and then you can look at this. 324 00:19:32,700 --> 00:19:37,160 It allows you to take a look at both the instructions, the memory, and all this. 325 00:19:39,000 --> 00:19:45,520 So what we did is that we used the PIN hooks or the API to watch for memory writes and 326 00:19:45,520 --> 00:19:46,240 executes. 327 00:19:46,400 --> 00:19:49,020 And then we just tracked the writes inside of a hash table. 328 00:19:49,480 --> 00:19:54,580 And the simple heuristic we use is that if we see an execution on one of those written 329 00:19:54,580 --> 00:19:56,180 pages, then we dump. 330 00:19:57,860 --> 00:19:59,740 And it turns out this is pretty successful. 331 00:20:00,000 --> 00:20:02,580 So for the most common packers... that is... 332 00:20:02,700 --> 00:20:05,320 70% of them, this works pretty well. 333 00:20:05,620 --> 00:20:09,060 What it doesn't work on are packers that self-verify. 334 00:20:10,720 --> 00:20:16,200 This is a highly scientific sampling of the known malware, which is what we have. 335 00:20:16,400 --> 00:20:20,820 And this is the top packers that we see. 336 00:20:21,040 --> 00:20:28,040 Primarily we see UPX as a packer, which this dynamic instrumentation method is able to 337 00:20:28,040 --> 00:20:28,400 get through. 338 00:20:28,760 --> 00:20:30,900 P-Compact is a close second. 339 00:20:31,760 --> 00:20:32,280 Armadillo. 340 00:20:32,280 --> 00:20:36,480 We'll ignore that one until the second technique, because this doesn't work on that. 341 00:20:36,740 --> 00:20:40,380 But FSG, ASPack, and ASProtect, those all work. 342 00:20:42,580 --> 00:20:47,620 So what we get is really good results on these four end packers. 343 00:20:48,320 --> 00:20:53,540 And just to validate the results, we created a simple hello world application. 344 00:20:54,860 --> 00:20:56,480 Probably the same one everybody's written. 345 00:20:56,680 --> 00:21:01,980 And then graphed the results on the basic block using a program called GDE. 346 00:21:02,280 --> 00:21:04,520 Or Go Visual Diagram Editor, or AS. 347 00:21:05,960 --> 00:21:12,420 So this chart here, each of these blocks represents a basic block inside of the executable. 348 00:21:12,620 --> 00:21:16,920 And every connection represents a transition from one basic block to another. 349 00:21:18,240 --> 00:21:22,200 So you can probably guess that this is Windows, because it's a lot bigger than you would expect. 350 00:21:23,020 --> 00:21:26,080 But what we're looking for here is an unpacking loop. 351 00:21:26,280 --> 00:21:31,320 We're looking for lots of... or a tightly coupled loop referencing itself. 352 00:21:32,280 --> 00:21:34,000 And in a pretty tightly bound area. 353 00:21:34,360 --> 00:21:39,380 So the thing with the most connections that's visible here in the hello world is on the 354 00:21:39,380 --> 00:21:40,300 lower right hand corner. 355 00:21:41,020 --> 00:21:45,680 And if we zoom in on this view, we can see that there's actually good execution in here. 356 00:21:45,860 --> 00:21:46,940 And it's pretty unreadable. 357 00:21:47,200 --> 00:21:54,160 But inside of each one of those boxes is a listing of the address that's been executed. 358 00:21:54,460 --> 00:21:58,380 And manually verifying it, you can go and look at this in OLLI. 359 00:21:58,620 --> 00:22:00,620 And it's actually the unpacking loop. 360 00:22:00,840 --> 00:22:00,960 Okay. 361 00:22:01,780 --> 00:22:02,260 Great. 362 00:22:03,160 --> 00:22:07,140 So the generic algorithm that we described previously works pretty well. 363 00:22:07,680 --> 00:22:12,720 And again, we have verified everything manually by manually unpacking it before we threw it 364 00:22:12,720 --> 00:22:13,280 at a lot of malware. 365 00:22:14,740 --> 00:22:19,880 All the addresses display clustering, which you can see on that previous graph. 366 00:22:20,260 --> 00:22:23,540 And what's nice about PIN is that you get the attach and detach. 367 00:22:23,940 --> 00:22:28,560 So that you can take a snapshot of an executable, get out of its way, and that sort of thing. 368 00:22:28,620 --> 00:22:30,500 So you could actually get around some of the other methods. 369 00:22:30,720 --> 00:22:31,440 Or the... 370 00:22:32,280 --> 00:22:32,840 Obfuscation methods. 371 00:22:34,420 --> 00:22:34,860 Okay. 372 00:22:34,940 --> 00:22:40,400 So Val's going to show you a demo of the dynamic instrumentation version. 373 00:22:45,060 --> 00:22:45,760 All right. 374 00:22:45,860 --> 00:22:49,440 So what we're going to do is unpack some malware. 375 00:22:57,140 --> 00:22:57,580 Okay. 376 00:22:57,760 --> 00:23:00,000 So we're going to go through the same steps. 377 00:23:00,060 --> 00:23:02,120 Just to show you that we're being honest. 378 00:23:02,180 --> 00:23:02,260 Okay. 379 00:23:02,280 --> 00:23:04,580 This one's packed with a packer called FSG. 380 00:23:05,720 --> 00:23:07,360 It's also a real piece of malware. 381 00:23:07,500 --> 00:23:08,220 Same one, NetBull. 382 00:23:09,040 --> 00:23:12,140 It actually executes and opens a port, does what it's supposed to do. 383 00:23:17,200 --> 00:23:20,320 We're going to go ahead and make sure that it's not running and that I've killed it. 384 00:23:20,840 --> 00:23:24,440 I'm going to open it up in IDA so you can see how messed up the analysis is. 385 00:23:26,920 --> 00:23:28,500 So IDA gives us a little error there. 386 00:23:29,020 --> 00:23:29,680 You look... 387 00:23:29,680 --> 00:23:30,060 Same... 388 00:23:30,060 --> 00:23:32,060 Basically, same stuff as UPEX. 389 00:23:32,260 --> 00:23:33,700 The names window's empty. 390 00:23:34,220 --> 00:23:35,860 There's no functions for us to look at. 391 00:23:37,100 --> 00:23:38,820 Imports are going to be all pretty much gone. 392 00:23:39,960 --> 00:23:43,880 So this file, as far as analysis goes, is useless to us. 393 00:23:43,960 --> 00:23:45,140 So we need to unpack it. 394 00:23:46,420 --> 00:23:47,860 So we're going to fire up our tool here. 395 00:23:49,600 --> 00:23:51,980 Run it against the FSG packed NetBull. 396 00:23:52,100 --> 00:23:52,820 It's going to run. 397 00:23:53,840 --> 00:23:58,440 And right now what it's doing is it's detecting memory reads, writes, executes, and dumping 398 00:23:58,440 --> 00:23:59,180 files for us. 399 00:24:02,720 --> 00:24:05,860 And second, you should see, yeah, there's a bunch of dump files up here. 400 00:24:06,800 --> 00:24:09,820 Each file is named with what we call the candidate OEP address. 401 00:24:11,480 --> 00:24:14,000 We're working on automating the import table rebuilding. 402 00:24:14,920 --> 00:24:16,820 We just ran out of time, but we'll have that soon. 403 00:24:17,500 --> 00:24:21,040 But for now, I'm going to go ahead and rebuild it with import rec just to show that it's 404 00:24:21,740 --> 00:24:23,440 memory's all in the right place, everything's good. 405 00:24:27,660 --> 00:24:28,960 So we're going to pick... 406 00:24:29,680 --> 00:24:30,040 Okay. 407 00:24:30,060 --> 00:24:34,440 This dump file right here of FSG, and it turns out it's the same original entry point 408 00:24:34,440 --> 00:24:36,420 as we found earlier with the OLLI debug script. 409 00:24:37,240 --> 00:24:39,260 So that's one way to validate our results. 410 00:24:39,440 --> 00:24:41,700 We know we're actually finding the true original entry point. 411 00:24:42,580 --> 00:24:43,940 So we've prepared that dump file. 412 00:24:44,200 --> 00:24:50,500 We're going to open up the dynamic instrumentation dump file that we created, and we'll get 413 00:24:50,500 --> 00:24:51,300 a totally different view. 414 00:24:52,500 --> 00:24:55,940 The nice thing is that now there's a command line tool for doing this that you can script, 415 00:24:55,980 --> 00:24:57,720 automate, pass out a bunch of files. 416 00:25:00,460 --> 00:25:04,920 So you can see already right away, you know, the name's Windows fully populated. 417 00:25:06,980 --> 00:25:08,640 There's going to be valid assembly instructions. 418 00:25:09,180 --> 00:25:12,200 And this is a memory dump, sort of like what you do with Lord PE. 419 00:25:12,460 --> 00:25:14,700 So in here's a bunch of internal functions to the file. 420 00:25:16,480 --> 00:25:21,700 Imports window, same stuff, basically as we saw before, except it's a much more automated 421 00:25:21,700 --> 00:25:22,860 technique for doing this. 422 00:25:25,740 --> 00:25:28,820 And you can see here, valid assembly instructions. 423 00:25:32,560 --> 00:25:34,000 All kinds of interesting stuff. 424 00:25:35,200 --> 00:25:37,280 And yeah, IDA's graphing. 425 00:25:37,600 --> 00:25:42,360 So one way to tell how good your unpack is, is if you can actually follow cross-references. 426 00:25:42,620 --> 00:25:44,020 And it turns out we can. 427 00:25:44,140 --> 00:25:47,980 So we've got a very good unpack dump of this file. 428 00:25:48,500 --> 00:25:53,180 And there's some strings that are actually here, English readable, makes sense, whereas 429 00:25:53,180 --> 00:25:54,580 previously they were basically garbage. 430 00:25:56,680 --> 00:25:59,840 So it turns out the first technique's pretty good at unpacking these things. 431 00:26:05,300 --> 00:26:06,220 All right. 432 00:26:06,820 --> 00:26:07,780 Back to Danny. 433 00:26:10,180 --> 00:26:11,900 There are some problems with it. 434 00:26:12,420 --> 00:26:17,140 First of all, using PIN, since it puts itself inside the process's memory, it's extremely 435 00:26:17,140 --> 00:26:17,860 detectable. 436 00:26:18,460 --> 00:26:24,820 If a packer actually goes and does a memory checksum, like Armadillo or TLOC do, it'll 437 00:26:24,820 --> 00:26:25,320 find it. 438 00:26:26,500 --> 00:26:29,020 So there's some more work that needs to be done. 439 00:26:29,180 --> 00:26:30,040 The first one is we need to find the memory. 440 00:26:30,060 --> 00:26:34,520 We need to extend this to a method that's generic and not detectable. 441 00:26:35,820 --> 00:26:37,640 PIN is currently extremely slow. 442 00:26:37,860 --> 00:26:40,340 It's about a thousand times slower than native at best. 443 00:26:40,540 --> 00:26:44,780 And other tools, if you do any sort of other analysis, can be even slower. 444 00:26:44,960 --> 00:26:46,620 So we need to find a faster implementation. 445 00:26:47,940 --> 00:26:53,260 So we need some core operating system component that monitors all the memory, intercepts all 446 00:26:53,260 --> 00:26:58,980 memory accesses, is fast, and facilitates logging, and is a fundamental part of the 447 00:26:58,980 --> 00:26:59,240 OS. 448 00:27:00,060 --> 00:27:00,220 It's a very complex system. 449 00:27:00,220 --> 00:27:02,240 It's not the kind of OS that the packer expects to be there. 450 00:27:03,140 --> 00:27:06,660 So to do that, we overload the memory management unit. 451 00:27:06,860 --> 00:27:12,560 And so this is kind of going to go into an OS 101, or how virtual memory works, to explain 452 00:27:12,560 --> 00:27:13,360 the process a bit. 453 00:27:14,400 --> 00:27:20,080 So the Intel memory management has a system where each process has its own dedicated memory. 454 00:27:20,520 --> 00:27:25,900 And each address must be translated from virtual memory to an actual physical address, which 455 00:27:25,900 --> 00:27:28,300 corresponds to addresses inside of your stick of RAM. 456 00:27:28,300 --> 00:27:34,700 And non-physical address extension 32-bit processors use a two-page index and a byte 457 00:27:34,700 --> 00:27:34,980 index. 458 00:27:35,240 --> 00:27:37,440 And each process has its own page directory. 459 00:27:38,100 --> 00:27:42,720 So if we go through a translation, we start off with our own virtual address. 460 00:27:43,240 --> 00:27:48,160 And the CPU will divide that up by its virtual page number. 461 00:27:48,620 --> 00:27:52,220 The very first 10 bits are going to be the page directory index. 462 00:27:52,820 --> 00:27:58,280 And that's going to be used in conjunction with the CR3 register to find the page directory. 463 00:27:59,280 --> 00:28:03,900 That's then used to find the page table entry and the page table index. 464 00:28:04,160 --> 00:28:09,580 And inside that page table entry, you're going to find supervisor bits, control bits, and 465 00:28:09,580 --> 00:28:10,120 that sort of thing. 466 00:28:10,220 --> 00:28:11,480 So that's where we're going to unload it. 467 00:28:11,820 --> 00:28:16,320 And then at the very end, the byte index is going to find the actual address inside of 468 00:28:16,320 --> 00:28:17,380 the page that's desired. 469 00:28:19,380 --> 00:28:25,080 So all the page directory and page table information is a hardware-defined data structure. 470 00:28:25,880 --> 00:28:28,060 And that all contains things like . . . 471 00:28:28,060 --> 00:28:28,520 . . . permissions. 472 00:28:28,900 --> 00:28:32,260 So for instance, you don't want kernel code to be able to . . . I'm sorry. 473 00:28:32,340 --> 00:28:36,060 You don't want user code to be able to access kernel code. 474 00:28:36,660 --> 00:28:41,280 You also need to know whether the particular bit of memory has been paged to a disk. 475 00:28:42,140 --> 00:28:47,580 And it also . . . there's also user-defined bits which is how the U.S. or how Microsoft 476 00:28:47,580 --> 00:28:49,260 implements demand paging. 477 00:28:51,320 --> 00:28:56,780 So this is somewhat of a difficult or a time-intensive process. 478 00:28:56,780 --> 00:29:02,300 so what Intel has done is they've implemented a TLB or a translation look-aside buffer and its 479 00:29:02,300 --> 00:29:07,000 whole process is to do these translations and then once it's translated to keep it into an 480 00:29:07,000 --> 00:29:14,680 associative cache and whenever whenever there's a page fault like say for instance the page isn't 481 00:29:14,680 --> 00:29:20,640 loaded into RAM or they're incorrect privileges or there's demand paging then that'll that'll 482 00:29:20,640 --> 00:29:27,780 actually hit that so and these are all indicated by special fields in there Intel further optimizes 483 00:29:27,780 --> 00:29:32,540 memory lookups into two two domains the first one is data so if you're referencing inside of 484 00:29:32,540 --> 00:29:38,500 a data structure or referencing an array that'll hit the data TLB instructions actual executions 485 00:29:38,500 --> 00:29:44,260 hit the instruction TLB and the instruction TLB is a little bit more optimized because less lookups 486 00:29:44,260 --> 00:29:50,040 that means faster code and and when it comes down to it the DTLB is accessed less and 487 00:29:50,640 --> 00:29:57,380 so to load addresses into the DTLB you simply reference memory so that top instruction the 488 00:29:57,380 --> 00:30:03,100 same thing as dereferencing a pointer and see instruction TLB the way you can manually load 489 00:30:03,100 --> 00:30:08,680 this is take the address that you want to execute just swap it out with a ret instruction which is 490 00:30:08,680 --> 00:30:17,680 a Xerox c3 call it return and then restore it and that'll manually load the TLB so when you 491 00:30:17,680 --> 00:30:20,180 actually get a memory lookup the first thing that 492 00:30:20,640 --> 00:30:24,520 happens is that the hardware determines whether it's present in the cache and if it is and it just 493 00:30:24,520 --> 00:30:29,060 returns it if it's not there then it the hardware actually goes through the process of walking the 494 00:30:29,060 --> 00:30:34,940 page directory and once it finds it in there it acts it looks and see if the page table is valid 495 00:30:34,940 --> 00:30:42,660 if it's not then it pay swaps over to the operating system on the page fault handler so if 496 00:30:42,660 --> 00:30:46,640 it's page to disk it retrieves it and then the next thing that it looks at is are the permissions 497 00:30:46,640 --> 00:30:50,620 correct and this permission checking portion of the DTLB is the permission checking portion of the 498 00:30:50,640 --> 00:30:59,420 page fault handler is what we we use for our own purposes so we came up this tool named saffron if 499 00:30:59,420 --> 00:31:06,740 there's any firefly fans in the audience that's where it came from so what saffron is is it's a 500 00:31:06,740 --> 00:31:14,120 Intel pin and the hybrid page fault handler it was inspired by Ollie bone by Joe Stewart this is 501 00:31:14,120 --> 00:31:18,620 really great work we saw it at DEF CON last year and started looking at this so Joe if you're out 502 00:31:18,620 --> 00:31:19,220 there thank you 503 00:31:20,640 --> 00:31:27,580 it of course it's designed for 32-bit Intel CPUs and we replaced the Windows trap handler and log 504 00:31:27,580 --> 00:31:33,980 memory accesses there were some previous work on this Jamie Butler and Sherry Sparks wrote the 505 00:31:33,980 --> 00:31:40,600 shadow Walker rootkit which is actually used to hide memory access or our rootkits inside of the 506 00:31:40,600 --> 00:31:46,740 kernel packs packs actually implements non executable frames inside of the page fault 507 00:31:46,740 --> 00:31:50,620 handler it's another good work but that's for Linux again all of this work is done by the 508 00:31:50,640 --> 00:31:57,240 Sfully bone by Joe Stewart this is break on execute for identifying these things and this is real 509 00:31:57,240 --> 00:32:02,960 good and memo lies recently came out and uninformed by scape and this is used for tracing memory 510 00:32:02,960 --> 00:32:11,620 accesses and all uses a similar technique so our implementation is we find the interrupt descriptor 511 00:32:11,620 --> 00:32:18,140 table and change out the page fault handler and saffron we slipped that in there and then just 512 00:32:18,140 --> 00:32:20,240 monitored to make sure that we're looking at the right 513 00:32:21,300 --> 00:32:27,180 memory if we find something that's interesting to us we log it and hand it off to the saffron 514 00:32:27,180 --> 00:32:33,000 monitor process and the executable that's packed is in its own set of memory and we don't have any 515 00:32:33,000 --> 00:32:40,480 direct hooks inside of it so it allows us to keep that keep it untainted of our code again 516 00:32:40,480 --> 00:32:47,120 we overload the supervisor bit we mark the supervisor bit on every valid page table entry 517 00:32:47,120 --> 00:32:52,800 and then invalidate it so that we can actually get good lookups inside of it finding the memory 518 00:32:52,800 --> 00:32:58,820 can be a little interesting the Ollie bone method was to tag the areas of memory yourself but we've 519 00:32:58,820 --> 00:33:04,180 actually automated this process we do two things we scroll through all available memory and tag 520 00:33:04,180 --> 00:33:09,980 it we also look at the portable executable header and find what the executable wants to 521 00:33:09,980 --> 00:33:16,920 load so that we can get that information out as well so once the trap to the page fault handler 522 00:33:16,920 --> 00:33:22,920 occurs we determine if we're interested in it we unset the supervisor bit to allow the process 523 00:33:22,920 --> 00:33:32,820 to load memory into the TLB and then we reset it afterwards so this we need to modify the auto 524 00:33:32,820 --> 00:33:38,120 unpacker that we had before and essentially the change that we make is that we look for written 525 00:33:38,120 --> 00:33:44,640 pages and then monitor for executions into that page and so each execution that incurs inside of 526 00:33:44,640 --> 00:33:46,900 one of those written pages is marked as our 527 00:33:46,920 --> 00:33:52,240 original entry point and that once we find that we dump the memory of the process 528 00:33:58,900 --> 00:34:05,840 so what we see our reads writes and executes we can actually control the execution of the 529 00:34:05,840 --> 00:34:10,960 program because it's essentially stopped while the page fault handler servicing it and all of 530 00:34:10,960 --> 00:34:16,120 these are extremely apparent and you know but the other thing is you only get executions for a 531 00:34:16,120 --> 00:34:16,640 certain page 532 00:34:17,200 --> 00:34:25,200 the the upside of this is that you get very accurate and very fast very fast results so we'll 533 00:34:25,200 --> 00:34:37,220 go ahead and show a demo of this here so what we're gonna do is look at T lock which is a 534 00:34:37,220 --> 00:34:43,360 particularly annoying piece of malware here so what we're going to do is load this into PID show 535 00:34:43,360 --> 00:34:44,700 you that it's actually T lock 536 00:34:46,980 --> 00:34:57,140 and then will load the U P X version too and what we're gonna do at the U P X version is use that to just show that there's a we can find the 537 00:34:57,140 --> 00:35:03,420 OEP and a different version of this so we load that up we load the olly script 538 00:35:03,420 --> 00:35:06,780 to find the original entry point you don't have to do this it's just 539 00:35:06,780 --> 00:35:10,620 illustration for what we're showing here but again we're looking at this the 540 00:35:10,620 --> 00:35:16,060 address ends with the BB08 instruction so after we do that 541 00:35:18,660 --> 00:35:27,280 The first step to do is to load the Saffron module into memory, or into the kernel, and 542 00:35:27,280 --> 00:35:35,500 once that happens, we're going to execute the Saffron kernel monitor, or the user land 543 00:35:35,500 --> 00:35:38,680 monitor process, and we give it the TLOC executable. 544 00:35:38,980 --> 00:35:43,420 So this is actually TLOC executing, and if you look at this address, the first one that 545 00:35:43,420 --> 00:35:46,680 it hit on is this original entry point that ends with BB08. 546 00:35:53,880 --> 00:35:59,000 We're going to load the packed version of TLOC just to show that it doesn't look so 547 00:35:59,000 --> 00:35:59,320 happy. 548 00:36:01,180 --> 00:36:03,200 And as you can see, there's nothing here. 549 00:36:03,340 --> 00:36:06,140 There's a start function, which is pretty useless. 550 00:36:07,600 --> 00:36:08,940 Not a lot of good information. 551 00:36:10,000 --> 00:36:12,140 There's the usual suspects. 552 00:36:12,240 --> 00:36:13,080 When you have a... 553 00:36:13,080 --> 00:36:14,260 Packed executable. 554 00:36:14,960 --> 00:36:19,420 And the strings have no useful information, except for my password, it looks like. 555 00:36:23,700 --> 00:36:27,180 And the flow graph is extremely useless, too. 556 00:36:27,240 --> 00:36:28,040 There's only one function. 557 00:36:28,980 --> 00:36:34,560 So we're going to close that, load up one of the files that we unpacked, and what we 558 00:36:34,560 --> 00:36:39,440 do is we'll take that first one that unpacked and drag that over into IDA and see how that 559 00:36:39,440 --> 00:36:39,800 looks. 560 00:36:40,540 --> 00:36:41,660 We'll let that go through. 561 00:36:44,640 --> 00:36:46,360 Things are looking a little better. 562 00:36:46,480 --> 00:36:48,480 The names window's populating. 563 00:36:50,340 --> 00:36:52,300 IDA's not screaming in pain anymore. 564 00:36:53,140 --> 00:36:54,240 Or no more than usual. 565 00:36:55,360 --> 00:36:58,260 We have functions inside of the actual executable. 566 00:37:00,000 --> 00:37:02,500 If we look at the top graph, there's lots of blue. 567 00:37:02,600 --> 00:37:04,340 Blue are code sections, so that's good. 568 00:37:04,960 --> 00:37:10,520 We have actual functions in here with assembly, and like Val said, there's a good way to 569 00:37:10,520 --> 00:37:11,360 look at... 570 00:37:11,360 --> 00:37:15,520 Look and see if there's useful information is to get cross references, and those exist 571 00:37:15,520 --> 00:37:16,200 inside of here. 572 00:37:18,460 --> 00:37:22,700 And again, we're getting a lot more useful code than we did before, and the flow graph 573 00:37:23,540 --> 00:37:24,300 looks good. 574 00:37:26,560 --> 00:37:28,580 Names exist, and it's continuing to populate. 575 00:37:33,620 --> 00:37:37,820 And we have strings as well. 576 00:37:38,660 --> 00:37:39,100 Okay. 577 00:37:41,400 --> 00:37:42,140 Here we go. 578 00:37:42,220 --> 00:37:46,720 And so we have registry keys, we have the original name of the malware that the author 579 00:37:46,720 --> 00:37:49,980 probably put in there to brag about it, and that sort of thing. 580 00:37:50,080 --> 00:37:55,600 So everything looks good, and then from here on you can do your standard analysis and go 581 00:37:55,600 --> 00:37:56,000 on to that. 582 00:38:02,040 --> 00:38:02,600 All right. 583 00:38:02,620 --> 00:38:04,040 So auto-unpack results. 584 00:38:04,400 --> 00:38:08,420 It turns out to be an extremely effective method for bypassing this detection. 585 00:38:10,060 --> 00:38:16,560 SEH decode problems are completely circumvented because we don't actually touch the executable. 586 00:38:16,780 --> 00:38:22,320 And likewise, any memory checksum that occurs, we don't modify process memory, so it's 587 00:38:22,320 --> 00:38:24,380 going to look at itself and say everything looks good. 588 00:38:24,580 --> 00:38:26,180 So we get good dumps. 589 00:38:26,200 --> 00:38:30,340 And this is effective across a good wide range of packers. 590 00:38:32,440 --> 00:38:34,420 There are some caveats to this. 591 00:38:34,620 --> 00:38:36,420 We have some system requirements. 592 00:38:36,420 --> 00:38:39,020 This only runs on XP Service Pack 2. 593 00:38:39,280 --> 00:38:43,920 And the reason for this is that Vista really isn't a target for malware authors yet because 594 00:38:43,920 --> 00:38:45,580 it's not very widely deployed. 595 00:38:46,360 --> 00:38:53,140 Data execution prevention has to be turned off because Microsoft actually uses the page 596 00:38:53,140 --> 00:38:55,620 fault handler to implement some of those. 597 00:38:55,920 --> 00:38:57,800 Second one is single CPU mode. 598 00:38:58,020 --> 00:39:00,560 There are strange mutexes that we don't have access to easily. 599 00:39:00,940 --> 00:39:03,220 So that's the other thing that has to happen. 600 00:39:03,520 --> 00:39:05,080 And it's 32-bit only. 601 00:39:05,160 --> 00:39:05,560 Again. 602 00:39:06,420 --> 00:39:07,860 64 is typically 32-bit. 603 00:39:08,060 --> 00:39:11,340 But there's no reason why this method couldn't be ported to 64-bit mode. 604 00:39:14,300 --> 00:39:16,880 Next one is that you have to run it on real hardware. 605 00:39:17,360 --> 00:39:21,800 Virtual machines, it turns out, we found out last week, play their own tricks with the 606 00:39:21,800 --> 00:39:22,760 instruction TLB. 607 00:39:23,820 --> 00:39:27,500 And furthermore, if you're using a virtual machine for this, it'll work most times. 608 00:39:27,620 --> 00:39:30,020 But in most cases, it's extremely detectable. 609 00:39:30,380 --> 00:39:33,140 And you should also take precautions because of this. 610 00:39:33,280 --> 00:39:34,760 Make a good restore procedure. 611 00:39:34,760 --> 00:39:36,860 And don't run it on the internet. 612 00:39:38,300 --> 00:39:42,820 Also, if you're looking at TLOC, make sure you don't have a kernel debugger attached. 613 00:39:44,080 --> 00:39:48,820 Because otherwise, TLOC will play its usual tricks of dividing by zero and creating strange 614 00:39:48,820 --> 00:39:49,300 exceptions. 615 00:39:51,380 --> 00:39:57,060 So what we're going to do here is we'll have an initial release of the dynamic instrumentation 616 00:39:57,060 --> 00:40:00,360 version of Saffron, which is already on Fence of Computing right now. 617 00:40:00,760 --> 00:40:04,740 And we're going to package up a kernel version and provide that to Microsoft. 618 00:40:04,760 --> 00:40:06,280 We'll do that later. 619 00:40:06,560 --> 00:40:11,500 And we'll integrate a lot of the results into Fence of Computing, and it'll be available 620 00:40:11,500 --> 00:40:13,300 next to the standard sample download. 621 00:40:15,180 --> 00:40:21,400 So what we're going to do right now is we'll have a montage sequence of packers that'll 622 00:40:21,400 --> 00:40:23,820 go by, and Val will actually do that for you. 623 00:40:24,680 --> 00:40:25,620 I'll let you... 624 00:40:25,620 --> 00:40:25,960 Thank you. 625 00:40:28,640 --> 00:40:33,980 Okay, this is sort of the you're going to die packers section. 626 00:40:35,760 --> 00:40:39,000 So what we're going to do is we're going to rapidly run through basically every packer 627 00:40:39,000 --> 00:40:43,320 we're able to get our hands on, and unpack them automatically very rapidly. 628 00:40:44,680 --> 00:40:46,380 And this is sort of just showing... 629 00:40:47,360 --> 00:40:48,580 Okay, this is the TLOC video. 630 00:40:48,700 --> 00:40:49,160 One second. 631 00:40:54,040 --> 00:40:54,400 Okay. 632 00:40:55,820 --> 00:40:59,120 So what we're going to do is generate lots of original entry points and dump files. 633 00:40:59,460 --> 00:41:00,680 The first one is ASPack. 634 00:41:00,900 --> 00:41:04,740 And you can see right there, same original entry points. 635 00:41:04,760 --> 00:41:05,240 Same entry point as before. 636 00:41:07,800 --> 00:41:08,960 ASProtect is another. 637 00:41:09,100 --> 00:41:09,980 It's a pretty good packer. 638 00:41:10,460 --> 00:41:11,820 Still falls to Saffron. 639 00:41:12,160 --> 00:41:13,140 Same entry point. 640 00:41:15,420 --> 00:41:20,140 And this is sort of like a proof of concept that, hey, look, we can unpack pretty much 641 00:41:20,140 --> 00:41:21,880 all of these commonly used packers. 642 00:41:22,000 --> 00:41:23,560 FSG, same thing. 643 00:41:23,740 --> 00:41:26,360 And it's typically going to be the first, second address. 644 00:41:26,620 --> 00:41:28,180 I think we've seen a couple within the first four. 645 00:41:31,560 --> 00:41:33,200 Here's a packer called Mew. 646 00:41:35,340 --> 00:41:38,220 And there we get the magic OEP in the first hit. 647 00:41:41,980 --> 00:41:46,200 So this method basically covers like 90% of all the packers that get used. 648 00:41:46,320 --> 00:41:49,180 There's some really hard custom ones we haven't been able to get our hands on to test. 649 00:41:49,360 --> 00:41:51,880 But, you know, there's PECompact, no problem. 650 00:41:55,120 --> 00:41:58,000 I think the next one is Petite. 651 00:41:59,220 --> 00:41:59,620 PEPack. 652 00:42:02,320 --> 00:42:04,060 There it is in the second hit. 653 00:42:08,000 --> 00:42:12,380 One thing we didn't do was test any of the advanced options that some of these packers have. 654 00:42:12,720 --> 00:42:15,700 So this is sort of the default options that they come with. 655 00:42:16,760 --> 00:42:18,300 There's Petite, third hit. 656 00:42:22,060 --> 00:42:26,980 So, yeah, if you've got a packer out there, if you have something that we don't have listed that you think is interesting, 657 00:42:27,260 --> 00:42:30,000 shoot us an email and we'll give it a test and see how it goes. 658 00:42:30,620 --> 00:42:32,220 There's PECs, no problem. 659 00:42:36,700 --> 00:42:37,500 Again, UPEX. 660 00:42:37,960 --> 00:42:41,320 This time we're doing it with the kernel version, so the dynamic instrumentation version. 661 00:42:41,860 --> 00:42:43,620 We pop that one in the second hit. 662 00:42:46,220 --> 00:42:49,320 And even scrambled version of UPEX, no problem, same thing. 663 00:42:51,620 --> 00:42:54,100 Pretty much all packers we managed to take care of. 664 00:42:54,720 --> 00:42:57,800 Armadillo's a little interesting, and I think that one's coming up pretty soon. 665 00:42:59,300 --> 00:43:02,200 We don't get exactly the same OEP, but it turns out we do. 666 00:43:02,200 --> 00:43:09,140 It turns out it's in the same page of memory, so the dump is valid and is totally readable, works fine. 667 00:43:09,860 --> 00:43:14,320 And so you can see here, every time we ran that, we generated a bunch of dump files for each one, 668 00:43:14,500 --> 00:43:17,400 named with our candidate original entry points. 669 00:43:19,300 --> 00:43:20,160 So there it is. 670 00:43:25,200 --> 00:43:25,640 Okay. 671 00:43:26,400 --> 00:43:31,680 Again, all the paper and presentation code is currently available on offensivecompeting.net, 672 00:43:31,680 --> 00:43:34,840 and we'd like you all to go there and participate in there. 673 00:43:34,980 --> 00:43:39,960 But I'd also like to take time to thank Laurie Lebrock, Houdini Scape, Bug Check, Sky Wing, 674 00:43:40,860 --> 00:43:44,160 the awesome Ty Bordell, and everybody in Uninformed and Vax. 675 00:43:45,460 --> 00:43:48,100 And at this time, I guess we're ready for questions. 676 00:43:49,200 --> 00:43:49,800 Thank you. 677 00:43:55,400 --> 00:43:55,840 Sure. 678 00:44:07,980 --> 00:44:09,000 We've not tried it yet. 679 00:44:10,100 --> 00:44:13,760 The question was if we tried it against the morphine unpacker, 680 00:44:13,780 --> 00:44:18,960 and that was another one that we didn't have time to do, but we suspect it'll work okay. 681 00:44:32,480 --> 00:44:33,380 Well, I couldn't quite hear this. 682 00:44:33,380 --> 00:44:34,780 Yeah, you might want to come up here, because we're...