All right. Welcome. This talk is called Function Hooking for OSX and Linux. My name is Joe. Okay. So these slides are on my blog, timetoplead.com. You can go get them right now if you want to follow along. Instead of giving you guys a long intro about who I am and why I'm cool, I just want to give a shout out to SoftSec for getting my back during the conference because this is my first DEF CON and I didn't know anybody. And FreeJumpESP. And I also want to say sorry to the mom and the kids that I elbowed in the face as I was running through Caesars to get to the Apple store for an adapter. My bad. All right. So I'm not a security researcher. I just have a T-shirt that says security. So you can feel free to get on Twitter and call me a script kitty. I'm just Joe D'Amato. All right. So here's the problem. I was running around doing Ruby consulting for a lot of shops and I noticed the common problem amongst everybody was that their Ruby binaries, after running in memory for a long time, kind of look like that. They're very large and slow. And people want the Ruby binaries to be small, thin, and fast. But the problem is that most people are really lazy. They don't want to install custom patches or rebuild a Ruby binary or do anything too complicated. They just want to install some library that will allow them to figure out where the Ruby binary is leaking memory and adjust the Ruby code and then, you know, live happily ever after. So it turns out that in order to do that, there's a set of function hooking techniques you can employ. To make this all possible. So that's what this talk is about. This talk is about three function hooking techniques that I researched and implemented to build a Ruby memory profiler. But since I know there's people in the audience that don't really give a shit about performance analysis and memory profilers, there's also an evil example at the end of what these things can be applied to. So just a quick note before we get rolling. There's a lot of assembly syntax in this. There's a lot of assembly code in this talk. And it'll all be in AT&T syntax. So I can't, sorry, I saw somebody, heard somebody say, oh, I can't read the, I can't read anything other than AT&T. AT&T, my bad. So to actually do this, we need to know a few things, right? So we need to know about the ABI for the system we're working on. The question that we need to start with, though, is what the fuck's an ABI? An ABI is an application binary interface. So what does an ABI actually tell us? It tells us a few things. Some of the more important things for this talk are it tells us about alignment, the alignment of the program stack, data types, stuff like that. It tells us about calling convention, how functions are working, and then it tells us about the arguments are called, where arguments live, how that stuff works, and about object file and library formats. The great thing about the ABIs is that they form a hierarchy, kind of like this hierarchy of beards. These beards are all related in some way. They're all parts of each other. And the ABIs are the exact same way. You have the big system 5 ABI, which is like 200 pages. And then as you read it, it has sections where it says, like, hey, at this point, open up your architecture-specific ABI and read that one. And the great thing about that is that it has a page in it where it says, hey, everything in here is true, but you also need to read everything in the I3D6 ABI. So if you actually want to understand what's going on, you need to read, like, 700 pages. And there's also ABIs for MIPS, ARM, PBC, and Itanium as well. But lucky for us, the x86-64 calling convention on OSX is based on the system 5 AMD64 ABI. So there's some overlap. So we actually get saved a little bit. So instead of actually reading all this stuff, we can just actually accomplish the function hooking that I'm going to describe shortly. So the first thing we need to worry about is alignment. And all that we really care about for alignment in this talk with function hooking is that the end of the argument area before you call a function has to be aligned on a 16-byte boundary. So the way this is usually accomplished is either the compiler will generate code that never misaligns the stack, or you'll see instructions like this sprinkled throughout your code that are just aligning the stack pointer. We also need to care about calling convention. So for calling convention, all we really care about is that function arguments, wow, that color is horrible, function arguments live in registers left to right. So first argument's an RDI, second's an RSI, et cetera, et cetera. But that's only for integer class items. Other stuff gets passed on the stack, similar to the way arguments get passed on I3D6 if there's any I3D6 hackers in the room. Registers are either caller or quality saved. And that's all you pretty much need to know about calling convention to make binary patching work later. The big thing we need to know more about, though, is that we care about two of them. The first that we care about is ELF, the executable linkable format. This is the format that was chosen as the standard format in the System 5 ABI for, oh, really? Oh, hey, Dad. Sorry, let me turn that off real quick. All right. So the ELF ABI was picked as the standard for Unix and Unix-like operating systems. So what does that look like? Well, it kind of looks like this. You have an ELF header, you have a program header table, various sections, text section, RO data section, and then you have another table at the bottom. So the program header table indexes different segments. So segments contain sections. And the section header table at the bottom just indexes the sections. You can use a library like LibELF to help you wander through an ELF object, extracting useful information. The thing to keep in mind is that executables and shared objects each have their own set of data. So the picture that we were just looking at, this guy right here, this set of sections and data exists for each section. So you can use that to library your executable loads. And that will be important later, and you'll see why. So a couple sections that are important to know about. You have the text section where code lives. You have the PLT section. This is stub code that helps to resolve absolute function addresses. The got PLT, this is where absolute function addresses are stored, and these are used by the PLT entries. There's also a couple debugging sections, debug info and GNU debug link. There's two other pretty useful sections. You've got the DIN SIM and the DIN STIR. These two sections are used for dynamic linking. So DIN SIM and DIN STIR are used for dynamic linking. So DIN SIM creates a mapping between exported symbol names and offsets, and DIN STIR stores the actual symbol names themselves. And in addition to those, there's two other sections, the SIM tab and the STIR tab. SIM tab and STIR tab are actually supersets of the information in the DIN SIM and the DIN STIR. They include other things like local variables and other symbols that aren't exported. This is useful for debugging, like if you get a backtrace and you want to know what's going on, you want to know what arguments a function takes, you'll need to know. And there's lots of other sections, like for stack unwinding and exception handling and stuff like that. Okay. So the other binary format we care about is the mock O binary format. This is the binary format used on OSX. Here's a picture of what it looks like. It starts with a header. It consists of a bunch of load commands. Load commands describe the way the binary is going to be laid out in memory. So there's a segment command. The segment command is a pretty important one. It describes how segments are going to be laid out. And much like an ELF, you have segments, and segments contain sections. And we're going to go through some of the sections in a bit. All right. So the man page for DYLD, if you have a Mac box, you can check out the man page. And it has a bunch of APIs for touching mock O objects. So you can iterate through the objects that are mapped into the process. You can play around with them and do stuff, and it's pretty useful. And just like with ELF, each dilib or bundle that gets loaded has its own set of data in the picture that we saw before. So just a couple of sections. These are sort of similar to ELF. We have the text section. This is where code lives. There's the symbol stub section. This is where code lives. This is just a list of jump cue instructions that are useful for runtime dynamic linking, which we're going to go over in a bit. So don't worry. There's going to be a really pretty picture. That will be hard to see. But if you're looking at the slides online, it will make a lot more sense. So there's also the stub helper, stub code that helps to resolve absolute function addresses. And the symbol pointer section, which just holds table entries that are referenced by the stub helpers above. The other important thing to note is that symbol tables do not live in a segment. They have their own load command. The LC SIM tab load command. The LC SIM tab load command command. So this is just basically a structure that tells you, hey, at this spot in memory, there's a bunch of strings and there's a bunch of mappings from these strings to offsets. And there's also a die SIM tab. So the die SIM tab in Mako, which describes how dynamic functions work, that's actually just a list of offsets into the SIM tab. So on ELF you have two separate tables. On Mako you have one table and then you have a set of indexes into that table for things that are exported. All right. So let's look at a couple tools, some useful tools for die SIM testing objects before we actually start writing codes. So you got NM. NM is pretty useful. It comes on OSX and Linux. You run NM on your binary. You get symbol values on the left and symbol names on the right. And down the middle there's information about what type of symbol it is, whether it's a local symbol, global, undefined, whatever. Object dump is also useful. Object dump comes, you can get it on Linux. If you want it for OSX, you have to go find bin utils and build it. I have no idea why it doesn't come standard. So if you just run object dump on, like, say, your Ruby binary, you get offsets on the left, op codes, instructions, and then helpful metadata. There's also read elf. Read elf is obviously for elf objects. This comes on Linux. It outputs a huge amount of information. This is just sort of a small screen shot of what you get. It's really, really useful. And O tool is the complement on OSX that will output useful information about Maco objects. We also need to talk about strip. Not that kind of strip. There's a strip binary that you can run. It will strip out whatever sections you want. You can go through stripping as many sections as you want out of your binary. But the problem is your binary may not actually fit. So you can go through and strip as many sections as you want out of your binary. But the problem is your binary may not actually fit. So you need to obviously leave the text section in. If you strip the text section, you have no codes. And having no codes is no fun. You also need to leave in the dynamic symbol and string tables. Otherwise, dynamic linking will fail. Okay. So we know enough stuff. Let's start hooking some functions. So functions can be called in lots of different ways on x86. There's two really common ways. One common way is calling indirectly via a register, like you see on the top. So in this example, RBX will just hold the address of the function you want to call. And then you can call it directly via a register. And it'll just indirectly call the function. Or the way on the bottom, it looks like, so when you run object dump, you'll see things like you see on the bottom where it'll look like you're actually calling a function directly. But you're not. That's not the actual full address. It's just a 32-bit displacement. So what does that actually mean? Well, we can do an anatomy of a call instruction and get a better idea of what's going on. So this is just object dump output. And as you see on the right, there's a call instruction. And it has, it's calling the function A function, A underscore function. And the address that it lives at is 4363DC. So the way that's calculated is, so in object dump, right, this is the address of this instruction. This is the call opcode E8. And then the four bytes after it are the 32-bit displacement to the function you're trying to call. The way that's actually calculated is you take ‑‑ we're trying to call A function, so that's at that address, 4363DC. That's calculated by taking the address of the next instruction that's going to be executed and adding it to the 32-bit displacement. Remember that X86 is a little endy, and so you need to flip the bytes around before you add it. So hooking A function is actually pretty easy, right? You can just go around overwriting all the displacements that you see and replace the displacements to call your own handler function instead. And your handler function can do whatever you want. In my case, since I'm building a memory profiler, I actually want the Ruby binary to keep working after I'm done overwriting stuff. So I'm going to do my thing, and I'm also going to call the original function so that the Ruby VM can do what it was supposed to do normally. Writing the codes for this are ‑‑ you know, the codes are easy, right? This is pseudo code, so relax. But you just iterate through looking at each byte. Once you find a call instruction, you just do some math, and you can check if the 32-bit displacement matches the function that you're trying to hook, and if it does, you just overwrite that to call your handler function instead. Right? I mean, like, how hard could it possibly be? Well, it turns out that it's actually a little more tricky than just that, so you need to be kind of careful. When you're dealing with the 32-bit displacement, overwriting an existing call with another call, that's fine, right? Like, the stack will already be fine. We were going to call a function anyway, and we're just going to overwrite with a different call, so alignment's fine, the registers are all fine, everything's good to go, everybody's cool. But the problem is we can't redirect to code that's outside of the instruction pointer plus the 32-bit displacement, because we can only put in a 32-bit displacement. So if you're redirecting to code that's far away, you get fucked. The only way to get around this is to scan the address space, searching for a free slot where you can M map a page in to put in a bunch of code to call. So that's how you get around that. You basically just scan the address space, calling M map. Within the address space, in that instruction pointer plus 32-bit displacement window. And that works. But it doesn't work for everything. So if you call a function that's exported by a dynamic library, that works slightly differently. So we need to talk a little bit about how runtime dynamic linking actually works. So I'm going to describe how it works on ELF, but it works almost identically on Mach O. And I'm going to have a pretty picture soon that's going to show the difference between ELF and Mach O. The code is exactly the same, it's just the instructions are rearranged slightly. So here's a call instruction calling a function that has a function that's exported. So if you look at the top, you can see over there on the far right, it says, you know, RB new object at PLT. RB new object is just some function in the Ruby VM. And at PLT, that's object dumps. Object dump is telling us, hey, you're not actually calling that function specifically, you're calling this, like, dynamic linking thing that's going to go find it and actually execute it later. So if we disassemble instructions starting at that address, you see there's three instructions that come up. There's a jump, a push, and a jump. The first jump is referring to an address that exists, that address lives in the got PLT section. That is actually an entry in a table. That entry initially points at the address of the push instruction. Okay. So I'm going to say that again. Because the first time I saw this, I didn't understand why that was. So initially when you first execute your binary, before this function is called for the first time, the initial state of the system is that you have a jump instruction that's jumping to an address stored in a table. The address stored in the table points at the push instruction that comes right after the jump. Okay. Now, the reason why that is is because after you execute that jump and you land right there on that push instruction, you execute the push instruction, which puts an ID on the stack, and then it invokes the runtime dynamic linker. Runtime dynamic linker goes and it runs around and it finds the function you wanted to call using the ID that was pushed. It then goes back to the table and fills in the address of the function in the table. That way, subsequent calls to the same function will just live and land you in that function directly instead of landing you back at that push instruction. Does that make sense? Is everybody cool with that? All right. Word. So hooking that is actually really easy. We can just pretend that we're the dynamic linker and go around poisoning other people's global offset table entries. So we just redirect execution by cruising around, finding other people's PLT entries and just overrunning them with the address that we care about. So how that would work, here's an example. You have this thing. You're calling RB new object. The table entry is poisoned with a bad function. Let's just say the address is OX dead beef. And you land in this other function where you can do whatever you want. But the question that might be running through your mind now, or I hope it's running through your mind is, hey, Joe, in your handler function, you're calling the function you're trying to hook. So shouldn't that be an infinite loop? It turns out that's not because if you remember from earlier, each dynamic object that you load into your process has its own set of functions. So if you remember from earlier, each dynamic object has its own set of data. So each dynamic object has its own set of PLT entries and its own global offset table. So as long as you poison everybody else's except for the got PLT section of the library that includes your bad code, as long as you don't poison your library itself, then you'll be okay. Dynamic linking will happen normally for you. You just have to overwrite everybody else's shit. Okay. So what about mock O? We just described how it works in ELF. What about mock O? Well, it's not quite like comparing apples to oranges. It's actually a lot ‑‑ it's actually really similar. This is the graphic that I was talking about that's, like, super sexy that I spent a lot of time drawing. ELF is on the top, obviously. Mock O is on the bottom. The instructions ‑‑ the actual assembly instructions that are generated are exactly the same. They're just rearranged, right? So on ELF, you have a bunch of ‑‑ you have a jump instruction followed by invoking the dynamic linker as we saw on the other slides. But on mock O, they just put all the jump instructions together and then all the dynamic linking invocation functions together. So they just kind of rearrange stuff. But it's exactly the same thing. It works the same way. Okay. So we know how to hook two types of functions. But there's a third type that also matters. Inline functions. And this is actually really fucking tricky. We can't hook ‑‑ there's a function that I needed to hook to build this memory profiler called add free list. But the problem with trying to hook add free list is that it's marked as inline, which means that the compiler has the option of inserting the instructions for this function directly into whoever is calling this function. So if that happens, you won't actually see any explicit call instructions in your binary. So what do we do now? Are we jacked? Or is there a way around this? Well, it turns out that if you look closely at this generated code, you should notice something. You notice there's this identifier called free list and free list is getting updated to point to the new head of the free list as objects are recycled. Notice that free list isn't defined 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 It lives at some static address. Add free list updates the free list by inserting something at the head of the free list. Why not just search the binary for all move instructions that target free list and just overwrite all of those? And it turns out that that might actually work, but there are a few problems. The system isn't actually ready to make a call instruction at that point. What does that mean, not ready? Remember before we talked about the ABI and how the ABI talks about alignment and calling convention? Well, it's not quite ready to call a function because there's a bunch of move instructions, if we just implement a call instruction, there's no guarantee that the stack is aligned or arguments are on the right registers. So what do we need to do? Well, it turns out that we can actually use another type of instructions instead. We can actually just use a jump. We can insert a jump instruction, transfer execution to an assembly stub that we generate at run time and recreate the instruction we overwrote, set the system up. up to call a function and then do whatever we're going to do, whether it's good or bad or whatever, and then jump back and resume execution as if nothing happened. And this is sort of like the crusty third hook that you need to pull this off. So there's a checklist of things we need to do. We need to save and restore any registers. We need to align the stack. We need to regenerate the instruction that we're overwriting. We need to arrange for any arguments that we care about to end up being put in the right register so they get passed to the function. We need to invoke our code that does whatever it's going to do. And then we need to resume execution as if nothing happened. So here's an example of an instruction that is updating the free list that we're going to overwrite. So this is just saying, hey, you know, I'm going to take RBX and I'm going to write it to the top of the free list. So we can't put a call instruction there because as we said, the system is not ready to make a function call. The stack isn't aligned. Arguments aren't in the right place. Everything is all fucked up. So what we can do instead is we can just insert a jump instruction. So it turns out the move instruction is seven bytes. The jump instruction including the offset is only five bytes. So since we can't grow or shrink the binary, we have to insert two nop pads just to make everything work out. So this address right there, that's just the address of the assembly stub we're going to transfer execution to. And when the assembly stub finishes, we want to return back to this nop and then just fall through and continue executing as if nothing happened. So here's a shortened assembly stub that actually does this. This is an assembly stub that when I ran my code, this was generated at runtime. It has to be generated at runtime because this first instruction up here at the top, this is getting overwritten. And who knows what it's going to get ‑‑ who knows what it's going to get ‑‑ who knows what that instruction is going to look like when your Ruby binary is actually running in memory. Everybody compiles their own Ruby. And so this could ‑‑ instead of using RBX, who knows, maybe you're using RCX on your Ruby binary. So you just search for anything that targets a free list, you overwrite it with a jump, and then you have to regenerate what you overwrote on this little trampoline here. This is actually shortened. The real thing is way, way longer and way uglier. But I'm going to provide a link to the code later. That way you can actually check it out if you want to see what the deal is. So yeah, we need to recreate the instruction we overwrote. We need to save or register past our argument. So I'm just going to save RDI. That's where the first argument lives, if you remember from earlier in the talk. We're going to move the free list into RDI because for a memory profiler, I want to know who just got freed. That way I can track information about them. I'm going to save RBX because I'm going to fill it with a value in a minute. I'll align the stack to comply with the ABI. And then I'll move the address of a handler function into RBX. That's why I saved RBX a few instructions earlier. I'll call my handler function. I'm going to call my handler function. I'm going to call my handler function indirectly via RBX. And then the handler function will just do whatever, something good or something bad, you know, wherever you want to do. You do a bunch of cleanup, and then you just jump back to wherever you overwrote the instruction originally. And the crazy thing about all this hackery is that when you're done, it actually works. You can actually use this. Like, if you have Ruby and you care, you can gem install memprof and you can actually use this thing. And all the code that hooks these three types of functions exist in this Ruby library that I built called memprof. So you can go on GitHub. You can read the code. And it's real. And it's actually pretty powerful. These three things can be used to build lots and lots of really interesting tools. So just to give you, like, a quick rundown of, like, some of the cool things you can do with this. So, you know, we can do stuff about memory, right? Like, I can look ‑‑ I can write this Ruby code. I can say, hey, include my memory profiler, start profiling, run a bunch of code and give me stats. And I'll get stats like this. Like, I can find out on the exact file and line number where objects are being allocated. So if I have a memory leak, I can look and I can say, hey, you know, on file 1.RB, I can say, hey, you know, on file 1.RB, on line 25, I'm creating a million objects. So I can know exactly where things are leaking, which is pretty fucking sick. So the text‑based output is cool, but it's really painful to grok. So my roommate Amon built a web UI for this thing. So you can, like, actually traverse stuff and you can learn all kinds of crazy information. You can find, you know, who's holding references to what other objects, where you're leaking memory. You can look at, like, you know, the inheritance tree of objects. You can look at the subclasses of string and rails. You can explore the socket hierarchy. You can look at, like, you know, all kinds of stuff. You can find, like, massive arrays. You can ‑‑ and since in Ruby, all Ruby code is actually just stored in the heap, I can actually just go and regenerate Ruby code from, you know, using these function hooks, right, because I can just grab these things when they're getting created and I can just reverse them back into Ruby code, which is pretty cool, too. You can go and look through Ruby standard library and you can say, oh, look at these, like, really bizarre functions that exist in Ruby. You can see, you know, if you have sockets open to any web services, you can see what you're connected to on what port. And that's all pretty cool. Request information, too, right? Like, you can find out, you know, how long requests take, you know, which rails control you hit, request information. You get crazy stuff, too. You can track MySQL queries, right? Since I can hook into anything, I can track anything I want. I can track, like, MySQL queries, how many I make, how long they take. I can track the garbage collector, how many calls there are to the GC, how long these calls take. You know, I can track how many objects are created in general, which is pretty useful as well. And that's all cool, but what about some evil things we can do instead, right? Performance stuff is cool, but maybe you don't care about performance. So you can build a library that does something evil, but you'll need a social engineering aspect of it, because you need some way to get people to actually use your library. In a few slides, we'll talk about how to actually get around this so you don't actually need to social engineer. But for right now, we can build a library that will do something bad, but we need to just convince people to use it. So how would we do that? Well, evil lives. I built an evil version of memprof. The branch on GitHub is just DNW. That stands for do not want. If you try to build it, it will fail. It will fail. If you try to build it, it will fail. I put a bug in the make file. That way, nobody can accidentally go screw themselves by building this. And the way that I would social engineer somebody to use this is I would say, like, hey, bro, this makes Ruby faster. And it does make Ruby faster, because what it does is it hooks the read system call. It looks for a magic cookie. In this case, the magic cookie is just my name, Joe. And then it just disables the garbage collector. The result is Ruby is fast. But it is now a time bomb, right? So, eventually, it'll eat all the RAM on your system and your system will melt down. And, you know, if you really want to drill it in and hey, it makes Ruby faster, like, look, here's a bullshit benchmark, but just don't tell them it's bullshit, but look, here's normal Ruby and normal Ruby takes seven and a half seconds to do the stupid benchmark. You know, 26 requests per second. Then after you use this thing that makes Ruby faster, suddenly you shave off a second off the time and you're now up to 30 requests per second. So you show this to a Ruby developer and they're like, dude, I need this right now. So here's So anyway, this example is stupid, but the whole point is just to prove a concept. You can do anything, right? Like, you could hook read and write and just phone home with all the data coming in or leaving the application. You can fork a back door when you see a specific cookie. You can do whatever you want. But the shitty thing is that we have to social engineer, and that's kind of a problem. This guy also has a problem. Unrelated to social engineering. So the problem with social engineering, at least from my perspective, is that it's painful. And I don't want to have to social engineer things if I don't have to, like, if there's a way around it. So it would be cool if there was a way to inject these evil libraries into running processes without having to convince people to download my thing and look at some bullshit benchmark. It turns out that there is a way of doing this. There is a project written by this guy called Sean Clowes or Clowes or something. It's called inject SO. It injects libraries into running processes using Ptrace. This hack is actually super, super clever. To borrow a very awesome Internet meme, fucking magnets, how do they work? I want to know how inject SO works because it's pretty cool. It's actually pretty simple. It just uses Ptrace. Ptrace allows you to view and modify the register set and address space of another process. Any permissions that are set on the memory in that process are ignored. So even if a page is marked as read only, you can still write all over it or do whatever you want. So the way inject SO works is it just attaches to a target process using Ptrace. It saves a copy of a small piece of the program stack. It saves a copy of the register set. And then it creates a fake stack frame with a save return address of zero. You then set the register set to point to DL open. So you have, you know, the instruction pointer pointing to the address of DL open. You set up the arguments so you have the path to the shared object, the mode stored in RSI. And you just let it rip. Okay. So you wait and eventually it will segfault because once DL open is done, it will load in this library that you want it to load in and then it will segfault because the save return address is zero. When the segfault happens, you as the tracing process, you as the tracer of the process will get a signal. So the child process will get suspended. You'll be awoken and you'll be allowed to do stuff to it. So you just restore the stack, restore the register set and just let the world continue execute, you know, let the world continue on its merry way as if nothing bad happened. And this actually works. It's pretty cool. But there's a couple downsides. There's downsides to both aspects of the ptrace method and the evil DSO method. Remote allocating memory is a pain in the ass. It's doable, but it's just really painful to do. Generating segfaults and running processes might be bad. Perhaps the system administrator has some alert thing set up so that if a segfault happens, then they get an email. I have that set up on my boxes, so if I get a segfault, I know that something bad happened. And binary patching in general is hard to do, like the stuff that we just talked about earlier. Those things are complicated. And it's possible to do with ptrace, but like adding another layer of difficult in between you and actually doing what you want to do is no fun. So binary patching is hard, but doing it with ptrace, it just makes it harder. The problem with the evil DSO, though, is getting the user to use your library might be hard. And already running processes will need to be killed and restarted, which also sucks. And you need to poison the app each time it's started. And in general, binary patching is hard. But what if there was a way to build a binary patching system? Like bootstrap the process? What if we can just combine both methods? Like we can just use inject SO to load the evil DSO, and then the evil DSO will just take it from there and do a bunch of, you know, do all the crazy binary patching stuff. So in order to do that, we needed a 64-bit version of inject SO. But it turns out that the inject SO that I talked about by that dude Sean is only 32-bit. So I sort of, I was getting ready to write a 64-bit port, and then I searched the internet a bit and I found that this guy Stealth ported inject SO to 64-bit boxes. This is his, like, blog post releasing the source code. I did a bunch of trivial cleanup on it because it was, like, a little bit old and kind of ‑‑ it was suffering from some bit rot. So I did some minor cleanup and I put the code on the Git Hubs. So if you want to copy that, you can go there and check it out. I tested it on a 64-bit Ubuntu VM, and it actually works. So I can use inject SO and I can inject my stupid thing that looks for the string Joe and disables the garbage collector, and then it DOSs my box. So it's pretty cool. Because, like, I had this thing. I had this web server running and I just used inject SO to infect it and then all hell broke loose after that. I was going to try to demo that, but then I wouldn't be able to give the rest of the talk. So I can't demo that. So anyway, using those two things, I don't know. I think it's kind of cool. I don't know if it's a jackpot or not, but it's definitely pretty sweet. So we talked about some attack mechanisms. So what are some defense mechanisms to protect against this sort of thing? Like I said earlier, I'm not a security researcher, so I don't know, like, you know, all the up‑to‑date, like, amazing defense mechanisms that are out there, but what I do know for sure is that everything's a trade‑off. So there's a trade‑off between defense mechanisms that are difficult to implement, difficult to understand, versus just, like, you know, standard run‑of‑the‑mill, like Ubuntu, like out of the box, right? So I went through some of the defense mechanisms that I know about and discussed, like, you know, really briefly, like, why they're good or why they're bad or whatever. So I'm sure you guys know about the NX bit, the no‑execute bit. That's cool, but if we just load our DSO and our DSO can just call mprotect and say, hey, this page that was, you know, not execute, just make it execute. So that doesn't really help us. You can strip out debug information, but most people just use pre‑built binaries. Most people just use whatever comes with their distribution of Linux. So I can just, like, look ‑‑ I can just get that VM, I can download the debug symbols and I can figure out all the offsets myself. You can statically link everything if you want and then you still have the problem of getting overwritten otherwise, but you're not ‑‑ you don't have to worry about your global offset tables getting poisoned. The problem with that is your binaries get pretty fat. You can put all the text ‑‑ all text code in ROM. That will probably work. I don't know. I mean, perhaps there's, like, some really crazy legacy C code that if it's put in read‑only memory will actually die, but in theory that's supposed to work. So that would actually work. You can also not load DSOs at runtime. The problem with that, then, is you sacrifice any and all plug‑in architectures that are out there. You can disable P trace, but P trace is pretty sweet. If you disable P trace, you lose GDB and you lose S trace, and both of those are really fucking useful. You can also check proc PID maps. That works. So if you watch proc PID maps all day long and somebody injects a library in, that's It will appear on proc PID maps and you can see, like, hey, this, like, you know, whatever, evil.SO is now running in my process. So that would actually work. It's just kind of painful to do that. So anyway, sort of looking forward, like, on more experimental stuff, I think my ‑‑ for me personally, my future research is going to be sort of geared toward exploring alternative binary formats and, like, other crazy things you can do. So just to recap. We talked about three different binary ‑‑ binary patches. Matching techniques. These techniques leave the original binary in working fashion after they're done. So kind of like teeth whitening, right? Like you can still eat food after you're done, just looks a little different. Same deal. And there's three things that matter, right? There's alignment, calling convention, and object file and library formats. And before I end this, I actually had a couple extra slides of something really weird I found that I wanted to share in case I had some extra time. So look, an alien baby. . And I saw this really crazy post to a mailing list. The title of the post was interesting behavior on OSX. This was written by a guy named Steven Edwards, November 29, 2007. This is the link to the actual mailing list post that he wrote. It turns out that Leopard actually has a PE loader, which is really weird. So if you execute this code on a Leopard system and you try to load a Win32 executable, you get this output. So for whatever reason. There's a loader for Win32 binaries on OSX. So even if you had all the Windows DLLs in place in the right path so everything could be found, it wouldn't actually work anyway, right, because all the APIs are different. But it's still sort of curious. Like, why the hell is there a PE loader in OSX? So I don't know if anybody out there knows the answer to that question. But definitely something to check out if you guys are looking to play around with something weird. I think this is now disabled. I tried this recently on Snow Leopard and it didn't work. But I remember trying this a few months ago on Snow Leopard and I thought it did work. So I'm not really sure what's going on with that, but it's pretty bizarre that there's a PE loader in Leopard. Anyway, thanks for coming. And there's a bunch of links at the bottom. If you get this talk online and you want to read more about some of this stuff, I have a bunch of blog posts describing each of the binary patching mechanisms in much more detail. But thanks for listening, guys.