1 00:00:00,000 --> 00:00:06,880 All right. Welcome. This talk is called Function Hooking for OSX and Linux. My name is Joe. 2 00:00:09,600 --> 00:00:14,460 Okay. So these slides are on my blog, timetoplead.com. You can go get them right now if you want 3 00:00:14,460 --> 00:00:19,760 to follow along. Instead of giving you guys a long intro about who I am and why I'm cool, 4 00:00:19,920 --> 00:00:24,360 I just want to give a shout out to SoftSec for getting my back during the conference 5 00:00:24,360 --> 00:00:29,060 because this is my first DEF CON and I didn't know anybody. And FreeJumpESP. And I also 6 00:00:29,060 --> 00:00:32,680 want to say sorry to the mom and the kids that I elbowed in the face as I was running 7 00:00:32,680 --> 00:00:35,840 through Caesars to get to the Apple store for an adapter. My bad. 8 00:00:36,880 --> 00:00:41,740 All right. So I'm not a security researcher. I just have a T-shirt that says security. 9 00:00:42,320 --> 00:00:45,480 So you can feel free to get on Twitter and call me a script kitty. I'm just Joe D'Amato. 10 00:00:46,900 --> 00:00:50,720 All right. So here's the problem. I was running around doing Ruby consulting for a lot of 11 00:00:50,720 --> 00:00:55,460 shops and I noticed the common problem amongst everybody was that their Ruby binaries, after 12 00:00:55,460 --> 00:00:57,920 running in memory for a long time, kind of look like that. 13 00:01:00,340 --> 00:01:08,440 They're very large and slow. And people want the Ruby binaries to be small, thin, and fast. 14 00:01:09,260 --> 00:01:12,820 But the problem is that most people are really lazy. They don't want to install custom patches 15 00:01:12,820 --> 00:01:16,740 or rebuild a Ruby binary or do anything too complicated. They just want to install some 16 00:01:16,740 --> 00:01:20,780 library that will allow them to figure out where the Ruby binary is leaking memory and 17 00:01:20,780 --> 00:01:23,420 adjust the Ruby code and then, you know, live happily ever after. 18 00:01:24,080 --> 00:01:28,600 So it turns out that in order to do that, there's a set of function hooking techniques you can employ. 19 00:01:29,480 --> 00:01:33,540 To make this all possible. So that's what this talk is about. This talk is about three function hooking 20 00:01:33,540 --> 00:01:37,440 techniques that I researched and implemented to build a Ruby memory profiler. But since I know 21 00:01:37,440 --> 00:01:40,460 there's people in the audience that don't really give a shit about performance analysis and memory 22 00:01:40,460 --> 00:01:44,780 profilers, there's also an evil example at the end of what these things can be applied to. 23 00:01:45,540 --> 00:01:49,780 So just a quick note before we get rolling. There's a lot of assembly syntax in this. 24 00:01:49,840 --> 00:01:52,620 There's a lot of assembly code in this talk. And it'll all be in AT&T syntax. 25 00:01:54,720 --> 00:01:58,440 So I can't, sorry, I saw somebody, heard somebody say, oh, I can't read the, I can't read anything 26 00:01:58,440 --> 00:01:59,040 other than AT&T. 27 00:01:59,060 --> 00:02:06,580 AT&T, my bad. So to actually do this, we need to know a few things, right? So we need to know about 28 00:02:06,580 --> 00:02:10,460 the ABI for the system we're working on. The question that we need to start with, though, is 29 00:02:10,460 --> 00:02:16,200 what the fuck's an ABI? An ABI is an application binary interface. So what does an ABI actually 30 00:02:16,200 --> 00:02:20,700 tell us? It tells us a few things. Some of the more important things for this talk are it tells 31 00:02:20,700 --> 00:02:26,920 us about alignment, the alignment of the program stack, data types, stuff like that. It tells us 32 00:02:26,920 --> 00:02:29,040 about calling convention, how functions are working, and then it tells us about the 33 00:02:29,060 --> 00:02:32,920 arguments are called, where arguments live, how that stuff works, and about object file and 34 00:02:32,920 --> 00:02:39,060 library formats. The great thing about the ABIs is that they form a hierarchy, kind of like 35 00:02:39,060 --> 00:02:44,700 this hierarchy of beards. These beards are all related in some way. They're all parts of each 36 00:02:44,700 --> 00:02:50,020 other. And the ABIs are the exact same way. You have the big system 5 ABI, which is like 200 37 00:02:50,020 --> 00:02:54,520 pages. And then as you read it, it has sections where it says, like, hey, at this point, open up 38 00:02:54,520 --> 00:02:59,040 your architecture-specific ABI and read that one. And the great thing about that is that it has 39 00:02:59,060 --> 00:03:05,820 a page in it where it says, hey, everything in here is true, but you also need to read everything 40 00:03:05,820 --> 00:03:09,080 in the I3D6 ABI. So if you actually want to understand what's going on, you need to read, like, 41 00:03:09,080 --> 00:03:14,520 700 pages. And there's also ABIs for MIPS, ARM, PBC, and Itanium as well. But lucky for us, 42 00:03:14,560 --> 00:03:22,640 the x86-64 calling convention on OSX is based on the system 5 AMD64 ABI. So there's some overlap. 43 00:03:22,800 --> 00:03:26,680 So we actually get saved a little bit. So instead of actually reading all this stuff, we can just 44 00:03:29,060 --> 00:03:33,360 actually accomplish the function hooking that I'm going to describe shortly. So the first thing we 45 00:03:33,360 --> 00:03:38,080 need to worry about is alignment. And all that we really care about for alignment in this talk with 46 00:03:38,080 --> 00:03:42,940 function hooking is that the end of the argument area before you call a function has to be aligned 47 00:03:42,940 --> 00:03:47,080 on a 16-byte boundary. So the way this is usually accomplished is either the compiler will generate 48 00:03:47,080 --> 00:03:51,940 code that never misaligns the stack, or you'll see instructions like this sprinkled throughout your 49 00:03:51,940 --> 00:03:58,540 code that are just aligning the stack pointer. We also need to care about calling convention. So 50 00:03:59,660 --> 00:04:03,200 for calling convention, all we really care about is that function arguments, wow, that color is 51 00:04:03,200 --> 00:04:08,680 horrible, function arguments live in registers left to right. So first argument's an RDI, second's an 52 00:04:08,680 --> 00:04:13,440 RSI, et cetera, et cetera. But that's only for integer class items. Other stuff gets passed on 53 00:04:13,440 --> 00:04:17,380 the stack, similar to the way arguments get passed on I3D6 if there's any I3D6 hackers in the room. 54 00:04:17,860 --> 00:04:22,680 Registers are either caller or quality saved. And that's all you pretty much need to know about 55 00:04:22,680 --> 00:04:28,000 calling convention to make binary patching work later. The big thing we need to know more about, 56 00:04:28,860 --> 00:04:34,820 though, is that we care about two of them. The first that we care about is ELF, the executable 57 00:04:34,820 --> 00:04:39,160 linkable format. This is the format that was chosen as the standard format in the System 5 ABI 58 00:04:39,780 --> 00:04:50,480 for, oh, really? Oh, hey, Dad. Sorry, let me turn that off real quick. All right. So the ELF ABI was 59 00:04:50,480 --> 00:04:54,080 picked as the standard for Unix and Unix-like operating systems. So what does that look like? 60 00:04:54,140 --> 00:04:58,800 Well, it kind of looks like this. You have an ELF header, you have a program header table, various 61 00:04:59,060 --> 00:05:03,700 sections, text section, RO data section, and then you have another table at the bottom. So the 62 00:05:03,700 --> 00:05:09,320 program header table indexes different segments. So segments contain sections. And the section 63 00:05:09,320 --> 00:05:14,520 header table at the bottom just indexes the sections. You can use a library like LibELF to 64 00:05:14,520 --> 00:05:18,560 help you wander through an ELF object, extracting useful information. The thing to keep in mind is 65 00:05:18,560 --> 00:05:23,320 that executables and shared objects each have their own set of data. So the picture that we 66 00:05:23,320 --> 00:05:28,400 were just looking at, this guy right here, this set of sections and data exists for each 67 00:05:29,060 --> 00:05:32,020 section. So you can use that to library your executable loads. And that will be important 68 00:05:32,020 --> 00:05:37,600 later, and you'll see why. So a couple sections that are important to know about. You have 69 00:05:37,600 --> 00:05:41,760 the text section where code lives. You have the PLT section. This is stub code that helps to 70 00:05:41,760 --> 00:05:46,300 resolve absolute function addresses. The got PLT, this is where absolute function addresses are 71 00:05:46,300 --> 00:05:50,140 stored, and these are used by the PLT entries. There's also a couple debugging sections, debug 72 00:05:50,140 --> 00:05:55,640 info and GNU debug link. There's two other pretty useful sections. You've got the DIN SIM and 73 00:05:55,640 --> 00:05:59,040 the DIN STIR. These two sections are used for dynamic linking. So DIN SIM and DIN STIR are used for 74 00:05:59,060 --> 00:06:03,040 dynamic linking. So DIN SIM creates a mapping between exported symbol names and offsets, and 75 00:06:03,040 --> 00:06:10,520 DIN STIR stores the actual symbol names themselves. And in addition to those, there's two other 76 00:06:10,520 --> 00:06:14,280 sections, the SIM tab and the STIR tab. SIM tab and STIR tab are actually supersets of the 77 00:06:14,280 --> 00:06:19,740 information in the DIN SIM and the DIN STIR. They include other things like local variables and 78 00:06:19,740 --> 00:06:24,500 other symbols that aren't exported. This is useful for debugging, like if you get a backtrace and 79 00:06:24,500 --> 00:06:26,880 you want to know what's going on, you want to know what arguments a function takes, you'll need 80 00:06:29,060 --> 00:06:32,640 to know. And there's lots of other sections, like for stack unwinding and exception handling and 81 00:06:32,640 --> 00:06:36,880 stuff like that. Okay. So the other binary format we care about is the mock O binary format. This is 82 00:06:36,880 --> 00:06:42,040 the binary format used on OSX. Here's a picture of what it looks like. It starts with a header. It 83 00:06:42,040 --> 00:06:46,440 consists of a bunch of load commands. Load commands describe the way the binary is going to be 84 00:06:46,440 --> 00:06:50,460 laid out in memory. So there's a segment command. The segment command is a pretty important one. It 85 00:06:50,460 --> 00:06:54,460 describes how segments are going to be laid out. And much like an ELF, you have segments, and 86 00:06:54,460 --> 00:06:58,360 segments contain sections. And we're going to go through some of the sections in a bit. 87 00:06:59,560 --> 00:07:05,820 All right. So the man page for DYLD, if you have a Mac box, you can check out the man page. And it 88 00:07:05,820 --> 00:07:11,800 has a bunch of APIs for touching mock O objects. So you can iterate through the objects that are 89 00:07:11,800 --> 00:07:16,720 mapped into the process. You can play around with them and do stuff, and it's pretty useful. And 90 00:07:16,720 --> 00:07:21,840 just like with ELF, each dilib or bundle that gets loaded has its own set of data in the picture 91 00:07:21,840 --> 00:07:25,700 that we saw before. So just a couple of sections. These are sort of similar to ELF. We have the 92 00:07:25,700 --> 00:07:29,040 text section. This is where code lives. There's the symbol stub section. This is where code lives. 93 00:07:29,060 --> 00:07:33,320 This is just a list of jump cue instructions that are useful for runtime dynamic linking, which 94 00:07:33,320 --> 00:07:37,400 we're going to go over in a bit. So don't worry. There's going to be a really pretty picture. 95 00:07:37,520 --> 00:07:41,160 That will be hard to see. But if you're looking at the slides online, it will make a lot more 96 00:07:41,160 --> 00:07:44,960 sense. So there's also the stub helper, stub code that helps to resolve absolute function 97 00:07:44,960 --> 00:07:50,960 addresses. And the symbol pointer section, which just holds table entries that are referenced by 98 00:07:50,960 --> 00:07:56,140 the stub helpers above. The other important thing to note is that symbol tables do not live in a 99 00:07:56,140 --> 00:07:59,040 segment. They have their own load command. The LC SIM tab load command. The LC SIM tab load command 100 00:07:59,040 --> 00:08:04,020 command. So this is just basically a structure that tells you, hey, at this spot in memory, 101 00:08:04,140 --> 00:08:08,920 there's a bunch of strings and there's a bunch of mappings from these strings to offsets. And 102 00:08:08,920 --> 00:08:15,900 there's also a die SIM tab. So the die SIM tab in Mako, which describes how dynamic functions 103 00:08:15,900 --> 00:08:20,660 work, that's actually just a list of offsets into the SIM tab. So on ELF you have two separate 104 00:08:20,660 --> 00:08:24,560 tables. On Mako you have one table and then you have a set of indexes into that table for things 105 00:08:24,560 --> 00:08:29,020 that are exported. All right. So let's look at a couple tools, some useful tools for die SIM 106 00:08:29,040 --> 00:08:33,520 testing objects before we actually start writing codes. So you got NM. NM is pretty useful. It 107 00:08:33,520 --> 00:08:38,840 comes on OSX and Linux. You run NM on your binary. You get symbol values on the left and symbol 108 00:08:38,840 --> 00:08:42,140 names on the right. And down the middle there's information about what type of symbol it is, 109 00:08:42,180 --> 00:08:46,340 whether it's a local symbol, global, undefined, whatever. Object dump is also useful. Object dump 110 00:08:46,340 --> 00:08:49,660 comes, you can get it on Linux. If you want it for OSX, you have to go find bin utils and build 111 00:08:49,660 --> 00:08:53,280 it. I have no idea why it doesn't come standard. So if you just run object dump on, like, say, 112 00:08:53,340 --> 00:08:57,560 your Ruby binary, you get offsets on the left, op codes, instructions, and then helpful metadata. 113 00:08:59,040 --> 00:09:03,960 There's also read elf. Read elf is obviously for elf objects. This comes on Linux. It outputs a 114 00:09:03,960 --> 00:09:07,660 huge amount of information. This is just sort of a small screen shot of what you get. It's really, 115 00:09:07,720 --> 00:09:14,560 really useful. And O tool is the complement on OSX that will output useful information about 116 00:09:14,560 --> 00:09:21,940 Maco objects. We also need to talk about strip. Not that kind of strip. There's a strip binary 117 00:09:21,940 --> 00:09:25,340 that you can run. It will strip out whatever sections you want. You can go through stripping as 118 00:09:25,340 --> 00:09:28,800 many sections as you want out of your binary. But the problem is your binary may not actually 119 00:09:28,800 --> 00:09:29,020 fit. So you can go through and strip as many sections as you want out of your binary. But the 120 00:09:29,680 --> 00:09:33,640 problem is your binary may not actually fit. So you need to obviously leave the text section 121 00:09:33,640 --> 00:09:38,060 in. If you strip the text section, you have no codes. And having no codes is no fun. You also 122 00:09:38,060 --> 00:09:41,720 need to leave in the dynamic symbol and string tables. Otherwise, dynamic linking will fail. 123 00:09:43,320 --> 00:09:48,700 Okay. So we know enough stuff. Let's start hooking some functions. So functions can be called 124 00:09:48,700 --> 00:09:52,740 in lots of different ways on x86. There's two really common ways. One common way is calling 125 00:09:52,740 --> 00:09:57,640 indirectly via a register, like you see on the top. So in this example, RBX will just hold the 126 00:09:57,640 --> 00:09:59,020 address of the function you want to call. And then you can call it directly via a register. 127 00:09:59,020 --> 00:10:04,800 And it'll just indirectly call the function. Or the way on the bottom, it looks like, so when 128 00:10:04,800 --> 00:10:07,480 you run object dump, you'll see things like you see on the bottom where it'll look like you're 129 00:10:07,480 --> 00:10:11,400 actually calling a function directly. But you're not. That's not the actual full address. It's 130 00:10:11,400 --> 00:10:15,900 just a 32-bit displacement. So what does that actually mean? Well, we can do an anatomy of a 131 00:10:15,900 --> 00:10:20,160 call instruction and get a better idea of what's going on. So this is just object dump output. 132 00:10:20,420 --> 00:10:23,840 And as you see on the right, there's a call instruction. And it has, it's calling the 133 00:10:23,840 --> 00:10:28,560 function A function, A underscore function. And the address that it lives at is 4363DC. 134 00:10:28,560 --> 00:10:34,040 So the way that's calculated is, so in object dump, right, this is the address of this instruction. 135 00:10:34,400 --> 00:10:39,500 This is the call opcode E8. And then the four bytes after it are the 32-bit displacement 136 00:10:39,920 --> 00:10:44,540 to the function you're trying to call. The way that's actually calculated is you take 137 00:10:44,540 --> 00:10:50,240 ‑‑ we're trying to call A function, so that's at that address, 4363DC. That's 138 00:10:50,240 --> 00:10:53,800 calculated by taking the address of the next instruction that's going to be executed and 139 00:10:53,800 --> 00:10:57,980 adding it to the 32-bit displacement. Remember that X86 is a little endy, and so you need 140 00:10:58,560 --> 00:10:59,980 to flip the bytes around before you add it. 141 00:11:01,920 --> 00:11:05,320 So hooking A function is actually pretty easy, right? You can just go around overwriting 142 00:11:05,320 --> 00:11:10,440 all the displacements that you see and replace the displacements to call your own handler 143 00:11:10,440 --> 00:11:14,380 function instead. And your handler function can do whatever you want. In my case, since 144 00:11:14,380 --> 00:11:17,440 I'm building a memory profiler, I actually want the Ruby binary to keep working after 145 00:11:17,440 --> 00:11:21,460 I'm done overwriting stuff. So I'm going to do my thing, and I'm also going to call the 146 00:11:21,460 --> 00:11:24,680 original function so that the Ruby VM can do what it was supposed to do normally. 147 00:11:25,460 --> 00:11:28,360 Writing the codes for this are ‑‑ you know, the codes are easy, right? This is 148 00:11:28,560 --> 00:11:33,460 pseudo code, so relax. But you just iterate through looking at each byte. Once you find 149 00:11:33,460 --> 00:11:37,500 a call instruction, you just do some math, and you can check if the 32-bit displacement 150 00:11:37,500 --> 00:11:42,020 matches the function that you're trying to hook, and if it does, you just overwrite 151 00:11:42,020 --> 00:11:46,060 that to call your handler function instead. Right? I mean, like, how hard could it possibly 152 00:11:46,060 --> 00:11:49,740 be? Well, it turns out that it's actually a little more tricky than just that, so you 153 00:11:49,740 --> 00:11:53,560 need to be kind of careful. When you're dealing with the 32-bit displacement, overwriting 154 00:11:53,560 --> 00:11:58,040 an existing call with another call, that's fine, right? Like, the stack will already 155 00:11:58,560 --> 00:12:01,320 be fine. We were going to call a function anyway, and we're just going to overwrite 156 00:12:01,320 --> 00:12:05,560 with a different call, so alignment's fine, the registers are all fine, everything's good 157 00:12:05,560 --> 00:12:09,520 to go, everybody's cool. But the problem is we can't redirect to code that's outside 158 00:12:09,520 --> 00:12:14,040 of the instruction pointer plus the 32-bit displacement, because we can only put in a 159 00:12:14,040 --> 00:12:17,380 32-bit displacement. So if you're redirecting to code that's far away, you get fucked. 160 00:12:17,660 --> 00:12:21,900 The only way to get around this is to scan the address space, searching for a free slot 161 00:12:21,900 --> 00:12:25,880 where you can M map a page in to put in a bunch of code to call. So that's how you get 162 00:12:25,880 --> 00:12:28,540 around that. You basically just scan the address space, calling M map. Within the address space, 163 00:12:28,560 --> 00:12:34,060 in that instruction pointer plus 32-bit displacement window. And that works. But it doesn't work 164 00:12:34,060 --> 00:12:37,300 for everything. So if you call a function that's exported by a dynamic library, that 165 00:12:37,300 --> 00:12:41,060 works slightly differently. So we need to talk a little bit about how runtime dynamic 166 00:12:41,060 --> 00:12:45,140 linking actually works. So I'm going to describe how it works on ELF, but it works almost identically 167 00:12:45,140 --> 00:12:48,560 on Mach O. And I'm going to have a pretty picture soon that's going to show the difference 168 00:12:48,560 --> 00:12:52,980 between ELF and Mach O. The code is exactly the same, it's just the instructions are rearranged 169 00:12:52,980 --> 00:12:58,000 slightly. So here's a call instruction calling a function 170 00:12:58,000 --> 00:13:03,320 that has a function that's exported. So if you look at the top, you can see over there 171 00:13:03,320 --> 00:13:08,340 on the far right, it says, you know, RB new object at PLT. RB new object is just some function 172 00:13:08,340 --> 00:13:13,320 in the Ruby VM. And at PLT, that's object dumps. Object dump is telling us, hey, you're 173 00:13:13,320 --> 00:13:16,760 not actually calling that function specifically, you're calling this, like, dynamic linking 174 00:13:16,760 --> 00:13:19,160 thing that's going to go find it and actually execute it later. 175 00:13:20,920 --> 00:13:25,120 So if we disassemble instructions starting at that address, you see there's three instructions 176 00:13:25,120 --> 00:13:27,460 that come up. There's a jump, a push, and a jump. 177 00:13:28,440 --> 00:13:33,740 The first jump is referring to an address that exists, that address lives in the got 178 00:13:33,740 --> 00:13:39,440 PLT section. That is actually an entry in a table. That entry initially points at the 179 00:13:39,440 --> 00:13:43,960 address of the push instruction. Okay. So I'm going to say that again. Because the first 180 00:13:43,960 --> 00:13:48,420 time I saw this, I didn't understand why that was. So initially when you first execute 181 00:13:48,420 --> 00:13:53,860 your binary, before this function is called for the first time, the initial state of the 182 00:13:53,860 --> 00:13:57,980 system is that you have a jump instruction that's jumping to an address stored in a table. 183 00:13:58,580 --> 00:14:03,060 The address stored in the table points at the push instruction that comes right after 184 00:14:03,060 --> 00:14:07,420 the jump. Okay. Now, the reason why that is is because after you execute that jump and 185 00:14:07,420 --> 00:14:11,080 you land right there on that push instruction, you execute the push instruction, which puts 186 00:14:11,080 --> 00:14:13,760 an ID on the stack, and then it invokes the runtime dynamic linker. 187 00:14:14,740 --> 00:14:18,640 Runtime dynamic linker goes and it runs around and it finds the function you wanted to call 188 00:14:18,640 --> 00:14:22,920 using the ID that was pushed. It then goes back to the table and fills in the address 189 00:14:22,920 --> 00:14:27,980 of the function in the table. That way, subsequent calls to the same function will just live 190 00:14:28,000 --> 00:14:30,740 and land you in that function directly instead of landing you back at that push instruction. 191 00:14:32,040 --> 00:14:35,660 Does that make sense? Is everybody cool with that? All right. Word. 192 00:14:36,740 --> 00:14:40,160 So hooking that is actually really easy. We can just pretend that we're the dynamic 193 00:14:40,160 --> 00:14:45,780 linker and go around poisoning other people's global offset table entries. So we just redirect 194 00:14:45,780 --> 00:14:49,600 execution by cruising around, finding other people's PLT entries and just overrunning 195 00:14:49,600 --> 00:14:53,500 them with the address that we care about. So how that would work, here's an example. 196 00:14:53,660 --> 00:14:57,880 You have this thing. You're calling RB new object. The table entry is poisoned with a 197 00:14:58,000 --> 00:15:03,460 bad function. Let's just say the address is OX dead beef. And you land in this other 198 00:15:03,460 --> 00:15:08,100 function where you can do whatever you want. But the question that might be running through 199 00:15:08,100 --> 00:15:12,440 your mind now, or I hope it's running through your mind is, hey, Joe, in your handler function, 200 00:15:12,700 --> 00:15:16,800 you're calling the function you're trying to hook. So shouldn't that be an infinite 201 00:15:16,800 --> 00:15:22,520 loop? It turns out that's not because if you remember from earlier, each dynamic object 202 00:15:22,520 --> 00:15:27,980 that you load into your process has its own set of functions. So if you remember from 203 00:15:28,000 --> 00:15:30,500 earlier, each dynamic object has its own set of data. So each dynamic object has its 204 00:15:30,500 --> 00:15:35,380 own set of PLT entries and its own global offset table. So as long as you poison everybody 205 00:15:35,380 --> 00:15:43,420 else's except for the got PLT section of the library that includes your bad code, as 206 00:15:43,420 --> 00:15:47,500 long as you don't poison your library itself, then you'll be okay. Dynamic linking will 207 00:15:47,500 --> 00:15:50,260 happen normally for you. You just have to overwrite everybody else's shit. 208 00:15:51,320 --> 00:15:54,740 Okay. So what about mock O? We just described how it works in ELF. What about mock O? Well, 209 00:15:54,760 --> 00:15:57,880 it's not quite like comparing apples to oranges. It's actually a lot ‑‑ it's actually really 210 00:15:58,000 --> 00:16:01,300 similar. This is the graphic that I was talking about that's, like, super sexy that I spent 211 00:16:01,300 --> 00:16:06,300 a lot of time drawing. ELF is on the top, obviously. Mock O is on the bottom. The instructions 212 00:16:06,300 --> 00:16:10,620 ‑‑ the actual assembly instructions that are generated are exactly the same. They're 213 00:16:10,620 --> 00:16:15,840 just rearranged, right? So on ELF, you have a bunch of ‑‑ you have a jump instruction 214 00:16:15,840 --> 00:16:20,220 followed by invoking the dynamic linker as we saw on the other slides. But on mock O, 215 00:16:20,300 --> 00:16:23,580 they just put all the jump instructions together and then all the dynamic linking invocation 216 00:16:23,580 --> 00:16:27,080 functions together. So they just kind of rearrange stuff. But it's exactly the same thing. It 217 00:16:28,000 --> 00:16:32,140 works the same way. Okay. So we know how to hook two types of functions. 218 00:16:32,520 --> 00:16:36,420 But there's a third type that also matters. Inline functions. And this is actually really 219 00:16:36,420 --> 00:16:40,100 fucking tricky. We can't hook ‑‑ there's a function that I needed to hook to build 220 00:16:40,100 --> 00:16:43,780 this memory profiler called add free list. But the problem with trying to hook add free 221 00:16:43,780 --> 00:16:47,160 list is that it's marked as inline, which means that the compiler has the option of 222 00:16:47,160 --> 00:16:53,140 inserting the instructions for this function directly into whoever is calling this function. 223 00:16:54,020 --> 00:16:57,660 So if that happens, you won't actually see any explicit call instructions in your binary. 224 00:16:58,760 --> 00:17:02,700 So what do we do now? Are we jacked? Or is there a way around this? Well, it turns out 225 00:17:02,700 --> 00:17:07,620 that if you look closely at this generated code, you should notice something. You notice 226 00:17:07,620 --> 00:17:12,220 there's this identifier called free list and free list is getting updated to point 227 00:17:12,220 --> 00:17:18,080 to the new head of the free list as objects are recycled. Notice that free list isn't defined 228 00:17:18,080 --> 00:17:27,980 in this function. So it has file level scope. So what about a crazy, crazy stupid idea? Since free list has file level scope, it can 229 00:17:28,000 --> 00:17:33,040 It lives at some static address. Add free list updates the free list by inserting something 230 00:17:33,040 --> 00:17:36,920 at the head of the free list. Why not just search the binary for all move instructions 231 00:17:36,920 --> 00:17:43,180 that target free list and just overwrite all of those? And it turns out that that might 232 00:17:43,180 --> 00:17:46,820 actually work, but there are a few problems. The system isn't actually ready to make a 233 00:17:46,820 --> 00:17:50,880 call instruction at that point. What does that mean, not ready? Remember before we talked 234 00:17:50,880 --> 00:17:57,040 about the ABI and how the ABI talks about alignment and calling convention? Well, it's 235 00:17:57,040 --> 00:18:02,120 not quite ready to call a function because there's a bunch of move instructions, if we 236 00:18:02,120 --> 00:18:04,920 just implement a call instruction, there's no guarantee that the stack is aligned or 237 00:18:04,920 --> 00:18:09,460 arguments are on the right registers. So what do we need to do? 238 00:18:09,680 --> 00:18:14,540 Well, it turns out that we can actually use another type of instructions instead. We can 239 00:18:14,540 --> 00:18:20,220 actually just use a jump. We can insert a jump instruction, transfer execution to an 240 00:18:20,220 --> 00:18:24,980 assembly stub that we generate at run time and recreate the instruction we overwrote, 241 00:18:25,220 --> 00:18:26,340 set the system up. 242 00:18:26,340 --> 00:18:29,700 up to call a function and then do whatever we're going to do, whether it's good or bad 243 00:18:29,700 --> 00:18:33,440 or whatever, and then jump back and resume execution as if nothing happened. And this 244 00:18:33,440 --> 00:18:36,440 is sort of like the crusty third hook that you need to pull this off. 245 00:18:37,200 --> 00:18:41,340 So there's a checklist of things we need to do. We need to save and restore any registers. 246 00:18:41,540 --> 00:18:46,380 We need to align the stack. We need to regenerate the instruction that we're overwriting. We 247 00:18:46,380 --> 00:18:50,260 need to arrange for any arguments that we care about to end up being put in the right 248 00:18:50,260 --> 00:18:54,620 register so they get passed to the function. We need to invoke our code that does whatever 249 00:18:54,620 --> 00:18:57,820 it's going to do. And then we need to resume execution as if nothing happened. 250 00:18:59,640 --> 00:19:03,540 So here's an example of an instruction that is updating the free list that we're going 251 00:19:03,540 --> 00:19:07,320 to overwrite. So this is just saying, hey, you know, I'm going to take RBX and I'm going 252 00:19:07,320 --> 00:19:10,860 to write it to the top of the free list. So we can't put a call instruction there because 253 00:19:10,860 --> 00:19:14,060 as we said, the system is not ready to make a function call. The stack isn't aligned. 254 00:19:14,220 --> 00:19:16,060 Arguments aren't in the right place. Everything is all fucked up. 255 00:19:16,520 --> 00:19:21,160 So what we can do instead is we can just insert a jump instruction. So it turns out the move 256 00:19:21,160 --> 00:19:24,600 instruction is seven bytes. The jump instruction including the offset is only five bytes. So 257 00:19:24,620 --> 00:19:30,120 since we can't grow or shrink the binary, we have to insert two nop pads just to make 258 00:19:30,120 --> 00:19:33,520 everything work out. So this address right there, that's just the address of the assembly 259 00:19:33,520 --> 00:19:37,020 stub we're going to transfer execution to. And when the assembly stub finishes, we want 260 00:19:37,020 --> 00:19:40,500 to return back to this nop and then just fall through and continue executing as if 261 00:19:40,500 --> 00:19:41,060 nothing happened. 262 00:19:41,960 --> 00:19:45,680 So here's a shortened assembly stub that actually does this. This is an assembly stub that when 263 00:19:45,680 --> 00:19:49,580 I ran my code, this was generated at runtime. It has to be generated at runtime because 264 00:19:49,580 --> 00:19:53,480 this first instruction up here at the top, this is getting overwritten. And who knows 265 00:19:53,480 --> 00:19:54,600 what it's going to get ‑‑ who knows what it's going to get ‑‑ who knows what 266 00:19:54,620 --> 00:19:57,740 that instruction is going to look like when your Ruby binary is actually running in memory. 267 00:19:58,000 --> 00:20:01,740 Everybody compiles their own Ruby. And so this could ‑‑ instead of using RBX, who 268 00:20:01,740 --> 00:20:05,960 knows, maybe you're using RCX on your Ruby binary. So you just search for anything that 269 00:20:05,960 --> 00:20:08,840 targets a free list, you overwrite it with a jump, and then you have to regenerate what 270 00:20:08,840 --> 00:20:12,520 you overwrote on this little trampoline here. This is actually shortened. The real thing 271 00:20:12,520 --> 00:20:16,220 is way, way longer and way uglier. But I'm going to provide a link to the code later. 272 00:20:16,340 --> 00:20:18,720 That way you can actually check it out if you want to see what the deal is. 273 00:20:19,240 --> 00:20:24,040 So yeah, we need to recreate the instruction we overwrote. We need to save or register 274 00:20:24,620 --> 00:20:27,380 past our argument. So I'm just going to save RDI. That's where the first argument lives, 275 00:20:27,500 --> 00:20:33,440 if you remember from earlier in the talk. We're going to move the free list into RDI 276 00:20:33,440 --> 00:20:37,620 because for a memory profiler, I want to know who just got freed. That way I can track 277 00:20:37,620 --> 00:20:42,680 information about them. I'm going to save RBX because I'm going to fill it with a value 278 00:20:43,160 --> 00:20:48,220 in a minute. I'll align the stack to comply with the ABI. And then I'll move the address 279 00:20:48,220 --> 00:20:53,680 of a handler function into RBX. That's why I saved RBX a few instructions earlier. I'll 280 00:20:53,680 --> 00:20:54,600 call my handler function. I'm going to call my handler function. I'm going to call my handler 281 00:20:54,600 --> 00:20:59,080 function indirectly via RBX. And then the handler function will just do whatever, something 282 00:20:59,080 --> 00:21:03,560 good or something bad, you know, wherever you want to do. You do a bunch of cleanup, 283 00:21:03,700 --> 00:21:07,780 and then you just jump back to wherever you overwrote the instruction originally. 284 00:21:08,620 --> 00:21:12,700 And the crazy thing about all this hackery is that when you're done, it actually works. 285 00:21:12,960 --> 00:21:17,200 You can actually use this. Like, if you have Ruby and you care, you can gem install memprof 286 00:21:17,200 --> 00:21:20,040 and you can actually use this thing. And all the code that hooks these three types 287 00:21:20,040 --> 00:21:23,740 of functions exist in this Ruby library that I built called memprof. So you can go on GitHub. 288 00:21:23,740 --> 00:21:28,340 You can read the code. And it's real. And it's actually pretty powerful. These three 289 00:21:28,340 --> 00:21:32,480 things can be used to build lots and lots of really interesting tools. So just to give 290 00:21:32,480 --> 00:21:35,040 you, like, a quick rundown of, like, some of the cool things you can do with this. 291 00:21:36,000 --> 00:21:39,240 So, you know, we can do stuff about memory, right? Like, I can look ‑‑ I can write 292 00:21:39,240 --> 00:21:43,020 this Ruby code. I can say, hey, include my memory profiler, start profiling, run a bunch 293 00:21:43,020 --> 00:21:46,980 of code and give me stats. And I'll get stats like this. Like, I can find out on the exact 294 00:21:46,980 --> 00:21:50,820 file and line number where objects are being allocated. So if I have a memory leak, I can 295 00:21:50,820 --> 00:21:53,720 look and I can say, hey, you know, on file 1.RB, I can say, hey, you know, on file 1.RB, 296 00:21:53,720 --> 00:21:58,240 on line 25, I'm creating a million objects. So I can know exactly where things are leaking, 297 00:21:58,360 --> 00:22:03,140 which is pretty fucking sick. So the text‑based output is cool, but it's really painful to 298 00:22:03,140 --> 00:22:09,120 grok. So my roommate Amon built a web UI for this thing. So you can, like, actually 299 00:22:09,120 --> 00:22:12,080 traverse stuff and you can learn all kinds of crazy information. You can find, you know, 300 00:22:12,080 --> 00:22:15,880 who's holding references to what other objects, where you're leaking memory. You can look 301 00:22:15,880 --> 00:22:21,120 at, like, you know, the inheritance tree of objects. You can look at the subclasses 302 00:22:21,120 --> 00:22:23,700 of string and rails. You can explore the socket hierarchy. You can look at, like, you know, 303 00:22:24,160 --> 00:22:28,400 all kinds of stuff. You can find, like, massive arrays. You can ‑‑ and since in Ruby, 304 00:22:28,500 --> 00:22:31,780 all Ruby code is actually just stored in the heap, I can actually just go and regenerate 305 00:22:31,780 --> 00:22:36,520 Ruby code from, you know, using these function hooks, right, because I can just grab these 306 00:22:36,520 --> 00:22:39,200 things when they're getting created and I can just reverse them back into Ruby code, 307 00:22:39,320 --> 00:22:42,760 which is pretty cool, too. You can go and look through Ruby standard library and you 308 00:22:42,760 --> 00:22:46,140 can say, oh, look at these, like, really bizarre functions that exist in Ruby. You can see, 309 00:22:46,240 --> 00:22:50,260 you know, if you have sockets open to any web services, you can see what you're connected 310 00:22:50,260 --> 00:22:52,780 to on what port. And that's all pretty cool. 311 00:22:53,700 --> 00:22:56,760 Request information, too, right? Like, you can find out, you know, how long requests 312 00:22:56,760 --> 00:23:02,120 take, you know, which rails control you hit, request information. You get crazy stuff, 313 00:23:02,180 --> 00:23:05,100 too. You can track MySQL queries, right? Since I can hook into anything, I can track 314 00:23:05,100 --> 00:23:08,880 anything I want. I can track, like, MySQL queries, how many I make, how long they take. 315 00:23:09,020 --> 00:23:12,620 I can track the garbage collector, how many calls there are to the GC, how long these 316 00:23:12,620 --> 00:23:17,960 calls take. You know, I can track how many objects are created in general, which is pretty 317 00:23:17,960 --> 00:23:22,720 useful as well. And that's all cool, but what about some evil things we can do instead, 318 00:23:22,880 --> 00:23:23,000 right? 319 00:23:23,700 --> 00:23:27,520 Performance stuff is cool, but maybe you don't care about performance. So you can build 320 00:23:27,520 --> 00:23:31,240 a library that does something evil, but you'll need a social engineering aspect of it, because 321 00:23:31,240 --> 00:23:34,700 you need some way to get people to actually use your library. In a few slides, we'll 322 00:23:34,700 --> 00:23:37,320 talk about how to actually get around this so you don't actually need to social engineer. 323 00:23:37,480 --> 00:23:41,280 But for right now, we can build a library that will do something bad, but we need to 324 00:23:41,280 --> 00:23:47,000 just convince people to use it. So how would we do that? Well, evil lives. I built an evil 325 00:23:47,000 --> 00:23:51,480 version of memprof. The branch on GitHub is just DNW. That stands for do not want. 326 00:23:52,080 --> 00:23:53,680 If you try to build it, it will fail. It will fail. If you try to build it, it will fail. 327 00:23:53,700 --> 00:23:57,960 I put a bug in the make file. That way, nobody can accidentally go screw themselves by building 328 00:23:57,960 --> 00:24:01,480 this. And the way that I would social engineer somebody to use this is I would say, like, 329 00:24:01,500 --> 00:24:08,040 hey, bro, this makes Ruby faster. And it does make Ruby faster, because what it does 330 00:24:08,040 --> 00:24:12,140 is it hooks the read system call. It looks for a magic cookie. In this case, the magic 331 00:24:12,140 --> 00:24:18,120 cookie is just my name, Joe. And then it just disables the garbage collector. The result 332 00:24:18,120 --> 00:24:23,280 is Ruby is fast. But it is now a time bomb, right? 333 00:24:24,960 --> 00:24:28,000 So, eventually, it'll eat all the RAM on your system and your system will melt down. 334 00:24:29,320 --> 00:24:32,520 And, you know, if you really want to drill it in and hey, it makes Ruby faster, like, 335 00:24:32,540 --> 00:24:36,300 look, here's a bullshit benchmark, but just don't tell them it's bullshit, but look, here's 336 00:24:36,300 --> 00:24:41,320 normal Ruby and normal Ruby takes seven and a half seconds to do the stupid benchmark. 337 00:24:42,080 --> 00:24:46,420 You know, 26 requests per second. Then after you use this thing that makes Ruby faster, 338 00:24:46,580 --> 00:24:50,540 suddenly you shave off a second off the time and you're now up to 30 requests per second. 339 00:24:50,660 --> 00:24:53,680 So you show this to a Ruby developer and they're like, dude, I need this right now. So here's 340 00:24:56,120 --> 00:25:00,240 So anyway, this example is stupid, but the whole point is just to prove a concept. 341 00:25:00,360 --> 00:25:01,140 You can do anything, right? 342 00:25:01,240 --> 00:25:05,080 Like, you could hook read and write and just phone home with all the data coming in or 343 00:25:05,080 --> 00:25:05,700 leaving the application. 344 00:25:06,080 --> 00:25:08,820 You can fork a back door when you see a specific cookie. 345 00:25:09,600 --> 00:25:10,720 You can do whatever you want. 346 00:25:12,040 --> 00:25:15,800 But the shitty thing is that we have to social engineer, and that's kind of a problem. 347 00:25:18,060 --> 00:25:19,480 This guy also has a problem. 348 00:25:21,200 --> 00:25:22,380 Unrelated to social engineering. 349 00:25:24,760 --> 00:25:29,760 So the problem with social engineering, at least from my perspective, is that it's painful. 350 00:25:31,280 --> 00:25:35,420 And I don't want to have to social engineer things if I don't have to, like, if there's 351 00:25:35,420 --> 00:25:35,940 a way around it. 352 00:25:36,000 --> 00:25:39,780 So it would be cool if there was a way to inject these evil libraries into running processes 353 00:25:39,780 --> 00:25:43,140 without having to convince people to download my thing and look at some bullshit benchmark. 354 00:25:44,020 --> 00:25:45,720 It turns out that there is a way of doing this. 355 00:25:45,900 --> 00:25:49,400 There is a project written by this guy called Sean Clowes or Clowes or something. 356 00:25:49,920 --> 00:25:50,960 It's called inject SO. 357 00:25:51,180 --> 00:25:53,720 It injects libraries into running processes using Ptrace. 358 00:25:53,980 --> 00:25:56,240 This hack is actually super, super clever. 359 00:25:57,900 --> 00:26:02,460 To borrow a very awesome Internet meme, fucking magnets, how do they work? 360 00:26:03,120 --> 00:26:05,600 I want to know how inject SO works because it's pretty cool. 361 00:26:06,580 --> 00:26:07,600 It's actually pretty simple. 362 00:26:07,840 --> 00:26:08,760 It just uses Ptrace. 363 00:26:08,840 --> 00:26:12,200 Ptrace allows you to view and modify the register set and address space of another process. 364 00:26:12,480 --> 00:26:15,300 Any permissions that are set on the memory in that process are ignored. 365 00:26:15,500 --> 00:26:18,900 So even if a page is marked as read only, you can still write all over it or do whatever 366 00:26:18,900 --> 00:26:19,240 you want. 367 00:26:21,160 --> 00:26:25,200 So the way inject SO works is it just attaches to a target process using Ptrace. 368 00:26:25,600 --> 00:26:28,120 It saves a copy of a small piece of the program stack. 369 00:26:28,680 --> 00:26:30,400 It saves a copy of the register set. 370 00:26:30,960 --> 00:26:34,300 And then it creates a fake stack frame with a save return address of zero. 371 00:26:36,620 --> 00:26:38,820 You then set the register set to point to DL open. 372 00:26:39,200 --> 00:26:41,880 So you have, you know, the instruction pointer pointing to the address of DL open. 373 00:26:42,000 --> 00:26:46,640 You set up the arguments so you have the path to the shared object, the mode stored 374 00:26:46,640 --> 00:26:47,100 in RSI. 375 00:26:47,620 --> 00:26:48,760 And you just let it rip. 376 00:26:48,760 --> 00:26:49,380 Okay. 377 00:26:49,460 --> 00:26:52,720 So you wait and eventually it will segfault because once DL open is done, it will load 378 00:26:52,720 --> 00:26:55,520 in this library that you want it to load in and then it will segfault because the save 379 00:26:55,520 --> 00:26:56,360 return address is zero. 380 00:26:58,040 --> 00:27:03,320 When the segfault happens, you as the tracing process, you as the tracer of the process 381 00:27:03,320 --> 00:27:04,180 will get a signal. 382 00:27:05,060 --> 00:27:06,740 So the child process will get suspended. 383 00:27:07,020 --> 00:27:09,240 You'll be awoken and you'll be allowed to do stuff to it. 384 00:27:09,320 --> 00:27:13,280 So you just restore the stack, restore the register set and just let the world continue 385 00:27:13,280 --> 00:27:16,560 execute, you know, let the world continue on its merry way as if nothing bad happened. 386 00:27:16,900 --> 00:27:17,860 And this actually works. 387 00:27:17,880 --> 00:27:18,380 It's pretty cool. 388 00:27:18,820 --> 00:27:20,220 But there's a couple downsides. 389 00:27:20,280 --> 00:27:24,440 There's downsides to both aspects of the ptrace method and the evil DSO method. 390 00:27:25,900 --> 00:27:27,840 Remote allocating memory is a pain in the ass. 391 00:27:27,980 --> 00:27:29,800 It's doable, but it's just really painful to do. 392 00:27:30,540 --> 00:27:32,880 Generating segfaults and running processes might be bad. 393 00:27:33,660 --> 00:27:38,220 Perhaps the system administrator has some alert thing set up so that if a segfault happens, 394 00:27:38,360 --> 00:27:39,200 then they get an email. 395 00:27:39,460 --> 00:27:43,280 I have that set up on my boxes, so if I get a segfault, I know that something bad happened. 396 00:27:44,080 --> 00:27:47,300 And binary patching in general is hard to do, like the stuff that we just talked about 397 00:27:47,300 --> 00:27:47,500 earlier. 398 00:27:48,760 --> 00:27:49,480 Those things are complicated. 399 00:27:50,860 --> 00:27:55,720 And it's possible to do with ptrace, but like adding another layer of difficult in between 400 00:27:55,720 --> 00:27:57,360 you and actually doing what you want to do is no fun. 401 00:27:58,860 --> 00:28:02,180 So binary patching is hard, but doing it with ptrace, it just makes it harder. 402 00:28:02,760 --> 00:28:05,960 The problem with the evil DSO, though, is getting the user to use your library might 403 00:28:05,960 --> 00:28:06,320 be hard. 404 00:28:06,480 --> 00:28:10,540 And already running processes will need to be killed and restarted, which also sucks. 405 00:28:10,940 --> 00:28:13,520 And you need to poison the app each time it's started. 406 00:28:14,540 --> 00:28:16,740 And in general, binary patching is hard. 407 00:28:17,420 --> 00:28:18,740 But what if there was a way to build a binary patching system? 408 00:28:18,740 --> 00:28:19,540 Like bootstrap the process? 409 00:28:19,900 --> 00:28:22,140 What if we can just combine both methods? 410 00:28:22,480 --> 00:28:27,620 Like we can just use inject SO to load the evil DSO, and then the evil DSO will just 411 00:28:27,620 --> 00:28:30,960 take it from there and do a bunch of, you know, do all the crazy binary patching stuff. 412 00:28:32,000 --> 00:28:35,380 So in order to do that, we needed a 64-bit version of inject SO. 413 00:28:35,620 --> 00:28:39,860 But it turns out that the inject SO that I talked about by that dude Sean is only 32-bit. 414 00:28:40,160 --> 00:28:43,420 So I sort of, I was getting ready to write a 64-bit port, and then I searched the internet 415 00:28:43,420 --> 00:28:48,000 a bit and I found that this guy Stealth ported inject SO to 64-bit boxes. 416 00:28:48,880 --> 00:28:51,060 This is his, like, blog post releasing the source code. 417 00:28:51,620 --> 00:28:55,080 I did a bunch of trivial cleanup on it because it was, like, a little bit old and kind of 418 00:28:55,080 --> 00:28:56,540 ‑‑ it was suffering from some bit rot. 419 00:28:56,680 --> 00:28:59,660 So I did some minor cleanup and I put the code on the Git Hubs. 420 00:29:00,040 --> 00:29:02,020 So if you want to copy that, you can go there and check it out. 421 00:29:02,480 --> 00:29:05,720 I tested it on a 64-bit Ubuntu VM, and it actually works. 422 00:29:07,840 --> 00:29:12,500 So I can use inject SO and I can inject my stupid thing that looks for the string Joe 423 00:29:12,500 --> 00:29:15,660 and disables the garbage collector, and then it DOSs my box. 424 00:29:16,000 --> 00:29:17,640 So it's pretty cool. 425 00:29:17,680 --> 00:29:18,520 Because, like, I had this thing. 426 00:29:18,740 --> 00:29:22,860 I had this web server running and I just used inject SO to infect it and then all hell broke 427 00:29:22,860 --> 00:29:23,520 loose after that. 428 00:29:23,740 --> 00:29:26,180 I was going to try to demo that, but then I wouldn't be able to give the rest of the talk. 429 00:29:26,580 --> 00:29:27,660 So I can't demo that. 430 00:29:29,360 --> 00:29:30,920 So anyway, using those two things, I don't know. 431 00:29:30,940 --> 00:29:31,500 I think it's kind of cool. 432 00:29:31,620 --> 00:29:33,540 I don't know if it's a jackpot or not, but it's definitely pretty sweet. 433 00:29:35,800 --> 00:29:37,640 So we talked about some attack mechanisms. 434 00:29:37,840 --> 00:29:40,160 So what are some defense mechanisms to protect against this sort of thing? 435 00:29:41,000 --> 00:29:44,620 Like I said earlier, I'm not a security researcher, so I don't know, like, you know, all the up‑to‑date, 436 00:29:44,640 --> 00:29:48,640 like, amazing defense mechanisms that are out there, but what I do know for sure is that 437 00:29:49,300 --> 00:29:50,040 everything's a trade‑off. 438 00:29:50,240 --> 00:29:53,240 So there's a trade‑off between defense mechanisms that are difficult to implement, difficult 439 00:29:53,240 --> 00:29:57,240 to understand, versus just, like, you know, standard run‑of‑the‑mill, like Ubuntu, 440 00:29:57,380 --> 00:29:58,360 like out of the box, right? 441 00:29:59,460 --> 00:30:03,540 So I went through some of the defense mechanisms that I know about and discussed, like, you 442 00:30:03,540 --> 00:30:05,780 know, really briefly, like, why they're good or why they're bad or whatever. 443 00:30:06,460 --> 00:30:09,140 So I'm sure you guys know about the NX bit, the no‑execute bit. 444 00:30:09,440 --> 00:30:13,120 That's cool, but if we just load our DSO and our DSO can just call mprotect and say, hey, 445 00:30:13,160 --> 00:30:16,360 this page that was, you know, not execute, just make it execute. 446 00:30:16,740 --> 00:30:17,920 So that doesn't really help us. 447 00:30:18,740 --> 00:30:21,680 You can strip out debug information, but most people just use pre‑built binaries. 448 00:30:21,740 --> 00:30:23,760 Most people just use whatever comes with their distribution of Linux. 449 00:30:24,160 --> 00:30:28,380 So I can just, like, look ‑‑ I can just get that VM, I can download the debug symbols 450 00:30:28,380 --> 00:30:29,720 and I can figure out all the offsets myself. 451 00:30:30,700 --> 00:30:34,800 You can statically link everything if you want and then you still have the problem of 452 00:30:34,800 --> 00:30:37,420 getting overwritten otherwise, but you're not ‑‑ you don't have to worry about 453 00:30:37,420 --> 00:30:39,100 your global offset tables getting poisoned. 454 00:30:39,400 --> 00:30:41,140 The problem with that is your binaries get pretty fat. 455 00:30:42,080 --> 00:30:44,860 You can put all the text ‑‑ all text code in ROM. 456 00:30:45,800 --> 00:30:46,700 That will probably work. 457 00:30:46,820 --> 00:30:47,180 I don't know. 458 00:30:47,180 --> 00:30:50,340 I mean, perhaps there's, like, some really crazy legacy C code that if it's put in read‑only 459 00:30:50,340 --> 00:30:53,760 memory will actually die, but in theory that's supposed to work. 460 00:30:53,860 --> 00:30:54,880 So that would actually work. 461 00:30:55,620 --> 00:30:57,780 You can also not load DSOs at runtime. 462 00:30:58,060 --> 00:31:01,200 The problem with that, then, is you sacrifice any and all plug‑in architectures that 463 00:31:01,200 --> 00:31:01,580 are out there. 464 00:31:02,220 --> 00:31:04,760 You can disable P trace, but P trace is pretty sweet. 465 00:31:04,840 --> 00:31:07,960 If you disable P trace, you lose GDB and you lose S trace, and both of those are really 466 00:31:07,960 --> 00:31:08,520 fucking useful. 467 00:31:09,220 --> 00:31:11,100 You can also check proc PID maps. 468 00:31:11,820 --> 00:31:12,580 That works. 469 00:31:13,080 --> 00:31:17,160 So if you watch proc PID maps all day long and somebody injects a library in, that's 470 00:31:17,180 --> 00:31:21,360 It will appear on proc PID maps and you can see, like, hey, this, like, you know, whatever, 471 00:31:21,480 --> 00:31:23,920 evil.SO is now running in my process. 472 00:31:24,180 --> 00:31:25,220 So that would actually work. 473 00:31:25,340 --> 00:31:26,740 It's just kind of painful to do that. 474 00:31:28,980 --> 00:31:35,200 So anyway, sort of looking forward, like, on more experimental stuff, I think my ‑‑ for 475 00:31:35,200 --> 00:31:38,160 me personally, my future research is going to be sort of geared toward exploring alternative 476 00:31:38,160 --> 00:31:40,320 binary formats and, like, other crazy things you can do. 477 00:31:42,240 --> 00:31:43,100 So just to recap. 478 00:31:43,360 --> 00:31:47,080 We talked about three different binary ‑‑ binary patches. 479 00:31:47,180 --> 00:31:47,540 Matching techniques. 480 00:31:48,440 --> 00:31:54,660 These techniques leave the original binary in working fashion after they're done. 481 00:31:55,140 --> 00:31:56,480 So kind of like teeth whitening, right? 482 00:31:56,520 --> 00:31:58,620 Like you can still eat food after you're done, just looks a little different. 483 00:31:58,760 --> 00:31:59,240 Same deal. 484 00:31:59,840 --> 00:32:01,340 And there's three things that matter, right? 485 00:32:01,360 --> 00:32:04,980 There's alignment, calling convention, and object file and library formats. 486 00:32:05,840 --> 00:32:09,740 And before I end this, I actually had a couple extra slides of something really weird I found 487 00:32:09,740 --> 00:32:11,500 that I wanted to share in case I had some extra time. 488 00:32:11,760 --> 00:32:13,220 So look, an alien baby. 489 00:32:13,920 --> 00:32:14,300 . 490 00:32:15,440 --> 00:32:18,540 And I saw this really crazy post to a mailing list. 491 00:32:18,660 --> 00:32:20,920 The title of the post was interesting behavior on OSX. 492 00:32:21,100 --> 00:32:25,060 This was written by a guy named Steven Edwards, November 29, 2007. 493 00:32:25,380 --> 00:32:29,360 This is the link to the actual mailing list post that he wrote. 494 00:32:30,080 --> 00:32:35,060 It turns out that Leopard actually has a PE loader, which is really weird. 495 00:32:35,180 --> 00:32:40,740 So if you execute this code on a Leopard system and you try to load a Win32 executable, you 496 00:32:40,740 --> 00:32:41,640 get this output. 497 00:32:42,000 --> 00:32:43,080 So for whatever reason. 498 00:32:43,080 --> 00:32:45,900 There's a loader for Win32 binaries on OSX. 499 00:32:46,660 --> 00:32:51,620 So even if you had all the Windows DLLs in place in the right path so everything could 500 00:32:51,620 --> 00:32:54,660 be found, it wouldn't actually work anyway, right, because all the APIs are different. 501 00:32:54,880 --> 00:32:55,860 But it's still sort of curious. 502 00:32:55,980 --> 00:32:58,040 Like, why the hell is there a PE loader in OSX? 503 00:32:58,940 --> 00:33:01,340 So I don't know if anybody out there knows the answer to that question. 504 00:33:01,460 --> 00:33:04,580 But definitely something to check out if you guys are looking to play around with something 505 00:33:04,580 --> 00:33:04,920 weird. 506 00:33:05,040 --> 00:33:06,220 I think this is now disabled. 507 00:33:06,420 --> 00:33:08,660 I tried this recently on Snow Leopard and it didn't work. 508 00:33:09,520 --> 00:33:12,480 But I remember trying this a few months ago on Snow Leopard and I thought it did work. 509 00:33:12,480 --> 00:33:15,160 So I'm not really sure what's going on with that, but it's pretty bizarre that there's 510 00:33:15,160 --> 00:33:16,980 a PE loader in Leopard. 511 00:33:19,700 --> 00:33:20,740 Anyway, thanks for coming. 512 00:33:20,840 --> 00:33:22,080 And there's a bunch of links at the bottom. 513 00:33:22,200 --> 00:33:25,560 If you get this talk online and you want to read more about some of this stuff, I have 514 00:33:25,560 --> 00:33:29,540 a bunch of blog posts describing each of the binary patching mechanisms in much more detail. 515 00:33:29,960 --> 00:33:31,060 But thanks for listening, guys.