So, hi everyone. My name is Maxim Shudrak. Friends call me Max. I am a researcher. This is the last DEF CON talk this year, which is really sad, but we're going to talk about malware fighting in the next 45 minutes, which is cool. So, let's go. First of all, I'd like to thank DEF CON goons who gave me a chance to speak on this stage. This is a real honor for me, and this is the first DEF CON in my life ever. So, I'm talking, and this is the first DEF CON. I'm really excited and enjoy to be here. Originally, I'm from Russia. Defend PhD there in more research in 2016. Had a chance to work on malware analysis in Israel for two years, and since 2018, I've been living in the Bay Area. So, I'm with you guys. As you can see, my background combines experience in malware analysis and fuzzing. So, one day I got an idea. Why not try to search for bugs in malware? Sounds like a crazy idea. But my good friend, Jonathan Brossard, who actually inspired me to present this talk, was excited. So, this presentation is logically divided into three parts. In the first part, I'm going to explain why, where, and how we can search for bugs in malware. Why coverage-guided fuzzing is the best technique to search for these bugs? What kind of problems we have to address to be able to find bugs in malware? And, of course, I'm planning to show a demo. My fuzzer implemented on top of WinFA. In the last part, I'll show you several cool zero-day I found in different malicious samples and explain future directions of work. So, before I actually start fuzzing binaries, I decided to find and look over some leaked malware source code files. And just to understand that this idea makes sense and it's worth to spend time on it. And guess what? Right in the one of the first source code files, I found this comment in Russian, which can be translated in the following way. So, I was really laughing for a couple of minutes. I thought, okay, looks like this idea makes sense. And I'm going to find some bugs in this sample and probably in another. By the way, it's Carberb. So, when they write malware, they have to do a lot of complex things like initial infection, payload delivery, and most importantly for us, communication with C&C. There are a lot of things that can potentially go wrong here. So, an ideal place for us to search for bugs would be some complex parser of incoming commands from C&C or some complex file format parser. While some samples leverage very trivial algorithms to communicate with C&C, there are a lot of samples that support really complex communication protocols implemented from scratch. Despite of this complexity, bad guys usually are rarely interested in implementing secure code for many reasons, such as lack of time, expertise. So, in most cases, we will not see things like SLR, DEP, or any other anti-exploitation techniques, which is actually good for us. Sometimes the code is so badly written that malware doesn't work if environment has slightly changed. This tweet actually explains it a lot. So, and of course, searching for bugs in bad guys' code could not be boring. So, I hope you think the same. So, hacking back in general is a pretty well-known research topic. There were a bunch of great talks at DEF CON presented in the past. I can safely guess this idea has lived with hackers' community for decades. I just listed two of cool DEF CON talks presented last year. But what about fuzzing malware? Well, there are much, much less publications in this field. Actually, there is no systematic research on this topic at all. I found several research papers published by Academia. But the main goal of this research was to find and trigger some malicious code paths hidden in malware using fuzzing, which is a bit opposite. In this talk, I am focusing on bug hunting. And how we can use these bugs to defend against malware. So, legal issues. It's less relevant for this talk. But anyway, I want to say that hacking back is in very, very deep gray zone. There are a lot of questions with attack attributions, scopes of attack, and a lot of other things. So, but no one can stop us to search for bugs in malware. So, it's obvious. Cool. We now understand our motivation and legal aspects. Let's say we found some bug in malware. What kind of benefits we can get from that? Let's imagine that we found some memory corruption bug that lead to crash in some sample that are spreading around the planet. Such bug actually might be quite useful. I guess many of you remember famous kill switch found in WannaCry, which significantly helped to slow down spreading of this sample. If you place one file with special name in one specific folder, WannaCry will not infect your machine. Of course, they left this kill switch on purpose. But if we can automatically or semi-automatically find some memory corruption, for example, in some complex file parser, we don't even need such gifts from them. We can just place this file in our operating system and malware will not infect this machine. Besides that, if we can somehow trigger such bug remotely, we would be able to do a lot of other cool things. Like we can stop malware from spreading over our network or slow down it or shut down existing agents just by modifying network packets coming to and from CNC. It's especially cool if we can do that against botnet that are trying to perform a DDoS attack against us. For example, if bots have some vulnerability in the victim's response parser, we just need to send our exploit back to bots and it will cause a crash. Later in the demo, I'll show that actually it's possible. Well, it would be really great if we can trigger remote code execution here. Of course, we can take control over botnet or shut down existing agents or track down botnet owners and do a lot of other cool things here. And of course, our sweet dream bug is remote code execution on CNC. In this case, we have guard mode and can do everything. But in my opinion, it's less likely today because most CNC are written in memory-safe languages like Python, PHP, Go, or any other. I don't see any reason to write it in C or C++. Okay, how can we search for these bugs? Today, phasing is the most efficient technique to search for bugs in memory-safe languages. Actually, phasing is very important for software security at all. Top tech companies, huge open-source projects who integrated phasing within development lifecycle, they all report that this technique improves security of their products. Linus Torvald recently said that phasing actually improves security of Linux kernel, which is really cool. Okay, what's phasing? Phasing is actually a very simple technique. You provide potentially invalid or malformed input to your program and monitor your program for crash. Nice and hard. So you start your phaser. Phaser generates input and sends this input into your program. All you need is to sit and pray that it will find some cool crash for you. This picture actually precisely explains my feelings when my phaser reports a new unique crash. I'm usually very happy, like really happy. Okay, what is coverage-guided phasing? Many years ago when phasing was dumb and blind, phasers considered the program as a black box into which we send our test cases. It usually worked pretty good for trivial bugs that are located not deep in the code. People wanted to find more complex bugs, deeper in the code, so they decided to instrument program under test at compilation stand and provide this coverage back into our phaser to be able to improve test case generation. So the best example of such phaser is American Phaser Lob or AFL. So during coverage-guided phasing, if we manage to find a test case that triggers a new code path in our program, the phaser saves this test case and then performs subsequent mutation on top of this new finding. The same for the next code path and for the next. And this way we can touch much more code deeper in the program and find more bugs. In theory, of course, dumb-blind phaser at some point can also find this path. But it can take a lot of time to find them. So the best example is this code. In case of coverage-guided phaser, it's going to take about several minutes to find this null pointer dereference. But in case of dumb-blind phaser, it can take years to find the same problem. So you see why coverage-guided phaser is a really powerful tool. It can be so effective. Okay. Today, there are two states of the problem. There are phasers. It's AFL and libphaser. There are a lot of AFL forks implemented on top of AFL. So, for example, I really recommend this kernel phaser KFL. And what is more important for us, there is WinAFL, a port of AFL for Windows binaries. Sorry. So AFL injects instrumentation routines during the compilation process. So the result in binary will have this AFL maybe log function injected in each basic block. In case of malware, we have one tiny problem. We don't have source code. So I guess it's not a surprise, right? Actually, we have even more problems. Malware usually unpacks and executes code, the most important part of malware, dynamically at runtime. In this case, source code instrumentation is useless. We have to find some way to be able to provide back to our phaser coverage of such dynamically unpacked and executed code paths. And we can try some other tools and techniques of automatic malware deobfuscation. But in my opinion, it's less scalable and works only for specific subset of samples. So besides that, if you want to search for bugs in CNC communication, we have to encrypt our test cases the same way as malware. So we have a lot of problems here. But thanks God, WinAFL doesn't need source code for binary phasing. So we don't need source code of our sample. Instrumentation is implemented on top of DynamoRio, dynamic binary instrumentation framework. What is dynamic binary instrumentation? I'll call it DBI. DBI is a technique of analyzing the behavior of a binary application at runtime through the injection of instrumentation code. I just want you to give a basic idea how it works. Let's say we have our DBI engine and binary we want to instrument. At step 1, DynamoRio launches this binary suspended, injects instrumentation library, hooks entry point to redirect control flow into instrumentation library and resumes execution. So at this point, it looks like a traditional, classic DLL injection and control flow hijacking. But at step 4, starts the magic. DynamoRio takes the first basic block, copies it, and in a special place called code cache, then it performs transformation of this basic block to be able to inject instrumentation routines, instructions specified by a user, and then execute it in this special code cache. The most challenging stuff is to make this execution transparent towards instrumented binary. And DynamoRio knows how to do this. It's a really complicated task, and they're doing pretty good. So then we take the next basic block, copy it in code cache, perform transformation, inject our instrumentation routines, execute it, take the next one, and so on, until we reach the exit point of our program. So you can see, this way we can instrument everything that is executed here. And on our CPU. So we had three challenges. Lack of source code, obfuscation, and encryption. WinIFL plus DynamoRio solves the first problem and actually creates a new one. WinIFL supports only file-based fuzzing. So we can't actually perform fuzzing of network traffic parsers, which is a very serious limitation for us. To address this problem, I decided to implement a patch on top of WinIFL and call it NetIFL. Suppose we have our fuzzer and our malware, instrumented by DynamoRio in memory. Let's assume our sample sends some request to CNC. Instead of actually sending it to CNC, we redirect this request to our fuzzer. Our fuzzer generates a new test case, encrypts this test case if it's necessary, and then sends this response back into our sample. Then we update our coverage bitmap, triggered by this test case, estimate code coverage, provide this code coverage back to our fuzzer, and fuzzer generates a new test case, restarts our sample or target function in our sample, and repeats all previous steps. It sounds like a bit complex scheme, but it's actually pretty easy to use. All you need is to specify IP address, port to listen on, and seed file. That's it. The fuzzer will do all the rest for you. If you need to encrypt your test case before sending them back into our sample, you can provide a path to your custom encryption library. NetIFL will load this library, and will use this exported function to encrypt test cases. If you don't like default CNC, you can define your own one. If, for example, you need to implement some really complex communication logic with your target. Okay, let's see how it actually works. Okay, here we go. So, in this virtual machine, I have a release build of NetIFL. But before we actually start our fuzzer, let me explain the code we are trying to analyze. So, it's Dexter version 2, malware designed to steal credit and debit card information from point of sale terminals. So, in this function, HTTP main, they have initialization. So, they generate agent string. Then they open connection with CNC using standard Windows API functions. So, they will use post request to send commands to CNC. Then it gets some information about victim's machine, and then actually sends this data via standard Windows API functions. If they successfully send this data, they call this function, getCookie. So, let's go in this function. In this function, they receive commands from CNC, coming back, via browser cookies. So, this function, internet getCookie, is used for this purpose. So, it's very trivial function. You just call it, and you get your command, and you get your cookies in p command variable. So, then they perform some fuzzing, they perform parsing of this command. And then, let's go back. If they manage to obtain it, and command starts with dollar, they are going to execute this command. So, this way they implement communication with CNC. So, as you can see, we need to implement some non-trivial communication logic with our CNC. So, we need to implement our custom CNC library. Okay. It's actually not hard. All we need is to define two functions. So, this is our custom CNC. We need to define CNC need that receives server port to listen on. Then we have, like, standard initialization of these circuits. And then we call listen on this port. And we have to implement the second function, CNC run. It receives test case from NetAFL for each generated test case that NetAFL wants to send in our sample. So, in this case, we have to accept this connection, receive this data coming from our malware, generate our response. We have to say for our malware that everything is okay. We receive your request. This is your response. And you can get your cookies. Then we generate cookie. There is nice function from Microsoft, internet set cookie. We generate this cookie on top of data provided by NetAFL. And that's it. So, this way we can compile this binary and provide pass to our fuzzer. Okay. This is command to run NetAFL. It looks like a pretty long command. But actually, it's not hard to understand. The first argument defines a pass to our custom CNC library. The second parameter is port to listen on. The third and fourth parameters standard AFL in and out directories. In directory, we should have seed file. Minus D specifies address of our DynamoRio binaries. Minus T is timeout. Then we have internal WinAFL arguments. You can find detailed explanation on GitHub. And then we have really important argument fuzz iterations. This argument tells NetAFL how many iterations should pass before actually restart the whole target sample. So, this parameter can directly affect your stability. So, this is very important. So, in my case, I choose 5,000. It works pretty well for me. So, and the last parameter is the pass to our malware. Okay, let's run. So, it's initialization. Everything loaded successfully. Initialized. In a few seconds, we will see standard AFL screen. Okay, here it is. It's not related. So, as you can see, our fuzzer stats looks pretty healthy. We have, we already found like two, six paths. We have coverage. Our stability looks pretty good, 97%. Execution speed is growing, but it's still very slow because we are running in a very slow virtual machine with no parallelization. But if you can leave our fuzzer like this for a couple of hours, we can find bugs. Okay. Let's start. So, if you leave our fuzzer for four hours with no parallelization, it will easily find several bugs in our sample. Okay, great. Let's see what I managed to find in malware. First malware I selected for my experiment was Mirai. Mirai is malware that targets IoT devices and uses devices as a part of botnet for large-scale DDoS attacks. This malware was used in some of the largest and most disruptive DDoS attacks in history, which caused major Internet platforms and services to be unavailable for large amount of users in different regions of the world. In 2017, source code of Mirai was leaked, and different Mirai-like botnets adapted this code and still operating in the wild. The fun fact about Mirai that it looks like authors even followed some security practices and used memory debugging tool electric fans to search for heap overflows and use after free bugs, which is a bit unusual for malware. Mirai DDoS capabilities are based on HTTP flood and several low-level network attacks. The most interesting part for us in terms of exploitation would be this HTTP response parser. Mirai needs to parse HTTP response coming back from victims to be able to perform HTTP flood attack. This parser has about 830 lines of code, hundreds of potentially dangerous operations with memory, with pointers, strings, so this is a wonderful target for our phaser. As a seed file, I decided to use this very basic HTTP response. I ran my phaser for 24 hours and managed to find 43 unique crashes, which was caused by a single bug in relative URL's handler. Execution speed was around 1,000 executions per second, which is pretty good, and phaser managed to find approximately 430 unique paths, which is also pretty good. What was the root of this bug? If our HTTP response contains a relative URL, this code branch is triggered. In case of incorrect relative URL, variable double i always equal negative values, which cause a memory violation and crash. This is logical error, so authors forgot to set up this argument to zero. They use it in code early and forgot to set it in case of relative URLs. So this is one example of a test case that caused a crash. So if you see that Mirai or Mirai-like botnet is trying to attack you, you can just answer this bot with this HTTP response and they will all crash. So let me show how it actually works. Okay. So in this virtual machine, in the right terminal, we have our... Just a second. In the right terminal, we have our HTTP server, our victim implemented in Python. So it's just a simple HTTP server. In the left part, we have debug build of Mirai. Debug build because we want to see what's actually going on. And in debug build, they're printing a lot of information, which is useful for demo. Okay, let's start our server. So I guess it's start and running. Before actually start Mirai, let me explain one thing. So before actually start HTTP flat attack, they need to connect with CNC. I was too lazy to deploy actual CNC server, so I decided to implement in this Python script CNC and just a response like random data to our Mirai sample. And surprisingly for me, it worked. So after dozens of attempts, it actually start HTTP flat, which was good. So it was like really easy solution for me. So to be clear, my Python server are now doing two jobs. The first one is to be HTTP response server victim. And the second one is to be a CNC for our Mirai to answer that everything is okay. I'm up, let's start. So okay, let's run. So you can see it start in debug mode. It tries to attempt to connect with CNC. And after dozens of attempt, it will receive some meaningful data and actually start HTTP flat. Okay, here it is. So it's starting HTTP flat. It's sending HTTP request to our Python server. And Python server is going to answer with this mode. This is the malicious HTTP response I showed in the previous slide. Okay, so here it is. We have segmentation fault and crash. So profit. This way we can stop Mirai to attack us. Okay, thank you. There is more bugs in different samples. So... I already presented this sample. When I was showing that AFL, this is Dexter version two. The first version of Dexter was one of the first known botnets that targeted point-of-sale terminals. As I already said in the demo, Dexter communicates with CNC or HTTP protocol via post request and receive commands over the response cookies. Actually, in case of Dexter, you don't need phasing at all. The malware code is so badly written that you can just answer with a long command and it will crash. And actually, it's remote code execution. You can find actually. Do you see this URL? It has 255 bytes. We can send command longer than this 255 bytes and it will cause stack overflow and we can explore it. And as I said before, there is no anti-exploitation techniques. It's like 90s. We can easily cause remote code execution. So, really old-school bug. TinyNuke. TinyNuke is a Zio-style banking trojan designed to perform manage the browser attack using web injects. It has a function loadWebInjects. The name of this function, I guess, is pretty self-explanatory. It is designed to ask CNC server provide a payload to be injected in the victim's browser and then this response is represented as JSON. In JSON format, it's then passed into our custom JSON parser. This JSON parser actually is very, very complex. They implement it from scratch. So, it's a very good target for us. This is example of seed file I used to feed in NetAFL. And after 24 hours of fuzzing, I had pretty good results. Three unique paths, 800 executions per second, and four unique crashes. The root of these crashes is this function. It causes infinite recursion in case of a very long response which contains only opening brackets. So, in my case, it was like 7,000. So, it will cause a very, very deep recursion, stack overflow, and it finally will trigger a crash in our target. So, last example. Kinz. Kinz is a banking trojan fully based on leaked Zio source code with some minor technical improvements. So, all bugs I found in Kinz are also related to Zios. This example has been used to attack financial institutions in Europe. It has HTTP response parser. So, I decided to use this simple HTTP response as a seed again. The trojan receives payload and commands from CNC over HTTP protocol. The HTTP response is then parsed by two complex routine analyze HTTP response. And analyze HTTP response body. And, again, it's a very interesting target for us. 24 hours of fuzzing get me 22 unique crashes which was triggered by one problem. This time, it was a bit more complex. So, function getMimeHeader is used to extract a value of content length from HTTP response. But if the string contains a value, if the string doesn't contain a value of content length at all, it causes an integer overflow. And, as a result, the function will return negative values. So, this getMimeHeader will return negative values. But then, copyX is called with this value returned by getMimeHeader. So, in my case, it was always negative 3. It will try to override the whole memory layout of our sample. So, if we try to copy with negative 3, it means that we are trying to override the whole memory layout. It's probably exploitable. We can sometimes control this negative value, but I'm not sure 100%. So, this is example of a crash case. We just need to send content lengths with blank value. Okay. Let's discuss challenges and issues. First of all, of course, in my case, I want to say that I skipped the hardest part. Reverse engineering and searching for target function. I took source code to reduce time of searching for a target. Of course, for real-world samples, you need to perform, like, initial reverse engineering of your sample and find this target function you want to analyze. Sometimes it can take a lot of time. Secondly, of course, there are bugs in WinAFL, NetAFL, DynamoRio, especially when you deal with highly obfuscated samples. Third, you need to find test case, so you need a seed file. Sometimes it might be really difficult, especially if CNC is down or you don't know what kind of file malware wants, so it might be challenging. Then we have a problem with encryption. So, sometimes encryption algorithm is very complex, and I know it might be really painful to perform reverse engineering of this algorithm. In this case, you can try to pipe. You can try to patch program just to disable encryption at all. It worked for me sometimes. And stability. Stability plays, like, critical role when you're doing coverage-guided phasing. So if your stability, if your coverage is always different when you send the same test case in your target, it means that your target is unstable and your phaser doesn't understand what's going on, actually. So it's sending one file and it causes different code paths. And usually it means that you waste your coverage-guided phaser to NASA. So you have to pay attention for this. I also found this tool very useful when you want to find the target routine in your sample. I have implemented this tool on top of DynamoRU. It's basically L-trace for Windows, but transparent towards instrumented program. So it will trace all API calls in malware and print this information in the file. So it's less detectable than standard API call tracer. So I hope, guys, it will work for you. You can try it. It's open source, BSD license. So take it. In future, I guess it would be really cool if we can somehow find this target function for our phaser automatically. Also, it would be great to increase stability. I implemented some heuristics to make it more stable. It worked for me. I hope it will work for you. And it would be great to have some code coverage visualization tool for your phaser. I know there are some of them, but it would be great to adapt it for NetAFL. And, of course, improve stability. Okay, so I hope I convinced you that bugs in malware might be useful. And you can really find these bugs using phasing technique. Of course, NetAFL can and should be used to find bugs in network-based application. So it's general purpose phaser. I designed it to be general purpose. You can use it to find bugs in benign software. So I recently found one CVE in network-based application for Windows. So you can try it. And I'm also currently merging this project with WinAFL just to reduce the amount of different projects on this planet. So I guess I'm going to finish this work in two or three weeks. So you don't need... In future, you will not need NetAFL. Everything will be merged with original branch, WinAFL. And soon, probably in September, I'm going to release NetAFL for Linux. I hope in September. Thank you for your attention. Thank you.