[00:00.000 --> 00:08.420] Hello, everybody. My name is Angelo. And I'm here to talk a little bit about fighting malware [00:08.420 --> 00:14.640] with deep learning. So, I will show you how you can use deep learning to build a classifier, [00:15.360 --> 00:29.980] binary classifier. And I'd like to thank you for coming. And thank you, [00:29.980 --> 00:40.240] here. But it's been very amazing here. So, and I'd like to thank my company, [00:40.240 --> 00:50.380] TOTUS, to support me to come here also. All right. So, who am I? I work as an ethical hacker [00:50.380 --> 00:58.680] at TOTUS. It's the largest software company in Latin America. We build ERP. And I'm a part-time [00:58.680 --> 01:08.640] data scientist. Basically, you know, self-made. And then I'm a part-time PhD student. I hope my [01:08.640 --> 01:15.100] supervisor won't listen to that. And I'm interested in deep learning data science [01:16.200 --> 01:23.960] for malware detection classification. Okay. So, first, I want to share [01:27.360 --> 01:37.120] some experience I had. Okay. So, I was not a researcher, you know. I got a degree in mathematics, [01:37.120 --> 01:42.480] but I was not a researcher. So, I went to... I started working with computers, programming, [01:42.480 --> 01:49.400] and so on. So, actually, you're not researching. But then I wanted to make some research. And I [01:50.360 --> 01:57.300] wanted to get into this machine learning, deep learning, AI thing. [01:57.300 --> 02:04.020] And I didn't know how. So, I started studying machine learning. There's online resources [02:04.020 --> 02:13.020] and books. Deep learning and malware analysis. I've already knew something about malware analysis, [02:13.020 --> 02:23.260] but I kind of learned more. Okay. And then, since you want to make research, you need to start [02:23.260 --> 02:29.040] studying the state-of-the-art papers. You know, to see what's going on. What's in the edge. [02:29.660 --> 02:36.780] So, okay. And then I started to read these papers. And I got stuck when I tried to reproduce them. [02:37.340 --> 02:46.720] Because they are very theoretical, you know. And sometimes you don't have enough information [02:46.720 --> 02:52.980] to reproduce them. So, this is a very big problem in machine learning papers. And [02:53.780 --> 02:59.220] for malware analysis, in particular. Because the guys, they don't want to tell you their [02:59.220 --> 03:06.280] hyperparameters. They don't share data. So, it's quite hard to reproduce the papers. But also, [03:06.280 --> 03:14.300] sometimes because you just don't know the basics. You haven't been through the basics. You didn't [03:14.300 --> 03:25.680] code something simpler to see it working. So, you need to do that. Okay. So, here what happens [03:25.680 --> 03:32.520] is because we actually... what he's saying here is that there is a difference between [03:33.240 --> 03:36.880] knowing the path and walking the path. What does that mean in this context? [03:37.480 --> 03:44.800] Means that I wanted to do research, but I haven't done anything yet. I want to jump, you know, [03:44.800 --> 03:53.420] make a big... take a big step. So, that's quite hard to do. So, you need to step back, [03:53.420 --> 04:00.780] right? And fill that gap. And how you do that? Basically, you want to build [04:02.860 --> 04:10.960] that's very important for machine learning in not only in our research, but any kind of research. [04:11.140 --> 04:15.340] You need to build your baseline... your own baseline models from the scratch. [04:15.380 --> 04:21.460] And from the scratch, I mean that you don't need to implement backpropagation yourself. [04:21.980 --> 04:27.340] You don't need. You can, but you don't need. You can use a high-level framework, deep learning [04:27.340 --> 04:33.820] framework to do that for you. So, that will be your starting point for doing research. [04:34.620 --> 04:41.240] And the second thing is that you want to build simple and working models using the techniques [04:41.240 --> 04:48.960] you want to master in research. Because you need to learn them very well if you want to contribute [04:48.960 --> 04:52.400] with something new. And the third point is... [04:54.040 --> 05:01.100] this is kind of strange, but you need to not contribute first to be able to contribute later. [05:02.160 --> 05:06.820] First, you need to learn the basics. Then, you know, you are not contributing. You are [05:06.820 --> 05:13.420] contributing with yourself. And then, you can then try to contribute with the scientific community or [05:14.620 --> 05:20.460] whatever. So, always be humble. You don't know everything. [05:21.780 --> 05:29.280] Okay. So, we are data scientists. Right? Right. So, we need... in our case, we need malware. [05:29.280 --> 05:35.020] A lot of malware. Meaning, we need instances. We need data to feed our models. [05:36.220 --> 05:43.620] This is... that's... this one is my favorite repository. It's called VirusShare. So, there [05:43.620 --> 05:53.520] you can find... I checked yesterday. Almost 34 million instances of malware to download. So, [05:53.520 --> 06:01.900] you can download huge files. Six gigabytes each with 60,000 instances there. And you can play [06:01.900 --> 06:10.420] with them. Okay. And then, one good thing is that they are already labeled using VirusTotal. [06:26.690 --> 06:34.150] So, as you can see, I told you several layers. And the first layers are low-level features and [06:34.150 --> 06:42.490] the deeper layers, high-level features. What we are interested in is getting this data from this [06:42.490 --> 06:50.730] datasets. I will show you. And then, try to figure out if they are... if that... if they [06:50.730 --> 07:08.610] are malicious or not. Because we're trying to detect malware. And then, we have also [07:08.610 --> 07:14.310] the recurrent neural networks. They are good for sequential modeling. So, if you get sequential [07:14.310 --> 07:22.070] data, varying time or space, something like that, you can use recurrent neural networks for that. [07:22.070 --> 07:27.170] They're good for dynamic analysis. So, okay. So, we need to build our machine learning, [07:27.170 --> 07:35.710] deep learning data science pipeline. This is actual real information that I took about one [07:35.710 --> 07:45.130] month to download malware and goodware. And then, I set up an environment hypervisor with nine [07:45.130 --> 07:52.530] virtual machines running for four months around the clock to run this malware and get the dynamic [07:52.530 --> 08:00.250] information. And then, we need to do the basics from data science. Meaning, we need to [08:00.750 --> 08:07.330] pre-process our data. We need to clean the data. We need to make some feature engineering. [08:08.070 --> 08:14.550] We need to get the best features for our problem. In this case, the feature engineering [08:15.410 --> 08:26.750] was minimal. Okay. I haven't done almost any. Okay. And then, we are ready to play with the models. [08:26.750 --> 08:33.350] So, you use a high-level framework and you can start building the models. Okay. So, all right. [08:34.470 --> 08:41.130] So, I have here three models to show you. I will share this code, the Jupyter notebooks, [08:41.130 --> 08:45.590] and I will share the data set, the data sets, because there are several data sets. [08:47.110 --> 08:52.270] So, don't worry if you can't get something, because I will need to be very quick. [08:53.750 --> 08:59.210] Now, I just need some help here, because I will need to throw the code there, and I can't see [08:59.210 --> 09:08.030] the code here, you know, because my screen is extended. Can someone help me there from AI Village, [09:08.030 --> 09:20.920] please? Yes, but then I can't see the code from here, you know, when I drag. [09:25.140 --> 09:33.280] Please. So, what I need is I open here the code, and I want to project there, [09:33.280 --> 09:54.260] but the screen is extended. That's why I... So, I won't have time to explain step-by-step the code, [09:54.880 --> 10:01.280] but basically what I'm showing here is an example of each of those models I showed before. You know, [10:01.280 --> 10:07.180] this one is a multi-layer perceptron for static analysis data. So, basically, [10:08.940 --> 10:13.380] what we do first is to import all the libraries. [10:14.420 --> 10:22.340] Necessary libraries for Python libraries. They're amazing. So, here's our data set. [10:22.700 --> 10:26.320] I'm getting data from the PE sections. So, PE sections basically [10:27.880 --> 10:35.260] describes the sections in the executable file, okay? So, one information here that's [10:35.260 --> 10:43.280] very important is this entropy. Usually, when the entropy is high, it means that either the code [10:43.280 --> 10:52.880] is encrypted or packed. So, malware authors, they usually like packing the content of the file [10:52.880 --> 10:59.880] to avoid antivirus detection. But that's not enough, okay? This can be very high, but it's not [10:59.880 --> 11:07.600] malware. So, can't rely only on one feature. So, okay, and there is a column here saying [11:07.600 --> 11:13.040] some malware or not, okay? So, this is how our data looks like, and this is tabular data. It's [11:13.040 --> 11:22.340] in a table. Here, I do some correlation analysis, you know, to figure out correlation among the [11:22.340 --> 11:28.780] features. You see that some features are very highly correlated. So, we can drop them out. [11:28.780 --> 11:36.200] You don't need to duplicate data. And then, what I'm doing here is opening another data set [11:36.200 --> 11:42.500] that is called imports, PE imports. PE imports basically, you know, when you [11:43.720 --> 11:47.500] create a program, you need to import functions from DLLs and so on. [11:48.820 --> 11:53.460] And maybe we can find, we can, the machine learning, deep learning algorithm can spot some patterns [11:54.220 --> 12:02.760] that are used for malware, in malwares. So, what we're doing here is, in this data set, [12:02.760 --> 12:09.400] is the most 1,000 important imported functions in reverse order. So, for example, this malware [12:10.860 --> 12:17.840] imported this GetProcAddress function, and this GetProcAddress function is the most important, [12:17.840 --> 12:23.720] most imported function. Okay? So, this is what it means. So, you see that there are a lot of [12:23.720 --> 12:28.960] features here, and they are categorical. Categorical features are a little bit harder [12:29.420 --> 12:34.960] to deal with, because you can't just play with them. You're not playing with numbers, [12:34.960 --> 12:42.260] okay? We're playing with categories. All right. So, and in the end, we have said, [12:42.260 --> 12:47.820] and this data set has 47,000 [12:50.000 --> 12:56.900] entries. Okay. So, we remove duplicates, and then we merge the files, [12:57.460 --> 13:04.240] and then now we have the data set ready. Okay. I'll jump a little bit here. We convert this data [13:04.240 --> 13:13.340] sets, pandas data frames to NumPy arrays to feed the models. And then there is another problem [13:13.340 --> 13:23.640] here. We need to check the imbalance. And this is high imbalanced. See? 24 to 1. So, we have 24 [13:24.480 --> 13:30.300] malware instances for each goodware instances. So, that's a problem. Okay? [13:31.700 --> 13:39.840] Then we need to make the strength test split stratified. So, meaning that after performing [13:39.840 --> 13:48.620] this split, we need to keep the proportions. Okay? And basically here, I'm standardizing [13:49.270 --> 13:57.100] the code. I will only standardize the code that is numeric. See? I'm not touching the [13:59.560 --> 14:07.980] categorical data. Okay. So, let's jump to the model. Ah, here. There is something interesting. [14:08.080 --> 14:15.140] We need to deal with imbalance. So, the data set is imbalanced. So, we can try to use some [14:16.000 --> 14:26.120] algorithm to oversample, oversampling technique. So, for example, this moat won't work here, [14:26.120 --> 14:31.020] because it can't deal with categorical features. And there is a variation called [14:31.020 --> 14:40.120] smote nominal and continuous that was taking just too much time to run. So, I gave up for now. [14:40.120 --> 14:44.900] And then we can use a random oversampler. But random oversampler is bad, because it just [14:45.700 --> 14:55.340] duplicates data. But in our case, helped. So, after running that, you see, we can see here [14:55.340 --> 15:02.060] that now the data set is balanced. And then I will perform some visualization using T-SNE. [15:02.820 --> 15:11.780] T-SNE is a pretty awesome algorithm for visualizing high dimensional data. [15:13.060 --> 15:20.040] Basically, it projects high dimensional data onto the plane or to a space. So, you can [15:20.040 --> 15:27.000] have an idea how the data is in that high dimensional space. And then you can also [15:27.000 --> 15:32.600] have an idea about the decision boundaries your model will need to learn. And if you see, [15:32.600 --> 15:40.220] this is complicated, this one. Red is malware and green is good work. We need to separate them out. [15:40.560 --> 15:48.040] So, deep learning. So, here's a model, deep learning model for multilayer perceptron. As [15:48.040 --> 15:52.900] you can see, the model is quite simple. This is not research. This is just [15:55.340 --> 16:01.800] that homework, you know. I said in the beginning that I need to understand how to build a simple [16:01.800 --> 16:09.460] model in order to try to build something more complex or new. So, this is just a model with [16:09.460 --> 16:16.680] two dense layers. And then after that, we create the model, get here the summary, [16:22.160 --> 16:30.600] 146,000 parameters. It's not really too much, right? Here's a model. We can get this model, [16:32.640 --> 16:37.540] the architecture of the model, just with one command. And now here, there is an algorithm [16:37.540 --> 16:45.900] that I would like to explain more deep, but I can't because I don't have time. [16:45.900 --> 16:52.980] But this is a model selection. Basically, I'm making an automation of the model selection here. [16:52.980 --> 17:00.000] I'm getting those hyperparameters and testing each one of them, combinations, you know, [17:00.000 --> 17:07.540] and performing a threefold cross-validation to see which combination is better. [17:08.520 --> 17:15.680] And after that, we train the model for some time. This case didn't take too much, okay? [17:15.680 --> 17:21.360] I trained at home, okay? I have a NVIDIA RTX 2080 [17:23.160 --> 17:37.200] Ti. So 4,300 to the cores. It's a decent card. So 10,000 seconds, roughly three hours. [17:37.200 --> 17:41.500] And here, it's important to evaluate the model using the test set. [17:42.440 --> 17:49.500] And okay, we have here the results from the model selection. So this is the best combination, [17:49.500 --> 17:57.500] dropout rate, 0.6. The first model architecture here is a dropout first, [17:57.500 --> 18:02.420] and then batch normalization. There is a holy war about that. You know, people know you should [18:02.420 --> 18:11.080] use batch norm first and then dropout, or vice versa, and neither of them are both in any order. [18:11.620 --> 18:18.020] But actually, it depends on the nature of your data. So that's why you need to test it. [18:19.500 --> 18:26.100] And the lowest number of neurons won. So after that, we perform an evaluation. And with the [18:26.100 --> 18:33.180] evaluation, we have a thing called confusion matrix to show the true negatives, false positives, [18:33.180 --> 18:41.080] false negatives, and true positives. First, what we do is to create a benchmark. [18:42.200 --> 18:49.140] So suppose that you have a model that predicts that every example is a malware. So what happens? [18:49.140 --> 18:53.660] These are the numbers you get. And you see, since the data set is imbalanced, [18:53.660 --> 18:58.520] you get pretty good numbers here for accuracy, precision, recall, F1 score. [18:58.960 --> 19:06.220] This is misleading. So you need to use a better metric for unbalanced data set. The best metric, [19:06.220 --> 19:12.400] as far as I know, is a balanced accuracy. And balanced accuracy is showing here that, basically, [19:12.920 --> 19:18.320] you have chosen... you have predicted every example as a malware. [19:19.060 --> 19:22.980] So it's very bad. This is the worst case scenario. And then when we apply [19:24.800 --> 19:33.340] on the test set, our model, we get this result. So it's much better. So as you can see, [19:33.340 --> 19:39.840] if we were using a multi-layer perceptron, that is a very simple deep neural network, [19:39.840 --> 19:45.400] we can already get very good results. Now you can imagine those very, very deep models [19:46.140 --> 19:52.700] with those inception layers and so on. You can get much better. [19:52.700 --> 19:59.760] Okay, now the next example, I'll just show you the model. This is interesting, [19:59.760 --> 20:04.560] because what I have done here is just to treat the binary data [20:06.860 --> 20:14.740] like an image, as if it was an image. So take a look at that. This is what we get. [20:15.080 --> 20:22.640] So these are malware images. I get the binary, and then I treat each byte like a grayscale, [20:22.640 --> 20:31.280] and then scale it. So we get this information. And then, basically, we feed it to a convolutional [20:31.280 --> 20:37.260] neural network, because convolutional neural networks are specialists in dealing with images. [20:37.260 --> 20:43.420] So the same problem, imbalanced, and so on. And then visualization looks similar to what [20:43.420 --> 20:48.700] we have seen there. Pretty complicated decision boundary. And then deep learning. [20:48.700 --> 20:54.620] So this is our model, convolutional model. So as you can see, it's very, very simple. [20:54.620 --> 21:03.500] We have a convolutional layer, and then we have... there is a model selection here. [21:03.540 --> 21:08.600] It's about, should we apply max pooling first, and then batch normalization, or [21:09.860 --> 21:15.520] batch normalization first and max pooling. This is also a holy war, you know? Nobody knows [21:16.300 --> 21:24.600] what's better. But what you can do is to perform model selection with cross-validation, [21:24.600 --> 21:31.660] which is better for your kind of data. All right. So basically, we have a convolutional layer, [21:31.660 --> 21:37.260] and then max pooling, and then another convolutional layer, and then max pooling. [21:37.260 --> 21:44.780] And then we flatten them out to feed to a fully connected layer for classification, [21:44.780 --> 21:52.580] or in our case, binary classification. Okay. So we train this for some time. [21:55.050 --> 21:57.630] I think I still got some five minutes. [21:59.950 --> 22:04.830] I want to show you how much time I needed to train. [22:06.070 --> 22:21.340] Here. So 15,000 seconds, five hours, four hours. Not too much, really. [22:22.080 --> 22:30.360] And the results. So cross-validations and results. Evaluation. Okay. So the bank mark, [22:30.360 --> 22:35.140] also pretty good. Accuracy, precision, recall F1 score, because they don't deal [22:35.140 --> 22:41.340] very well with unbalanced datasets. Balanced accuracy is the worst possible. [22:41.840 --> 22:48.580] And then when we use our model to predict using the test set, we get this. [22:49.240 --> 22:55.480] So also not good. But there are some explanations here. First, because [22:56.640 --> 23:03.960] I think the main explanation here is because malware, usually nowadays, they are all packet. [23:05.180 --> 23:11.160] So the content is encrypted or obfuscated. So what you are seeing here [23:13.320 --> 23:20.580] is mostly random noise. So you can't expect the algorithm to do much better. [23:20.760 --> 23:26.820] But still, it's a pretty good result. You could just, for example, [23:26.820 --> 23:35.220] add and step in your pre-processing pipeline to unpack this. But this is quite complicated, [23:35.220 --> 23:41.140] because each malware uses a different technique, different key. Sometimes the key, [23:41.140 --> 23:51.800] and so on. So it's complicated. And then, finally, dynamic analysis. Dynamic analysis, [23:51.800 --> 24:00.220] I will show you the network called long short-term memory. It's pretty good for dealing with [24:01.160 --> 24:08.440] sequential data. And all this software you see for speech recognition, automatic translation, [24:08.440 --> 24:13.260] use this kind of network. So here I'm getting the sequence of calls, [24:13.260 --> 24:22.160] of API calls. See? T0, T1, T2. These are the calls the malware does to the Windows API. [24:22.160 --> 24:30.140] So I got just 1,024. And then, basically, I do the job here of cleaning and then [24:30.880 --> 24:36.480] balancing the dataset. And take a look at this. Even more simpler network. [24:37.380 --> 24:46.520] There is a version. If you are running TensorFlow and Keras, you can use the Qt version [24:46.520 --> 24:54.790] that runs much faster than the LSTM version. And then, basically, [24:57.740 --> 25:03.340] this takes much more time to train, okay? Your GPU will be burning there for some hours. [25:03.340 --> 25:15.200] Really burning 90%, 100% usage for several hours. And in this case, it took about [25:17.100 --> 25:26.240] 29,000 seconds, nine hours, okay? And the results here of the model selection. [25:26.660 --> 25:30.960] And, basically, you get our confusion matrix and then get the results here. [25:30.960 --> 25:40.320] You see? We got 91% balanced accuracy. This is a little bit impressive. Because our [25:41.200 --> 25:48.120] dataset is small, you know? And the number of features, I don't know if you have noticed there, [25:48.120 --> 25:56.380] but the dataset has 1,024 features. And we used only, I think, 300. Because my computer didn't [25:56.380 --> 26:02.200] have enough memory. Because we need to make a conversion of these features from categorical [26:02.200 --> 26:09.060] to one hot encoders. Encoding, sorry. Before feeding the model. But my computer didn't have [26:09.060 --> 26:16.220] enough memory for that. So, all right. Now we can have an idea how you can [26:16.860 --> 26:22.400] apply deep learning for model detection and classification. That was the idea of this talk. [26:22.400 --> 26:38.700] So, let me just close here. Can you see there? The slides? Okay. So, okay. And my research, [26:38.700 --> 26:44.040] basically, is about malware behavior. And I'm researching specialized data augmentation [26:44.040 --> 26:50.180] methods. Because the current ones, they just don't work very well with this kind of data. [26:50.180 --> 26:56.060] And also specialized representation learning techniques from our detection classification. [26:56.060 --> 27:01.160] And this will lead to improvements in detection and classification of zero days [27:01.160 --> 27:10.760] and polymorphic and metamorphic malware. And that's it. Thank you very much for attending.