1 00:00:00,000 --> 00:00:02,320 Today we're going to be talking about programmatic debugging. 2 00:00:03,060 --> 00:00:05,780 Now before any of you get up and walk out, 3 00:00:05,820 --> 00:00:07,720 because I didn't tell you exactly what that means, 4 00:00:08,260 --> 00:00:09,760 let's go a little bit about my past. 5 00:00:10,440 --> 00:00:14,020 I like to lead that up because it gives me like 15 seconds and I'm done 6 00:00:14,020 --> 00:00:16,280 and you guys are happy instead of saying, hey, why didn't you talk more? 7 00:00:20,380 --> 00:00:21,840 I was raised on a computer. 8 00:00:22,120 --> 00:00:23,880 Had one when I was 7 or 8. 9 00:00:23,980 --> 00:00:24,760 Dad worked for IBM. 10 00:00:27,400 --> 00:00:28,980 Took to programming, really liked it. 11 00:00:29,660 --> 00:00:31,720 Then went and got a degree. 12 00:00:32,140 --> 00:00:33,940 Started out as a vocal performance major. 13 00:00:38,220 --> 00:00:42,080 Trust me, after CTF, this long you do not want to hear me sing. 14 00:00:43,280 --> 00:00:46,320 It would have to be some sort of Elvis melody or something 15 00:00:46,320 --> 00:00:47,880 because I just can't hit anything high. 16 00:00:49,360 --> 00:00:53,320 So after spending a year and a half as a computer science major, 17 00:00:53,480 --> 00:00:55,540 I jumped into pre-med because, well, 18 00:00:55,560 --> 00:00:58,880 I didn't want to go into computer science without testing other areas. 19 00:00:59,560 --> 00:01:03,580 And, of course, bachelors in computer science was in for me. 20 00:01:04,760 --> 00:01:07,000 Got out, got into a teaching job. 21 00:01:08,680 --> 00:01:11,520 And, I don't know, computer sought me out. 22 00:01:12,040 --> 00:01:14,560 Did a lot of fun stuff and, well, I'm here. 23 00:01:16,220 --> 00:01:22,420 I do a lot of research into security issues by divine intervention, 24 00:01:22,640 --> 00:01:25,740 some help from friends and, well, because I love it. 25 00:01:27,680 --> 00:01:28,960 I lead a capital. 26 00:01:28,980 --> 00:01:30,460 I have a capture the flag team called Last Place. 27 00:01:30,680 --> 00:01:31,640 Anybody heard of capture the flag? 28 00:01:34,220 --> 00:01:35,060 Lots of fun. 29 00:01:35,120 --> 00:01:36,300 I encourage you all to try out. 30 00:01:36,660 --> 00:01:38,440 You got to qualify for it. 31 00:01:38,520 --> 00:01:41,580 So you got to sign up a couple months ahead before DEF CON. 32 00:01:42,640 --> 00:01:46,340 So check out kenshodo.com and you too can have a lot of fun. 33 00:01:47,860 --> 00:01:49,440 I have a lovely family. 34 00:01:51,240 --> 00:01:57,640 And they see me probably less than they'd like but more than some other people. 35 00:01:58,060 --> 00:01:58,960 I keep trying to do that. 36 00:01:58,960 --> 00:02:00,580 I'm trying to tell them that and they don't get that. 37 00:02:02,700 --> 00:02:03,860 What is programmatic debugging? 38 00:02:05,480 --> 00:02:07,600 Well, you might think. 39 00:02:09,700 --> 00:02:10,760 What do you think? 40 00:02:11,380 --> 00:02:12,520 Somebody speak it out. 41 00:02:18,300 --> 00:02:18,900 Fuzzing. 42 00:02:23,920 --> 00:02:27,320 Interactively debugging or automating the debugging process. 43 00:02:29,060 --> 00:02:33,900 Dude, that is, that's really good. Can we like relay this? Because I don't want to throw 44 00:02:33,900 --> 00:02:38,420 that far. That was close, but I'm getting there. You're jumping way ahead. I'm sorry. 45 00:02:42,920 --> 00:02:51,600 The idea behind programmatic debugging is the use of debugging code in a canned fashion. 46 00:02:51,600 --> 00:02:59,320 So you write a program to debug another program. Not always, but we're talking rough definition 47 00:02:59,320 --> 00:03:05,760 here. So what's debugging? Well, it's getting rid of bugs, right? And if you think that, 48 00:03:06,200 --> 00:03:12,120 well, I should probably get out and let Martha Stewart come up and finish the talk. I don't 49 00:03:12,120 --> 00:03:17,740 care about getting rid of bugs at this very moment. And there's a reason that I maintain 50 00:03:17,740 --> 00:03:21,500 my anonymity, because I can say that now. In my day job, well, I can't quite get away 51 00:03:21,600 --> 00:03:28,060 with it. But what am I interested in when I'm doing programmatic debugging? Somebody. 52 00:03:30,860 --> 00:03:37,920 Finding bugs. Very smart. I don't care necessarily to get rid of them, but I'm interested in 53 00:03:37,920 --> 00:03:45,980 finding them. A debugger allows you to access the important components of a running process. 54 00:03:46,740 --> 00:03:51,580 By the way, I've not slept much. I've been competing in CCF. So if I trip and fall, I 55 00:03:51,600 --> 00:03:56,380 fall asleep during my talk, somebody please get up and nudge me and I'll carry on what 56 00:03:56,380 --> 00:04:06,520 I can. It's the access of a running program, its memory, its CPU state, and several other 57 00:04:06,520 --> 00:04:14,160 goodies in a way that is outside of the program. Again, it's a rough definition. Some of you 58 00:04:14,160 --> 00:04:16,400 can argue that, but that's what we're going to go with. 59 00:04:21,660 --> 00:04:28,760 In the last couple years, I've seen the advent of several very interesting tools that implement 60 00:04:28,760 --> 00:04:33,080 programmatic debugging. The first one that I ran into is PyDebug, and some of you have 61 00:04:33,080 --> 00:04:40,340 heard of it. PyDebug is a part of the PyMay hacking framework. Thank you, Pedram. It's 62 00:04:40,340 --> 00:04:44,880 a really cool tool. Unfortunately, it only works on Windows. It does work in Python, 63 00:04:45,100 --> 00:04:51,580 woohoo, which is good. ImmunitySec just came out with a debugger with a Python interface, 64 00:04:51,600 --> 00:04:57,340 basically what it looks like, and I can't speak to their coding practices, but it looks 65 00:04:57,340 --> 00:05:03,980 like they took OliDebug, of great fame. Anybody love Oli? Oli's good. I like Oli on Windows. 66 00:05:04,500 --> 00:05:09,900 They took OliDebug, or at least the look of it and feel of it, wrapped it into their 67 00:05:09,900 --> 00:05:18,520 own debugger with new themes of colors, and provided a Python programming interface. Kudos 68 00:05:18,520 --> 00:05:20,760 to them. The Python interface was very smart. 69 00:05:22,120 --> 00:05:30,940 Then, Invisigoth, in a very un-vocal way, that is Invisigoth, wrote a tool called Vtrace 70 00:05:30,940 --> 00:05:35,620 and released it to the public. Vtrace is what we're going to be talking most about, because 71 00:05:35,620 --> 00:05:40,620 Vtrace is my favorite, and I'm the speaker, and if you have a problem with it, you talk. 72 00:05:43,360 --> 00:05:50,880 I promised a good friend I'd tell you about NoxDebug a little. NoxDebug is a Ruby-based 73 00:05:51,600 --> 00:06:04,580 program debugger. Used to be that all debugging frameworks were just, you had raw C code that 74 00:06:04,580 --> 00:06:11,700 you had to write to. This is why GDB has, well, it's one of the few debuggers on the 75 00:06:11,700 --> 00:06:16,920 POSIX platform, and it's why there are so few real debuggers on Windows, because we 76 00:06:16,920 --> 00:06:18,100 had this C interface. 77 00:06:18,100 --> 00:06:26,320 What I'm seeing, however, is your favorite programming language, and mine, well, yeah, 78 00:06:26,340 --> 00:06:35,280 Python, coming out with their own hooks into debugging, such as Ptrace on the POSIX side, 79 00:06:35,380 --> 00:06:46,360 and the Win32 debugging routines on Windows. What does this mean? Who cares? I'll never 80 00:06:46,360 --> 00:06:47,480 write a debugger in my life. 81 00:06:47,480 --> 00:06:49,020 I don't know why you're saying this to me. 82 00:06:51,140 --> 00:06:56,820 You should care, because we've just taken the difficulty gap and shrunk it immensely 83 00:06:56,820 --> 00:07:00,280 for you, too, to do amazing things using programmatic debugging. 84 00:07:02,480 --> 00:07:11,220 So what can we do? Well, a few examples from, I don't know, am I allowed to say that 85 00:07:11,220 --> 00:07:17,460 I helped the Intel Guardians folks in their venture against Microsoft? 86 00:07:17,460 --> 00:07:23,540 You guys heard about Intel Guardians, right? Intel Guardians, they rock. They're so cool. 87 00:07:24,480 --> 00:07:31,080 They had, they put government dollars to good work. I know you don't believe me, but to 88 00:07:31,080 --> 00:07:34,320 good work, government dollars, yes, same sentence, good. 89 00:07:35,940 --> 00:07:42,100 DHS money investigating the relative security of virtual machines. Why? Well, because you 90 00:07:42,100 --> 00:07:46,640 probably have virtual machines in your data center, and probably on most of your desktops, 91 00:07:46,640 --> 00:07:51,620 at least most of your desktops in this room. I know I've got one in every system I own, 92 00:07:53,540 --> 00:08:01,000 to prove that virtual machine environments are not to be used as security divisors. 93 00:08:01,580 --> 00:08:06,560 You do not mix security domains on virtual machines. Anyway, I got to play a part in that, 94 00:08:06,660 --> 00:08:14,660 and it was really cool. I built a lot of tools, and some since then, and I'm going to tell 95 00:08:14,660 --> 00:08:16,040 you a little bit about them. 96 00:08:16,040 --> 00:08:21,960 Live patching. Anybody hacked any video games? Get around keys or whatever? You don't have 97 00:08:21,960 --> 00:08:25,600 to admit it. It's illegal. I'm not encouraging it, but I know some of you probably have done 98 00:08:25,600 --> 00:08:31,980 it. In fact, some of my best friends got into security field by, well, something like 99 00:08:31,980 --> 00:08:37,920 that. So I wrote a tool called Live Patch. I give a memory location, and I give what 100 00:08:37,920 --> 00:08:45,660 bytes I want to see there. From a bash shell, it attaches to a binary, modifies the running 101 00:08:46,040 --> 00:08:50,660 process memory, and releases it. And it gets to keep spinning. 102 00:08:51,760 --> 00:08:58,100 Live dumping. This is something that I often couple with process grep. Live dumping is 103 00:08:58,100 --> 00:09:05,120 the idea of, from a bash prompt, I run my tool, I give it an address and a length, and 104 00:09:05,120 --> 00:09:10,600 it spits out, in nice hex fashion, all the bytes in that chunk that I've just described. 105 00:09:11,400 --> 00:09:15,880 Then there's process grep. I don't claim responsibility for this one, although I probably 106 00:09:16,040 --> 00:09:21,180 will have to give it to you since Invisigoth saw fit to remove it from his tools. 107 00:09:22,040 --> 00:09:27,200 Mem grep is really cool. You type mem grep from a bash prompt. You guys get the idea 108 00:09:27,200 --> 00:09:36,760 I like bash too. You can use cmd.exe as well. The idea is you type mem grep, give a seed 109 00:09:36,760 --> 00:09:43,780 or a needle, as it's called. Mem grep attaches to a process, searches through that process 110 00:09:43,780 --> 00:09:45,540 for the needle. 111 00:09:45,540 --> 00:09:52,400 In your haystack. Or process stack, or whatever kind of stack it's looking at. 112 00:09:53,680 --> 00:09:58,720 And a tool that's kind of had to take a back burner recently due to preparation for CTF 113 00:09:58,720 --> 00:10:03,260 and a few other things at work. Vampire Jack SSHD. This is a tool that Doc Brown and I 114 00:10:03,260 --> 00:10:13,360 wanted to get ready for CTF this year. But the idea is you create a program that attaches 115 00:10:13,360 --> 00:10:23,440 to the SSHDemon, handles children as they're spawned, and then basically Vampire Jacks in 116 00:10:23,440 --> 00:10:27,780 and allows standard in, standard output to be written to a file even though you're not 117 00:10:27,780 --> 00:10:30,800 the one that has the session open. Really cool, fun stuff. 118 00:10:34,240 --> 00:10:40,420 What else can we do? Well, basically everything that your favorite debugger can do, we can 119 00:10:40,420 --> 00:10:42,600 do with Vtrace or one of the others that we mentioned. 120 00:10:43,940 --> 00:10:52,520 I like search memory. The basis for Mem grep, anybody done searching through memory space 121 00:10:52,520 --> 00:10:56,160 and GDB? Oh my goodness, do I feel your pain. 122 00:10:57,240 --> 00:11:04,860 I actually, since then, since finding Vtrace, I have figured out a way to make GDB's very 123 00:11:07,000 --> 00:11:12,300 obtuse programming language do searching, but it's extremely slow. Mem grep is very fast. 124 00:11:12,300 --> 00:11:21,780 And search memory. In order to use, in order to use Vtrace for all that I want to use it for, 125 00:11:21,920 --> 00:11:28,100 I found that I have wrapped it into my Python shell. Yes, I like to use Python as a shell. 126 00:11:28,240 --> 00:11:37,520 Interactive Python is an amazing way to compute. By using Vtrace, it allows me to attach, just 127 00:11:38,260 --> 00:11:40,700 randomly attach to a process or start one up. 128 00:11:41,420 --> 00:11:48,680 Step through execution, searching, running, breakpointing, everything that you would do in a 129 00:11:48,680 --> 00:11:51,160 normal program, I can do right in my Python shell. 130 00:11:54,880 --> 00:11:57,280 The real question here is, what do you want to do with it? 131 00:11:59,660 --> 00:12:04,720 At the end of this, you're going to walk away with some rantings and ravings of mine. 132 00:12:05,760 --> 00:12:08,280 But I'm going to stick you with this challenge. 133 00:12:09,160 --> 00:12:11,140 What are you going to present on next year? 134 00:12:12,260 --> 00:12:13,860 What are you going to do with this information? 135 00:12:15,880 --> 00:12:25,940 Too many people that are very, very intelligent, or motivated, or whatnot, simply lack the encouragement to be excellent. 136 00:12:26,300 --> 00:12:29,540 I want to take that, take this moment and encourage you all. 137 00:12:29,620 --> 00:12:31,460 And I will do it again, because that's just how I am. 138 00:12:34,640 --> 00:12:36,620 People like pictures, so I threw these in. 139 00:12:36,620 --> 00:12:38,520 I don't know how well you can see them. 140 00:12:39,320 --> 00:12:46,220 On the left hand side, I've got a sans program written by my good friend Ed Skotas called Format Flaw. 141 00:12:46,660 --> 00:12:49,180 Sorry Ed, I didn't ask your permission before throwing it in. 142 00:12:50,300 --> 00:12:55,560 On the right hand side, I've got an interactive Vtrace session open. 143 00:12:57,840 --> 00:13:04,420 So we run Format Flaw, we import my own interface to Vtrace. 144 00:13:05,140 --> 00:13:07,000 Then I tell it to attach to Format Flaw. 145 00:13:07,840 --> 00:13:09,800 It gives me a little backtrace. 146 00:13:10,980 --> 00:13:11,820 Tell it to run. 147 00:13:12,420 --> 00:13:16,340 And then when I overflow the buffer, we get... 148 00:13:18,840 --> 00:13:19,840 What is this? 149 00:13:24,820 --> 00:13:29,120 Anyway, we get the different messages that I can't read right now. Sorry. 150 00:13:32,700 --> 00:13:33,640 Using a tool... 151 00:13:33,640 --> 00:13:36,280 Just a little helper script that I wrote. 152 00:13:37,540 --> 00:13:42,200 It basically provides me with the view that I like to see in GDB. 153 00:13:42,720 --> 00:13:44,620 And Vtrace allows me to step. 154 00:13:45,080 --> 00:13:49,640 So you see my next instruction tells me where I'm at. 155 00:13:50,080 --> 00:13:54,660 This is stack information and more stack information from a different perspective. 156 00:13:54,840 --> 00:13:58,700 All my registers, where I'm at, and what the next instruction is. 157 00:14:04,380 --> 00:14:06,520 So the real question here is, so what? 158 00:14:08,720 --> 00:14:10,660 Yeah, yeah, at least you have fun. 159 00:14:10,920 --> 00:14:12,000 Cool, we knew that. 160 00:14:16,240 --> 00:14:20,700 But wouldn't it be interesting if we could take this new knowledge and apply it in some applicable way? 161 00:14:20,920 --> 00:14:22,800 I know that Java has a bad name. 162 00:14:23,120 --> 00:14:25,540 Probably some of you would cheer for that. 163 00:14:27,660 --> 00:14:32,880 Java has a bad name probably because marketers saw one little component of Java and ran with it. 164 00:14:33,460 --> 00:14:34,740 Called a Java applet. 165 00:14:34,820 --> 00:14:36,180 And they marketed the crap out of it. 166 00:14:37,360 --> 00:14:42,120 And they had everybody and their boss writing Java code for little applets. 167 00:14:42,320 --> 00:14:43,100 And it was annoying. 168 00:14:43,320 --> 00:14:44,020 It was slow. 169 00:14:44,240 --> 00:14:45,240 And it got a bad name. 170 00:14:45,380 --> 00:14:50,800 The problem was they had selected only one of the usable things of Java. 171 00:14:51,800 --> 00:14:54,140 So what do we do with each new piece of information? 172 00:14:54,460 --> 00:14:55,800 This one for example. 173 00:14:56,620 --> 00:15:02,240 How do we take programmatic debugging or the ability to interact with a debugger 174 00:15:02,240 --> 00:15:09,380 at a Python code level and encourage vulnerabilities to magically appear? 175 00:15:09,820 --> 00:15:10,680 That be interesting? 176 00:15:16,660 --> 00:15:19,020 Well, that sounds kind of like fuzzing, right? 177 00:15:19,300 --> 00:15:22,240 I mean, you fuzz and you make a program break. 178 00:15:22,380 --> 00:15:24,900 And that means that, well, you've got some sort of a vulnerability. 179 00:15:25,120 --> 00:15:29,560 Even if you can't exploit it for shellcode, you can at least DOS the program. 180 00:15:31,080 --> 00:15:32,640 This is a little bit different. 181 00:15:32,820 --> 00:15:37,500 What we're going to talk about here is something that can be applied to fuzzing to help it. 182 00:15:37,820 --> 00:15:44,160 But the idea is more, how do we indicate that we're breaking? 183 00:15:44,540 --> 00:15:48,220 But more importantly, how can we indicate that we may break it? 184 00:15:49,680 --> 00:15:56,600 See, fuzzing is weak only because only when you truly break something do you have anything at all. 185 00:15:56,660 --> 00:15:59,540 And then you have to try to track back how you break it. 186 00:15:59,560 --> 00:16:00,400 We broke it in the first place. 187 00:16:00,940 --> 00:16:08,060 If we could stop a buffer overflow before it happens and trigger and mention, 188 00:16:08,420 --> 00:16:10,880 hey, you know, this might be a good place for you to go look. 189 00:16:14,020 --> 00:16:20,820 These things are more important, in my opinion, for vulnerability discovery than simply breaking a program. 190 00:16:21,000 --> 00:16:27,100 Although, I've got to tell you, I know some great fuzzers who break programs all the time. 191 00:16:27,180 --> 00:16:29,540 In fact, one of the Ken Chodo guys said, 192 00:16:29,560 --> 00:16:32,600 this guy was referring to what he does during the day, 193 00:16:32,700 --> 00:16:38,940 and he says, I think, he says, I just throw a lot of poo and things break really cool. 194 00:16:41,920 --> 00:16:43,140 I like elegance. 195 00:16:43,220 --> 00:16:44,920 I'd rather know where that poo really hit. 196 00:16:48,600 --> 00:16:50,740 So let's take buffer overflows, for example. 197 00:16:51,080 --> 00:16:54,400 There are several major vulnerabilities in binary programs. 198 00:16:56,560 --> 00:16:58,980 Buffer overflows are probably the sexiest. 199 00:17:00,380 --> 00:17:06,000 So how do we identify a buffer overflow or a potential buffer overflow? 200 00:17:10,720 --> 00:17:15,420 Just so I know, how many of you have written any Python code at all? 201 00:17:15,980 --> 00:17:16,640 Raise your hand. 202 00:17:17,680 --> 00:17:19,740 Excellent. Excellent. 203 00:17:20,140 --> 00:17:23,420 So you've probably heard of something called an object, right? 204 00:17:24,080 --> 00:17:25,640 Object orientation in classes? 205 00:17:25,640 --> 00:17:29,940 Well, Vtrace introduces a breakpoint class, 206 00:17:30,100 --> 00:17:32,920 which can be subclassed to do whatever functionality you want. 207 00:17:34,080 --> 00:17:40,100 So we set a custom breakpoint at key functions that are notorious for causing buffer overflows. 208 00:17:41,840 --> 00:17:45,020 We do some stack analysis and buffer analysis, 209 00:17:47,200 --> 00:17:54,800 and we can identify if this particular overwrite is going to go beyond the length of this buffer, 210 00:17:54,800 --> 00:17:58,220 or at least to a high degree of certainty. 211 00:18:01,380 --> 00:18:01,860 Yeah. 212 00:18:02,620 --> 00:18:03,980 There's a great deal of value in it. 213 00:18:06,100 --> 00:18:08,560 I'll give you a little example of attaching with Vtrace. 214 00:18:08,740 --> 00:18:09,700 We start Python. 215 00:18:09,900 --> 00:18:11,980 We run from Vtrace import star, 216 00:18:12,520 --> 00:18:13,500 because that's how I roll. 217 00:18:14,120 --> 00:18:15,460 Me equals get trace, 218 00:18:16,220 --> 00:18:17,700 and then attach to the PID. 219 00:18:19,180 --> 00:18:20,600 We add some breakpoints. 220 00:18:22,240 --> 00:18:23,960 I like to tell it run forever, 221 00:18:23,960 --> 00:18:25,560 especially in this type of work. 222 00:18:26,140 --> 00:18:28,240 This means when I hit a breakpoint, 223 00:18:28,340 --> 00:18:31,900 execute the code at that breakpoint that the object identifies. 224 00:18:32,180 --> 00:18:33,320 Remember, custom breakpoints. 225 00:18:34,380 --> 00:18:35,420 And then keep going. 226 00:18:36,100 --> 00:18:40,060 I don't want to break and take a look at the stack and look at the registers. 227 00:18:40,300 --> 00:18:42,180 Just execute the code and keep going. 228 00:18:43,060 --> 00:18:44,140 Then I tell it to run. 229 00:18:50,680 --> 00:18:51,900 So for the next few slides, 230 00:18:51,900 --> 00:18:55,760 I'm going to go through and address how we might approach this 231 00:18:56,440 --> 00:19:01,800 from some of the very most popular ways of causing a buffer overflow. 232 00:19:02,600 --> 00:19:03,480 So memcpy. 233 00:19:04,660 --> 00:19:05,660 It takes a source, 234 00:19:05,760 --> 00:19:06,380 takes a destination, 235 00:19:06,640 --> 00:19:07,140 takes a length, 236 00:19:07,280 --> 00:19:08,700 and copies from one to the other. 237 00:19:08,740 --> 00:19:09,700 It doesn't care anything else. 238 00:19:13,300 --> 00:19:15,060 So we break at memcpy, 239 00:19:18,260 --> 00:19:19,800 or at a call to memcpy, 240 00:19:19,920 --> 00:19:20,320 specifically, 241 00:19:20,320 --> 00:19:22,900 before it does its little prolog thing 242 00:19:22,900 --> 00:19:25,420 and sets up its new frame pointer. 243 00:19:26,800 --> 00:19:28,700 So we check the length of dest. 244 00:19:30,200 --> 00:19:31,460 We can find dest, 245 00:19:31,520 --> 00:19:33,020 the destination buffer, 246 00:19:33,920 --> 00:19:35,480 by looking at the stack pointer 247 00:19:35,480 --> 00:19:37,200 and moving up four bytes. 248 00:19:42,020 --> 00:19:44,020 That gives us a pointer to either, 249 00:19:44,100 --> 00:19:44,720 typically, 250 00:19:44,820 --> 00:19:45,800 either a heap location 251 00:19:46,140 --> 00:19:47,020 or a stack location. 252 00:19:47,240 --> 00:19:48,140 If you don't know what those are, 253 00:19:48,280 --> 00:19:49,160 that's okay. 254 00:19:49,160 --> 00:19:50,820 Don't get up and walk out. 255 00:19:50,920 --> 00:19:51,640 I'm not going to kill you. 256 00:19:54,200 --> 00:19:57,580 But they are just two significantly different 257 00:19:57,580 --> 00:19:59,840 and very important ways to store information. 258 00:20:01,400 --> 00:20:02,440 On the heap, 259 00:20:02,620 --> 00:20:04,940 specifically targeting DLMalloc in this case, 260 00:20:05,340 --> 00:20:08,340 we can often check the length of a buffer 261 00:20:10,360 --> 00:20:14,120 because the pointer to a malloc buffer 262 00:20:15,560 --> 00:20:17,360 is exactly four bytes 263 00:20:17,360 --> 00:20:19,140 after the length of the buffer. 264 00:20:19,160 --> 00:20:20,580 Kind of cool, huh? 265 00:20:21,480 --> 00:20:24,120 So we simply go to the pointer 266 00:20:24,120 --> 00:20:25,360 and we back up four bytes 267 00:20:25,360 --> 00:20:27,740 and we read in four bytes. 268 00:20:27,880 --> 00:20:28,600 How many bytes, 269 00:20:28,820 --> 00:20:30,380 or how many bits is four bytes? 270 00:20:32,420 --> 00:20:32,860 32. 271 00:20:33,240 --> 00:20:34,200 Why is that important? 272 00:20:36,040 --> 00:20:38,560 Yes, this talk will be focused on 32-bit operating, 273 00:20:38,700 --> 00:20:39,980 or 32-bit systems. 274 00:20:41,160 --> 00:20:42,500 It's just the way I had to write it. 275 00:20:43,020 --> 00:20:43,860 64 bits. 276 00:20:44,000 --> 00:20:45,140 On a 64-bit system, 277 00:20:45,260 --> 00:20:45,920 what would this number be? 278 00:20:49,320 --> 00:20:49,840 Seven. 279 00:20:49,920 --> 00:20:50,340 Exactly. 280 00:20:50,560 --> 00:20:51,360 Give that man... 281 00:20:51,360 --> 00:20:51,960 Oh, dude. 282 00:20:52,300 --> 00:20:53,760 You are so pwned. 283 00:21:02,520 --> 00:21:03,660 By the way, 284 00:21:03,680 --> 00:21:04,300 I've got to give credit. 285 00:21:08,440 --> 00:21:11,220 Hey, Squires. 286 00:21:14,260 --> 00:21:15,700 I won't waste this on them. 287 00:21:26,820 --> 00:21:28,000 Invisigoth and Squires, everyone. 288 00:21:28,700 --> 00:21:29,700 They love giving me crap. 289 00:21:32,880 --> 00:21:34,240 The dude without any hair 290 00:21:34,240 --> 00:21:35,560 until you see the back of his head, 291 00:21:35,660 --> 00:21:36,760 he's the one who wrote V-trace. 292 00:21:37,200 --> 00:21:37,880 That's Invisigoth. 293 00:21:44,640 --> 00:21:45,540 I got you. 294 00:21:46,400 --> 00:21:47,720 This one's got your name on it. 295 00:21:47,760 --> 00:21:49,140 You come back and I'll throw it at you. 296 00:21:51,720 --> 00:21:52,480 So anyway, 297 00:21:52,660 --> 00:21:53,230 as we were saying... 298 00:22:02,520 --> 00:22:03,620 Thank you very much. 299 00:22:07,300 --> 00:22:07,880 So anyway, 300 00:22:08,680 --> 00:22:12,080 so we jump to the pointer for the heap chunk. 301 00:22:12,320 --> 00:22:13,280 We back up four bytes. 302 00:22:13,360 --> 00:22:14,820 We get the size, right? 303 00:22:15,980 --> 00:22:16,360 Okay. 304 00:22:16,400 --> 00:22:17,220 In an ideal world, 305 00:22:17,280 --> 00:22:17,740 that'd be true. 306 00:22:18,400 --> 00:22:19,720 We'll go over in a few minutes 307 00:22:19,720 --> 00:22:21,300 how that's not always the case. 308 00:22:21,500 --> 00:22:23,020 But if you actually hit a pointer 309 00:22:23,500 --> 00:22:25,640 to a real buffer chunk, 310 00:22:26,200 --> 00:22:27,320 it does work that way. 311 00:22:27,660 --> 00:22:28,480 On the stack, 312 00:22:28,640 --> 00:22:28,760 well, 313 00:22:29,560 --> 00:22:30,400 stack variables 314 00:22:30,400 --> 00:22:32,680 are typically your local variables 315 00:22:32,680 --> 00:22:33,540 for a subroutine. 316 00:22:33,620 --> 00:22:34,460 If you've written code, 317 00:22:34,680 --> 00:22:35,160 hopefully, 318 00:22:35,240 --> 00:22:36,080 that'll make sense to you. 319 00:22:46,140 --> 00:22:47,020 All right. 320 00:22:47,040 --> 00:22:47,720 Bring it on back. 321 00:22:47,720 --> 00:22:47,780 Come on, baby. 322 00:23:00,440 --> 00:23:04,100 The truth is... 323 00:23:04,100 --> 00:23:08,900 The truth is... 324 00:23:10,600 --> 00:23:12,080 I am Atlas. 325 00:23:17,840 --> 00:23:18,300 Okay. 326 00:23:25,420 --> 00:23:27,800 I got a picture of one of those things 327 00:23:27,800 --> 00:23:30,760 stuck to my head last year at CTF. 328 00:23:30,800 --> 00:23:31,400 It was pretty fun. 329 00:23:32,500 --> 00:23:33,140 So anyway, 330 00:23:33,920 --> 00:23:34,780 on the stack, 331 00:23:34,880 --> 00:23:36,800 we generally store local variables 332 00:23:36,800 --> 00:23:39,260 for whatever subroutine we're in. 333 00:23:39,600 --> 00:23:41,500 And yes, you may have several of them. 334 00:23:41,720 --> 00:23:42,680 Most often, 335 00:23:42,780 --> 00:23:43,560 we are only... 336 00:23:43,560 --> 00:23:44,260 Well, okay. 337 00:23:45,780 --> 00:23:46,860 The really easy 338 00:23:46,860 --> 00:23:48,760 to score vulnerabilities 339 00:23:48,760 --> 00:23:50,860 can be found... 340 00:24:00,480 --> 00:24:01,760 You bet. 341 00:24:04,820 --> 00:24:05,540 Love you guys. 342 00:24:06,900 --> 00:24:07,560 Whoo! 343 00:24:08,540 --> 00:24:09,500 Dude. 344 00:24:10,800 --> 00:24:12,120 We need it. 345 00:24:15,480 --> 00:24:16,420 Last place. 346 00:24:16,860 --> 00:24:17,440 Thank you very much. 347 00:24:18,320 --> 00:24:19,980 Although I may have you change that 348 00:24:19,980 --> 00:24:21,280 to whatever place we come in. 349 00:24:25,740 --> 00:24:26,180 Nice. 350 00:24:27,780 --> 00:24:28,220 Nice. 351 00:24:30,900 --> 00:24:31,340 Anyway, 352 00:24:31,420 --> 00:24:32,040 I'm in the middle 353 00:24:32,040 --> 00:24:33,140 of the competition right now. 354 00:24:33,240 --> 00:24:34,620 So I love you all a lot 355 00:24:34,620 --> 00:24:35,720 to lead my team 356 00:24:35,720 --> 00:24:36,540 and come talk. 357 00:24:37,860 --> 00:24:38,440 So anyway, 358 00:24:39,240 --> 00:24:40,200 it is interesting 359 00:24:40,200 --> 00:24:42,180 to overwrite different variables 360 00:24:43,020 --> 00:24:43,880 on the stack 361 00:24:43,880 --> 00:24:45,340 between you and return pointer. 362 00:24:45,480 --> 00:24:46,840 But if you can get to the return pointer, 363 00:24:46,840 --> 00:24:47,620 you win. 364 00:24:48,280 --> 00:24:49,460 In some cases. 365 00:24:49,620 --> 00:24:50,000 Anyway, 366 00:24:50,980 --> 00:24:52,400 but it's of interest to you, 367 00:24:52,420 --> 00:24:52,760 either way. 368 00:24:53,740 --> 00:24:54,320 So we... 369 00:24:54,720 --> 00:24:56,020 So a very rudimentary way 370 00:24:56,020 --> 00:24:57,060 to look for buffer overflows 371 00:24:57,060 --> 00:24:57,600 on the stack 372 00:24:57,600 --> 00:24:58,740 is to check 373 00:25:00,180 --> 00:25:01,200 EBP plus four, 374 00:25:01,400 --> 00:25:02,280 the base pointer, 375 00:25:04,540 --> 00:25:06,380 and beyond the base pointer is ret, 376 00:25:06,480 --> 00:25:07,880 so that's where we get the plus four. 377 00:25:08,220 --> 00:25:09,000 And we subtract 378 00:25:10,360 --> 00:25:11,240 the destination... 379 00:25:12,820 --> 00:25:14,060 the destination buffer 380 00:25:14,060 --> 00:25:15,900 to see how long we have to go 381 00:25:15,900 --> 00:25:16,820 to overwrite the return pointer. 382 00:25:20,620 --> 00:25:22,160 So we wrote a... 383 00:25:22,160 --> 00:25:22,940 Well, 384 00:25:23,020 --> 00:25:24,340 I wrote a very... 385 00:25:24,980 --> 00:25:26,400 This is a sparse version 386 00:25:26,400 --> 00:25:27,620 of MemCopyBreaker. 387 00:25:27,960 --> 00:25:29,100 If you want the real thing, 388 00:25:29,180 --> 00:25:30,200 go get Atlas Utils, 389 00:25:32,060 --> 00:25:32,940 downloadable on the web, 390 00:25:33,040 --> 00:25:34,080 probably at my website somewhere. 391 00:25:35,720 --> 00:25:36,200 MemCopyBreaker, 392 00:25:36,300 --> 00:25:36,740 it's a... 393 00:25:37,440 --> 00:25:39,060 It's a breakpoint publisher, 394 00:25:39,160 --> 00:25:40,020 something else that I wrote. 395 00:25:41,500 --> 00:25:42,880 When writing a breakpoint, 396 00:25:43,800 --> 00:25:45,160 you simply overwrite 397 00:25:45,160 --> 00:25:46,640 the call to notify. 398 00:25:46,660 --> 00:25:46,800 So, 399 00:25:46,820 --> 00:25:46,940 notify, 400 00:25:46,940 --> 00:25:48,460 or the definition of notify. 401 00:25:48,720 --> 00:25:49,600 Notify gives you 402 00:25:49,600 --> 00:25:51,420 a pointer to the breakpoint, 403 00:25:51,600 --> 00:25:52,660 the event, 404 00:25:53,400 --> 00:25:54,700 and the trace itself. 405 00:25:54,920 --> 00:25:56,360 So you can do all of your manipulation 406 00:25:56,360 --> 00:25:59,020 and investigation there. 407 00:25:59,520 --> 00:26:01,260 So just as an example, 408 00:26:01,460 --> 00:26:02,920 we pull in the EIP, 409 00:26:03,080 --> 00:26:03,780 the instruction pointer. 410 00:26:04,440 --> 00:26:05,860 We pull in the stack pointer 411 00:26:05,860 --> 00:26:06,600 and the base pointer. 412 00:26:08,940 --> 00:26:10,980 We use our special math skills 413 00:26:10,980 --> 00:26:12,560 to determine how long 414 00:26:12,560 --> 00:26:13,880 the copy is going to take. 415 00:26:15,700 --> 00:26:16,980 Pulling that off the stack too. 416 00:26:18,760 --> 00:26:20,360 We find out how far away 417 00:26:20,360 --> 00:26:21,220 rep pointer is. 418 00:26:21,360 --> 00:26:23,220 We get the dust in the source. 419 00:26:24,500 --> 00:26:26,440 We check to see how long 420 00:26:26,440 --> 00:26:29,220 the destination buffer is. 421 00:26:30,400 --> 00:26:31,320 And then we compare. 422 00:26:33,100 --> 00:26:34,360 If there's going to be 423 00:26:34,360 --> 00:26:34,880 an overwrite, 424 00:26:35,380 --> 00:26:36,000 we alert. 425 00:26:37,200 --> 00:26:38,100 Pretty simple, huh? 426 00:26:41,680 --> 00:26:43,760 Except when attacking, 427 00:26:43,880 --> 00:26:44,740 certain libraries. 428 00:26:48,480 --> 00:26:51,560 Some subroutines don't start off 429 00:26:51,560 --> 00:26:53,200 by changing the base pointer. 430 00:26:54,380 --> 00:26:56,020 They use that base pointer for, 431 00:26:56,220 --> 00:26:57,260 well, whatever else 432 00:26:57,260 --> 00:26:58,020 they want to use it for. 433 00:26:58,820 --> 00:27:00,440 So measuring to the base pointer 434 00:27:00,440 --> 00:27:01,380 is not going to get us there. 435 00:27:01,600 --> 00:27:02,460 So what do we do? 436 00:27:02,660 --> 00:27:03,800 How do we overcome this? 437 00:27:06,580 --> 00:27:07,940 Well, there are several options. 438 00:27:09,060 --> 00:27:10,160 The one at the bottom, 439 00:27:10,220 --> 00:27:11,600 if you hate reading the whole book, 440 00:27:11,720 --> 00:27:12,420 the one at the bottom 441 00:27:12,420 --> 00:27:13,100 is the one that I found 442 00:27:14,600 --> 00:27:16,840 But you can, 443 00:27:17,140 --> 00:27:18,900 and since this is my background, 444 00:27:19,140 --> 00:27:20,040 this is the first thing 445 00:27:20,040 --> 00:27:20,620 that came to mind, 446 00:27:20,740 --> 00:27:22,400 you can disassemble the whole code 447 00:27:22,400 --> 00:27:25,560 and use that to intelligently 448 00:27:25,560 --> 00:27:28,180 inform your tracker. 449 00:27:29,600 --> 00:27:30,680 It's kind of ugly. 450 00:27:31,240 --> 00:27:32,480 Most of those solutions, 451 00:27:32,640 --> 00:27:34,980 you look for stack setup 452 00:27:34,980 --> 00:27:37,900 throughout the subroutine, 453 00:27:38,100 --> 00:27:38,880 unfortunately. 454 00:27:39,020 --> 00:27:40,740 Well, for spaghetti code, 455 00:27:40,900 --> 00:27:42,520 especially code that 456 00:27:43,700 --> 00:27:44,160 yeah, 457 00:27:45,800 --> 00:27:48,880 code that could be jumping all over 458 00:27:49,400 --> 00:27:51,340 based on input that you don't know about, 459 00:27:51,920 --> 00:27:53,040 this can get really ugly. 460 00:27:53,220 --> 00:27:55,760 We can also take the bottom. 461 00:27:55,880 --> 00:27:56,820 Just before returning, 462 00:27:57,360 --> 00:27:58,980 you've got to clean up all that stack. 463 00:27:59,500 --> 00:28:00,400 So oftentimes, 464 00:28:00,660 --> 00:28:02,460 there are opcodes that will tip their hat 465 00:28:02,460 --> 00:28:06,360 into how big a space you're living in. 466 00:28:06,500 --> 00:28:07,960 These are all very rough estimates, 467 00:28:08,160 --> 00:28:09,600 but they get you a start. 468 00:28:11,060 --> 00:28:13,940 What I actually found to be the best case 469 00:28:13,940 --> 00:28:16,380 in my exercises 470 00:28:17,920 --> 00:28:19,760 was stack backtracing. 471 00:28:20,700 --> 00:28:23,300 The idea is you start at your stack pointer 472 00:28:23,300 --> 00:28:23,820 because, well, 473 00:28:23,840 --> 00:28:25,660 your stack pointer is your stack pointer. 474 00:28:25,820 --> 00:28:27,220 They don't use that to, 475 00:28:27,220 --> 00:28:27,740 you know, 476 00:28:27,740 --> 00:28:28,940 plot stuff all over 477 00:28:28,940 --> 00:28:30,360 and use it as a general register. 478 00:28:31,000 --> 00:28:32,000 Your stack pointer, 479 00:28:33,340 --> 00:28:34,140 you then, 480 00:28:34,700 --> 00:28:36,100 you start at the stack pointer 481 00:28:36,100 --> 00:28:38,500 and you back up four bytes 482 00:28:39,220 --> 00:28:40,460 take out that four byte, 483 00:28:40,580 --> 00:28:40,780 what? 484 00:28:40,860 --> 00:28:41,180 What's that? 485 00:28:41,480 --> 00:28:42,280 How many bits? 486 00:28:44,460 --> 00:28:46,320 You turn that 32 bit, 487 00:28:46,340 --> 00:28:48,500 that 32 bits into a number 488 00:28:48,500 --> 00:28:50,480 and compare it against your memory maps 489 00:28:50,480 --> 00:28:52,640 because your memory maps define to the computer 490 00:28:53,440 --> 00:28:55,400 what valid memory space looks like. 491 00:28:55,860 --> 00:28:57,900 If you find that that four bytes 492 00:28:57,900 --> 00:29:00,320 actually points to a legitimate memory space, 493 00:29:01,220 --> 00:29:03,280 you can then turn it around 494 00:29:03,280 --> 00:29:05,460 and do more analysis such as, 495 00:29:05,640 --> 00:29:06,260 well, 496 00:29:06,940 --> 00:29:07,980 stuff on this slide. 497 00:29:10,280 --> 00:29:11,960 So if the 32 bit number 498 00:29:11,960 --> 00:29:13,480 that you've just reached 499 00:29:13,480 --> 00:29:14,780 is part of maps, 500 00:29:15,060 --> 00:29:16,040 the memory maps, 501 00:29:17,280 --> 00:29:18,680 we can then back up 502 00:29:18,680 --> 00:29:21,160 and it's not totally trivial, 503 00:29:21,200 --> 00:29:22,500 but it's not that hard 504 00:29:22,500 --> 00:29:24,340 to back up from that location 505 00:29:24,340 --> 00:29:27,020 and see if the previous opcode was a call. 506 00:29:27,740 --> 00:29:29,500 I say it's not easy because, well, 507 00:29:29,580 --> 00:29:31,340 if any of you have gotten into 508 00:29:33,280 --> 00:29:34,240 opcode disassembly, 509 00:29:34,260 --> 00:29:35,420 you'll recognize that, 510 00:29:35,480 --> 00:29:36,420 well, you can have a 511 00:29:36,420 --> 00:29:37,780 two byte opcode, 512 00:29:37,780 --> 00:29:40,460 that performs a call or a jump. 513 00:29:40,580 --> 00:29:41,800 You can have a, 514 00:29:41,940 --> 00:29:44,000 well, several other variations 515 00:29:44,000 --> 00:29:45,900 that get really nasty. 516 00:29:46,160 --> 00:29:49,040 So we just stuck them all in a list 517 00:29:49,040 --> 00:29:50,160 and we checked for all of them. 518 00:29:54,200 --> 00:29:55,440 And then ultimately, 519 00:29:55,680 --> 00:29:58,480 the more testing we choose to do, 520 00:29:58,600 --> 00:30:00,460 the slower the program goes 521 00:30:00,460 --> 00:30:03,480 that you're executing 522 00:30:03,480 --> 00:30:06,120 and the more accurate you become 523 00:30:06,120 --> 00:30:08,160 with your alerts. 524 00:30:09,380 --> 00:30:10,340 When hacking, 525 00:30:10,500 --> 00:30:12,580 I often tell friends, 526 00:30:12,720 --> 00:30:13,720 students, whomever, 527 00:30:13,800 --> 00:30:14,760 when hacking, 528 00:30:14,900 --> 00:30:16,460 you are iterative 529 00:30:17,120 --> 00:30:18,620 and not afraid of failure. 530 00:30:19,740 --> 00:30:20,300 Why? 531 00:30:20,560 --> 00:30:22,060 Because it only takes one win 532 00:30:22,060 --> 00:30:24,580 to make a thousand failures worthwhile. 533 00:30:26,640 --> 00:30:27,540 Who was it? 534 00:30:28,140 --> 00:30:28,700 Charlie. 535 00:30:29,060 --> 00:30:31,960 Charlie, yeah, Charlie Miller. 536 00:30:33,520 --> 00:30:35,080 Recently, not too long ago, 537 00:30:35,080 --> 00:30:36,660 posted on his blog 538 00:30:36,660 --> 00:30:38,080 a check from the government 539 00:30:38,080 --> 00:30:38,920 for $80,000 540 00:30:38,920 --> 00:30:40,120 for one O-Day 541 00:30:40,120 --> 00:30:41,040 that he sold them. 542 00:30:43,060 --> 00:30:44,060 I think that's worth 543 00:30:44,060 --> 00:30:45,080 a few failures, don't you? 544 00:30:46,380 --> 00:30:47,520 So what we're trying to do 545 00:30:47,920 --> 00:30:48,980 is limit the scope 546 00:30:48,980 --> 00:30:52,000 of how much we have to look at 547 00:30:52,000 --> 00:30:54,280 to see if we can overflow a buffer 548 00:30:54,280 --> 00:30:56,060 or find some other vulnerability. 549 00:31:00,300 --> 00:31:03,100 And here's the majority of code 550 00:31:03,100 --> 00:31:03,560 for, 551 00:31:03,560 --> 00:31:05,520 for part of the tool 552 00:31:05,520 --> 00:31:06,620 that I call FindRat. 553 00:31:07,340 --> 00:31:08,860 So we start at the stack pointer 554 00:31:09,400 --> 00:31:10,880 and we continue 555 00:31:12,400 --> 00:31:14,340 until we meet all those criteria 556 00:31:14,340 --> 00:31:15,160 that I just listed. 557 00:31:16,780 --> 00:31:17,180 Again, 558 00:31:17,300 --> 00:31:19,020 the final tool is 559 00:31:19,500 --> 00:31:20,440 in the Atlas Utils. 560 00:31:20,440 --> 00:31:21,320 You can take a look at it later. 561 00:31:21,720 --> 00:31:22,140 And hey, 562 00:31:22,300 --> 00:31:23,400 it's not perfect. 563 00:31:23,540 --> 00:31:24,280 I'm not perfect. 564 00:31:24,520 --> 00:31:25,860 I'm not the best person in the world. 565 00:31:25,940 --> 00:31:27,220 I just love to do fun stuff. 566 00:31:27,660 --> 00:31:28,580 Send me bug reports. 567 00:31:29,060 --> 00:31:29,620 Better yet, 568 00:31:29,700 --> 00:31:30,380 send me improvements. 569 00:31:30,980 --> 00:31:31,660 That's awesome. 570 00:31:31,800 --> 00:31:32,860 I will love to include you 571 00:31:32,860 --> 00:31:33,060 and, 572 00:31:33,580 --> 00:31:34,640 and list your name 573 00:31:34,640 --> 00:31:34,980 as, 574 00:31:35,040 --> 00:31:35,660 as a contributor. 575 00:31:37,820 --> 00:31:38,260 FindNextHeap, 576 00:31:38,340 --> 00:31:38,920 this uses, 577 00:31:39,560 --> 00:31:40,200 oh wait, 578 00:31:40,240 --> 00:31:40,360 wait, 579 00:31:40,440 --> 00:31:40,740 sorry. 580 00:31:42,000 --> 00:31:44,940 What if we have a heap buffer? 581 00:31:45,380 --> 00:31:46,020 How do we, 582 00:31:46,080 --> 00:31:46,760 what do we do then? 583 00:31:46,900 --> 00:31:47,580 Remember I said 584 00:31:47,580 --> 00:31:49,360 only in the most ideal situation 585 00:31:49,360 --> 00:31:51,500 do you have that magic length 586 00:31:51,960 --> 00:31:52,820 just before your, 587 00:31:52,880 --> 00:31:53,560 your buffer pointer. 588 00:31:53,660 --> 00:31:54,300 What about a struct? 589 00:31:54,740 --> 00:31:55,900 If you've got a structure, 590 00:31:56,820 --> 00:31:58,200 you may be memcopying 591 00:31:58,200 --> 00:31:59,540 in and out of that structure, 592 00:31:59,980 --> 00:32:00,580 you know, 593 00:32:00,580 --> 00:32:01,260 20 times 594 00:32:01,260 --> 00:32:02,420 and never hit 595 00:32:02,860 --> 00:32:03,440 the actual base 596 00:32:03,440 --> 00:32:04,040 of the structure. 597 00:32:04,820 --> 00:32:05,340 Well, 598 00:32:05,400 --> 00:32:06,360 because you're accessing 599 00:32:06,360 --> 00:32:07,200 its elements. 600 00:32:07,500 --> 00:32:07,840 So you, 601 00:32:07,920 --> 00:32:09,480 if you try to take a length 602 00:32:09,480 --> 00:32:10,600 from the four bytes 603 00:32:11,140 --> 00:32:13,040 previous to the buffer pointer, 604 00:32:13,720 --> 00:32:15,000 you're probably gonna run into 605 00:32:15,000 --> 00:32:16,260 other areas of the structure. 606 00:32:18,880 --> 00:32:19,400 Ooh, 607 00:32:19,620 --> 00:32:20,140 that's toughy. 608 00:32:20,300 --> 00:32:20,720 So, 609 00:32:20,780 --> 00:32:22,060 how do we overcome that one? 610 00:32:23,400 --> 00:32:23,920 Well, 611 00:32:24,040 --> 00:32:24,380 we, 612 00:32:24,500 --> 00:32:25,700 we write a tool called 613 00:32:26,400 --> 00:32:26,920 FindNextHeap. 614 00:32:30,140 --> 00:32:31,740 And get connected chain. 615 00:32:32,320 --> 00:32:32,840 FindNextHeap 616 00:32:32,860 --> 00:32:33,160 says, 617 00:32:33,560 --> 00:32:33,920 all right, 618 00:32:33,940 --> 00:32:35,100 I have this address. 619 00:32:35,300 --> 00:32:36,380 I need to know 620 00:32:36,380 --> 00:32:37,820 what kind of, 621 00:32:37,860 --> 00:32:39,440 or what the boundaries 622 00:32:39,440 --> 00:32:40,760 of this heap chunk 623 00:32:40,760 --> 00:32:41,600 that I'm in are. 624 00:32:41,880 --> 00:32:42,200 Okay, 625 00:32:42,240 --> 00:32:43,540 so I create a buffer 626 00:32:43,540 --> 00:32:44,840 of a thousand bytes, 627 00:32:45,020 --> 00:32:45,920 but I'm really only 628 00:32:46,740 --> 00:32:47,780 interested in, 629 00:32:47,940 --> 00:32:49,820 or I'm writing to four bytes 630 00:32:49,820 --> 00:32:50,500 in the middle of them. 631 00:32:51,020 --> 00:32:52,160 Or a hundred bytes 632 00:32:52,160 --> 00:32:53,180 near the end. 633 00:32:53,720 --> 00:32:54,240 Whatever. 634 00:32:56,140 --> 00:32:57,760 So we start at the beginning 635 00:32:57,760 --> 00:32:59,300 of heap memory. 636 00:33:00,920 --> 00:33:02,140 And we traverse 637 00:33:02,820 --> 00:33:04,100 through a linked list 638 00:33:05,140 --> 00:33:05,540 because, 639 00:33:05,700 --> 00:33:06,060 well, 640 00:33:06,200 --> 00:33:06,780 it's, 641 00:33:06,820 --> 00:33:07,800 it's a big chunk of memory 642 00:33:07,800 --> 00:33:08,680 but we have to keep track 643 00:33:08,680 --> 00:33:09,220 of it somehow, 644 00:33:09,340 --> 00:33:09,500 right? 645 00:33:10,280 --> 00:33:11,660 So we've got this huge chunk 646 00:33:11,660 --> 00:33:12,080 of memory 647 00:33:12,080 --> 00:33:14,300 and we create little allocations 648 00:33:14,300 --> 00:33:15,280 and they are linked. 649 00:33:15,720 --> 00:33:17,100 So we can walk through 650 00:33:18,060 --> 00:33:19,860 all the heap allocations 651 00:33:22,220 --> 00:33:23,420 by starting at the base. 652 00:33:24,100 --> 00:33:25,160 So we start at the base 653 00:33:25,740 --> 00:33:27,360 and we go through the range 654 00:33:27,360 --> 00:33:28,140 and when, 655 00:33:28,980 --> 00:33:29,220 well, 656 00:33:29,220 --> 00:33:30,420 we meet certain criteria 657 00:33:30,420 --> 00:33:31,760 to say that we're at the end, 658 00:33:31,760 --> 00:33:33,040 we stop. 659 00:33:39,860 --> 00:33:40,460 When, 660 00:33:41,620 --> 00:33:43,200 when we identify that 661 00:33:45,700 --> 00:33:47,560 we've now traversed beyond 662 00:33:47,560 --> 00:33:50,240 the address that I first handed in, 663 00:33:50,840 --> 00:33:51,680 we stop. 664 00:33:54,140 --> 00:33:56,220 We keep track of the address 665 00:33:56,220 --> 00:33:56,960 that we had before 666 00:33:56,960 --> 00:33:59,320 and we identify the actual chunk. 667 00:34:01,140 --> 00:34:01,740 Well, 668 00:34:04,560 --> 00:34:05,700 it gets me how long 669 00:34:05,700 --> 00:34:06,180 I've got to go 670 00:34:06,180 --> 00:34:07,300 to overwrite the next buffer. 671 00:34:08,560 --> 00:34:09,440 Why is that important? 672 00:34:09,680 --> 00:34:09,820 Well, 673 00:34:09,880 --> 00:34:10,740 the first couple of bytes 674 00:34:10,740 --> 00:34:11,500 on the next buffer 675 00:34:11,500 --> 00:34:13,900 can be sometimes useful. 676 00:34:16,300 --> 00:34:17,940 It serves as a good stop 677 00:34:20,480 --> 00:34:21,600 and often means 678 00:34:21,600 --> 00:34:22,580 that I've already overwritten 679 00:34:22,580 --> 00:34:23,480 something of value. 680 00:34:23,940 --> 00:34:25,080 And sometimes it means 681 00:34:25,080 --> 00:34:26,120 that I'll just crash the system 682 00:34:26,120 --> 00:34:26,900 because it's got a canary. 683 00:34:26,960 --> 00:34:27,920 But those are the things 684 00:34:27,920 --> 00:34:28,660 you've got to find out. 685 00:34:30,920 --> 00:34:31,720 For any of you 686 00:34:31,720 --> 00:34:33,540 Another example, strcpy. 687 00:34:35,240 --> 00:34:38,140 See how well my cliffhanger abilities have maintained. 688 00:34:40,000 --> 00:34:40,480 Strcpy. 689 00:34:40,640 --> 00:34:43,860 Well, strcpy is a great tool, right? 690 00:34:45,700 --> 00:34:46,680 If you're a hacker. 691 00:34:49,460 --> 00:34:50,820 What does strcpy mean? 692 00:34:51,440 --> 00:34:54,240 Well, it means I don't set how long of a copy it is. 693 00:34:54,580 --> 00:35:01,180 I simply say, okay, take this string and, well, copy it as long as you don't run into a null byte. 694 00:35:02,140 --> 00:35:04,900 Copy that over here into this other buffer, right? 695 00:35:05,840 --> 00:35:09,120 Well, what if that other buffer is 14 bytes long, 696 00:35:09,300 --> 00:35:13,680 and the original string was supposed to be 7 bytes long, 697 00:35:13,860 --> 00:35:18,580 but, well, the coder didn't really check much, and it turned out to be 30 bytes long. 698 00:35:19,300 --> 00:35:20,660 We now have an overwrite. 699 00:35:22,460 --> 00:35:31,340 There are other more secure ways of doing strcpy. 700 00:35:31,800 --> 00:35:33,100 Such as strcpy. 701 00:35:33,880 --> 00:35:40,940 Strcpy takes a little bit of memcpy, wraps it into strcpy, and says, okay, copy until we get to a null byte. 702 00:35:41,420 --> 00:35:42,780 Because that's what terminates the string. 703 00:35:42,980 --> 00:35:48,520 Oh, by the way, if you go over 100 bytes or whatever number your programmer sets in, stop there. 704 00:35:49,200 --> 00:35:49,820 Cool, right? 705 00:35:49,940 --> 00:35:53,380 It was supposed to get rid of all buffer overflows having to do with string exception. 706 00:35:55,120 --> 00:35:55,520 Strcpy. 707 00:35:55,680 --> 00:35:55,860 Strcpy. 708 00:35:56,960 --> 00:36:01,400 Problem is, just like everywhere else in security, 709 00:36:02,220 --> 00:36:07,880 if you fail to understand and implement a given security feature 710 00:36:09,520 --> 00:36:12,140 without considering its strengths and its weaknesses, 711 00:36:13,300 --> 00:36:14,420 it's just another tool. 712 00:36:15,320 --> 00:36:15,760 Right? 713 00:36:20,780 --> 00:36:23,420 Strcpy, we check the destination pointer and the source pointer. 714 00:36:23,520 --> 00:36:28,100 We do the same calculations that we talked about for memcpy by getting a size. 715 00:36:28,220 --> 00:36:29,860 How long do we need to make this overwrite? 716 00:36:29,960 --> 00:36:30,900 And, by the way, 717 00:36:30,940 --> 00:36:31,700 did it make it? 718 00:36:33,280 --> 00:36:35,080 Write your code, find out. 719 00:36:36,800 --> 00:36:37,600 Stern copy? 720 00:36:38,960 --> 00:36:43,560 Well, we do desk pointer and source pointer, but then we take size 721 00:36:43,560 --> 00:36:46,840 and we make sure that it limits it appropriately. 722 00:36:47,300 --> 00:36:48,540 Now, why is that important? 723 00:36:49,860 --> 00:36:58,520 Because the fact that my string, my source string is only five bytes long and I'm copying 724 00:36:58,520 --> 00:37:01,660 into a 20 byte buffer doesn't matter. 725 00:37:01,700 --> 00:37:05,220 That means you missed a vulnerability that time. 726 00:37:05,360 --> 00:37:05,900 Not a problem. 727 00:37:06,060 --> 00:37:06,360 Right? 728 00:37:06,440 --> 00:37:07,320 What about the next time? 729 00:37:07,480 --> 00:37:09,240 What if that string grows without bounds? 730 00:37:15,420 --> 00:37:20,040 Strcat, very similar to string copy, only you start at the end of the first string 731 00:37:20,040 --> 00:37:21,280 and append onto it. 732 00:37:22,260 --> 00:37:24,320 It's pretty difficult to get this one right. 733 00:37:25,600 --> 00:37:30,400 So, as soon as you run into Strcat, you probably just want to go look at it. 734 00:37:30,580 --> 00:37:31,500 So, so. 735 00:37:31,500 --> 00:37:35,700 So Sturcat, at least one point in the code, just kind of alerted, hey, I got Sturcat here. 736 00:37:35,760 --> 00:37:36,700 You want to come check it out? 737 00:37:40,280 --> 00:37:42,360 Enough about buffer overflows, though. 738 00:37:43,060 --> 00:37:44,020 That's old hat. 739 00:37:47,820 --> 00:37:49,220 What about format string exceptions? 740 00:37:50,420 --> 00:38:02,220 Is there a way that we can tell if we have a format string that can be provided by a user or somebody across the network? 741 00:38:04,160 --> 00:38:06,640 Not perfectly, but there are some hints. 742 00:38:07,080 --> 00:38:13,840 If you break on printf, for example, you check for your format string, ESP plus 8. 743 00:38:14,100 --> 00:38:18,140 It's the first parameter handed into printf. 744 00:38:19,220 --> 00:38:23,860 The other printf-like functions have their own location because they take different parameters. 745 00:38:27,460 --> 00:38:32,400 When we get the address of our format string, remember, it is always a format string, 746 00:38:32,500 --> 00:38:38,780 whether it's provided by the coder or by the user on the other end of the network in some foreign land that hates us. 747 00:38:40,900 --> 00:38:42,000 Where does it live? 748 00:38:43,720 --> 00:38:49,200 Now, shortly after I started writing this talk, I ran into some questions. 749 00:38:49,220 --> 00:38:59,780 Some folks from a vendor, which I will not mention, but they did very well on Ed's malware endpoint security product bake-off, I think it was. 750 00:39:01,760 --> 00:39:02,920 So go check them out. 751 00:39:03,720 --> 00:39:05,820 They were talking about the exact same thing. 752 00:39:05,980 --> 00:39:16,780 When they get certain system calls, they trace back, much like what we were talking about in here, and check to see, okay, where did the system call come from? 753 00:39:16,820 --> 00:39:17,540 Did it come from the heap? 754 00:39:19,640 --> 00:39:20,660 Let's stop it. 755 00:39:20,760 --> 00:39:22,780 Oh, crap, Lotus Notes just broke. 756 00:39:23,340 --> 00:39:24,740 Well, something like that. 757 00:39:25,200 --> 00:39:27,480 So, did it come from the stack? 758 00:39:27,980 --> 00:39:34,460 Typically, you don't want to be making a, eh, this is all imperfect. 759 00:39:34,840 --> 00:39:39,440 Okay, I'm only giving you options to investigate and trying to narrow them down. 760 00:39:39,920 --> 00:39:47,480 Yes, you may create a format string on the stack using various tools or other function calls. 761 00:39:48,140 --> 00:39:49,060 Most often. 762 00:39:49,220 --> 00:40:02,020 If you see a format string coming from the stack, however, you want to investigate, because this should normally be something that at least originates in write-only, or read-only memory. 763 00:40:07,400 --> 00:40:08,900 Sprintf, some more fun stuff. 764 00:40:09,020 --> 00:40:11,860 It just changes the locations pretty much very similar. 765 00:40:16,320 --> 00:40:17,680 Snprintf, hey, this is cool. 766 00:40:17,740 --> 00:40:19,040 We can limit that length. 767 00:40:19,220 --> 00:40:19,640 A little bit. 768 00:40:20,180 --> 00:40:26,100 So, at least we're not creating buffer overflows, hopefully. 769 00:40:26,840 --> 00:40:28,140 Well, you want to find out? 770 00:40:29,560 --> 00:40:32,980 Programmatic debugging will allow you to help, or will help you. 771 00:40:33,120 --> 00:40:33,460 Sorry. 772 00:40:36,880 --> 00:40:38,040 There are various others. 773 00:40:38,740 --> 00:40:39,180 Scanf. 774 00:40:40,180 --> 00:40:41,320 How am I doing on time? 775 00:40:42,720 --> 00:40:44,200 Yeah, that's kind of what I was thinking. 776 00:40:44,300 --> 00:40:45,400 That's why I'm blowing through these. 777 00:40:47,000 --> 00:40:47,680 Get S. 778 00:40:48,680 --> 00:40:54,100 I had one written, and the notify was, oh, my gosh, I have Get S running. 779 00:40:55,100 --> 00:40:59,500 The man page for Get S says, do not ever use Get S. 780 00:41:01,440 --> 00:41:02,160 Gee, thanks. 781 00:41:02,220 --> 00:41:02,760 I like to write it. 782 00:41:02,800 --> 00:41:04,020 Well, they wrote it at a different time. 783 00:41:04,100 --> 00:41:06,800 But do not ever use Get S. 784 00:41:06,840 --> 00:41:07,000 Why? 785 00:41:07,200 --> 00:41:15,260 Well, because it will just sit there and happily feed bytes into, well, wherever you tell it to go. 786 00:41:15,980 --> 00:41:16,840 This is a problem. 787 00:41:17,680 --> 00:41:18,260 This is a good thing. 788 00:41:20,640 --> 00:41:21,680 I'll let you decide. 789 00:41:23,720 --> 00:41:24,480 Get C. 790 00:41:24,700 --> 00:41:26,280 Get C gets one byte. 791 00:41:26,660 --> 00:41:27,180 Okay? 792 00:41:27,240 --> 00:41:29,180 How can I do a buffer overflow with one byte? 793 00:41:30,540 --> 00:41:37,140 Well, oftentimes, Get C is called with a loop around it set to some number. 794 00:41:37,380 --> 00:41:44,580 Loops in assembly code are typically wrapped around the ECX register or RCX, depending on your platform. 795 00:41:47,680 --> 00:41:57,220 So it's probably a good idea just to go find the Get C function, wherever it was called from, look at the logic around it, look for the loops, see how you can manipulate them that way. 796 00:42:01,840 --> 00:42:02,320 Memchar. 797 00:42:03,160 --> 00:42:04,200 I'm not even going to go into. 798 00:42:05,220 --> 00:42:17,280 If you would like to be really elite, you start looking at assembly opcodes in the program itself, such as RepStow, RepMove. 799 00:42:19,080 --> 00:42:21,020 Rep is an opcode modifier. 800 00:42:21,340 --> 00:42:33,320 It can be wrapped around many different opcodes, and it says, okay, as long as ECX is greater than zero, and I decrement it every time, hey, that kind of sounds like a for loop, right? 801 00:42:35,120 --> 00:42:38,040 We then do this opcode. 802 00:42:38,200 --> 00:42:41,720 So maybe it's storing data into a location. 803 00:42:42,020 --> 00:42:43,840 Maybe it's moving it into a location. 804 00:42:44,400 --> 00:42:45,080 Either way. 805 00:42:45,760 --> 00:42:47,660 By seeking out in the disassembly. 806 00:42:49,060 --> 00:42:56,500 Any rep byte operations, you will often find another loop that you can then investigate. 807 00:42:56,720 --> 00:43:01,800 Not a whole lot I can do for you in programmatic debugging on a grand scale. 808 00:43:03,400 --> 00:43:10,320 However, you can create a template that you can simply say, okay, stop at this point and start tracking. 809 00:43:10,760 --> 00:43:12,080 Tell me what ECX is. 810 00:43:12,200 --> 00:43:12,800 Things like that. 811 00:43:12,900 --> 00:43:17,020 It's a little bit more focused than everything else I've been talking about, but it still helps and it works. 812 00:43:20,100 --> 00:43:21,740 Reminder of how format strings work. 813 00:43:25,180 --> 00:43:27,160 And here we are to the most important part of the presentation. 814 00:43:28,660 --> 00:43:29,660 What else can we do? 815 00:43:31,160 --> 00:43:32,160 I'll leave that up to you. 816 00:43:34,080 --> 00:43:36,520 The force moves strong in your family. 817 00:43:37,060 --> 00:43:38,980 Pass on what you have learned. 818 00:43:40,520 --> 00:43:41,380 Go and be fruitful. 819 00:43:44,000 --> 00:43:46,640 And I'm looking forward to seeing you guys up here next year. 820 00:43:47,900 --> 00:43:58,120 Many thanks to my God, my wife, my kids, my friends, Last Place, Visigoth, and Kinshoto, Squires, and all the bullets that they shot at me. 821 00:43:59,760 --> 00:44:02,100 Anybody want some of this raw energy drink? 822 00:44:03,500 --> 00:44:08,400 The great folk over at the OCTF, is that what it's called? 823 00:44:09,740 --> 00:44:11,400 Thank you very much to Moose. 824 00:44:11,860 --> 00:44:15,640 Moose comes up to me and says, dude, we've got to get you some Brondo. 825 00:44:19,040 --> 00:44:20,100 What the heck is that? 826 00:44:20,340 --> 00:44:31,960 He says, well, it's like Mountain Dew with echinacea and whatever, all sorts of nutrients or whatever to help you win CTF. 827 00:44:35,040 --> 00:44:35,440 Electrolytes. 828 00:44:35,560 --> 00:44:35,880 There we go. 829 00:44:35,920 --> 00:44:36,240 Thank you. 830 00:44:36,600 --> 00:44:37,600 All right, come get one. 831 00:44:38,800 --> 00:44:39,780 Thank you all very much. 832 00:44:39,780 --> 00:44:40,320 Have a great day.