Welcome to AI Village. The next talk is It's a Beautiful Day in the Malware Neighborhood by Matt. And basically we'd like to thank our sponsors, Endgame, Silence, Sophos, and Tinder. And of course, Silence Your Cell Phones. And if you have an open seat next to you, please raise your hand so the people next to you or people coming in can know there's a seat. Thanks. Hey, good afternoon, everyone. Even though Silence is a sponsor, don't worry, this isn't a sponsored talk. This tool is completely open source. So again, my name is Matt Maisel. I'm the manager of security data science at Silence. And specifically today, I'm going to be talking about the use of nearest neighbor search techniques applied to malware similarity, specifically in a tool called Rogers. That's open source on GitHub right now. I have a feature branch that I'm working on some updates from the content today that I'll present. But this tool is designed for malware analysts and security data scientists to perform malware similarity research. So just some motivation. So building databases of our malware is one of the things that is interesting for analysts and for data scientists. So search and retrieval of similar samples, you know, can provide valuable context to analysts and systems. The objective in this case is to basically build a database, index our malware by some attribute or some set of attributes. And when we have some unknown sample, query that database. And hopefully if we've ever seen a similar sample, we get back valuable context, you know, maybe other labeled samples, maybe samples that have been reversed and we have, you know, a lot of details on. So that's a use case kind of number one for these systems. Use case number two is, you know, if a sample we've never seen before and it also doesn't match anything in our corpus, you know, we can prioritize that for manual analysis or maybe more advanced reverse engineering. Finally, kind of the final use case of search and retrieval systems for malware similarity is to augment larger systems, maybe doing clustering or maybe doing classification. So we can use nearest neighbor search techniques to basically process incoming alerts and leverage all of the information from our that we get back with the context to determine if we want to route that sample to other workflows in our environment. Historically this of course has been done in big databases of cryptographic hashes. Fuzzy hashing, notably SS deep is also kind of a standard approach still and still a good factor I'd argue. So how does this relate to nearest neighbor search? So if we consider malware similarity as being performed through comparison of raw bytes or extracted static and dynamic features that distill the semantic characteristics we can take these features and represent them in this end dimensional feature space and with that we can feed that into a lot of nearest neighbor search algorithms as well as other machine learning algorithms as well too. So nearest neighbor search simply put is the task of being given a set of samples X or corpus, basically take a query sample or unknown sample XQ and query the index and basically get back the K nearest neighbors according to some distance function. And there's you know there's a lot of things that can be done with that. So if you look at the you know many different distance functions that can be used here, a gentleman earlier today mentioned Euclidean, you know we can use cosine, we can even use other metrics like string metrics that operate on edit distance or Levenstein distance. And ultimately nearest neighbor search of course is hard at scale you know with high dimensional space so we have to look at approximate variance of this that allows some error threshold epsilon and basically kind of bounds our true distance whenever we query our index. And just again as a really simple example here if this kind of two dimensional space here is if we query this red dot here, the K nearest neighbor for three would be these three samples in the inner circle. If there is maybe, if this is maybe an approximate variance you know there might be a chance that we could accidentally return some of these samples in the larger kind of radius here. So don't worry I don't have any algorithms but I actually cut out a lot of this but there's a lot of interesting theory and literature around nearest neighbor search. You know over the past few years I've been working with a lot of these data centers for several decades. I kind of categorized them specifically in three different areas. So there's tree based methods you know where we're basically partitioning our data set into you know these cells in our feature space. So we use tree data structures to exploit that nature and basically rapidly look up and identify the cell. Kind of shown here in this case here still in a two dimensional space. We use a tree data structure to quickly look up and identify nearest neighbors in this particular cell. There's also hashing nearest neighbor methods as well too. You know where we're typically in this case applying a non cryptographic hash that kind of has this property of you know ensuring that any small input or any small change in the input space only results in a small change in the output space. So the idea is here we're actually looking for collisions between similar objects. In this case we can come up with different hashing algorithms. Locality sensitive hashing is one kind of popular one where the whole idea is to find hashing functions that take some input. So some input sample in this case represented by our feature vector. And if we hash it we can actually find the hash function that takes some input. So if we hash it we ultimately want to end up in the same bucket or end up with the same hash code if it's similar. And then again ultimately this determines it reduces the number of candidates in this case that end up in the same bucket ultimately to actually do distance comparisons on. Because doing you know again doing like pairwise distance is super expensive and no one wants to do that. Finally a kind of more recent really more recent approach for nearest neighbor search is graph based methods. So the general idea here is that we're going to build these proximity graphs. Maybe several layers of these proximity graphs. So we're going to build these proximity graphs. of graphs that we kind of stack together. And we have algorithms that basically do a initial kind of offline phase of building our graph and connect to the neighbors and with these edges based off of their similarity. And then ultimately query time we kind of end up in one part of the graph and navigate around basically traverse our graph potentially traverse multiple layers and build up our candidate set for comparison. The downside here is that a lot of the graphing algorithms are extremely expensive. You know we have to basically find specific types of graphs. We're going to do this when we do our build our offline build phase. That make them easy to search through and traverse through in a short amount of time. So there's a crap ton of methods out there. I highly recommend checking out this ANN benchmarks page. It's on GitHub. There's a paper associated with all two. Every so often this developer reruns all the latest and greatest implementations of these various nearest neighbor search methods across a wide variety of data sets for benchmarking. Kind of the typical benchmark that's used here is this trade off between queries per second so how fast can we look up items to the recall. In this case the recall is the fraction of the true nearest neighbors returned in our search. So the kind of general idea here is that you know up and to the right is better but you can kind of see usually there's this trade off of hey we can query our index for nearest neighbors very quickly. Again if this is like in a large production system but we get that at the expense of having really low recall. Conversely if we you know really want a high recall basically we want our approximate methods to bring back the exact results and we typically trade that off at the cost of queries per second. Now one algorithm to kind of point out here and I'll get into it in a bit is this HNSW or hierarchical navigable small world. So that's this line right here. So it does fairly well and again this is just one example of this New York Times data set for K equals 100. If you go back to their site you'll see that it actually does fairly well across a large a wide variety of data sets. And also you can see that it does well at varying levels of K. So hence kind of why that's one of the algorithms that I specifically picked to look at and you know use the implementation in Rogers for our similarity. So this method was recently created in 2017 against based on a lot of different algorithms in graph nearest neighbor search. But the basic idea at a high level is basically construct this multilayer graph and use it to greedily identify candidates for comparison. So as I kind of alluded to in my overview slide. You know there's this phase where we construct this graph. We query the candidates through this traversal mechanism and then iteratively search neighboring nodes until some stopping criteria is met. So HNSW defines all that all the kind of methods there for the stopping criteria for the way that we build the graph. And ultimately kind of just to sketch this out here after we built this graph consisting of multiple layers. So it's really multiple layers of graphs. You know we set different parameters for this algorithm to determine how frequently basically how deep a sample ends up in one of these layers. I should say how shallow. So we basically start from the top layer here going down to the bottom layer. But the idea at query time is once we've constructed these layers of graphs we start at some point here and navigate. In this case there's only one sample. So it basically searches the neighborhood goes right to the sample. Eventually reaches a local minimum here because there's no other neighbors to look for. And then drops down to the next layer. And it kind of this process continues until eventually we get to the final layer. And this is also can be tuned as well to a query time to determine how deep into the. Layers of graphs we'd like to go. But ultimately this results and again the kind of this paper based. Bases this approach and basically ensures that any samples that we visit across all the layers are likely to be nearest neighbors. And that's ultimately what we use to determine the number of candidates that we end up querying. Or sorry end up doing a comparison against with our query. So that's a graph based method. So there's also another really recent method too that I actually caught at the NIPS workshop. And it's called the graph based method. And it's kind of a little bit different. It's basically a machine learning research conference back last fall. It's really interesting. So it's called prioritized dynamic continuous indexing or PDCI. And there's an earlier iteration of this just called dynamic continuous indexing as well too. So the authors here actually design an exact randomized algorithm. And it's built around this idea that we're going to avoid partitioning samples by vector space. So kind of going back to this example with the tree based methods where we're kind of splitting up our feature space along the each feature. That gets, that basically has a lot of issues. And PDCI, the authors notice that, you know, what we can do is just build these indices and basically take our samples and project them along a random direction. And we do this so we can control the number of indices we want to build to basically determine how well this method actually works. And the idea is we construct multiple indices and the kind of, the main gist is that as you visit the indices and you query the index, you end up at a place where you're, you're basically going to get a number of indexes. And that's kind of the way basically your query is projected to. Any of the samples that are nearby, either in this case that are larger or smaller, you kind of pop those samples off. And if they end up appearing in all indices, again this paper kind of shows that that's highly likely to be the exact nearest neighbors. So you add that to the candidate set for comparison. And again this, this is particularly interesting because in this, specifically within this exact nearest neighbor search method, some of the guarantees in this paper are, are pretty, pretty compelling. And unfortunately though because it's an academic paper, I mean the author is a pretty well, a very well respected PhD student I think at Berkeley. But there's no open source implementation yet. So I went ahead and tried to do a naive implementation in Rogers specifically. So that's PDCI. Again these two algorithms are the ones that kind of I focus on right now for this talk. So other, other malware similarity systems, you know, there's quite a few that have different approaches to doing this for nearest neighbor search or just similarity systems. So I'm going to go ahead and show you the, the probability of similarity in general. So of course FiresTotal has different ways to index data, also using SS deep. But also a, a clustering, basically clustering API that is based off of feature hashing of the static from my understanding from the, the docs, from the structural data pulled out from static feature extraction. And this is actually where I source some of my, my datasets for evaluation of these methods, which you'll soon see to the, to my detriment unfortunately. Our very own Brian Wallace, one of the, one of the first the AI Village core team members actually came up with a blog, wrote a blog post in Virus Bulletin a few years back that basically exploited the way that the SSDeep message digest is built to eliminate the number of comparisons needed. And then more recently, you know, again, you can take this idea and basically apply it to Elasticsearch as well too. So you can just use an off-the-shelf database to actually use the same kind of method here for indexed SSDeep. So again, that's using one of the similarity digest methods that's kind of in the bigger, larger group, I should say, of hashing-based nearest neighbor methods. And then, of course, there's kind of the popular academic implementations of malware similarity systems. So BitShred is highly cited back in 2011. So it uses pairwise Jacquard similarity, and it uses Hadoop to do that. So it gets, again, fully pairwise, so that's very expensive, hence the need for Hadoop. There's also the malware provenance system. Which is a little bit more recent, that uses minhash, a type of the, basically an LSH family, a locality-sensitive hashing family that approximates Jacquard similarity. So that's used across the sliding window of Ngram features on disassembled samples. And yeah, those are the two final ones here. There's also Malheur, which focuses more on the behavioral feature similarity comparison, specifically for Cuckoo. It's a, there's a lot of different capabilities built in there for clustering and also classification. But underneath the hood, there's this kind of use of looking at behavior features for doing prototype identification and prototype selection. So you can basically identify prototypes that are in a large cluster, pretty much like the Centroid around these points, and use that for a way to quickly do comparison. And then, there's also the Brothers Wurzburg, which kind of takes some computer vision ideas from, basically, indexing images, and takes a binary role, but even basically converts it into a gray scale image and then indexes that. So there's a handful of systems out there. There's a lot of algorithms to kind of choose from again that have a lot of different properties. Again going back to the performance metric there of query by recall. So when we're approaching the design of the system to do malware similarity and specifically use that system to evaluate different nearest name research techniques. You know I kind of define these four key design ideas. So number one of course we have to extract and store sample metadata from our raw features. We have to then transform that raw data into some feature representation again in this end dimensional feature space. And we might have a variety of different vectorization pipelines that we want to experiment with. Talks earlier today mentioned a few TFIDF. I use feature hashing specifically in my more recent approach. So we might want to kind of change out that vectorization pipeline depending on you know what we want to evaluate and what features we want to include. And also how large our data set might be. So after we transform our features with one of these pipelines we then fit you know the different nearest neighbor methods and you know do some bookkeeping maybe have to save some basically save some of the database structures that are required. And that's kind of the fit stage. And finally once we fit all these indices you know we want to actually query samples and then basically again kind of pick the parameter K to determine how many nearest neighbors we're going to pull back. And then we possibly if we have this database of sample metadata we also might want to display the contextual features. Maybe again we have some case database of previous incidences in our environment and we want to annotate our samples that we pull back with some of that. And it might help for more of the analyst context kind of thing. So that really gives us or really what I kind of came up with is this design then for Rogers. Rogers is a Python 3 application that you know pretty much has a sample cluster. And then we want to have a sample cluster class. In this case it's really only a sample class consisting of the PE class. That really only focuses on really basic static feature extraction using PE file. But it's built in a way that you could expand the number of sample classes if you again bring in you know other things other than just portable executables. Vectorizer so there's a basically a scikit-learn pipeline APIs that are pretty much used extensively. And in this case right now I have latent semantic analysis which again earlier talk kind of uses some of those ideas with the TDA. And then we would have a lot of other things like PIDF-IDF and then projecting down. And then more recently just because I started getting into data sets that were a little bit larger to handle I started looking at feature hashing approaches you know and this basically can be extended with anything supported by scikit-learn or other vectorizers as well too. And then finally the kind of final component here is this index class you know which in this case I have implementations or I use libraries for like HNSW or I use the LSH force in scikit-learn which you know I was going to be deprecated anyway here soon. But then I have an implementation for index SS deep and PDCI and it pretty much at this point uses SQLite you know as the store for at least the index SS deep and the PDCI methods. All of the feature data too is I forgot to mention is stored in protobuf a message definition that has basically kind of a structure that allows you to add different modalities of features. So you can add static features, dynamic features, contextual features and then give them if you want to you can give that a variable type if you'd like to actually automatically build like feature vectorizers later on but again that's not really not really supported yet in the vectorizer class. Cool so now unfortunately to the sad panda part of my talk. So yeah doing these types of experiments and getting data sets you know for doing malware similarity is difficult. Unfortunately I didn't really get as much time on doing this experiment as I wanted to. What we're looking at here again is we have two charts. So we have two charts so this chart right here is this recall at K for exact nearest neighbor. So at the on the X axis we have the number of K's that we picked for each of the experiments and then on the left on the Y axis here is the recall. Again this is the fraction of the true nearest neighbors returned for the query. On this side right here we have precision at K for neighborhood class. So this is actually more of a same kind of metric where we're looking at the relevant documents over the K or the total relevant sorry relevant over the total number of relevant documents in our data set. In this case I'm actually just using the class for the samples to basically say hey if I query the sample and I get back all my results in my query are all the same class you know because I have labels for them. You know that indicates to me we have high precision at K. So if you look at this it does very well for these samples and I kind of I'll illustrate this in a sec you'll kind of see why. But the actual exact nearest neighbor results are pretty bad. You know we have .3 for PDCI and then HNSW kind of is you know pretty low. And again I did parameter selection or I did some grid search you know for parameters for each of these methods and tried to come up with things but I really couldn't get anywhere and you know just to kind of highlight this again this is this data set is coming from the VT clustering data set with 27,000 samples and 15 classes. So cool. So a quick demo time to kind of illustrate you know at least the interface. So Rogers is exposed as a command line application so if you want to use it on the CLI you can. But I also have APIs exposed to basically make it so that you can import Rogers and then build an index. And then I use Plotly in this case just to visualize some of the results. So I'll blow this up a little bit real quick. Like I said it's still probably hard to see in the back but the idea here is that I've previously fit an index. This one specifically HNSW and again I have the standard APIs here for passing in a sample, setting the number of K, getting back the neighbors. And in this case if we clear this and we can see we get back the sample. Here's the query sample just kind of again doing some print statements here just to make it easy to see what we're getting back. Then here's the nearest neighbors and you know again look how similar these are this is cosine similarity. The graph here just kind of displays them and if we look at some of the kind of features here. So here's the query sample. You know we can see that this is with the Lamer. This is the Lamer. This is the Lamer. This is the Lamer label. And we look at some of these other samples. It's reg run. There's another one too. And yeah totally different. So the labels themselves are different. And what I kind of realized after getting into this is that given the fact that my feature space is limited to the static feature extraction methods. You know and given that you know we're really only looking at samples that are usually I think a lot of these are just like packed samples. Everything really looks identical. So I think that kind of might explain why I had really bad results. And kind of I don't know if you guys have anything to add to that but I think it illustrates the need for me to get better data sets for evaluation here. So just for a little bit bonus. So Zory was released at Black Hat. And because of the frustration I had with some of the feature extraction around the basic static stuff. I went ahead and implemented a Zory feature extraction class for PE. And you know just in this case I only got to pulling out a bag of words for the mnemonics. So it's just kind of an example of this particular sample running Zory on it. I was able to do run this on like five hundred samples or so and build it up to a vectorizer. And again now we can rerun rerun the same query with I'm sorry this is a different sample. We can now leverage the bag of word mnemonics as well in addition to the other kind of static features that are pulled out. And it gives us slightly different results. Again I haven't really done any formal comparison of these methods. So this kind of wraps up stuff I think. But the general idea here you know is that Rogers is a tool for one experimenting with your different nearest neighbor search techniques. But also is a tool to build out vectorizers for the different methods. Similarity you know doing similarity in your environment might depend you know you might have different use cases. You might only want to do static comparison. You might only want to do dynamic. You might want to do both. You might want to you know apply like an automated disassembler like Zory on it. There's clearly at least in the vectorizers that I published with this tool you know it's very limited to kind of just PE. And there's definitely opportunity for different modalities. You know there's also opportunities for doing feature selection and learning representations as well too. To come up with a better feature space that could be used for similarity comparison. For experiments definitely my case you know I did run some parameter optimization but unfortunately just need to get more data sets for doing benchmarks. And also I think it would be interesting to evaluate different distance metrics you know beyond just Euclidean for some of this and basically use that to determine again similarity for some of these methods. And again some of these methods like HNSW by default uses cosine. And then there's a lot of other things that you can do. But like PDCI is Euclidean. And finally more use cases. So potentially in this case you know we're only indexing Mauer samples. But you could also potentially index benign samples as well too. And of course like the key is being able to continuously update the index with new samples as they come in and have been classified or been analyzed. So doing like a partial fit or like an insert operation would be pretty easy to extend as well too. Cool. So this point for questions. So again this tool is up on GitHub. I do have a feature branch. I apologize I got to get it out. It's just been crazy the past week. So this feature branch will add feature hashing. Will add the PDCI and basically publish the experiment. Again I do feel that the experimental results were pretty weak but maybe could be explained just by the data set I was using. Certainly it would be kind of interesting to experiment more. So yeah. And pull requests are welcome. Any questions at all? Sorry go ahead. Show the vectorizers? Yeah. So I guess sorry because I was kind of experimenting with this. Right here. So this is a signature vectorizer. So this one is actually using the Yara rules repo. So I just used the Yara detections as features themselves. And kind of try to figure out like what in this case apply TDF. TDF. TDF. TDF. IDF. To determine like what signatures are more useful compared to others. Go ahead. Oh so yeah in this case there really isn't any feature selection other than applying like TF IDF and then projecting down. Any final questions? I'm sorry I can't hear you at all. I apologize I still can't hear you. Oh so the metrics on the distance calc- so yeah. As I mentioned HN, HNSW uses cosine. The PDCI is using Euclidean. But the metrics are just this, this recall at K. So basically I do exact nearest neighbor search on my data set just using brute force. And I compute that for Euclidean and then I compute that for cosine. And then those are like kind of the, basically the ground truth that I use in this case for this recall at K for the nearest, sorry for the exact neighbors. You have a chance to improve your precision recall curves by some metric on any features? Yeah so I mentioned like you know I don't, don't do any feature learning. Don't do any of that kind of stuff. And I also think that you know going beyond some of the basic static features would, would definitely obviously change the results significantly. So I hope to do that in the future. Oh sorry didn't see you. Yeah so. Just a little background. So the question is you know do more experiments pretty much. Experiments are fun. Sometimes they're, they're sad. But you know it's always got to push forward to the next one. The other question was like what, what did I learn kind of doing this implementation? It was definitely really a lot of fun to do the PDCI implementation. Again it's really naive in Python 3 using, using SQLite basically to, to store those indices. So it was great to have a chance to kind of go through a paper that again has no published source code. You know and then based, or no published implementation for RAP2.0. Yeah. And then try to do that. And then yeah a lot of, a lot of Python 3 kind of things I picked up doing this project as well. Yeah there's not the, so those algorithms don't do any feature engineering. They don't do any feature selection. You basically pass in your, your, your feature vector into index. So that's kind of a pre-processing step. In this case here which I did at the vectorization phase. Yeah. Yeah. Yeah. Cool. Thanks everyone.