1 00:00:00,000 --> 00:00:09,800 My name is Sean Webb. I'm a member of Soldier X, and this talk is called Runtime Process Insemination, and it's brought to you by Soldier X. 2 00:00:10,520 --> 00:00:21,220 Who am I? I am just another random blogger on the inner tubes. I run a tech blog called 0xfeedface.org, and I'm a professional security analyst. 3 00:00:21,220 --> 00:00:33,300 I've been working professionally for about three and a half to four years. I'm a 12-year C89 programmer. It is the language of love. I love it, absolutely. 4 00:00:35,400 --> 00:00:51,200 I'm a member of Soldier X, Binary Revolution, and Hack3R. All three of them are kind of sister communities related to each other, and they're really fun, really a good part, really good communities to be in. 5 00:00:51,220 --> 00:00:52,400 I'm a member of Soldier X, Binary Revolution, and Hack3R. All three of them are kind of sister communities related to each other, and they're really fun, really good communities to be in. 6 00:00:52,400 --> 00:00:58,040 If you guys want to hang around on IRC with like-minded people, check those communities out. 7 00:00:59,520 --> 00:01:11,060 I have a few disclaimers in my talk. The opinions and views expressed here are solely mine, not my employer's. They're not paying me to speak here. 8 00:01:11,660 --> 00:01:19,520 My talk is semi-random. There's a lot of things that I need to cover, a lot of background information that I need to cover. 9 00:01:20,220 --> 00:01:21,200 But I'm going to take a few minutes and I'm going to talk about some of the things that I need to cover. 10 00:01:21,220 --> 00:01:24,620 But I'm going to tie it together at the end. It's all going to make sense at the end. 11 00:01:25,220 --> 00:01:37,540 In this talk, almost nothing new is explained. The theory is very well known. The underlying theory you can find on Frack, Google, and quite a few other resources. 12 00:01:38,540 --> 00:01:44,420 I am today, though, going to explain a slightly new technique, a new spin on things. 13 00:01:46,140 --> 00:01:51,080 The presentation and tools discussed here are only for education. 14 00:01:52,640 --> 00:01:54,960 I'm just saying this to cover my butt. 15 00:01:57,200 --> 00:02:01,760 I make a few assumptions in this talk. I assume you know what Linux is. 16 00:02:01,940 --> 00:02:09,460 And even if you don't, I'm sorry, the same concepts I talk about here are going to carry over to Windows and OS X. 17 00:02:10,680 --> 00:02:15,380 I assume you have a basic knowledge of C and 32-bit Linux memory management. 18 00:02:15,760 --> 00:02:20,820 The concepts here apply both to 32- and 64-bit Linux. 19 00:02:21,220 --> 00:02:25,200 And any other OS you want to use. 20 00:02:26,160 --> 00:02:32,660 I assume you know what printf, memset, receive, libc, that kind of stuff is. 21 00:02:32,780 --> 00:02:41,540 I assume you know what a heap and a stack are, what an anonymous memory mapping is, what the difference between all three of those are, that kind of thing. 22 00:02:41,860 --> 00:02:45,380 I assume you have the ability and desire to think abstractly. 23 00:02:45,420 --> 00:02:50,520 We're going to be talking about some abstract ideas here, and sometimes in parallel. 24 00:02:51,220 --> 00:02:52,480 It can get kind of confusing. 25 00:02:52,800 --> 00:03:00,980 If you get confused, I will be in the Q&A room for Track 3 after this talk. 26 00:03:03,880 --> 00:03:11,660 This presentation and the tool that we will release at the end of the day today assume a non-modified memory layout. 27 00:03:11,820 --> 00:03:15,000 So that means no GRSEC, no PACS, no ASLR. 28 00:03:15,320 --> 00:03:19,460 And even if you do have a modified memory layout, 29 00:03:19,460 --> 00:03:21,900 you can get past that. 30 00:03:22,080 --> 00:03:23,260 Even GRSEC. 31 00:03:23,480 --> 00:03:25,540 It takes a bit of work, but it's possible. 32 00:03:26,620 --> 00:03:28,040 Give a little bit of history. 33 00:03:28,740 --> 00:03:33,520 For the past few years, I've been diving into CGI and web application vulnerabilities. 34 00:03:34,060 --> 00:03:36,340 And I used connect-back shellcode. 35 00:03:37,900 --> 00:03:45,320 So the shellcode, after I exploited it, I'd inject the shellcode into the web app, and it would connect back to me and drop me into a shell. 36 00:03:45,780 --> 00:03:47,980 I needed reliable, random access. 37 00:03:48,080 --> 00:03:49,440 So I needed to be able to break it down. 38 00:03:49,460 --> 00:03:50,500 I needed to be dropped into a shell. 39 00:03:50,740 --> 00:03:52,020 No matter where I'm at. 40 00:03:52,100 --> 00:03:58,860 If I'm at home, if I'm in my friend's place, if I'm in a hotel, McDonald's, it doesn't matter. 41 00:03:59,040 --> 00:04:01,580 I need to be able to get into that shell. 42 00:04:01,900 --> 00:04:04,260 So firewall holes are a bit of a problem. 43 00:04:04,680 --> 00:04:10,680 Because if I'm at a friend's place, if I'm at McDonald's, if I'm not at home, I don't control the network. 44 00:04:11,020 --> 00:04:17,060 I could pwn the network and gain control of it, but I really just want to take care of my end goal, my target. 45 00:04:17,980 --> 00:04:22,180 I needed a way to reuse existing connections to the web server. 46 00:04:23,740 --> 00:04:26,920 I also needed a way to covertly sniff traffic. 47 00:04:27,140 --> 00:04:32,120 There was a time when the traffic was wrapped in SSL for HTTPS. 48 00:04:33,000 --> 00:04:37,520 And I needed to be able to grab the decrypted SSL traffic. 49 00:04:37,940 --> 00:04:40,200 So I created a project called lib-hijack. 50 00:04:40,260 --> 00:04:42,440 And we're going to discuss that a little bit more. 51 00:04:42,620 --> 00:04:45,360 And we're going to release a new version at DEF CON today. 52 00:04:46,140 --> 00:04:47,040 If the network doesn't work. 53 00:04:47,060 --> 00:04:47,480 If the network permits. 54 00:04:50,080 --> 00:04:54,480 So to set the stage, I've got a shell via a CGI web application exploit. 55 00:04:54,680 --> 00:04:56,600 And I need a reliable way to get back in. 56 00:04:57,120 --> 00:04:59,480 Apache is a really good candidate to target. 57 00:04:59,680 --> 00:05:02,040 Because it's already listening for connections. 58 00:05:02,620 --> 00:05:06,340 It's already got its front door open saying, come right on in. 59 00:05:06,820 --> 00:05:11,760 So what I need to do, my end goal, is to modify the Apache process somehow. 60 00:05:12,080 --> 00:05:15,180 To run a shell when a special string is sent. 61 00:05:17,060 --> 00:05:21,340 So when I send it get slash shell, HP slash 1.1. 62 00:05:21,480 --> 00:05:23,120 It's going to drop me into a shell. 63 00:05:23,240 --> 00:05:25,420 So if I run the whoami command, it's going to say Apache. 64 00:05:26,700 --> 00:05:30,600 So in order to do that, in order to accomplish that end goal. 65 00:05:30,880 --> 00:05:33,580 I need to hook certain functions in runtime. 66 00:05:35,360 --> 00:05:40,220 And there's some curtain techniques to getting your shell code stored and run. 67 00:05:43,500 --> 00:05:45,820 You can store your shell code on the stack. 68 00:05:47,560 --> 00:05:50,040 And that used to be really popular. 69 00:05:51,300 --> 00:05:57,300 Aleph1's article, Smashing the Stack for Fun and Profit, uses the stack. 70 00:05:57,440 --> 00:06:00,540 And that's where most attackers used to put their code. 71 00:06:01,420 --> 00:06:04,800 However, on most systems today, the stack is non-executable. 72 00:06:06,160 --> 00:06:07,500 So you can't really do that. 73 00:06:07,520 --> 00:06:08,680 That's not really reliable. 74 00:06:09,180 --> 00:06:13,300 You could store your shell code at the current instruction pointer, EIP. 75 00:06:14,400 --> 00:06:15,540 There's a problem with that. 76 00:06:15,580 --> 00:06:16,800 That would work. 77 00:06:17,060 --> 00:06:20,840 However, it mucks up the original code. 78 00:06:21,040 --> 00:06:26,260 So what you have to do then is back up the code that was going to run. 79 00:06:26,740 --> 00:06:28,280 Overwrite it with your shell code. 80 00:06:28,600 --> 00:06:34,340 And then after your shell code is run, restore the backed up. 81 00:06:34,480 --> 00:06:38,720 In the previous talk, in the JewGod talk, that's what the author did. 82 00:06:38,940 --> 00:06:43,480 But we're going to dive a little bit further into a new technique. 83 00:06:45,080 --> 00:06:47,040 And the problem with this technique is, 84 00:06:47,060 --> 00:06:49,640 you get your shell code only to run once. 85 00:06:49,880 --> 00:06:53,200 Your shell code can only run once in this paradigm, 86 00:06:53,400 --> 00:06:55,400 in this way, in this mode of thinking. 87 00:06:55,840 --> 00:06:59,440 But we need, every time that string is sent, 88 00:07:00,640 --> 00:07:03,740 get slash shell HP slash 1.1, 89 00:07:05,180 --> 00:07:09,180 our shell code, our malicious code needs to run. 90 00:07:09,360 --> 00:07:11,860 So it needs to run multiple times, not just once. 91 00:07:12,820 --> 00:07:14,940 We could store the shell code on the heap, 92 00:07:14,940 --> 00:07:19,180 but also the heap on a lot of systems today is non-executable. 93 00:07:19,300 --> 00:07:23,840 It's becoming more popular to set your heap as non-executable. 94 00:07:24,980 --> 00:07:26,760 You could use LD preload, 95 00:07:27,260 --> 00:07:29,000 and that would be a great technique, 96 00:07:29,100 --> 00:07:30,560 but that requires root, 97 00:07:31,300 --> 00:07:33,340 and the process has already started. 98 00:07:33,440 --> 00:07:34,320 Apache is already running. 99 00:07:34,540 --> 00:07:38,440 And you would need to gain root on the system in order to do that. 100 00:07:38,620 --> 00:07:41,500 And really, you don't really ever need to root a box 101 00:07:41,500 --> 00:07:43,740 unless you're doing some crazy things. 102 00:07:46,460 --> 00:07:48,980 So we're going to talk a little bit about process loading, 103 00:07:49,100 --> 00:07:53,200 what happens when the execve system call is called. 104 00:07:54,060 --> 00:07:57,120 The kernel first checks for file existence. 105 00:07:57,420 --> 00:07:58,980 Make sure that the file is there 106 00:07:58,980 --> 00:08:01,040 and that you have the permissions to run it. 107 00:08:02,300 --> 00:08:05,320 The kernel then loads what is called the runtime linker. 108 00:08:05,440 --> 00:08:07,620 A lot of people call that the RTLD. 109 00:08:09,140 --> 00:08:11,280 The kernel loads the process metadata 110 00:08:11,280 --> 00:08:13,100 and initializes the stack. 111 00:08:13,420 --> 00:08:14,920 The metadata is located in the stack, 112 00:08:14,940 --> 00:08:18,560 located at that hex address at 08048000 113 00:08:18,560 --> 00:08:20,700 on Intel 32-bit Linux. 114 00:08:22,040 --> 00:08:23,660 And the runtime linker 115 00:08:23,660 --> 00:08:28,100 is going to finish loading the process into memory. 116 00:08:28,980 --> 00:08:31,200 It loads all the shared objects, 117 00:08:31,280 --> 00:08:32,140 all the dependencies. 118 00:08:32,740 --> 00:08:35,720 For you Windows guys, those are DLLs. 119 00:08:36,180 --> 00:08:39,400 And it finds those shared objects 120 00:08:39,400 --> 00:08:43,080 by looking at the .dynamics section of your binary 121 00:08:43,080 --> 00:08:44,660 for the DT needed entries. 122 00:08:44,940 --> 00:08:47,860 It then patches what is called the procedure linkage table 123 00:08:47,860 --> 00:08:50,600 and global offset table for needed dynamic functions. 124 00:08:50,900 --> 00:08:54,480 We're going to dive deep into the PLT-GOT later on in the talk. 125 00:08:55,240 --> 00:08:57,640 It then calls the initialization routines 126 00:08:57,640 --> 00:08:59,360 and then finally calls main. 127 00:08:59,420 --> 00:09:03,760 Finally returns control back to your actual program. 128 00:09:05,000 --> 00:09:05,560 ELF. 129 00:09:05,880 --> 00:09:07,180 Best movie ever. 130 00:09:07,380 --> 00:09:08,580 I love this movie. 131 00:09:08,640 --> 00:09:09,980 I started watching it in November. 132 00:09:10,160 --> 00:09:11,700 This really isn't related to the talk. 133 00:09:12,840 --> 00:09:14,460 I'm big on Christmas. 134 00:09:14,940 --> 00:09:15,640 I started watching it in November. 135 00:09:15,980 --> 00:09:18,040 It is the executable and linkable format. 136 00:09:18,260 --> 00:09:21,260 It was created by Sun for System 5 137 00:09:21,870 --> 00:09:23,100 and Linux adopted it. 138 00:09:24,520 --> 00:09:26,800 Windows file format PECoff 139 00:09:26,800 --> 00:09:29,100 is based off of ELF. 140 00:09:29,200 --> 00:09:33,080 And Mac OS X's format is based off of ELF as well. 141 00:09:34,400 --> 00:09:35,820 All it is is metadata. 142 00:09:36,080 --> 00:09:37,520 It's data that describes data. 143 00:09:37,820 --> 00:09:40,260 It tells the runtime linker what to load 144 00:09:40,260 --> 00:09:41,240 and how to load it. 145 00:09:41,680 --> 00:09:43,340 So it describes where to load, 146 00:09:43,340 --> 00:09:44,780 different parts of the object file. 147 00:09:45,240 --> 00:09:47,860 An object file is just a generic term 148 00:09:47,860 --> 00:09:51,120 that can mean shared object or your program. 149 00:09:51,460 --> 00:09:56,760 Or an object file like when you compile a program 150 00:09:56,760 --> 00:09:58,420 and you create an object file 151 00:09:58,420 --> 00:09:59,580 and then you link them all together. 152 00:09:59,800 --> 00:10:00,540 That kind of thing. 153 00:10:01,680 --> 00:10:05,740 An object file contains a main ELF header 154 00:10:05,740 --> 00:10:08,540 which contains pointers to other headers. 155 00:10:08,680 --> 00:10:11,140 And these other headers are the really important ones. 156 00:10:11,420 --> 00:10:12,900 The other headers include 157 00:10:12,900 --> 00:10:13,780 the process header, 158 00:10:13,920 --> 00:10:16,800 which in order for your object file to be, 159 00:10:18,420 --> 00:10:19,540 to work, 160 00:10:19,680 --> 00:10:22,180 you need one entry minimum. 161 00:10:22,540 --> 00:10:25,080 It contains virtual address locations. 162 00:10:25,200 --> 00:10:26,820 So where in memory it's going to be stored. 163 00:10:26,980 --> 00:10:28,500 It contains access rights. 164 00:10:29,320 --> 00:10:30,660 Read, write, and execute. 165 00:10:30,960 --> 00:10:31,940 And alignment. 166 00:10:32,160 --> 00:10:33,760 Where inside that virtual memory, 167 00:10:33,960 --> 00:10:36,300 virtual address location is it going to be stored. 168 00:10:36,460 --> 00:10:38,400 In the beginning, in the middle, in the end. 169 00:10:39,620 --> 00:10:41,740 You also have section headers. 170 00:10:41,740 --> 00:10:43,560 You don't have to have any section headers. 171 00:10:44,580 --> 00:10:48,180 All it does is it describes the P headers. 172 00:10:48,400 --> 00:10:50,760 It contains a string table and debugging entries, 173 00:10:50,880 --> 00:10:52,180 if any, in compiler comments. 174 00:10:52,480 --> 00:10:53,940 When you strip a program, 175 00:10:54,080 --> 00:10:55,640 you're stripping out most, if not all, 176 00:10:55,720 --> 00:10:56,640 of the section headers. 177 00:10:57,440 --> 00:10:58,800 Fun little trivia. 178 00:10:59,500 --> 00:11:02,420 Old school viruses used to store their code 179 00:11:02,420 --> 00:11:05,360 in the compiler comments section header. 180 00:11:07,300 --> 00:11:10,120 Dynamic headers contain the relocation entries. 181 00:11:10,120 --> 00:11:12,540 And he stubs in the PLT GOT. 182 00:11:12,820 --> 00:11:13,960 And that is the jackpot. 183 00:11:14,060 --> 00:11:15,760 That's where we're going to be spending most of our time. 184 00:11:18,380 --> 00:11:21,020 Ptrace is the debugging facility for Linux. 185 00:11:22,680 --> 00:11:24,620 It is a kernel system call. 186 00:11:24,760 --> 00:11:29,300 And GDB, the GNU debugger, relies heavily on Ptrace. 187 00:11:29,820 --> 00:11:32,820 If Ptrace didn't exist, GDB wouldn't. 188 00:11:33,240 --> 00:11:35,740 It allows you to read and write from and to 189 00:11:35,740 --> 00:11:38,820 arbitrary memory locations, if it's valid. 190 00:11:38,820 --> 00:11:42,120 And you can write to memory locations 191 00:11:42,120 --> 00:11:44,020 that are marked as read only. 192 00:11:44,940 --> 00:11:46,940 It allows you to get and set registers. 193 00:11:47,360 --> 00:11:50,140 So basically, when you use Ptrace, you are God. 194 00:11:50,420 --> 00:11:52,560 You own the process. 195 00:11:52,620 --> 00:11:54,040 You own the debuggee. 196 00:11:54,800 --> 00:11:57,900 So when we talk about a debugger and a debuggee, 197 00:11:58,080 --> 00:12:00,800 the debuggee becomes a child of the debugger. 198 00:12:00,860 --> 00:12:02,260 Let's say we're a Firefox developer, 199 00:12:02,640 --> 00:12:06,280 and we're finally going around fixing the memory leaks 200 00:12:06,280 --> 00:12:08,520 that every Firefox user complains about. 201 00:12:08,520 --> 00:12:11,100 And we're going to use GDB to do it. 202 00:12:11,140 --> 00:12:12,960 So we have Firefox and GDB. 203 00:12:13,200 --> 00:12:15,100 They're two unrelated programs, 204 00:12:15,360 --> 00:12:16,600 started at different times. 205 00:12:17,300 --> 00:12:20,520 When you attach GDB to Firefox, 206 00:12:21,280 --> 00:12:23,140 conceptually speaking, in the kernel, 207 00:12:23,380 --> 00:12:26,800 Firefox becomes a child of the debugger, 208 00:12:26,840 --> 00:12:29,900 as if GDB spawned Firefox, 209 00:12:30,680 --> 00:12:33,500 even though they were started at different times 210 00:12:33,500 --> 00:12:34,840 from different places. 211 00:12:35,220 --> 00:12:38,040 So when you attach a debugger to a debuggee, 212 00:12:39,320 --> 00:12:42,100 the debuggee becomes a child of the debugger. 213 00:12:42,220 --> 00:12:44,720 And I'm going to talk a bit about child processes 214 00:12:44,720 --> 00:12:45,840 and parent processes, 215 00:12:46,020 --> 00:12:48,120 so it's a little bit easier to speak. 216 00:12:48,280 --> 00:12:49,240 And that's what I'm talking about. 217 00:12:49,480 --> 00:12:52,040 The debugger is lib hijack. 218 00:12:52,100 --> 00:12:53,140 It's GDB. 219 00:12:53,360 --> 00:12:55,580 The debuggee is the victim process. 220 00:12:58,260 --> 00:12:59,380 Ptrace is destructive. 221 00:12:59,880 --> 00:13:02,740 You can do a lot of things with Ptrace 222 00:13:02,740 --> 00:13:04,540 that it really wasn't meant to do. 223 00:13:04,800 --> 00:13:07,840 In fact, I think the original Ptrace engineer 224 00:13:07,840 --> 00:13:09,120 was likely evil. 225 00:13:09,360 --> 00:13:10,640 He knew it could be abused. 226 00:13:10,760 --> 00:13:12,520 He probably even created some back doors 227 00:13:12,520 --> 00:13:13,280 on the systems. 228 00:13:14,940 --> 00:13:17,120 So we know we have some arbitrary code 229 00:13:17,120 --> 00:13:18,480 that we need to store. 230 00:13:18,600 --> 00:13:19,740 We can't store it in the stack. 231 00:13:19,900 --> 00:13:20,920 We can't store it in the heap. 232 00:13:21,040 --> 00:13:24,160 We can't store it in where EIP points to. 233 00:13:24,480 --> 00:13:25,900 Where can we store it? 234 00:13:27,940 --> 00:13:31,200 So what we need to do is allocate memory in the child. 235 00:13:31,660 --> 00:13:33,940 But unlike Windows and OS X, 236 00:13:34,080 --> 00:13:37,380 we cannot allocate from the parent process. 237 00:13:37,840 --> 00:13:41,020 The child process must be the one to allocate. 238 00:13:41,260 --> 00:13:44,660 In Linux, in FreeBSD, in Solaris, 239 00:13:44,800 --> 00:13:50,260 there is no API to allocate memory 240 00:13:50,260 --> 00:13:51,780 in a different process. 241 00:13:52,580 --> 00:13:56,220 So the child process must be the one to allocate memory. 242 00:13:56,620 --> 00:13:59,080 So what we need to do is we need to find 243 00:13:59,080 --> 00:14:00,240 where the kernel is called. 244 00:14:00,380 --> 00:14:04,380 And the kernel is called by a software interrupt 245 00:14:04,380 --> 00:14:05,560 called int80. 246 00:14:06,220 --> 00:14:09,420 We need to find out where int80 is in the program. 247 00:14:09,780 --> 00:14:13,060 But the problem is the program will never call the kernel. 248 00:14:13,440 --> 00:14:14,800 Likely will never call the kernel. 249 00:14:15,160 --> 00:14:16,840 Instead it will call library functions 250 00:14:16,840 --> 00:14:17,840 which call the kernel. 251 00:14:18,260 --> 00:14:20,120 libc calls the kernel everywhere. 252 00:14:20,580 --> 00:14:22,720 So libc is a great candidate to find 253 00:14:22,720 --> 00:14:23,800 where the kernel is called. 254 00:14:24,560 --> 00:14:27,140 We can find a library function that calls the kernel 255 00:14:27,140 --> 00:14:28,880 by crawling through the ELF metadata, 256 00:14:29,080 --> 00:14:30,740 by crawling through all these different headers. 257 00:14:32,080 --> 00:14:34,840 So the main ELF header contains a pointer 258 00:14:34,840 --> 00:14:36,060 to the process header. 259 00:14:36,200 --> 00:14:37,980 The process header contains a pointer 260 00:14:37,980 --> 00:14:39,300 to the dynamic headers. 261 00:14:42,560 --> 00:14:45,180 And the dynamic headers have a pointer 262 00:14:45,180 --> 00:14:47,160 to the global offset table. 263 00:14:48,380 --> 00:14:52,100 And the second entry of the global offset table 264 00:14:52,100 --> 00:14:54,300 contains a pointer to the link map. 265 00:14:54,560 --> 00:14:57,460 I said second and the slide says one 266 00:14:57,460 --> 00:14:59,560 because we're starting at a zero index. 267 00:15:00,780 --> 00:15:03,960 The link map is a structure created and maintained 268 00:15:03,960 --> 00:15:06,100 by the runtime linker and dlopen. 269 00:15:08,000 --> 00:15:12,260 The link map points to each shared object's ELF headers. 270 00:15:12,440 --> 00:15:15,040 So every single shared object, 271 00:15:15,080 --> 00:15:16,520 every single object file, 272 00:15:16,820 --> 00:15:18,980 your program, its dependencies, 273 00:15:19,180 --> 00:15:20,360 have ELF headers. 274 00:15:20,540 --> 00:15:24,960 And the ELF headers contain a symbol table 275 00:15:24,960 --> 00:15:28,920 and a symbol table is basically all the functions, 276 00:15:29,420 --> 00:15:30,880 all the global variables, 277 00:15:31,100 --> 00:15:33,300 all that kind of stuff that gets loaded in memory. 278 00:15:34,800 --> 00:15:37,120 So we'll parse through the ELF headers 279 00:15:37,120 --> 00:15:38,520 starting at that hex address. 280 00:15:42,560 --> 00:15:46,680 The ELF headers include lists of loaded functions. 281 00:15:48,300 --> 00:15:52,380 So we found int80 by crawling through all the ELF headers. 282 00:15:52,460 --> 00:15:54,580 I saved you all the nitty-gritty details 283 00:15:54,580 --> 00:15:55,520 because it's boring. 284 00:15:56,320 --> 00:16:00,640 And so we found int80 in a shared object 285 00:16:00,640 --> 00:16:02,420 and we backed up the registers. 286 00:16:02,420 --> 00:16:05,140 We're going to mess with some of the registers here 287 00:16:05,140 --> 00:16:05,980 and we backed them up. 288 00:16:07,100 --> 00:16:11,380 We're going to set EIP to the address of where we found int80 289 00:16:12,420 --> 00:16:14,720 and we're going to set up the stack 290 00:16:14,720 --> 00:16:17,320 to call the MMAP system call. 291 00:16:19,260 --> 00:16:22,660 We're going to continue execution until MMAP finishes. 292 00:16:23,360 --> 00:16:25,300 So what we did is we stored... 293 00:16:26,720 --> 00:16:29,820 MMAP requires you to pass in some certain data. 294 00:16:29,920 --> 00:16:31,840 It requires you to pass in a structure. 295 00:16:32,420 --> 00:16:34,820 And in that structure it says 296 00:16:35,420 --> 00:16:38,040 where in memory you want this new memory mapping, 297 00:16:38,200 --> 00:16:39,500 how long you want it to be, 298 00:16:39,740 --> 00:16:42,300 the access rights, that kind of thing. 299 00:16:42,620 --> 00:16:44,640 And we stored that structure in the stack. 300 00:16:44,860 --> 00:16:46,900 Now that part isn't getting executed. 301 00:16:47,220 --> 00:16:50,180 It's just data getting passed to MMAP. 302 00:16:51,180 --> 00:16:52,740 So that's completely okay to do. 303 00:16:54,800 --> 00:16:56,840 So after calling MMAP, 304 00:16:56,860 --> 00:16:58,740 we now have a newly allocated mapping. 305 00:16:59,020 --> 00:17:02,080 And the EAX register contains the address 306 00:17:02,420 --> 00:17:04,020 of that newly allocated mapping. 307 00:17:04,240 --> 00:17:06,500 And this right here is the new technique, by the way. 308 00:17:07,240 --> 00:17:08,620 I forgot to mention that. 309 00:17:08,840 --> 00:17:10,240 You can write to it. 310 00:17:10,320 --> 00:17:12,300 You can write to that newly allocated mapping. 311 00:17:13,120 --> 00:17:15,720 Even if the mapping is marked non-writable. 312 00:17:15,840 --> 00:17:17,780 Even if it's marked read and execute 313 00:17:17,780 --> 00:17:19,860 or just read only or execute only, 314 00:17:20,020 --> 00:17:21,220 you can write to it. 315 00:17:21,300 --> 00:17:23,040 Because with ptrace, you're God. 316 00:17:24,060 --> 00:17:25,260 You can restore the... 317 00:17:25,260 --> 00:17:27,920 You'll then restore the backed up registers 318 00:17:27,920 --> 00:17:29,160 as if nothing happened. 319 00:17:30,260 --> 00:17:32,080 And we're going to push a return address 320 00:17:32,420 --> 00:17:33,880 back into the... 321 00:17:34,420 --> 00:17:35,500 onto the stack. 322 00:17:35,940 --> 00:17:37,840 Because the shellcode needs to know 323 00:17:37,840 --> 00:17:39,100 where to return control to. 324 00:17:40,360 --> 00:17:41,920 Your program, Apache, 325 00:17:42,120 --> 00:17:43,960 was probably sitting there listening for connections. 326 00:17:44,080 --> 00:17:45,440 And we want our shellcode to run 327 00:17:45,440 --> 00:17:47,820 and then have Apache 328 00:17:48,560 --> 00:17:50,700 continue on doing its normal thing. 329 00:17:50,820 --> 00:17:52,060 So we need a push return address. 330 00:17:52,400 --> 00:17:54,100 So we'll decrement an ESP 331 00:17:54,100 --> 00:17:55,680 by the size of unsigned long, 332 00:17:55,840 --> 00:17:58,060 which is 4 bytes on 32-bit 333 00:17:58,060 --> 00:17:59,160 and 8 bytes on 64. 334 00:17:59,520 --> 00:18:01,660 And then we'll copy EIP 335 00:18:03,720 --> 00:18:05,260 We'll then write the shellcode 336 00:18:05,260 --> 00:18:06,720 to the newly allocated mapping 337 00:18:06,720 --> 00:18:08,860 and set the EIP address 338 00:18:08,860 --> 00:18:11,860 to the location of the shellcode. 339 00:18:12,560 --> 00:18:14,380 And we'll detach from the process, 340 00:18:14,500 --> 00:18:15,580 sit back, relax, 341 00:18:15,940 --> 00:18:17,100 and enjoy life. 342 00:18:17,200 --> 00:18:18,660 We've just injected our shellcode 343 00:18:18,660 --> 00:18:19,960 and got it to run. 344 00:18:20,880 --> 00:18:21,720 But wait. 345 00:18:22,640 --> 00:18:23,440 There's more. 346 00:18:26,720 --> 00:18:27,540 Hijacking functions. 347 00:18:27,800 --> 00:18:29,600 So we injected our shellcode 348 00:18:29,600 --> 00:18:30,140 and that's great. 349 00:18:30,140 --> 00:18:31,920 We got our shellcode to run once. 350 00:18:32,020 --> 00:18:33,700 But remember, we wanted our shellcode 351 00:18:33,700 --> 00:18:35,380 to run more than one time. 352 00:18:35,520 --> 00:18:37,360 Anytime that receive is called, 353 00:18:37,480 --> 00:18:38,940 our code needs to run. 354 00:18:39,220 --> 00:18:41,860 So we're going to take a look at the global offset table 355 00:18:41,860 --> 00:18:43,900 and the procedure linkage table. 356 00:18:44,140 --> 00:18:45,480 What that is, 357 00:18:45,480 --> 00:18:47,160 is it's an array of function addresses. 358 00:18:48,060 --> 00:18:49,720 Anytime your program calls 359 00:18:49,720 --> 00:18:52,400 printf or receive, memset, 360 00:18:52,600 --> 00:18:54,200 any function 361 00:18:54,200 --> 00:18:55,800 that comes from a shared object, 362 00:18:57,540 --> 00:18:58,900 it's actually going to, 363 00:18:58,920 --> 00:19:00,120 it's not actually going to call 364 00:19:00,140 --> 00:19:01,920 that function directly. 365 00:19:02,300 --> 00:19:04,320 It's actually going to call a stub entry 366 00:19:04,320 --> 00:19:05,080 in the GOT, 367 00:19:05,300 --> 00:19:08,120 which then calls printf. 368 00:19:08,320 --> 00:19:10,000 And that allows your program 369 00:19:10,000 --> 00:19:12,140 to be relocated 370 00:19:12,140 --> 00:19:13,920 in all the shared objects, all the dependencies, 371 00:19:14,140 --> 00:19:15,320 to be located 372 00:19:15,320 --> 00:19:18,000 in completely random places in memory. 373 00:19:18,660 --> 00:19:20,360 And allows your programs 374 00:19:20,360 --> 00:19:21,600 to still be able to call 375 00:19:22,040 --> 00:19:23,480 printf, receive, memset, 376 00:19:24,340 --> 00:19:25,600 no matter what. 377 00:19:25,940 --> 00:19:28,000 No matter where it's loaded in memory. 378 00:19:28,660 --> 00:19:29,940 And all your 379 00:19:30,140 --> 00:19:32,260 reference functions are in the GOT PLT. 380 00:19:32,340 --> 00:19:33,620 In the global offset table, 381 00:19:33,720 --> 00:19:34,900 procedure linkage table. 382 00:19:35,200 --> 00:19:36,340 So we're going to use a technique 383 00:19:36,340 --> 00:19:39,160 that Silvio Caesar architected 384 00:19:39,160 --> 00:19:41,220 called PLT GOT Redirection. 385 00:19:41,500 --> 00:19:43,400 And we're going to create 386 00:19:43,400 --> 00:19:45,300 a stub entry in our shellcode 387 00:19:45,300 --> 00:19:47,260 and we're going to overwrite 388 00:19:47,260 --> 00:19:48,420 that stub address 389 00:19:48,420 --> 00:19:51,500 with the address of where the function 390 00:19:51,500 --> 00:19:53,780 is really loaded in memory. 391 00:19:54,940 --> 00:19:56,040 And then we'll 392 00:19:56,040 --> 00:19:57,780 replace its corresponding 393 00:19:57,780 --> 00:19:59,560 entry in the GOT 394 00:20:00,580 --> 00:20:02,240 with the address 395 00:20:02,240 --> 00:20:03,040 of our shellcode. 396 00:20:03,180 --> 00:20:04,500 And that's the redirection. 397 00:20:04,600 --> 00:20:05,380 That's it. 398 00:20:05,680 --> 00:20:08,180 You've now hijacked a function. 399 00:20:08,840 --> 00:20:11,520 But when you're hijacking functions, 400 00:20:11,780 --> 00:20:12,760 be careful. 401 00:20:13,260 --> 00:20:15,240 Because multiple shared objects 402 00:20:15,240 --> 00:20:17,760 can implement functions of the same name. 403 00:20:18,060 --> 00:20:19,180 Even though 404 00:20:19,180 --> 00:20:21,040 they may have different signatures. 405 00:20:21,480 --> 00:20:22,560 So let's say Apache 406 00:20:22,560 --> 00:20:24,780 was hysterically written 407 00:20:24,780 --> 00:20:26,260 in ugly C++ 408 00:20:26,260 --> 00:20:29,740 and it used 409 00:20:30,140 --> 00:20:31,460 libc as well. 410 00:20:31,640 --> 00:20:33,460 So it uses libc++ 411 00:20:33,460 --> 00:20:34,660 and libc. 412 00:20:34,940 --> 00:20:38,100 Let's say libc++ implemented printf. 413 00:20:38,560 --> 00:20:39,960 And libc 414 00:20:39,960 --> 00:20:41,660 implemented printf as well. 415 00:20:41,900 --> 00:20:44,180 So we have two library functions 416 00:20:44,180 --> 00:20:45,720 that implemented printf. 417 00:20:45,860 --> 00:20:47,460 That is completely valid. 418 00:20:48,920 --> 00:20:50,180 There's no problem 419 00:20:50,180 --> 00:20:50,660 with that. 420 00:20:51,920 --> 00:20:53,700 And they can even have a different signature, 421 00:20:53,880 --> 00:20:54,900 a different function prototype 422 00:20:54,900 --> 00:20:56,700 accepting different 423 00:20:58,200 --> 00:20:58,880 arguments. 424 00:21:00,140 --> 00:21:01,780 Returning a different return value. 425 00:21:03,180 --> 00:21:03,980 So you 426 00:21:03,980 --> 00:21:05,740 want to make sure that you target 427 00:21:05,740 --> 00:21:06,540 the correct function. 428 00:21:06,760 --> 00:21:08,340 You need to know your target well 429 00:21:08,340 --> 00:21:09,480 when you're hijacking functions. 430 00:21:11,600 --> 00:21:13,920 What I do is I set up 431 00:21:13,920 --> 00:21:15,540 a VM and I mimic the victim. 432 00:21:15,720 --> 00:21:17,700 I set up even the same OS, 433 00:21:18,060 --> 00:21:19,320 the same patch levels, 434 00:21:19,600 --> 00:21:21,880 the same type of proprietary software. 435 00:21:22,180 --> 00:21:23,680 I mimic my victim 436 00:21:23,680 --> 00:21:25,780 100% so that I know 437 00:21:25,780 --> 00:21:27,000 what I'm getting myself into. 438 00:21:27,100 --> 00:21:28,480 So I can dive into it deeply 439 00:21:30,240 --> 00:21:30,760 without any surprises. 440 00:21:32,720 --> 00:21:34,020 Once you hijack, 441 00:21:34,080 --> 00:21:35,800 you cannot reliably remove 442 00:21:35,800 --> 00:21:38,000 a hijack because of the 443 00:21:38,540 --> 00:21:39,900 multiple shared object 444 00:21:39,900 --> 00:21:40,780 issue. 445 00:21:41,120 --> 00:21:43,840 Because libc and libc++ can implement 446 00:21:44,440 --> 00:21:45,820 functions of the same name 447 00:21:45,820 --> 00:21:47,340 or name your library there. 448 00:21:48,920 --> 00:21:50,020 So we want to be able 449 00:21:50,020 --> 00:21:51,160 to inject shared objects. 450 00:21:51,320 --> 00:21:52,420 And why do we want to do that? 451 00:21:52,540 --> 00:21:54,560 Because we don't want to have to write a ton of shellcode. 452 00:21:55,020 --> 00:21:56,120 Assembly is tedious. 453 00:21:56,460 --> 00:21:58,060 It is architecture dependent. 454 00:22:00,140 --> 00:22:01,680 It will allow you to move freely. 455 00:22:02,960 --> 00:22:04,420 As freely as you would 456 00:22:04,420 --> 00:22:06,100 if you needed to 457 00:22:06,100 --> 00:22:08,320 pwn a lot of systems in a short amount of time. 458 00:22:09,400 --> 00:22:10,700 So what we want to do 459 00:22:10,700 --> 00:22:11,500 is write in C 460 00:22:11,500 --> 00:22:12,860 or use other libraries. 461 00:22:13,000 --> 00:22:14,220 The possibilities are endless. 462 00:22:14,340 --> 00:22:17,160 You could write your malicious code in Haskell 463 00:22:17,160 --> 00:22:19,060 or Erlang if you wanted. 464 00:22:19,300 --> 00:22:20,580 I don't know why you would, 465 00:22:21,480 --> 00:22:22,460 but you could. 466 00:22:23,420 --> 00:22:25,320 So there are two ways of doing it. 467 00:22:25,600 --> 00:22:26,800 And there's the cheating way. 468 00:22:26,880 --> 00:22:28,980 Use a stub shellcode that calls dlopen. 469 00:22:30,140 --> 00:22:31,020 That's what every single 470 00:22:31,800 --> 00:22:32,880 project does out there. 471 00:22:33,020 --> 00:22:35,100 Inject.so does that. 472 00:22:35,340 --> 00:22:36,700 Jewgod does that. 473 00:22:37,120 --> 00:22:39,620 And right now lib hijack 474 00:22:39,620 --> 00:22:40,380 does that as well. 475 00:22:40,860 --> 00:22:42,460 There's also the real way 476 00:22:43,240 --> 00:22:45,320 which is rewrite dlopen 477 00:22:45,320 --> 00:22:46,920 in the parent process. 478 00:22:47,260 --> 00:22:49,520 In lib hijack or your debugger. 479 00:22:50,760 --> 00:22:52,220 So the cheating way 480 00:22:52,220 --> 00:22:54,360 what you'll do 481 00:22:54,360 --> 00:22:55,900 is you'll allocate 482 00:22:55,900 --> 00:22:57,380 a new memory mapping 483 00:22:58,080 --> 00:22:59,360 using the new technique 484 00:23:00,140 --> 00:23:01,460 on how to force the child 485 00:23:01,460 --> 00:23:03,440 to allocate a new memory mapping. 486 00:23:03,940 --> 00:23:06,120 And you'll store some auxiliary data 487 00:23:06,120 --> 00:23:07,180 in the mapping. 488 00:23:07,700 --> 00:23:09,480 You'll store the path, 489 00:23:09,840 --> 00:23:11,100 the file system path 490 00:23:11,100 --> 00:23:13,220 of where the shared object resides. 491 00:23:14,240 --> 00:23:16,260 You'll store the name 492 00:23:16,260 --> 00:23:17,700 of the function to hijack 493 00:23:17,700 --> 00:23:19,480 and the stub shellcode. 494 00:23:19,680 --> 00:23:21,000 And the stub shellcode 495 00:23:21,000 --> 00:23:23,120 will call dlopen and dlsim. 496 00:23:23,320 --> 00:23:25,080 So it'll call dlopen 497 00:23:25,080 --> 00:23:26,880 to open your shared object 498 00:23:26,880 --> 00:23:29,080 and dlsim to find 499 00:23:29,080 --> 00:23:29,900 the address 500 00:23:29,900 --> 00:23:32,020 of your malicious 501 00:23:32,840 --> 00:23:33,240 function. 502 00:23:35,340 --> 00:23:36,780 It'll then replace 503 00:23:36,780 --> 00:23:38,400 the global offset table entry 504 00:23:38,960 --> 00:23:41,160 with the entry found via dlsim. 505 00:23:41,380 --> 00:23:43,080 So it'll find out dynamically 506 00:23:43,080 --> 00:23:45,020 where your function's located 507 00:23:45,020 --> 00:23:46,800 in memory and then 508 00:23:46,800 --> 00:23:48,520 replace in the GOT 509 00:23:49,080 --> 00:23:50,520 where your malicious 510 00:23:51,080 --> 00:23:52,740 function is located. 511 00:23:54,120 --> 00:23:55,060 The GOT 512 00:23:55,080 --> 00:23:56,220 cheating way has some advantages. 513 00:23:56,400 --> 00:23:57,220 It's easy. 514 00:23:57,880 --> 00:23:59,820 I've written the assembly code 515 00:23:59,820 --> 00:24:01,460 and I'm going to release it today for you. 516 00:24:01,920 --> 00:24:03,120 It is extendable. 517 00:24:03,400 --> 00:24:06,440 All you have to do to hijack more functions, 518 00:24:06,580 --> 00:24:07,460 multiple functions, 519 00:24:07,680 --> 00:24:09,820 is literally just copy and paste code. 520 00:24:10,740 --> 00:24:11,840 It is fast. 521 00:24:12,560 --> 00:24:13,700 Assembly is fast. 522 00:24:14,100 --> 00:24:16,300 Handwritten assembly is usually fast 523 00:24:16,300 --> 00:24:17,260 if you write it right. 524 00:24:18,360 --> 00:24:20,160 There are some disadvantages though. 525 00:24:20,620 --> 00:24:22,120 When you do this technique 526 00:24:22,120 --> 00:24:23,180 you're going to have an entry 527 00:24:25,080 --> 00:24:26,280 file in proc pin maps. 528 00:24:26,740 --> 00:24:28,760 So when a system administrator 529 00:24:30,340 --> 00:24:31,840 thinks that Apache 530 00:24:31,840 --> 00:24:33,000 is acting weird 531 00:24:33,000 --> 00:24:34,140 and he's like, hmm, 532 00:24:34,400 --> 00:24:35,660 it's been pwned. 533 00:24:36,400 --> 00:24:38,260 One of the things that it'll do 534 00:24:38,260 --> 00:24:39,820 for forensic purposes is 535 00:24:39,820 --> 00:24:41,300 he'll look in proc pin maps 536 00:24:41,300 --> 00:24:43,700 for anything that might be evil 537 00:24:43,700 --> 00:24:45,780 or malicious and he'll see 538 00:24:45,780 --> 00:24:47,600 because you used dlopen 539 00:24:47,600 --> 00:24:48,780 that your 540 00:24:51,540 --> 00:24:52,740 shared object 541 00:24:52,740 --> 00:24:53,340 was loaded. 542 00:24:55,440 --> 00:24:57,140 And you rely on 543 00:24:57,140 --> 00:24:58,300 architecture dependent 544 00:24:59,200 --> 00:25:00,420 stub shellcode. 545 00:25:00,640 --> 00:25:02,360 I don't know, maybe you're pwning a 546 00:25:02,360 --> 00:25:04,680 web farm that has a mix of 32-bit 547 00:25:04,680 --> 00:25:06,200 and 64-bit systems. 548 00:25:06,790 --> 00:25:08,480 So now instead of having 549 00:25:09,080 --> 00:25:10,260 one set of 550 00:25:11,070 --> 00:25:12,880 code, now you have to duplicate 551 00:25:12,880 --> 00:25:14,840 your code and do the exact same 552 00:25:14,840 --> 00:25:17,080 thing for 64-bit 553 00:25:17,080 --> 00:25:18,900 which on Linux has 554 00:25:18,900 --> 00:25:20,780 a slightly different calling convention 555 00:25:20,780 --> 00:25:21,820 than 32-bit. 556 00:25:23,840 --> 00:25:25,060 So the real world 557 00:25:25,080 --> 00:25:25,700 is that you want to 558 00:25:25,700 --> 00:25:26,840 re-implement dlopen 559 00:25:26,840 --> 00:25:28,660 by loading the dependencies 560 00:25:28,660 --> 00:25:31,020 of your malicious shared object 561 00:25:31,020 --> 00:25:31,360 yourself. 562 00:25:31,900 --> 00:25:33,180 Some of the dependencies 563 00:25:33,180 --> 00:25:34,780 can be loaded via dlopen 564 00:25:34,780 --> 00:25:35,980 and some of the dependencies 565 00:25:35,980 --> 00:25:38,040 might not even be needed 566 00:25:38,040 --> 00:25:40,020 to be loaded at all. 567 00:25:40,420 --> 00:25:42,560 Like libc is in just about 568 00:25:42,560 --> 00:25:44,860 every single program out there. 569 00:25:45,000 --> 00:25:46,480 I don't think I've seen a program 570 00:25:46,480 --> 00:25:48,560 that didn't depend on libc. 571 00:25:49,940 --> 00:25:51,660 So you'll load your dependencies 572 00:25:55,080 --> 00:25:56,180 and you'll find a sniffer 573 00:25:56,180 --> 00:25:57,980 using libpcap 574 00:25:57,980 --> 00:25:59,580 inside of Apache 575 00:26:00,660 --> 00:26:02,940 which you can do with libhijack. 576 00:26:05,880 --> 00:26:07,080 You wouldn't 577 00:26:07,080 --> 00:26:09,060 want to have a system administrator 578 00:26:09,060 --> 00:26:10,740 look at the procpidmaps 579 00:26:10,740 --> 00:26:12,860 file and see 580 00:26:12,860 --> 00:26:14,900 that libpcap 581 00:26:14,900 --> 00:26:17,300 is loaded. 582 00:26:17,560 --> 00:26:19,020 If I were a system administrator 583 00:26:19,020 --> 00:26:20,320 and I saw that, I'd be like 584 00:26:20,320 --> 00:26:21,420 what the freak. 585 00:26:22,860 --> 00:26:24,800 I'd be like, we've been 586 00:26:25,080 --> 00:26:25,180 stoned. 587 00:26:25,600 --> 00:26:27,560 So what you'll do is 588 00:26:27,560 --> 00:26:30,520 you'll create memory maps 589 00:26:31,360 --> 00:26:32,940 and you'll write 590 00:26:32,940 --> 00:26:34,120 shared object data 591 00:26:34,120 --> 00:26:35,960 to those new memory maps. 592 00:26:36,200 --> 00:26:37,720 You'll look at the process headers 593 00:26:37,720 --> 00:26:40,080 which tell you how to load 594 00:26:40,080 --> 00:26:42,700 that shared object 595 00:26:42,700 --> 00:26:44,420 and you'll load 596 00:26:44,860 --> 00:26:45,900 what it tells you to 597 00:26:45,900 --> 00:26:47,240 in these new memory mappings. 598 00:26:47,520 --> 00:26:50,100 And then you'll patch into the runtime linker 599 00:26:50,100 --> 00:26:51,580 and that's important 600 00:26:51,580 --> 00:26:52,980 because the runtime linker, 601 00:26:55,080 --> 00:26:56,160 it's responsible for resolving 602 00:26:56,160 --> 00:26:58,400 where in memory functions 603 00:26:58,400 --> 00:27:00,660 are located like printf 604 00:27:00,660 --> 00:27:02,460 and memset and mmap 605 00:27:02,460 --> 00:27:03,320 that kind of thing. 606 00:27:04,810 --> 00:27:06,740 You'll run the initialization routines 607 00:27:06,740 --> 00:27:08,040 of dependencies 608 00:27:08,040 --> 00:27:10,240 that you loaded by hand. 609 00:27:11,020 --> 00:27:12,460 You do not 610 00:27:12,460 --> 00:27:14,040 run the initialization routines 611 00:27:14,040 --> 00:27:16,440 for dependencies that have already 612 00:27:16,440 --> 00:27:18,300 been loaded or that were loaded 613 00:27:18,300 --> 00:27:19,260 by dlopen. 614 00:27:20,220 --> 00:27:21,680 And then you'll do the 615 00:27:21,680 --> 00:27:24,560 PLTGOT redirection technique. 616 00:27:25,080 --> 00:27:26,360 You'll hijack the global offset table. 617 00:27:27,000 --> 00:27:28,620 There's some advantages to that. 618 00:27:28,740 --> 00:27:29,800 It's completely anonymous. 619 00:27:30,200 --> 00:27:33,000 So now you've hijacked 620 00:27:33,000 --> 00:27:34,240 receive in Apache 621 00:27:35,040 --> 00:27:36,920 and the system administrator 622 00:27:36,920 --> 00:27:38,400 when he cats progpid maps 623 00:27:38,400 --> 00:27:40,520 is not going to see anything out of the ordinary. 624 00:27:40,920 --> 00:27:42,700 He's going to see, oh libc is loaded. 625 00:27:43,500 --> 00:27:44,320 That's cool. 626 00:27:44,600 --> 00:27:46,680 So he's going to just continue on his way. 627 00:27:48,080 --> 00:27:49,280 It's extensible. 628 00:27:50,360 --> 00:27:51,120 You can 629 00:27:51,120 --> 00:27:52,440 extend it really easy. 630 00:27:52,520 --> 00:27:53,500 It's really powerful. 631 00:27:55,080 --> 00:27:56,880 There are some disadvantages though. 632 00:27:57,020 --> 00:27:58,780 There's just one disadvantage. 633 00:27:59,240 --> 00:28:00,820 It takes a lot of 634 00:28:00,820 --> 00:28:02,960 freaking time to implement and research. 635 00:28:03,320 --> 00:28:04,900 I've been looking at this 636 00:28:04,900 --> 00:28:06,920 doing it the real way 637 00:28:06,920 --> 00:28:08,220 for about 638 00:28:08,220 --> 00:28:11,020 a year to two years off and on. 639 00:28:11,360 --> 00:28:13,100 And I know 640 00:28:13,100 --> 00:28:14,740 it from a higher level 641 00:28:14,740 --> 00:28:16,960 perspective and I'm just barely starting 642 00:28:16,960 --> 00:28:19,060 to understand it from a really 643 00:28:19,060 --> 00:28:20,040 low level perspective. 644 00:28:20,460 --> 00:28:22,280 But it's really hard to implement. 645 00:28:22,840 --> 00:28:23,180 Um 646 00:28:25,080 --> 00:28:26,840 there's probably around 647 00:28:26,840 --> 00:28:28,660 a million lines of code to read through. 648 00:28:28,860 --> 00:28:30,560 It's a major 649 00:28:31,080 --> 00:28:31,440 project. 650 00:28:32,340 --> 00:28:35,100 Shared objects can have dependencies. 651 00:28:35,940 --> 00:28:36,900 Shared objects 652 00:28:36,900 --> 00:28:38,780 have their own procedure linkage table 653 00:28:38,780 --> 00:28:39,960 and global offset table. 654 00:28:40,360 --> 00:28:42,520 Prior to this slide we were talking about 655 00:28:42,520 --> 00:28:43,780 the program's main 656 00:28:43,780 --> 00:28:46,960 GOT PLT. But each shared object 657 00:28:46,960 --> 00:28:48,320 because it can be loaded 658 00:28:48,880 --> 00:28:50,880 in random spots in memory 659 00:28:50,880 --> 00:28:52,220 and because it can 660 00:28:53,080 --> 00:28:54,880 depend on other shared objects 661 00:28:55,540 --> 00:28:57,220 it has its own 662 00:28:57,220 --> 00:28:58,060 PLT GOT. 663 00:28:59,020 --> 00:29:00,480 So what you'll do 664 00:29:00,480 --> 00:29:02,820 if you can hijack inside 665 00:29:02,820 --> 00:29:05,200 of shared objects. You can hijack 666 00:29:05,200 --> 00:29:06,780 inside the main program and you can 667 00:29:06,780 --> 00:29:08,540 hijack inside shared objects. 668 00:29:08,920 --> 00:29:10,960 So what you'll do is you'll loop through the 669 00:29:10,960 --> 00:29:12,880 dynamic structures found in the 670 00:29:12,880 --> 00:29:15,140 link map and you'll use the same 671 00:29:15,140 --> 00:29:17,000 PLT GOT redirection technique 672 00:29:17,000 --> 00:29:18,720 against the shared objects. 673 00:29:18,980 --> 00:29:20,300 It's the exact same principle. 674 00:29:22,060 --> 00:29:22,840 You can 675 00:29:22,840 --> 00:29:25,060 even hijack shared objects 676 00:29:25,080 --> 00:29:26,540 that have been loaded via DLOpen. 677 00:29:26,680 --> 00:29:28,660 So Pigeon is a great example 678 00:29:28,660 --> 00:29:31,000 of that and even Apache. It uses 679 00:29:31,000 --> 00:29:32,360 DLOpen heavily. 680 00:29:32,660 --> 00:29:35,000 It uses just about everything as a 681 00:29:35,000 --> 00:29:37,000 plugin. All it is is it's a 682 00:29:37,000 --> 00:29:38,900 frontend to all these 683 00:29:38,900 --> 00:29:40,420 plugins, to all these backends. 684 00:29:40,800 --> 00:29:43,080 And you can hijack 685 00:29:43,080 --> 00:29:45,060 shared objects that have been loaded 686 00:29:45,060 --> 00:29:46,880 via DLOpen because DLOpen 687 00:29:48,060 --> 00:29:48,640 injects 688 00:29:48,640 --> 00:29:50,540 a new link map entry. 689 00:29:52,700 --> 00:29:54,580 Lib hijack makes 690 00:29:55,080 --> 00:29:56,900 injection of arbitrary code and 691 00:29:56,900 --> 00:29:58,980 hijack of dynamically loaded functions 692 00:29:58,980 --> 00:30:01,000 extremely easy. 693 00:30:02,640 --> 00:30:04,540 Lib hijack right now is around 694 00:30:04,540 --> 00:30:06,600 1800-1900 lines of code 695 00:30:06,600 --> 00:30:08,980 and I've made it so easy for you 696 00:30:08,980 --> 00:30:10,760 that you can inject your shellcode 697 00:30:10,760 --> 00:30:12,880 in as little as 8 lines of C. 698 00:30:14,100 --> 00:30:15,140 It loads 699 00:30:15,140 --> 00:30:16,980 shared objects via the cheating method 700 00:30:16,980 --> 00:30:18,660 right now because the real method, 701 00:30:18,800 --> 00:30:20,840 the real way, is really freaking 702 00:30:20,840 --> 00:30:21,500 hard to do. 703 00:30:24,100 --> 00:30:25,060 Injecting shellcode 704 00:30:25,080 --> 00:30:26,940 you can inject shellcode in as little as 705 00:30:26,940 --> 00:30:28,860 8 lines of C. It has full 706 00:30:28,860 --> 00:30:31,580 32-bit and 64-bit 707 00:30:31,580 --> 00:30:32,820 Linux support 708 00:30:32,820 --> 00:30:34,820 on the Intel platform. 709 00:30:35,200 --> 00:30:37,080 So that includes AMD as well. 710 00:30:39,940 --> 00:30:43,040 I develop mainly on 711 00:30:43,040 --> 00:30:45,340 32-bit and I've abstracted 712 00:30:45,340 --> 00:30:46,500 most of the things away 713 00:30:47,080 --> 00:30:47,860 so that 714 00:30:49,600 --> 00:30:51,020 I don't really 715 00:30:51,020 --> 00:30:53,280 have to test heavily in 64-bit. 716 00:30:53,420 --> 00:30:54,940 I do most of my testing 717 00:30:55,080 --> 00:30:56,840 in 32-bit and I've 718 00:30:56,840 --> 00:30:58,400 abstracted all the architecture 719 00:30:59,080 --> 00:31:00,320 dependent code 720 00:31:00,970 --> 00:31:02,880 away so that all the new features 721 00:31:02,880 --> 00:31:05,040 I write, I don't have to 722 00:31:05,040 --> 00:31:06,040 worry about architecture. 723 00:31:07,360 --> 00:31:08,920 Other OS's are coming 724 00:31:08,920 --> 00:31:10,880 soon. So we're working 725 00:31:10,880 --> 00:31:12,060 right now on 726 00:31:12,760 --> 00:31:14,180 porting to FreeBSD 727 00:31:14,180 --> 00:31:16,600 and we have some interest in 728 00:31:16,600 --> 00:31:17,740 porting to OS X. 729 00:31:19,220 --> 00:31:21,080 I'm always looking for help. 730 00:31:21,160 --> 00:31:22,620 This is a fun project. 731 00:31:22,740 --> 00:31:24,640 It's challenging. There's a lot of 732 00:31:25,080 --> 00:31:26,980 problems to solve in unique ways. 733 00:31:27,360 --> 00:31:29,200 And I'm always looking for help 734 00:31:29,200 --> 00:31:30,840 from programmers 735 00:31:30,840 --> 00:31:32,480 that write good C89. 736 00:31:34,940 --> 00:31:36,300 That's where my project is 737 00:31:36,920 --> 00:31:38,760 located. You guys all have CDs 738 00:31:38,760 --> 00:31:40,800 and you can copy and paste that 739 00:31:40,800 --> 00:31:42,860 link. That's where 740 00:31:42,860 --> 00:31:44,480 you can find my project. Clone it. 741 00:31:45,100 --> 00:31:46,860 Fork it. Whatever you want to do. 742 00:31:48,220 --> 00:31:49,080 So at the end 743 00:31:49,080 --> 00:31:51,220 of the day I'm going to release version 0.5 744 00:31:51,220 --> 00:31:52,320 of lib hijack. 745 00:31:53,960 --> 00:31:54,780 In this 746 00:31:55,080 --> 00:31:57,160 release, it's a major milestone 747 00:31:57,160 --> 00:31:58,960 release. You can hijack within 748 00:31:58,960 --> 00:31:59,980 shared objects. 749 00:32:01,660 --> 00:32:02,980 It does break 750 00:32:02,980 --> 00:32:05,220 existing 0.3 and 0.4 751 00:32:05,220 --> 00:32:07,140 API. Most of you, if not all of you, 752 00:32:07,180 --> 00:32:08,980 really don't have to worry about that 753 00:32:08,980 --> 00:32:10,820 because those releases 754 00:32:10,820 --> 00:32:12,120 were private. 755 00:32:13,060 --> 00:32:14,600 Or semi-private. 756 00:32:16,820 --> 00:32:19,100 I fixed some massive memory 757 00:32:19,100 --> 00:32:20,780 leaks. I had an issue. Before 758 00:32:20,780 --> 00:32:23,300 this release, lib hijacks 759 00:32:23,300 --> 00:32:23,840 used to... 760 00:32:39,940 --> 00:32:40,760 I had this 761 00:32:41,060 --> 00:32:41,340 little 762 00:32:42,340 --> 00:32:42,640 in the 763 00:32:42,640 --> 00:32:42,660 computer. 764 00:32:42,660 --> 00:32:42,720 It had this 765 00:32:42,720 --> 00:32:42,740 little 766 00:32:42,740 --> 00:32:42,760 little 767 00:32:42,760 --> 00:32:42,780 little 768 00:32:43,260 --> 00:32:43,540 little 769 00:32:43,540 --> 00:32:43,840 little 770 00:32:44,820 --> 00:32:45,100 little 771 00:32:55,080 --> 00:33:05,240 Should I give it a go? No. Okay. Anyways. So I fixed some massive memory 772 00:33:05,240 --> 00:33:12,120 leaks. Before this release, the usage of lib hijack was pretty limited. Its scope was pretty 773 00:33:12,120 --> 00:33:19,120 limited. It was mainly just hijacking major functions that your program calls. But now 774 00:33:19,120 --> 00:33:24,620 that you can hijack within shared objects, you can do whatever you want to do with lib 775 00:33:24,620 --> 00:33:30,720 hijack. You can create a fuzzer. You can create some sort of hyper visor. You can do all sorts 776 00:33:30,720 --> 00:33:38,500 of things with lib hijack. Its possibilities are limited only to your imagination. So before 777 00:33:38,500 --> 00:33:44,480 this release, I really didn't care about memory leaks. I was like, ah, it's just going to do its 778 00:33:44,480 --> 00:33:50,360 thing and quit. And it's going to do it well, of course. But it had some major memory leaks. It 779 00:33:50,360 --> 00:33:54,280 would fill up to 27 megs when I injected code into 780 00:33:54,280 --> 00:33:54,520 . 781 00:33:54,520 --> 00:33:54,600 . . 782 00:33:54,600 --> 00:34:04,580 Open VPN. And now I've trimmed that down so it only takes up 256K when injecting into 783 00:34:04,580 --> 00:34:10,080 Open VPN. So I've fixed some massive memory leaks. Probably better than Firefox. 784 00:34:13,280 --> 00:34:18,360 So there's been some various bug fixes. I had a little bit, a few bugs with mmap. Calling 785 00:34:18,360 --> 00:34:19,540 mmap, I fixed that. 786 00:34:27,400 --> 00:34:28,020 . 787 00:34:34,140 --> 00:34:42,300 Now, that one scared me. Was not expecting that. Okay. So we still have some things to 788 00:34:42,300 --> 00:34:48,820 do with lib hijack. There's still some things left to do. I need to figure out why certain 789 00:34:48,820 --> 00:34:54,580 functions don't show up in GOT resolution. There's, there's some, there's some problems. 790 00:34:54,580 --> 00:35:01,600 really weird issue going on in the GOT resolution that just some functions just don't show up I 791 00:35:01,600 --> 00:35:08,620 think I might be quitting a little bit early and so or stopping the resolution not quitting but 792 00:35:08,620 --> 00:35:17,120 stopping the resolution a little bit prematurely and in 0.6 we're also going to hopefully support 793 00:35:17,120 --> 00:35:22,740 injecting shared objects the real way this release is about six months out and I'm hoping that I have 794 00:35:22,740 --> 00:35:31,640 about four months of research left and then I'll do two months of implementation so we're working 795 00:35:31,640 --> 00:35:40,120 on porting to FreeBSD the real way you injecting shared objects the real way is more important than 796 00:35:40,120 --> 00:35:45,880 the FreeBSD port but if someone wants to go ahead and do that for the 0.6 release I'm accepting 797 00:35:45,880 --> 00:35:52,080 patches whoa if you're looking for an adventure 798 00:35:52,740 --> 00:36:00,560 port it to Android I'd love to see the security implement in implications of it running on Android 799 00:36:00,560 --> 00:36:09,000 Android uses elf it's Linux and and everything it runs itself and so you could you could use you 800 00:36:09,000 --> 00:36:17,260 could hijack some sort of Java program and do some sort of cool things with that on Android I'd 801 00:36:17,260 --> 00:36:22,020 really love to see that I'm always looking for help so if you have some interesting ideas implement 802 00:36:22,740 --> 00:36:30,340 send me a patch file so we know that that we we can hijack functions and we now have a really a tool 803 00:36:30,340 --> 00:36:36,900 that makes that really easy easy so what we can do to what can we do to prevent it we're security 804 00:36:36,900 --> 00:36:42,300 analysts and security engineers some of us and so what we can do is we can make sure the PL2 GOT 805 00:36:42,300 --> 00:36:48,600 entries point to the correct library but like I said multiple libraries can implement functions of 806 00:36:48,600 --> 00:36:49,200 the same name 807 00:36:53,060 --> 00:36:53,940 yes God 808 00:37:06,020 --> 00:37:17,000 so what you can do how do you do that there's no way to do that oh yeah okay 809 00:37:26,260 --> 00:37:35,340 they hate me don't they okay so uh so we can't really do that that's not really a viable option 810 00:37:35,340 --> 00:37:41,480 because live see like I said earlier in the talk live see and live C++ can implement functions of 811 00:37:41,480 --> 00:37:49,180 the same name in runtime there's no way to be able to figure out which printf is the right printf 812 00:37:49,920 --> 00:37:52,720 you can use dtrace I am in love with people 813 00:37:52,740 --> 00:38:01,040 dtrace or dtrace dtrace was created by Sun for Solaris and it is non-destructive debugging it 814 00:38:01,040 --> 00:38:10,480 allows you to debug the diet dive down into your programs oh yeah there we go okay it allows you 815 00:38:10,480 --> 00:38:18,180 to dive down into your programs and and do it non-destructively there are some destructive things 816 00:38:18,180 --> 00:38:20,460 that you can do with with with each 817 00:38:22,740 --> 00:38:27,360 with dtrace but you're very limited on what you can do 818 00:38:34,620 --> 00:38:44,880 okay so you could limit ptrace usage like the Apache user in Linux by default is able to use 819 00:38:44,880 --> 00:38:52,240 ptrace and there really should be no reason for that but Linux being the great operating 820 00:38:52,740 --> 00:38:55,700 system it is is never going to get true security 821 00:39:03,080 --> 00:39:11,340 um it's never going to get true security it's going to uh it's never going to get our back 822 00:39:16,100 --> 00:39:17,780 I love this okay 823 00:39:20,000 --> 00:39:22,280 our back is is a great security 824 00:39:22,740 --> 00:39:29,380 solution that that true Enterprise operating systems have have implemented Solaris uses it 825 00:39:29,380 --> 00:39:34,360 Linux needs it but I don't think that Linux is going to get it because of all the politics 826 00:39:34,360 --> 00:39:43,240 surrounding kernel development so you could use static binaries but that is a major disk 827 00:39:43,240 --> 00:39:49,320 and memory usage hog if you compiled everything statically lib hijack really won't even work 828 00:39:49,320 --> 00:39:52,340 um disk usage is not that 829 00:39:52,340 --> 00:40:00,560 that big of a deal but memory usage is you could maybe create some sort of smart hypervisor solution 830 00:40:01,060 --> 00:40:06,920 you could use grsec or packs but that only protects to a certain extent or you could 831 00:40:06,920 --> 00:40:12,240 use static and dynamic mixture of static and dynamic profiling so you can profile the the program 832 00:40:14,080 --> 00:40:19,000 before it's even ran and say this is what it's going to do these are the kind of shared objects 833 00:40:19,000 --> 00:40:22,020 and things that it's going to run and you can uh 834 00:40:22,340 --> 00:40:27,900 dynamically check while it's running uh whether that whether the dynamic profile 835 00:40:28,590 --> 00:40:33,660 me uh matches the static profile you'll watch for changes in the global offset table 836 00:40:35,000 --> 00:40:42,980 and you'll make sure changes reflect uh the static profile but what about shared objects 837 00:40:42,980 --> 00:40:50,860 loaded via DL open the static profiling doesn't uh help with with uh with plug-in architectures 838 00:40:50,860 --> 00:40:52,160 like just by 839 00:40:52,340 --> 00:40:58,560 every program that you use today Firefox Pigeon Apache and just about anything it's all plug-ins 840 00:40:59,640 --> 00:41:04,960 so I'm going to use I'm going to do a little bit of a demo I'm going to show you what happens 841 00:41:04,960 --> 00:41:16,580 uh when I inject a shared object so I need to set up a mirror on my display so that things can look right 842 00:41:27,240 --> 00:41:32,460 okay so right here can can everyone see that is that a good font 843 00:41:37,800 --> 00:41:42,740 all right so we have open VPN running a server right here 844 00:41:44,680 --> 00:41:51,200 and what we're going to do is we're going to we're going to connect to it 845 00:41:53,440 --> 00:41:58,020 And, what we're going to do is when I send it that command, it's going to drop me into 846 00:41:58,020 --> 00:41:58,400 a shell. 847 00:41:58,600 --> 00:42:00,480 Right now you can see I send it. 848 00:42:00,640 --> 00:42:01,780 It's working normally. 849 00:42:02,100 --> 00:42:03,940 It's not going to drop me into a shell. 850 00:42:08,920 --> 00:42:13,300 So we're going to run the inject shared object program, which I'm going to release today 851 00:42:13,300 --> 00:42:14,300 with lib hijack. 852 00:42:14,300 --> 00:42:15,200 It's a part of it. 853 00:42:22,040 --> 00:42:23,040 Oh, that's awesome. 854 00:42:23,180 --> 00:42:23,600 Okay. 855 00:42:28,020 --> 00:42:34,300 I'm running this demo as root, but because of the awesome security in Linux, it doesn't 856 00:42:34,300 --> 00:42:35,400 have to be run as root. 857 00:42:35,640 --> 00:42:40,140 It has to be run as the same user as your target process. 858 00:42:40,620 --> 00:42:44,280 So if you're targeting Apache and it's running as the Apache user, it's going to run as the 859 00:42:44,300 --> 00:42:44,520 Apache user. 860 00:42:44,520 --> 00:42:46,540 Then it needs to run as Apache. 861 00:42:48,460 --> 00:42:51,320 But I'm doing it with root just because it's easier. 862 00:43:04,500 --> 00:43:08,800 We've just injected a shared object into open VPN. 863 00:43:09,520 --> 00:43:12,100 We're going to cat the proc pin maps. 864 00:43:14,300 --> 00:43:14,760 File. 865 00:43:15,600 --> 00:43:17,920 And I'm going to show you that it's there. 866 00:43:18,140 --> 00:43:21,700 You can see that the shared object is loaded. 867 00:43:21,920 --> 00:43:24,160 Our malicious shared object is loaded. 868 00:43:24,260 --> 00:43:31,040 So now I'm going to connect to open VPN. 869 00:43:32,280 --> 00:43:33,420 Type in shell. 870 00:43:34,520 --> 00:43:36,120 It didn't disconnect me. 871 00:43:37,580 --> 00:43:39,620 We now have a shell. 872 00:43:46,660 --> 00:43:50,640 And we can see that open VPN is still running. 873 00:43:53,660 --> 00:43:55,560 And it's running like usual. 874 00:44:03,240 --> 00:44:05,000 That is my talk. 875 00:44:05,200 --> 00:44:06,660 Thank you very much for coming. 876 00:44:06,840 --> 00:44:08,620 I really enjoyed speaking here. 877 00:44:09,620 --> 00:44:10,260 . 878 00:44:13,640 --> 00:44:14,280 Yeah. 879 00:44:14,300 --> 00:44:14,500 Thank you.