So what I'm going to be talking about here is basically some not even really fancy pants thing. It's more of an artistic expression, if anything else, if you really want to call it that. It's really just a fun, clever technique to be an asshole to people. So now I'll introduce myself if this shit is going on the screen. Is it there? I'm totally sending a signal. Maybe I'm not. Hang on. Let's try this. Yeah. Yes, no. Nothing. There we go. All right. Okay. So my name is Frank2. It's not actually Frank Squared. Some asshole spammer on Twitter has Frank2, and I've been trying to ‑‑ I tried to socially engineer them to get rid of the account because, hey, it's technically a spam account and I can get rid of it. But apparently if you're honest with them, they won't do it because I was requesting to get a name taken away or something like that, even though it was a spam account, but, you know, fuck them, fuck Twitter. A lot of people recognize me by my hat. I'm with DC949 and DC310, and I also work for Rapid7. I can't put that there because there's a lot of F bombs here. My manager did not want me to do that. So I gave this talk at ThoughtCon. But I was given only about, I want to say, 15 minutes. 15 minutes or so to speak. And this is kind of a complex topic. So it was basically a stand‑up performance that just so happened to feature reverse engineering and all sorts of funny stuff like that. And there were basically two camps. There was this one guy on Twitter who said, I have no idea what the fuck Frank2 is talking about, but it's awesome. And then there was this dude on Reddit who saw my slides and he was upset with all the weed references. And he was, you know, angered that such a serious thing is going on. This topic wasn't really covered so seriously. And the bottom line was, you know, he wanted more content and less bullshit. So I kind of want to focus on this quote. Nothing personal to the dude from Reddit if he just so happens to be in this audience. Because he had a fair criticism. Because the talk didn't have a lot of content, it was bullshit. Because it was geared to be, you know, a stand‑up routine for hackers, basically. But at the same time, I kind of feel like people don't really present their information in a very entertaining way. Even though it is. It's always very cut and dry, here is EIP, here is ESP, here is how you perform exploit, here is my O day, now clap. And a lot of people ‑‑ it just ‑‑ it just seems like ‑‑ thank you, I appreciate that. And it just seems like to me that this stuff is just ‑‑ there's so much entertainment at this convention alone. So it just feels like a lot of people should be presenting it in an entertaining fashion. And that's what I'm trying to do. So this is basically what you're going to see. Extremely referential shit to computer science. And absolutely juvenile humor. So I hope you'll appreciate it. That's exactly what I'm going to try for here. If you came for a serious talk, I'm very sorry. So if you look at the ‑‑ this is basically a very scientific analysis of the previous talk that I gave. If you compare the science to the medication, quote, unquote, that I took while performing, not performing. Not performing the talk but writing it, you'll see there was a lot more medication involved. But don't worry because there's a little bit more science now. So if you were ‑‑ well, you know, technically that counted as medication. So I'm not sure. I would probably have to increase the bar a little bit. Anyway. So here's the content. A while back my friend Merlin right here who is sitting in the front stage wrote this really awesome IRC Python repo bot that basically takes one lines ‑‑ it was a really awesome exercise in teaching functional programming, and it's a whole lot of fun to mess with. You don't even really have to ‑‑ or it's ‑‑ you can just add function after function after function. This is actually a combination of all sorts of different functions here. And all it really is is just me on IRC doing a rainbow sine wave that says just wagging my dick. And it's one of the silliest things you can do, but, I mean, look at that. It's a really awesome rainbow sine wave of just wagging my dick. And it's just beautiful. And for some reason I thought, hey, what if I applied this to binary obfuscation? I have no idea why I came to that conclusion, but it turned out awesome nonetheless. So I'm going to cover a few basics here before continuing. Just a second. I need some water. So F of X formulas are actually, you know, very simple in their concept. I mean, you have ‑‑ they work exactly like regular functions. You got your X, and then you got your input, and you get X times 7, so you get whatever X was times 7 equals your value. You know, in Python you can make a lambda function. Lambda X, X times 7. If you want to do Java, I'm sorry, I really don't think you should do that. And you know, mathematical functions can get a whole lot more complicated than just this. I mean, they can do ‑‑ there's like integrals and shit. How do they work? You know? So there's all sorts of shit you can do there, and they can get a whole lot more complicated, but this is ‑‑ it's a very basic concept. As far as assembly is concerned, you have your jump and cross. Call instructions. But when you see them in a debugger specifically, they don't ‑‑ they look like ‑‑ it's like jump 0040100, at least that's what it looks like in the debugger, but if you look at the actual instruction, it's an offset that goes from, you know, 5 to 10 or whatever the hell you want it to be. Call is the exact same way, but it pushes a whole bunch of stuff onto your stack. Except for the fact when you have, like, jump EAX or jump dereference EAX or things like that, that is actually a specific call to an address, and usually you don't have the ability to determine what's in EAX just specifically from doing disassembly or anything like that. This will come up later. It can be an issue, so you have to be aware of it. So let's talk about jump short for a bit. So jump short ‑‑ see, now you're starting to figure it out. Thank you. Okay. So jump short is a special jump instruction in the X86 architecture that allows you to have a single byte offset instead of a four byte offset, which reduces a lot of space. This is going to be significant again later on because of all the manipulation that's going to be happening with the individual instructions. So it's important to keep in mind that, you know, a jump short only has a range of about 256 signed. I can't even remember how big the number is for a four byte number, and don't even get me started on 64 bit. There's no such thing as call short, and jump short and call short, who knows? So here's some computer science witchcraft. In the middle of making these slides, I realized that, you know, you can actually define the assembly as null space. If you look in between the individual instructions, it's actually ‑‑ every ‑‑ every instruction is executed one after another. And you can technically interpret this as an unconditional jump to the next instruction. So if we assume that after every instruction, there's an unconditional jump so long as the control flow of the instruction doesn't necessarily change, then we can assume that there is an unconditional jump between every instruction. So if you look at this ‑‑ at this assembly, for example. You'll see that, you know, we just have a very basic thing. I encourage you to decode the ASCII, by the way. So this is ‑‑ basically this is just a regular set of instructions. The jump zeros you see here in the middle are essentially the unconditional jumps you don't really see. They go from the instruction one after another. So that's essentially what's going on there. So it's therefore possible to place every single assembly instruction in anywhere you want, but only if every instruction is followed by an unconditional jump, because if you're transposing the assembly and you need to maintain the same code as before, then you're going to have to attach an unconditional jump to every single instruction. So let's see here. Okay. As far as one‑dimensional arrays are concerned, because we're going to be drawing on a grid, you can technically interpret a one‑dimensional grid as a two‑dimensional ‑‑ or a one‑dimensional array, I'm sorry. A one‑dimensional array is a two‑dimensional array. It just requires a little math. You have to multiply by the row or something like that. I can't remember exactly, but it's not that hard to figure out. So this gives us the ability to interpret an array as an XY grid. So if we have the assembly instructions that have unconditional jumps that can be linked, one to another, we can technically draw the instructions on an XY grid and then apply an unconditional jump after every single instruction in order to maintain the same code flow. And this is pretty fucking cool. So in order to actually accomplish this, you have to follow a few steps. First you need to disassemble every instruction, because obviously you're trying to figure out what your code is. Then you allocate space and memory a whole lot larger, or at least ‑‑ at least I preferred like ten times or something like that than the original code. So this way ‑‑ this way you have a whole lot of room to mess with, because this gives you the ability to do all sorts of different shapes and sizes and whatever the hell you want. It gives you a lot more room. So ‑‑ and for every instruction, you want to turn the FX value. And once you do that, you place the instruction at the corresponding XY location. And then you join the instruction with an unconditional jump. Then you mark the memory as executable and you run that. But unfortunately, there are a lot of issues that you're going to contend with. Like the 6,000‑year‑old earth. So like gravity, it only works in theory. In practice, it's not exactly going to work, because there's all sorts of different shit to deal with, because x86 is just a cluster fuck. All your jump instructions, fucked. Your call instructions are fucked. Self‑referential code is fucked. Any code that relies on iteration individually manipulate the code you're doing, you don't even think about it. So let's start with jump instructions. Since jump instructions are offsets, when they wind up placed in a different location than they were originally, you're not going to point to wherever you thought they were. Because again, like I said earlier, they're offsets. So if you have a jump instruction right here and it says, hey, I'm pointing five bytes over here. That's a jump short. And then you move it over here. It says, hey, I'm pointing five ‑‑ hey, wait, where did it go? So you're going to have an issue there. So short jumps are in a similar situation. You may as well just completely ignore short jumps when you're dealing with this, unless ‑‑ it's just not even worth it, because there's going to be a long distance, because of the ‑‑ especially if you're doing a ‑‑ if you're dealing with a single dimensional array, there's going to be a lot of issues with ‑‑ there's going to be a lot of issues with the jump shorts, because it's going to be a lot longer than you think it is. Short jumps are easily fixed. Because all you really have to do is convert them to long jumps, but then after you convert them to long jumps, you have to figure out what the new offsets are. Dealing with register‑based jumps, like I said earlier, that's just going to be a whole pain in the ass, because automatically determining where every register is is going to rely on a lot of compiler ‑‑ knowledge of compiler theory and determining whether or not ‑‑ or determining what the register is. Just from reading the code a little bit. It's just not worth it. It can be a function pointer or a class function pointer or something like that. Unless you're going to want to do a whole bunch of extra work, it's really not worth it. You may as well just ignore it. F of X formulas, when you actually write them, it's not really as elegant as it is on paper. If you want to ‑‑ if you want to do it right, or at least, have it go to the right place or the best, do it well, it's going to require a lot of work because you'll have to do function pointers for your constants. No, not function pointers for your constants, but you'll have to deal with the voodoo of C and C++ in order to figure out your class pointers and things like that. Why do I keep saying class pointers? So here's how you deal with it. At disassembly you want to convert all your jump shorts to jump pants before storing them away. Like I said earlier, it will make things a whole lot easier for you because you're just dealing with the offsets and it's very simple. The actual offsets are going to be a huge pain in the ass, though. Every instruction that you detect that has an offset, it needs to be recalculated. This means that you need to keep track of both the instructions and where they're going to be placed as well as the targets that they're going to be. So you have to track the instruction that they're pointing at as well as the individual instruction. And they would probably take me way too long to actually explain how this works. And I don't know how to do it verbosely. So the source code that was supplied with the DEF CON CD is basically an implementation that I'm going to demonstrate on stage. So I would suggest that you look at that to figure out how I did it there. After all the instructions are replaced, you're going to want to replace the old offsets with the new offsets. Duh. Assuming you didn't fuck up the offsets. It's that simple. So now that we have all the caveats out of the way, let's go with a more realistic implementation. It's something like this. So first you disassemble the instructions. Then you prepare your buffer. Then you initialize the formula constants that you have. And then after that, you're going to want to iterate over the F of X values, determine the data pointers. And you're going to be wanting to track all the potentially messed up instructions that you're going to be disassembling. Then after that, once you've dealt with all that, write the instructions to the corresponding pointers that you've created. And then you're going to want to go back to the old offsets. Then you're going to want to repair all your conditional jumps, mark the new section of memory as executable. Then you run it. And assuming that everything worked out fine and everything is pointing at the right place, weird things happen when this gets all messed up. So, yeah. It's just a lot of messed up instructions and bad pointers, and you're jumping into really weird locations in memory. So once it all works, you run it. It's pretty awesome, isn't it? So why does this really matter? Is this really just a trick? Is this a trick and pony show, or is it doing anything? Like, does this actually do anything useful, or does it just look pretty? So the actual utility of this is that once you isolate the assembly instructions and the ‑‑ once you actually isolate the assembly instructions, we can put the individual assembly instructions anywhere we want, simply based on a formula rather than having to write an entire function that will likely be fingerprinted by AV or something like that. And just be able to ‑‑ whole series of mathematical functions that will just draw a whole bunch of ‑‑ will draw a whole bunch of assembly onto the code ‑‑ onto the code into memory. So in order to obfuscate the clarity of the code path, all you really need to do is make a bunch of functions, maybe select them at random, maybe select them, you know, iteratively or something like that, or just, you know, determine based on, you know, who you're attacking or something like that, generate a random mathematical thing, and you can do all sorts of stuff. If you want to perform various polymorphic techniques, you can also use this mathematical form to do that as well. So instead of having to write code that manipulates ‑‑ like I was saying, that manipulates your code in a specific way, every time, you can write just completely random functions that decide to ‑‑ yeah, you can write a series of functions and then just have those functions specifically ‑‑ yup, the alcohol's really hitting me now! Wow! Okay. The audience commands it. So instead of writing code, I already said all that. And remember that anti‑reversing isn't specifically about just, you know, finding the really cool anti‑debug techniques. It's really not about doing all of that. I mean, sure, you could do a really awesome thing, break out of IDA, spawn last measure everywhere, and completely fuck with the reverse engineer, but at the end of the day, you know, it's really about just being a complete dick to the reverser. Because if you're a complete dick to the reverser, he's just going to get pissed off and say, you know, fuck this malware. And just walk away. And meanwhile you'll be able to sell all your bots to the Russian business network, no problem. Because you're just pissing off every reverse engineer. They don't know how to Google for shit. They don't know what the hell you're doing because your code is all fucking messed up. And it's just ‑‑ you know, they're just getting pissed off. It's a game of psychology, really. So if you're extremely creative and can figure all sorts of just different things to do out, then you're going to be a really awesome anti‑reverser. Because it's really about just completely fucking with people. That's all anti‑reversing really is. Sure, it's ‑‑ you know, it's ‑‑ there's a technical problem. There's a technical aspect to it as well. But you know, you're really just trying to get them away from your code. That's all it really is. So here's what I'm going to do. I'm going to take the obfuscation function and I'm going to obfuscate it. Then I'm going to take the obfuscated version of the obfuscated function and I'm going to obfuscate the obfuscator again. So let's pull up the code. All right. So here is math troll.com. It essentially contains my sine wave example. You can see ‑‑ let's see. Obfuscate by formula. So here is obfuscate by formula. And you can see the assembly instructions here. They're all in working order and they're doing their thing. If you look at the code, it won't help you. This is just the assembly instructions. Everything that's been compiled. And you'll notice that I use C++. I'm very sorry. I try to avoid C++ as much as possible. So let's see here. Where did I put that break point? Right here is where the actual ‑‑ this is where the obfuscated function is. So once it gets here, it actually ‑‑ this is going to be the obfuscated function now. So as you can see, the jump instructions have been applied. There's a whole bunch of just different stuff in this buffer. It makes it look a whole lot more obfuscated than it really is. And it goes to each and every individual instruction just perfectly fine. And it's ‑‑ I'll show you the shape in just a moment. This is just arbitrarily ‑‑ I can just hold F8 the whole time or actually I can just run it and it will still be fine. There it goes all the way to the end. So the code is still fine and it's now ‑‑ and there's a bunch of different jump instructions there. So it ‑‑ it looks messed up. But I'll explain why it's not that messed up in a moment. Here's a ‑‑ oh, oops, wrong direction. Here's a visual representation of what the stack looks like. Every time this happens, I generate a random sine wave formula that will arbitrarily, you know, make a bunch of different shapes. But this is the coolest one of the batch. So I decided to put this up. I believe the code starts about ‑‑ can you see my mouse? Yeah. I think it starts about here. I can't remember exactly. But it wraps around and keeps going. And this is basically what you're seeing is the actual code flow. So it goes all the way ‑‑ you know, it just does a cool sine wave. But I mean, it's not just sine waves. You can also do spirals, too. So you can ‑‑ and these are really the only two formulas that I included on the source. Like I said, you can do a whole lot of other creative stuff that you want to. And this is ‑‑ what this is essentially is just a diff. From the initial buffer and the finished buffer, and it actually just looks exactly like that. And it looks pretty cool to me. But you know, the issue is that you're using unconditional jumps. So ‑‑ and that's really bad. Because if you use unconditional jumps, like I said, you're just ‑‑ the code flow is still exactly the same. Because the unconditional jumps were there to begin with. So if you have the unconditional jumps there, all you really need to do is go. Go from the entry point to the end and get rid of the jump instructions. All you have to do is read those instructions and boom, there you go. You got your code. And it's completely unobfuscated. So how do you deal with this? Well, the inverse of an unconditional jump is a conditional jump, which goes in two directions, which makes it more awesome. In fact, you could say it's 50% more awesome. So if we ‑‑ but that provides a very interesting dichotomy. Because ‑‑. Well, if we need conditional jumps but we also need unconditional jumps, what the fuck do we do? So that's what opaque predicates are for. So for those who don't know, an opaque predicate is essentially a Boolean statement that always evaluates to a specific version no matter what. So let's consider the null space expansion I talked about earlier. If you have a set of instructions and they have unconditional jumps between every instruction, it also follows that ‑‑. Okay. If you have a set of instructions which don't have a direct effect on the assembly, can be applied. For example, as long as you write very specific instructions that don't modify the underlying assembly of what you're trying to obfuscate, like ‑‑ if you're trying not to mess with registers, then as long as you maintain the state of every assembly instruction, then you're good to go. And this is pretty awesome, too. So you can consider every assembly instruction to be able to be wrapped like this. You have your preamble, your assembly data, and then your postscript. And the preamble is essentially what comes before the assembly instruction, and the postscript is obviously what comes after it. So the preamble section is typically used for two things, or it can be used for two things. You can repair the after effects of the previous preamble of the past opaque predicate. The anti‑debug code chunks can go in there, too. But it ‑‑ the preamble is very limited. Because you can't really do that much. The postscript you can do a whole lot more with, because it's actually going into the next instruction, so you don't have to really worry about it that much. So you can do a whole bunch of other stuff, as well. So what can this section be used for? You can put opaque predicates and obfuscated jumps to link to the very next section. Anti‑debug, you can also stick in here, too. General code flow obfuscation, that sort of stuff. Encryption. One of the things that I think would be ‑‑ is really cool is that actually I'm working on it right now. Is being able to encrypt and decrypt every single instruction so that as every instruction is executed, it decrypts the next section and decrypts the next section and decrypts the next section. Oh, by the way, here's a bunch of anti‑debug in the preamble. I guess you don't get the rest of the code. And there's all sorts of other stuff you can do with that, too. So here's a great example of this. In green, we have our preamble data. And a very generic call to is debugger present, which, you know, all it does is once it figures out that there's a debugger, it gives you the finger and jumps to some random code section that will probably spawn last measure or something awesome like that. Who knows? And we have a very simple opaque predicate here at the bottom. You maintain the value of EAX in the post script of the top instruction. Zor it so that JZ thinks, oh, well, I obviously can go either left or right. I think I'll go right because it's zero. And then you pop EAX and you get your EAX back and the next instruction isn't modified at all. Then you got the next instruction, et cetera, et cetera, et cetera. So this introduces a whole lot more issues because what you're going to wind up running into is it's going to be really hard to determine which instructions affect which. And there's all sorts of other nonsense. If you have shmoo balls, I highly suggest you throw them at me because I'm going to be that guy because I didn't finish all of that. So okay, good, no one has shmoo balls, I'm safe. Our F of X formulas also don't necessarily need to be iteratively run. You don't have to do just F1, F2. If you're clever, you can figure out specifically how many instructions you're going to have and then. The instruction is just F of 27, F of 54, F of 9, and that will essentially place your instructions in random places. And when you do that, depending on how you wrote your code, that will allow you to ‑‑ that will essentially allow you to determine ‑‑ man, I shouldn't have had that last shot. Wow. All right. All right. You ready? No. For that long a pause, I kind of deserve it. So you can essentially just determine it iteratively and it will still link your instructions. They'll just be completely randomly placed. So if your code ‑‑ if your code ‑‑ if your code ‑‑ if your code ‑‑ if your code is generated from a predictable formula, then it also follows that the entry point is predictable. So you can take this to one level more before you actually wind up getting to your code. You can essentially obfuscate the entry point in some way or another, do a whole like 300 assembly instructions that just gets the entry point. Oh, by the way, here's a little bit of anti‑debug. Oh, it manipulates the entry point just so much that you can't even run the code. So there's all sorts of stuff you can do there, too. So there are various drawbacks to this as well. So you're ‑‑ yeah, there are various issues that you're going to be still running into this even though this is pretty cool. So this technique assumes you have simply compiled code. So if you've basically compiled it with either GCC or, God forbid, VC++ ‑‑ well, VC++ is actually pretty cool for a few reasons, but, you know, actually all compilers suck. What am I talking about? Anyway. So if ‑‑ this really assumes that you have simply compiled code because as it's disassembling the instructions, it's following a very simple path. So it's not going to assume that there's all sorts of really weird tricks that are going on. For example, if you have an opaque predicate that goes off into la la land, then it's going to fuck up and add those instructions and your code is going to be all messed up. So if you're trying to obfuscate somewhat obfuscated or manipulated assembly, it's not going to work out that well for you. There's also a massive memory footprint because as ‑‑ let me go back to the image to show you ‑‑ there's a lot of blue space here and there's not a lot of red space. All the red space is the code, all the blue space is ‑‑ sure, there are things you can do to make it a whole lot more efficient, but there's obviously a whole lot of extra space there that's going to be an issue for you. So it's going to have a massive memory footprint if you want to do it right or, yeah, if you want to isolate out the instructions. Yeah, you're dealing with a gigantic data set. So it gets significantly larger when you obfuscate more than just one function. So if you have some really awesome packer you downloaded from mal code or something like that, well, I really hope it's efficient for you. So function pointers are unpredictably fucked. Sometimes they're good. Sometimes they're not. It really depends on what you're doing. So it's definitely going to be an issue. Because you're not going to be able to predict where ‑‑ when a function pointer is accomplished in assembly, it's usually a register‑based instruction like call EAX, call ECX or an offset to ECX usually if you're dealing with a C++ class. So they're going to be broken in some way or another. So it's the same way with ‑‑ C++ STL is usually okay, but sometimes it will wind up getting fucked. So you're going to have to deal with that. The more clever you get, if you get into the preamble postscript stuff I was talking about, the more you think about it, the more it gets really complicated because, oh, okay, I'm going to push this here and this set here and then ‑‑ where's the dragon book? I need the dragon book. And it's usually what will ‑‑ AUDIENCE MEMBER 2. AUDIENCE MEMBER 3. AUDIENCE MEMBER 4. AUDIENCE MEMBER 5. AUDIENCE MEMBER 6. AUDIENCE MEMBER 10. AUDIENCE MEMBER 11. AUDIENCE MEMBER 16. AUDIENCE MEMBER 18. AUDIENCE MEMBER 20. AUDIENCE MEMBER 22. AUDIENCE MEMBER 23. AUDIENCE MEMBER 27. AUDIENCE MEMBER 28. AUDIENCE MEMBER 29. AUDIENCE MEMBER 32. AUDIENCE MEMBER 33. AUDIENCE MEMBER 34. for months and months on end trying to figure out what exactly you're doing. So I think I'm pretty short on time, but that's essentially the end of it. I hope you learned something today. I got really drunk, so I'm not sure exactly what happened. So if you want to follow me on Twitter, like I said earlier, my Twitter account is FrankSquared. I have a blog and a website. If you like ANSI, it's on my website. So cool. That's my talk. Thank you very much for coming.