Our next talk is by Robert Brandon on JumpGate, Accelerating Reverse Engineering into Hyperspace Using AI. We'd like to thank our sponsors, Endgame, Silence, Sophos, and Tinder. And we'd also ask if you could raise your hand if you have an open seat next to you so that basically people in the back know that there's a seat available. And then finally, please silence your cell phones, and here's Rob. All right, how are you doing, folks? So I'm going to talk a little bit about something that's been kind of an obsession of mine for the last couple of years. So, like Tori, who am I? Been working in tech for a while. Finished my Ph.D. in Computer Science at the University of Maryland, Baltimore County last year, largely on the research that I'm going to be talking about today. And I'm currently a threat hunter with Bruce Allen Hamilton's Stark Labs. So when you're doing research, in a lot of cases, it's a good idea to kind of figure out, what's the big question you're trying to answer? Otherwise, it's really easy to find rabbit holes that you're going to go down and waste a lot of time without solving kind of what you initially set to solve out. So the big question that I've been trying to figure out is, is there a good way to represent machine code in a way that computers can understand? You can kind of sit back and say, well, you know, of course, computers understand machine code because they execute it. But that's like saying, you know, a line chef at McDonald's understands, all of cuisine, because they can follow a set of instructions. You know, what I mean by understand is, can they take that particular bit of code and in some way place it in the context of all the other code that exists? You know, so it's kind of the question of, is there a representation of machine code that captures the semantic meaning of that code in a way that both computers and humans can kind of make easy comparisons between different types of different pieces of code? And this has a lot of applications. You know, one of the big problems or tasks in reverse engineering is the problem of binary similarity. You know, so given a piece of, or given a program, you don't just want to know, is this thing malware? You want to know, what kind of malware is it? You know, does it have some kind of encryption component that could mean that it's ransomware? You know, is this similar to other rats that we've seen? This also has a lot of applications in vulnerability discovery. You know, if you have a program and you decide, hey, is there, are there any vulnerabilities in this program? You're going to be asking questions like, does it have a library that we already have, that has known vulnerabilities? You know, and in a lot of cases, you can do that with signatures, but then those signatures tend to break if the library gets recompiled. So there are definitely ways to approach the problem right now, and BIM BIF is awesome, but it doesn't really scale well. You know, BIM BIF works great if you've got three or four binaries you want to compare. If you've got three or four thousand, though, it very quickly becomes computationally infeasible. Then we also have similarity hashes like SSDs and SDHash, and those are great, once again, if your only question is similarity. But those hashes don't really do, don't really help you as far as being able to encode any of these semantic meanings of a function or a program. And so kind of the other problem I'm trying to tackle with this is, how do you model binaries for machine learning? So most machine learning algorithms, you need some kind of fixed length input. In a lot of cases, when you're working with a program, those fixed length feature vectors are going to be constructed by domain experts. You know, they'll look at things and say, like, okay, there's important things like, how many sections are in the PE header? You know, how long is the text section? You know, how many bytes is the binary? How much data is there? What's the entropy? But those features aren't always comprehensive. One domain expert might pick a different set of features than another domain expert. There's no real good way to decide what's the right way to do it. And in a lot of cases, some things, like an N-gram computation, that can be pretty computationally intensive once you get into it. There's all this sort of layered number of N's. When you're looking at machine code, machine code doesn't easily fit into a fixed length feature vector. Machine code can be incredibly variable in length. You know, if you're looking at functions, the length of the functions kind of just don't fit on the verbosity of the program. Some people like writing really long functions and putting everything in long functions. Other programmers will have very short functions. And, of course, label data is really hard to obtain. You know, nobody's going to sit down and look at all the programs out there, and exhaustively, you know, categorize them by, okay, this one has some network connectivity, this one has description, this one has text. The other real significant challenge in this field is there aren't a lot, compared to fields like vision and language, there really aren't a lot of machine learning researchers working in the security field. And even the ones that are working in the security field, the ones working on, you know, binary analysis in reverse engineering are a very small subset of that. So because of that, I like to try and find approaches from other domains where other people have been successful that I can apply to the domain I'm working in. So for binary analysis, I found the field of natural language processing to be extremely useful. Because, really, there are a lot of structural similarities between computer languages and human languages. You know, I mean, they're both created by kind of the same webware. So both consist of some kind of arbitrary length sequence. You know, both have a very rich semantic and conceptual information content, you know, on top of just what you both are composed of. You know, functions have meaning to humans at a level that's higher than the actual code, you know, or the actual sequence of instructions. And fortunately enough, there's been a lot of research done on how you process and represent language. You know, it's been ongoing since before computers were a machine. And I always like to try and avoid reinventing the wheel when I'm working. So a lot of natural language models kind of rely on converting the text into some kind of pi-dimensional space, you know, a hyperspace. So a hyperspace is basically any Euclidean space with more than three dimensions. So, you know, it's a space where you can do things like add vectors. And more importantly for data science and machine learning, you can compute distance between the vectors. So in data science, you know, it's very common to model similarity by taking the distance between two vectors in some high-dimensional feature space. And in machine learning, you know, machine learning, fundamentally, the vast majority of machine learnings are just trying to figure out how do you take these data points and draw a line between them. So kind of as an example up there, you have a data set in one dimension of x's and y's. You want to figure out how can you draw a line between the x's and y's. You know, if you're just working in that one dimension, there's no way that you can do that. But if you can do something like, say, take the square of each value and move it up into a higher dimension, in that higher dimension, you can then draw a line between the two classes. So the ways that you can take language and you can move it into higher spaces, you know, there's been a lot of techniques invented to do this sort of thing. One of the most common techniques is a bag-of-words model, where you just take a straight count of each of the words that are present in the document that induce that constructive effort. That also translates fairly well to a machine code. You know, you can do a bag of opcodes and just take a count of the opcodes. You can also do engrams, which are basically where you take, say, the sentence, the cat ran past the cat. You know, the two grams would be the cat, cat ran, and ran past. That works reasonably well. I mean, the problem with engrams is when you're looking at machine code, and once you get over about five grams, you start looking at processing power on the order of ones to try and compute all of the five grams that are present in a set of binaries. So most of the... But most of the really awesome natural language approaches have kind of moved away from just basic law counts and they've moved to a concept called embeddings. And what embeddings are trying to do is take those word counts, or take a document and convert it into a dense vector within some higher dimensional state. And what I mean by dense is there aren't a lot of zeros in it. It's going to be made up of real numbers. That just makes complications easier. So in general, most documents are kind of sparse. You know, your typical document does not have all of the words in the English language that are in it. So a vector of that document is going to have a whole lot of zeros in it. But the other really cool thing about word embeddings as well is the vectors within the states, within the five dimensional states, just kind of naturally cluster into areas where high level meaning for humans kind of resides within that area. So for example, if you train a vector space model on a whole lot of English language documents, you're going to end up with a space or a hyperplane or a space within that high dimensional space where the concept of capital cities is. You'll have another space where the concept of countries is. So you can do things like say the vector for London plus the vector for Britain minus the vector for France will land you somewhere in the region of the vector for Paris. So one problem with figuring out how you apply this to machine code though is that for most of the natural language vector models, the vector is constructed by examining the co-location of words. You know, what words are used together? The meaning of a word, you can kind of infer that by its name. So when you try to apply these concepts to machine code, you run into the problem of first off, what is the equivalent to words in machine code? You know, you're looking for something that has a fairly high level semantic meaning to a human. You know, something that encodes a lot of very dense information. But isn't too generalizable. You know, it's not so general that it has no meaning. So you could use, say, opcodes. But an opcode like push, move, that, you know, it really doesn't say much about what the program does on its own. You know, something like a basic block is another intuitive structure that you could use. And those are easy to use. You know, a basic block is basically just continuous execution of code in the assembly without being granted. So in the assembly, it would be a sequence of things until you run into a jump or a call or some other point where the code has to make a decision about what to do. But at least from my thoughts, when trying to figure out, you know, kind of what is the basic view of a program that you'd want to apply some kind of semantic meaning to, functions seem like the natural choice. I mean, programmers, when they're coding, they commonly break things up into functions. Reverse engineers, when they're looking at a binary, they usually try and break it up into functions and then figure out what each function does. So the problem now, you know, now that we've decided to use functions is functions don't exhibit the same kind of temporal properties that language does. You know, if you're looking at a document of English language, you know, the words that are right next to each other, you can say that these words have something to do with each other. If you're looking at a static binary, you really can't make that a function. You know, you're going to have function definitions right next to each other that really have nothing to do with each other. You might have printf defined here right next to, you know, open socket, which is right next to, you know, encrypt all the things in the ransomware. And each of those functions, you know, sequentially in the binary really, their location in the binary really doesn't say anything about what function they perform. So when you're looking at machine code, you can't take a co-occurrence type of approach. You really have to look at the composition of the function. You know, what instructions comprise this function? You know, not so much what's next to it. So from there, you know, if you're going to be working with a binary, you have to look at a compositional approach. You have to figure out, okay, how do we represent the compositional function? So just looking at, you know, average length of x86 instructions in a wide variety of machine code, you know, the common length of most instructions is probably around seven bytes. You know, to include this instruction as well as the operands. That's really computationally infeasible if you're just trying to do n-grams. And you can do, you know, two or three grams, and each of the n-grams is just going to be a subsection of a instruction. And, you know, half an instruction really doesn't give you a lot of usefulness. You know, you could sit down and have a human who really knows assembly really well figure out, okay, how, you know, what are the things that are significant when I'm looking at assembly? So you could, you know, you might find things like, okay, if I see a bunch of pushes followed by, you know, the pop, followed by, you know, a bunch of xors, you know, followed by a bunch of xors, that might, that's probably some type of a encoding encryption routine that's, you know, a significant pattern. But there's a lot of patterns like that. They're extremely variable length. And figuring out, you know, which patterns are significant and how do you encode them is not really a tractable problem. You know, if you step back and think about it intuitively, you know, if you really want to think about it intuitively, and you want to figure out how do you build something, then you need to, you know, if you want to know what features are significant to represent something, then kind of knowing how to build it, figuring out how do you build it, you know, which features are important to construct those things, is a useful way to go about it. But to test the approach, you know, and it's a really hard problem for humans to do, why not let a neural network figure out how to compose functions? And then just take what the neural, you know, take what the neural network's going to do. Save all the work. So fortunately, there is a type of neural network that does that, which is a character RNN. Character RNN is a generative neural network that generates test sequences, you know. The great thing about a generative neural network is you don't need label data to train it. You know, the data is its own label. And the other really awesome thing about this particular architecture is all of the popular deep learning frameworks out there have examples of code for doing this, you know, just in their reference libraries. It's a fairly common part of this type of thing. So training a generative RNN, you basically are going to show it a sequence of bytes, one byte at a time. It's going to try and predict the next byte in the sequence. You know, if it gets it wrong, you know, after you go through and let it predict everything, you can go back and let it correct the weights and just the network training. So like in this example, you know, you have a neural network that at each time stamp is trying to predict the output based on all the sequence of letters it's seen so far. You show up a letter C, it's going to predict the letter A, you know, good job neural network, you know, that's right. You know, as it says, okay, I've seen C and A, I'm going to predict the next letter C. Great. It says, okay, now I've seen C and A, C. Maybe the next letter is Q. You know, at that point, the training is going to go back to C or D. That's not a valid word. Let's go ahead and correct the weights at that point in a way to hopefully get it working right. So this is an example. This is just a bit of assembly that a, I trained a generative neural, or a generative RNN on lots of assembly. You can kind of see that it's reasonable looking assembly, except all the registers, right? You know, there's no registers that you wouldn't expect to see. And it even learned to clean up the stack when it's done. So what you end up with after you train one of these networks is you basically have a method for embedding your functions into a higher dimensional state. You know, the final set of activations in your general, generative neural networks, you can basically treat as a high dimensional vector. You know, if you have 100 neurons in your network, then each of them is going to have an activation, which is a number. So you've got a 100 dimensional vector within vector space. And just given the way the training process works, similar sequences of code are going to quality similar activations within the neural network. So you can kind of say that if you've got two pieces of code, each of the vectors, you can say they're similar based on the fact that they activate a neural network that's looked at a whole lot of F86 code the same way. So of course, you know, being a data scientist, there's no science without testing. You can say, hey, this is great, but how do you know all these numbers that you're generating actually do what you want them to do and you're not just generating a bunch of neural network garbage? So in order to test this, I trained three generative LSTMs on a data set consisting of functions from React OS, Arch Linux, and Windows 10. They were compiled with both GPPT and Visual Studio. The overall data set was about 23 million functions. LSTMs were all single layered. I trained one with 100 nodes, one with 500 nodes, and one with 1,000 nodes, just to kind of get a broad understanding of how much representation can you get within a particular neural network architecture. And just given that the LSTMs take regular training, the only training we should be using is retraining that as we thought. And with React OS, we should be trying to calculate that 500 times less. And all the statistics, if anybody cares, probably can chat later. So the next big decision is do you want to work with assembly versus the raw binary? There's been some prior research, but in a lot of cases, people want to disassemble the code first, which, you know, that's reasonable. I mean, if you're trying to look at things, a lot of people like looking at assembly a lot better than binary. There are some downsides to that though. The first problem is what is the correct disassembly? You know, every disassembler can disassemble things slightly different. You might have one disassembler that wants to use AT&T syntax, another one that wants to use syntax. And kind of figuring out which one is correct is a hard problem. And really, I prefer to try and keep things as close to the level as possible and as close to the original data set without introducing more in the other data sets. One problem I did try to find a workaround for, though, is that raw binary code misses some valuable semantic information. So the x86 instruction set is a, for example, any function calls are done relative to the current address. So if you have a, let's say, if you have a printf sitting at address 20 in memory, if it's called from a function at address 5, it'll say actually call me plus 15. You know, if you call it at address 10, it'll say call the function at me plus 10. So that, you know, that introduces some problems because there's no way to tell that the same function is being called multiple times. So in order to work around this, I do some basic normalization of the data. You know, if there's a function that's being imported somewhere in the binary, then I'll just take, you know, before I send it over to be vectorized, I'll just compute a 32-bit hash of the function name and substitute that into the address. So that every time you, let's say, call printf, the neural network sees that this is a call for the same hash. So how do we evaluate these embeddings after we use them? In a lot of cases, the criteria for deciding, you know, are embeddings any good is kind of like, well, do they work for the problem I'm trying to solve? That's, you know, that's great from an engineering perspective, not so great from a scientific rigor perspective. So one of the ways you can kind of evaluate embeddings are you can kind of plot them out and look to see, you know, do they kind of have some kind of structure that's interesting. You know, you can do random sampling or just have somebody kind of eyeball whether they look good or not. Now that's okay, but I mean, having a human kind of eyeball them, that's, A, it doesn't scale, and then you're also kind of prone to bias. You know, if you really want your algorithm to work, you might be tempted to ignore some of the stuff that you can't quite explain and focus on the stuff that you can. So in order to evaluate these embeddings, I'm going to kind of borrow some similar things that one or two researchers in the natural language space have been doing to figure out some standard tests for an embedding model. So in the natural language realm, you have things like standard lists of synonyms. You can evaluate, okay, do the embeddings say that all of these words are synonyms to each other? We don't really have that for machine code. So the criteria I'm proposing are consistencies. You know, are these embeddings consistent with embeddings generated with other algorithms or with other models? As well as, you know, the ultimate job of an embedding model is to try and extract some semantic content of something into a fixed dimensional space. So can we come up with some standardized tests to kind of measure how much semantic meaning is being extracted? This is just a scatterplot of some of the embeddings, you know, plotted by operating system and compiler. So you can kind of tell, well, it's not quite as obvious with the colors, but you can kind of see that, you know, even just eyeballing it, the stuff that was compiled with GCC, which would have been the Arch Linux, you know, is sitting in a very different area of the space compared to the stuff that was compiled with Visual Studio. And as a human analyst, you know, I would say, okay, you know, yeah, so, you know, Visual Studio and GCC use completely different function prologues in the book than in the text. Totally different. So when I'm looking at it, it's totally obvious that they're different. But, you know, when we were training the embeddings, we didn't try to optimize them. We didn't say, we didn't tell the model, hey, these things are separate, so you should learn that. That's just something that is picked up on its own. And for evaluating consistency, I'm going to define two types of consistency. I'm going to say heart consistency, which is, you know, using bottle one, the nearest neighbor for function A in set one. If we do the same nearest neighbor measurement on set two, they have to have the exact same nearest neighbor. And that's a fairly rigorous criteria there. That's something you definitely, by random chance, it's not going to happen. And even two reasonable models, you know, like, for example, is the word fluffy, you know, a closer synonym to salt than the word dose. They both kind of have the same meaning, but which of those is, should be, you know, the closest neighbor to salt? So to kind of relax you a little bit, I also have a measure of salt consistency, which is that, you know, if you take function A in set one, then its nearest neighbor in set two is going to be within the 10 nearest neighbors. That way you're saying, you know, it may not be exactly the closest one, but it's still, you know, in the same area. So evaluating the models, I think I actually got some pretty good results for consistency here. So for the consistency measurement, I took a random sample of 10,000 functions, just because doing a full end-by-end measurement of 23 million takes forever. And out of those 10,000 functions, around a quarter of them met the criteria for hard consistency between models. So that's basically both the 100-node neural network and the 500-node neural network. Twenty-five percent of the time, they said, okay, these two functions have the exact same nearest neighbor. And when I relax that to the salt consistency of 10 nearest neighbors, then we are still getting around 50 plus percent, which is a really good result. If you try and look at what you'd expect from random chance, you wouldn't expect that in your year this kind of consistency. This really kind of gives you some kind of a confidence that the neural networks are learning consistent, useful stuff about the data that they're being shown. So for the problem of standardized tests, standardized tests, not just for your kids anymore, some of the tests I came up with are, given the embedding, can we tell what, given just embedding, can we train a model or tell what compiler was used to build that, to compile that function? Going a little further, can we tell what optimization settings were used with that compiler? And on top of that, can we determine whether a particular, functions from a particular library, for example, WS232.dll, can we tell whether those were used in this function from the embedding? So to do that, given that I had labeled data because I compiled all these things myself, I trained several classifiers. So I trained the logistic progression classifier to tell what compiler was used. And no surprise, that actually got 100% accuracy. Which, looking at the PC plot from earlier, you can kind of tell that the two classes are basically linearly separable in this space. What I was actually even more impressed with was the softmax classifier that I built for detecting the compiler optimization actually got between 72 and 85% accuracy depending on which embedding type, you know, we've got a little better with the higher dimensional embedding. That's really impressive when you consider that in a lot of cases, especially for small functions, a compiler may not generate different code. You know, if you have a code, if you have a function that just takes two numbers, adds them, and returns the result, it doesn't really matter if you're using 01 or 03. There's really not much you can do to change that and make it better. And even in the cases where the softmax classifier didn't get the correct optimization setting, it still determined that it was the correct compiler. You wouldn't just say, well, I know this is GCC, but I'm not, you know, I'm going to say maybe it's 51% that it's compiled with dash 01 and 49.5% that it's compiled with dash 03. That's definitely not the case. In addition to that, I trained a random port classifier on functions that had WF232 or imports from WF232.dll and functions that didn't. That was able to get about a 78 to 91% accuracy, with the accuracy increasing with the dimensionality of embeddings, which I thought that was actually really cool. The embeddings really appear to be encoding things like, okay, this has an import from the Windows network library. You know, it looks like these things work. So how can we actually do something with it? So this is where the framework that I'm working on calling JumpGate comes in. So basically one of the challenges in the reverse engineering space is that there is definitely not a shortage of tools to use. You've got IDA Pro, you've got Binary Ninja, you've got Redare. All these frameworks do really awesome stuff. None of them interoperate well, and none of them really work in the same way. So in a lot of cases, you know, analysts will kind of pick which do they like better. You know, you have some people that are like, I'm an IDA Pro user, you know, I don't want to touch anything else. Others are like, oh, IDA Pro is too expensive, I like Redare. So my goal here is to be able to take embeddings and use them with whatever kind of front-end you want. If you're an IDA user, you should be able to use as many of these things and embedding models that Redare users train and vice versa. So kind of the architecture with JumpGate is you'll have your client, which is going to be IDA Pro, you know, Binary Ninja, Redare, whatever front-end you really like to use. The key here is the vectorizer, which is basically just a Python class that implements the... You can write vectorizers for whatever framework you want, you know, if you're a PyTorch person, you can write your vectorizer in PyTorch. If you're a Keras person, you can write it in Keras. If you really like just coding everything from the ground up, you can do that. You know, as long as you can send something, a flat-screen device, then you're good to go. From there, your vectorizer will, you know, convert, move it up into this high-dimensional, into your high-dimensional space, and you can send it on to modules that do whatever tasks you want to do, whatever you want to do. If you want to train a compiler software, go for that. You know, if you want to have it do find the 10 nearest neighbors in your collection of functions and send back whether you have something within a certain distance, you can do that. You know, it's really intentionally less very freeform, so that you can do whatever you want with it. Like I said, some of the, you know, sample applications that I've either tested or am working on right now are, you know, are function similarities. So, I've got this binary that I just found somewhere, I don't even know. In my, in all the other binaries I've seen, do I have similar functions? Compiler identification, is this binary, which functions were compiled with Visual Studio? You know, are they running at what's compiled with Visual Studio? That might be interesting. Crypto, is that, a little more challenging because that requires building a data set of known cryptos or functions, stream classifier, and all that stuff. But, this kind of enables that a little bit. And kind of the only limitation is, what can you think of to do with vector, vector space model code? So, some of the ongoing stuff I'm doing with this, planning on doing a, building out a 64-bit data set model. That should be a lot more interesting, 54 bits, where everything's moving right now, as well as, you don't have the same, function, calling different complexity that you have with x86. So, you can do compiler problems on 54 bits with the same actually. That should be fairly interesting. Also, in the middle of transitioning the entire project from Python 2 to Python 3. But, not up in GitHub right now. While I finish that transition, hopefully, I'll get it up there within the next week or so. And, other than that, that's my talk. You can message me on Twitter if you want.