1 00:00:05,220 --> 00:00:10,740 My name is Chris Eagle, thanks for coming, and clearly nothing better to do on a Sunday 2 00:00:10,740 --> 00:00:24,500 afternoon at DEF CON, but okay, I'm not good with microphones, okay, again I'm Chris Eagle, 3 00:00:24,500 --> 00:00:31,760 and this talk is titled Creating Genetic Mutations to Survive the Vulnerability Window, and there's 4 00:00:31,760 --> 00:00:39,540 zero scientific knowledge in here, better subtitle is Stupid Hex Editor Tricks, or Creating Nipples 5 00:00:39,540 --> 00:00:46,740 in the Gene Pool, I guess it was, or just Nipples in the Pool was suggested from the crowd, and you 6 00:00:46,740 --> 00:00:51,600 know, you might like some of that better than this talk, well, we'll see. So again, Stupid Hex Editor 7 00:00:51,600 --> 00:00:54,420 Tricks, this is not deep, deep stuff, all right? 8 00:00:56,820 --> 00:01:02,360 And anything I say has nothing to do with who I work for, or who I know, or no guilt by association 9 00:01:02,360 --> 00:01:10,080 stuff there. So the stuff I'm going to talk about is really born from ideas developed out of playing 10 00:01:10,080 --> 00:01:15,420 CTF for the past couple years, where your threat model is, you have an application that has 11 00:01:15,420 --> 00:01:21,700 vulnerabilities, and it needs to survive for three days, okay, with some pretty serious people 12 00:01:21,700 --> 00:01:22,880 looking for holes in this thing. 13 00:01:23,720 --> 00:01:24,160 But, 14 00:01:25,080 --> 00:01:33,760 perhaps, well, we'll get to the more general use case, maybe. The idea, again, no source code available, you have to run 15 00:01:33,760 --> 00:01:40,420 the service for whatever reason that might be, there are business cases like that, and in our case, the patch 16 00:01:40,420 --> 00:01:46,200 needs to survive less than three days, okay? In the real world, you may not know how long the vendor will take to 17 00:01:46,200 --> 00:01:53,820 patch the thing, so we'll just go from there, three days. I'll use x86 examples as I go through this stuff, I 18 00:01:53,820 --> 00:01:54,120 think 19 00:01:54,500 --> 00:02:02,680 it could be easily adapted to other platforms. It's pretty generic techniques. Again, there's nothing really deep going on 20 00:02:02,680 --> 00:02:14,200 here. So, a while back, Dan Gere talked about the problem with the software monoculture, in which a large number of 21 00:02:14,200 --> 00:02:21,860 platforms, all running essentially the same software, are, you know, provide for this, the capability of mass 22 00:02:21,860 --> 00:02:23,480 propagations of worms and such. 23 00:02:24,680 --> 00:02:36,660 And the idea is that in order to mitigate this sort of monoculture problem, you've got to address the similarity in all the software 24 00:02:36,660 --> 00:02:45,380 some way, you know, short of getting everybody to migrate to different operating systems and so on. This is, again, one, you know, 25 00:02:45,420 --> 00:02:53,720 novel approach, perhaps, or not, to go about that. So, we'll talk about a couple things. The idea, the notion of a vulnerability window, 26 00:02:53,720 --> 00:03:04,160 you can talk about it a lot of different ways, but basically spans the time frame from the discovery of a vulnerability, whether it's 27 00:03:04,160 --> 00:03:13,600 disclosed or not, up through the time at which a patch becomes available, and then you could extend that through the delay for lazy system 28 00:03:13,600 --> 00:03:22,700 administrators getting the patch applied, and that's sort of a different problem. Two components, so you can break the window into two, as I view it. 29 00:03:22,700 --> 00:03:34,060 You have discovery time to disclosure time, which, you know, if nothing ever gets disclosed, that could be forever. But, so, your vulnerability in that window 30 00:03:34,060 --> 00:03:41,140 is a lot more difficult to pin down. Obviously, if you can spot the fact that you're vulnerable, then the thing has become somewhat public. 31 00:03:43,340 --> 00:03:52,680 Requires cooperation of the discoverer in order to make this thing known. It's sometimes difficult to get, or it propagates wide enough that the 32 00:03:52,700 --> 00:04:02,440 eventually it leaks out to someone who is cooperative. And during this window, you have very little defensive capability. If you're running a vulnerable 33 00:04:02,440 --> 00:04:12,140 application and, you know, it's accessible remotely or by whatever means, then you are probably vulnerable. Obviously, the vulnerable application 34 00:04:12,140 --> 00:04:22,540 in its default configurations. Then you have the time window from disclosure, okay, to the availability of a patch. 35 00:04:22,700 --> 00:04:34,300 And that, that can last differing amounts of time, depending on the disclosure model used by the discoverer. And during this window, you might have 36 00:04:34,300 --> 00:04:41,860 defense by mitigation. If you know how to mitigate the attacks, you know, the severest, the most severe would be secure in the service. 37 00:04:43,220 --> 00:04:52,680 But there may be other techniques and that's sort of what I'm going to talk about. And the idea here is that we want this window to be able to 38 00:04:52,700 --> 00:04:56,600 to be shorter than the time 39 00:04:56,600 --> 00:05:00,460 it takes to automate that vulnerability and turn it into, say, a worm. 40 00:05:01,460 --> 00:05:04,280 Because, of course, then all hell breaks loose 41 00:05:04,280 --> 00:05:08,680 and that's where the monoculture comes into play. But if we can keep that 42 00:05:08,680 --> 00:05:11,760 short, then maybe we can survive through there. 43 00:05:13,800 --> 00:05:15,660 So, in that window, what do we do? 44 00:05:16,220 --> 00:05:20,580 If we're waiting for the vendor-supplied patch, you don't have a whole lot of options. There might be 45 00:05:20,580 --> 00:05:24,380 a patch offered up by the discoverer. We don't see this too often. 46 00:05:24,680 --> 00:05:28,440 Often, in cooperative disclosure, the discoverer 47 00:05:28,440 --> 00:05:31,880 will coordinate with the vendor and details will be released 48 00:05:31,880 --> 00:05:36,020 when the patch becomes available. Alternatively, 49 00:05:36,260 --> 00:05:39,980 the discoverer will just throw this problem out there and offer you no real means 50 00:05:39,980 --> 00:05:44,140 to fix it. And you're left to your own devices to understand the nature of the problem 51 00:05:44,140 --> 00:05:47,820 and secure the whole. Alternatively, 52 00:05:48,140 --> 00:05:50,300 and we don't see this too often, but 53 00:05:50,300 --> 00:05:54,200 we have seen a couple of interesting cases in the past year or so where an independent researcher 54 00:05:54,200 --> 00:05:58,360 provides a patch, not the person necessarily who discovered 55 00:05:58,360 --> 00:06:02,240 the hole, and they make this thing available. You have 56 00:06:02,240 --> 00:06:06,300 Ilfak Gilfanov's WMF patch that came out last year, 57 00:06:06,340 --> 00:06:09,700 widely touted by SANS as the way to fix that hole. 58 00:06:10,000 --> 00:06:13,280 And I think EI came out recently with an Internet Explorer patch 59 00:06:13,960 --> 00:06:17,880 for some client-side vulnerabilities there. They can be controversial because 60 00:06:17,880 --> 00:06:18,960 who do you trust? 61 00:06:20,300 --> 00:06:22,640 What's the software QA process that goes on these things? 62 00:06:24,540 --> 00:06:29,000 And what are all the interactions that they may cause with the application 63 00:06:29,000 --> 00:06:31,980 that they claim to patch? 64 00:06:32,200 --> 00:06:35,760 So there's also some controversy that goes along with these things. 65 00:06:38,660 --> 00:06:40,780 So, if we take, you know, 66 00:06:40,780 --> 00:06:44,300 I don't want to turn anything into a disclosure debate, unlikely to happen here today. 67 00:06:44,660 --> 00:06:48,860 But personally, I don't care whether you disclose or not. That's up to you. 68 00:06:48,960 --> 00:06:50,280 I don't care if you coordinate with a vendor. 69 00:06:50,300 --> 00:06:51,480 If you coordinate with a vendor or not, that's up to you. 70 00:06:52,600 --> 00:06:54,320 They're probably not paying for your time anyway. 71 00:06:55,460 --> 00:07:00,320 And I'm just kind of in the school that says if you do choose to disclose, 72 00:07:01,320 --> 00:07:03,500 it'd be nice if you told people how to protect themselves. 73 00:07:04,680 --> 00:07:08,260 Don't offer a patch necessarily, but telling them why, you know, 74 00:07:08,260 --> 00:07:12,480 not just, hey, here's my O-Day, but maybe here's what you can do to prevent it. 75 00:07:12,720 --> 00:07:15,080 And maybe that includes shutting down the service. 76 00:07:15,260 --> 00:07:17,160 But maybe there are some other things that you can do. 77 00:07:18,300 --> 00:07:20,140 And that's kind of where I fall on all that. 78 00:07:20,140 --> 00:07:22,300 And I don't think it's worth getting into. 79 00:07:22,400 --> 00:07:23,580 There's enough of that going on. 80 00:07:24,980 --> 00:07:29,520 So, before I get any further, there have been a few patching talks that have gone on 81 00:07:29,520 --> 00:07:31,180 through Black Hat and DEF CON, I guess. 82 00:07:31,300 --> 00:07:32,500 And they've been kind of interesting. 83 00:07:32,740 --> 00:07:36,620 They've talked about the new Microsoft hot patching features 84 00:07:37,220 --> 00:07:41,620 where with certain compiler switches, you basically have built-in room 85 00:07:42,400 --> 00:07:46,160 at the beginning of every function to kind of hook in, divert the function call, 86 00:07:48,720 --> 00:07:50,020 and inject patches. 87 00:07:50,140 --> 00:07:54,100 And that's not what this is all about. 88 00:07:54,240 --> 00:07:57,200 This is what I'm talking about here is simple changes, 89 00:07:57,580 --> 00:08:01,900 relatively simple changes to a binary to alter its run-time characteristics, 90 00:08:02,300 --> 00:08:08,660 not necessarily its behavior, sufficiently enough to foil automated exploitation attempts. 91 00:08:10,800 --> 00:08:17,180 Sometimes doing this is a lot easier than fixing the problem behaviorally, 92 00:08:17,220 --> 00:08:19,140 which may require addition. 93 00:08:21,280 --> 00:08:25,720 Injection of additional code to perform any kind of boundary checking, error checking, 94 00:08:25,920 --> 00:08:31,200 whatever it might be that you would like to do to fix the problem. 95 00:08:31,440 --> 00:08:32,180 Uncheck string copy. 96 00:08:32,400 --> 00:08:36,700 We need to add some kind of check on the length of the buffer that's going to get copied and so on. 97 00:08:36,860 --> 00:08:38,280 And adding those checks takes space. 98 00:08:38,800 --> 00:08:41,380 Space is sometimes at a premium inside a binary. 99 00:08:42,260 --> 00:08:47,080 And creating that space, obviously without the source code to be able to recompile it, 100 00:08:48,480 --> 00:08:49,360 is probably... 101 00:08:50,140 --> 00:08:54,040 you know, a sophistication level above what I'm going to talk about here. 102 00:08:54,500 --> 00:08:58,660 And so finally, these things are basically security through obscurity. 103 00:08:59,020 --> 00:09:03,660 So again, I do not tout them as a way, as a long-term fix. 104 00:09:03,860 --> 00:09:07,600 Obviously you're interested in getting the problem patched at the behavioral level, 105 00:09:07,680 --> 00:09:10,460 not just at the sort of characteristics level. 106 00:09:13,380 --> 00:09:19,520 So with that, also they are not necessarily, in most cases, not unexploitable. 107 00:09:20,180 --> 00:09:26,200 They are just exploitable in a different way than the rest of the monocults are out there. 108 00:09:26,640 --> 00:09:32,640 So the point is, once a worm hits the streets, if they... 109 00:09:32,640 --> 00:09:37,480 unless they are very good and build the worm to sort of brute force their way through every machine they attack, 110 00:09:37,660 --> 00:09:39,160 or just those that they're unsuccessful against, 111 00:09:39,480 --> 00:09:44,080 then perhaps the worm will give you a try and just pass on by. 112 00:09:44,740 --> 00:09:47,460 So again, they remain not a long-term solution. 113 00:09:48,040 --> 00:09:49,700 Ultimately the patch is what we're after. 114 00:09:51,540 --> 00:09:59,300 So some assumptions, I guess, that automated exploits are generally built for specific target layouts. 115 00:09:59,780 --> 00:10:03,060 I mean, they may understand a few different target layouts. 116 00:10:03,220 --> 00:10:08,260 So we've got the sort of things that will hit Win2K, WinXP, various service packs, and so on. 117 00:10:08,500 --> 00:10:12,420 But they tend to look for specific features on each of those. 118 00:10:14,060 --> 00:10:18,660 And, or exploit common functionality across all of the platforms. 119 00:10:18,980 --> 00:10:20,120 So if you look back, 120 00:10:20,140 --> 00:10:24,860 if they're a little different than your stock Win2K service pack 4 box or whatever they're at these days, 121 00:10:25,020 --> 00:10:28,080 then maybe even these things will not hit you. 122 00:10:28,200 --> 00:10:29,040 And that's the goal. 123 00:10:30,620 --> 00:10:33,340 Most automated attacks aren't smart enough to evolve. 124 00:10:34,080 --> 00:10:36,860 These things are out there looking for the low-hanging fruit, 125 00:10:36,980 --> 00:10:41,280 scanning for a target list, and then iteratively hitting each target on the list. 126 00:10:41,440 --> 00:10:45,380 And when they fail to propagate to a target, they often just move on. 127 00:10:45,500 --> 00:10:49,020 Now your dedicated, serious attacker out there, 128 00:10:49,020 --> 00:10:51,560 who's after you individually, specifically, 129 00:10:52,560 --> 00:10:57,140 might realize that their stock attack is not working against you 130 00:10:57,140 --> 00:11:01,380 and may then take the time to try to brute force their way into your system. 131 00:11:01,780 --> 00:11:04,700 And ultimately, they may make your way through. 132 00:11:06,940 --> 00:11:13,600 And this is not probably the protection that is for you if that's your threat model. 133 00:11:14,280 --> 00:11:15,900 Again, patches are the best bet. 134 00:11:18,100 --> 00:11:18,620 Okay. 135 00:11:18,620 --> 00:11:22,160 So into binary patching, which is ultimately where we're going. 136 00:11:22,380 --> 00:11:24,600 Again, it's really stupid hex editor tricks. 137 00:11:25,440 --> 00:11:27,100 So it's not for everybody. 138 00:11:27,240 --> 00:11:28,760 It's something of a black art. 139 00:11:29,980 --> 00:11:34,280 You need to have some familiarity at least with the assembly language level, 140 00:11:34,400 --> 00:11:39,020 maybe some binary auditing, some executable file format layout issues, 141 00:11:39,240 --> 00:11:45,600 things like that to understand what your opportunities are with any given binary. 142 00:11:45,820 --> 00:11:47,340 Is there room to add code? 143 00:11:48,620 --> 00:11:49,120 How much room? 144 00:11:49,300 --> 00:11:50,240 Can I create room? 145 00:11:50,540 --> 00:11:55,480 Can I divert the flow of execution to some other location where there is some room? 146 00:11:55,700 --> 00:11:56,520 And so on. 147 00:11:56,600 --> 00:12:01,100 And then what code needs to be moved to accommodate any behavioral checks that you would like to add? 148 00:12:02,580 --> 00:12:07,760 And unfortunately, compilers are often concerned about the size issues. 149 00:12:08,880 --> 00:12:10,860 Not all the time, but often. 150 00:12:11,040 --> 00:12:13,000 And don't generally too much free space. 151 00:12:13,140 --> 00:12:13,800 You can find some. 152 00:12:15,080 --> 00:12:16,760 Sometimes you can create some 153 00:12:16,760 --> 00:12:18,600 if you're willing to dig into programming. 154 00:12:18,620 --> 00:12:20,600 You can create some program headers and sort of spread things out. 155 00:12:21,100 --> 00:12:24,480 But as soon as you start messing with the virtual address layout of the binary, 156 00:12:24,640 --> 00:12:28,740 then you're adding an additional level of complexity on top of all your changes. 157 00:12:30,520 --> 00:12:33,640 And ultimately, if we want to add some new behavior 158 00:12:33,640 --> 00:12:39,040 and we need to use additional functions that may not be included, 159 00:12:39,200 --> 00:12:42,880 like a safer version of string copy, which includes checks and so on, 160 00:12:43,020 --> 00:12:45,700 if that function was not imported by the original binary, 161 00:12:45,700 --> 00:12:52,520 then you have the additional task of adding the capability of the binary to locate that function and so on. 162 00:12:52,600 --> 00:12:54,140 So we have all these sort of cascading problems. 163 00:12:56,000 --> 00:13:00,320 So, some simple mutations that maybe we can deal with. 164 00:13:01,100 --> 00:13:04,100 The ones that are easiest involve stack mutations. 165 00:13:04,400 --> 00:13:09,860 And stack mutations simply alter the stack layout to something other than the stock stack layout. 166 00:13:10,080 --> 00:13:12,940 And I'll talk about what that is. 167 00:13:13,040 --> 00:13:14,260 And again, they're the simplest to perform. 168 00:13:14,260 --> 00:13:17,860 There are some simple heap mutations that you can do 169 00:13:17,860 --> 00:13:20,560 without having to add a whole lot of extra code or change too much. 170 00:13:20,860 --> 00:13:23,840 And the goal, again, is to just alter the heap layout 171 00:13:23,840 --> 00:13:26,640 so that it looks different than what the attacker expected, 172 00:13:26,800 --> 00:13:33,380 since some of these things require very specific knowledge of where overwrite code will land. 173 00:13:34,360 --> 00:13:38,960 Format string mutations, these are, again, harder and evolve more to changing the characteristics, 174 00:13:39,140 --> 00:13:40,440 so I don't spend a lot of time there. 175 00:13:41,300 --> 00:13:43,560 And then uninitialized stack variables. 176 00:13:43,560 --> 00:13:48,540 In which we hope to sort of alter the stack layout to move the uninitialized variable again. 177 00:13:48,720 --> 00:13:54,080 And that's kind of up with stack mutations in a different sort of vein. 178 00:13:59,260 --> 00:14:00,740 So I'll start with stack mutations. 179 00:14:01,740 --> 00:14:05,680 And the basic idea with stack mutations, a very simple idea, 180 00:14:05,900 --> 00:14:11,420 is to simply grab more stack space in the function prolog. 181 00:14:11,620 --> 00:14:13,540 A lot of functions start off with grabbing more stack space. 182 00:14:13,560 --> 00:14:14,480 Grab in some local variable space. 183 00:14:15,040 --> 00:14:17,280 And you can see the code that does it there. 184 00:14:17,460 --> 00:14:22,880 If you set up a stack frame and adjust for your local variable storage, 185 00:14:23,060 --> 00:14:30,600 in this case, the first prolog there grabs 34 hex bytes on the stack for some local variables. 186 00:14:31,320 --> 00:14:33,340 And that's often done as a one byte constant. 187 00:14:33,500 --> 00:14:34,960 So that has some implications later on. 188 00:14:35,080 --> 00:14:37,060 But if you want more space, grab more space. 189 00:14:38,740 --> 00:14:40,540 Subtract 80 hex bytes. 190 00:14:40,540 --> 00:14:45,800 And that builds you just, in this case, 50 hex bytes or 80 bytes of space 191 00:14:45,800 --> 00:14:48,400 to kind of rearrange the layout of the stack. 192 00:14:50,060 --> 00:14:54,320 In the second case, they're grabbing a much larger chunk of stack space. 193 00:14:54,600 --> 00:14:57,680 In that case, just over 1K worth of stack space. 194 00:14:58,120 --> 00:15:00,720 And that constant happens to be larger. 195 00:15:00,880 --> 00:15:05,880 And so you can make significantly larger adjustments in that case 196 00:15:05,880 --> 00:15:08,260 to grab a significantly larger amount of stack space 197 00:15:08,260 --> 00:15:14,100 and have larger variance in your rearrangement of things. 198 00:15:14,400 --> 00:15:19,360 In a sense, this is poor man's address space randomization. 199 00:15:19,940 --> 00:15:22,020 Okay, when you start doing these kinds of things. 200 00:15:22,100 --> 00:15:27,160 Just because, again, your stack layouts, offsets, return addresses 201 00:15:27,160 --> 00:15:31,540 end up getting shifted around so that the stock exploits don't work. 202 00:15:33,380 --> 00:15:37,560 Once you grab that extra stack space, you need to sift through the code 203 00:15:37,560 --> 00:15:39,900 and just adjust pointers into the stack frame, 204 00:15:40,560 --> 00:15:44,620 which tend to be relative offsets from a frame pointer or the stack pointer itself. 205 00:15:45,200 --> 00:15:48,340 So in stack pointer based frames there, ESP based frames, 206 00:15:49,600 --> 00:15:54,660 then in actuality, ESP based frames, there's no adjustment needed for local variables. 207 00:15:55,280 --> 00:15:59,620 Because everything just remains relative to ESP, it's just higher up in the stack space. 208 00:16:00,460 --> 00:16:04,460 And you've built a gap, a buffer of sorts at the bottom of the stack 209 00:16:04,460 --> 00:16:05,980 that just remains unused. 210 00:16:07,560 --> 00:16:10,400 Of course, we have to also assume that we don't care too much about memory consumption. 211 00:16:11,240 --> 00:16:16,380 Okay, or not start grabbing too much stack space just for the hell of it. 212 00:16:21,040 --> 00:16:25,020 The only thing that does need adjusting in the ESP based stack frames 213 00:16:25,020 --> 00:16:29,400 is the depth to the function arguments sitting under the return address, as mentioned there. 214 00:16:29,560 --> 00:16:33,100 So you don't have to adjust any references to local arguments 215 00:16:33,100 --> 00:16:36,560 because everything floats with the stack pointer, which is just higher up. 216 00:16:36,560 --> 00:16:41,100 And you have to dig deeper in that stack frame to get to your function arguments. 217 00:16:42,000 --> 00:16:45,640 When you have a frame pointer based stack frame, like an EBP based stack frame, 218 00:16:45,800 --> 00:16:47,840 then you need to adjust all your local variable arguments 219 00:16:47,840 --> 00:16:52,160 because they're higher up in the stack, so we have to subtract a greater offset off of EBP. 220 00:16:52,840 --> 00:16:57,020 But there is no adjustment required for your function arguments. 221 00:16:57,380 --> 00:17:01,800 The first case is usually easier because there's typically more local variables declared 222 00:17:01,800 --> 00:17:04,460 than there are arguments, so there's fewer changes, perhaps. 223 00:17:07,180 --> 00:17:10,420 So here would be a simple example where you see a stack layout 224 00:17:10,420 --> 00:17:14,860 where they basically grab about 1K of stack space up there, 225 00:17:14,980 --> 00:17:18,560 and you see some variables declared in the stack, including that of our 3F8, 226 00:17:19,180 --> 00:17:25,200 which is about a 1K stack-based buffer, which is perhaps susceptible to a buffer overflow. 227 00:17:26,020 --> 00:17:30,120 And you can see that the declaration or the way they access that 228 00:17:30,120 --> 00:17:36,520 is using a relative offset from EBP there in the last line of the text. 229 00:17:36,560 --> 00:17:41,700 Where that equates to EBP minus 3F8. 230 00:17:41,820 --> 00:17:43,720 You can see the offsets over in the left margin. 231 00:17:45,360 --> 00:17:46,840 So that might be the default layout. 232 00:17:47,000 --> 00:17:49,780 Now, if we can inject, say, 2K of code into that buffer, 233 00:17:49,920 --> 00:17:51,540 obviously we have a stack-based buffer overflow, 234 00:17:53,400 --> 00:17:56,520 corrupting EIP and allowing, perhaps, remote code execution. 235 00:17:58,380 --> 00:18:00,280 So we can make a quick adjustment here, 236 00:18:00,440 --> 00:18:05,860 and instead of subtracting 414 hex, we change one byte in the code. 237 00:18:05,960 --> 00:18:06,540 We are now sure. 238 00:18:06,560 --> 00:18:07,680 We are subtracting 814 hex. 239 00:18:08,040 --> 00:18:10,160 So now our buffer in the stack is about 2K. 240 00:18:12,900 --> 00:18:14,760 And everything just shifts up. 241 00:18:15,120 --> 00:18:18,400 It requires us, in this case, I shifted everything up in the stack, 242 00:18:18,540 --> 00:18:23,480 remain the relative relationships among all the stack variables. 243 00:18:24,320 --> 00:18:29,660 And for all intents and purposes, you can still treat the form of R3F8, 244 00:18:29,840 --> 00:18:32,740 which is now VAR7F8, as a 1K buffer. 245 00:18:33,300 --> 00:18:35,300 It's just a 1K buffer that has about 246 00:18:35,300 --> 00:18:37,960 a 1K padding area beneath it. 247 00:18:38,100 --> 00:18:41,500 So if somebody was overflowing with something slightly over 1K, 248 00:18:42,060 --> 00:18:42,940 they don't reach EIP. 249 00:18:44,700 --> 00:18:46,060 It's that simple. 250 00:18:46,360 --> 00:18:50,600 And we have to go in and change that subtraction operation 251 00:18:50,600 --> 00:18:52,860 where we allocate our locals. 252 00:18:53,120 --> 00:18:54,640 That's a one-byte flip right there. 253 00:18:55,080 --> 00:18:57,800 And we go in and change any references, 254 00:18:58,820 --> 00:19:03,300 EBP-based references to all of the local arguments 255 00:19:03,300 --> 00:19:04,460 that are sitting up there in the stack. 256 00:19:04,460 --> 00:19:07,840 So we end up with EBP-7F8, 257 00:19:07,960 --> 00:19:09,360 and that still reaches the start of the buffer. 258 00:19:10,420 --> 00:19:12,760 Again, not terribly sophisticated stuff. 259 00:19:12,940 --> 00:19:14,380 Does it remain vulnerable? Yes. 260 00:19:15,280 --> 00:19:18,820 Is the stack layout the same as the rest of the monoculture? No. 261 00:19:19,560 --> 00:19:23,380 So again, maybe you survive that vulnerability window 262 00:19:23,380 --> 00:19:24,720 just a little bit longer. 263 00:19:25,620 --> 00:19:28,900 An alternative would be to simply move the buffer entirely, 264 00:19:30,340 --> 00:19:34,220 where we don't move any of the other stack-based variables. 265 00:19:34,460 --> 00:19:38,380 And we again grab all the extra space 266 00:19:38,380 --> 00:19:40,760 and just change the offset to that single variable, 267 00:19:40,920 --> 00:19:41,940 the stack-based buffer, 268 00:19:42,520 --> 00:19:45,520 which moves to the top of the local variable space, 269 00:19:45,760 --> 00:19:47,940 still a 1K or a 1,016-byte buffer, 270 00:19:48,040 --> 00:19:49,500 if I did my math right, 271 00:19:50,920 --> 00:19:53,980 followed on the tail end by a bunch of local variables 272 00:19:54,820 --> 00:19:56,780 and another 1,000 bytes of padding. 273 00:19:57,340 --> 00:19:58,500 Same sort of effect. 274 00:20:01,260 --> 00:20:03,960 There's a side effect here that now, 275 00:20:03,960 --> 00:20:06,120 in order to get to EIP, 276 00:20:06,580 --> 00:20:09,320 the attacker also has to write through a lot of other local variables, 277 00:20:10,340 --> 00:20:13,160 some of which, hopefully none of which, 278 00:20:13,940 --> 00:20:15,220 it's poor man's canaries. 279 00:20:16,080 --> 00:20:18,100 If you corrupt any of these variables, 280 00:20:18,360 --> 00:20:22,760 the function may error out in a variety of different ways, 281 00:20:22,960 --> 00:20:24,240 hopefully cleanly. 282 00:20:25,120 --> 00:20:27,020 And if we error out cleanly, 283 00:20:27,080 --> 00:20:30,940 perhaps we'll avoid any activation of the stack-based buffer overflow. 284 00:20:31,580 --> 00:20:33,260 So this gets in, 285 00:20:33,960 --> 00:20:37,660 sort of a small demonstration of permutations of these variables. 286 00:20:37,940 --> 00:20:39,340 If you can rearrange your buffers, 287 00:20:39,920 --> 00:20:42,320 move file descriptors or something like that 288 00:20:42,320 --> 00:20:44,860 below an over-writable buffer, 289 00:20:45,040 --> 00:20:46,720 then you start to inject, 290 00:20:48,380 --> 00:20:51,100 get perhaps some error conditions that might crop up 291 00:20:51,100 --> 00:20:52,760 that prevents things from getting triggered as well. 292 00:20:57,600 --> 00:20:59,120 Variations on this theme are, 293 00:20:59,800 --> 00:21:02,700 again, sometimes you rely on 294 00:21:03,360 --> 00:21:04,300 offsets into the stack. 295 00:21:04,620 --> 00:21:06,380 Things are more sophisticated these days 296 00:21:06,380 --> 00:21:08,860 where we try to get addresses to things like a jump ESP, 297 00:21:09,500 --> 00:21:10,820 or jump to a known register, 298 00:21:11,100 --> 00:21:13,620 but when people do rely on known addresses in the stack, 299 00:21:14,220 --> 00:21:17,060 if we can change where things lie, 300 00:21:17,240 --> 00:21:18,800 where's your shellcode, for example? 301 00:21:18,960 --> 00:21:21,280 If you have to point at your shellcode for any reason, 302 00:21:21,380 --> 00:21:23,500 if we can just move the location of the shellcode, 303 00:21:24,340 --> 00:21:25,640 then you're not going to hit it. 304 00:21:26,100 --> 00:21:27,540 Even though you might trigger the overflow, 305 00:21:27,820 --> 00:21:28,880 you don't land in your shellcode. 306 00:21:29,180 --> 00:21:31,920 So simple ways to move the shellcode around, 307 00:21:31,920 --> 00:21:35,140 just involve grabbing more stack space, 308 00:21:35,660 --> 00:21:36,820 from main on up. 309 00:21:37,060 --> 00:21:39,060 If main grabs more stack space than it needs, 310 00:21:39,120 --> 00:21:41,100 regardless of whether it has any stack-based buffers, 311 00:21:41,420 --> 00:21:43,900 then all the stack addresses for every local variable 312 00:21:43,900 --> 00:21:47,120 allocated by functions called from main change. 313 00:21:48,480 --> 00:21:53,220 So it's, again, poor man's stack randomization, 314 00:21:53,340 --> 00:21:55,020 I suppose is the best way to put it. 315 00:21:56,680 --> 00:21:58,060 I went backwards, I guess. 316 00:21:59,340 --> 00:22:01,780 Reordering local variables, I mentioned briefly that one. 317 00:22:01,920 --> 00:22:04,020 Just place additional locals between buffers 318 00:22:04,020 --> 00:22:06,520 and save return address, and so on. 319 00:22:08,040 --> 00:22:10,700 Heat mutations, there's less we can do here 320 00:22:10,700 --> 00:22:13,060 without getting into the behavioral modification of the program. 321 00:22:13,720 --> 00:22:18,860 The easy change is that when a heat overflow occurs 322 00:22:18,860 --> 00:22:21,300 in fixed-size buffers, 323 00:22:21,300 --> 00:22:24,520 so what you're looking for here is when the object 324 00:22:24,520 --> 00:22:26,020 that it's getting overflowed out of 325 00:22:26,020 --> 00:22:27,540 is a fixed-size object in memory, 326 00:22:28,440 --> 00:22:30,140 malloc with a fixed-size constant, 327 00:22:30,140 --> 00:22:33,200 change the constant, grab more space. 328 00:22:34,300 --> 00:22:40,060 These are more particular to the layout of the memory, 329 00:22:41,860 --> 00:22:43,920 because you have to have the right alignment 330 00:22:43,920 --> 00:22:45,340 for all your pointers to line up 331 00:22:45,340 --> 00:22:47,060 and the unlink operation and all that. 332 00:22:48,480 --> 00:22:52,840 So if your object is 40 bytes it gets overflowed out of, 333 00:22:53,000 --> 00:22:55,120 ask for 90, ask for 100. 334 00:22:55,740 --> 00:22:57,160 Get a larger-sized chunk. 335 00:22:57,360 --> 00:22:59,260 Then you build a little bit of padding on the backside, 336 00:22:59,260 --> 00:23:03,100 and the attacker has a harder time finding that next frame 337 00:23:03,100 --> 00:23:04,500 or that next chunk in memory 338 00:23:04,500 --> 00:23:06,080 and lining up the pointers correctly. 339 00:23:07,780 --> 00:23:09,900 If the size of the chunk is computed 340 00:23:09,900 --> 00:23:11,320 by some mathematical operation, 341 00:23:11,680 --> 00:23:14,700 then you have a little more difficulty, 342 00:23:15,280 --> 00:23:17,700 because you're going to have to influence the math there 343 00:23:17,700 --> 00:23:19,180 to try to grow that size out 344 00:23:19,180 --> 00:23:20,980 to grab more space than you need. 345 00:23:21,080 --> 00:23:22,900 Again, we want to do these things 346 00:23:22,900 --> 00:23:24,540 where memory is not necessarily a premium. 347 00:23:25,520 --> 00:23:27,900 And grabbing more space can be done 348 00:23:27,900 --> 00:23:29,820 without bringing the system to its knees. 349 00:23:30,900 --> 00:23:33,280 So the idea here is we mutate 350 00:23:33,280 --> 00:23:35,400 to increase the amount of space requested 351 00:23:35,400 --> 00:23:38,200 so that the overflow out of the heat buffer 352 00:23:38,980 --> 00:23:42,220 doesn't quite behave the way the attacker might want it to. 353 00:23:43,500 --> 00:23:45,360 Again, we're still not unexploitable, 354 00:23:45,500 --> 00:23:47,440 so this is just silly. 355 00:23:48,580 --> 00:23:50,820 Hex edit, if you have a 16-byte object 356 00:23:50,820 --> 00:23:52,380 that's just a data buffer or something 357 00:23:52,380 --> 00:23:54,260 and you're copying to that, grab 64 bytes. 358 00:23:55,080 --> 00:23:56,620 The attacker doesn't know you did it 359 00:23:56,620 --> 00:23:58,560 unless they start brute forcing things 360 00:23:58,560 --> 00:24:01,040 and they get their pointers to line up beyond there, 361 00:24:01,200 --> 00:24:04,840 then hopefully they'll move on to the next target. 362 00:24:07,120 --> 00:24:08,460 Again, computed size mutations, 363 00:24:08,820 --> 00:24:10,400 if they're computed in a size a little more difficult, 364 00:24:10,520 --> 00:24:12,180 because you have to influence 365 00:24:12,180 --> 00:24:14,380 the size of the argument to malloc, 366 00:24:15,640 --> 00:24:17,500 which even if you just want to add 367 00:24:17,500 --> 00:24:18,660 an arithmetic operation, 368 00:24:18,900 --> 00:24:20,380 you may need as many as five bytes 369 00:24:20,380 --> 00:24:21,740 in there to do that. 370 00:24:23,080 --> 00:24:25,060 Format string mutations, harder to do. 371 00:24:25,060 --> 00:24:27,340 If it's a format string vulnerability, 372 00:24:27,980 --> 00:24:29,320 what you want to do is substitute 373 00:24:29,320 --> 00:24:30,680 your own format string in there, 374 00:24:31,200 --> 00:24:33,480 which often involves pushing an extra argument 375 00:24:33,960 --> 00:24:36,040 to a format string that you can specify, 376 00:24:36,200 --> 00:24:38,060 and you can build format strings almost anywhere you want. 377 00:24:38,160 --> 00:24:39,820 Usually you can find space to do that. 378 00:24:40,580 --> 00:24:42,760 Use any old string that you hardly ever use, 379 00:24:43,300 --> 00:24:44,880 terminate things like copyright notices 380 00:24:44,880 --> 00:24:46,540 and just overwrite it with a format string, 381 00:24:46,760 --> 00:24:49,520 and then get your pointer to the format string 382 00:24:49,520 --> 00:24:50,320 pushed on the stack. 383 00:24:50,420 --> 00:24:51,880 Just point at that string that you edit 384 00:24:51,880 --> 00:24:54,060 in the binary virtually anywhere. 385 00:24:56,140 --> 00:24:58,060 And again, as long as you can create the space 386 00:24:58,060 --> 00:25:00,040 to do that, you can craft format strings 387 00:25:00,040 --> 00:25:01,160 just anywhere in memory. 388 00:25:05,640 --> 00:25:07,200 Modified post-return stack adjustment. 389 00:25:07,820 --> 00:25:10,700 Oh yeah, that's simple binary patching stuff. 390 00:25:10,820 --> 00:25:12,240 Anytime you try to push extra arguments, 391 00:25:12,760 --> 00:25:14,680 when you have C standard calling conventions, 392 00:25:14,860 --> 00:25:16,020 you're going to have to clean up after yourself. 393 00:25:16,280 --> 00:25:17,640 So don't ever forget to do that. 394 00:25:21,400 --> 00:25:22,360 Uninitialized stack variables. 395 00:25:22,920 --> 00:25:24,540 Halvar spent some time talking about this 396 00:25:24,540 --> 00:25:25,840 over the past year or so. 397 00:25:27,520 --> 00:25:29,340 You have this variable in the stack 398 00:25:29,340 --> 00:25:32,000 that doesn't get initialized by the function, 399 00:25:32,200 --> 00:25:37,260 and the exploitable condition is to make some function calls 400 00:25:37,260 --> 00:25:39,860 that grow the stack space through that variable, 401 00:25:40,060 --> 00:25:42,500 leaving a value that you can predict 402 00:25:42,500 --> 00:25:44,140 in the uninitialized variable 403 00:25:44,140 --> 00:25:47,380 and hope that it gets used in a manner that's exploitable. 404 00:25:47,840 --> 00:25:49,900 So the idea here is move your variable. 405 00:25:50,380 --> 00:25:51,260 Permute the stack. 406 00:25:52,160 --> 00:25:52,680 Okay? 407 00:25:52,680 --> 00:25:55,360 That way the attacker, 408 00:25:55,540 --> 00:25:57,140 when they're trying to lay that one value 409 00:25:57,140 --> 00:25:58,720 into that memory location 410 00:25:58,720 --> 00:26:00,740 through some sequence of calls 411 00:26:00,740 --> 00:26:02,520 that grows and shrinks the stack, 412 00:26:03,320 --> 00:26:07,560 then their picture of your uninitialized variable, 413 00:26:07,880 --> 00:26:08,840 where they think it is, 414 00:26:08,840 --> 00:26:10,780 is different than where it actually ends up. 415 00:26:11,100 --> 00:26:12,460 So even though you still don't necessarily 416 00:26:12,460 --> 00:26:13,700 get to initialize the thing, 417 00:26:13,880 --> 00:26:16,460 it's not where the attacker thought it might be, 418 00:26:16,660 --> 00:26:18,640 so he doesn't get to initialize it 419 00:26:18,640 --> 00:26:20,080 with what he had hoped either. 420 00:26:20,460 --> 00:26:22,520 He's got to spend some time, perhaps brute force, 421 00:26:22,680 --> 00:26:23,280 processing through memory 422 00:26:23,280 --> 00:26:24,820 to try to figure out that you've done this, 423 00:26:24,940 --> 00:26:26,920 or again, hopefully, move on. 424 00:26:27,540 --> 00:26:27,620 Okay? 425 00:26:27,740 --> 00:26:29,540 The idea here, again, 426 00:26:29,580 --> 00:26:31,380 is to hopefully not be the low-hanging fruit. 427 00:26:32,340 --> 00:26:32,780 Okay? 428 00:26:32,940 --> 00:26:33,760 You're still fruit. 429 00:26:33,980 --> 00:26:35,940 You're just not hanging so low. 430 00:26:38,320 --> 00:26:38,760 Okay. 431 00:26:39,120 --> 00:26:39,800 Going quick. 432 00:26:40,660 --> 00:26:41,100 Okay? 433 00:26:41,120 --> 00:26:43,660 So a couple simple demonstrations. 434 00:26:43,880 --> 00:26:45,180 I tell you, this is not a real technical talk. 435 00:26:45,300 --> 00:26:48,780 It's just some ideas thrown out there from CTF world. 436 00:26:50,080 --> 00:26:51,320 That's really light text. 437 00:26:51,680 --> 00:26:52,120 Okay? 438 00:26:52,680 --> 00:26:53,200 People read that, 439 00:26:53,240 --> 00:26:54,220 or is that too light in the back? 440 00:26:55,540 --> 00:26:56,020 Too light? 441 00:26:56,280 --> 00:26:57,100 Let me see. 442 00:26:57,740 --> 00:26:58,500 If you don't mind, 443 00:26:58,560 --> 00:26:59,320 if I edit my slides, 444 00:26:59,320 --> 00:26:59,800 you're on the fly. 445 00:27:03,800 --> 00:27:04,300 What was that? 446 00:27:05,940 --> 00:27:06,500 Kill the lights? 447 00:27:13,680 --> 00:27:14,320 Oh, man. 448 00:27:14,580 --> 00:27:15,160 Don't do that. 449 00:27:21,340 --> 00:27:22,300 Can't see my keyboard. 450 00:27:30,820 --> 00:27:31,980 No, that didn't work. 451 00:27:35,160 --> 00:27:36,200 Got, like, bigger, too. 452 00:27:36,280 --> 00:27:36,500 Sorry. 453 00:27:49,180 --> 00:27:49,620 Okay. 454 00:27:49,720 --> 00:27:50,240 Is that better? 455 00:27:50,700 --> 00:27:51,960 Of course, it's darker, too. 456 00:27:52,120 --> 00:27:52,260 So. 457 00:27:53,660 --> 00:27:54,100 Okay. 458 00:27:54,780 --> 00:27:57,760 So, you know, here's a couple simple examples. 459 00:27:57,940 --> 00:28:01,420 Simple examples that if the demonstrations go right, 460 00:28:01,500 --> 00:28:01,960 they'll work. 461 00:28:02,020 --> 00:28:04,480 And if not, then sorry I wasted an hour. 462 00:28:07,040 --> 00:28:13,440 So this is basically the area out of SQL Server 463 00:28:13,440 --> 00:28:17,720 that was responsible for the SQL slammer worm. 464 00:28:18,080 --> 00:28:23,600 And you can see that Microsoft is using the Sprintf function call here, 465 00:28:23,740 --> 00:28:27,800 always, you know, known to be a poor choice. 466 00:28:28,660 --> 00:28:32,880 And the idea is that if you can see the snippets of strings over there 467 00:28:32,880 --> 00:28:38,560 in the right margin, they are sandwiching some static text around, 468 00:28:40,600 --> 00:28:41,880 let's see, which is it? 469 00:28:41,880 --> 00:28:46,300 This var, which one? 470 00:28:48,340 --> 00:28:48,860 Okay. 471 00:28:48,940 --> 00:28:50,060 I don't have a pointer, unfortunately. 472 00:28:51,000 --> 00:28:54,940 The right above the Sprintf call, there's a push EAX. 473 00:28:55,200 --> 00:29:01,480 That is your buffer that's going to be the destination for all of this stuff. 474 00:29:02,180 --> 00:29:02,700 Okay. 475 00:29:02,740 --> 00:29:06,660 So if we can get a large value into that buffer represented by var24c8 476 00:29:06,660 --> 00:29:11,700 at the top, all that text gets squeezed in between the static 477 00:29:11,880 --> 00:29:15,180 text snippets, one being your format string and the other one 478 00:29:15,180 --> 00:29:16,560 that ends up on the tail. 479 00:29:18,560 --> 00:29:22,020 So that's a problem because if you can see the math there, 480 00:29:22,160 --> 00:29:29,380 that destination buffer is what, 84 hex bytes above EBP and so on, 481 00:29:29,500 --> 00:29:31,700 which was, what's that, 120, 132 bytes? 482 00:29:31,860 --> 00:29:32,880 Not a big buffer. 483 00:29:33,700 --> 00:29:34,140 Okay. 484 00:29:34,160 --> 00:29:36,280 So if we can fill up that top buffer with enough stuff, 485 00:29:36,500 --> 00:29:38,200 obviously we have a buffer overflow. 486 00:29:38,760 --> 00:29:39,200 Okay. 487 00:29:39,200 --> 00:29:43,740 And so the idea is on the left you have the original stack layout 488 00:29:43,740 --> 00:29:48,740 with that poor little buffer ponme right there down at the bottom. 489 00:29:49,280 --> 00:29:52,880 And with some stack pointer manipulation, 490 00:29:53,140 --> 00:29:56,200 when we allocate our local variables, 491 00:29:56,400 --> 00:30:02,220 we can reorient or relocate ponme up to the top of the stack 492 00:30:02,220 --> 00:30:04,300 and allocate it even some extra space. 493 00:30:04,480 --> 00:30:08,440 And now it's not where the attacker expects it to be. 494 00:30:08,440 --> 00:30:12,380 So here is the mutated code. 495 00:30:12,600 --> 00:30:13,180 Okay. 496 00:30:13,200 --> 00:30:14,820 I don't show you the stack adjustment up front, 497 00:30:14,920 --> 00:30:18,680 but the mutated code basically places ponme, 498 00:30:18,760 --> 00:30:20,720 the buffer, all the way up at the top of the stack. 499 00:30:21,400 --> 00:30:21,980 Okay. 500 00:30:22,160 --> 00:30:24,140 And that's the basic idea. 501 00:30:24,360 --> 00:30:29,260 Now, not wanting to unleash a slammer on myself, 502 00:30:29,860 --> 00:30:37,460 Metasploit has an exploit for this vulnerability. 503 00:30:37,840 --> 00:30:38,420 Okay. 504 00:30:38,440 --> 00:30:40,980 And here's a VMware image. 505 00:30:41,300 --> 00:30:43,220 We'll see if this will all work out for me. 506 00:30:43,440 --> 00:30:46,580 You can see I've got SQL Server running on here. 507 00:30:47,360 --> 00:30:51,720 And we'll come out to the Metasploit console where I have 508 00:30:52,620 --> 00:30:56,420 and the box that we're looking at, as you can see, is unpatched. 509 00:30:56,860 --> 00:30:57,300 Okay. 510 00:30:57,300 --> 00:30:57,860 Trust me. 511 00:30:58,820 --> 00:30:59,420 All right. 512 00:31:00,720 --> 00:31:03,380 And I've got the Metasploit exploit all set up to go, 513 00:31:03,600 --> 00:31:06,600 which only takes seeing the keys to type. 514 00:31:10,260 --> 00:31:10,960 Thank you. 515 00:31:11,580 --> 00:31:12,060 Okay. 516 00:31:12,180 --> 00:31:15,680 So it's all set up to go against that particular target, I hope. 517 00:31:15,820 --> 00:31:16,760 And so we run it. 518 00:31:16,800 --> 00:31:21,640 And Metasploit is always exciting when it doesn't work. 519 00:31:27,020 --> 00:31:30,820 Run two Windows VMs on a doggie Dell laptop. 520 00:31:31,240 --> 00:31:32,080 Please work. 521 00:31:37,100 --> 00:31:37,580 Okay. 522 00:31:38,120 --> 00:31:44,300 There's the command prompt on the unpatched system. 523 00:31:44,580 --> 00:31:45,420 Thrilling, right? 524 00:31:45,480 --> 00:31:46,580 We can all do this in our sleep. 525 00:31:48,340 --> 00:31:53,540 So here's our other system, okay, which is running, 526 00:31:53,660 --> 00:31:56,340 which is host name patched. 527 00:31:56,900 --> 00:31:59,320 And it is also running SQL Server. 528 00:31:59,780 --> 00:32:02,540 And I'm going to go over here and grab my canned text. 529 00:32:07,360 --> 00:32:08,800 I don't know if that will be useful. 530 00:32:10,840 --> 00:32:11,240 Okay. 531 00:32:14,680 --> 00:32:17,900 For that host, because I don't like typing, 532 00:32:18,040 --> 00:32:22,160 and I can barely run Metasploit from the console. 533 00:32:22,300 --> 00:32:29,140 So we get out of this guy and paste in some text to, as you can see, 534 00:32:30,200 --> 00:32:31,720 change the host name over here. 535 00:32:32,540 --> 00:32:35,120 And unfortunately, we don't end up with a command prompt. 536 00:32:36,560 --> 00:32:37,040 Okay. 537 00:32:37,780 --> 00:32:40,240 And you have to trust me, that was patched. 538 00:32:40,920 --> 00:32:41,400 Okay. 539 00:32:41,440 --> 00:32:42,320 And we go back. 540 00:32:46,420 --> 00:32:49,600 And SQL Server even managed to survive the attack. 541 00:32:50,840 --> 00:32:51,320 Okay. 542 00:32:51,360 --> 00:32:57,180 That patch takes all of... 543 00:32:58,300 --> 00:32:59,360 I don't have it. 544 00:33:00,280 --> 00:33:00,840 Do I? 545 00:33:00,840 --> 00:33:01,800 Oh, it's four bytes. 546 00:33:02,360 --> 00:33:03,340 As I mentioned here. 547 00:33:04,240 --> 00:33:04,640 Okay. 548 00:33:04,720 --> 00:33:05,640 It's a four byte patch. 549 00:33:05,740 --> 00:33:06,680 Doesn't change any behavior. 550 00:33:07,240 --> 00:33:09,640 All it does is modify the stack layout, 551 00:33:09,840 --> 00:33:11,620 and now you look different than everybody else. 552 00:33:12,140 --> 00:33:15,400 And sometimes that's all we're trying to hope for. 553 00:33:15,860 --> 00:33:16,300 Okay. 554 00:33:16,340 --> 00:33:19,340 If you insist on running those vulnerable applications before the patches are out, 555 00:33:19,480 --> 00:33:23,320 then maybe this can help. 556 00:33:23,380 --> 00:33:24,740 Again, it's not unexploitable. 557 00:33:25,460 --> 00:33:25,900 Okay. 558 00:33:25,940 --> 00:33:28,740 But it introduces a lot more complexity to the attacker. 559 00:33:30,440 --> 00:33:33,320 Because the layout of the stack is significantly different, 560 00:33:33,420 --> 00:33:37,560 and to get to the return address down here, 561 00:33:38,460 --> 00:33:42,400 they still have to go through a substantial number of other local arguments. 562 00:33:42,720 --> 00:33:42,880 Okay. 563 00:33:42,940 --> 00:33:45,660 8K worth of stuff to blow through in this case, 564 00:33:45,700 --> 00:33:48,380 because we've moved that buffer very far away from the stack, 565 00:33:48,540 --> 00:33:50,280 and there's a whole lot of other local variables in there. 566 00:33:50,420 --> 00:33:55,020 So the complexity to the attacker is increased significantly. 567 00:33:57,800 --> 00:33:58,240 Okay. 568 00:33:59,580 --> 00:34:00,060 Next. 569 00:34:00,620 --> 00:34:05,720 So that's an idea where a simple change might help you survive against a worm propagation. 570 00:34:06,020 --> 00:34:07,140 Again, sophisticated attacker. 571 00:34:07,420 --> 00:34:10,820 He has a challenge of not knowing what your binary looks like anymore, 572 00:34:11,000 --> 00:34:15,440 because he can't do a stock SQL server install and grab the DLLs 573 00:34:15,440 --> 00:34:16,960 and understand the memory layout and so on. 574 00:34:17,120 --> 00:34:19,320 So at least he has that added complexity. 575 00:34:19,540 --> 00:34:21,360 It doesn't mean they can't brute force you remotely, 576 00:34:21,600 --> 00:34:23,020 but that gets to be a noisy operation. 577 00:34:23,660 --> 00:34:25,560 And hopefully you'll spot that one coming. 578 00:34:26,280 --> 00:34:26,760 Okay. 579 00:34:26,760 --> 00:34:30,620 The other one I'm going to try to do here is RPC Decon. 580 00:34:30,880 --> 00:34:32,120 A little bit more recent, 581 00:34:32,240 --> 00:34:35,780 a little bit less opportunity to modify things. 582 00:34:36,320 --> 00:34:38,540 You see on the left, there's a, 583 00:34:39,620 --> 00:34:42,100 I don't know how Microsoft writes their code sometimes, 584 00:34:42,340 --> 00:34:43,900 but they have a string buffer, 585 00:34:44,100 --> 00:34:45,900 a very small string buffer allocated. 586 00:34:46,160 --> 00:34:49,720 It's only like 32 bytes allocated on the stack, 587 00:34:49,900 --> 00:34:53,080 and that array receives the machine name, 588 00:34:53,140 --> 00:34:56,100 the host name for the host running the service, 589 00:34:56,100 --> 00:34:57,380 or the host lookup. 590 00:34:57,540 --> 00:34:58,460 And ultimately, 591 00:34:58,900 --> 00:35:00,520 if you go through the rest of the function, 592 00:35:00,640 --> 00:35:01,700 which I don't identify here, 593 00:35:01,800 --> 00:35:03,240 but obviously this stuff is public, 594 00:35:05,800 --> 00:35:07,860 that host name getting copied into that buffer 595 00:35:07,860 --> 00:35:09,920 results in the buffer overflow 596 00:35:11,300 --> 00:35:12,920 that yields the exploitable condition. 597 00:35:13,380 --> 00:35:14,340 Now in this case, 598 00:35:14,420 --> 00:35:16,560 we don't have quite as much ability to change. 599 00:35:16,920 --> 00:35:18,800 They are only using a small amount of stack space. 600 00:35:19,020 --> 00:35:20,800 This is a one byte subtraction. 601 00:35:20,960 --> 00:35:21,920 You have to be careful. 602 00:35:22,040 --> 00:35:22,680 You say, well, you know, 603 00:35:22,720 --> 00:35:24,820 so I can subtract all the way up to FF. 604 00:35:26,100 --> 00:35:27,280 Okay, there's a problem with that 605 00:35:27,280 --> 00:35:29,340 when you deal with the offsets, 606 00:35:29,360 --> 00:35:32,540 because the offsets generated from EBP 607 00:35:32,540 --> 00:35:34,060 will also be one byte offsets, 608 00:35:34,080 --> 00:35:35,340 and they're treated as signed integers. 609 00:35:36,160 --> 00:35:39,600 And when you start subtracting EBP minus FF, 610 00:35:39,680 --> 00:35:40,720 minus and minus, 611 00:35:40,820 --> 00:35:41,800 you're actually moving forward, 612 00:35:41,880 --> 00:35:43,480 and so you have these signedness issues. 613 00:35:43,720 --> 00:35:45,260 So restricted to about the, 614 00:35:45,260 --> 00:35:49,100 the best we can do is to change this up to about a 7C, 615 00:35:49,180 --> 00:35:51,700 I think is where we start to reach the limits 616 00:35:51,700 --> 00:35:54,620 of EBP's ability to reach this variable 617 00:35:54,620 --> 00:35:58,260 without running into a one byte signed issue. 618 00:35:59,140 --> 00:36:01,440 Okay, but that simple change, 619 00:36:01,600 --> 00:36:02,300 that small change, 620 00:36:02,440 --> 00:36:05,020 obviously there's not a lot of randomness left there 621 00:36:05,620 --> 00:36:06,720 or available there, 622 00:36:07,760 --> 00:36:09,200 but the attacker, 623 00:36:09,320 --> 00:36:11,240 and the attacker can certainly brute force you. 624 00:36:11,420 --> 00:36:12,060 So, you know, 625 00:36:12,060 --> 00:36:14,280 where do you maybe play around with things? 626 00:36:14,460 --> 00:36:17,300 You just, you use alternate alignments. 627 00:36:18,740 --> 00:36:20,120 Instead of changing this, 628 00:36:20,200 --> 00:36:21,140 just following the top, 629 00:36:21,240 --> 00:36:22,420 we're going to keep stack pointer aligned 630 00:36:22,420 --> 00:36:23,400 on a four byte boundary, 631 00:36:23,400 --> 00:36:27,620 but we might adjust string one to start, 632 00:36:27,700 --> 00:36:30,100 instead of 7C, 7B. 633 00:36:30,940 --> 00:36:32,840 So then any alignments, 634 00:36:32,840 --> 00:36:35,520 any pointers to things fall off by four bytes 635 00:36:35,520 --> 00:36:36,860 when you pop them into EIP. 636 00:36:37,660 --> 00:36:40,180 Okay, because they're trying to get addresses into EIP, 637 00:36:40,380 --> 00:36:43,320 and they're expecting a particular alignment 638 00:36:43,320 --> 00:36:45,500 in this buffer as they overwrite EIP. 639 00:36:45,580 --> 00:36:46,720 If you shift it off by one, 640 00:36:47,280 --> 00:36:49,560 they don't get quite what they had hoped for. 641 00:36:49,840 --> 00:36:51,960 So, you know, just other opportunities. 642 00:36:52,160 --> 00:36:52,320 So, 643 00:36:53,700 --> 00:36:55,180 I'm going to try to wrap this up, 644 00:36:55,320 --> 00:36:57,480 and we'll see if RPC Decom 645 00:36:57,480 --> 00:36:59,400 is still running on some of these boxes. 646 00:37:01,480 --> 00:37:02,400 Okay, and 647 00:37:04,160 --> 00:37:06,160 let's go back to the unpatched box 648 00:37:06,160 --> 00:37:08,460 and try our Metasploit code here. 649 00:37:18,320 --> 00:37:19,640 And bam, that was quick. 650 00:37:20,560 --> 00:37:21,020 Okay, 651 00:37:21,020 --> 00:37:24,040 everybody sees at the bottom I have a command prompt 652 00:37:25,760 --> 00:37:27,100 on the unpatched system. 653 00:37:28,120 --> 00:37:28,540 Okay, 654 00:37:30,020 --> 00:37:30,960 exit out of there, 655 00:37:31,700 --> 00:37:33,900 and I kind of like Metasploit in that respect. 656 00:37:34,540 --> 00:37:36,520 They clean up very nicely after themselves. 657 00:37:37,220 --> 00:37:39,540 Okay, they leave RPC Decom up and running, 658 00:37:39,660 --> 00:37:41,500 and you can come right back into the system. 659 00:37:41,860 --> 00:37:45,400 Okay, that's far more food than I possess. 660 00:37:46,820 --> 00:37:47,300 Okay, 661 00:37:48,160 --> 00:37:49,200 we will give, 662 00:37:51,060 --> 00:37:52,680 the unpatched system a try. 663 00:37:57,560 --> 00:38:00,980 I can't promise you Decom's not already whacked on that box. 664 00:38:04,800 --> 00:38:05,240 So, 665 00:38:05,340 --> 00:38:06,720 it's not a very good demonstration 666 00:38:06,720 --> 00:38:07,880 if we don't even know it's running. 667 00:38:08,000 --> 00:38:10,160 What's a quick command line to show that Decom's running? 668 00:38:10,300 --> 00:38:10,700 RPC. 669 00:38:10,920 --> 00:38:11,400 Anybody know? 670 00:38:13,440 --> 00:38:13,880 Netstat? 671 00:38:14,900 --> 00:38:15,780 That works. 672 00:38:23,860 --> 00:38:24,380 Okay, 673 00:38:24,580 --> 00:38:26,740 and what's our favorite port number for RPC Decom? 674 00:38:28,560 --> 00:38:29,080 440, 675 00:38:29,140 --> 00:38:30,600 UDP 445 right there? 676 00:38:33,420 --> 00:38:33,940 Okay, 677 00:38:34,160 --> 00:38:35,060 TCP 445? 678 00:38:35,880 --> 00:38:36,400 Okay. 679 00:38:36,860 --> 00:38:37,380 So, 680 00:38:39,120 --> 00:38:42,440 we'll assume that Decom remains up and running. 681 00:38:44,680 --> 00:38:46,900 I apologize for not getting that checked out. 682 00:38:48,300 --> 00:38:49,720 And we switch to, 683 00:38:57,460 --> 00:38:57,900 okay, 684 00:38:58,620 --> 00:38:59,580 we give it a go. 685 00:38:59,680 --> 00:39:00,000 Yep, 686 00:39:00,080 --> 00:39:00,580 couldn't mind. 687 00:39:01,640 --> 00:39:02,080 Okay, 688 00:39:02,160 --> 00:39:02,480 so, 689 00:39:02,540 --> 00:39:08,200 that simple little fix defeats the stock Metasploit exploit to get into RPC Decom. 690 00:39:08,780 --> 00:39:15,680 And we hope that the authors of the Metasploit payloads and exploits are a little bit smarter than the average worm arthur. 691 00:39:16,400 --> 00:39:16,840 Okay, 692 00:39:16,840 --> 00:39:18,980 since they're probably cutting and pasting that stuff anyway. 693 00:39:19,700 --> 00:39:20,840 They're right out of Metasploit. 694 00:39:20,980 --> 00:39:21,200 So, 695 00:39:21,240 --> 00:39:23,900 we go back to the box and give it a go. 696 00:39:27,060 --> 00:39:28,220 And I think, 697 00:39:28,240 --> 00:39:28,420 well, 698 00:39:28,500 --> 00:39:29,820 it's still showing as running. 699 00:39:30,280 --> 00:39:30,720 Yeah, 700 00:39:30,720 --> 00:39:34,440 I think it's probably been whacked. 701 00:39:34,920 --> 00:39:35,360 Okay, 702 00:39:35,400 --> 00:39:36,760 I'm not sure we can connect to it anymore. 703 00:39:38,140 --> 00:39:38,580 But, 704 00:39:39,180 --> 00:39:40,200 that's the basic idea. 705 00:39:40,920 --> 00:39:43,260 And I think that about, 706 00:39:43,420 --> 00:39:43,580 well, 707 00:39:43,600 --> 00:39:46,360 I'm at 10 minutes and that's probably going to roll into, 708 00:39:46,360 --> 00:39:47,120 questions. 709 00:39:48,120 --> 00:39:48,420 Again, 710 00:39:48,520 --> 00:39:49,520 it's like silly, 711 00:39:49,600 --> 00:39:51,180 silly hex editor tricks. 712 00:39:51,640 --> 00:39:52,100 Okay, 713 00:39:52,120 --> 00:39:53,040 something you can do at home. 714 00:39:53,660 --> 00:39:54,120 Okay, 715 00:39:54,240 --> 00:39:55,100 if you're not a coder. 716 00:39:55,200 --> 00:39:55,740 You know, 717 00:39:55,740 --> 00:39:56,340 you don't have to, 718 00:39:56,340 --> 00:39:56,500 again, 719 00:39:56,600 --> 00:39:57,900 inject new code into binary, 720 00:39:58,100 --> 00:39:58,720 change the binary, 721 00:39:58,920 --> 00:39:59,980 build new DLLs, 722 00:40:00,000 --> 00:40:02,380 use Microsoft's hot patching, 723 00:40:02,380 --> 00:40:03,760 hot patching compiler techniques, 724 00:40:04,280 --> 00:40:09,920 or wait for some third party necessarily to offer up a behavioral hot fix. 725 00:40:10,400 --> 00:40:10,860 Okay, 726 00:40:10,980 --> 00:40:11,340 if, 727 00:40:11,440 --> 00:40:12,220 you know, 728 00:40:12,220 --> 00:40:13,900 this is something that you're interested in. 729 00:40:13,960 --> 00:40:14,720 Works in CTF. 730 00:40:15,360 --> 00:40:15,820 Okay, 731 00:40:17,860 --> 00:40:18,440 that's the other thing. 732 00:40:18,880 --> 00:40:19,320 Okay, 733 00:40:19,320 --> 00:40:19,540 with that, 734 00:40:19,720 --> 00:40:23,560 I thank you for coming and I'll take any questions that anybody may have. 735 00:40:23,660 --> 00:40:24,700 Is somebody standing back here? 736 00:40:24,700 --> 00:40:35,820 yes I think there is a there's some possibility I've tried to I've pondered 737 00:40:35,820 --> 00:40:43,140 that and the idea is it I mean the problem is say if you come out and say 738 00:40:43,140 --> 00:40:49,280 hey here's a mutation and you make the mutation public then everybody uses the 739 00:40:49,280 --> 00:40:53,380 same mutation and we're right back where we started from so yeah you want a way 740 00:40:53,380 --> 00:40:59,800 to build like a mutation generator and you generate the mutation so that 741 00:40:59,800 --> 00:41:05,440 hopefully it's fairly unique or at least randomized to your circumstances and 742 00:41:05,440 --> 00:41:10,040 once you again some of these patches these patches are only like four and 743 00:41:10,040 --> 00:41:16,680 seven bytes in these two cases and not behavioral you know strictly layout wise 744 00:41:16,680 --> 00:41:23,240 so once you understand where the bytes need to go you can probably specify a 745 00:41:23,240 --> 00:41:23,360 randomization and then you can go back and say hey here's a mutation and you 746 00:41:23,360 --> 00:41:25,840 range of values and you need to understand the relationships among the 747 00:41:25,840 --> 00:41:30,960 values if I modify this if I subtract off here I need to add over here and I 748 00:41:30,960 --> 00:41:33,720 can subtract here's the range I can subtract off of and once you pick that 749 00:41:33,720 --> 00:41:37,500 then you can probably spit out a mutation automatically yeah so there's 750 00:41:37,500 --> 00:41:40,300 probably some opportunity obviously the more you want to do the more complex it 751 00:41:40,300 --> 00:41:45,520 gets so yes right the the quality of 752 00:41:45,520 --> 00:41:49,020 disassembly you have to do and the level of detail you have to grok the 753 00:41:49,020 --> 00:41:53,260 stack layout here to be able to do these patches is just a couple of 754 00:41:53,360 --> 00:41:56,560 steps shy of spotting the vulnerability in the first place and probably being 755 00:41:56,560 --> 00:42:04,020 able to patch it directly is that yeah and that's why I don't know where the 756 00:42:04,020 --> 00:42:10,300 skill level separates I think it I personally think it's easier to look at 757 00:42:10,300 --> 00:42:16,300 assembly then to take that extra step and understand the implications of 758 00:42:16,300 --> 00:42:22,880 trying to inject new code or or shift code or even then dig into modifying a 759 00:42:22,880 --> 00:42:28,800 PE headers or things like that I think that's that is a it's a it's probably a 760 00:42:28,800 --> 00:42:33,400 fine line I think again this is this is probably a lower technical skill level 761 00:42:35,440 --> 00:42:40,420 and obviously I think you know applying the diffs or changing bytes in a binary 762 00:42:40,420 --> 00:42:44,120 and shipping it you know say and run this code is difficult in either case 763 00:42:44,120 --> 00:42:48,040 but I think you can be more confident that these will work without breaking 764 00:42:48,040 --> 00:42:50,520 your code sooner than getting the patch in 765 00:42:53,520 --> 00:42:57,940 what happens when the vendor issues the official patch do you have to go back 766 00:42:57,940 --> 00:43:03,880 and revert to what it was before that I suppose that depends on the vendors 767 00:43:03,880 --> 00:43:08,040 patching mechanism and I don't know if they verify whether the the md5 or 768 00:43:08,040 --> 00:43:12,420 whatever on the original on the DLL that's going to be replaced because tip 769 00:43:12,420 --> 00:43:17,100 most it depends on the patching technique okay if I'm if I'm good if the 770 00:43:17,100 --> 00:43:22,740 vendors going to give you a whole new DLL okay then the overwrite will just 771 00:43:22,880 --> 00:43:26,160 kill this fix and hope and incorporate the vendor fix if the vendor is going to 772 00:43:26,160 --> 00:43:33,480 come into your DLL and do a hotfix over the top of it then you have issues you 773 00:43:33,480 --> 00:43:37,840 might have to roll this back and so that they are hot fixing the known quantity 774 00:43:37,840 --> 00:43:41,960 that they had back in their development shops yeah that's a good question okay 775 00:43:41,960 --> 00:43:49,180 thank you a lot of vendors are moving toward code signing as a means of 776 00:43:49,180 --> 00:43:52,420 protecting against rootkits and Trojans and that sort of thing 777 00:43:52,420 --> 00:43:56,700 in that circumstance it wouldn't be generally possible for a user to modify 778 00:43:56,700 --> 00:43:59,720 a binary on their own because they wouldn't be able to get it signed are 779 00:44:00,140 --> 00:44:04,420 there any dynamic techniques that could potentially be used along these same 780 00:44:04,420 --> 00:44:09,820 lines to maybe hot catch something that would would be able to get around the 781 00:44:09,820 --> 00:44:13,980 code signing I haven't really thought about it the only thing that comes to 782 00:44:13,980 --> 00:44:18,420 mind is you if you could modify it in memory in place if the code signing the 783 00:44:18,420 --> 00:44:22,000 code signature checks take place when the binaries initially loaded into 784 00:44:22,000 --> 00:44:22,300 memory 785 00:44:22,420 --> 00:44:27,700 and I don't know that they periodically recheck the integrity of the binary in 786 00:44:27,700 --> 00:44:33,260 any way shape or form so you if if you want to do it in a hotfix way you'd 787 00:44:33,260 --> 00:44:38,240 have to have direct write access to process memory space which is tough on 788 00:44:38,240 --> 00:44:43,500 some processors although if you've seen Joanna's stuff on Vista rootkits 789 00:44:45,020 --> 00:44:51,260 there's some opportunities there perhaps thanks any other questions 790 00:44:52,940 --> 00:44:55,820 okay I think I'm out of here on time and thanks for coming