1 00:00:00,000 --> 00:00:06,180 Today I'm going to be talking about unpacking, semi-automatic unpacking. 2 00:00:06,200 --> 00:00:09,900 I'm not talking about guns here, I'm actually talking about unpacking of executables, 3 00:00:10,800 --> 00:00:14,220 specifically Windows executables, because we're talking about using Alibug here. 4 00:00:14,600 --> 00:00:21,120 Alibone is a plug-in that I wrote for Alibug, so it could be applied in other ways, 5 00:00:21,240 --> 00:00:24,240 but here's the proof of concept I've got for it. 6 00:00:25,400 --> 00:00:30,600 So to get started, the problem that I have in my job as a reverse engineer, 7 00:00:30,740 --> 00:00:37,460 I have to look at malware all day long, and it's packed pretty much 95% of the time, 8 00:00:38,140 --> 00:00:41,880 and you know how many packers there are out there, there's just a ton of them, 9 00:00:42,340 --> 00:00:45,300 all different versions of them, and they all work slightly differently, 10 00:00:45,960 --> 00:00:53,180 and so what ends up happening is I've got to learn the ins and outs of a dozen or so packers 11 00:00:53,180 --> 00:00:54,220 that might be used every day. 12 00:00:54,240 --> 00:01:01,340 And it's kind of boring, once you've done it and learned exactly where to set your breakpoints 13 00:01:01,340 --> 00:01:07,680 and how to go about unpacking a particular packer, it becomes just a tedious thing at that point. 14 00:01:07,980 --> 00:01:11,760 And really I don't want to spend my time messing around with the packers, 15 00:01:11,780 --> 00:01:15,920 I want to get inside the malware and find out what it is that it's doing, 16 00:01:16,200 --> 00:01:19,840 and so the packer is just kind of an annoyance. 17 00:01:22,080 --> 00:01:24,200 So I thought I'd just kind of sum up. 18 00:01:24,240 --> 00:01:28,640 First, how people are approaching this problem right now, other than me. 19 00:01:29,620 --> 00:01:36,940 You know, reverse engineering is about as hard as you can get as far as the approaches you can take here, 20 00:01:37,020 --> 00:01:39,340 doing it manually, and that's what I end up doing, 21 00:01:39,420 --> 00:01:44,480 because actually I'd rather spend my time looking at a debugger than going out 22 00:01:44,480 --> 00:01:49,820 and trying to see if I can download something that will automatically unpack the malware that I'm looking at. 23 00:01:51,760 --> 00:01:54,200 So if we're going to go through all this trouble, 24 00:01:54,240 --> 00:01:58,300 of learning how to reverse engineer this particular packer, 25 00:01:58,320 --> 00:02:03,140 then we might as well just write an unpacking engine for it to save us some time later on. 26 00:02:03,300 --> 00:02:04,640 So that's what a lot of people have done. 27 00:02:04,720 --> 00:02:10,500 They've written these unpacking engines that are tailored to not only usually the packer, 28 00:02:10,520 --> 00:02:11,880 but a specific version of the packer. 29 00:02:11,940 --> 00:02:15,860 So as somebody releases a new packer version, you've got to get the latest unpacker version. 30 00:02:16,820 --> 00:02:21,620 And so this has taken up a lot of time if you're into this kind of thing, too. 31 00:02:21,700 --> 00:02:23,760 Like the antivirus companies, they have to do this. 32 00:02:24,240 --> 00:02:29,380 If they want to be able to scan the malware and detect if they have a signature that already matches it, 33 00:02:29,440 --> 00:02:33,480 because anybody can take the same malware and pack it a dozen different ways, 34 00:02:33,560 --> 00:02:38,300 and if the antivirus company doesn't have all of those packers and a way to work around them, 35 00:02:38,360 --> 00:02:40,940 then they're not going to detect it the other ways it's packed. 36 00:02:42,000 --> 00:02:49,860 But if they go down this route and they write an unpacking engine for each packer that's out there, 37 00:02:49,960 --> 00:02:54,220 what happens is your scanner, as you can imagine, is going to get pretty bloated after a while. 38 00:02:54,240 --> 00:02:58,900 I mean, you've got to put a lot of code in there to deal with all these specific variants of packers. 39 00:03:00,520 --> 00:03:04,800 So unfortunately, what I think a lot of the antivirus companies do is just don't bother. 40 00:03:04,980 --> 00:03:09,140 And it's kind of scary to me how often I see new malware come out. 41 00:03:09,260 --> 00:03:11,260 And, you know, it's the same old thing. 42 00:03:11,320 --> 00:03:14,340 It might be an Arabot or Agabot, something that we've seen over and over again. 43 00:03:14,460 --> 00:03:17,100 But, you know, it's been newly compiled, newly packed, 44 00:03:17,260 --> 00:03:23,440 and most of the antivirus companies don't detect it until they get a copy of it and actually write a signature. 45 00:03:25,860 --> 00:03:31,400 The other way, or one of the other ways, I guess, you can approach the problem is to emulate. 46 00:03:32,200 --> 00:03:36,860 That is, you know, write your own virtual CPU instruction decoder 47 00:03:36,860 --> 00:03:43,400 and, you know, your own executable loader that will pick up the executable, parse it, 48 00:03:43,480 --> 00:03:49,400 and then start executing your emulation engine to, you know, unpack the code gradually. 49 00:03:50,100 --> 00:03:53,740 So in this way, if you're good enough at, 50 00:03:54,240 --> 00:03:57,400 you know, emulating the CPU, usually x86, 51 00:03:58,440 --> 00:04:04,360 then you don't really have to deal with specifics of that particular packer's algorithms. 52 00:04:04,540 --> 00:04:05,060 You don't really care. 53 00:04:05,160 --> 00:04:07,040 You're just going to emulate it just like the CPU would. 54 00:04:08,480 --> 00:04:11,340 But, you know, writing these things is pretty hard. 55 00:04:11,500 --> 00:04:13,220 You know, I'm not smart enough to write one. 56 00:04:13,760 --> 00:04:18,400 So the people that do end up writing these end up, you know, either being the corporate, 57 00:04:18,640 --> 00:04:23,060 the antivirus companies, or somebody that's put a lot of time and effort into it. 58 00:04:23,060 --> 00:04:25,120 And so they generally don't give these away for free. 59 00:04:27,200 --> 00:04:28,280 There may be some exceptions. 60 00:04:29,640 --> 00:04:34,960 But, again, you also have to be very careful about, you know, accurately emulating that CPU 61 00:04:34,960 --> 00:04:39,140 because if you, you know, mess up one instruction and you, you know, load a register wrong, 62 00:04:39,280 --> 00:04:42,120 then something's going to go terribly wrong with that program and it's not going to unpack. 63 00:04:42,440 --> 00:04:43,420 So you've got to get it right. 64 00:04:46,160 --> 00:04:47,740 Another way is kind of a shortcut. 65 00:04:48,900 --> 00:04:52,560 You know, if you have no other way to get around something and you just have no other time, 66 00:04:52,560 --> 00:04:56,140 it's not a bad way for approaching malware anyway. 67 00:04:56,980 --> 00:05:01,920 Basically, you just take a, like a virtual machine perhaps in VMware, 68 00:05:02,100 --> 00:05:04,000 you just run the code. 69 00:05:04,140 --> 00:05:08,200 While it's in memory, you just dump that virtual or physical memory. 70 00:05:08,440 --> 00:05:13,560 And then you've got an image of the code after the unpacking part ran 71 00:05:13,560 --> 00:05:15,520 and you've got basically the unpack code. 72 00:05:15,800 --> 00:05:20,920 Now, it's pretty messed up in terms of, you know, where the PE headers are aligned 73 00:05:22,560 --> 00:05:25,120 and where the import tables are going to be all screwed up. 74 00:05:25,620 --> 00:05:27,100 But, you know, you've got the code. 75 00:05:27,220 --> 00:05:29,920 You could probably load it into IDA and look at it. 76 00:05:30,100 --> 00:05:31,720 And so that's what a lot of people can do. 77 00:05:32,400 --> 00:05:40,260 The problem is that you don't have that code in the same condition that it was 78 00:05:40,860 --> 00:05:43,820 when it was first run or in an unpack state. 79 00:05:44,120 --> 00:05:48,940 What happens is you've got all these variables that are initialized or uninitialized 80 00:05:48,940 --> 00:05:49,900 when the code starts. 81 00:05:50,300 --> 00:05:52,140 And then as your program runs, 82 00:05:52,560 --> 00:05:54,600 all these variables in memory get filled with other values. 83 00:05:54,820 --> 00:05:58,940 And so now you've dumped this memory position with those values in it. 84 00:05:59,060 --> 00:06:02,840 And so you're running it in a completely, you know, unknown state really. 85 00:06:04,440 --> 00:06:07,600 And you've also got to figure out where the start of the code is, 86 00:06:07,660 --> 00:06:10,560 what we call the original entry point, the OEP. 87 00:06:10,800 --> 00:06:13,060 You've got to find that in the code. 88 00:06:13,160 --> 00:06:18,000 Now, a lot of compilers, it's pretty easy to spot where that OEP is 89 00:06:18,000 --> 00:06:22,420 because they use a very specific prolog, 90 00:06:22,560 --> 00:06:25,000 to starting up the actual code in main. 91 00:06:25,800 --> 00:06:28,380 But if somebody perhaps just wrote it in assembly, 92 00:06:28,700 --> 00:06:31,160 just, you know, just a raw image that they put together, 93 00:06:31,460 --> 00:06:33,180 you know, you may not be able to tell right away. 94 00:06:33,300 --> 00:06:36,580 You might have to do some sophisticated tracing to figure that out. 95 00:06:36,700 --> 00:06:41,280 So all in all, it's a good solution if you just maybe want to unpack something 96 00:06:41,280 --> 00:06:43,720 and see what the strings are inside it. 97 00:06:43,760 --> 00:06:47,560 But in terms of running it and debugging it, it's not really a good option. 98 00:06:50,520 --> 00:06:52,540 So I thought about, you know, 99 00:06:52,560 --> 00:06:55,820 what is it that most of the packers have in common? 100 00:06:57,360 --> 00:07:01,380 And everyone that I looked at, or pretty much most of them that I looked at, 101 00:07:01,520 --> 00:07:03,440 work basically the same way. 102 00:07:03,520 --> 00:07:06,720 What they'll do is they will go through each section of code. 103 00:07:07,140 --> 00:07:09,840 They'll pack it, encrypt it, compress it somehow. 104 00:07:10,300 --> 00:07:13,760 And then they'll append a stub section at the end, 105 00:07:13,800 --> 00:07:16,240 which has their special unpacking code. 106 00:07:16,500 --> 00:07:17,960 Then they'll go to the PE header. 107 00:07:18,060 --> 00:07:22,300 They'll edit the entry point to now point to their stub code. 108 00:07:22,560 --> 00:07:26,880 And then when you start that executable, the stub code will run, 109 00:07:26,980 --> 00:07:28,740 and then it'll do its unpacking. 110 00:07:28,800 --> 00:07:30,940 And then after it's done unpacking, 111 00:07:30,960 --> 00:07:34,880 it will jump to the original entry point, the OEP, and begin running. 112 00:07:35,280 --> 00:07:38,120 And a lot of times that just happens to be the first section. 113 00:07:40,180 --> 00:07:43,640 Here's a diagram, pretty much just kind of shows you the difference, 114 00:07:43,800 --> 00:07:47,540 you know, what something looks like in the code sections of an unpacked 115 00:07:47,540 --> 00:07:49,120 and a packed executable. 116 00:07:49,180 --> 00:07:50,580 And you can see on the pack side, 117 00:07:51,140 --> 00:07:54,520 that you see a stub section at the end that's been added. 118 00:07:54,840 --> 00:07:56,800 And then that's where the entry point is. 119 00:07:57,000 --> 00:07:58,420 And then after it runs, 120 00:07:58,460 --> 00:08:00,980 we jump to that original entry point in the first section. 121 00:08:01,820 --> 00:08:03,540 So a lot of them work like this. 122 00:08:05,280 --> 00:08:06,740 So working with OLLI Debug, 123 00:08:06,900 --> 00:08:08,700 a lot of times I just sat there and said, 124 00:08:09,120 --> 00:08:13,100 you know, if I could just set a breakpoint right there on that first section of code, 125 00:08:13,300 --> 00:08:17,340 no matter where in that code it's running, 126 00:08:17,420 --> 00:08:19,600 if it would just break when it hit that one section, 127 00:08:20,260 --> 00:08:23,400 then all I'd have to do is just run the code, 128 00:08:23,560 --> 00:08:25,720 and it would break right there, 129 00:08:25,820 --> 00:08:27,180 and I'd be at the original entry point, 130 00:08:27,300 --> 00:08:28,280 and I'd be good to go. 131 00:08:28,380 --> 00:08:29,140 It'd be unpacked. 132 00:08:30,120 --> 00:08:32,400 And in a way you can kind of do this already. 133 00:08:32,840 --> 00:08:35,840 OLLI Debug lets you set a breakpoint on access 134 00:08:36,540 --> 00:08:38,680 for a particular section in memory. 135 00:08:40,200 --> 00:08:43,500 The problem with using the break on access in this way 136 00:08:43,500 --> 00:08:47,660 is that your stub code now is going to, 137 00:08:47,720 --> 00:08:49,580 before it's going to run this code, 138 00:08:49,600 --> 00:08:51,660 this code section that it's unpacking, 139 00:08:51,720 --> 00:08:55,200 it's got to read and write from it probably thousands 140 00:08:55,200 --> 00:08:58,660 or how many ever instructions there are in that code. 141 00:08:58,840 --> 00:09:01,160 So there's a lot of times that this break on access 142 00:09:01,160 --> 00:09:03,300 is just going to break because it was a read access 143 00:09:03,300 --> 00:09:05,040 or it was a write access. 144 00:09:06,080 --> 00:09:08,020 So what ends up happening here 145 00:09:08,390 --> 00:09:10,440 is that you're just going to go really, really slow 146 00:09:10,440 --> 00:09:14,020 until you figure out whether you're actually reading, writing, 147 00:09:14,100 --> 00:09:15,040 or you're executing. 148 00:09:15,300 --> 00:09:18,500 Because the way that the x86 architecture 149 00:09:18,500 --> 00:09:21,800 is designed, you really don't have a way to tell 150 00:09:22,340 --> 00:09:25,860 built in between, the difference between a read access 151 00:09:25,860 --> 00:09:26,940 and an execute access. 152 00:09:27,720 --> 00:09:29,440 And so if you're going to have to do this, 153 00:09:29,500 --> 00:09:30,920 you might as well just use tracing. 154 00:09:31,100 --> 00:09:32,760 Tracing has been around for a long time. 155 00:09:34,640 --> 00:09:37,820 The problem with tracing is that, okay, you say, 156 00:09:37,940 --> 00:09:40,580 well, all right, I want this code to stop 157 00:09:41,120 --> 00:09:44,200 whenever I'm in between this address and this address 158 00:09:44,200 --> 00:09:44,900 and I'm executing. 159 00:09:45,100 --> 00:09:46,840 So tracing is going to single step through it 160 00:09:46,840 --> 00:09:48,560 and just keep checking, am I at this address yet? 161 00:09:48,640 --> 00:09:49,520 Am I at this address yet? 162 00:09:49,640 --> 00:09:50,700 And just keep single stepping through. 163 00:09:50,840 --> 00:09:53,940 And it makes the code really, really slow in unpacking. 164 00:09:54,000 --> 00:09:55,420 And depending on how they packed it, 165 00:09:55,520 --> 00:09:59,680 it could be too slow to even be practical 166 00:09:59,680 --> 00:10:01,560 in terms of what we're talking about, 167 00:10:01,640 --> 00:10:03,960 trying to unpack things fast for malware analysis. 168 00:10:04,320 --> 00:10:06,000 Now, if you're just trying to crack a program 169 00:10:06,000 --> 00:10:08,160 because you don't want to buy the serial number, 170 00:10:08,360 --> 00:10:10,900 then it may not matter to you to spend two or three days tracing it. 171 00:10:11,080 --> 00:10:12,280 But for what we want to do, 172 00:10:12,380 --> 00:10:14,400 tracing is not really an option most of the time. 173 00:10:14,700 --> 00:10:16,820 And another problem with tracing is that, 174 00:10:16,840 --> 00:10:18,960 you can detect it pretty easily because the program, 175 00:10:19,220 --> 00:10:21,400 and several packers do this already, 176 00:10:21,620 --> 00:10:23,700 they will check to see how long it's taking to run. 177 00:10:23,820 --> 00:10:26,020 They can check to see how many CPU cycles have passed. 178 00:10:26,240 --> 00:10:27,680 And if it's taking too long, they can say, 179 00:10:27,760 --> 00:10:29,600 oh, I'm being traced and just, you know, 180 00:10:29,600 --> 00:10:31,940 change the path of execution or just quit. 181 00:10:33,680 --> 00:10:36,220 So what I said, I wish I had a way just to break 182 00:10:36,220 --> 00:10:40,500 on the execution only of a particular section of memory 183 00:10:40,500 --> 00:10:44,220 without tracing, without hitting read access or write access. 184 00:10:44,940 --> 00:10:46,380 So what I came up with, 185 00:10:46,380 --> 00:10:51,940 I call olibone, break on execute is the bone part. 186 00:10:52,560 --> 00:10:56,140 And this basically is a proof of a concept that implements 187 00:10:56,140 --> 00:10:59,660 this type of, you know, break on execute thing 188 00:10:59,660 --> 00:11:00,320 that I've been wanting. 189 00:11:00,940 --> 00:11:03,860 So the way we do this, you know, 190 00:11:03,880 --> 00:11:06,540 getting around this x86 architecture limitation, 191 00:11:07,840 --> 00:11:10,520 we borrow some ideas from the PACS project. 192 00:11:10,980 --> 00:11:15,360 Now, instead of doing what the PACS project does is, 193 00:11:15,360 --> 00:11:18,420 you know, it's designed to protect your stack 194 00:11:18,420 --> 00:11:20,400 from being executed, you know, 195 00:11:20,420 --> 00:11:21,900 in case of an overflow or your heap. 196 00:11:22,360 --> 00:11:27,220 But we're just going to tell it to protect an arbitrary page 197 00:11:27,220 --> 00:11:29,220 of memory or more than one pages of memory 198 00:11:29,220 --> 00:11:31,460 that comprise the section that we're targeting here. 199 00:11:36,580 --> 00:11:38,420 So a little bit of a review here. 200 00:11:38,520 --> 00:11:41,500 This is actually the page exec feature of PACS. 201 00:11:41,540 --> 00:11:43,400 There's more than just that one feature. 202 00:11:45,020 --> 00:11:52,460 But basically it works because the CPU has something called 203 00:11:52,460 --> 00:11:54,640 translation look-aside buffers. 204 00:11:54,940 --> 00:12:01,020 And what it does is when you go to tell the CPU that you need 205 00:12:01,020 --> 00:12:04,640 to read or execute a particular address in memory, 206 00:12:04,900 --> 00:12:10,380 it has to do a virtual address to physical address translation 207 00:12:10,380 --> 00:12:12,920 to figure out where in the physical memory to go, 208 00:12:13,400 --> 00:12:16,420 to get that piece of memory that you're asking for. 209 00:12:16,780 --> 00:12:21,240 And this is done using a page table walk and it's kind of slow. 210 00:12:21,600 --> 00:12:26,520 So what the translation look-aside buffer does is it saves 211 00:12:26,520 --> 00:12:30,200 that translation so the next time you go to look for that address, 212 00:12:30,520 --> 00:12:33,620 it's already cached for you and it makes it much, much faster. 213 00:12:34,040 --> 00:12:37,880 And the great thing about the way it was implemented is that they put 214 00:12:37,880 --> 00:12:42,660 in a separate look-aside buffer for your read and your write, 215 00:12:43,400 --> 00:12:46,360 so you can have a much faster access than your execution access. 216 00:12:48,000 --> 00:12:51,700 So what we're able to do, and this is how the page exec feature works, 217 00:12:51,800 --> 00:12:55,880 is that you can cache the read access. 218 00:12:56,240 --> 00:12:58,820 And so the next time that it goes to read the page, 219 00:12:58,880 --> 00:12:59,980 it just proceeds as normal. 220 00:13:00,400 --> 00:13:03,680 But then when it goes to execute and it looks in the cache 221 00:13:03,680 --> 00:13:09,120 and that execute translation is not there, then it has to do a page fault. 222 00:13:09,320 --> 00:13:13,120 And what happens with PACS is it takes over the page fault handler 223 00:13:13,400 --> 00:13:16,940 and then says, you know, let's find out where this page is. 224 00:13:17,080 --> 00:13:18,940 Oh, it's, you know, somebody's trying to execute the stack. 225 00:13:19,060 --> 00:13:19,940 We're going to kill this process. 226 00:13:20,400 --> 00:13:24,680 And so basically it has a way of marking these pages. 227 00:13:24,780 --> 00:13:31,400 There's the page table entry for each page has different bits 228 00:13:32,140 --> 00:13:33,020 that have different meanings. 229 00:13:33,320 --> 00:13:36,100 There's a user supervisor bit in there. 230 00:13:36,220 --> 00:13:41,340 And PACS overloads that to actually mean whether something is page protected 231 00:13:41,340 --> 00:13:41,800 or not. 232 00:13:41,800 --> 00:13:44,300 So we can do the same thing, basically. 233 00:13:46,980 --> 00:13:49,760 Instead, though, of, you know, protecting our stack and our heap, 234 00:13:49,820 --> 00:13:53,480 all we care about is that these pages of physical memory 235 00:13:53,480 --> 00:13:57,120 that belong to the process that we want to unpack, 236 00:13:58,220 --> 00:14:02,460 these are what we're going to actually tell our, you know, page protection. 237 00:14:02,600 --> 00:14:05,700 This is what we want you to prevent people from executing. 238 00:14:06,820 --> 00:14:10,340 And the other difference is instead of like PACS killing the process, 239 00:14:10,420 --> 00:14:11,520 like it's, you know, something, 240 00:14:11,800 --> 00:14:12,020 you know, something evil, 241 00:14:12,320 --> 00:14:15,580 what we're going to do is we're going to immediately tell the page fault handler 242 00:14:15,580 --> 00:14:19,460 to bail out and jump to the int1 handler for us. 243 00:14:19,640 --> 00:14:24,980 And what that lets us do is immediately return control back to olidebug. 244 00:14:25,560 --> 00:14:29,280 So what happens is in olidebug, it just throws up a single-step break 245 00:14:29,680 --> 00:14:31,220 and the program stops. 246 00:14:31,620 --> 00:14:35,800 Olidebug is right there and you're hopefully at the original entry point. 247 00:14:38,880 --> 00:14:41,780 So the way that this is implemented for us, 248 00:14:41,780 --> 00:14:46,880 for olibone, there is a DLL, which is a plug-in to olidebug, 249 00:14:47,520 --> 00:14:49,720 and then there is a kernel driver. 250 00:14:50,440 --> 00:14:55,020 And this kernel driver's job is just to implement this PACS-like page protection 251 00:14:56,060 --> 00:14:57,820 for anything that we tell it to. 252 00:15:00,200 --> 00:15:04,480 All we need to do now in our plug-in is send an IO control and just tell it, 253 00:15:04,520 --> 00:15:07,480 you know, these are the sections that we're interested in 254 00:15:07,480 --> 00:15:09,760 and it will then apply that protection to them. 255 00:15:09,760 --> 00:15:15,340 So because it's implemented like this and it's a, you know, 256 00:15:15,360 --> 00:15:21,760 kind of a split architecture, you could conceivably write an IDA pro plug-in 257 00:15:22,300 --> 00:15:25,880 that would do basically the same thing using the same kernel module. 258 00:15:26,000 --> 00:15:29,400 If you're into IDA pro, you could definitely port this over to that 259 00:15:29,400 --> 00:15:30,800 if anybody was wondering that. 260 00:15:33,560 --> 00:15:39,320 All right, so basically a walk-through of how this might work 261 00:15:39,320 --> 00:15:40,880 in real life here. 262 00:15:41,960 --> 00:15:45,220 For our data access, the first thing that's going to happen 263 00:15:45,220 --> 00:15:51,580 when we're unpacking our program is we're going to try to read or write 264 00:15:51,580 --> 00:15:52,820 from our target section. 265 00:15:53,640 --> 00:15:59,100 So the CPU looks and sees that it doesn't have anything in its cache already 266 00:15:59,100 --> 00:16:02,440 for that virtual address, so it's going to do a page table walk 267 00:16:02,440 --> 00:16:06,540 and it's going to basically generate a page fault. 268 00:16:07,440 --> 00:16:09,300 This is where we come in. 269 00:16:09,320 --> 00:16:14,580 We come in with our Ollibone sys kernel module. 270 00:16:16,380 --> 00:16:20,780 So our page fault handler takes over for the Windows page fault handler 271 00:16:20,780 --> 00:16:25,540 and just says, okay, we're going to see now if this page fault is due 272 00:16:25,540 --> 00:16:30,340 to our code running or if this is just a normal page fault. 273 00:16:30,540 --> 00:16:33,300 Now, if it's just a normal page fault for some other reason, 274 00:16:33,360 --> 00:16:35,400 it will pass it down to the system page fault handler. 275 00:16:36,740 --> 00:16:39,200 Otherwise, what it will do is it will, 276 00:16:39,320 --> 00:16:45,060 it will toggle that bit that we marked to say that this page is protected. 277 00:16:45,380 --> 00:16:47,040 So now that page is unprotected. 278 00:16:47,260 --> 00:16:49,260 And then it will read from that page. 279 00:16:49,680 --> 00:16:53,120 And what that does is once you read, it automatically caches 280 00:16:53,120 --> 00:16:57,020 in our look aside buffer that translation. 281 00:16:57,440 --> 00:17:02,100 And so then it will now reset that page table entry bit back 282 00:17:02,100 --> 00:17:03,920 to say that it's protected. 283 00:17:04,240 --> 00:17:06,760 And so now we've cached a read entry. 284 00:17:06,960 --> 00:17:08,620 The only thing that's not cached now 285 00:17:09,320 --> 00:17:10,040 is that it will execute access. 286 00:17:10,700 --> 00:17:15,820 So what happens then, our unpacking program is now done reading and writing. 287 00:17:15,960 --> 00:17:19,720 It's using that cached entry for these pages and proceeding on its way. 288 00:17:20,280 --> 00:17:24,900 And now finds that it's time to execute. 289 00:17:25,080 --> 00:17:27,740 We're at the, hopefully, at the original entry point. 290 00:17:28,920 --> 00:17:32,680 So once again, we get a page fault because it's not in the cache. 291 00:17:33,100 --> 00:17:36,920 So it checks to see is this due to Ollibone. 292 00:17:36,920 --> 00:17:40,280 If so, what it will do immediately, basically, 293 00:17:40,280 --> 00:17:42,280 is just pop one extra argument off the stack 294 00:17:42,280 --> 00:17:45,460 because the N1 handler doesn't have that extra argument. 295 00:17:45,820 --> 00:17:49,460 And jumps right to it and dumps us out where we want to be. 296 00:17:51,920 --> 00:17:56,540 So some problems that we've run into basically using this 297 00:17:57,420 --> 00:18:01,500 is that, you know, virtual machines and emulators 298 00:18:01,500 --> 00:18:06,420 don't always perfectly emulate the x86 299 00:18:07,800 --> 00:18:09,160 translation look-aside buffers. 300 00:18:09,360 --> 00:18:11,480 Now, VMware works great. 301 00:18:11,620 --> 00:18:12,560 It's pretty solid. 302 00:18:13,780 --> 00:18:16,660 If you get to something like Box and QEMU, 303 00:18:16,760 --> 00:18:17,920 they're actually emulating. 304 00:18:18,180 --> 00:18:20,700 And what they've done, I've looked at the source code, 305 00:18:20,820 --> 00:18:24,860 they've actually implemented a single translation look-aside buffer. 306 00:18:24,980 --> 00:18:27,340 So they basically, you know, ruined our whole idea 307 00:18:27,340 --> 00:18:30,580 of having these split buffers that we can, you know, 308 00:18:30,580 --> 00:18:34,960 leave one cached and use the other one as our protection mechanism. 309 00:18:34,960 --> 00:18:39,700 So theoretically, they could work if they were just to implement split TLBs. 310 00:18:39,920 --> 00:18:42,720 So right now, as far as I know, 311 00:18:42,980 --> 00:18:46,200 there's no way that this would work on Box or QEMU. 312 00:18:47,280 --> 00:18:50,240 Also, I imagine this means that Pax wouldn't work there 313 00:18:50,240 --> 00:18:52,180 with the page exec protection at least. 314 00:18:53,180 --> 00:18:56,280 And then Microsoft Virtual PC, I have not tried. 315 00:18:56,360 --> 00:19:00,200 If anybody has it and wants to throw Ollibone in there 316 00:19:00,200 --> 00:19:02,360 and see how it works, I'd be interested to hear from you. 317 00:19:09,500 --> 00:19:10,700 It's pretty easy to use. 318 00:19:11,960 --> 00:19:15,080 All you have to do is load in your executable and OLLI debug 319 00:19:15,080 --> 00:19:15,960 like you always would. 320 00:19:16,940 --> 00:19:19,920 The next thing is you have to do a little bit of guesswork 321 00:19:19,920 --> 00:19:24,420 and try to figure out what part of this executable 322 00:19:24,420 --> 00:19:28,400 is the final unpacked code section. 323 00:19:28,620 --> 00:19:31,260 What is it going to be when it's actually running unpacked? 324 00:19:31,760 --> 00:19:34,540 Like I said, it's usually the first section after the PE header, 325 00:19:34,620 --> 00:19:34,940 but not always. 326 00:19:36,300 --> 00:19:41,780 So now all you do is locate that in the memory map 327 00:19:41,780 --> 00:19:44,780 and you toggle the break on execute flag 328 00:19:44,780 --> 00:19:48,540 that's been added to the OLLI debug menu by Ollibone. 329 00:19:50,360 --> 00:19:51,520 Run the program. 330 00:19:52,960 --> 00:19:56,180 Then when you hit an int1, all you have to do 331 00:19:56,180 --> 00:19:59,620 is basically watch it come up and stop 332 00:19:59,620 --> 00:20:03,720 and decide for yourself whether it's unpacked or not really. 333 00:20:03,720 --> 00:20:06,940 I mean, Ollibone doesn't really have a way to know 334 00:20:06,940 --> 00:20:07,980 whether it's unpacked or not. 335 00:20:08,060 --> 00:20:11,180 You've got to be able to determine that from experience basically. 336 00:20:13,320 --> 00:20:21,440 So I've got a video demo here of some various packers being unpacked. 337 00:20:25,100 --> 00:20:30,220 All right, let's see if we can't kill this here. 338 00:20:33,940 --> 00:20:34,580 All right. 339 00:20:51,600 --> 00:20:53,020 I'll try to blow this up here a little bit 340 00:20:53,020 --> 00:20:54,320 so people can actually read it. 341 00:20:54,360 --> 00:20:56,520 I don't know how well you'll be able to actually see this in the back, 342 00:20:56,580 --> 00:20:58,140 but I hope you can see something. 343 00:21:03,720 --> 00:21:09,200 All right, our first executable we're going to unpack here is pack with FSG. 344 00:21:09,900 --> 00:21:12,060 And if you've ever unpacked FSG, 345 00:21:12,220 --> 00:21:16,060 you probably know that it's ridiculously easy to manually unpack. 346 00:21:16,500 --> 00:21:19,380 But, you know, just to show you how it works, we'll go through here. 347 00:21:19,700 --> 00:21:23,260 So what we're going to do first is we're going to look at the memory map 348 00:21:23,260 --> 00:21:24,440 and OLLI debug. 349 00:21:24,980 --> 00:21:29,340 And this lar fsg, these are the three sections 350 00:21:29,340 --> 00:21:31,540 that belong to our executable process. 351 00:21:32,500 --> 00:21:35,700 So we've got our PE header, first section. 352 00:21:35,820 --> 00:21:37,940 Second section is code. 353 00:21:38,180 --> 00:21:41,220 And then we've got our SFX import section. 354 00:21:41,660 --> 00:21:45,080 So we're going to highlight that code section there 355 00:21:45,080 --> 00:21:47,640 and set our break on execute toggle. 356 00:21:49,820 --> 00:21:53,060 So now what we do is just hit play and run the program, 357 00:21:53,280 --> 00:21:56,880 and immediately we come to the break on execute. 358 00:21:57,140 --> 00:21:59,640 It's telling us that it's stopped. 359 00:22:00,600 --> 00:22:01,520 So run that. 360 00:22:01,540 --> 00:22:02,700 Run our analyze code. 361 00:22:03,020 --> 00:22:07,720 And we can see here that we can now read the unpacked binary. 362 00:22:08,000 --> 00:22:11,600 This is Peter Benaya's collection he loaned to me 363 00:22:11,600 --> 00:22:12,860 for the purpose of this demonstration. 364 00:22:13,220 --> 00:22:14,180 So I appreciate that. 365 00:22:15,000 --> 00:22:16,820 But basically this is unpacked now. 366 00:22:16,960 --> 00:22:18,020 It was that easy. 367 00:22:21,500 --> 00:22:22,560 So let's do another one. 368 00:22:25,440 --> 00:22:30,100 All right, so upack, also kind of similar to FSG, 369 00:22:30,100 --> 00:22:33,280 pretty easy to unpack in this particular version. 370 00:22:33,720 --> 00:22:35,740 So we look at our memory map there again. 371 00:22:36,600 --> 00:22:38,180 There's our code section. 372 00:22:38,540 --> 00:22:40,040 And since we've looked at upack before, 373 00:22:40,200 --> 00:22:43,500 we know that's where we want to be when it's all over with. 374 00:22:46,480 --> 00:22:47,640 Set our break on execute. 375 00:22:47,940 --> 00:22:48,600 Hit play. 376 00:22:49,280 --> 00:22:50,920 And we've got a break on execute. 377 00:22:52,660 --> 00:22:54,620 Run our analysis and see what it looks like. 378 00:22:54,780 --> 00:22:56,100 And yes, it's unpacked. 379 00:22:59,940 --> 00:23:00,940 Let's do another one now. 380 00:23:00,980 --> 00:23:01,600 It's a little harder. 381 00:23:03,000 --> 00:23:03,580 All right. 382 00:23:04,080 --> 00:23:09,200 This is our demonstration of unpacking ASProtect. 383 00:23:09,280 --> 00:23:12,040 Is it ASProtect or ASProtect? 384 00:23:12,120 --> 00:23:12,740 Does anybody know? 385 00:23:14,100 --> 00:23:14,500 Okay. 386 00:23:15,420 --> 00:23:18,200 Whatever it is, it sounds a heck of a lot better than ASPAC. 387 00:23:23,400 --> 00:23:24,460 So we'll go here. 388 00:23:24,540 --> 00:23:25,340 We've loaded it in. 389 00:23:26,480 --> 00:23:30,620 Now if you'll notice here, there's something that we have to observe 390 00:23:30,620 --> 00:23:36,060 that we are actually stopped in the code section itself. 391 00:23:36,860 --> 00:23:40,040 So instead of being in a stub section to start our execution, 392 00:23:40,300 --> 00:23:41,960 we're actually in that code segment. 393 00:23:42,080 --> 00:23:44,960 So obviously we can't set a break on execute 394 00:23:45,580 --> 00:23:49,620 because we need to run it far enough to unpack. 395 00:23:49,760 --> 00:23:51,440 And if we just start our break on execute now, 396 00:23:51,520 --> 00:23:52,340 we're not going to do anything. 397 00:23:52,400 --> 00:23:53,720 We're just going to spin our wheels. 398 00:23:53,720 --> 00:23:58,640 So what I did there is just step through the first four instructions 399 00:23:58,640 --> 00:24:01,780 and basically it now jumps to another section. 400 00:24:01,980 --> 00:24:02,580 So that's good. 401 00:24:02,640 --> 00:24:03,900 We're out of that code section now. 402 00:24:03,940 --> 00:24:09,100 Now we can go back to our memory map since we're out of that section, 403 00:24:09,260 --> 00:24:11,140 and we can set our break on execute there. 404 00:24:14,260 --> 00:24:15,300 So run it. 405 00:24:15,500 --> 00:24:19,380 And uh-oh, it detected that we are running a debugger 406 00:24:19,380 --> 00:24:22,920 and is now giving us a warning message and saying, 407 00:24:22,920 --> 00:24:24,500 uh, you can't do this. 408 00:24:25,780 --> 00:24:27,420 So this is one of the things. 409 00:24:27,700 --> 00:24:30,920 You're running the code pretty much unchecked. 410 00:24:32,000 --> 00:24:34,960 So, you know, any type of debugger checks that it might do, 411 00:24:35,160 --> 00:24:36,880 you're going to fall prey to those. 412 00:24:37,000 --> 00:24:40,600 So this is something that is just kind of a trade-off. 413 00:24:40,660 --> 00:24:42,780 You're going to have to learn a little bit about these tricks 414 00:24:42,780 --> 00:24:43,980 and how to get around them. 415 00:24:46,600 --> 00:24:49,120 For ASProtect here, it's not that hard. 416 00:24:49,520 --> 00:24:52,320 Basically, the way it detected that we were running a debugger 417 00:24:52,320 --> 00:24:55,360 is just with a simple API call. 418 00:24:56,700 --> 00:24:58,700 So what we're going to do is, 419 00:24:59,140 --> 00:25:02,580 we're going to make the is debug present plug-in here 420 00:25:02,580 --> 00:25:06,880 tell the API just by setting a variable in our peb 421 00:25:06,880 --> 00:25:08,900 to tell it that there is no debugger present. 422 00:25:10,500 --> 00:25:13,600 And so once we do that, we'll just go through the steps again, 423 00:25:13,720 --> 00:25:16,720 walk through until we're at a section that's not the code segment. 424 00:25:18,660 --> 00:25:20,140 Set our break on execute. 425 00:25:21,700 --> 00:25:22,920 And hit play. 426 00:25:24,200 --> 00:25:27,340 So now we are somewhere, and it looks like we're in our code section. 427 00:25:27,540 --> 00:25:31,160 So let's look and see what actually we are executing. 428 00:25:33,400 --> 00:25:36,680 And this time we actually had to remove a bad analysis 429 00:25:36,680 --> 00:25:40,540 because Olliedebug got a little bit confused about what was what. 430 00:25:41,180 --> 00:25:44,920 And we look here, and actually we're executing a return. 431 00:25:45,160 --> 00:25:47,980 So what's happened here is we've ended up in our code section, 432 00:25:48,100 --> 00:25:49,420 but we're not done unpacking yet. 433 00:25:49,420 --> 00:25:53,680 This was just a return back into the unpacking code. 434 00:25:54,140 --> 00:25:57,160 So now what we have to do is manually go back, 435 00:25:58,460 --> 00:26:01,580 untoggle, remove the break on execute flag, 436 00:26:02,960 --> 00:26:08,000 step back through with the F7 just to step one step 437 00:26:08,000 --> 00:26:11,840 so that we return back into, we're actually on the heap now, 438 00:26:12,560 --> 00:26:13,640 executing some code. 439 00:26:14,060 --> 00:26:16,260 And so now we're out of that code section. 440 00:26:16,360 --> 00:26:17,700 We can go back to our memory map, 441 00:26:17,700 --> 00:26:20,860 and we can set our break on execute one more time. 442 00:26:21,680 --> 00:26:22,420 Run it. 443 00:26:24,220 --> 00:26:25,680 And take a while. 444 00:26:25,900 --> 00:26:27,360 And it pops up. 445 00:26:28,240 --> 00:26:30,160 And we've got another break on execute. 446 00:26:34,880 --> 00:26:38,020 So this is actually unpacked here. 447 00:26:38,180 --> 00:26:42,500 Once again, Olliedebug is a little bit confused about how the analysis looks. 448 00:26:43,260 --> 00:26:45,460 If we run the analyze code here, 449 00:26:53,500 --> 00:26:56,200 I didn't get a good chance to show you that that was unpacked 450 00:26:56,200 --> 00:26:58,480 because Olliedebug's bad analysis here 451 00:26:58,480 --> 00:26:59,900 because a lot of that is ASCII in there 452 00:26:59,900 --> 00:27:01,300 because it's an assembly language program. 453 00:27:01,440 --> 00:27:02,880 It doesn't do such a good job sometimes. 454 00:27:03,740 --> 00:27:05,000 But that was unpacked. 455 00:27:06,180 --> 00:27:06,720 All right. 456 00:27:06,980 --> 00:27:09,600 Next one, PECompact. 457 00:27:10,720 --> 00:27:14,860 Once again, we are in the code section as we start out. 458 00:27:14,900 --> 00:27:16,760 We're not starting in the stub section. 459 00:27:16,760 --> 00:27:21,020 So we're going to need to walk through until we get to another section. 460 00:27:22,480 --> 00:27:27,940 So what happens here is it actually uses an access violation exception 461 00:27:28,520 --> 00:27:30,840 to do some of its unpacking work. 462 00:27:30,980 --> 00:27:33,460 It's going to do some of the stuff in the structured exception handler. 463 00:27:35,180 --> 00:27:39,860 So all we have to do basically is step into the structured exception handler, 464 00:27:40,680 --> 00:27:42,160 and now we're in another section. 465 00:27:42,300 --> 00:27:43,140 So we're good to go. 466 00:27:43,240 --> 00:27:46,420 Just go back in and set our break on execute. 467 00:27:49,420 --> 00:27:50,420 And run it. 468 00:27:50,520 --> 00:27:53,540 And now we are back into the code. 469 00:27:53,580 --> 00:27:57,320 And we're just going to jump here. 470 00:28:09,380 --> 00:28:11,020 Going to move our break on execute 471 00:28:11,020 --> 00:28:15,060 so we can jump back to more of the unpacking code. 472 00:28:17,840 --> 00:28:20,220 Now we're going to go back in now that we're out of there 473 00:28:20,220 --> 00:28:22,020 and set our break on execute one more time. 474 00:28:23,380 --> 00:28:24,120 And hit play. 475 00:28:24,220 --> 00:28:25,100 And now it's unpacked. 476 00:28:33,560 --> 00:28:35,960 Once again, Olliedebug not doing such a great job with the analysis. 477 00:28:36,100 --> 00:28:40,560 But you can see if you look down, you don't see the text strings from Peter, 478 00:28:40,680 --> 00:28:44,580 but you actually do see the message box and exit process calls there 479 00:28:44,580 --> 00:28:45,240 towards the bottom. 480 00:28:53,840 --> 00:28:54,540 All right. 481 00:28:54,580 --> 00:28:55,420 So one more here. 482 00:28:55,620 --> 00:28:56,200 T lock. 483 00:28:59,020 --> 00:28:59,700 All right. 484 00:28:59,720 --> 00:29:02,820 This one starts outside of the code section. 485 00:29:03,160 --> 00:29:05,860 So looks pretty good here. 486 00:29:06,200 --> 00:29:08,520 Go to our memory maps. 487 00:29:08,620 --> 00:29:09,820 Set a break on execute. 488 00:29:12,640 --> 00:29:16,800 And we've actually landed already, but we're not in the code section. 489 00:29:17,000 --> 00:29:17,580 So what happened? 490 00:29:18,220 --> 00:29:20,380 Well, T lock is using single steps. 491 00:29:20,620 --> 00:29:22,940 It's using int1s for its own code. 492 00:29:23,120 --> 00:29:25,740 And it's kind of getting in the way of our use of the int1 493 00:29:25,740 --> 00:29:28,740 to tell Olliedebug when something has hit a break on execute. 494 00:29:29,700 --> 00:29:34,260 So what we're going to have to do now is we're going to have to manually walk 495 00:29:34,260 --> 00:29:38,300 through all the int1 breaks that T lock does. 496 00:29:39,320 --> 00:29:39,800 All right. 497 00:29:39,800 --> 00:29:44,620 So we're just going to basically just keep hitting shift F9 here to keep playing 498 00:29:44,620 --> 00:29:48,080 until we land at one that looks like it's in our code section now. 499 00:29:49,900 --> 00:29:52,220 And Olliedebug tells us it's a break on execute. 500 00:29:53,100 --> 00:29:54,220 Run our analysis. 501 00:29:54,640 --> 00:29:56,540 And we're unpacked. 502 00:30:00,760 --> 00:30:01,500 All right. 503 00:30:01,540 --> 00:30:04,640 So that is it for the video. 504 00:30:04,700 --> 00:30:05,440 And there's a URL. 505 00:30:05,500 --> 00:30:08,700 You can actually download this from source and binaries. 506 00:30:19,000 --> 00:30:20,360 All right. 507 00:30:20,400 --> 00:30:26,220 So let's talk now a little bit about ways around this particular method 508 00:30:26,220 --> 00:30:27,120 of unpacking. 509 00:30:31,480 --> 00:30:38,460 So like I mentioned, anything that it's doing to try to detect 510 00:30:38,700 --> 00:30:42,160 that you're running on int2 debugger is not solved by Olliedebug. 511 00:30:42,600 --> 00:30:45,180 Basically, Olliedebug is just a shortcut to get you to the OEP, 512 00:30:45,320 --> 00:30:47,280 but you've got to work around these other tricks. 513 00:30:48,660 --> 00:30:51,760 So if they do have this code, you're unfortunately going to have to learn, 514 00:30:51,880 --> 00:30:53,720 you know, what's up and figure it out. 515 00:30:56,260 --> 00:30:59,940 Other types of packers might not work anything like this. 516 00:31:00,080 --> 00:31:04,500 There may be, you know, no idea of a stub section or, you know, 517 00:31:04,520 --> 00:31:06,000 just unpacking one section at a time. 518 00:31:06,000 --> 00:31:08,500 It might just unpack everything to the heap and run from there. 519 00:31:09,380 --> 00:31:12,660 Or it might unpack itself a little bit at a time. 520 00:31:12,780 --> 00:31:16,360 So this isn't really going to help you a lot with packers like that. 521 00:31:16,720 --> 00:31:19,100 Unfortunately, that kind of thing is a little bit harder to write, 522 00:31:19,220 --> 00:31:22,180 so there's not a lot of packers that actually implement it that I've seen. 523 00:31:22,900 --> 00:31:26,300 Most of the packers that I encounter every day are like these ones in the 524 00:31:26,300 --> 00:31:28,820 demo there and are pretty easy. 525 00:31:31,760 --> 00:31:35,760 So in terms of evasion now, you know, once, you know, 526 00:31:35,760 --> 00:31:39,880 all the packer authors out there, you know, find out that we're doing this, 527 00:31:40,260 --> 00:31:43,260 well, all they have to do is make their code work slightly differently, 528 00:31:43,420 --> 00:31:45,060 and, you know, now we've got a problem. 529 00:31:45,200 --> 00:31:49,140 So, for instance, if they're not running from the stub code, 530 00:31:49,260 --> 00:31:51,800 maybe they could run as part of the code section itself, 531 00:31:51,940 --> 00:31:55,340 so they're always in the code section with us, making it, you know, 532 00:31:55,380 --> 00:31:59,140 impossible for us to set that type of break on the section. 533 00:31:59,320 --> 00:32:04,180 But since we're setting our protection per page, 534 00:32:04,640 --> 00:32:08,060 we can break it down by 4K sections. 535 00:32:08,260 --> 00:32:11,640 So, you know, if it came to that, we might be able to work it so that, 536 00:32:11,720 --> 00:32:15,820 you know, only, you know, the first part of the code section was actually 537 00:32:15,820 --> 00:32:18,180 protected and the last couple of pages where, you know, 538 00:32:18,200 --> 00:32:21,060 they inserted their unpacking code might not be. 539 00:32:21,540 --> 00:32:24,220 So it's one of those things where you could just kind of have to adapt 540 00:32:24,220 --> 00:32:27,380 as they adapt and it just becomes kind of a cat and mouse game. 541 00:32:30,100 --> 00:32:33,860 There's other things, kind of bad stuff, I guess, that could happen. 542 00:32:36,400 --> 00:32:41,760 We're letting this unpacking code basically do whatever it wants to. 543 00:32:41,920 --> 00:32:44,100 You know, we don't really have any control of it while it's running. 544 00:32:44,280 --> 00:32:46,040 So it could do sneaky things. 545 00:32:46,560 --> 00:32:49,880 It could send its own I.O. controls to Ollibone, perhaps, 546 00:32:50,080 --> 00:32:53,100 and make it unprotect the code section. 547 00:32:54,020 --> 00:32:57,860 It could do other, you know, nasty things like protect, you know, 548 00:32:57,860 --> 00:33:02,160 parts of our kernel, which we might not want protected, cause us to crash. 549 00:33:03,940 --> 00:33:07,580 So, you know, I don't recommend you use this on production systems. 550 00:33:07,880 --> 00:33:10,800 This is really just kind of a proof of concept at this point. 551 00:33:10,940 --> 00:33:15,300 There's very, very little error checking in terms of what the code is allowed to do. 552 00:33:16,500 --> 00:33:20,980 So if you feel like, you know, you want to add some error protection to it 553 00:33:20,980 --> 00:33:23,680 and maybe work on the code, I'd be happy to accept patches. 554 00:33:24,000 --> 00:33:25,580 Like I said, the source code is out there. 555 00:33:26,060 --> 00:33:29,360 You know, really don't have any excuse to complain if, you know, 556 00:33:29,360 --> 00:33:32,140 you don't like exactly, you know, how it's, you know, 557 00:33:32,160 --> 00:33:34,120 protected against these type of attacks. 558 00:33:34,320 --> 00:33:36,540 You should just patch it and send it. 559 00:33:39,560 --> 00:33:42,720 So there's other small tricks perhaps that could be used. 560 00:33:44,060 --> 00:33:48,540 Maybe the packed code is going to affect its own memory permissions 561 00:33:48,540 --> 00:33:50,440 using the virtual protect API. 562 00:33:51,320 --> 00:33:54,880 So we might have to do something like, you know, hook that API. 563 00:33:55,160 --> 00:33:59,360 And, you know, at some point it becomes kind of a mess of trying to outdo each other's tricks. 564 00:33:59,480 --> 00:34:02,140 So, you know, right now what's probably going to happen is, 565 00:34:02,160 --> 00:34:07,080 the more advanced packers are going to just make sure that it's really still hard to unpack them. 566 00:34:07,280 --> 00:34:11,160 And, you know, the packers that are out there and have been out there that everybody uses 567 00:34:11,160 --> 00:34:13,720 that nobody maintains anymore probably aren't going to change. 568 00:34:13,940 --> 00:34:17,180 And, you know, it'll probably be a while before, you know, 569 00:34:17,220 --> 00:34:21,260 you'd see it be pretty much impossible to use this trick. 570 00:34:23,940 --> 00:34:30,620 I do want to say that after I wrote this, you know, program and started talking about it, 571 00:34:30,620 --> 00:34:36,180 somebody actually took me aside and said, hey, by the way, you're not the first person to do this. 572 00:34:36,300 --> 00:34:40,820 I actually found out that there is a private unpacker out there that's, you know, 573 00:34:40,820 --> 00:34:45,300 not well known that has this feature and has had it for a while apparently. 574 00:34:45,540 --> 00:34:49,080 So I don't want to claim to be the first to do this or anything like that. 575 00:34:49,140 --> 00:34:53,640 It's just, you know, my little proof of concept that I've been using and I just wanted to share it. 576 00:34:54,800 --> 00:34:56,320 So you can download it. 577 00:34:56,500 --> 00:34:59,100 You know, everything that I've written is GPLed. 578 00:34:59,780 --> 00:35:00,340 You know. 579 00:35:00,620 --> 00:35:03,400 On my immediate to-do list, which I'm probably not going to get to myself, 580 00:35:03,680 --> 00:35:09,220 which, you know, could probably use help for, I would like to be able to set break on execute 581 00:35:09,220 --> 00:35:15,560 for more than just the sections that are assigned by the PE headers. 582 00:35:16,100 --> 00:35:20,120 For instance, if something's writing out to the heap and executing there, 583 00:35:20,640 --> 00:35:22,680 I'd like to be able to protect that. 584 00:35:23,260 --> 00:35:27,360 I would also like to be able to set break on execute on DLLs. 585 00:35:28,200 --> 00:35:34,780 The problem and the reason that I'm not doing this already is because some of these DLLs are in shared memory. 586 00:35:35,060 --> 00:35:41,760 So if you go and set a break on execute on a particular, you know, section in kernel32.dll, 587 00:35:41,960 --> 00:35:46,700 then, you know, every program on your system is going to hit a break point immediately 588 00:35:46,700 --> 00:35:49,020 and blue screen your system if you're lucky. 589 00:35:51,560 --> 00:35:55,660 So there's a feature here called copy on write that can come into play. 590 00:35:55,660 --> 00:36:01,480 You can actually force your own copy of each page of memory. 591 00:36:02,820 --> 00:36:09,300 So it might be possible to force a write of the same byte perhaps to a particular location that's already there, 592 00:36:09,480 --> 00:36:14,180 forcing it to copy itself over to non-shared memory and then set your break on execute. 593 00:36:14,480 --> 00:36:18,460 But like I said, past this initial proof of concept, I haven't done that. 594 00:36:18,600 --> 00:36:20,820 So if anybody wants to implement that, feel free. 595 00:36:22,220 --> 00:36:24,820 And that's all I've got for the presentation. 596 00:36:24,820 --> 00:36:29,240 If anybody wants to ask some questions, the microphone is right there. 597 00:36:29,340 --> 00:36:30,920 Just make sure you talk into the mic. 598 00:36:32,920 --> 00:36:33,500 Thank you. 599 00:36:43,320 --> 00:36:44,000 Hey, Joe. 600 00:36:44,140 --> 00:36:44,400 Hi. 601 00:36:45,280 --> 00:36:54,020 I assume that if you've got a packer that's mixing any debug checks for soft breaks as well as hardware checkpoints, 602 00:36:54,200 --> 00:36:54,800 you're going to be able to do that. 603 00:36:54,820 --> 00:36:59,040 Are you going to have to manually step through that first before you kick in the memory protection? 604 00:36:59,660 --> 00:37:02,720 For example, I've seen at least some packers that will make one of the kernel calls 605 00:37:02,720 --> 00:37:07,020 that will kill all of your hardware debug points inside the process you're debugging. 606 00:37:08,140 --> 00:37:08,620 Right. 607 00:37:09,320 --> 00:37:13,780 So we're not really concerned with hardware breakpoints here at all. 608 00:37:13,940 --> 00:37:18,160 I mean, this is really kind of a hack on top of the kernel. 609 00:37:18,300 --> 00:37:23,380 So if they were using hardware breakpoints, it wouldn't affect us or if they were trying to change them. 610 00:37:23,380 --> 00:37:26,500 The segment trap doesn't count as a hardware breakpoint? 611 00:37:27,440 --> 00:37:27,920 No. 612 00:37:28,480 --> 00:37:29,300 No, it doesn't. 613 00:37:29,300 --> 00:37:32,120 It doesn't take up one of those hardware breakpoint registers at all, no. 614 00:37:32,240 --> 00:37:38,620 It's all implemented by our own custom page fault handler, which the malware really doesn't have any way to know about 615 00:37:38,620 --> 00:37:42,720 unless it actually gets wise to this trick and tests for it later on. 616 00:37:43,060 --> 00:37:43,620 Thank you. 617 00:37:43,860 --> 00:37:44,140 Thanks. 618 00:37:46,640 --> 00:37:52,240 So the 64-bit Intel, 619 00:37:52,240 --> 00:37:55,820 where they now have a proper in the AMD where they have a proper NX bit, 620 00:37:56,420 --> 00:38:01,500 does that mean that there will be some inherent protection against packers? 621 00:38:02,200 --> 00:38:06,260 Or could they just turn off the use of that bit for their code segments or whatever? 622 00:38:06,460 --> 00:38:08,540 I haven't really played with the NX bit too much. 623 00:38:08,660 --> 00:38:11,640 I actually don't have a processor that has it on it. 624 00:38:11,780 --> 00:38:12,860 I haven't gotten around to it. 625 00:38:13,780 --> 00:38:20,480 I don't see any reason that you couldn't write the same protection using that bit instead of the whole page fault handler hack. 626 00:38:20,580 --> 00:38:22,220 It would probably be actually a much more complicated process. 627 00:38:22,240 --> 00:38:22,800 It would probably be a much more elegant solution. 628 00:38:22,920 --> 00:38:26,080 It just wouldn't be, you know, compatible with all processors. 629 00:38:27,720 --> 00:38:31,140 But yeah, that's basically the same idea, yeah. 630 00:38:36,980 --> 00:38:37,840 All right, thanks everybody.