1 00:00:00,000 --> 00:00:06,020 All right. Well, props to you guys for showing up. It's the first talk of the morning, so I appreciate you being here. 2 00:00:06,780 --> 00:00:15,220 All right. So, my name is Tiller Beauchamp. I work at SAIC, do security consulting, pen testing, some other interesting stuff. 3 00:00:15,660 --> 00:00:20,660 And I did this research with Dave Weston, who used to work there with me and is now at Microsoft. 4 00:00:23,260 --> 00:00:26,720 So, yeah. Today, we're going to be talking about retrace. 5 00:00:26,720 --> 00:00:34,460 Retrace. So, we'll start with tracing applications at runtime and show how we can use this for vulnerability pinpointing. 6 00:00:34,740 --> 00:00:42,940 We'll go into programmatic debugging with a Ruby-based toolkit we wrote and end up with some generic kernels and application stuff. 7 00:00:45,560 --> 00:00:49,780 So, first, let's talk about the background of DTrace in case you're not familiar with it. 8 00:00:50,040 --> 00:00:54,740 This is a kernel-based dynamic tracing framework, something that Sun built many years ago. 9 00:00:56,780 --> 00:00:59,880 They open-sourced it under their SDDL license. 10 00:01:00,300 --> 00:01:04,320 It was released with Sun OS X back in 2005. 11 00:01:05,600 --> 00:01:09,180 And DTrace has since been ported to Leopard. 12 00:01:09,340 --> 00:01:14,800 So, back in October of 2007, it was the first time that we saw this on a desktop operating system. 13 00:01:15,280 --> 00:01:19,700 So, that's when we started to get curious about how can we use this for our exploit needs. 14 00:01:20,920 --> 00:01:23,400 We're also seeing it being ported to FreeBSD. 15 00:01:23,820 --> 00:01:25,920 It's currently in the FreeBSD 7. 16 00:01:26,720 --> 00:01:30,460 It's currently in the current branch and will be in the FreeBSD 8 stable branch. 17 00:01:30,740 --> 00:01:34,380 So, this guy, John Viral, ported it to FreeBSD. 18 00:01:34,760 --> 00:01:40,400 Yeah, so, what we're going to talk about mostly, we're going to use examples of OSX as an operating system. 19 00:01:40,580 --> 00:01:43,320 But, really, it's generic to anybody who implements DTrace. 20 00:01:43,460 --> 00:01:48,480 So, if you've got a Ruby interpreter and you've got DTrace, you can use our toolkit with a little bit of customization. 21 00:01:49,740 --> 00:01:56,340 One interesting thing, which I'm not sure, is I had heard in the upcoming iPhone releases that DTrace may be included. 22 00:01:56,720 --> 00:01:58,940 So, I know instruments is supposed to be included. 23 00:01:59,120 --> 00:02:02,740 So, that would implicitly tell you that there's some DTrace functionality. 24 00:02:03,000 --> 00:02:07,220 So, I'd be very interested in seeing how DTrace performs on a mobile device coming up. 25 00:02:08,020 --> 00:02:11,440 We've also seen it on Netrino, partial implementation. 26 00:02:13,120 --> 00:02:18,320 So, the basic overview here is that DTrace is a performance tracing framework. 27 00:02:18,800 --> 00:02:22,400 So, it enables you to observe what an application is doing at runtime. 28 00:02:22,760 --> 00:02:26,700 This may remind you of other tracing utilities like S-Trace or K-Trace. 29 00:02:26,720 --> 00:02:29,900 Or, things that are, sort of, single-purpose. 30 00:02:30,520 --> 00:02:39,460 DTrace gives you the framework to implement your own tools to either re-implement things like S-Trace or to specify exactly what you're interested in tracing. 31 00:02:41,080 --> 00:02:45,800 So, you do this by defining these probes and then actions that go along with the probes. 32 00:02:47,580 --> 00:02:49,800 So, this is what the architecture looks like here. 33 00:02:50,560 --> 00:02:53,220 We have a lot of cooperation from the kernel. 34 00:02:53,380 --> 00:02:55,740 That's what the bottom of the screen is here. 35 00:02:55,740 --> 00:03:04,480 So, you get these categories of probes, like system call probes or the function boundary tracing for tracing functions within the kernel itself. 36 00:03:05,780 --> 00:03:13,140 So, one useful way to think about this in analogy may be, like, consider an entire operating system where there's breakpoints in every strategic place. 37 00:03:13,320 --> 00:03:18,020 And, it's just a matter of enabling those breakpoints to get some type of instrumentation in that area. 38 00:03:18,540 --> 00:03:23,820 So, it really is like having a system with a debugger attached from top to bottom. 39 00:03:23,920 --> 00:03:24,760 So, that's kernel subsystems. 40 00:03:25,540 --> 00:03:25,720 The kernel sub-systems. 41 00:03:25,740 --> 00:03:26,020 The kernel itself. 42 00:03:26,180 --> 00:03:26,580 IO. 43 00:03:27,200 --> 00:03:27,600 Networking. 44 00:03:27,700 --> 00:03:28,560 User applications. 45 00:03:28,840 --> 00:03:33,980 And, at any time, you can just say, hey, I want to know what's going to happen when that particular point of instrumentation is hit. 46 00:03:35,720 --> 00:03:47,100 So, there's this user LAN library here, libdtrace, which is utilized by a number of applications, generic OS applications, but then also the dtrace user LAN binary itself. 47 00:03:47,560 --> 00:03:52,780 So, what we're interested in doing is writing these dtrace scripts, which will be run through this whole environment. 48 00:03:56,680 --> 00:04:03,840 So, we write these little dtrace scripts in a language called D. The authors describe it as a syntax of C. 49 00:04:04,420 --> 00:04:13,180 So, you basically specify your actions that get compiled into some intermediate form and then run with cooperation of the kernel as the application is being traced. 50 00:04:15,960 --> 00:04:20,740 Now, D actually turns out to be quite limiting because it doesn't have control flow. 51 00:04:21,000 --> 00:04:25,720 So, there's not things like loops, which really limits what we can do in our logic. 52 00:04:25,720 --> 00:04:33,720 So, it's really meant for taking measurements and recording, doing comparisons and small amounts of recording. 53 00:04:34,300 --> 00:04:35,800 And there's a good reason for this, right? 54 00:04:35,900 --> 00:04:46,060 You don't want to be the kernel and accidentally hand off control to some script that's hooking a function that's never going to return because the kernel will never get control and then you're completely screwed. 55 00:04:49,120 --> 00:04:55,360 So, you'll see that safety and performance are major priority requirements in the design of dtrace. 56 00:04:55,360 --> 00:04:59,920 So, sometimes this works to our advantage because we care about performance when we're doing tracing. 57 00:05:00,120 --> 00:05:04,060 And sometimes it's not to our advantage because you'll end up dropping probes and stuff. 58 00:05:04,300 --> 00:05:12,840 So, that's probably one of the main problems solved by our talk and by our framework is, of course, we've got this really cool framework that does tracing in the entire system. 59 00:05:12,940 --> 00:05:19,460 But it's primarily designed for troubleshooting, for performance, and for overall debugging of production systems. 60 00:05:19,620 --> 00:05:24,440 So, Sun created this as a way to say, hey, we can send our consultant in there on your live production system. 61 00:05:24,520 --> 00:05:25,340 And he can tell us what to do. 62 00:05:25,360 --> 00:05:40,880 He can tell you what's leaking memory, what's sitting on CPU for too long without you having to set up a completely separate system, which you would have normally had to do, recreating your production environment so that you can debug because it's too dangerous to attach a debugger to Apache or your SQL server in live. 63 00:05:41,300 --> 00:05:53,280 So, what's happened is that, of course, that gives us a lot of power as reverse engineers, but because of the safety and performance considerations, you'll see that we kind of have to figure out and do a MacGyver thing around some of those roadblocks. 64 00:05:55,620 --> 00:06:00,200 So, the bottom line for performance is that there really is no impact when DTrace is not active. 65 00:06:00,820 --> 00:06:04,260 And when it is active, it's been streamlined to be most efficient. 66 00:06:05,900 --> 00:06:08,560 Okay, so there's a million of different things we can do with DTrace. 67 00:06:08,920 --> 00:06:13,200 And a lot of them have been implemented with these singular tools like strace and truss and so. 68 00:06:13,480 --> 00:06:19,560 So, you can go re-implement those if you want or focus in on exactly what you're curious about in an application. 69 00:06:22,820 --> 00:06:25,340 So, the real takeaway here is that DTrace combines a lot of things. 70 00:06:25,340 --> 00:06:30,320 System performance, statistics, debugging information, and execution analysis into one small framework. 71 00:06:31,020 --> 00:06:44,420 So, it turns out to be very useful for us as reverse engineers because we want to answer very general questions about behavior, but then we also want the ability to zoom into a specific situation or condition and inspect exactly what the application is doing. 72 00:06:45,700 --> 00:06:53,260 We also have the ability to trace the application as it's interacting with the rest of the system or to trace multiple applications at once. 73 00:06:53,460 --> 00:06:54,000 Okay. 74 00:06:55,840 --> 00:07:01,940 So, before you run off and all write your own DTrace scripts, there's a lot of work that's already been done out there that you can reference. 75 00:07:02,260 --> 00:07:08,380 So, there's this thing called the DTrace Toolkit, which includes pretty much every kind of script you'd want for monitoring performance. 76 00:07:08,840 --> 00:07:19,040 Things like I.O., disk I.O., system call tracing, things to snoop file input and output, which is interesting, and monitoring file activity. 77 00:07:20,180 --> 00:07:23,600 Just to give you an idea of how powerful DTrace actually is, 78 00:07:23,660 --> 00:07:25,320 if you noticed in the 10.4 summary, there was a lot of stuff that was going on. 79 00:07:25,320 --> 00:07:31,040 So, the Tiger release, there was a utility that's been around since the creation of OSX called KTrace, 80 00:07:31,180 --> 00:07:34,140 and that was your main system call tracing tool. 81 00:07:34,480 --> 00:07:40,920 Well, that's been replaced in Leopard with a 50-line D script, and it performs about 60% better. 82 00:07:41,220 --> 00:07:44,860 So, you can actually replicate any of those tools that we showed on the previous slide. 83 00:07:45,120 --> 00:07:50,320 The nice thing is that you have a singular interface to all of these tools, 84 00:07:50,440 --> 00:07:54,860 so instead of having to write a custom bash script where you launch STrace, 85 00:07:54,860 --> 00:07:59,800 and then switch to GDB, you have all of this in a sort of uniform, very elegant framework 86 00:07:59,800 --> 00:08:02,320 that you can use to create sort of monster tools. 87 00:08:02,620 --> 00:08:05,820 And you'll see a lot of that in the Sun DTrace Toolkit, some really great examples. 88 00:08:07,520 --> 00:08:09,440 Okay, so let's take a look at some simple examples. 89 00:08:10,300 --> 00:08:13,900 This is a one-line DTrace script. This is a script here. 90 00:08:14,000 --> 00:08:16,420 You've got the probe definition and the action itself. 91 00:08:16,940 --> 00:08:20,520 Basically, what this is going to do is trace every application across the system, 92 00:08:21,280 --> 00:08:24,840 recording the number of times that each application makes a system call. 93 00:08:24,840 --> 00:08:26,420 Regardless of what that call was. 94 00:08:26,880 --> 00:08:30,900 So, the output from running this script for a short period of time shows us that, 95 00:08:30,900 --> 00:08:35,400 you know, Syslog made one system call, CubsD made four system calls, 96 00:08:35,620 --> 00:08:38,440 and VMware made almost 7,000 system calls. 97 00:08:38,880 --> 00:08:43,160 So, what we might be interested in doing here is drilling down and focusing on VMware 98 00:08:43,160 --> 00:08:45,680 and saying, okay, which system calls is it making? 99 00:08:45,940 --> 00:08:47,880 Or what the arguments to those system calls are. 100 00:08:49,760 --> 00:08:50,960 Here's another example. 101 00:08:51,500 --> 00:08:54,640 This shows a small DTrace script that, 102 00:08:54,840 --> 00:08:59,900 it hooks every open function, every open syscall function at the entry point, 103 00:09:00,100 --> 00:09:01,980 and then simply prints out the executable name 104 00:09:01,980 --> 00:09:03,880 and the first parameter of that function, 105 00:09:04,080 --> 00:09:05,800 which will be the file that was opened. 106 00:09:07,220 --> 00:09:09,240 So, output from this might look like this. 107 00:09:09,480 --> 00:09:12,040 It shows finders opening a plist file, 108 00:09:13,100 --> 00:09:15,700 VMware's opening dev view random, and so on. 109 00:09:15,820 --> 00:09:18,900 So, these configuration files and these resource files 110 00:09:18,900 --> 00:09:20,460 are being opened by these different applications. 111 00:09:20,760 --> 00:09:23,420 So, the cool part is you can start to use what are called predicates, 112 00:09:23,440 --> 00:09:24,500 which we'll explain in a second. 113 00:09:24,500 --> 00:09:28,580 Or you can say, when this system call happens with this particular argument, 114 00:09:28,880 --> 00:09:30,400 which files are then open? 115 00:09:30,740 --> 00:09:35,440 So, you can get some really deep sort of nested if-else type of statements 116 00:09:35,440 --> 00:09:39,360 to really zoom in on the particular area of the application that you're interested in, 117 00:09:39,380 --> 00:09:41,820 all without having access to the source code. 118 00:09:44,760 --> 00:09:47,740 Okay, so to review some of the terms we've heard here, 119 00:09:48,000 --> 00:09:51,000 there's probes, which are your points of instrumentation, 120 00:09:51,080 --> 00:09:53,140 or you can think of them as your points of hooking. 121 00:09:54,060 --> 00:09:57,460 There's these providers, which are just logical categories of probes. 122 00:09:58,020 --> 00:10:00,720 And then there's predicates, which are conditional statements. 123 00:10:01,120 --> 00:10:04,260 So, these are evaluated true or false, 124 00:10:04,300 --> 00:10:06,140 and then the action is carry it out or not. 125 00:10:06,380 --> 00:10:08,960 So, you can hook something and then decide not to do something. 126 00:10:09,580 --> 00:10:13,820 This is, if you've seen awk, it's much, the syntax is much like awk. 127 00:10:14,020 --> 00:10:17,160 You specify some condition, like, you know, the line, 128 00:10:17,280 --> 00:10:18,540 awk is processing a line. 129 00:10:18,660 --> 00:10:20,760 So, the line has four elements to it, 130 00:10:20,780 --> 00:10:22,900 and then the action that goes along with that. 131 00:10:23,140 --> 00:10:26,680 You know, and if there's four elements in the line, 132 00:10:26,760 --> 00:10:27,720 then print out the first one. 133 00:10:28,040 --> 00:10:31,580 So, it's a hook, a predicate, and an action. 134 00:10:32,680 --> 00:10:35,060 So, the way the syntax looks is like this. 135 00:10:35,240 --> 00:10:38,540 You've got your probe, which consists of the category, 136 00:10:38,920 --> 00:10:40,780 the module, which is usually a library, 137 00:10:41,080 --> 00:10:43,640 the function name, and then this last field 138 00:10:43,640 --> 00:10:45,100 is usually the entry point of the function, 139 00:10:45,300 --> 00:10:46,620 the exit point of the function, 140 00:10:46,720 --> 00:10:48,620 or every instruction within that function. 141 00:10:49,280 --> 00:10:51,240 The predicate is the conditional statement 142 00:10:51,240 --> 00:10:52,860 that's evaluated to true or false, 143 00:10:53,140 --> 00:10:54,220 and then the action is carried out 144 00:10:54,220 --> 00:10:55,280 depending on that predicate. 145 00:10:58,300 --> 00:11:01,360 Okay, so let's talk about how we utilize this 146 00:11:01,360 --> 00:11:02,740 for our reverse engineering tasks. 147 00:11:04,380 --> 00:11:06,480 Okay, so you probably already have some ideas 148 00:11:06,480 --> 00:11:08,200 of how this can be used for reverse engineering. 149 00:11:08,740 --> 00:11:10,360 You can get all sorts of information 150 00:11:10,360 --> 00:11:13,960 about how a particular application you're interested in, 151 00:11:14,020 --> 00:11:15,860 either testing for security or understanding, 152 00:11:17,100 --> 00:11:19,180 just a general understanding of how it's interacting 153 00:11:19,180 --> 00:11:20,560 with the system as a whole. 154 00:11:20,700 --> 00:11:22,580 So, you know what syscalls are going on, 155 00:11:23,140 --> 00:11:25,020 when it's making IOs, what files it's touching, 156 00:11:25,880 --> 00:11:27,400 all sorts of things about it. 157 00:11:27,540 --> 00:11:29,900 So the idea here is that you can really quickly, 158 00:11:30,060 --> 00:11:32,520 by either using a script that you've created beforehand 159 00:11:32,520 --> 00:11:34,260 or tailoring a script to the behavior 160 00:11:34,260 --> 00:11:35,480 of that particular application, 161 00:11:35,940 --> 00:11:37,600 you can get an idea of how it's working 162 00:11:37,600 --> 00:11:39,060 at a sort of a top level, 163 00:11:39,180 --> 00:11:40,780 and then you can zoom in on the areas 164 00:11:40,780 --> 00:11:43,160 that maybe are part of the attack surface 165 00:11:43,160 --> 00:11:45,060 or just going to give you some general ideas 166 00:11:45,060 --> 00:11:48,180 about the security of the application itself. 167 00:11:48,580 --> 00:11:49,880 So it's very useful. 168 00:11:50,060 --> 00:11:51,440 The other thing is you can use predicates. 169 00:11:51,440 --> 00:11:53,460 So, for example, you may want to trace 170 00:11:54,060 --> 00:11:57,080 until a function gets an input from your fuzzer, right? 171 00:11:57,160 --> 00:11:59,460 If string copy's getting 5,000 A's, 172 00:11:59,560 --> 00:12:01,220 maybe you want to trigger the debugger at that point 173 00:12:01,220 --> 00:12:03,120 and say, hey, I want to see what's going on here. 174 00:12:03,500 --> 00:12:05,720 So it kind of releases you from that idea 175 00:12:05,720 --> 00:12:07,700 of having to say a million breakpoints, 176 00:12:07,780 --> 00:12:09,720 which kill the performance and change the memory layout 177 00:12:09,720 --> 00:12:10,380 of your application, 178 00:12:11,020 --> 00:12:13,200 and will only trigger a debugger 179 00:12:13,200 --> 00:12:15,080 when there's a specific incidence 180 00:12:15,080 --> 00:12:16,140 that you're interested in. 181 00:12:17,460 --> 00:12:19,100 So the way we like to think of it 182 00:12:19,100 --> 00:12:20,760 is as a rapid development, 183 00:12:20,760 --> 00:12:23,620 a framework for reverse engineering tasks and tools. 184 00:12:24,280 --> 00:12:26,860 So the way we use it is we're lazy reverse engineers, right? 185 00:12:26,920 --> 00:12:27,640 We're lazy hackers. 186 00:12:28,000 --> 00:12:29,460 If something takes too long, 187 00:12:29,540 --> 00:12:31,040 if you have to attach the debugger 188 00:12:31,040 --> 00:12:33,220 and you have to write all these conditional breakpoints, 189 00:12:33,440 --> 00:12:34,600 you're going to get tired. 190 00:12:34,840 --> 00:12:37,420 But the idea here is that we've really sped up 191 00:12:37,420 --> 00:12:38,860 the exploit development time, 192 00:12:38,960 --> 00:12:40,280 the vulnerability assessment time 193 00:12:40,280 --> 00:12:44,000 by giving you the tools that can really tell you 194 00:12:44,000 --> 00:12:45,700 what things you're interested in very quickly. 195 00:12:45,920 --> 00:12:47,080 So you can script things. 196 00:12:47,320 --> 00:12:50,080 You can attach and unattach from an application 197 00:12:50,080 --> 00:12:51,040 as it runs. 198 00:12:51,240 --> 00:12:52,580 Because it's running in the kernel, 199 00:12:52,800 --> 00:12:54,640 you're not interfering with the memory layout at all. 200 00:12:54,920 --> 00:12:57,840 So if you've ever experienced this while using GDB, 201 00:12:58,080 --> 00:12:59,780 you set up an exploit, you get it working. 202 00:13:00,100 --> 00:13:02,660 The second you detach GDB, the memory layout's different, 203 00:13:02,760 --> 00:13:04,040 so now your shellcode doesn't execute 204 00:13:04,040 --> 00:13:04,980 and it crashes instead. 205 00:13:05,260 --> 00:13:06,920 You don't have those problems with Dtrace. 206 00:13:07,160 --> 00:13:09,100 So the idea is that you can refine 207 00:13:09,100 --> 00:13:11,520 what you're interested in or what you're doing 208 00:13:11,520 --> 00:13:14,220 without any performance penalties or slowdown at all. 209 00:13:18,240 --> 00:13:19,800 Okay, so let's look at some of the 210 00:13:20,080 --> 00:13:23,040 simple things we get with Dtrace that are provided for us 211 00:13:23,040 --> 00:13:25,240 that we would have to implement ourselves 212 00:13:25,240 --> 00:13:27,260 if we were to write our own custom tool. 213 00:13:27,900 --> 00:13:30,760 So this is things like control flow graphs, 214 00:13:31,200 --> 00:13:35,140 symbol resolution, call stack traces, 215 00:13:35,440 --> 00:13:38,040 and the ability to examine function parameters 216 00:13:38,040 --> 00:13:42,140 both in user space and kernel space. 217 00:13:42,660 --> 00:13:44,560 So here's an example of control flow. 218 00:13:44,940 --> 00:13:49,660 We've got the arrows to the right indicate 219 00:13:49,660 --> 00:13:50,940 that the function's being called, 220 00:13:51,000 --> 00:13:53,040 and then to the left indicate that the function's returning. 221 00:13:53,420 --> 00:13:55,480 So from here, we can get a sense of which functions 222 00:13:55,480 --> 00:13:57,380 are calling which functions and when they're returning. 223 00:13:57,500 --> 00:13:59,040 This is the dynamic call graph. 224 00:14:00,600 --> 00:14:02,260 Here's an example of a stack trace 225 00:14:02,260 --> 00:14:03,800 with all our symbol information. 226 00:14:04,140 --> 00:14:07,340 So this is the FTPD main function at the bottom here, 227 00:14:07,440 --> 00:14:09,780 and it's call stack trace all the way up to the top 228 00:14:09,780 --> 00:14:11,160 where it's calling string compare. 229 00:14:11,900 --> 00:14:13,840 So not only do you see the library, 230 00:14:14,020 --> 00:14:16,200 but also the function name and the instruction offset 231 00:14:16,200 --> 00:14:17,340 within that function. 232 00:14:17,520 --> 00:14:19,180 So it gives us the ability to pinpoint exactly 233 00:14:19,660 --> 00:14:20,600 where we are in the execution. 234 00:14:22,700 --> 00:14:26,100 We can quickly and conveniently record 235 00:14:26,100 --> 00:14:29,280 the function parameters as well as the return values. 236 00:14:29,920 --> 00:14:33,100 So this shows us that it's recording 237 00:14:33,100 --> 00:14:34,620 the first argument of the function. 238 00:14:34,940 --> 00:14:36,600 So if we're hooking string compare, 239 00:14:36,900 --> 00:14:39,960 it simply prints out what the first argument is, 240 00:14:39,980 --> 00:14:40,700 a special one. 241 00:14:41,140 --> 00:14:43,980 The syntax here actually gives you a little bit of idea 242 00:14:43,980 --> 00:14:45,040 of how DTrace is working. 243 00:14:45,400 --> 00:14:48,480 If you notice, the syntax there is copy nstr, 244 00:14:48,480 --> 00:14:50,540 because what you're actually doing here 245 00:14:50,540 --> 00:14:52,960 is you're asking the kernel to copy information 246 00:14:52,960 --> 00:14:55,640 from userland into the kernel to give you information. 247 00:14:56,360 --> 00:14:58,220 So the way a breakpoint works 248 00:14:58,220 --> 00:15:00,460 is by modifying the target process memory. 249 00:15:00,720 --> 00:15:01,940 It writes an in-through instruction, 250 00:15:02,360 --> 00:15:06,080 and that's what causes the execution to pause in the CPU, 251 00:15:06,260 --> 00:15:07,640 and the CPU calls back to you and says, 252 00:15:07,680 --> 00:15:08,360 what do you want to do? 253 00:15:08,560 --> 00:15:09,560 This is very different. 254 00:15:09,740 --> 00:15:11,860 It doesn't modify any instructions in any way 255 00:15:11,860 --> 00:15:14,160 or cause any pause in the execution 256 00:15:14,160 --> 00:15:15,800 of the program at runtime. 257 00:15:15,800 --> 00:15:18,360 So you're getting a great view 258 00:15:18,360 --> 00:15:20,300 of everything that's going on in this application 259 00:15:20,300 --> 00:15:21,480 from the kernel's perspective 260 00:15:21,480 --> 00:15:23,540 without disrupting it at all. 261 00:15:23,680 --> 00:15:25,020 So what you see is what you get here, 262 00:15:25,100 --> 00:15:26,440 and that really becomes useful. 263 00:15:26,740 --> 00:15:29,340 When you're attaching a fuzzer and you're annihilating this thing 264 00:15:29,340 --> 00:15:30,880 and causing tons of threads to happen, 265 00:15:31,120 --> 00:15:32,600 if you were to have a debugger attached, 266 00:15:32,900 --> 00:15:34,800 you'd be hitting breakpoints every two seconds 267 00:15:34,800 --> 00:15:37,000 and having to continue and really slow down. 268 00:15:37,180 --> 00:15:38,680 With DTrace, you don't have those problems. 269 00:15:38,920 --> 00:15:41,000 You can easily monitor an instrument of fuzzer 270 00:15:41,000 --> 00:15:43,340 without really any performance costs at all. 271 00:15:45,800 --> 00:15:47,760 Okay, so we also have a convenient way 272 00:15:47,760 --> 00:15:49,220 to reference all the CPU context. 273 00:15:49,520 --> 00:15:51,560 So with this global uregs variable 274 00:15:51,560 --> 00:15:52,860 that DTrace provides us, 275 00:15:52,980 --> 00:15:55,980 we can print out values like EIP, EAX, 276 00:15:56,080 --> 00:15:57,220 and all the CPU registers. 277 00:15:57,720 --> 00:16:00,960 That also works on more architectures 278 00:16:00,960 --> 00:16:01,860 than just x86. 279 00:16:02,240 --> 00:16:04,860 So if you saw that the EIP is actually abstracted, 280 00:16:05,340 --> 00:16:07,960 so it works on PowerPC, Spark, x86, 281 00:16:08,300 --> 00:16:09,580 a couple other architectures. 282 00:16:11,020 --> 00:16:12,380 So this is a cute little example 283 00:16:12,380 --> 00:16:14,420 that hooks the uname system call 284 00:16:14,420 --> 00:16:17,000 and then changes what it's going to return. 285 00:16:17,640 --> 00:16:19,180 So this would be kind of a pain in the ass 286 00:16:19,180 --> 00:16:21,160 for you to write some little rootkit-style thing 287 00:16:21,160 --> 00:16:23,140 that's going to hook this function 288 00:16:23,140 --> 00:16:24,760 and modify the return value, 289 00:16:24,920 --> 00:16:26,280 but DTrace lets us do it pretty easily. 290 00:16:26,700 --> 00:16:28,800 So we record the pointer to the buffer 291 00:16:28,800 --> 00:16:30,340 on the uname entry point, 292 00:16:30,580 --> 00:16:32,420 and then on the return point of the function, 293 00:16:32,580 --> 00:16:34,980 we just overwrite that buffer with our own information. 294 00:16:35,420 --> 00:16:37,060 So this shows us that Windows is running 295 00:16:37,060 --> 00:16:39,600 on a PowerPC architecture in 2010. 296 00:16:43,000 --> 00:16:44,300 Here's another small example 297 00:16:44,300 --> 00:16:46,020 that shows file process snooping. 298 00:16:46,400 --> 00:16:49,540 So here we hook the right entry point 299 00:16:49,540 --> 00:16:50,620 and the right return point. 300 00:16:50,860 --> 00:16:52,480 So same idea, record the buffer 301 00:16:52,480 --> 00:16:54,740 and then print it out when the function returns. 302 00:16:55,160 --> 00:16:56,840 So this would allow me to sniff 303 00:16:57,400 --> 00:16:59,260 all of the output from David's terminal 304 00:16:59,260 --> 00:17:01,140 or any other application. 305 00:17:03,080 --> 00:17:04,560 Okay, so hopefully this gives you 306 00:17:04,560 --> 00:17:06,760 the context of what we can use this for. 307 00:17:07,760 --> 00:17:09,380 We're thinking about monitoring 308 00:17:09,600 --> 00:17:11,980 the stack, about doing code coverage metrics. 309 00:17:12,400 --> 00:17:13,680 We could use those metrics 310 00:17:13,680 --> 00:17:15,760 to automate feedback to the fuzzer, 311 00:17:15,860 --> 00:17:18,100 to tell it to adjust inputs in certain ways, 312 00:17:18,700 --> 00:17:20,240 monitor for heap corruptions. 313 00:17:20,320 --> 00:17:21,460 That's a really useful area. 314 00:17:22,860 --> 00:17:24,560 So we're going to go over some of these now. 315 00:17:26,800 --> 00:17:28,760 So I talked to you already a little bit 316 00:17:28,760 --> 00:17:30,200 about the difference between debuggers 317 00:17:30,200 --> 00:17:33,120 and DTrace as a framework for monitoring an application, 318 00:17:33,440 --> 00:17:35,520 but just quickly, it's really important 319 00:17:35,520 --> 00:17:36,980 that you don't think of it as a debugger. 320 00:17:37,120 --> 00:17:38,880 There's things that GDB does 321 00:17:39,600 --> 00:17:41,260 that DTrace will never be able to do and vice versa. 322 00:17:41,800 --> 00:17:44,040 So one thing that DTrace doesn't do well 323 00:17:44,040 --> 00:17:44,980 is exception handling. 324 00:17:45,360 --> 00:17:47,400 So if you've got a trace script 325 00:17:47,400 --> 00:17:48,520 attached to an application 326 00:17:48,520 --> 00:17:50,380 and you trigger a buffer overflow, 327 00:17:50,780 --> 00:17:52,620 DTrace is not going to say memory error, 328 00:17:53,040 --> 00:17:55,160 read or write error at a particular address. 329 00:17:55,400 --> 00:17:56,840 It's just going to stop, because remember, 330 00:17:57,000 --> 00:17:58,520 it's designed for safety and performance. 331 00:17:58,820 --> 00:18:00,400 So as soon as something goes bad, 332 00:18:00,560 --> 00:18:02,380 it just detaches from the process 333 00:18:02,380 --> 00:18:03,540 and drops a probe and says, 334 00:18:03,680 --> 00:18:05,100 I don't want to have anything to do with it. 335 00:18:05,360 --> 00:18:07,920 But that being said, it's infinitely more flexible 336 00:18:09,660 --> 00:18:10,620 for conditional breakpoints 337 00:18:10,620 --> 00:18:12,900 or conditional instrumenting than GDB is. 338 00:18:13,120 --> 00:18:15,320 So what we find is that using the two together 339 00:18:15,320 --> 00:18:18,920 is going to give you the optimum bang for your buck. 340 00:18:19,160 --> 00:18:21,020 So what we'll do is we'll trace to a condition 341 00:18:21,020 --> 00:18:22,220 that we're really interested in. 342 00:18:22,340 --> 00:18:25,740 When we see that a buffer overflow is about to happen, 343 00:18:25,920 --> 00:18:28,700 we'll send the stop signal to the application. 344 00:18:29,040 --> 00:18:32,300 We can attach GDB, let GDB continue it, 345 00:18:32,340 --> 00:18:32,800 and there you go. 346 00:18:32,980 --> 00:18:34,900 So we find that's a really flexible situation 347 00:18:34,900 --> 00:18:36,180 for exploit development 348 00:18:36,180 --> 00:18:39,480 or for looking at a vulnerability close. 349 00:18:41,380 --> 00:18:43,620 The other interesting thing is the comparison 350 00:18:43,620 --> 00:18:45,520 against S-trace or L-trace 351 00:18:45,520 --> 00:18:46,640 or some of the other tracers you know. 352 00:18:46,880 --> 00:18:48,740 The main problem there is I don't know how many of you 353 00:18:48,740 --> 00:18:51,220 attach trust to a really complicated application, 354 00:18:51,500 --> 00:18:51,960 let's say Apache. 355 00:18:52,620 --> 00:18:54,460 I mean, the amount of system calls that are happening 356 00:18:54,460 --> 00:18:56,820 per second or per minute are just devastating. 357 00:18:56,960 --> 00:19:00,100 It's going to drive your CPU up and make it almost unusable. 358 00:19:00,320 --> 00:19:02,000 You don't have that problem with D-trace. 359 00:19:04,120 --> 00:19:05,920 Trust and some of the other tracers work 360 00:19:05,920 --> 00:19:08,520 by actually setting breakpoints when a system call is made 361 00:19:08,520 --> 00:19:10,420 or instrumenting PROC FS, 362 00:19:10,600 --> 00:19:12,980 so PROC FS pauses and allows you to grab the arguments. 363 00:19:13,200 --> 00:19:14,460 You don't have to do any of that. 364 00:19:14,640 --> 00:19:16,680 So Brendan Gregg did a little bit of study. 365 00:19:16,780 --> 00:19:18,280 You can check it out on BrendanGregg.com 366 00:19:18,280 --> 00:19:20,240 where he studied the performance of a trust 367 00:19:20,240 --> 00:19:22,660 implemented in D-trace versus actual trust, 368 00:19:22,840 --> 00:19:25,520 and D-trace is about 68% better performing. 369 00:19:25,800 --> 00:19:28,040 So you hardly notice system call tracing 370 00:19:28,040 --> 00:19:30,740 when you have a binary instrumented. 371 00:19:32,840 --> 00:19:35,940 Again, some of the limitations here are that 372 00:19:35,940 --> 00:19:38,360 D-trace is designed as like a command line 373 00:19:38,520 --> 00:19:39,180 or a real-time tool, 374 00:19:39,560 --> 00:19:42,620 so it really works on only standard in and standard out. 375 00:19:42,840 --> 00:19:45,020 So we had to do a lot of trickery with Ruby 376 00:19:45,020 --> 00:19:47,500 to parse the standard out to get things into arrays 377 00:19:47,500 --> 00:19:50,720 and to sort of retrofit data structures 378 00:19:50,720 --> 00:19:52,240 onto the output of D-trace. 379 00:19:52,460 --> 00:19:54,000 So if you're hoping to use it 380 00:19:54,000 --> 00:19:57,200 and you're just going to get an array of arguments 381 00:19:57,200 --> 00:19:59,960 that were touched by your fuzzer or something like that, 382 00:20:00,040 --> 00:20:01,280 you're not going to get it without our tool. 383 00:20:05,040 --> 00:20:06,580 Here's a couple other cautionaries. 384 00:20:06,740 --> 00:20:08,500 Like we said, it would be useful 385 00:20:08,520 --> 00:20:11,620 and really hard to use D-trace as a serious security tool. 386 00:20:11,840 --> 00:20:13,020 We'll talk a little bit later 387 00:20:13,020 --> 00:20:16,320 as creating a HIDS tailored to a custom application. 388 00:20:16,860 --> 00:20:18,020 The problem here is, remember, 389 00:20:18,160 --> 00:20:19,860 it's designed for performance and security, 390 00:20:20,300 --> 00:20:21,880 so if you start to get a lot of errors, 391 00:20:21,980 --> 00:20:24,180 D-trace will just back off and start to drop probes. 392 00:20:24,380 --> 00:20:26,460 So if you were going to use this as a dependable, 393 00:20:26,540 --> 00:20:29,600 hey, I need to know if anything bad ever happens on the HIDS, 394 00:20:29,700 --> 00:20:31,900 if an attacker was to just spray a bunch of data 395 00:20:31,900 --> 00:20:33,580 at the application and cause it to slow down, 396 00:20:33,940 --> 00:20:36,540 D-trace would just detach and it would go undetected. 397 00:20:36,720 --> 00:20:38,500 So those are some of the options. 398 00:20:38,520 --> 00:20:40,520 Those are some of the things you have to deal with 399 00:20:40,520 --> 00:20:41,340 when using D-trace. 400 00:20:47,660 --> 00:20:50,160 Okay, so let's talk a little bit about 401 00:20:50,160 --> 00:20:53,040 what we did with D-trace, combining it with Ruby 402 00:20:53,040 --> 00:20:56,060 and combining it with some other Ruby-based tools 403 00:20:56,060 --> 00:20:58,280 and a programmatic debugger framework 404 00:20:58,280 --> 00:21:00,800 that we've also written into the combo. 405 00:21:01,360 --> 00:21:04,820 So what we did was we hit certain roadblocks with D-trace 406 00:21:04,820 --> 00:21:06,980 where we just couldn't do certain things 407 00:21:06,980 --> 00:21:08,260 that we really wanted to be able to do. 408 00:21:08,520 --> 00:21:12,060 And we started looking at taking Ruby 409 00:21:12,060 --> 00:21:14,220 and combining these two things 410 00:21:14,220 --> 00:21:16,120 to give us better logic processing 411 00:21:16,120 --> 00:21:18,340 so that we weren't just doing the data recording. 412 00:21:18,920 --> 00:21:20,640 So we came up with retrace, 413 00:21:20,640 --> 00:21:22,220 which is a combination of these two things. 414 00:21:22,480 --> 00:21:26,160 So it utilizes this libDtraceRubyBindings library 415 00:21:26,160 --> 00:21:27,500 written by Chris Andrews 416 00:21:28,000 --> 00:21:30,380 to allow us to access D-trace from Ruby. 417 00:21:30,680 --> 00:21:33,320 So then what we do is write common tasks in Ruby 418 00:21:33,320 --> 00:21:35,320 that build these little D-trace scripts 419 00:21:35,320 --> 00:21:38,040 and send them off and run them and then handle the results. 420 00:21:40,840 --> 00:21:45,260 So another aspect of this is to utilize IDA 421 00:21:45,260 --> 00:21:48,620 and its static disassembly power 422 00:21:49,300 --> 00:21:51,420 with this runtime dynamic tracing. 423 00:21:51,580 --> 00:21:53,160 So now we get both the dynamic world 424 00:21:53,160 --> 00:21:54,420 and the static world together. 425 00:21:55,040 --> 00:21:58,420 There's a plug-in called IDA Rube for Ruby. 426 00:21:58,640 --> 00:22:00,060 And this has two parts. 427 00:22:00,200 --> 00:22:02,360 There's kind of the Ruby client module 428 00:22:02,360 --> 00:22:05,860 and then there's also the IDA server part, 429 00:22:06,040 --> 00:22:07,300 the IDA server plug-in. 430 00:22:07,300 --> 00:22:10,180 So what we do is we run IDA on a Windows box 431 00:22:10,180 --> 00:22:12,840 and a VMware, and then over the network, 432 00:22:12,960 --> 00:22:15,560 we can communicate to it with our Ruby-based framework, 433 00:22:15,920 --> 00:22:18,020 you know, saying things like color this instruction, 434 00:22:18,760 --> 00:22:20,500 annotate the disassembly in this way, 435 00:22:20,660 --> 00:22:23,080 or pull down the disassembly information itself. 436 00:22:23,840 --> 00:22:27,820 So this kind of gets around the lack of IDA GUI support 437 00:22:27,820 --> 00:22:31,200 on certain operating systems and architectures, right? 438 00:22:31,300 --> 00:22:32,580 If you're using Solaris, 439 00:22:32,680 --> 00:22:34,340 unless you have a Windows box next to you, 440 00:22:34,400 --> 00:22:37,280 you're not going to be able to combine static disassembly 441 00:22:37,300 --> 00:22:38,060 with runtime. 442 00:22:38,340 --> 00:22:41,740 By using the network XML RPC between IDA 443 00:22:41,740 --> 00:22:44,640 and Dtrace or Retrace, our framework, 444 00:22:44,840 --> 00:22:46,480 we're really opening up the merging 445 00:22:46,480 --> 00:22:49,380 between statistical analysis, our static analysis, 446 00:22:49,600 --> 00:22:51,240 and runtime analysis, 447 00:22:51,380 --> 00:22:53,320 and it makes a really powerful combo. 448 00:22:53,700 --> 00:22:55,960 The other thing is we skipped over MetaASM, 449 00:22:56,060 --> 00:22:59,000 but MetaASM, which is a Ruby-based assembler, 450 00:22:59,040 --> 00:23:01,460 disassembler, has some incredible power itself 451 00:23:01,460 --> 00:23:02,800 as part of the Metasplate framework. 452 00:23:03,140 --> 00:23:05,960 And you can do things like write PE files from memory. 453 00:23:05,960 --> 00:23:08,620 You can create assembly and disassemblies in Ruby, 454 00:23:08,800 --> 00:23:10,140 write them to ELF files. 455 00:23:10,300 --> 00:23:11,380 So you can do quite a bit of stuff, 456 00:23:11,520 --> 00:23:12,800 and that really comes into play 457 00:23:12,800 --> 00:23:15,300 and becomes really powerful with malware analysis, 458 00:23:15,600 --> 00:23:16,980 automated unpacking. 459 00:23:17,000 --> 00:23:17,980 There's just so many applications. 460 00:23:18,520 --> 00:23:20,960 And that's why we really picked Ruby as a framework, 461 00:23:21,100 --> 00:23:23,480 because we feel like the integration of all these tool sets 462 00:23:23,480 --> 00:23:26,320 is really the future for automating a lot of the tasks 463 00:23:26,320 --> 00:23:28,360 that used to be really manually intensive 464 00:23:28,360 --> 00:23:29,660 and hard to get done. 465 00:23:32,660 --> 00:23:35,240 So what we found here is that by using this framework, 466 00:23:35,240 --> 00:23:38,100 both the tracing and also the static analysis, 467 00:23:38,240 --> 00:23:41,040 it's allowed us to decrease the amount of time it takes 468 00:23:41,040 --> 00:23:44,900 to do the analysis to write successful exploits. 469 00:23:47,060 --> 00:23:51,560 We also had limitations with just doing tracing. 470 00:23:51,680 --> 00:23:53,660 We wanted to do things that were more like debugging. 471 00:23:54,200 --> 00:23:57,520 So we went ahead and wrote C Ruby bindings 472 00:23:57,520 --> 00:24:01,220 to wrap OSX's mock API, the debugging API, 473 00:24:01,580 --> 00:24:03,660 and the ability to utilize that from Ruby. 474 00:24:03,660 --> 00:24:06,860 So now we can do things like set traditional breakpoints, 475 00:24:07,100 --> 00:24:08,040 search through memory, 476 00:24:08,320 --> 00:24:12,400 change the memory permissions on different memory segments. 477 00:24:12,780 --> 00:24:14,740 We can search for specific opcodes 478 00:24:15,220 --> 00:24:17,280 and segments we know are executable. 479 00:24:17,880 --> 00:24:20,580 So this really provides us the third element. 480 00:24:20,920 --> 00:24:23,160 So we've got the tracing part with retrace, 481 00:24:23,180 --> 00:24:26,060 the disassembly with tools like IDA and MetaASM, 482 00:24:26,180 --> 00:24:27,680 and then this debugger ability. 483 00:24:30,000 --> 00:24:31,560 So the way the... 484 00:24:31,560 --> 00:24:33,200 I guess I sort of covered this already, but... 485 00:24:33,660 --> 00:24:35,820 The debugger really provides us a wrapper 486 00:24:35,820 --> 00:24:38,160 around the native debugging API, 487 00:24:38,980 --> 00:24:40,560 which allows us to do this stuff 488 00:24:40,560 --> 00:24:43,280 and, like, walk memory segments and catch exceptions 489 00:24:43,280 --> 00:24:44,940 and do symbol resolution and all that. 490 00:24:45,140 --> 00:24:47,320 A lot of people sort of have the idea 491 00:24:47,320 --> 00:24:53,020 that Mac OSX is really just BSD, 492 00:24:53,160 --> 00:24:54,040 but that's not the case. 493 00:24:54,120 --> 00:24:55,940 You don't have the standard Ptrace API 494 00:24:55,940 --> 00:24:58,640 like you would on every other POSIX system. 495 00:24:58,840 --> 00:25:00,360 It's really mock at the core. 496 00:25:00,560 --> 00:25:02,340 Even though there is a BSD subsystem, 497 00:25:02,540 --> 00:25:03,640 most of the intent of the process 498 00:25:03,640 --> 00:25:05,160 and the intensive sort of system internals 499 00:25:05,160 --> 00:25:06,760 and system programming you're going to be doing 500 00:25:06,760 --> 00:25:07,240 isn't mock. 501 00:25:07,540 --> 00:25:09,600 So we had to have the fun job of learning 502 00:25:10,240 --> 00:25:12,380 how to send mock messages back and forth 503 00:25:12,380 --> 00:25:13,380 to the kernel and stuff. 504 00:25:13,660 --> 00:25:15,340 But we've given that to you here, 505 00:25:15,460 --> 00:25:17,220 so you really have a flexible marriage 506 00:25:17,220 --> 00:25:18,700 between the best parts of debugging 507 00:25:18,700 --> 00:25:21,940 and Ruby and the best parts of retrace and detrace. 508 00:25:24,460 --> 00:25:26,400 Okay, so here's how it all comes together. 509 00:25:26,860 --> 00:25:30,280 There was this iPhoto format string vulnerability. 510 00:25:30,480 --> 00:25:32,620 We ran into Rob Carter and Nate McPheeters 511 00:25:32,620 --> 00:25:34,300 back in D.C., 512 00:25:34,300 --> 00:25:36,480 and they were showing us this thing 513 00:25:36,480 --> 00:25:38,520 and curious on how we could use detrace 514 00:25:38,520 --> 00:25:41,480 to really pull out the information we need 515 00:25:41,480 --> 00:25:43,940 from the application at runtime 516 00:25:43,940 --> 00:25:45,620 in order to understand how to write the exploit. 517 00:25:46,120 --> 00:25:47,900 So we threw all these tools together 518 00:25:47,900 --> 00:25:49,860 and were able to trace through 519 00:25:50,340 --> 00:25:51,600 all of the different print apps 520 00:25:51,600 --> 00:25:52,600 that happen in the application 521 00:25:52,600 --> 00:25:54,220 until like the 2001, 522 00:25:54,660 --> 00:25:55,860 which we were actually interested in, 523 00:25:56,340 --> 00:25:58,060 trace until we hit that spot 524 00:25:58,540 --> 00:26:00,000 and then do the application analysis, 525 00:26:00,240 --> 00:26:02,160 whereas just setting a breakpoint 526 00:26:02,160 --> 00:26:02,600 would have took forever. 527 00:26:03,180 --> 00:26:04,680 Iterating through each step. 528 00:26:05,900 --> 00:26:08,640 So we used the predicate feature 529 00:26:08,640 --> 00:26:11,340 to say, you know, only stop the application 530 00:26:11,340 --> 00:26:14,220 when one of the variables has this %25 in it. 531 00:26:15,020 --> 00:26:18,040 So the way we envisioned this with the framework 532 00:26:18,040 --> 00:26:19,200 is sort of like this. 533 00:26:19,420 --> 00:26:22,360 At the top here is the code 534 00:26:22,360 --> 00:26:24,260 for specifying the detrace 535 00:26:24,260 --> 00:26:27,460 within the detrace Ruby lib 536 00:26:27,460 --> 00:26:30,640 to say, you know, hook this vf.f function, 537 00:26:30,820 --> 00:26:32,360 and if the argument is 538 00:26:32,360 --> 00:26:35,000 this %25 in, 539 00:26:35,240 --> 00:26:36,880 then stop the application. 540 00:26:37,620 --> 00:26:39,700 So we go ahead and kick that off, 541 00:26:40,220 --> 00:26:42,420 and then we connect to our Ida Ruby server, 542 00:26:43,640 --> 00:26:46,000 and once it's stopped, 543 00:26:46,060 --> 00:26:47,620 we pull down some information about the function. 544 00:26:47,820 --> 00:26:49,000 Like, we want to see the disassembly 545 00:26:49,000 --> 00:26:49,660 of the function itself 546 00:26:50,380 --> 00:26:52,100 so that we can find the return instruction 547 00:26:52,100 --> 00:26:53,640 and set a breakpoint there. 548 00:26:54,080 --> 00:26:55,620 Then we continue the application, 549 00:26:57,020 --> 00:26:58,880 and we have a callback function 550 00:26:58,880 --> 00:26:59,720 for when our breakpoint hit, 551 00:26:59,780 --> 00:27:01,080 and then we continue the application. 552 00:27:01,080 --> 00:27:03,300 So then at the end of the function, 553 00:27:03,420 --> 00:27:04,120 we hit the breakpoint, 554 00:27:04,340 --> 00:27:06,400 and we want to inspect memory at that point 555 00:27:06,400 --> 00:27:09,820 to say, okay, is the overflow aligned properly? 556 00:27:10,680 --> 00:27:13,200 So the cool part about this is 557 00:27:13,200 --> 00:27:16,700 this is not quite automated exploit development, 558 00:27:16,940 --> 00:27:18,740 but it's about as close as you're going to get, right? 559 00:27:18,840 --> 00:27:21,620 So he kind of shaved over the fact 560 00:27:21,620 --> 00:27:24,300 that you had to sit there and hit continue 561 00:27:24,300 --> 00:27:26,840 1,000 times on every printf 562 00:27:26,840 --> 00:27:28,780 that could possibly happen in iPhoto. 563 00:27:28,940 --> 00:27:31,060 Well, imagine doing that 564 00:27:31,080 --> 00:27:32,900 during the exploit development process. 565 00:27:33,180 --> 00:27:34,680 So literally, this script could save you 566 00:27:34,680 --> 00:27:36,740 several dozen hours of a lifetime 567 00:27:36,740 --> 00:27:38,000 of an exploit developing it. 568 00:27:38,320 --> 00:27:41,000 And in fact, when Nate and Rob brought this to us, 569 00:27:41,080 --> 00:27:41,720 they were like, you know, 570 00:27:41,800 --> 00:27:43,480 we can't even stand GDB anymore. 571 00:27:43,700 --> 00:27:44,520 This is killing us. 572 00:27:44,780 --> 00:27:46,640 And so we actually came up with this script 573 00:27:46,640 --> 00:27:48,420 all sitting together at a bar one night, 574 00:27:48,560 --> 00:27:50,460 and it's actually worked out pretty well. 575 00:27:50,620 --> 00:27:52,700 So when you're done with this script, 576 00:27:52,820 --> 00:27:54,980 you know exactly what instruction you need to overwrite, 577 00:27:55,100 --> 00:27:58,080 and you can then use our opcode searching feature 578 00:27:58,080 --> 00:28:00,760 to just find instruction in executable memory 579 00:28:00,760 --> 00:28:02,040 that's going to point you right back there. 580 00:28:02,280 --> 00:28:04,100 So, I mean, two steps beyond this, 581 00:28:04,160 --> 00:28:05,460 and you have arbitrary code execution. 582 00:28:08,020 --> 00:28:09,860 All right, so here's another interesting idea, right? 583 00:28:10,000 --> 00:28:13,360 We know there's something funky with ASLR on OSX, 584 00:28:13,480 --> 00:28:16,660 and we may be interested in trying to analyze that. 585 00:28:16,940 --> 00:28:18,480 So one thing you might want to do is, 586 00:28:18,560 --> 00:28:20,380 you know, you found a bug in an application, 587 00:28:20,620 --> 00:28:22,380 and you're trying to figure out how the memory works 588 00:28:22,380 --> 00:28:24,780 so that you can use specific values 589 00:28:24,780 --> 00:28:26,320 in a return-to-libc-style attack. 590 00:28:27,080 --> 00:28:29,300 So what you can do is start up the application, 591 00:28:29,300 --> 00:28:31,060 and then do some simple resolution 592 00:28:31,060 --> 00:28:33,480 to figure out what interesting functions you want to use 593 00:28:33,480 --> 00:28:34,620 are at what locations. 594 00:28:35,340 --> 00:28:37,460 But, you know, these addresses will be randomized, 595 00:28:37,540 --> 00:28:39,180 or they should be, after each run. 596 00:28:39,340 --> 00:28:42,680 So you search memory for some sort of function pointer 597 00:28:42,680 --> 00:28:43,760 to one of those addresses, 598 00:28:43,980 --> 00:28:45,520 and then you restart the application 599 00:28:45,520 --> 00:28:46,580 after it's been randomized, 600 00:28:46,720 --> 00:28:49,620 and you search again for references to those same functions. 601 00:28:49,920 --> 00:28:52,900 So you try and find some spot in virtual memory 602 00:28:52,900 --> 00:28:55,660 that always contains a pointer to that randomized address. 603 00:28:55,980 --> 00:28:58,300 So this is the sort of task that you could also automate 604 00:28:58,300 --> 00:28:59,540 with this framework. 605 00:29:01,460 --> 00:29:03,640 So our framework's up on PopHopRed.org. 606 00:29:03,740 --> 00:29:04,740 We're going to stick it up there tonight. 607 00:29:05,500 --> 00:29:06,640 Retrace is already up there. 608 00:29:06,820 --> 00:29:08,100 We just need to add a red bug. 609 00:29:10,640 --> 00:29:12,680 Okay, so let's look at monitoring the stack. 610 00:29:13,460 --> 00:29:15,480 So what we're going to do here is, 611 00:29:15,640 --> 00:29:19,020 assume we have a classical stack buffer overflow. 612 00:29:19,540 --> 00:29:20,780 We're going to trace our application 613 00:29:21,340 --> 00:29:22,800 until that instant, 614 00:29:22,980 --> 00:29:25,140 and then print out all the information that we care about. 615 00:29:25,460 --> 00:29:27,420 So we want to do this to pinpoint exactly 616 00:29:27,420 --> 00:29:28,820 where the vulnerable function is. 617 00:29:29,040 --> 00:29:30,140 Yeah, this is a situation 618 00:29:30,140 --> 00:29:31,740 where maybe you're fuzzing an application, 619 00:29:32,000 --> 00:29:33,260 and you get an exception 620 00:29:33,260 --> 00:29:34,780 from whatever debugger you have attached. 621 00:29:34,980 --> 00:29:37,480 You look at the stack, and it's filled with As, right? 622 00:29:37,600 --> 00:29:39,520 But who overflew, where was that, 623 00:29:39,720 --> 00:29:41,120 and how can you leverage that 624 00:29:41,120 --> 00:29:43,460 to either understand the vulnerability itself 625 00:29:43,460 --> 00:29:44,360 so you can protect yourself 626 00:29:44,360 --> 00:29:46,480 and understand how important the patch is, 627 00:29:46,580 --> 00:29:48,700 or you want to leverage it for arbitrary code execution. 628 00:29:49,140 --> 00:29:51,480 And it's definitely not a one, two, three step 629 00:29:51,480 --> 00:29:52,500 from getting a stack overflow 630 00:29:52,500 --> 00:29:55,440 to understanding exactly the nature of that bug itself 631 00:29:55,440 --> 00:29:56,320 and whether it's new 632 00:29:56,320 --> 00:29:57,480 and what the problems with it are. 633 00:29:59,160 --> 00:30:01,060 All right, so here it is in one probe. 634 00:30:02,880 --> 00:30:04,320 We're going to hook every function 635 00:30:04,840 --> 00:30:06,280 in every return address, right? 636 00:30:06,880 --> 00:30:08,520 We'll hook every function in every module. 637 00:30:09,040 --> 00:30:11,660 If our EIP or the next instruction pointer 638 00:30:11,660 --> 00:30:14,460 is 4114 on the encoding of all a string of As, 639 00:30:14,980 --> 00:30:16,340 then we're going to stop the application, 640 00:30:16,600 --> 00:30:18,360 saying, okay, something bad happened here. 641 00:30:18,480 --> 00:30:19,120 Let's check it out. 642 00:30:20,020 --> 00:30:22,040 So, you know, this works in the generic sense 643 00:30:22,040 --> 00:30:23,940 where you're overflowing with large strings of As, 644 00:30:24,020 --> 00:30:25,760 but, I mean, that's not always the case, right? 645 00:30:26,320 --> 00:30:27,960 We want some more generic solution. 646 00:30:29,380 --> 00:30:33,240 So our approach here is to, at function entry, 647 00:30:33,340 --> 00:30:36,020 record what the return address is on the stack, 648 00:30:36,240 --> 00:30:39,020 and on function return, check what EIP is. 649 00:30:39,180 --> 00:30:41,160 Is our next instruction equal to that return? 650 00:30:41,420 --> 00:30:42,240 It should be. 651 00:30:42,640 --> 00:30:44,820 If it's not, we know something went wrong, right? 652 00:30:44,940 --> 00:30:45,820 There was some sort of overflow 653 00:30:45,820 --> 00:30:47,100 that corrupted the return address. 654 00:30:48,520 --> 00:30:49,680 We're going to give you a little tip 655 00:30:49,680 --> 00:30:51,940 that might save you several hours that it cost us. 656 00:30:52,220 --> 00:30:54,060 You see that second slide point there? 657 00:30:54,060 --> 00:30:58,200 Uregs RESP and not Uregs RESP? 658 00:30:58,780 --> 00:30:59,880 That is a big one. 659 00:31:00,020 --> 00:31:01,640 On the Core 2 dual architecture, 660 00:31:01,940 --> 00:31:03,120 which is a 64-bit architecture, 661 00:31:03,500 --> 00:31:05,580 they don't call it ESP, they call it SP. 662 00:31:05,800 --> 00:31:07,080 And if you confuse those two, 663 00:31:07,140 --> 00:31:08,740 it's going to cost you several hours of problems. 664 00:31:10,180 --> 00:31:13,700 Yeah, so RSP is the architecture independent reference to that. 665 00:31:14,320 --> 00:31:16,580 So we had some problems with tail call optimizations 666 00:31:16,580 --> 00:31:18,140 that happened at compile time, 667 00:31:18,340 --> 00:31:20,780 and also some functions that DTrace can't trace. 668 00:31:21,060 --> 00:31:22,680 So this kind of threw a wrench in the problem. 669 00:31:24,860 --> 00:31:27,920 So I won't bore you with the details of tail calls, 670 00:31:28,140 --> 00:31:29,920 but the basic idea is that some functions 671 00:31:29,920 --> 00:31:31,660 don't require their own stack frame. 672 00:31:31,860 --> 00:31:34,600 So although you're calling a new function, 673 00:31:34,700 --> 00:31:36,540 there's no new stack frame on the stack, 674 00:31:36,700 --> 00:31:37,680 so then it kind of messes up, 675 00:31:37,760 --> 00:31:39,520 well, how do you think about the return pointers and all that stuff? 676 00:31:39,900 --> 00:31:41,940 So DTrace has a particular way of dealing with that, 677 00:31:42,000 --> 00:31:43,860 and you just have to know what it chooses to do. 678 00:31:44,320 --> 00:31:46,060 If you're curious, I'll tell you all about it. 679 00:31:46,760 --> 00:31:50,080 But the basic idea is that instead of comparing the IP, 680 00:31:50,380 --> 00:31:52,020 when you enter a function, 681 00:31:52,080 --> 00:31:53,680 we record the return address on the stack, 682 00:31:54,060 --> 00:31:55,760 when we exit the function, 683 00:31:55,980 --> 00:31:58,040 then we look at that same spot in memory, 684 00:31:58,260 --> 00:31:59,100 see if it's the same. 685 00:32:00,500 --> 00:32:04,180 So we have this problem with functions we can't trace. 686 00:32:04,440 --> 00:32:06,300 Our solution is just to ignore them, 687 00:32:06,360 --> 00:32:07,660 because they're usually not the functions 688 00:32:07,660 --> 00:32:08,940 that we're interested in anyway. 689 00:32:09,620 --> 00:32:11,480 So it turns out DTrace has some problems 690 00:32:11,480 --> 00:32:14,960 with tracing functions like when they're inside jump tables, 691 00:32:15,060 --> 00:32:17,960 and so it chooses to just ignore them. 692 00:32:18,120 --> 00:32:18,960 It'll trace the entry point, 693 00:32:19,100 --> 00:32:20,960 but then it doesn't know how to do the return point. 694 00:32:23,620 --> 00:32:27,640 So our basic way to determine which functions can't be traced 695 00:32:27,640 --> 00:32:28,420 in this way is to say, 696 00:32:28,540 --> 00:32:30,840 show me the entry points for all the functions 697 00:32:31,200 --> 00:32:32,020 that you know about, 698 00:32:32,140 --> 00:32:33,180 and show me the return points. 699 00:32:33,480 --> 00:32:35,060 And then we look for mismatches, right? 700 00:32:35,180 --> 00:32:37,240 Because some will be in one list and not the other. 701 00:32:37,560 --> 00:32:39,780 We just exclude those functions with predicates. 702 00:32:41,600 --> 00:32:43,160 So let's take a look at how this works. 703 00:32:46,760 --> 00:32:47,620 Double click it? 704 00:32:59,460 --> 00:32:59,780 Yeah. 705 00:32:59,780 --> 00:33:01,120 Oh, it plays up here. 706 00:33:01,200 --> 00:33:02,940 Okay, so what we're going to do is run 707 00:33:03,520 --> 00:33:05,180 an RTSP QuickTime exploit 708 00:33:05,180 --> 00:33:07,260 like we've seen a lot of these this year, right? 709 00:33:07,880 --> 00:33:09,380 So it's listening on the local host, 710 00:33:09,580 --> 00:33:11,160 and then we're going to start up QuickTime 711 00:33:11,160 --> 00:33:13,260 and how to connect to this malicious server, 712 00:33:13,580 --> 00:33:15,540 and then Stack Overflow is going to be delivered. 713 00:33:16,480 --> 00:33:17,600 But what we're going to do 714 00:33:17,620 --> 00:33:19,500 there in between is attach Retrace 715 00:33:19,500 --> 00:33:21,540 to this application so that we can watch 716 00:33:21,540 --> 00:33:22,640 the overflow as it happens. 717 00:33:23,520 --> 00:33:25,560 So we load up QuickTime here. 718 00:33:27,540 --> 00:33:29,200 And we're going to attach Retrace. 719 00:33:29,520 --> 00:33:32,120 So we start up the Ruby environment. 720 00:33:34,540 --> 00:33:36,840 Now this is actually like your standard IRB, right? 721 00:33:36,960 --> 00:33:38,080 So you can go ahead and specify 722 00:33:38,520 --> 00:33:40,180 whatever kind of Ruby codes you want. 723 00:33:43,160 --> 00:33:44,920 So there's all our commands, right? 724 00:33:45,000 --> 00:33:47,380 Like searching memory and attaching a debugger 725 00:33:47,620 --> 00:33:47,920 and stuff like that. 726 00:33:48,360 --> 00:33:49,340 So what we're going to do is run, 727 00:33:49,480 --> 00:33:50,880 we're going to specify the PID 728 00:33:51,420 --> 00:33:55,120 and then run the Stack Overflow tracer on QuickTime. 729 00:33:59,140 --> 00:34:01,720 Now we go ahead and connect to the malicious server, 730 00:34:02,520 --> 00:34:03,820 which is going to deliver the payload. 731 00:34:04,200 --> 00:34:05,960 So a lot of things happen all at once there. 732 00:34:06,660 --> 00:34:07,600 The applicator, you know, 733 00:34:07,620 --> 00:34:09,040 it opened up a window to read that. 734 00:34:09,280 --> 00:34:11,240 The payload was delivered down here, 735 00:34:12,240 --> 00:34:14,420 and then Retrace stopped the application. 736 00:34:14,760 --> 00:34:15,820 So it's actually halted. 737 00:34:16,440 --> 00:34:17,480 So we get all this information 738 00:34:17,620 --> 00:34:19,600 printed out about the context 739 00:34:19,600 --> 00:34:21,260 of the application's current state. 740 00:34:21,880 --> 00:34:23,380 You know, we get a basic message 741 00:34:23,380 --> 00:34:24,800 about a stack overflow occurring. 742 00:34:25,420 --> 00:34:27,240 We see the exact module 743 00:34:27,240 --> 00:34:28,640 where the overflow occurred, 744 00:34:29,200 --> 00:34:30,540 the particular function, 745 00:34:31,740 --> 00:34:33,860 what we expected the return address to be, 746 00:34:34,020 --> 00:34:35,520 and what it actually was. 747 00:34:36,200 --> 00:34:37,820 So in this case, it's, you know, 748 00:34:37,920 --> 00:34:39,160 0x17 blah, blah, blah, 749 00:34:39,260 --> 00:34:41,040 but it was actually dead B. 750 00:34:41,600 --> 00:34:42,820 Probably a problem. 751 00:34:44,820 --> 00:34:46,920 So here's all the CPU registers. 752 00:34:47,120 --> 00:34:47,600 You can see, you know, 753 00:34:47,620 --> 00:34:48,320 you can see in this situation 754 00:34:48,320 --> 00:34:50,400 a number of them were overflown. 755 00:34:54,300 --> 00:34:55,440 ESI, the base pointer, 756 00:34:55,620 --> 00:34:56,080 and so on. 757 00:34:56,560 --> 00:34:57,640 So here's our stack trace. 758 00:34:59,480 --> 00:35:00,440 You can see, again, 759 00:35:00,600 --> 00:35:01,880 module name, function name, 760 00:35:02,040 --> 00:35:03,720 followed by the exact instruction offset 761 00:35:03,720 --> 00:35:04,540 within that function. 762 00:35:05,880 --> 00:35:07,240 So from here, we might be interested 763 00:35:07,240 --> 00:35:08,800 in doing something like, you know, 764 00:35:08,800 --> 00:35:11,060 attaching a debugger and examining the memory space, 765 00:35:11,700 --> 00:35:14,160 searching for instances 766 00:35:14,160 --> 00:35:16,120 of particular strings we want to use 767 00:35:19,200 --> 00:35:21,160 So what I'll do is I'll show how that happens 768 00:35:21,160 --> 00:35:24,540 with a different application here. 769 00:35:24,620 --> 00:35:25,360 Let's use ADM. 770 00:35:29,460 --> 00:35:31,020 So ADM's a chat client, right? 771 00:35:31,180 --> 00:35:32,280 I just picked it randomly. 772 00:35:36,940 --> 00:35:38,380 So imagine that we're going to do 773 00:35:38,380 --> 00:35:40,400 a simple return to libc, 774 00:35:40,560 --> 00:35:42,420 and we need to use bin bash 775 00:35:42,420 --> 00:35:44,980 as one of the parameters to some system call function, right? 776 00:35:45,400 --> 00:35:46,620 We need to know a location 777 00:35:47,620 --> 00:35:48,760 of the memory at this moment 778 00:35:48,760 --> 00:35:50,840 where bin bash is located. 779 00:35:51,420 --> 00:35:53,540 So what we're going to do is dump all of the memory, 780 00:35:53,780 --> 00:35:55,140 all the virtual memory segments, 781 00:35:55,340 --> 00:35:56,700 and then search through them. 782 00:35:57,380 --> 00:35:59,820 So in retrace, this works by taking each segment 783 00:35:59,820 --> 00:36:00,740 and dumping it to disk. 784 00:36:01,220 --> 00:36:03,140 With Redbug, we've actually implemented it 785 00:36:03,140 --> 00:36:04,520 so you don't need to dump it to disk. 786 00:36:05,960 --> 00:36:08,200 So here we search for bin bash, 787 00:36:08,480 --> 00:36:09,440 the string bin bash. 788 00:36:10,180 --> 00:36:11,500 Looks through all the memory segments 789 00:36:11,500 --> 00:36:13,000 and we get about five, six locations. 790 00:36:14,220 --> 00:36:15,980 So let's just attach a debugger 791 00:36:15,980 --> 00:36:17,460 and then check what the value 792 00:36:17,620 --> 00:36:18,580 at each of those locations is 793 00:36:18,580 --> 00:36:21,100 just to make sure that it's usable for our situation. 794 00:36:27,060 --> 00:36:29,340 So in retrace, what attach debugger does 795 00:36:29,340 --> 00:36:32,220 is just, it stops retrace 796 00:36:32,220 --> 00:36:34,200 and then attaches gdb to the process. 797 00:36:35,320 --> 00:36:37,360 So here we can see it's really in the same state. 798 00:36:40,480 --> 00:36:41,720 Now let's inspect memory 799 00:36:41,720 --> 00:36:42,740 at the first location. 800 00:36:44,580 --> 00:36:45,600 We can see that 801 00:36:45,600 --> 00:36:47,600 this is a string that starts with bin bash 802 00:36:47,620 --> 00:36:49,040 but it's got all this other junk 803 00:36:49,040 --> 00:36:49,720 on the end of it, right? 804 00:36:49,880 --> 00:36:51,360 This is not going to be useful to us. 805 00:36:53,080 --> 00:36:55,220 So we can expect the next memory location. 806 00:36:55,800 --> 00:36:57,860 It turns out to be bin bash and all terminated. 807 00:36:58,060 --> 00:36:59,200 So this is what we'd want to use, right? 808 00:36:59,620 --> 00:37:01,540 And as well, the other five locations 809 00:37:01,540 --> 00:37:02,460 are all bin bash. 810 00:37:02,660 --> 00:37:04,200 So those would be potentially used. 811 00:37:04,460 --> 00:37:06,080 So the idea here is to get around 812 00:37:06,080 --> 00:37:07,280 data execution prevention, 813 00:37:07,740 --> 00:37:10,260 you can manipulate to make a system call. 814 00:37:10,440 --> 00:37:11,880 In this case, if you make the system call 815 00:37:11,880 --> 00:37:13,800 slash bin bash, you're going to get a shell. 816 00:37:13,800 --> 00:37:14,680 That's a bash shell. 817 00:37:14,900 --> 00:37:17,060 So that's why it's so interesting on there 818 00:37:17,060 --> 00:37:19,400 because we can't just run code off the stack anymore. 819 00:37:19,680 --> 00:37:21,720 We'll have to return into mprotect 820 00:37:21,720 --> 00:37:23,080 or bin bash, something like that 821 00:37:23,080 --> 00:37:25,800 to actually leverage getting a shell on the box. 822 00:37:27,940 --> 00:37:29,800 Okay, so some other things we can do 823 00:37:29,800 --> 00:37:32,140 with retrace, you know, instruction level tracing, 824 00:37:32,400 --> 00:37:33,820 well, that will give us the ability 825 00:37:33,820 --> 00:37:34,920 to do code coverage. 826 00:37:35,200 --> 00:37:36,360 We're very interested in that. 827 00:37:38,520 --> 00:37:40,100 So let's briefly talk about it. 828 00:37:40,420 --> 00:37:41,920 The idea is pretty simple here. 829 00:37:42,180 --> 00:37:43,420 Detrace gives us the ability 830 00:37:43,800 --> 00:37:46,340 to print out the address of the instruction 831 00:37:46,340 --> 00:37:48,060 at each point. 832 00:37:48,400 --> 00:37:50,080 So we just print them all out 833 00:37:50,080 --> 00:37:51,460 and then map them into IDA 834 00:37:51,460 --> 00:37:52,580 and we can visually represent 835 00:37:52,580 --> 00:37:54,300 what code coverage looks like 836 00:37:54,300 --> 00:37:55,800 during a particular run of an application. 837 00:37:57,880 --> 00:37:59,520 So that's the whole approach. 838 00:37:59,640 --> 00:38:00,380 It's really pretty simple. 839 00:38:00,680 --> 00:38:02,120 There's things we have to deal with 840 00:38:02,120 --> 00:38:05,080 when we're tracing instructions 841 00:38:05,080 --> 00:38:07,700 that are in a library that's mapped 842 00:38:07,700 --> 00:38:09,100 like deep in virtual memory space 843 00:38:09,640 --> 00:38:11,540 and our disassembler, 844 00:38:11,600 --> 00:38:13,000 it's mapped at location 845 00:38:13,800 --> 00:38:14,160 0x1000. 846 00:38:14,640 --> 00:38:16,260 So we just have to deal with, you know, 847 00:38:16,300 --> 00:38:17,760 mapping those offsets. 848 00:38:18,100 --> 00:38:19,500 So it's not really a problem. 849 00:38:19,600 --> 00:38:22,240 You can just either trick your disassembler 850 00:38:22,240 --> 00:38:24,080 into specifying exactly where 851 00:38:24,080 --> 00:38:25,780 the starting point for the address is 852 00:38:25,780 --> 00:38:28,180 or you can do the offsets in Ruby itself. 853 00:38:30,800 --> 00:38:32,160 You do have some issues 854 00:38:32,160 --> 00:38:33,540 with performance, right? 855 00:38:33,620 --> 00:38:35,160 Because tracing every instruction 856 00:38:35,160 --> 00:38:37,440 can be kind of a heavy load 857 00:38:37,440 --> 00:38:38,360 on the system. 858 00:38:38,500 --> 00:38:40,560 So you just scope to only trace 859 00:38:40,560 --> 00:38:42,080 particular libraries or applications 860 00:38:43,800 --> 00:38:44,400 that you're running. 861 00:38:44,480 --> 00:38:46,880 So what this turns out to look like, 862 00:38:46,960 --> 00:38:48,620 the real fruit of it is this. 863 00:38:48,780 --> 00:38:50,840 You get a function call graph 864 00:38:50,840 --> 00:38:52,940 and the particular blocks that were traced 865 00:38:52,940 --> 00:38:54,240 or run during the application 866 00:38:54,240 --> 00:38:54,900 are highlighted. 867 00:38:55,260 --> 00:38:56,660 So you can say, okay, cool, 868 00:38:56,760 --> 00:38:58,460 I missed this whole section of code. 869 00:38:58,660 --> 00:39:00,080 Let me figure out how to tweak that jump 870 00:39:00,700 --> 00:39:02,700 to access and test the rest of it. 871 00:39:03,000 --> 00:39:04,840 Yeah, so maybe per fuzzer run 872 00:39:04,840 --> 00:39:05,880 you would change the color 873 00:39:05,880 --> 00:39:07,620 and then you end up with a really cool rainbow 874 00:39:07,620 --> 00:39:08,380 or a mandala. 875 00:39:13,800 --> 00:39:14,400 So you know that you're hitting code 876 00:39:14,400 --> 00:39:15,140 that you weren't before. 877 00:39:17,220 --> 00:39:18,700 You can also do stuff like this 878 00:39:18,700 --> 00:39:19,740 with simple dot graphs. 879 00:39:20,800 --> 00:39:23,180 Show what the system call graph 880 00:39:23,180 --> 00:39:23,940 looks like at runtime. 881 00:39:24,340 --> 00:39:26,840 So you may know that something was patched here 882 00:39:26,840 --> 00:39:29,220 and there was a vulnerability here 883 00:39:29,220 --> 00:39:31,380 and the patch was applied to this function 884 00:39:31,380 --> 00:39:33,520 so they're doing some sort of input sanitization here. 885 00:39:33,780 --> 00:39:35,560 But there's another code path to get there. 886 00:39:35,800 --> 00:39:37,560 So let's look at how we can 887 00:39:37,560 --> 00:39:39,620 get down here and access that 888 00:39:39,620 --> 00:39:40,760 vulnerable function again without 889 00:39:40,760 --> 00:39:42,720 going through the patched function. 890 00:39:45,520 --> 00:39:47,440 All right, so I'm going to tell you 891 00:39:47,440 --> 00:39:48,620 about how to use DTrace 892 00:39:48,620 --> 00:39:50,060 to write heap overflows. 893 00:39:50,380 --> 00:39:52,280 So how many of you are familiar out there 894 00:39:52,280 --> 00:39:53,420 with the term heap spraying 895 00:39:53,420 --> 00:39:54,420 and that type of stuff? 896 00:39:54,520 --> 00:39:55,660 Show of hands, okay. 897 00:39:56,020 --> 00:39:59,220 So heap spraying is all about heap determinism, 898 00:39:59,240 --> 00:40:00,700 controlling the determinism 899 00:40:00,700 --> 00:40:02,040 and the allocation patterns on the heap. 900 00:40:02,200 --> 00:40:03,600 So you can set this heap up 901 00:40:03,600 --> 00:40:06,800 so your overflow gets arbitrary code execution. 902 00:40:07,220 --> 00:40:08,740 If you want to know more about it, 903 00:40:08,800 --> 00:40:10,300 the immunity guys who have a booth here 904 00:40:10,300 --> 00:40:13,000 wrote a great paper on it called Debugging with IDE 905 00:40:13,800 --> 00:40:14,340 which is the only thing you want to know 906 00:40:14,340 --> 00:40:16,280 about advanced heap exploits. 907 00:40:16,380 --> 00:40:18,300 But the idea here is that in the past 908 00:40:18,300 --> 00:40:19,900 there was a primitive called the write4 909 00:40:19,900 --> 00:40:22,400 which essentially gave you a generic technique 910 00:40:22,400 --> 00:40:23,340 for exploiting a heap. 911 00:40:23,520 --> 00:40:25,060 Well, that's gone. There's heap protections 912 00:40:25,060 --> 00:40:27,000 and it never existed on BSD 913 00:40:27,000 --> 00:40:29,320 because they keep their metadata at a ban. 914 00:40:29,580 --> 00:40:31,340 That is, there's no function pointers 915 00:40:31,340 --> 00:40:33,060 that you can just overflow into 916 00:40:33,060 --> 00:40:34,780 to leverage arbitrary code execution. 917 00:40:35,320 --> 00:40:37,100 So what that means in plain English 918 00:40:37,100 --> 00:40:39,120 is that you have to understand a lot 919 00:40:39,120 --> 00:40:41,020 about how the application allocates things 920 00:40:41,020 --> 00:40:42,560 on the heap in order to 921 00:40:43,020 --> 00:40:44,420 exploit something successfully. 922 00:40:44,880 --> 00:40:46,480 So you're looking for function pointers, 923 00:40:46,780 --> 00:40:49,060 you're looking for any kind of 924 00:40:49,060 --> 00:40:50,540 application-specific data 925 00:40:50,540 --> 00:40:52,720 that you can overwrite with a heap overflow 926 00:40:52,720 --> 00:40:54,920 that's going to cause you to own the application. 927 00:40:55,680 --> 00:40:57,380 So on other platforms 928 00:40:57,380 --> 00:40:58,680 there's lots of tools to do this. 929 00:40:59,020 --> 00:41:00,740 Immunity Debugger's a great one on Windows. 930 00:41:01,180 --> 00:41:02,900 Gerardo Ricarde from Core 931 00:41:02,900 --> 00:41:05,080 wrote a great OpenGL visualizer 932 00:41:05,080 --> 00:41:06,380 of the heap that's based on Trust 933 00:41:06,380 --> 00:41:08,820 that I think works on Linux and works on 934 00:41:09,120 --> 00:41:10,700 any of the BSDs and Solaris. 935 00:41:10,860 --> 00:41:12,680 So you have stuff there. The problem was 936 00:41:12,680 --> 00:41:14,480 you really had nothing on Mac before this 937 00:41:14,480 --> 00:41:15,760 that could help you do that. 938 00:41:16,160 --> 00:41:17,740 So the idea here is that 939 00:41:17,740 --> 00:41:19,880 we have these arbitrary hooks 940 00:41:19,880 --> 00:41:22,440 on any function in the system with Dtrash, right? 941 00:41:22,580 --> 00:41:24,320 So let's use them to keep track 942 00:41:24,320 --> 00:41:25,740 of what the heap looks like 943 00:41:25,740 --> 00:41:27,220 and what are the contents on the heap. 944 00:41:27,400 --> 00:41:28,960 So quickly you can, of course, 945 00:41:29,560 --> 00:41:32,120 you can use Apple's standard instruments tool 946 00:41:32,120 --> 00:41:34,200 for memory leaking to identify double frees 947 00:41:34,200 --> 00:41:35,940 and double mallocs that are going to allow you to 948 00:41:35,940 --> 00:41:38,280 maybe you could leverage that for arbitrary code execution 949 00:41:39,280 --> 00:41:41,020 spot off by one errors 950 00:41:41,020 --> 00:41:42,460 and you can do all kinds of heap visualization. 951 00:41:44,200 --> 00:41:47,480 So the comparison to something like Ltrace 952 00:41:47,480 --> 00:41:48,300 which could also hook 953 00:41:48,960 --> 00:41:50,060 malloc operations is 954 00:41:50,060 --> 00:41:52,220 Ltrace is like bonds on the pirates 955 00:41:52,220 --> 00:41:54,260 and Dtrace is like bonds on the giants 956 00:41:54,260 --> 00:41:55,680 so it's Ltrace on steroids. 957 00:41:56,120 --> 00:41:58,080 And the idea here is that 958 00:41:59,340 --> 00:42:01,020 you can create a heuristic 959 00:42:01,020 --> 00:42:02,360 for your specific application 960 00:42:02,360 --> 00:42:04,820 remember I said it's all about application specific data 961 00:42:04,820 --> 00:42:06,640 you could create a heuristic that says 962 00:42:06,640 --> 00:42:09,020 if this pointer's ever on the heap next to somewhere 963 00:42:09,120 --> 00:42:10,520 that I know I can already overflow 964 00:42:11,080 --> 00:42:12,140 then there's my exploit. 965 00:42:12,380 --> 00:42:14,660 So you can do all that kind of really interesting stuff. 966 00:42:14,940 --> 00:42:16,960 There's tons of tools on OSX 967 00:42:16,960 --> 00:42:17,960 that'll help you do this 968 00:42:17,960 --> 00:42:19,720 that'll set up guard pages on the heap 969 00:42:19,720 --> 00:42:20,980 so you know if you've corrupted it 970 00:42:20,980 --> 00:42:22,780 but none of them are going to allow you to have 971 00:42:22,780 --> 00:42:25,400 the application specific intelligence that you need 972 00:42:25,400 --> 00:42:27,640 to really write these sort of advanced heap overflows. 973 00:42:29,100 --> 00:42:31,140 So quickly this is what a directed graph 974 00:42:31,140 --> 00:42:32,280 if you were just interested in 975 00:42:32,280 --> 00:42:33,640 what some of the allocation patterns are 976 00:42:33,640 --> 00:42:36,060 you could quickly create a graph with allocation patterns. 977 00:42:37,620 --> 00:42:38,860 Included in Dtrace itself 978 00:42:39,120 --> 00:42:41,620 though we've created an automatic heap smash detector 979 00:42:41,620 --> 00:42:42,940 that'll instrument IDA 980 00:42:42,940 --> 00:42:44,740 and basically how that works is 981 00:42:44,740 --> 00:42:47,240 we keep track of every allocation made to the heap 982 00:42:47,240 --> 00:42:48,340 all the pointers on there 983 00:42:48,340 --> 00:42:50,600 and if anybody ever string copies memory 984 00:42:50,600 --> 00:42:53,460 or does a mem copy onto a chunk that we know about 985 00:42:53,460 --> 00:42:54,900 so we know the size and the location 986 00:42:54,900 --> 00:42:57,840 and that write is too large 987 00:42:57,840 --> 00:42:59,100 we know there's going to be a heap overflow 988 00:42:59,100 --> 00:43:00,280 and rewrite it into IDA. 989 00:43:00,580 --> 00:43:02,260 So let me quickly show you 990 00:43:04,620 --> 00:43:06,580 that probe would look something like this 991 00:43:07,120 --> 00:43:08,840 so in this case we're looking at 992 00:43:09,120 --> 00:43:09,800 an entry to string copy 993 00:43:09,800 --> 00:43:11,400 we're looking at the arguments 994 00:43:11,400 --> 00:43:13,180 and then we'll check that probe there 995 00:43:13,180 --> 00:43:15,540 against our known locations in the heap 996 00:43:15,540 --> 00:43:16,720 and we'll let you know if they overflow. 997 00:43:17,280 --> 00:43:18,600 So I'll show you a quick video there. 998 00:43:22,880 --> 00:43:23,980 So real quick 999 00:43:24,980 --> 00:43:26,060 essentially what we're going to do 1000 00:43:26,060 --> 00:43:27,980 is show you a vulnerable C application here 1001 00:43:27,980 --> 00:43:29,280 it's just standard 1002 00:43:29,280 --> 00:43:31,460 they malloc something that's not large enough 1003 00:43:31,460 --> 00:43:32,980 and then we do a string copy onto it 1004 00:43:33,620 --> 00:43:35,980 and if you can see through the transparency in the background 1005 00:43:35,980 --> 00:43:37,460 we have a copy of IDA going on. 1006 00:43:39,120 --> 00:43:39,740 So when we're running this application 1007 00:43:40,300 --> 00:43:41,740 what we're going to do is 1008 00:43:42,380 --> 00:43:44,080 we're going to copy a bunch of A's 1009 00:43:44,080 --> 00:43:45,980 on the command line and hope that 1010 00:43:45,980 --> 00:43:47,200 that triggers a heap overflow. 1011 00:43:48,700 --> 00:43:50,160 So just to show you 1012 00:43:50,860 --> 00:43:51,800 that's what it looks like 1013 00:43:51,800 --> 00:43:53,120 we've tapped the heap smash 1014 00:43:53,120 --> 00:43:55,440 and we've sent a bunch of A's in via command line 1015 00:43:55,440 --> 00:43:57,960 this is our IDA disassembly of the same program 1016 00:43:58,980 --> 00:44:00,320 so just looking around there 1017 00:44:00,320 --> 00:44:01,960 and we're going to fire it off 1018 00:44:01,960 --> 00:44:03,660 and if all goes well 1019 00:44:03,660 --> 00:44:05,820 any heap overflows that are detected 1020 00:44:05,820 --> 00:44:08,100 will automatically be in the disassembly of IDA 1021 00:44:09,540 --> 00:44:11,360 that has a problem in this app 1022 00:44:11,360 --> 00:44:13,540 and later on you can leverage that 1023 00:44:13,540 --> 00:44:14,920 using another Dtrace script 1024 00:44:14,920 --> 00:44:16,240 to exploit it. 1025 00:44:17,880 --> 00:44:20,200 So here we're starting up the IDA Rube server 1026 00:44:20,200 --> 00:44:22,280 that's going to give us a connection to the OSX 1027 00:44:22,280 --> 00:44:23,420 this is all in a VM 1028 00:44:23,420 --> 00:44:24,660 you can see it's in VMware Fusion 1029 00:44:24,660 --> 00:44:26,160 so we're setting up our server here 1030 00:44:29,900 --> 00:44:31,520 go back to the command line 1031 00:44:32,220 --> 00:44:33,580 fire off those A's 1032 00:44:33,580 --> 00:44:34,860 and hope it triggers a heap overflow. 1033 00:44:42,300 --> 00:44:44,560 So amazingly it does catch the heap overflow 1034 00:44:44,900 --> 00:44:47,360 you can see there that's the probe that found it 1035 00:44:47,360 --> 00:44:48,640 tells us what EIP was 1036 00:44:48,640 --> 00:44:50,040 what the destination was 1037 00:44:50,040 --> 00:44:52,020 the interesting thing to note there is 1038 00:44:52,020 --> 00:44:54,920 the size of the buffer was only 1039 00:44:54,920 --> 00:44:56,980 15 bytes and we copied 30 1040 00:44:56,980 --> 00:44:58,960 so obviously we smashed the heap there 1041 00:44:59,520 --> 00:45:01,900 and now we can go into our IDA disassembly 1042 00:45:01,900 --> 00:45:03,100 and it will pinpoint that 1043 00:45:03,100 --> 00:45:04,920 so where that would be useful 1044 00:45:04,920 --> 00:45:06,620 is if you had a browser or something like that 1045 00:45:06,620 --> 00:45:07,960 and you're running a fuzzer against it 1046 00:45:09,120 --> 00:45:09,840 and you run for a couple days 1047 00:45:09,840 --> 00:45:11,320 or hours or whatever 1048 00:45:11,320 --> 00:45:12,680 come back to the IDA database 1049 00:45:12,680 --> 00:45:14,420 it's going to tell you everywhere there was a problem 1050 00:45:14,420 --> 00:45:16,800 and now you can sort of vet those and figure out 1051 00:45:16,800 --> 00:45:18,200 which situation would be exploitable 1052 00:45:18,200 --> 00:45:22,000 so that's the comment and the marking in red there 1053 00:45:22,000 --> 00:45:24,800 so it's really powerful for combining static analysis 1054 00:45:24,800 --> 00:45:25,420 and run time 1055 00:45:26,600 --> 00:45:28,080 we're kind of running out of time here 1056 00:45:28,080 --> 00:45:30,620 so let's shift this over to 1057 00:45:31,020 --> 00:45:31,960 where do you want to pick up? 1058 00:45:32,660 --> 00:45:33,060 yeah 1059 00:45:34,260 --> 00:45:35,480 we've got just about 2 minutes 1060 00:45:35,940 --> 00:45:36,940 so let's go to summation 1061 00:45:39,120 --> 00:45:40,420 so we have some sections here 1062 00:45:40,420 --> 00:45:41,620 on using this defensively 1063 00:45:41,620 --> 00:45:43,020 some things you can do to stop 1064 00:45:43,020 --> 00:45:46,680 by seeing system call patterns 1065 00:45:46,680 --> 00:45:47,720 that it's not supposed to do 1066 00:45:47,720 --> 00:45:49,420 again you have performance issues there 1067 00:45:49,420 --> 00:45:51,680 you guys can check out the rest of the slides here 1068 00:45:51,680 --> 00:45:53,260 we also talk about 1069 00:45:54,000 --> 00:45:55,620 doing some kernel level stuff 1070 00:45:56,080 --> 00:45:58,380 not particularly related to Dtrace 1071 00:45:58,380 --> 00:45:59,980 but you see this screen 1072 00:45:59,980 --> 00:46:01,100 and you have no idea what happened 1073 00:46:01,100 --> 00:46:02,060 you want to figure it out 1074 00:46:02,060 --> 00:46:06,320 it turns out that there's these logs that get dumped 1075 00:46:06,320 --> 00:46:08,060 they give you a lot of information 1076 00:46:09,120 --> 00:46:10,360 to do the kernel debugging 1077 00:46:11,420 --> 00:46:12,220 that traditionally 1078 00:46:12,220 --> 00:46:14,480 but if you do you have to set up a remote server 1079 00:46:14,480 --> 00:46:15,700 and it's kind of a pain in the ass 1080 00:46:15,700 --> 00:46:19,180 let me skip to the end 1081 00:46:26,040 --> 00:46:27,660 let me make one point here 1082 00:46:27,660 --> 00:46:27,960 about 1083 00:46:30,540 --> 00:46:32,040 tracing higher level application 1084 00:46:32,040 --> 00:46:33,320 because this is a cool idea 1085 00:46:35,360 --> 00:46:36,920 so what you can do here is 1086 00:46:36,920 --> 00:46:38,960 trace some action that's a high level thing 1087 00:46:39,120 --> 00:46:39,940 like a whole page load 1088 00:46:39,940 --> 00:46:42,480 or an SQL query or a DNS lookup 1089 00:46:42,480 --> 00:46:44,180 so these are not particular functions 1090 00:46:44,180 --> 00:46:46,860 but they're actions composed of lots of functions 1091 00:46:46,860 --> 00:46:49,000 so this makes sense for IO performance tracing 1092 00:46:49,000 --> 00:46:50,600 but also for pen testers 1093 00:46:50,600 --> 00:46:53,220 we can use this for our own purposes 1094 00:46:53,220 --> 00:46:53,920 for instance 1095 00:46:53,920 --> 00:46:56,500 we want to fuzz an application front end 1096 00:46:56,500 --> 00:46:58,020 that has a web front end 1097 00:46:58,020 --> 00:46:59,640 and then examine what's going on 1098 00:46:59,640 --> 00:47:01,400 with the SQL database in the back 1099 00:47:01,400 --> 00:47:03,620 so look at the particular SQL statements 1100 00:47:03,620 --> 00:47:05,640 and are they sanitized or not 1101 00:47:05,640 --> 00:47:08,500 so the idea here is that on the OSX platform 1102 00:47:09,120 --> 00:47:10,640 because DTrace is implemented in the kernel 1103 00:47:10,640 --> 00:47:12,640 it's really trivial for anyone running an app 1104 00:47:12,640 --> 00:47:15,400 on OSX to add DTrace probes 1105 00:47:15,400 --> 00:47:16,360 that will allow us to hook them 1106 00:47:16,360 --> 00:47:18,920 so we can hook Javascript, SQL injection 1107 00:47:18,920 --> 00:47:20,640 all kinds of stuff that are beyond 1108 00:47:20,640 --> 00:47:21,940 sort of standard memory corruption 1109 00:47:21,940 --> 00:47:24,740 and I'll show you real quick 1110 00:47:24,740 --> 00:47:25,680 where you can get the code 1111 00:47:30,120 --> 00:47:30,760 here you go 1112 00:47:30,760 --> 00:47:31,460 pop pop ret 1113 00:47:31,460 --> 00:47:33,640 email us if we don't put it up in the next couple days 1114 00:47:33,640 --> 00:47:34,700 we've been known to lag 1115 00:47:34,700 --> 00:47:35,600 and thank you for your time