1 00:00:00,000 --> 00:00:05,100 They told me that I couldn't have a wireless mic up here because apparently they're afraid I'm going to hack it. 2 00:00:06,200 --> 00:00:07,060 I don't understand. 3 00:00:07,440 --> 00:00:13,100 You know that you're at a very interesting conference when you walk around and Bluetooth pops up. 4 00:00:13,180 --> 00:00:17,320 See, I've got a Bluetooth tool that just pops up everything that it runs into. 5 00:00:17,660 --> 00:00:19,540 It's almost like a denial of service sometimes. 6 00:00:20,200 --> 00:00:27,480 You know that you're at an interesting conference when you get names like White Witch or... 7 00:00:27,480 --> 00:00:28,860 Oh, here's a good one. 8 00:00:29,000 --> 00:00:29,760 Nut Cheese. 9 00:00:32,840 --> 00:00:37,240 And yet I'm still amazed at the sheer numbers of people who forget to turn off their Bluetooth. 10 00:00:38,520 --> 00:00:39,500 At DEF CON. 11 00:00:40,860 --> 00:00:42,440 But that's not why you came to hear me today. 12 00:00:42,820 --> 00:00:45,500 But I'm giving people a little bit of time to walk in. 13 00:00:46,840 --> 00:00:47,980 Hello, my name is Atlas. 14 00:00:50,180 --> 00:00:51,540 I like what I do. 15 00:00:51,900 --> 00:00:52,960 And I do a lot of it. 16 00:00:55,400 --> 00:00:56,780 A while back... 17 00:00:56,780 --> 00:00:57,380 I'm in a... 18 00:00:57,380 --> 00:00:59,620 I'm in a subway in D.C. with a friend of mine. 19 00:01:00,380 --> 00:01:03,280 And I forgot to ask his permission, so I can't say who it was. 20 00:01:03,720 --> 00:01:08,680 But this friend is very well known by most of you, I guarantee it. 21 00:01:09,880 --> 00:01:11,200 And he is... 22 00:01:12,560 --> 00:01:16,880 He's the cream of the crop in security and in the hacker arena. 23 00:01:17,660 --> 00:01:19,680 Well, anyway, this guy is talking to me. 24 00:01:19,740 --> 00:01:24,840 And he says, Atlas, explain to me how buffer overflows work. 25 00:01:24,980 --> 00:01:26,800 Because I'm just a little fuzzy. 26 00:01:26,800 --> 00:01:30,640 I've read a little bit about it, but I just don't have it. 27 00:01:30,720 --> 00:01:32,440 And I told him an answer. 28 00:01:32,500 --> 00:01:39,560 But I thought to myself, if this guy needs a refresher course on heat buffer overflows, 29 00:01:41,480 --> 00:01:43,280 I think I should probably... 30 00:01:44,580 --> 00:01:46,160 I thought they were going to provide me music. 31 00:01:47,120 --> 00:01:51,240 I said, hey, there's probably a lot of other people out there that don't quite remember how they work 32 00:01:51,240 --> 00:01:53,440 or just never knew, and that's cool, too. 33 00:01:53,640 --> 00:01:55,300 They're actually kind of esoteric. 34 00:02:00,020 --> 00:02:00,540 Clicker? 35 00:02:03,940 --> 00:02:05,200 Is it Bluetooth? 36 00:02:05,640 --> 00:02:06,000 No. 37 00:02:08,120 --> 00:02:08,640 Great. 38 00:02:10,200 --> 00:02:11,320 God, I love that. 39 00:02:12,160 --> 00:02:13,920 Wow, this could be very interesting. 40 00:02:14,540 --> 00:02:15,280 There we go. 41 00:02:15,880 --> 00:02:16,920 It works again. 42 00:02:17,480 --> 00:02:19,840 So this is remedial heat buffer overflows. 43 00:02:20,360 --> 00:02:25,140 Basically, that means I understand most of you guys are elite enough to have done this before 44 00:02:25,140 --> 00:02:26,720 or at least... 45 00:02:26,720 --> 00:02:26,780 I don't know. 46 00:02:26,800 --> 00:02:28,040 Broached the subject before. 47 00:02:28,260 --> 00:02:29,300 So this is for you. 48 00:02:29,520 --> 00:02:35,740 This is a talk about how to exploit buffer overflow vulnerability 49 00:02:35,740 --> 00:02:41,900 specifically with a heap and a DLMalloc heap structure in place. 50 00:02:42,540 --> 00:02:43,840 We don't have a lot of time. 51 00:02:44,020 --> 00:02:45,000 Sorry about that. 52 00:02:45,620 --> 00:02:48,160 In the man page for the getS function, 53 00:02:50,440 --> 00:02:52,380 there is an exceptional note. 54 00:02:52,660 --> 00:02:55,980 It says, never use getS. 55 00:02:56,800 --> 00:02:59,800 Because it is impossible to tell without knowing the data in advance 56 00:02:59,800 --> 00:03:01,700 how many characters getS will read. 57 00:03:03,920 --> 00:03:08,140 Basically, don't use getS because we're in C now. 58 00:03:09,660 --> 00:03:12,840 In C, you have to tell it, much like assembly. 59 00:03:13,320 --> 00:03:15,600 This is how much room I want to give you. 60 00:03:15,720 --> 00:03:18,160 And getS just says, whoo, starting point. 61 00:03:18,260 --> 00:03:18,840 Let's just race. 62 00:03:19,360 --> 00:03:22,040 Da-na-na-na-na-na-na, looking for a zero. 63 00:03:22,040 --> 00:03:23,600 Da-na-na-na-na-na-na. 64 00:03:24,280 --> 00:03:26,820 And you hope that it's still within your buffer. 65 00:03:28,700 --> 00:03:30,500 But if it's not, who cares? 66 00:03:30,820 --> 00:03:31,680 You get a little crash. 67 00:03:32,060 --> 00:03:32,900 Not a big deal. 68 00:03:36,680 --> 00:03:38,660 F getS also has some issues. 69 00:03:38,820 --> 00:03:40,440 It is a step up from getS. 70 00:03:41,260 --> 00:03:43,540 There's a ton of C functions 71 00:03:43,540 --> 00:03:47,980 that give you all sorts of scary functionality. 72 00:03:48,820 --> 00:03:51,020 But the big ones are string copy, 73 00:03:51,400 --> 00:03:52,020 memory, 74 00:03:52,040 --> 00:03:52,620 mcopy, 75 00:03:54,140 --> 00:03:54,700 sscanf, 76 00:03:54,900 --> 00:03:58,520 or the scanf series or family of functions. 77 00:03:59,860 --> 00:04:01,380 One that a lot of people overlook 78 00:04:01,380 --> 00:04:04,320 is actually not a function in C at all. 79 00:04:05,260 --> 00:04:08,200 It's an assembly thing. 80 00:04:08,560 --> 00:04:12,300 In fact, it is the glue that makes all these functions work. 81 00:04:12,580 --> 00:04:16,100 And it is the moveS assembly opcode. 82 00:04:16,280 --> 00:04:17,060 Anybody heard of that? 83 00:04:18,280 --> 00:04:19,700 Yep, a lot of you. 84 00:04:20,500 --> 00:04:21,620 So moveS, 85 00:04:23,240 --> 00:04:26,940 takes a parameter or a prefix, 86 00:04:27,320 --> 00:04:28,240 says repeat. 87 00:04:28,560 --> 00:04:29,960 Hey, repeat this many times. 88 00:04:30,000 --> 00:04:31,180 Just move from here to here. 89 00:04:31,820 --> 00:04:32,440 That's pretty cool. 90 00:04:37,240 --> 00:04:38,780 How many of you are programmers 91 00:04:38,780 --> 00:04:40,760 or have written C programs 92 00:04:40,760 --> 00:04:42,160 or Python programs 93 00:04:42,160 --> 00:04:43,680 or any other language? 94 00:04:43,780 --> 00:04:44,360 Thank you. 95 00:04:44,440 --> 00:04:45,040 Excellent. 96 00:04:45,400 --> 00:04:46,040 Big, 97 00:04:47,360 --> 00:04:49,100 a large amount of you. 98 00:04:53,560 --> 00:04:55,980 You guys get to watch me walk up in front of the table 99 00:04:55,980 --> 00:04:58,020 because of the whole wireless thing. 100 00:04:58,580 --> 00:04:59,860 So if I fall off, 101 00:05:00,400 --> 00:05:02,140 please try to leave my badge alone 102 00:05:02,140 --> 00:05:03,420 and my wallet. 103 00:05:03,620 --> 00:05:04,600 You can take... 104 00:05:05,180 --> 00:05:06,760 Okay, no, I'm not going to give you permission either. 105 00:05:08,000 --> 00:05:08,900 When you're programming, 106 00:05:09,040 --> 00:05:10,600 what do you use to store information? 107 00:05:10,940 --> 00:05:12,060 You use a variable, right? 108 00:05:12,540 --> 00:05:12,940 Right? 109 00:05:14,400 --> 00:05:15,680 There are two types of variables 110 00:05:15,680 --> 00:05:17,780 in a C program. 111 00:05:18,120 --> 00:05:19,240 There are local variables. 112 00:05:22,040 --> 00:05:23,720 And those get stored on the stack. 113 00:05:24,780 --> 00:05:25,760 There are also 114 00:05:27,180 --> 00:05:29,100 dynamically allocated variables. 115 00:05:29,320 --> 00:05:29,940 In other words, 116 00:05:30,000 --> 00:05:32,200 I don't know at the time of writing 117 00:05:32,200 --> 00:05:34,820 how much data I'm going to need. 118 00:05:35,540 --> 00:05:37,800 So we set aside a huge chunk 119 00:05:37,800 --> 00:05:38,760 of virtual memory space 120 00:05:40,240 --> 00:05:41,280 and allow 121 00:05:42,540 --> 00:05:43,580 for a heap. 122 00:05:44,700 --> 00:05:45,580 Now, a heap 123 00:05:45,580 --> 00:05:48,180 is a very low-level function, 124 00:05:48,380 --> 00:05:51,120 but it is not operating system-specific. 125 00:05:52,680 --> 00:05:54,340 We'll get into that in just a second. 126 00:05:54,860 --> 00:05:56,900 The heap is dynamically allocated storage 127 00:05:58,640 --> 00:06:00,400 at whatever size you need. 128 00:06:02,240 --> 00:06:03,820 And it is... 129 00:06:03,820 --> 00:06:05,220 It can be 130 00:06:05,880 --> 00:06:07,200 very fun to secure. 131 00:06:09,560 --> 00:06:12,340 Who's heard of a stack-based buffer overflow before? 132 00:06:12,520 --> 00:06:15,040 In fact, who's heard of a buffer overflow at all? 133 00:06:15,240 --> 00:06:15,920 Show of hands. 134 00:06:16,200 --> 00:06:16,720 Excellent. 135 00:06:17,640 --> 00:06:19,380 How many of you know what it means? 136 00:06:21,380 --> 00:06:22,020 Thank you. 137 00:06:22,020 --> 00:06:22,280 Excellent. 138 00:06:23,140 --> 00:06:23,620 Good. 139 00:06:24,480 --> 00:06:26,460 Stack-based buffer overflows are beautiful. 140 00:06:27,480 --> 00:06:29,620 You start at one location 141 00:06:30,340 --> 00:06:31,520 and you meander. 142 00:06:32,480 --> 00:06:33,380 You meander. 143 00:06:33,560 --> 00:06:33,700 Oh! 144 00:06:33,800 --> 00:06:35,280 That byte needs to be right there, 145 00:06:35,380 --> 00:06:37,580 so you replace it very nicely for the program 146 00:06:37,580 --> 00:06:38,180 so it doesn't crash. 147 00:06:38,400 --> 00:06:39,020 Keep going, 148 00:06:39,180 --> 00:06:41,220 and you overwrite something of very big value. 149 00:06:41,700 --> 00:06:44,060 Oftentimes, this is the return pointer for your function. 150 00:06:45,040 --> 00:06:45,800 Heap, however, 151 00:06:45,840 --> 00:06:48,300 doesn't have the return pointer of the function 152 00:06:48,300 --> 00:06:49,440 stored in the heap. 153 00:06:52,040 --> 00:06:53,080 So what can I do? 154 00:06:53,560 --> 00:06:55,260 How do I take over a computer system 155 00:06:55,260 --> 00:06:57,040 using a heap buffer overflow? 156 00:06:59,060 --> 00:07:00,660 Well, before we get to that, 157 00:07:01,200 --> 00:07:02,860 I'd like to back up and say 158 00:07:03,580 --> 00:07:05,220 overflowing a buffer is valuable. 159 00:07:06,600 --> 00:07:07,760 Let me hear it, everybody. 160 00:07:08,680 --> 00:07:10,340 Overflowing a buffer is valuable. 161 00:07:10,580 --> 00:07:11,260 Why is that? 162 00:07:11,960 --> 00:07:12,420 . 163 00:07:13,420 --> 00:07:13,940 . 164 00:07:21,160 --> 00:07:21,680 Wow. 165 00:07:22,080 --> 00:07:22,600 Yes. 166 00:07:22,700 --> 00:07:23,440 Can I summarize? 167 00:07:23,740 --> 00:07:25,460 Because I don't remember everything you said. 168 00:07:25,520 --> 00:07:26,060 It was really good. 169 00:07:27,040 --> 00:07:29,080 But basically, there are several reasons 170 00:07:29,080 --> 00:07:30,760 that buffer overflows are great. 171 00:07:30,860 --> 00:07:33,520 You can stop an entire machine, 172 00:07:33,620 --> 00:07:34,760 or at least a process, 173 00:07:35,000 --> 00:07:35,960 from running. 174 00:07:36,000 --> 00:07:36,720 Denial of service. 175 00:07:37,220 --> 00:07:38,780 There are several other ways 176 00:07:39,180 --> 00:07:40,380 that you can exploit, 177 00:07:41,120 --> 00:07:41,960 not meaning, 178 00:07:42,420 --> 00:07:43,360 shellcode necessarily, 179 00:07:44,200 --> 00:07:45,840 but exploit a computer program 180 00:07:46,420 --> 00:07:47,620 because of a buffer overflow. 181 00:07:49,340 --> 00:07:50,460 First of all, 182 00:07:50,480 --> 00:07:53,320 you can overwrite other variables on the heap. 183 00:07:53,720 --> 00:07:54,960 So, for instance, 184 00:07:55,180 --> 00:07:57,580 if your program has in it, 185 00:07:58,240 --> 00:08:00,600 members of this group 186 00:08:00,600 --> 00:08:02,340 stored in this heap variable 187 00:08:03,400 --> 00:08:06,000 have administrative access to my tool. 188 00:08:06,800 --> 00:08:08,620 And you happened to buffer overflow 189 00:08:08,620 --> 00:08:09,500 just before that. 190 00:08:09,640 --> 00:08:10,440 Well, you can go, 191 00:08:10,540 --> 00:08:11,020 oh, yeah, 192 00:08:11,020 --> 00:08:12,300 hey, that's my group. 193 00:08:13,840 --> 00:08:14,720 Atlas group. 194 00:08:16,140 --> 00:08:18,520 Suddenly, you have administrative access. 195 00:08:18,740 --> 00:08:20,600 You can control things 196 00:08:20,600 --> 00:08:23,000 you should have no business controlling. 197 00:08:23,540 --> 00:08:24,920 Another interesting one 198 00:08:24,920 --> 00:08:25,700 that's often overlooked 199 00:08:25,700 --> 00:08:30,080 is the ability to take a string variable, 200 00:08:30,320 --> 00:08:30,820 for instance, 201 00:08:32,660 --> 00:08:35,300 get rid of that nasty little null at the end 202 00:08:35,300 --> 00:08:37,140 that keeps the printf 203 00:08:37,140 --> 00:08:38,880 from printing more beyond that point. 204 00:08:39,440 --> 00:08:40,040 . 205 00:08:40,640 --> 00:08:42,120 And instead of overwriting, 206 00:08:42,340 --> 00:08:43,220 you can now, 207 00:08:43,760 --> 00:08:45,060 with the help of a printf 208 00:08:45,060 --> 00:08:46,940 that very likely already exists for you, 209 00:08:49,640 --> 00:08:53,060 get access to read whatever comes after your buffer. 210 00:08:53,480 --> 00:08:53,980 So that, 211 00:08:54,140 --> 00:08:57,240 we'll call that end of string overwrites. 212 00:08:57,320 --> 00:08:58,980 You're overwriting the null 213 00:08:58,980 --> 00:08:59,780 at the end of your string. 214 00:09:01,400 --> 00:09:02,080 But, of course, 215 00:09:02,160 --> 00:09:02,920 the creme de la creme, 216 00:09:03,740 --> 00:09:04,480 the very, 217 00:09:04,580 --> 00:09:05,560 the most exciting, 218 00:09:05,660 --> 00:09:06,440 this is going to kill me. 219 00:09:06,820 --> 00:09:07,580 Hold on a second. 220 00:09:07,580 --> 00:09:11,380 The most exciting thing that we talk about 221 00:09:11,380 --> 00:09:13,120 when talking about heap buffer overflows 222 00:09:13,120 --> 00:09:18,280 is tinkering with in-band information. 223 00:09:18,840 --> 00:09:20,440 In-band management information, 224 00:09:20,780 --> 00:09:21,420 to be exact. 225 00:09:22,140 --> 00:09:23,660 It's a delicate process, 226 00:09:23,840 --> 00:09:26,060 and it's one that's not very well understood. 227 00:09:39,980 --> 00:09:44,740 So a little bit more about heap memory in particular, 228 00:09:45,880 --> 00:09:46,620 generically speaking. 229 00:09:47,960 --> 00:09:49,980 A heap is a chunk of information 230 00:09:50,560 --> 00:09:53,460 that is given to a process by the kernel. 231 00:09:54,480 --> 00:09:55,840 If the process needs more, 232 00:09:56,020 --> 00:09:58,840 it goes back and has a very brute force request 233 00:09:58,840 --> 00:09:59,700 to the kernel, 234 00:09:59,880 --> 00:10:00,800 hey, I need more space. 235 00:10:01,100 --> 00:10:02,240 Can I have more space, please? 236 00:10:03,880 --> 00:10:04,860 And the kernel will say, 237 00:10:04,980 --> 00:10:05,160 yeah, 238 00:10:05,980 --> 00:10:06,700 here's a couple gig. 239 00:10:07,160 --> 00:10:09,700 Okay, maybe a couple meg or whatever. 240 00:10:10,620 --> 00:10:11,880 It's up to the program 241 00:10:12,340 --> 00:10:14,440 to then decide how to manage that information. 242 00:10:14,940 --> 00:10:17,240 How many of you have a bedroom at home? 243 00:10:19,380 --> 00:10:21,660 How many of you have a clean bedroom at home? 244 00:10:22,920 --> 00:10:23,780 Get out of here. 245 00:10:23,920 --> 00:10:25,060 I don't want you in my room. 246 00:10:27,980 --> 00:10:29,100 Why is that? 247 00:10:29,700 --> 00:10:31,860 Well, because many of us understand, 248 00:10:32,440 --> 00:10:33,940 whether we're good at it or not, 249 00:10:33,940 --> 00:10:38,060 we understand that open space requires management. 250 00:10:38,660 --> 00:10:41,920 So DLMalloc is an implementation 251 00:10:41,920 --> 00:10:43,820 of a heap management system. 252 00:10:44,140 --> 00:10:45,660 It was written by a guy named Doug Lee, 253 00:10:45,900 --> 00:10:47,760 thus the DL part. 254 00:10:48,200 --> 00:10:49,900 Somebody asked me about that earlier. 255 00:10:49,980 --> 00:10:51,300 I forget what he said, 256 00:10:51,380 --> 00:10:52,180 but it was really funny. 257 00:10:53,720 --> 00:10:55,600 Yeah, sorry, sharing my internal jokes. 258 00:10:57,320 --> 00:11:01,620 So Doug Lee wrote this heap management system, 259 00:11:01,620 --> 00:11:04,160 and he made one mistake, 260 00:11:04,360 --> 00:11:05,560 or maybe it was intentional. 261 00:11:06,160 --> 00:11:07,820 I'm not beyond guessing that. 262 00:11:08,000 --> 00:11:09,060 And in my line of work, 263 00:11:09,180 --> 00:11:11,440 I like to think along those lines. 264 00:11:11,460 --> 00:11:12,920 I think the people who wrote the stack, 265 00:11:13,140 --> 00:11:14,600 I think they were just really evil. 266 00:11:15,840 --> 00:11:17,060 Because who else would say, 267 00:11:17,140 --> 00:11:20,380 hey, start here and keep writing over the return pointer? 268 00:11:22,300 --> 00:11:25,940 Doug Lee and several other heap implementations, 269 00:11:28,000 --> 00:11:30,980 they make use of what's called in-band management data. 270 00:11:30,980 --> 00:11:32,560 In other words, 271 00:11:32,620 --> 00:11:34,920 my chunk is 101 bytes long. 272 00:11:37,100 --> 00:11:38,220 Do some math, 273 00:11:38,480 --> 00:11:41,400 make it round off so that it's stack aligned 274 00:11:41,400 --> 00:11:42,500 and all that beautiful stuff, 275 00:11:42,660 --> 00:11:44,200 or aligned in some way. 276 00:11:45,780 --> 00:11:47,140 At the very beginning, 277 00:11:47,480 --> 00:11:49,080 just before what you see 278 00:11:49,080 --> 00:11:50,980 when you get a heap variable, 279 00:11:51,260 --> 00:11:54,280 the four bytes leading up to your memory space 280 00:11:54,280 --> 00:11:57,200 are actually a counter that says, 281 00:11:57,920 --> 00:11:59,840 hey, anybody looking at me, 282 00:11:59,840 --> 00:12:04,000 the next heap is X number of bytes ahead of me. 283 00:12:08,740 --> 00:12:09,260 Okay. 284 00:12:10,540 --> 00:12:12,080 So, heap buffer overflow. 285 00:12:13,780 --> 00:12:14,540 Wait a minute. 286 00:12:16,220 --> 00:12:17,380 I want to change that one. 287 00:12:18,100 --> 00:12:18,860 We'll get there. 288 00:12:21,320 --> 00:12:22,300 In this presentation, 289 00:12:22,560 --> 00:12:26,580 I use the words chunk and buffer fairly interchangeably. 290 00:12:26,720 --> 00:12:29,040 Understand that when you're talking to somebody 291 00:12:29,040 --> 00:12:31,400 who hacks around in kernel space all the time, 292 00:12:31,540 --> 00:12:32,460 which I don't, 293 00:12:32,460 --> 00:12:34,060 I spend some time there, 294 00:12:34,140 --> 00:12:34,520 I enjoy it, 295 00:12:34,620 --> 00:12:37,260 but they will say no. 296 00:12:37,460 --> 00:12:38,900 What the kernel gives you is a chunk. 297 00:12:39,060 --> 00:12:40,340 You're talking about a buffer. 298 00:12:40,680 --> 00:12:41,740 But I will use them both, 299 00:12:41,860 --> 00:12:43,080 so please don't get lost in that. 300 00:12:44,180 --> 00:12:44,960 Ha, it worked. 301 00:12:46,900 --> 00:12:47,800 So, for example, 302 00:12:49,380 --> 00:12:51,100 we make a call to malloc. 303 00:12:51,580 --> 00:12:53,460 This is the Doug Lee malloc, 304 00:12:53,580 --> 00:12:56,180 or yeah, this is the call basically that says, 305 00:12:56,340 --> 00:12:58,280 I need this many bytes. 306 00:12:59,040 --> 00:13:00,320 And this can be a variable. 307 00:13:00,520 --> 00:13:01,700 That's what's so cool about heap. 308 00:13:02,980 --> 00:13:03,840 So we're going to say, 309 00:13:04,600 --> 00:13:07,760 I want something at least 1024 in size 310 00:13:07,760 --> 00:13:09,700 because that's what I'm going to use of it. 311 00:13:10,460 --> 00:13:12,600 I don't really care if it's 1048 312 00:13:12,600 --> 00:13:16,460 as long as it doesn't eat up too much memory space. 313 00:13:22,940 --> 00:13:24,220 So malloc goes out, 314 00:13:24,300 --> 00:13:25,720 says based on these rules, 315 00:13:26,000 --> 00:13:28,180 I'm going to cut a chunk 316 00:13:29,040 --> 00:13:30,920 into a pointer four bytes into the chunk 317 00:13:30,920 --> 00:13:32,920 because we have our management stuff there. 318 00:13:34,740 --> 00:13:36,280 One thing that's important to note 319 00:13:37,140 --> 00:13:41,400 is that malloc and its counterpart free 320 00:13:41,400 --> 00:13:43,240 are called so frequently 321 00:13:44,800 --> 00:13:48,060 that most sanity checking that we will, 322 00:13:48,160 --> 00:13:49,920 that you'll probably think during this presentation, 323 00:13:50,080 --> 00:13:51,100 why don't I just check? 324 00:13:51,880 --> 00:13:55,260 Most sanity checking costs too much. 325 00:13:56,780 --> 00:13:58,660 If you take something 326 00:13:59,040 --> 00:14:00,600 that's iterated thousands of times 327 00:14:00,600 --> 00:14:02,180 during the boot of an operating system, 328 00:14:02,320 --> 00:14:02,780 for example, 329 00:14:03,920 --> 00:14:06,200 and you add a second here, 330 00:14:06,900 --> 00:14:07,860 even a half a second, 331 00:14:09,020 --> 00:14:09,980 over time, 332 00:14:11,240 --> 00:14:12,760 Vista doesn't boot very fast. 333 00:14:13,120 --> 00:14:14,380 Then again, over time, 334 00:14:14,380 --> 00:14:15,800 Vista doesn't boot very fast anyway. 335 00:14:16,020 --> 00:14:17,520 So I guess that doesn't matter, does it? 336 00:14:18,220 --> 00:14:19,800 But in the Linux world, 337 00:14:21,160 --> 00:14:25,300 we see that not much has been done to correct this. 338 00:14:27,300 --> 00:14:28,260 Some stuff has. 339 00:14:31,140 --> 00:14:33,440 The important part when dealing with 340 00:14:35,040 --> 00:14:38,240 this type of heat buffer overflow exploitation 341 00:14:39,040 --> 00:14:40,960 is the function called free. 342 00:14:42,200 --> 00:14:45,540 If we fill up a buffer 343 00:14:48,160 --> 00:14:51,640 and overwrite the next buffer's management information, 344 00:14:53,980 --> 00:14:56,220 the thing that's going to make use of that, however, 345 00:14:56,740 --> 00:14:58,660 is a call to free. 346 00:14:59,040 --> 00:15:00,420 So you've got two buffers. 347 00:15:01,040 --> 00:15:02,960 The second buffer has management information 348 00:15:02,960 --> 00:15:04,920 just past your buffer. 349 00:15:05,480 --> 00:15:07,340 You overwrite that, you call free, 350 00:15:07,420 --> 00:15:08,620 and free reads that information 351 00:15:08,620 --> 00:15:12,120 and makes decisions based on what you've written there. 352 00:15:23,920 --> 00:15:25,700 That slide seems out of place, doesn't it? 353 00:15:27,220 --> 00:15:28,800 But I'm going to say it anyway, 354 00:15:29,490 --> 00:15:32,060 because back in 2004, 355 00:15:34,020 --> 00:15:35,860 Microsoft vulnerability was found 356 00:15:35,860 --> 00:15:37,200 in a Microsoft core library 357 00:15:37,860 --> 00:15:40,520 in the parsing of JPEG files. 358 00:15:40,760 --> 00:15:42,500 You guys remember the GDI buffer overflow? 359 00:15:43,040 --> 00:15:46,280 Do you guys remember that it took third-party person 360 00:15:46,280 --> 00:15:49,300 to release a tool to even help you figure out 361 00:15:49,300 --> 00:15:49,820 if you're vulnerable? 362 00:15:50,000 --> 00:15:52,000 Because everything uses this thing. 363 00:15:53,000 --> 00:15:55,080 Anyway, this good friend of mine, Tom Liston, 364 00:15:55,700 --> 00:15:56,360 a shout-out. 365 00:15:56,580 --> 00:15:57,840 So he wrote this tool. 366 00:15:59,080 --> 00:16:00,400 He works for Intel Guardians. 367 00:16:00,660 --> 00:16:01,560 Good bunch of guys. 368 00:16:01,640 --> 00:16:02,240 Good friends of mine. 369 00:16:04,760 --> 00:16:06,880 The problem with learning heap buffer overflows, 370 00:16:07,000 --> 00:16:09,180 and the reason that it's not nearly as focused on 371 00:16:09,180 --> 00:16:10,440 as stack-based buffer overflows, 372 00:16:10,620 --> 00:16:12,360 is that stack-based buffer overflows 373 00:16:12,360 --> 00:16:15,080 are fairly consistent between platforms, 374 00:16:15,940 --> 00:16:17,640 at least among the same hardware. 375 00:16:17,720 --> 00:16:19,420 So you've got BSD, you've got Linux, 376 00:16:19,700 --> 00:16:22,800 you've got Windows, and Windows, and Windows, 377 00:16:22,940 --> 00:16:24,200 and every other version of Windows. 378 00:16:27,240 --> 00:16:29,180 There are several implementations 379 00:16:29,180 --> 00:16:30,700 of a malloc system. 380 00:16:31,060 --> 00:16:34,700 Doug Lee, I enjoy his very much. 381 00:16:35,480 --> 00:16:37,360 RTL is what they call it in Windows. 382 00:16:38,260 --> 00:16:41,060 The BSD PHK implementation, 383 00:16:41,760 --> 00:16:43,140 I hate this one. 384 00:16:43,880 --> 00:16:45,840 There is no in-band management, 385 00:16:46,040 --> 00:16:47,840 and I'm having to hack a binary 386 00:16:47,840 --> 00:16:48,980 doing this right now. 387 00:16:49,460 --> 00:16:51,860 In fact, you guys have been down to CTF, right? 388 00:16:52,620 --> 00:16:54,680 After this, I will be running down there 389 00:16:54,680 --> 00:16:57,160 because my team is still in a heated struggle 390 00:16:57,160 --> 00:16:58,540 for first place, 391 00:16:58,780 --> 00:17:01,140 and you guys should all come down and check it out. 392 00:17:01,200 --> 00:17:01,760 It's really cool. 393 00:17:02,000 --> 00:17:06,420 This is what good guys do to go be evil. 394 00:17:07,340 --> 00:17:09,200 There are also several other implementations. 395 00:17:09,300 --> 00:17:10,640 System 5 has their version. 396 00:17:11,380 --> 00:17:13,560 BSD for AI, I'm sorry. 397 00:17:14,280 --> 00:17:16,340 Yeah, BSD and AIX have some, 398 00:17:16,480 --> 00:17:17,560 did I say BDS? 399 00:17:18,400 --> 00:17:19,980 That means something completely different. 400 00:17:26,760 --> 00:17:27,940 What's most important 401 00:17:27,940 --> 00:17:29,500 isn't what happens 402 00:17:29,500 --> 00:17:33,140 when we allocate a buffer 403 00:17:33,140 --> 00:17:34,380 with the Doug Lee malloc. 404 00:17:34,900 --> 00:17:36,040 What's really important 405 00:17:36,560 --> 00:17:39,580 is what happens that we never see. 406 00:17:39,900 --> 00:17:43,620 You see, after you free a buffer, 407 00:17:44,840 --> 00:17:46,840 malloc says, or Doug Lee says, 408 00:17:48,140 --> 00:17:50,520 they don't need that information anymore. 409 00:17:51,700 --> 00:17:52,820 So you know what? 410 00:17:52,920 --> 00:17:54,460 I'm going to use some of it. 411 00:17:54,460 --> 00:17:56,240 Before, we said, 412 00:17:56,380 --> 00:17:57,680 hmm, this guy right here, 413 00:17:57,780 --> 00:17:59,160 just before the buffer 414 00:17:59,160 --> 00:18:00,160 is going to say 415 00:18:00,160 --> 00:18:02,240 how many bytes to the next buffer. 416 00:18:02,420 --> 00:18:04,140 However, when we free it, 417 00:18:04,240 --> 00:18:06,800 we're going to use several more chunks of memory 418 00:18:07,860 --> 00:18:09,660 to store more information. 419 00:18:10,200 --> 00:18:12,600 Because when you write a program, 420 00:18:12,720 --> 00:18:13,660 you're using a heap. 421 00:18:14,560 --> 00:18:17,120 Your heap chunk, you maintain that. 422 00:18:17,280 --> 00:18:18,120 You manage it. 423 00:18:18,260 --> 00:18:20,080 You have pointers to it 424 00:18:20,080 --> 00:18:22,760 that last throughout your program. 425 00:18:22,980 --> 00:18:24,440 But when you return it, 426 00:18:24,440 --> 00:18:25,440 you just say, 427 00:18:25,480 --> 00:18:26,460 I don't need that anymore. 428 00:18:26,980 --> 00:18:28,820 Doug Lee then has to take care of it. 429 00:18:28,900 --> 00:18:30,600 So the Doug Lee malloc 430 00:18:30,600 --> 00:18:33,140 sticks these buffers that are unused 431 00:18:34,040 --> 00:18:35,420 into a doubly linked list. 432 00:18:35,700 --> 00:18:37,300 Anybody know what a doubly linked list is? 433 00:18:38,940 --> 00:18:41,240 They were kind of a pain to do back in college, 434 00:18:41,360 --> 00:18:42,720 but they come in very handy. 435 00:18:43,920 --> 00:18:45,920 So Doug Lee stuck all these suckers 436 00:18:45,920 --> 00:18:47,640 back into a doubly linked list. 437 00:18:52,080 --> 00:18:53,940 And the particular exploit 438 00:18:53,940 --> 00:18:55,240 that we're going to talk about today 439 00:18:55,880 --> 00:18:57,460 has to do with this doubly linked list. 440 00:18:58,160 --> 00:18:58,560 Specifically, 441 00:19:00,160 --> 00:19:02,260 what do you do when you have two buffers 442 00:19:02,260 --> 00:19:04,380 that are freed in this doubly linked list? 443 00:19:07,380 --> 00:19:08,160 I don't know. 444 00:19:08,440 --> 00:19:09,680 Well, you just leave them alone, right? 445 00:19:09,780 --> 00:19:10,580 They point to each other. 446 00:19:10,640 --> 00:19:10,920 You're cool? 447 00:19:11,560 --> 00:19:11,980 Uh-uh. 448 00:19:13,920 --> 00:19:15,260 Because on your hard drive, 449 00:19:15,440 --> 00:19:17,620 what happens when you've got chunks of data 450 00:19:17,620 --> 00:19:19,660 spread all over your hard drive? 451 00:19:23,260 --> 00:19:25,060 It gets slower than Vista. 452 00:19:25,320 --> 00:19:26,180 Did I say that? 453 00:19:27,800 --> 00:19:28,240 Yes. 454 00:19:28,360 --> 00:19:30,900 It slows down because the operating system 455 00:19:30,900 --> 00:19:32,580 is having to tell the hard drive, 456 00:19:32,760 --> 00:19:34,400 go here, go here, go here, go here, 457 00:19:34,400 --> 00:19:35,320 go here, go here, go here. 458 00:19:37,300 --> 00:19:37,740 Malloc... 459 00:19:39,200 --> 00:19:40,920 Heap implementations also have to deal 460 00:19:40,920 --> 00:19:42,160 with the same thing, fragmentation. 461 00:19:42,620 --> 00:19:44,800 So when you get two free heaps next to each other, 462 00:19:45,540 --> 00:19:47,880 the doggone suckers, they've got to be big. 463 00:19:47,880 --> 00:19:51,340 So we join them using a tool called unlink, 464 00:19:51,440 --> 00:19:52,960 and unlink is where the magic lies. 465 00:19:59,100 --> 00:20:00,620 So, in our example, 466 00:20:04,280 --> 00:20:06,660 we have a buffer. 467 00:20:07,240 --> 00:20:09,300 Now, I could have chosen 100 bytes 468 00:20:09,300 --> 00:20:10,780 or 104 bytes, 469 00:20:10,880 --> 00:20:11,920 and it probably would have been prettier. 470 00:20:12,020 --> 00:20:13,780 I chose 101 because it proves 471 00:20:14,580 --> 00:20:16,800 a little bit more of how this works. 472 00:20:17,020 --> 00:20:17,860 And I don't know about you, 473 00:20:17,880 --> 00:20:19,560 but when I had to learn this, 474 00:20:19,760 --> 00:20:21,580 it wasn't about reading about it. 475 00:20:21,640 --> 00:20:23,740 It wasn't about the theory of how it worked, 476 00:20:23,840 --> 00:20:24,580 because I understood that. 477 00:20:25,360 --> 00:20:27,140 But it was about the bits and the bytes 478 00:20:27,140 --> 00:20:28,600 and how it actually worked 479 00:20:28,600 --> 00:20:30,600 that got me to write the presentation. 480 00:20:31,080 --> 00:20:32,620 So if that's cool with you, 481 00:20:32,700 --> 00:20:33,780 I want to get into the details. 482 00:20:35,220 --> 00:20:36,140 101 bytes. 483 00:20:38,080 --> 00:20:39,360 Malloc, DL Malloc, 484 00:20:39,480 --> 00:20:42,720 then allocates, truly, 112 bytes. 485 00:20:44,340 --> 00:20:45,740 There's a little mathematics 486 00:20:45,740 --> 00:20:46,460 to go into there. 487 00:20:46,460 --> 00:20:51,860 I wrote a question for my friend Gmark 488 00:20:51,860 --> 00:20:52,920 for Hacker Jeopardy 489 00:20:52,920 --> 00:20:56,520 that basically took this to heart, 490 00:20:56,660 --> 00:20:58,660 but then I pulled it out later 491 00:20:58,660 --> 00:21:00,260 at the request of a friend of mine 492 00:21:00,260 --> 00:21:00,540 who says, 493 00:21:00,720 --> 00:21:02,260 dude, they're not going to know that. 494 00:21:02,380 --> 00:21:03,700 They haven't seen your talk yet. 495 00:21:04,220 --> 00:21:05,440 So I pulled it. 496 00:21:06,000 --> 00:21:07,140 Anyway, the idea is 497 00:21:07,140 --> 00:21:08,900 you call Malloc, 498 00:21:08,940 --> 00:21:10,340 you say, I need 101 bytes. 499 00:21:10,580 --> 00:21:12,060 You don't know that Malloc needs to know 500 00:21:12,060 --> 00:21:13,400 that you need four or more, 501 00:21:13,560 --> 00:21:15,320 or that you don't tell Malloc 502 00:21:15,320 --> 00:21:17,880 that you need four more than you really need. 503 00:21:18,060 --> 00:21:19,280 Malloc adds that on. 504 00:21:19,460 --> 00:21:20,900 So you've got 105 bytes, 505 00:21:21,820 --> 00:21:24,580 and then in the interest of alignment 506 00:21:25,060 --> 00:21:27,620 and beauty and bliss and even numbers, 507 00:21:29,000 --> 00:21:31,560 DL Malloc then extends that 508 00:21:31,560 --> 00:21:33,540 to the next eight-byte boundary. 509 00:21:34,480 --> 00:21:35,560 What's an eight-byte boundary? 510 00:21:36,480 --> 00:21:38,820 Well, that's a thing in assembly that... 511 00:21:38,820 --> 00:21:39,940 No, no. 512 00:21:40,100 --> 00:21:41,740 An eight-byte boundary is, 513 00:21:42,720 --> 00:21:44,440 well, eight, 16. 514 00:21:44,440 --> 00:21:45,680 It's a multiple of eight. 515 00:21:47,760 --> 00:21:48,480 Nothing magic. 516 00:21:50,900 --> 00:21:52,280 I had to throw this slide in 517 00:21:52,280 --> 00:21:53,300 because a buddy of mine 518 00:21:53,300 --> 00:21:54,600 heard that I was giving the talk. 519 00:21:54,680 --> 00:21:56,140 He said, dude, you're not going to include Windows? 520 00:21:56,600 --> 00:21:56,960 Ha! 521 00:21:57,340 --> 00:21:57,980 I'm not coming. 522 00:21:58,680 --> 00:22:00,320 So here's for you, Lenox, 523 00:22:01,480 --> 00:22:02,200 if you came. 524 00:22:03,500 --> 00:22:05,160 I know I saw some of your friends, 525 00:22:05,260 --> 00:22:06,280 but I didn't see you. 526 00:22:10,200 --> 00:22:11,380 Anyway, Windows actually, 527 00:22:11,560 --> 00:22:12,680 and I won't spend much time here, 528 00:22:12,840 --> 00:22:14,420 Windows has a function 529 00:22:14,440 --> 00:22:16,960 that's a very similar idea of allocated buffers. 530 00:22:17,080 --> 00:22:17,920 You own them. 531 00:22:18,060 --> 00:22:18,960 You take care of them. 532 00:22:19,080 --> 00:22:19,720 You tend to them. 533 00:22:20,100 --> 00:22:21,600 When they are checked in, however, 534 00:22:21,720 --> 00:22:25,860 they are checked into an array of doubly linked lists. 535 00:22:26,580 --> 00:22:27,560 It's the biggest difference. 536 00:22:27,760 --> 00:22:29,740 There's some other details of how they work, 537 00:22:29,820 --> 00:22:31,020 and I won't get into them. 538 00:22:31,140 --> 00:22:32,160 I don't want to confuse you, 539 00:22:32,220 --> 00:22:32,980 but I had to throw it in. 540 00:22:39,160 --> 00:22:39,680 Okay. 541 00:22:43,380 --> 00:22:45,120 So we have heap buffer 542 00:22:45,120 --> 00:22:47,300 that we've allocated. 543 00:22:47,660 --> 00:22:49,760 We've stuck in my first name. 544 00:22:50,000 --> 00:22:51,200 Then we have another one 545 00:22:51,200 --> 00:22:54,060 that includes my alias, Atlas. 546 00:22:54,560 --> 00:22:55,760 And then I say, ha! 547 00:22:55,760 --> 00:22:58,240 I don't really want to give out my first name. 548 00:22:58,320 --> 00:22:59,960 So I delete the first buffer, 549 00:23:00,080 --> 00:23:02,240 and it becomes a freed buffer. 550 00:23:02,980 --> 00:23:03,220 It's a buffer chunk. 551 00:23:05,460 --> 00:23:06,040 Malik says, 552 00:23:08,300 --> 00:23:10,160 I understand that you don't need this. 553 00:23:10,280 --> 00:23:11,200 I'm going to take care of it. 554 00:23:11,400 --> 00:23:13,380 I'm going to use the first eight bytes. 555 00:23:13,900 --> 00:23:14,840 How many bits is that? 556 00:23:15,620 --> 00:23:16,240 Yell it out. 557 00:23:17,600 --> 00:23:18,000 64. 558 00:23:18,400 --> 00:23:19,100 64 bits. 559 00:23:19,280 --> 00:23:21,320 I'm going to use the first 64 bits 560 00:23:21,880 --> 00:23:23,620 of your memory space. 561 00:23:23,960 --> 00:23:24,620 So, you know, 562 00:23:24,640 --> 00:23:25,720 you've got your memory pointer 563 00:23:25,720 --> 00:23:28,740 to the very first byte of what you see. 564 00:23:28,880 --> 00:23:29,880 Not the first four bytes, 565 00:23:30,000 --> 00:23:31,560 but the first byte you see. 566 00:23:31,560 --> 00:23:33,220 And from there, 567 00:23:33,380 --> 00:23:35,500 it sucks up eight bytes 568 00:23:36,380 --> 00:23:39,340 to 32-bit addresses. 569 00:23:42,720 --> 00:23:45,680 The buffer in the linked list, 570 00:23:45,760 --> 00:23:47,640 it's been deallocated 571 00:23:48,560 --> 00:23:50,060 just before it. 572 00:23:50,140 --> 00:23:52,340 And the buffer that's been deallocated 573 00:23:52,340 --> 00:23:53,280 just after it. 574 00:23:53,980 --> 00:23:54,840 Got that? 575 00:23:58,460 --> 00:24:00,540 There are a couple other places in, 576 00:24:00,540 --> 00:24:02,820 within your buffer that it will write to. 577 00:24:03,320 --> 00:24:04,520 We won't get to that now. 578 00:24:04,620 --> 00:24:06,540 I have slides that actually explain that better. 579 00:24:08,400 --> 00:24:09,260 So when, 580 00:24:10,440 --> 00:24:11,940 when the memory is allocated, 581 00:24:12,220 --> 00:24:14,540 so notice this is used, 582 00:24:15,360 --> 00:24:16,860 this is kind of what the, 583 00:24:16,880 --> 00:24:18,700 the memory space looks like. 584 00:24:20,120 --> 00:24:21,100 We have 585 00:24:23,180 --> 00:24:25,020 a long integer, 586 00:24:25,220 --> 00:24:26,200 32 bits. 587 00:24:26,920 --> 00:24:27,900 Then we have 588 00:24:28,460 --> 00:24:29,140 101 chairs, 589 00:24:29,140 --> 00:24:29,900 characters, 590 00:24:30,880 --> 00:24:32,100 and then we have 591 00:24:32,100 --> 00:24:33,380 some padding at the end. 592 00:24:33,540 --> 00:24:33,760 Right? 593 00:24:34,020 --> 00:24:34,420 Right. 594 00:24:38,600 --> 00:24:39,000 However, 595 00:24:40,840 --> 00:24:43,420 this is a bit closer to what you will see 596 00:24:43,420 --> 00:24:46,460 when looking at a freed buffer. 597 00:24:48,660 --> 00:24:50,960 I tried to find a good way to do this. 598 00:24:51,300 --> 00:24:53,560 I chose to do it in a struct type format. 599 00:24:53,980 --> 00:24:56,080 I do have something that I stole 600 00:24:56,080 --> 00:24:57,100 from Once Upon a Free. 601 00:24:57,220 --> 00:24:58,440 You guys heard of Frack, right? 602 00:25:00,040 --> 00:25:01,560 I'll show that later too. 603 00:25:01,640 --> 00:25:03,000 I wanted to take two different approaches. 604 00:25:03,580 --> 00:25:05,360 But for those of you who like the struct, 605 00:25:05,600 --> 00:25:06,580 here it is for you. 606 00:25:06,940 --> 00:25:08,640 So we start out with our long. 607 00:25:08,860 --> 00:25:12,280 This is the length of your buffer 608 00:25:14,260 --> 00:25:16,940 including all the rigamarole at the end, 609 00:25:16,980 --> 00:25:17,920 the padding and that. 610 00:25:19,980 --> 00:25:21,380 It is an even number 611 00:25:23,360 --> 00:25:25,620 unless the previous guy is not in use. 612 00:25:26,900 --> 00:25:28,320 We will say that many times. 613 00:25:28,440 --> 00:25:29,080 Don't worry about it. 614 00:25:29,840 --> 00:25:30,880 Just keep it in your head. 615 00:25:30,980 --> 00:25:31,820 If it is an even number, 616 00:25:32,200 --> 00:25:33,580 the guy before me is not in use. 617 00:25:33,620 --> 00:25:34,480 If it is an odd number, 618 00:25:34,700 --> 00:25:36,600 the guy before me is in use. 619 00:25:37,700 --> 00:25:39,180 So you will look through here. 620 00:25:39,280 --> 00:25:41,020 You will see the number 71 621 00:25:41,020 --> 00:25:42,480 or 71 hex. 622 00:25:42,800 --> 00:25:43,340 Forgive me. 623 00:25:45,540 --> 00:25:46,760 But that doesn't mean that 624 00:25:47,540 --> 00:25:50,120 71 hex later is where the next buffer starts. 625 00:25:50,200 --> 00:25:52,280 It really starts 70 hex later. 626 00:25:52,720 --> 00:25:55,060 But that just means that the guy before him is in use. 627 00:25:56,300 --> 00:25:56,660 Then, 628 00:25:56,660 --> 00:25:59,480 we see two longs. 629 00:25:59,480 --> 00:26:00,200 The previous guy. 630 00:26:00,320 --> 00:26:00,860 The next guy. 631 00:26:01,280 --> 00:26:01,800 Then, 632 00:26:01,820 --> 00:26:05,320 the remainder of your character space. 633 00:26:08,240 --> 00:26:09,280 The length, 634 00:26:10,060 --> 00:26:10,580 again, 635 00:26:10,660 --> 00:26:12,480 of your buffer. 636 00:26:13,700 --> 00:26:15,080 I don't know why they did that. 637 00:26:15,200 --> 00:26:17,260 I have a feeling it was for performance sake 638 00:26:17,260 --> 00:26:19,000 when freeing the guy before you. 639 00:26:19,240 --> 00:26:19,780 But anyway, 640 00:26:20,700 --> 00:26:23,380 there is another copy of your length 641 00:26:24,420 --> 00:26:25,460 without the one. 642 00:26:25,460 --> 00:26:26,760 So it is always even. 643 00:26:28,020 --> 00:26:28,500 And then, 644 00:26:28,520 --> 00:26:29,020 the pad size. 645 00:26:30,780 --> 00:26:31,800 Everybody still with me? 646 00:26:32,800 --> 00:26:33,580 If you are not, 647 00:26:34,060 --> 00:26:34,880 raise your hand. 648 00:26:38,040 --> 00:26:38,360 See, 649 00:26:38,760 --> 00:26:40,540 that is how I get people to think that they are with me 650 00:26:40,540 --> 00:26:41,680 or tell me that they are with me 651 00:26:41,680 --> 00:26:43,420 because nobody wants to stick up their hand. 652 00:26:53,780 --> 00:26:54,660 So as I mentioned, 653 00:26:54,660 --> 00:26:58,360 the magic happens in an area called unlink. 654 00:26:58,560 --> 00:27:00,000 It is actually a macro. 655 00:27:00,280 --> 00:27:00,940 Do you guys understand? 656 00:27:01,220 --> 00:27:05,460 Who here understands the difference between a macro and a function? 657 00:27:05,820 --> 00:27:07,640 And don't worry if you don't. 658 00:27:08,660 --> 00:27:09,260 Okay. 659 00:27:10,340 --> 00:27:11,520 In C programming, 660 00:27:11,900 --> 00:27:14,960 they allow you to do all sorts of things 661 00:27:14,960 --> 00:27:18,260 that help you as a developer think about the program 662 00:27:18,260 --> 00:27:20,640 aside from secure coding. 663 00:27:22,720 --> 00:27:24,040 You write a function, 664 00:27:24,040 --> 00:27:26,100 that you want to call over and over, 665 00:27:26,240 --> 00:27:28,420 and it becomes something in memory, 666 00:27:28,560 --> 00:27:30,400 its own entity in memory, 667 00:27:30,520 --> 00:27:32,940 that can be called from all sorts of different places. 668 00:27:33,140 --> 00:27:33,500 Oh yeah, 669 00:27:33,520 --> 00:27:34,060 from up here too. 670 00:27:34,340 --> 00:27:35,380 And at the end, 671 00:27:35,400 --> 00:27:36,100 there is a return. 672 00:27:36,760 --> 00:27:37,460 And hey, 673 00:27:37,540 --> 00:27:38,860 that return pointer that we talked about, 674 00:27:40,000 --> 00:27:43,740 then sends the instruction pointer back to where it came from. 675 00:27:44,920 --> 00:27:45,760 Most of the time. 676 00:27:45,800 --> 00:27:46,080 Actually, 677 00:27:46,120 --> 00:27:47,360 there is some fuzzy stuff there 678 00:27:47,360 --> 00:27:50,280 that evil people like Invisigoth from Kenshodo do 679 00:27:50,280 --> 00:27:54,020 that actually don't return where it came from. 680 00:27:54,020 --> 00:27:54,340 It's supposed to. 681 00:27:54,620 --> 00:27:55,100 Anyway, 682 00:27:56,360 --> 00:27:57,720 then you have macros. 683 00:27:58,040 --> 00:27:59,620 The idea behind a macro is 684 00:28:00,300 --> 00:28:02,720 I want this to be exceedingly fast, 685 00:28:03,320 --> 00:28:05,620 so it's going to be inserted inline 686 00:28:06,360 --> 00:28:07,460 wherever I write this. 687 00:28:07,720 --> 00:28:08,120 But, 688 00:28:08,200 --> 00:28:10,980 because I know that I will dork that up 689 00:28:11,620 --> 00:28:14,440 if I do it 25 times like I'm going to have to. 690 00:28:14,980 --> 00:28:16,100 So I write a macro, 691 00:28:16,280 --> 00:28:17,400 I put the macro in, 692 00:28:17,480 --> 00:28:18,420 the compiler says, 693 00:28:18,540 --> 00:28:19,300 oh it's a macro, 694 00:28:20,200 --> 00:28:22,120 and I just want to make sure that 695 00:28:22,720 --> 00:28:23,780 that gets replaced. 696 00:28:24,020 --> 00:28:25,420 Before compiling. 697 00:28:26,180 --> 00:28:26,620 Also, 698 00:28:26,700 --> 00:28:27,340 macros are used, 699 00:28:27,480 --> 00:28:27,760 yes, 700 00:28:27,820 --> 00:28:31,080 I know macros are used for numbers 701 00:28:32,480 --> 00:28:35,340 that you don't want to have to call to a variable all the time. 702 00:28:35,400 --> 00:28:36,140 It's a performance thing. 703 00:28:36,800 --> 00:28:37,380 It's cool. 704 00:28:39,140 --> 00:28:40,460 So unlink is a macro, 705 00:28:40,600 --> 00:28:43,040 and the basic idea behind unlink is 706 00:28:44,140 --> 00:28:46,420 the guy behind me has a forward pointer, 707 00:28:47,220 --> 00:28:49,780 and the guy in front of me 708 00:28:49,780 --> 00:28:51,380 has a backward pointer. 709 00:28:52,180 --> 00:28:52,620 Well, 710 00:28:52,620 --> 00:28:54,800 I'm bowing out of this puppy, 711 00:28:55,600 --> 00:28:56,080 so 712 00:28:58,240 --> 00:29:00,400 the guy in front of me, 713 00:29:00,480 --> 00:29:03,240 his back pointer should really point to this guy over here. 714 00:29:04,660 --> 00:29:06,260 And this guy over here, 715 00:29:06,360 --> 00:29:07,100 the guy behind me, 716 00:29:07,180 --> 00:29:09,700 his forward pointer should point to the guy over here. 717 00:29:11,840 --> 00:29:12,960 I didn't say why. 718 00:29:14,180 --> 00:29:15,060 Can anyone guess? 719 00:29:16,240 --> 00:29:17,940 Why would you use unlink? 720 00:29:21,100 --> 00:29:23,320 The use of unlink is actually, 721 00:29:23,500 --> 00:29:25,720 it has several uses in the DLMalloc. 722 00:29:26,700 --> 00:29:29,060 The use that we will be speaking of specifically 723 00:29:30,140 --> 00:29:34,020 is the use of joining two adjacent buffers 724 00:29:34,700 --> 00:29:38,620 into one larger chunk that can be used for many more things. 725 00:29:39,920 --> 00:29:40,520 So, 726 00:29:40,540 --> 00:29:41,480 you call free, 727 00:29:42,640 --> 00:29:47,680 your buffer is now freed and has all the nifty little management stuff in, 728 00:29:47,680 --> 00:29:48,400 involved. 729 00:29:49,040 --> 00:29:50,380 But then unlink, 730 00:29:50,400 --> 00:29:51,580 or it checks to see, 731 00:29:51,700 --> 00:29:52,080 hey, 732 00:29:52,160 --> 00:29:53,840 is this previous buffer in use? 733 00:29:54,320 --> 00:29:55,420 And it looks at what? 734 00:29:57,360 --> 00:29:59,420 It looks at that first four bytes. 735 00:30:01,600 --> 00:30:02,120 What's that? 736 00:30:03,420 --> 00:30:03,900 Exactly. 737 00:30:05,080 --> 00:30:08,360 It looks at that first four bytes just before your buffer. 738 00:30:09,940 --> 00:30:11,140 Is it even or odd? 739 00:30:12,280 --> 00:30:12,760 So, 740 00:30:14,720 --> 00:30:15,680 it's even. 741 00:30:15,800 --> 00:30:16,440 What would we do? 742 00:30:16,440 --> 00:30:17,700 Would we call unlink or not? 743 00:30:20,160 --> 00:30:20,600 Yes. 744 00:30:20,840 --> 00:30:21,800 I heard the answer. 745 00:30:21,900 --> 00:30:22,180 Yes. 746 00:30:22,340 --> 00:30:23,020 You call it. 747 00:30:23,080 --> 00:30:23,220 Why? 748 00:30:23,340 --> 00:30:23,620 Because, 749 00:30:23,680 --> 00:30:23,840 well, 750 00:30:23,920 --> 00:30:25,560 the one isn't there. 751 00:30:26,100 --> 00:30:26,680 It's even. 752 00:30:26,820 --> 00:30:27,920 It means zero, 753 00:30:28,000 --> 00:30:28,700 which means false. 754 00:30:28,980 --> 00:30:30,800 The guy before me is not in use. 755 00:30:33,440 --> 00:30:33,880 However, 756 00:30:36,000 --> 00:30:37,980 notice that this buffer, 757 00:30:39,440 --> 00:30:40,700 the deciding factor 758 00:30:41,700 --> 00:30:44,280 of whether or not I call unlink is up to me. 759 00:30:44,280 --> 00:30:47,140 Because I just overflowed the guy before him. 760 00:30:48,320 --> 00:30:49,160 Does that make sense? 761 00:30:53,520 --> 00:30:55,060 But the devil is in the details. 762 00:30:55,540 --> 00:30:57,900 Do pay close attention. 763 00:30:58,660 --> 00:30:59,120 Ooh, 764 00:30:59,180 --> 00:31:01,400 that really didn't show up very well on the projector. 765 00:31:08,800 --> 00:31:11,100 Maybe with a laser pointer I can highlight it. 766 00:31:11,160 --> 00:31:11,660 How's that sound? 767 00:31:12,340 --> 00:31:12,820 So, 768 00:31:12,820 --> 00:31:14,040 an in use memory buffer. 769 00:31:14,200 --> 00:31:15,540 I told you that we'd go over this again. 770 00:31:15,740 --> 00:31:17,400 I stole this from Once Upon a Free. 771 00:31:17,540 --> 00:31:20,580 I have to give them credit because they did all this ASCII art. 772 00:31:21,120 --> 00:31:22,580 I modified it a little bit, 773 00:31:22,640 --> 00:31:23,080 so it's adapted. 774 00:31:24,860 --> 00:31:25,260 So, 775 00:31:25,300 --> 00:31:25,940 we've got the chunk. 776 00:31:26,280 --> 00:31:27,140 It starts here. 777 00:31:27,540 --> 00:31:28,220 Previous size, 778 00:31:28,340 --> 00:31:29,060 32 bits long. 779 00:31:29,340 --> 00:31:30,080 Of course, 780 00:31:30,140 --> 00:31:31,220 if it's even or odd, 781 00:31:31,340 --> 00:31:32,080 it means something. 782 00:31:34,800 --> 00:31:35,360 I'm sorry. 783 00:31:37,360 --> 00:31:39,380 This is before our chunk. 784 00:31:39,980 --> 00:31:41,360 I didn't adapt it very well, 785 00:31:41,420 --> 00:31:41,700 actually. 786 00:31:41,800 --> 00:31:42,800 I should have moved this to the other side. 787 00:31:42,800 --> 00:31:42,900 I'll move this down. 788 00:31:43,200 --> 00:31:44,700 This is a little confusing. 789 00:31:44,800 --> 00:31:45,160 I apologize. 790 00:31:46,340 --> 00:31:48,100 Here's where our data starts. 791 00:31:48,380 --> 00:31:50,680 The first four bytes is the size, 792 00:31:50,740 --> 00:31:52,340 and that's or, 793 00:31:53,500 --> 00:31:54,220 bitwise or, 794 00:31:54,860 --> 00:31:56,160 of previous in use. 795 00:31:56,840 --> 00:31:57,880 Then we have our memory. 796 00:31:58,280 --> 00:31:59,760 Then we have our next chunk. 797 00:32:06,020 --> 00:32:06,500 And, 798 00:32:07,200 --> 00:32:07,680 again, 799 00:32:07,880 --> 00:32:09,060 that should be down lower. 800 00:32:09,560 --> 00:32:10,480 This last, 801 00:32:12,060 --> 00:32:12,780 32 bits, 802 00:32:12,800 --> 00:32:12,980 integer. 803 00:32:13,300 --> 00:32:14,360 What does it really say? 804 00:32:15,980 --> 00:32:16,660 What's it mean? 805 00:32:17,520 --> 00:32:18,480 Previous size. 806 00:32:19,600 --> 00:32:20,080 Well, 807 00:32:20,120 --> 00:32:21,200 that's this up here. 808 00:32:22,540 --> 00:32:23,140 I'm sorry. 809 00:32:23,220 --> 00:32:26,260 This size right here minus the previous in use. 810 00:32:26,600 --> 00:32:27,080 So, 811 00:32:27,100 --> 00:32:27,340 we're good. 812 00:32:28,660 --> 00:32:29,140 Yuck. 813 00:32:34,180 --> 00:32:35,680 An unused memory buffer, 814 00:32:35,880 --> 00:32:39,300 somebody who's been sent for deletion, 815 00:32:39,360 --> 00:32:40,000 so to speak, 816 00:32:40,020 --> 00:32:40,900 if you're into Tron. 817 00:32:44,780 --> 00:32:45,740 Has also, 818 00:32:45,840 --> 00:32:46,980 just before our buffer, 819 00:32:47,080 --> 00:32:47,920 we have a previous size. 820 00:32:48,200 --> 00:32:50,500 Then we have the size of our buffer. 821 00:32:51,900 --> 00:32:53,740 Then our forward link, 822 00:32:53,980 --> 00:32:55,160 32 bit buffer, 823 00:32:55,320 --> 00:32:55,660 and we'll see, 824 00:32:55,700 --> 00:32:57,640 or a 32 bit integer, 825 00:32:57,880 --> 00:32:58,520 a pointer. 826 00:32:58,800 --> 00:33:00,840 We'll see this in real data in a little bit. 827 00:33:01,480 --> 00:33:03,200 And a back pointer. 828 00:33:04,080 --> 00:33:05,420 Then we see the old memory. 829 00:33:05,860 --> 00:33:06,340 And, 830 00:33:06,480 --> 00:33:06,680 again, 831 00:33:06,740 --> 00:33:07,700 we see the previous size. 832 00:33:10,880 --> 00:33:11,360 So, 833 00:33:12,900 --> 00:33:13,800 we've been lost. 834 00:33:14,400 --> 00:33:15,340 We got a map. 835 00:33:16,280 --> 00:33:16,660 Okay, 836 00:33:16,700 --> 00:33:18,260 we kind of got an idea where we're going, 837 00:33:18,340 --> 00:33:18,440 right? 838 00:33:19,220 --> 00:33:19,600 Okay, 839 00:33:19,660 --> 00:33:20,360 don't show your hands. 840 00:33:20,520 --> 00:33:21,080 Just agree. 841 00:33:22,700 --> 00:33:23,080 Good. 842 00:33:26,840 --> 00:33:27,220 So, 843 00:33:27,300 --> 00:33:28,160 when on link is called, 844 00:33:29,340 --> 00:33:30,780 back and forward, 845 00:33:30,940 --> 00:33:31,900 as I'll call them, 846 00:33:31,980 --> 00:33:32,860 obviously it's 847 00:33:32,860 --> 00:33:33,040 but, 848 00:33:35,720 --> 00:33:37,360 they are used to determine 849 00:33:38,900 --> 00:33:40,800 what bytes to overwrite in memory. 850 00:33:40,880 --> 00:33:41,060 Because, 851 00:33:41,360 --> 00:33:42,100 in the end, 852 00:33:42,140 --> 00:33:43,040 that's what this is all about. 853 00:33:43,180 --> 00:33:43,720 It's not about, 854 00:33:43,820 --> 00:33:44,360 ooh, 855 00:33:44,780 --> 00:33:45,140 let's, 856 00:33:45,860 --> 00:33:49,160 let's make this point to some other part of the linked list. 857 00:33:49,320 --> 00:33:49,420 No, 858 00:33:49,420 --> 00:33:49,500 no, 859 00:33:49,520 --> 00:33:49,580 no, 860 00:33:49,600 --> 00:33:49,680 no. 861 00:33:49,920 --> 00:33:51,800 We want to overwrite bytes. 862 00:33:52,780 --> 00:33:53,820 Is that not why we're here? 863 00:33:54,360 --> 00:33:54,720 Yeah. 864 00:33:55,820 --> 00:33:56,440 Why is that? 865 00:33:57,240 --> 00:33:57,600 Well, 866 00:33:57,620 --> 00:33:59,120 because if I overwrite the right bytes, 867 00:33:59,620 --> 00:34:00,500 I can tell, 868 00:34:00,540 --> 00:34:01,780 at the instruction pointer, 869 00:34:01,920 --> 00:34:02,600 where to go. 870 00:34:04,040 --> 00:34:04,400 And, 871 00:34:04,440 --> 00:34:04,640 yes, 872 00:34:05,020 --> 00:34:06,520 that has the same meaning as, 873 00:34:06,540 --> 00:34:07,700 as when I tell you where to go. 874 00:34:08,920 --> 00:34:10,400 It goes to hell in a hand basket. 875 00:34:10,540 --> 00:34:10,900 Or, 876 00:34:11,360 --> 00:34:11,980 as a hacker thinks, 877 00:34:12,140 --> 00:34:14,320 it goes into my code where it should be. 878 00:34:18,060 --> 00:34:18,920 So this happens 879 00:34:18,920 --> 00:34:20,800 whether you're collapsing backwards, 880 00:34:20,980 --> 00:34:22,340 so you're freeing, 881 00:34:23,460 --> 00:34:24,100 or, 882 00:34:24,180 --> 00:34:24,580 yeah, 883 00:34:24,660 --> 00:34:25,640 you're freeing this buffer 884 00:34:25,640 --> 00:34:27,160 and collapsing backwards, 885 00:34:28,160 --> 00:34:29,580 or you're freeing 886 00:34:29,580 --> 00:34:31,120 and collapsing forwards. 887 00:34:31,400 --> 00:34:33,040 On link is called both times, 888 00:34:33,100 --> 00:34:34,300 both ways are exploitable 889 00:34:35,600 --> 00:34:38,760 in some operating systems or some implementations. 890 00:34:41,480 --> 00:34:42,740 So now we've got two hands, 891 00:34:42,800 --> 00:34:43,220 we've got a map. 892 00:34:46,200 --> 00:34:48,560 Let's take some real examples here. 893 00:34:50,960 --> 00:34:51,400 80496C4, 894 00:34:51,540 --> 00:34:53,980 what type of operating system am I talking about? 895 00:34:54,060 --> 00:34:54,580 Somebody yell it out. 896 00:34:56,920 --> 00:34:58,780 What's very common with an 804? 897 00:34:59,080 --> 00:34:59,520 Ed, 898 00:34:59,600 --> 00:34:59,880 you know. 899 00:35:00,460 --> 00:35:00,900 No? 900 00:35:01,600 --> 00:35:02,040 Yeah, 901 00:35:02,060 --> 00:35:02,260 sure. 902 00:35:02,920 --> 00:35:03,840 You're just being hard. 903 00:35:04,520 --> 00:35:05,160 It's Linux. 904 00:35:05,960 --> 00:35:06,400 POSIX, 905 00:35:06,560 --> 00:35:07,140 UNIX, 906 00:35:07,220 --> 00:35:08,100 you name it, 907 00:35:08,140 --> 00:35:10,260 they probably have an 804 something in there. 908 00:35:12,580 --> 00:35:13,060 80496C4, 909 00:35:13,140 --> 00:35:14,020 this is actually Linux, 910 00:35:16,780 --> 00:35:17,740 and 804AOE8. 911 00:35:17,960 --> 00:35:18,980 Oh, wait, no. 912 00:35:19,200 --> 00:35:19,460 Hmm. 913 00:35:20,180 --> 00:35:21,820 Those don't look right, right? 914 00:35:23,340 --> 00:35:24,720 Why don't they look right? 915 00:35:24,860 --> 00:35:26,660 Because that's not typically where a heap lives. 916 00:35:26,960 --> 00:35:27,720 In fact, 917 00:35:27,760 --> 00:35:29,440 in our test program 918 00:35:30,040 --> 00:35:31,300 that we will get to in a minute, 919 00:35:34,200 --> 00:35:34,680 AOE8 920 00:35:35,460 --> 00:35:36,380 is the address 921 00:35:37,620 --> 00:35:39,380 of a GOT entry. 922 00:35:40,740 --> 00:35:42,280 Somebody yell out what GOT means? 923 00:35:43,160 --> 00:35:44,300 Global offset table. 924 00:35:44,420 --> 00:35:45,540 Global offset table, 925 00:35:45,620 --> 00:35:46,320 thank you very much. 926 00:35:47,080 --> 00:35:48,120 Global offset table, 927 00:35:48,160 --> 00:35:48,720 how is it used? 928 00:35:50,240 --> 00:35:51,200 For exploits. 929 00:35:51,280 --> 00:35:52,700 For exploits. 930 00:35:53,220 --> 00:35:53,700 Yes. 931 00:35:54,200 --> 00:35:54,680 Yes, 932 00:35:54,680 --> 00:35:55,360 I like this guy. 933 00:35:57,160 --> 00:35:57,640 Okay. 934 00:35:57,700 --> 00:35:58,440 By design, 935 00:35:58,620 --> 00:36:00,360 the GOT table is a place where 936 00:36:02,000 --> 00:36:03,620 relocated parts of code 937 00:36:03,620 --> 00:36:04,780 that you didn't have to write 938 00:36:04,780 --> 00:36:05,820 because they were in a shared library 939 00:36:06,400 --> 00:36:07,340 are placed 940 00:36:07,340 --> 00:36:08,580 when you start a program. 941 00:36:08,580 --> 00:36:11,040 So how many of you have written a program 942 00:36:11,040 --> 00:36:11,880 that calls a library? 943 00:36:12,880 --> 00:36:13,800 A lot of you. 944 00:36:14,080 --> 00:36:14,540 Why? 945 00:36:14,700 --> 00:36:16,780 Because it's just so doggone easy. 946 00:36:17,460 --> 00:36:18,380 Why do it again? 947 00:36:19,940 --> 00:36:20,400 So, 948 00:36:21,040 --> 00:36:22,780 the loader for an operating system, 949 00:36:22,980 --> 00:36:23,860 and in this case, 950 00:36:23,860 --> 00:36:24,300 Linux, 951 00:36:24,920 --> 00:36:25,380 says, 952 00:36:26,380 --> 00:36:28,020 I need the SSL library. 953 00:36:28,340 --> 00:36:29,220 I'm going to load it up here. 954 00:36:30,780 --> 00:36:33,440 And because we're a PLT GOT type thing, 955 00:36:34,000 --> 00:36:36,000 we're going to stick the beginning address 956 00:36:36,960 --> 00:36:38,320 for this function. 957 00:36:38,320 --> 00:36:40,140 In the global offset table. 958 00:36:42,640 --> 00:36:43,200 Meanwhile, 959 00:36:43,320 --> 00:36:46,440 I call some SSL command. 960 00:36:46,740 --> 00:36:46,880 Well, 961 00:36:47,000 --> 00:36:48,300 let's just say printf. 962 00:36:48,360 --> 00:36:49,020 People know printf. 963 00:36:49,660 --> 00:36:51,440 So I call printf, 964 00:36:51,620 --> 00:36:53,540 and I'm not really calling printf. 965 00:36:54,880 --> 00:36:55,780 Not right away. 966 00:36:56,100 --> 00:36:57,320 What I'm really doing, 967 00:36:57,520 --> 00:36:59,740 my code makes a call 968 00:36:59,740 --> 00:37:02,500 to the PLT. 969 00:37:02,520 --> 00:37:03,400 Who can yell it out? 970 00:37:04,900 --> 00:37:05,460 Yeah. 971 00:37:05,640 --> 00:37:06,480 Procedure linkage table. 972 00:37:07,200 --> 00:37:08,280 Procedure linkage table. 973 00:37:08,480 --> 00:37:09,020 Thank you very much. 974 00:37:09,300 --> 00:37:11,740 We make a call into this special area 975 00:37:11,740 --> 00:37:13,220 at the beginning of our program 976 00:37:13,220 --> 00:37:14,640 called the procedure linkage table. 977 00:37:15,920 --> 00:37:17,840 It then jumps to 978 00:37:18,520 --> 00:37:20,240 wherever your SSL library 979 00:37:20,240 --> 00:37:21,380 or your printf function 980 00:37:22,300 --> 00:37:23,340 really live. 981 00:37:23,780 --> 00:37:24,800 It's really easy. 982 00:37:24,920 --> 00:37:26,300 The loader has to just keep track 983 00:37:26,300 --> 00:37:27,480 of a small area, 984 00:37:27,660 --> 00:37:29,520 and it's called relocation. 985 00:37:29,680 --> 00:37:30,540 Beautiful thing. 986 00:37:30,640 --> 00:37:31,820 Helps us reuse code. 987 00:37:32,300 --> 00:37:33,960 Also a beautiful thing because 988 00:37:33,960 --> 00:37:35,800 if we can overwrite four bytes, 989 00:37:35,800 --> 00:37:38,820 we have a great place to start. 990 00:37:45,780 --> 00:37:46,720 At the bottom, 991 00:37:46,760 --> 00:37:49,960 you see an example heap buffer overflow 992 00:37:49,960 --> 00:37:51,440 for DLMalloc. 993 00:37:52,000 --> 00:37:52,900 We start off 994 00:37:54,840 --> 00:37:56,140 with hexadecimal representation 995 00:37:56,720 --> 00:37:58,300 of an address in memory. 996 00:37:59,640 --> 00:38:00,320 Wait a minute. 997 00:38:00,360 --> 00:38:01,360 That looks kind of weird, doesn't it? 998 00:38:04,460 --> 00:38:05,460 But, trust me, 999 00:38:05,520 --> 00:38:06,480 this is how it looks like. 1000 00:38:07,140 --> 00:38:10,280 We are on a least significant bit first 1001 00:38:11,280 --> 00:38:12,060 operating system. 1002 00:38:13,400 --> 00:38:14,340 Who came up with that? 1003 00:38:14,420 --> 00:38:14,800 I don't know. 1004 00:38:15,500 --> 00:38:16,420 But the idea is, 1005 00:38:16,520 --> 00:38:19,540 instead of 80496C4, 1006 00:38:19,620 --> 00:38:21,500 we're going to start out with the C4 first 1007 00:38:21,500 --> 00:38:23,900 because it's just cooler for some reason. 1008 00:38:25,120 --> 00:38:26,700 C4, 96, 04, 1009 00:38:27,180 --> 00:38:28,720 and not just 8, of course. 1010 00:38:28,820 --> 00:38:29,720 It's 08. 1011 00:38:30,940 --> 00:38:31,340 So we're going to start out 1012 00:38:31,340 --> 00:38:31,860 with the C4 first. 1013 00:38:31,860 --> 00:38:34,220 Then we have another address placed here. 1014 00:38:34,560 --> 00:38:36,480 What do you think these are going to end up being 1015 00:38:37,560 --> 00:38:40,980 when we hit the free unlink mechanism? 1016 00:38:44,780 --> 00:38:45,300 Pointers. 1017 00:38:45,800 --> 00:38:48,100 Forward and backwards pointers, specifically. 1018 00:38:49,080 --> 00:38:51,860 So we provide forward and backward pointers. 1019 00:38:52,960 --> 00:38:54,420 D times 92, 1020 00:38:54,640 --> 00:38:56,580 and I chose D for specific reasons, 1021 00:38:56,840 --> 00:38:58,860 but basically it could be anything. 1022 00:39:00,900 --> 00:39:05,040 Then I chose also to use a negative 4. 1023 00:39:06,120 --> 00:39:08,780 The reason is, unlink doesn't care. 1024 00:39:09,700 --> 00:39:12,500 What it cares about is that it is even 1025 00:39:12,500 --> 00:39:15,700 and it has a couple other checklist-y things 1026 00:39:16,340 --> 00:39:18,360 that negative 4 is just beautiful for. 1027 00:39:18,580 --> 00:39:19,500 So I use it twice. 1028 00:39:19,880 --> 00:39:20,760 I use it here, 1029 00:39:20,920 --> 00:39:22,840 and then I need a 4 byte pad 1030 00:39:22,840 --> 00:39:24,660 for this particular exploit. 1031 00:39:25,840 --> 00:39:27,000 So I use pad 0. 1032 00:39:27,060 --> 00:39:28,140 Hey, it works. 1033 00:39:28,140 --> 00:39:30,480 And then I put another negative 4 1034 00:39:32,100 --> 00:39:36,860 because just before the next buffer comes what? 1035 00:39:39,020 --> 00:39:39,920 It's length. 1036 00:39:40,420 --> 00:39:43,760 And again, unlink doesn't care what it is. 1037 00:39:43,920 --> 00:39:45,840 So I'm going to use negative 4 again. 1038 00:39:46,080 --> 00:39:47,560 Negative 4 means what? 1039 00:39:49,180 --> 00:39:51,360 Is negative 4 odd or even? 1040 00:39:55,060 --> 00:39:56,560 Okay, not bit-wise. 1041 00:39:58,040 --> 00:39:59,440 Negative 4 is even. 1042 00:39:59,880 --> 00:40:01,340 If you drop the negative, 1043 00:40:01,620 --> 00:40:02,480 it's an even number. 1044 00:40:04,940 --> 00:40:06,920 You see, you can tell it's an even number 1045 00:40:06,920 --> 00:40:07,700 because of the C. 1046 00:40:09,400 --> 00:40:10,460 Can you figure that out? 1047 00:40:11,300 --> 00:40:11,960 Just kidding. 1048 00:40:12,600 --> 00:40:14,440 So yes, it's an even number, 1049 00:40:14,540 --> 00:40:17,320 meaning that my buffer doesn't, 1050 00:40:17,320 --> 00:40:18,520 it's not allocated. 1051 00:40:19,440 --> 00:40:21,340 I'll tell you why in a little bit. 1052 00:40:21,780 --> 00:40:25,500 And then I stick the same two numbers afterwards. 1053 00:40:25,500 --> 00:40:26,620 What do you think I'm doing? 1054 00:40:26,720 --> 00:40:31,420 Why am I bothering to do this upfront stuff right here? 1055 00:40:33,660 --> 00:40:35,560 Well, because I'm trying to make it so that 1056 00:40:35,560 --> 00:40:38,420 if they free the second buffer, I win. 1057 00:40:38,880 --> 00:40:41,440 If they free my buffer, I still win. 1058 00:40:46,580 --> 00:40:47,760 So we have a map. 1059 00:40:48,460 --> 00:40:49,460 We have two hands. 1060 00:40:49,900 --> 00:40:51,040 We have a flashlight. 1061 00:40:51,040 --> 00:40:51,240 We have a flashlight. 1062 00:40:54,100 --> 00:40:55,840 So we've just set up memory space 1063 00:40:55,840 --> 00:40:58,620 to make my buffer, which is in use, 1064 00:40:58,840 --> 00:41:00,280 look like it's unused, 1065 00:41:01,140 --> 00:41:04,440 at least for most intents and purposes. 1066 00:41:08,640 --> 00:41:09,980 And we mentioned the got. 1067 00:41:11,260 --> 00:41:12,580 And got is so beautiful, 1068 00:41:12,700 --> 00:41:14,340 and it is read-write, 1069 00:41:15,020 --> 00:41:15,960 so we like it. 1070 00:41:17,900 --> 00:41:19,300 There are a few gotchas. 1071 00:41:22,960 --> 00:41:25,280 Unlink doesn't just do the overwriting 1072 00:41:25,280 --> 00:41:27,540 that we want it to do, okay? 1073 00:41:28,180 --> 00:41:29,420 Unlink does two things. 1074 00:41:29,680 --> 00:41:33,660 It does one direction, pointing to a new place, 1075 00:41:34,860 --> 00:41:37,780 but it also has the real problem 1076 00:41:37,780 --> 00:41:39,820 of going the other direction as well. 1077 00:41:40,700 --> 00:41:45,740 So when we unlink where you've placed your shellcode, 1078 00:41:47,800 --> 00:41:49,280 the eighth buffer, 1079 00:41:49,280 --> 00:41:49,300 the seventh byte, 1080 00:41:51,260 --> 00:41:52,940 through the twelfth byte 1081 00:41:53,720 --> 00:41:55,140 are going to get clobbered. 1082 00:41:55,840 --> 00:41:58,720 Yes, for you leadists, it's seven through 11. 1083 00:42:00,580 --> 00:42:01,720 It's going to get clobbered. 1084 00:42:01,760 --> 00:42:02,340 Can't help it. 1085 00:42:02,420 --> 00:42:02,800 Sorry. 1086 00:42:02,980 --> 00:42:03,860 It's kind of how it works. 1087 00:42:04,040 --> 00:42:07,460 So we start out with a couple opcodes 1088 00:42:08,240 --> 00:42:09,340 in our shellcode. 1089 00:42:09,560 --> 00:42:10,520 If you're shellcoding, 1090 00:42:11,100 --> 00:42:12,600 you're probably doing opcodes anyway. 1091 00:42:13,940 --> 00:42:17,120 Except, of course, if you're using that HD Moore guy's tool, 1092 00:42:17,120 --> 00:42:18,700 who I love. 1093 00:42:18,760 --> 00:42:19,300 He's really awesome. 1094 00:42:20,860 --> 00:42:21,340 Metasploit. 1095 00:42:21,420 --> 00:42:22,920 Because Metasploit's just so easy. 1096 00:42:23,940 --> 00:42:26,360 So we do our MSF payload 1097 00:42:27,000 --> 00:42:29,400 and dump that to whatever storage facility, 1098 00:42:29,620 --> 00:42:31,180 a file or whatever. 1099 00:42:32,740 --> 00:42:36,320 And we then have to precede the payload from Metasploit 1100 00:42:36,320 --> 00:42:37,860 with some of our own bytes. 1101 00:42:38,100 --> 00:42:39,340 We stick in an EB, 1102 00:42:39,500 --> 00:42:42,620 which is a jump to a single byte offset, 1103 00:42:42,860 --> 00:42:43,700 which is important 1104 00:42:43,700 --> 00:42:46,120 because we don't want to throw any zeros into this number. 1105 00:42:47,120 --> 00:42:47,500 One byte. 1106 00:42:48,360 --> 00:42:56,400 So we jump 0x, 0xe is the length. 1107 00:42:57,860 --> 00:42:59,200 Including the opcode, 1108 00:42:59,260 --> 00:43:01,640 it actually ends up being 0x10, 1109 00:43:01,840 --> 00:43:03,400 or 10, sorry. 1110 00:43:04,820 --> 00:43:05,680 We'll see this. 1111 00:43:05,760 --> 00:43:06,740 I'm trying to explain it now 1112 00:43:06,740 --> 00:43:08,540 because in a minute you're going to see exactly this. 1113 00:43:10,020 --> 00:43:10,980 We then... 1114 00:43:10,980 --> 00:43:12,900 Oh, I see it right there. 1115 00:43:13,880 --> 00:43:16,960 EB, 0e, aa, aa, aa. 1116 00:43:19,660 --> 00:43:21,860 We use A's just because they're easy. 1117 00:43:22,080 --> 00:43:27,040 They also provide decent NOP codes 1118 00:43:27,040 --> 00:43:29,200 if you have any left over. 1119 00:43:29,360 --> 00:43:33,100 Notice I'm not just sticking in eight A's here 1120 00:43:33,100 --> 00:43:35,000 because eight's really what's going to get clobbered. 1121 00:43:35,080 --> 00:43:38,260 I wanted to show you a little bit more representatively 1122 00:43:38,260 --> 00:43:39,380 of what it's going... 1123 00:43:39,380 --> 00:43:41,680 of the structures involved here. 1124 00:43:41,820 --> 00:43:42,540 So I used A's. 1125 00:43:42,880 --> 00:43:43,820 Or four... 1126 00:43:43,820 --> 00:43:45,140 Three sets of A's. 1127 00:43:46,580 --> 00:43:47,100 And then we're going to go ahead 1128 00:43:47,100 --> 00:43:48,120 and we can do this shorter. 1129 00:43:50,440 --> 00:43:51,320 But there we are. 1130 00:43:53,020 --> 00:43:54,320 Now here's where the gotcha comes. 1131 00:43:54,660 --> 00:43:55,460 And yes, I realize, 1132 00:43:55,520 --> 00:43:56,860 if you want to get up and walk out, 1133 00:43:58,040 --> 00:43:58,660 that's okay. 1134 00:43:58,880 --> 00:43:59,640 I'll forgive you. 1135 00:44:01,780 --> 00:44:05,960 They fixed this in glibc 2.5. 1136 00:44:06,520 --> 00:44:08,720 In fact, I think I've seen it in 2.35. 1137 00:44:11,120 --> 00:44:15,320 Somebody very intelligent adjusted the macro 1138 00:44:15,320 --> 00:44:16,840 so that it says, 1139 00:44:16,840 --> 00:44:20,640 hmm, just before we do this, 1140 00:44:21,180 --> 00:44:22,400 hey, guy to my left, 1141 00:44:22,560 --> 00:44:23,600 are you pointing at me? 1142 00:44:24,060 --> 00:44:24,660 Okay, good. 1143 00:44:25,460 --> 00:44:26,180 Guy to my right? 1144 00:44:26,620 --> 00:44:27,480 Good, we're set. 1145 00:44:27,600 --> 00:44:28,520 Now we can unlink. 1146 00:44:31,320 --> 00:44:34,380 I was hoping that by the time we got here, 1147 00:44:34,520 --> 00:44:36,960 I would have a solution to this problem. 1148 00:44:38,160 --> 00:44:40,340 Buddy of mine, Chris Eagle, 1149 00:44:40,520 --> 00:44:41,380 maybe you've heard of him. 1150 00:44:43,740 --> 00:44:44,620 Rumor has it, 1151 00:44:45,220 --> 00:44:46,820 Chris has a way around it. 1152 00:44:48,180 --> 00:44:49,640 We're going to talk after the con. 1153 00:44:49,900 --> 00:44:51,520 After he's no longer my enemy 1154 00:44:51,520 --> 00:44:52,260 and we're friends again, 1155 00:44:52,380 --> 00:44:53,340 we're going to chat. 1156 00:44:56,600 --> 00:44:58,460 So I wrote a little vulnerable program. 1157 00:44:58,640 --> 00:44:59,820 Actually, it's a toy, 1158 00:44:59,940 --> 00:45:01,100 not just for this presentation. 1159 00:45:01,920 --> 00:45:04,400 So instead of just the very bare minimum 1160 00:45:04,400 --> 00:45:06,080 of what we need, 1161 00:45:06,340 --> 00:45:10,700 I started out and made a five heat buffer program. 1162 00:45:12,620 --> 00:45:15,540 And I freed them in kind of a funky way. 1163 00:45:16,120 --> 00:45:17,380 So take a look. 1164 00:45:18,160 --> 00:45:19,240 Can you guys see this? 1165 00:45:19,480 --> 00:45:20,120 I'm sorry, 1166 00:45:20,240 --> 00:45:21,800 this font doesn't show up very well 1167 00:45:21,800 --> 00:45:22,740 on the projectors here. 1168 00:45:26,020 --> 00:45:27,220 I'm not hearing and complaining, 1169 00:45:27,400 --> 00:45:27,960 so I'm going to go on. 1170 00:45:29,600 --> 00:45:31,400 So hackme.c starts out 1171 00:45:31,400 --> 00:45:33,200 just with the typical include stuff, 1172 00:45:33,440 --> 00:45:34,200 nothing real special, 1173 00:45:34,380 --> 00:45:35,760 standard lib, standard IO strings. 1174 00:45:37,740 --> 00:45:39,020 Then we have a main function. 1175 00:45:39,700 --> 00:45:41,260 Very simple program, actually. 1176 00:45:41,800 --> 00:45:42,720 A main function, 1177 00:45:42,840 --> 00:45:44,640 we set aside five, 1178 00:45:46,600 --> 00:45:49,780 array of five character pointers to, 1179 00:45:50,480 --> 00:45:51,020 I'm sorry, 1180 00:45:51,880 --> 00:45:52,800 yeah, character pointers. 1181 00:45:57,540 --> 00:45:58,380 Yeah, I know. 1182 00:45:58,760 --> 00:45:59,640 Yeah, I know. 1183 00:46:00,060 --> 00:46:00,860 You guys are thinking, 1184 00:46:01,760 --> 00:46:02,340 wait a minute, 1185 00:46:03,000 --> 00:46:05,240 a buffer or an array 1186 00:46:06,440 --> 00:46:08,300 kind of makes it so I don't have to use pointers, right? 1187 00:46:08,600 --> 00:46:10,240 And pointers mean I don't have to use an array. 1188 00:46:10,340 --> 00:46:12,900 But actually we are storing the pointers themselves. 1189 00:46:13,180 --> 00:46:14,080 So that's why they're five. 1190 00:46:15,080 --> 00:46:16,480 We also have a loop counter. 1191 00:46:17,080 --> 00:46:18,980 And we loop zero to five. 1192 00:46:19,320 --> 00:46:21,180 So zero, one, two, three, four. 1193 00:46:22,020 --> 00:46:23,260 Okay, no, not five. 1194 00:46:24,240 --> 00:46:25,260 And we create 1195 00:46:27,620 --> 00:46:29,240 some heap buffers using 1196 00:46:29,240 --> 00:46:32,780 the call to calc or c-alloc. 1197 00:46:33,680 --> 00:46:34,860 You've got m-alloc, 1198 00:46:35,000 --> 00:46:36,020 you've got c-alloc, 1199 00:46:36,120 --> 00:46:37,480 you've got several others 1200 00:46:37,480 --> 00:46:40,040 that they all do different things. 1201 00:46:40,100 --> 00:46:42,820 C-alloc is intended to 1202 00:46:43,880 --> 00:46:45,920 provide multiple buffers in a row 1203 00:46:45,920 --> 00:46:47,020 with one call. 1204 00:46:47,540 --> 00:46:48,880 And it zeroes them out 1205 00:46:48,880 --> 00:46:50,200 for those of you who really care. 1206 00:46:52,280 --> 00:46:54,660 Then we do a get s into each buffer. 1207 00:46:55,480 --> 00:46:58,400 Starting at four and going down to zero. 1208 00:46:59,140 --> 00:47:00,760 Just to throw in an extra kink. 1209 00:47:00,980 --> 00:47:02,360 Let us play around a little bit. 1210 00:47:03,000 --> 00:47:05,020 And then we have two loops here 1211 00:47:05,020 --> 00:47:06,860 that print out the contents 1212 00:47:06,860 --> 00:47:09,200 and free our buffers. 1213 00:47:09,600 --> 00:47:10,900 Notice I'm stepping by two. 1214 00:47:11,200 --> 00:47:12,280 So I'm doing the, 1215 00:47:12,280 --> 00:47:14,240 the even ones first 1216 00:47:14,240 --> 00:47:15,880 and the odd ones second. 1217 00:47:17,000 --> 00:47:17,940 Y'all with me still? 1218 00:47:19,180 --> 00:47:20,500 I wanted to create a tool 1219 00:47:20,940 --> 00:47:23,800 that could provide more than just 1220 00:47:23,800 --> 00:47:25,540 a demonstration for this class, 1221 00:47:25,780 --> 00:47:26,620 or class, 1222 00:47:26,800 --> 00:47:27,880 this talk. 1223 00:47:28,360 --> 00:47:29,640 But you can take home with you, 1224 00:47:29,700 --> 00:47:30,300 you can play with it 1225 00:47:30,300 --> 00:47:31,660 and have fun 1226 00:47:31,660 --> 00:47:33,080 and learn on your own 1227 00:47:33,080 --> 00:47:34,860 and not have to rewrite your own program. 1228 00:47:35,040 --> 00:47:36,680 I hate writing test programs. 1229 00:47:37,140 --> 00:47:38,100 They're really annoying. 1230 00:47:38,280 --> 00:47:39,780 It's like, I just want to do it. 1231 00:47:39,920 --> 00:47:41,020 So can you give me something? 1232 00:47:42,440 --> 00:47:44,640 Who read the Erickson book, 1233 00:47:44,720 --> 00:47:45,860 Hacking the Art of Exploitation? 1234 00:47:47,320 --> 00:47:48,000 Excellent book. 1235 00:47:48,180 --> 00:47:48,720 I love it. 1236 00:47:49,060 --> 00:47:50,540 In fact, I like it better. 1237 00:47:50,640 --> 00:47:51,740 And don't get me wrong. 1238 00:47:52,040 --> 00:47:53,620 I like it better than 1239 00:47:54,180 --> 00:47:55,260 shell coders handbook. 1240 00:47:56,020 --> 00:47:57,080 And don't throw eggs. 1241 00:47:57,240 --> 00:47:58,340 Shell coders is great. 1242 00:47:59,160 --> 00:48:00,420 Erickson just really understood 1243 00:48:00,420 --> 00:48:01,600 how to say, 1244 00:48:02,160 --> 00:48:03,700 these guys need some examples. 1245 00:48:03,780 --> 00:48:04,720 They need some hands on. 1246 00:48:05,060 --> 00:48:08,140 And they needed a very explicit teaching 1247 00:48:08,140 --> 00:48:09,680 of what's going on. 1248 00:48:09,820 --> 00:48:10,960 And he did an exceptional job. 1249 00:48:10,960 --> 00:48:12,600 The guys who did shell coders are awesome. 1250 00:48:13,080 --> 00:48:14,320 Some of them are my friends. 1251 00:48:14,500 --> 00:48:15,400 I'm not picking on them. 1252 00:48:15,700 --> 00:48:17,260 They write more depth. 1253 00:48:17,760 --> 00:48:19,760 But Erickson did awesome with his examples. 1254 00:48:19,980 --> 00:48:20,860 And I keep turning that off. 1255 00:48:22,400 --> 00:48:23,280 So HackMeDetails. 1256 00:48:23,440 --> 00:48:24,740 We compile this down 1257 00:48:24,740 --> 00:48:26,080 on a Linux system 1258 00:48:26,800 --> 00:48:27,680 using GCC. 1259 00:48:28,420 --> 00:48:29,960 This tells you exactly what I did. 1260 00:48:31,400 --> 00:48:32,320 We get a warning. 1261 00:48:32,560 --> 00:48:34,180 The get us function is dangerous 1262 00:48:34,180 --> 00:48:35,340 and should not be used. 1263 00:48:35,620 --> 00:48:37,360 Well, then it should be pulled from the library, 1264 00:48:37,380 --> 00:48:37,940 you idiot. 1265 00:48:40,960 --> 00:48:41,940 So I get a warning. 1266 00:48:42,060 --> 00:48:43,920 Why would a developer ignore a warning? 1267 00:48:46,680 --> 00:48:48,860 Then I use OBSDUMP, 1268 00:48:48,980 --> 00:48:51,760 which is a very cool binary manipulation, 1269 00:48:52,800 --> 00:48:54,500 a binary analysis tool 1270 00:48:54,500 --> 00:48:56,700 for ELF binaries specifically, 1271 00:48:57,080 --> 00:48:58,160 but they do Windows too. 1272 00:48:58,920 --> 00:48:59,560 OBSDUMP, 1273 00:48:59,680 --> 00:49:02,920 using the R command, 1274 00:49:03,060 --> 00:49:05,880 gives me the global offset table. 1275 00:49:06,640 --> 00:49:07,920 I take a look here. 1276 00:49:08,100 --> 00:49:09,360 It looks like the free, 1277 00:49:11,380 --> 00:49:11,820 free function. 1278 00:49:13,400 --> 00:49:15,020 Its location is stored 1279 00:49:16,780 --> 00:49:17,660 in 804964. 1280 00:49:18,060 --> 00:49:21,040 Notice I didn't say it's going to be loaded at 804964. 1281 00:49:21,580 --> 00:49:23,460 Its pointer will be stored there. 1282 00:49:27,740 --> 00:49:28,920 This is on your CD 1283 00:49:30,140 --> 00:49:31,160 for DEF CON, 1284 00:49:31,360 --> 00:49:33,320 so don't worry if you can't see all this. 1285 00:49:33,520 --> 00:49:35,760 I'm going to tell you what you need to see. 1286 00:49:37,760 --> 00:49:40,480 A clean heap memory allocation 1287 00:49:40,960 --> 00:49:42,040 of our program, mind you, 1288 00:49:42,080 --> 00:49:43,260 not just some ethereal thing. 1289 00:49:45,480 --> 00:49:48,340 Here is the 32-bit integer 1290 00:49:48,340 --> 00:49:50,260 that marks the beginning of our first buffer. 1291 00:49:50,860 --> 00:49:52,060 And if you can read it, 1292 00:49:52,100 --> 00:49:55,580 it says 00000071. 1293 00:49:58,860 --> 00:50:00,340 You hop down a little bit, 1294 00:50:00,400 --> 00:50:01,200 and if you can see it, 1295 00:50:01,260 --> 00:50:02,700 it also says, 1296 00:50:03,140 --> 00:50:05,940 oh, right here, 00000071. 1297 00:50:06,180 --> 00:50:07,200 That's our second buffer. 1298 00:50:10,200 --> 00:50:13,360 How many bytes between this and that? 1299 00:50:15,100 --> 00:50:15,980 I'll take hex. 1300 00:50:16,220 --> 00:50:18,380 If you don't want to do the conversion in your head, 1301 00:50:18,460 --> 00:50:20,000 I know some of you are probably just 1302 00:50:20,000 --> 00:50:21,580 turn that function off while you're here. 1303 00:50:23,000 --> 00:50:23,420 Yes. 1304 00:50:23,900 --> 00:50:24,680 70 hex. 1305 00:50:26,980 --> 00:50:28,200 Pop down here a little bit, 1306 00:50:28,240 --> 00:50:29,060 see the same thing. 1307 00:50:29,800 --> 00:50:31,080 And a couple more. 1308 00:50:31,160 --> 00:50:32,120 I only had so much room. 1309 00:50:33,960 --> 00:50:34,380 So, 1310 00:50:35,010 --> 00:50:36,200 71 means, 1311 00:50:37,260 --> 00:50:38,040 70 hex, 1312 00:50:39,020 --> 00:50:41,740 or that with previous in use of 1. 1313 00:50:43,640 --> 00:50:44,880 70 actually converts, 1314 00:50:44,980 --> 00:50:47,620 if you pop up Kcalc, 1315 00:50:47,640 --> 00:50:49,240 if you're using KDE like I do, 1316 00:50:50,100 --> 00:50:51,300 it converts to 112. 1317 00:50:53,480 --> 00:50:53,920 Wow. 1318 00:50:55,820 --> 00:50:56,780 Everybody over here, 1319 00:50:56,840 --> 00:50:57,560 look over here. 1320 00:50:57,620 --> 00:50:58,480 This projector is better. 1321 00:50:58,600 --> 00:50:59,040 I like it. 1322 00:50:59,820 --> 00:51:00,720 I can read it. 1323 00:51:02,080 --> 00:51:03,460 So I wrote this little exploit. 1324 00:51:04,000 --> 00:51:04,720 Simple piece. 1325 00:51:05,840 --> 00:51:07,120 It's in what language? 1326 00:51:09,180 --> 00:51:09,660 Yeah. 1327 00:51:10,280 --> 00:51:11,880 It's my latest love. 1328 00:51:12,760 --> 00:51:13,240 Python. 1329 00:51:13,700 --> 00:51:14,640 Import the OS. 1330 00:51:15,160 --> 00:51:15,640 Module. 1331 00:51:15,840 --> 00:51:16,500 Import struct. 1332 00:51:17,020 --> 00:51:18,500 I put in some shellcode. 1333 00:51:22,700 --> 00:51:23,920 10 byte shellcode. 1334 00:51:24,040 --> 00:51:24,240 Anybody? 1335 00:51:24,460 --> 00:51:25,080 Is that not slick? 1336 00:51:25,440 --> 00:51:26,040 Okay, no. 1337 00:51:26,160 --> 00:51:27,560 Really, it's a lot longer. 1338 00:51:27,600 --> 00:51:28,260 I chopped it. 1339 00:51:29,840 --> 00:51:30,520 I do, 1340 00:51:30,700 --> 00:51:32,580 I create a variable called at. 1341 00:51:32,780 --> 00:51:33,420 It says, 1342 00:51:33,420 --> 00:51:35,100 this is where I want to overwrite. 1343 00:51:35,880 --> 00:51:36,280 2, 1344 00:51:37,260 --> 00:51:38,500 this is what I want to change it to. 1345 00:51:39,080 --> 00:51:39,980 I want to point here. 1346 00:51:42,580 --> 00:51:44,200 And that's where my shellcode ends up. 1347 00:51:45,800 --> 00:51:47,380 I then create at addy, 1348 00:51:47,420 --> 00:51:49,760 which just converts this from a Python, 1349 00:51:50,580 --> 00:51:53,300 a Python number into an actual byte. 1350 00:51:55,080 --> 00:51:55,480 And, 1351 00:51:55,900 --> 00:51:57,300 I do the same thing to 2. 1352 00:51:58,700 --> 00:51:59,980 Then I open my program. 1353 00:52:01,080 --> 00:52:02,460 Shove in a whole bunch of crap. 1354 00:52:02,460 --> 00:52:04,480 And I get a shell on the other, 1355 00:52:04,600 --> 00:52:05,140 or I get a, 1356 00:52:05,160 --> 00:52:07,040 actually a listener on the other end. 1357 00:52:09,240 --> 00:52:10,760 The most important part here though is, 1358 00:52:11,820 --> 00:52:13,840 you'll see I'm writing to one buffer here 1359 00:52:13,840 --> 00:52:14,780 and another buffer there. 1360 00:52:15,260 --> 00:52:16,000 That's just fluff. 1361 00:52:16,940 --> 00:52:18,420 But then I write to the important one. 1362 00:52:18,480 --> 00:52:20,820 I've selected one to be my buffer overflow. 1363 00:52:21,960 --> 00:52:23,000 So I start out with, 1364 00:52:28,020 --> 00:52:29,700 an address that doesn't, 1365 00:52:29,720 --> 00:52:30,680 no, wait a minute here. 1366 00:52:32,940 --> 00:52:33,920 This is the shellcode. 1367 00:52:34,060 --> 00:52:34,620 Forgive me. 1368 00:52:35,140 --> 00:52:36,400 We start out with the, 1369 00:52:36,400 --> 00:52:38,780 with the jump over the crap that gets nailed. 1370 00:52:39,800 --> 00:52:41,320 We then do our shellcode. 1371 00:52:42,380 --> 00:52:43,480 And we terminate, 1372 00:52:43,500 --> 00:52:44,720 we terminate it with a, 1373 00:52:45,660 --> 00:52:46,300 carriage return. 1374 00:52:47,520 --> 00:52:48,680 We write the next buffer. 1375 00:52:48,840 --> 00:52:50,700 And then I just selected the last one 1376 00:52:50,700 --> 00:52:51,560 to be our buffer overflow. 1377 00:52:51,860 --> 00:52:53,020 So we start out with, 1378 00:52:53,220 --> 00:52:54,120 this is a, 1379 00:52:54,140 --> 00:52:56,140 this is an example that you should have on your CD. 1380 00:52:56,380 --> 00:52:59,200 It only focuses on collapsing, 1381 00:53:00,280 --> 00:53:01,320 collapsing in one direction. 1382 00:53:02,460 --> 00:53:03,400 I've written several. 1383 00:53:03,500 --> 00:53:04,000 They're all there. 1384 00:53:04,500 --> 00:53:05,120 Okay, thanks. 1385 00:53:07,520 --> 00:53:08,880 So we start out with our buffer. 1386 00:53:09,160 --> 00:53:10,240 We do our number four, 1387 00:53:10,420 --> 00:53:10,920 negative four, 1388 00:53:10,980 --> 00:53:11,480 negative four. 1389 00:53:12,080 --> 00:53:13,220 And then we stick our addresses. 1390 00:53:14,140 --> 00:53:15,280 Flush it and we're good. 1391 00:53:17,540 --> 00:53:18,120 This is what, 1392 00:53:18,120 --> 00:53:21,340 this is what the stack looks like before we free. 1393 00:53:22,420 --> 00:53:23,300 I'm sorry, the heap. 1394 00:53:24,940 --> 00:53:28,520 So we filled up one section of heap with hex four five, 1395 00:53:28,660 --> 00:53:29,180 which is an E. 1396 00:53:30,240 --> 00:53:30,940 We have our number, 1397 00:53:31,020 --> 00:53:31,540 negative four. 1398 00:53:32,460 --> 00:53:32,900 This is pad zero. 1399 00:53:33,000 --> 00:53:34,620 This converts to pad zero in ASCII. 1400 00:53:35,160 --> 00:53:36,080 Negative four. 1401 00:53:37,420 --> 00:53:39,380 Our number minus 12. 1402 00:53:40,340 --> 00:53:42,500 That's because you go one direction. 1403 00:53:42,680 --> 00:53:45,320 You're going to be offset by 12 bytes 1404 00:53:46,180 --> 00:53:48,920 because it's not just the start of the next heap 1405 00:53:48,920 --> 00:53:50,000 that you're talking about. 1406 00:53:50,080 --> 00:53:52,100 It's an offset from the heap. 1407 00:53:54,360 --> 00:53:56,100 Pointer to shellcode comes next. 1408 00:53:57,160 --> 00:53:59,600 And right here we're about to clobber that spot. 1409 00:53:59,780 --> 00:54:01,340 And our shellcode starts there. 1410 00:54:01,900 --> 00:54:03,480 Notice, got entry, 1411 00:54:05,880 --> 00:54:06,360 804834E. 1412 00:54:06,520 --> 00:54:07,800 So this is where the, 1413 00:54:08,440 --> 00:54:09,700 I forget what I grabbed here, 1414 00:54:09,780 --> 00:54:10,120 the free. 1415 00:54:10,340 --> 00:54:13,820 This is where free lives in virtual memory space. 1416 00:54:14,980 --> 00:54:15,820 After free, 1417 00:54:17,080 --> 00:54:21,400 we still have pointer to shellcode. 1418 00:54:22,440 --> 00:54:23,900 Still have some negative fours, 1419 00:54:23,900 --> 00:54:24,240 other stuff. 1420 00:54:24,460 --> 00:54:26,680 You'll notice this used to be pad zero. 1421 00:54:26,780 --> 00:54:27,700 It's now an address. 1422 00:54:28,960 --> 00:54:30,440 The start of our shellcode, 1423 00:54:30,440 --> 00:54:31,940 which jumps over that crud, 1424 00:54:31,960 --> 00:54:32,960 couple not codes, 1425 00:54:33,320 --> 00:54:34,260 and our shellcode. 1426 00:54:36,940 --> 00:54:41,160 Next call to free is going to be sent to 804AOE8. 1427 00:54:41,420 --> 00:54:42,220 And if you look up here, 1428 00:54:44,920 --> 00:54:45,320 804AOE0. 1429 00:54:45,820 --> 00:54:47,420 Eight is halfway through. 1430 00:54:47,720 --> 00:54:49,080 It's going to point to right there. 1431 00:54:53,960 --> 00:54:55,540 So I was going to demo it. 1432 00:54:55,660 --> 00:54:58,240 I'm thankful that I had the slides 1433 00:54:58,240 --> 00:55:00,100 that show what the demo was going to be, basically. 1434 00:55:01,160 --> 00:55:01,980 Here's my Python, 1435 00:55:02,180 --> 00:55:02,900 interactive Python, 1436 00:55:03,360 --> 00:55:04,500 the hacking tool champions. 1437 00:55:06,560 --> 00:55:08,580 And netcat to port 4444. 1438 00:55:08,840 --> 00:55:09,740 Thank you, HG. 1439 00:55:11,740 --> 00:55:12,760 I type the ID, 1440 00:55:13,500 --> 00:55:14,640 or I type ID, 1441 00:55:14,820 --> 00:55:15,560 and hey, 1442 00:55:15,700 --> 00:55:18,020 the program ID runs on the other box. 1443 00:55:20,100 --> 00:55:20,540 Demo, 1444 00:55:20,540 --> 00:55:21,480 not going to get to it. 1445 00:55:21,520 --> 00:55:22,280 Sorry, I got two minutes. 1446 00:55:23,480 --> 00:55:24,560 I was going to demo. 1447 00:55:24,820 --> 00:55:26,040 It still makes me stupid. 1448 00:55:27,960 --> 00:55:28,820 Here's the bad news. 1449 00:55:28,900 --> 00:55:29,700 I talked about it already. 1450 00:55:29,700 --> 00:55:32,040 Somebody very intelligent said, 1451 00:55:32,220 --> 00:55:32,300 hey, 1452 00:55:32,460 --> 00:55:34,060 if these guys aren't pointing at me, 1453 00:55:34,720 --> 00:55:35,260 bomb. 1454 00:55:36,200 --> 00:55:37,680 Corrupted double-linked list. 1455 00:55:39,020 --> 00:55:40,280 So what do we have left? 1456 00:55:40,960 --> 00:55:41,460 Well, 1457 00:55:41,580 --> 00:55:43,320 we've already talked about them a bit. 1458 00:55:44,180 --> 00:55:45,260 Variable modification, 1459 00:55:47,320 --> 00:55:49,240 end of string termination overwrite, 1460 00:55:50,880 --> 00:55:51,920 or be creative. 1461 00:55:53,040 --> 00:55:53,960 First of all, 1462 00:55:54,900 --> 00:55:56,740 heap management is a complex thing. 1463 00:55:56,840 --> 00:55:59,000 There are many aspects to it. 1464 00:55:59,000 --> 00:56:01,260 We've only started to scratch the surface. 1465 00:56:01,700 --> 00:56:02,840 There's more out there. 1466 00:56:04,400 --> 00:56:06,460 Creativity is how we first discovered 1467 00:56:06,460 --> 00:56:08,040 how to leverage buffer overflows. 1468 00:56:08,300 --> 00:56:10,580 It's about getting something 1469 00:56:11,300 --> 00:56:12,620 and then leveraging it. 1470 00:56:13,460 --> 00:56:13,940 Pentesters, 1471 00:56:14,140 --> 00:56:15,960 what do you do when you nail a box? 1472 00:56:16,260 --> 00:56:16,820 You say, 1473 00:56:16,900 --> 00:56:17,220 woohoo, 1474 00:56:17,300 --> 00:56:17,900 call the client. 1475 00:56:18,180 --> 00:56:19,500 No, you pivot. 1476 00:56:20,080 --> 00:56:21,760 You then leverage what you have. 1477 00:56:24,120 --> 00:56:26,280 A couple resources that don't show up at all. 1478 00:56:26,700 --> 00:56:28,140 They are on the CD. 1479 00:56:28,140 --> 00:56:29,720 You should click them from there 1480 00:56:29,720 --> 00:56:30,520 or print them off. 1481 00:56:32,520 --> 00:56:33,940 Special thanks to my creator, 1482 00:56:34,060 --> 00:56:34,640 my family, 1483 00:56:34,780 --> 00:56:36,000 to Intel Guardians, 1484 00:56:36,180 --> 00:56:37,320 who are dear friends of mine, 1485 00:56:38,240 --> 00:56:38,720 Kenshodo, 1486 00:56:38,840 --> 00:56:41,380 for giving me an opportunity to get a start, 1487 00:56:42,340 --> 00:56:43,380 and last place, 1488 00:56:43,620 --> 00:56:45,740 some of my closest friends and my team. 1489 00:56:47,300 --> 00:56:48,160 Some graffiti, 1490 00:56:48,980 --> 00:56:50,120 because graffiti is cool. 1491 00:56:51,540 --> 00:56:52,020 Bug, 1492 00:56:52,100 --> 00:56:52,520 ringwraith, 1493 00:56:52,620 --> 00:56:52,900 menace, 1494 00:56:53,000 --> 00:56:53,160 jewel. 1495 00:56:54,640 --> 00:56:55,780 Thank you very much, guys. 1496 00:56:57,560 --> 00:56:58,920 Some of this stuff was stolen. 1497 00:56:59,760 --> 00:57:00,860 You need to give credit, 1498 00:57:00,980 --> 00:57:01,920 otherwise you'll get sued. 1499 00:57:02,460 --> 00:57:04,160 Some people actually do know who I am. 1500 00:57:05,780 --> 00:57:06,880 Thank you very much. 1501 00:57:07,240 --> 00:57:08,220 It's been a great crowd. 1502 00:57:08,620 --> 00:57:09,440 I enjoyed it.