Alright! I am really excited to introduce Alexandre again. He spoke in my track last year and did a bang-up job. Last year was his first time speaking at any kind of big conference at all. Since then he's gone on quite the world tour. He went to China, he went to Hack in the Box, he spoke in Europe, and now he's back here to speak again. Let's give Alexandre a big round of applause. Have a good time, my friend. Thank you so much. Yeah. Hello, guys. Good afternoon. Let's talk about .NET malware reversing. It's a pretty easy talk. I think so. I'm a security researcher. Here is our agenda about my presentation. Here you have my last presentations on Confidence, Defcon China, Hack in the Box, Amsterdam, and Defcon last year. I recommend you take some time to read it. In the last line you see my tool, my personal 3H tool, malware view tool. If you have some free time, you can test it. Okay. Let's start. There are lots of people here. Honestly, once again, I was expecting 10 or 12 people. Because most malware analysts don't like .NET malware. Basically, malware analysts while analyzing .NET malware are concerned to unpack a binary from memory or find the encryption routine in .NET malware or even to decrypt resources embedded in this kind of malware. According to my experience, .NET malware can be so challenging because sometimes advanced authors, malware authors try to modify the .NET malware, the application directly in the intermediate language. And I've been I've been checking and analyzing some malwares in .NET which try to attack the .NET runtime. So it's recommended to learn a bit more on intermediate language and some information about metadata in .NET runtime. Most of the time we find .NET malwares around the world and we have base usually native code sorry .NET manage code uh uh native code on the memory. This is the common scenario. But sometimes things can be worse. Sometimes things can be a bit more complicated. You see, this is a real case that I faced some month ago. The suspect received an email, clicked on the link, dropped uh uh a first dropper. This dropper uh fetch uh first payload on the internet. This payload is composed by two pieces. One piece is a native code. The other piece is a encrypt manage code. This uh first piece in native language, this code is injected in into another process in a remote process. Once this code is injected uh this code loads the first manage code decrypt and load. This manage code fetch a second stage from the internet. The second stage is the real payload and the third stage tells us the code is a network. So, uh uh uh uh uh uh uh code into the memory. In this case, for example, I have, I use the CLR create instance to get a interface named IclrMathHost. From this interface I use the getRuntime to get another interface named IclrRuntimeInfo. From this interface I use a new method named getInterface to load the runtime. After, afterwards I use the execute application to activate the domain. I start the runtime using the start method. And finally, I use the executing the full application domain to start the real application, the real infection, the real payload. So, this is the end of the demo. As you know, a .NET framework is composed by a runtime, CLR, and some libraries. Uh, malware authors, uh, can write, uh, malwares using different languages, uh, such as, uh, C-sharp, F-sharp, uh, VB.net, and so on. Basically, uh, these source codes are compiled into a common language in, uh, infrastructure and, uh, for the, uh, for the finally run by the CLR. Uh, we, we have several excellent tools to analyze .NET, uh, malwares. I try to list some of them here. I use them, uh, daily in my daily job. Personally, I also use some, uh, additional tools that I try to show you here in red, uh, .NET. Uh, these are great tools to analyze .NET malwares and reverse some of them. Uh, of course, uh, you must know that most .NET, most .NET malwares, uh, use, uh, some kind of packers or cryptors. So, we, I, I, I try to show you some of them here. Uh, pay attention that the last one, VM protect 3.40. Uh, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, uh, was released, uh, uh, I, I believe one or uh, two weeks ago, uh, and now, uh, it includes, uh, .NET obfuscation. Uh, when you are, uh, working, uh, with .NET malwares, uh, you should, uh, or you must try to remember that most of them, eh, are using obfuscators and obfuscators can. Obfuscator, uh. Obfuscators are only putting what is best for, you know, a good the control flow. This obfuscator try to rename methods, attributes, classes and so on. Some of them uh encode all of the strings of course. Uh try to encode and uh obfuscate the cross reference. You know, it's so complicated to analyze this kind of uh code uh while you're using uh any kind of obfuscators. Uh so it's very important that you know about that. When uh when I'm analyzing a .NET MAUI, usually I see the same functions load the payload. In this case, I try to show it in green here. Uh assembly dot load, assembly dot load file and the method info dot invoke. It's almost, almost, almost the same things. Uh uh most time it uh we see a kind of uh reflection approach. And uh when you see a uh reflection approach, you will see a sequence similar to that. Uh I'm using load or load file followed by get type, get method and invoke the method. So, reflections is uh is so used uh during uh .NET MAUI's. So, uh it's a very common approach. Another approach, another possible, possible approach is using get assembly name plus get type, get method, invoke again. As most MAUIs have been using resources or uh these .NET MAUIs try to hide uh some DL or uh payload into resources. You see uh sequences like this one in pink. Uh find resource, size of resources and so on. Uh sometimes uh you see uh other sequences like this, this one in orange. Uh this one is the when uh malicious .NET assembly uh uh is trying to load some external DLL. All of, all of these approach are based on uh trying to load something in run time to evade your static analysis. Here I, I'm showing you uh first example of a, of a static analysis. So, uh first very basic malware. I opening DNSPy. We have the entry point. Uh there in green. I click now the entry point. I find a class named the Pineapple Buffer Cache Event Source. Inside this class I see the main function. And, and at the bottom I see the main function. So, I see our potential malicious function. Name it target invocation exception. Following this function I see that this, this very special function is trying to load uh an assembly. And uh this function are using a URL to load this assembly. But in this case the web link comes from several attributes. For example I took one. The first one. Uh and I check that this attribute is being set by gearing uh new assembly using get execut assembly here. At the bottom. This function is trying to read a resource. This resource is encrypted. This is the decrypted. And when you decrypt the resource, you see, uh, PE is executable. So, uh, each, uh, each of these, uh, variables are, are related to a resource that represent a possible or potential malicious native code. So, uh, uh, uh, uh, uh, uh, uh, uh, uh, uh, uh, uh, uh, here, uh, I'm showing the same code, but, uh, using intermediate language as, it's similar. You see the main function, the endpoint. You see, at the right, our function, our malicious function. We have a token. Uh, token is that number 06. And, uh, uh, uh, uh, uh, uh, the stock, uh, works, uh, a kind of index inside, uh, metadata table, name it member reference table there. And when, uh, when I try to, uh, fold it, I find, uh, uh, our function here at bottom. And more details are expanded. As you see, implementation flags are, uh, I also L, intermediate language and manage because I'm handling a manage code. This is a, this is a manifest. A manifest in this case is a file that will wrap up everything. In this case you can see the assembly name, you can see several manage resources related to uh embedded research and at bottom you can see further information about this malware. As I mentioned before this point uh this malware is using reflection to load malicious contents at run time so you can see here several calls to get execute assembly methods in several points in this intermediate language code. Sometimes you, you are facing a bit harder uh .NET malware and this malware uh brings some uh encrypted resources so you can use DNSPy or other uh two. Drop this resource and you can see that uh this malware is using the same code as the scripted and using reflexo. Reflexo is a kind of plugin. You can add the original code and load the resource, the scripted resource to analyze the malware. In this case you must to remove, you must remove the old references because the old reference are reference uh the scripted resource. So in this case you drop the resource, decrypt, edit the original .NET malware, load again and that's it. You can analyze your .NET malware uh using this uh this technique. Of course it's always recommended to know a bit more uh intermediate language and of course metadata. I, I, I think that about 9% of case .NET marvers or .NET malware authors trying to insert some malicious code in R initializers with .NET malware. I, I hope these data are or finalizers. In this case, for example, usually I try to check, uh, every, uh, class constructor to find or, uh, unpacker, or decrypter, or any kind of, uh, hooking code. It's very usual, for example, in, in the last few weeks I've been seeing, uh, several dot net malwares trying to compromise the JIT. Uh, in this case, this, uh, this special malwares, this special advanced malwares trying to, uh, hook this function compiler, uh, sorry, compile methods, uh, to compromise the JIT. In this case, when the JIT, uh, generates the native code, uh, this, this special malware can hide or, uh, hide the JIT code. So, uh, the malware can never, uh, encrypt user code in runtime. For, for example, in this case I try to see the class constructor. There usually I found, uh, I find some, uh, some, uh, hooking code. This hooking code tried to compromise the compile method and by, uh, using this trick, uh, the malware can or hide or encrypt the user code. It's a very config way to strip mate. clever technique. There are other, uh, .net, uh, details here. Most .net malwares bring some very important, uh, metadata. So, metadata basically are information about classes, attributes, members. Usually, uh, usually you, you, uh, get very valuable information from metadata. Uh, a .net application, as you see, is composed by a managed equitable and a managed code. It's not so hard to analyze this kind of code, but you need to, uh, have some, uh, uh, some guidelines to do that. For example, a managed code is composed by, uh, a p-header, of course, clr-header, metadata, uh, and, uh, code. Uh, the, uh, the code is, uh, data and intermediate language. It's case. I have the compiler here. Uh, I, I have, the managed, uh, model, I have , uh, resource files that, uh, are compiled to a final version of, uh, composed by a manifest, a managed model, and a resource file. Uh, uh, resource file. And you, using pictures, you see that, this is the part that i have the, this gently, uh, uh uh dot net header is composed by P header, CLR header, CLR data and native code. And at the right side you can see the same picture but in a detailed view. Metadata is very important because there uh I can find very uh valuable things and information. Metadata in this case is composed by definition tables, reference tables and a manifest table. Uh there are several tables inside of these uh these classes. And you should remember that all the dot net uh malware infection try to use one of these tricks or code manipulation inside the class constructor or f- f- analyzer. Uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh uh try to load some uh a function, a function, or use a uh component. So the tricks are the same. Always the same. Here is a second case, a bit more complicated case. This is uh real dot net malware. I'm I'm showing here the intermediate language and the met data information. As you see, you see the name, type def reference table, you see the method name, in this case man, and the flags. In this case I, I'm using the IODASP to show this screen. As you see the real managed code is so small, less than 5%. As you see the real managed code is so small, less than 5%. Metadata is so important because as I told you, describes some information about members, attributes, properties, classes and so on. And this metadata is organized by using a kind of relational database. And it's so interesting because you see I have a table, 1, 2, 3, 4, 5 slots. Each slot is dedicated to a class. And each slot tells me which methods belong to this class. That's right. Uh, this metadata are organized in named streams and classified in metadata, in metadata heaps and metadata tables. Meta, uh, metadata heaps is a type of metadata heaps is so easy because there are, uh, our only concern is about strings. So a string heap is more important for us. And inside these six named streams we have, uh, hash US, uh, is related to user strings and hash dash is a kind of uncompressed metadata stream. So I, I believe that, uh, these, uh, two ones are the most important. When I talk about, uh, metadata tables, uh, we have several tables. It is almost impossible to remember everyone, every table. You see, I list all the tables here. And, uh, since here works using a token. A token is a kind of structure, composed by four bytes. The first bytes determines the table. The remaining three bytes tell us the row inside this table. Here I show you, uh, some statistical information about metadata. And at bottom I show some user strings that I commented about the named strings. Intermediate language in .NET is a kind of, uh, ASMI language based on stack. So you see several instructions. Uh, load something onto stack or pop something from stack, from stack. You see, uh, a very usual application or tool named NGIN. Uh, NGIN is a very useful application and NGIN is, uh, executable to compile, to compile, uh, intermediate language code into a native code. It's so easy to do that. But, in the last three years, I've seen several mowers trying to attack the .NET framework. In this case, uh, this kind of, uh, infection are a bit more complicated. We have several, uh, we have several, uh, kinds of .NET mowers, but assemblies are classified in private assemblies and shared assemblies, of course. Uh, you should remember that most mower samples, uh, try to hide some malicious content and resources. Hide by encrypting or obscene some .NET mowers, then some .NET mowers try to download, uh, an external assemble from somewhere. And other, uh, .NET mowers are based on multi-file assemblies. In this case, it's a bit more complicated because these authors try to compose the .NET assembly in, uh, and a split over multiple files. Uh, and during this time, all the battiest stuff got dynamically crushed. Uh, a- after one changing that you're getting more and that new process is different then another net happen by adding righteren uh to evade the defenses. It's so easy to generate a multi-file dot net malware. For example I show you here a step by step. In this case it's so easy uh I compile for example a hooking dot cs model. I compile a injection dot cs and using uh a main program named defcon dot cs I generate a library named uh malware lib here. It's so easy to do that. Of course this operation touch several metadata tables but it it's normal. Here uh we have a real manifest about our second malware. You see some native models being loaded at bottom. Some external reference to assemblies and at this case in sorry in this case our uh our assembly is a strong assembly because uh it uh it is uh uh digital uh it's signed by a private uh key from somewhere. The second screen here uses the uh the uh the uh the driver uh the assembly name. You see several custom attributes. You see that uh the signer is using SIG SIG one algorithm. You see several managed resources being used. And uh at the bottom you see that the uh the uh the uh the uh the uh the uh the uh the uh the uh the real name of this malware is Microchip Realtek driver dot z. I show you here how to compile a final executable in C sharp. It's pretty easy. If you have a key pair you can sign uh disassembly by using this procedure. Here at bottom. After signing this dot net assembly you can see the public key. There in red. During some infections uh I've seen uh native uh native uh uh codes trying to copy uh dot net assembly to GEC. GEC is global means global assembly cache. In this case the author uh was trying to execute the native code, copy the manage code to GEC because other pieces of this malware uh would will uh would use uh the this shared assembly in the GEC to continue the uh the infection. It's a kind of second stage. It's pretty easy to copy to that uh because you can use GEC util there in orange. Or you can use the MICL package to do that. Of course uh to copy anything to GEC you need to have uh key pair to sign your dot net assembly. Uh uh uh uh image. Uh calendar. Ultrasonaut or you you need to have. If you don't have one you can use the only the public key and trying uh delay signing. In, in this case you will uh sign the dot net assembly only use the public key. Uh to do that you can follow this procedure. You can add some resources by using slash resources. You can uh add some references by using slash references, it's pretty easy. Of course, uh, I don't have enough time here to show you, uh, how to, uh, uh, to explain every single instruction in intermediate language. So I left some, some slides here showing each important instruction in intermediate, in intermediate language. Uh, I don't have enough time to do that, unfortunately. And here, I show you, uh, real, real malware, uh, is the same malware. So I try to comment several lines. Of course, you must know about C-Sharp to understand this code. Uh, here we don't have anything different from, uh, C-Sharp code, but, uh, the names and, and the keywords are a bit different. You have, uh, public, uh, class, you have a private class, you have a CLID class, you have a constructor here, uh, cpar, you have some private fields, we have, uh, some stack reservation there, max stack. You have several operations load things to stack, and pop up the things from stack. You have a custom, a custom instance here at bottom. You have private class, you have, uh, uh, invocation of a native code in, in the middle here. You see, uh, pinvoke implementation here, trying to call, uh, a managed code. You have here, at bottom, uh, an, uh, local variable being initialized with zeros. We have a try, uh, try catch, uh, structure. So, it's so similar to, uh, C-Sharp code. I try to comment several lines here, uh, to help you later. You have, for example, here, heh, our, our .NET malware are, are, uh, uh, interested in get our, uh, keystrokes. You see, uh, uh, in this case, malware is trying to hook, uh, some, uh, keyboard operations. Uh, as, as you see, uh, the .NET malware is trying to hook the key down event and key up event. So, uh, it's a kind of keylogger. At bottom here, we have, uh, some event declaration. Uh, in, in this case, our .NET malware is trying to subscribe some functions, uh, on, on this event. Uh, at bottom, key down, at up, uh, key, uh, key up. And finally, uh, we have, uh, uh, uh, uh, uh, uh, uh, uh, uh, uh, uh, uh, or .NET malware using aggregate, uh, uh, uh, to invoke this event. So easy to do that. Several, uh, constructions being called here. Oh, finally, I found the decryption function. This function is re- responsible for decrypting several strings from this malware. As you see, I try to comment line by line, or the most important line here. Uh, you can, you can try to monitor all DLLs load, uh, from the GAC by using first lock or even process monitor. But, uh, as I told you, uh, uh, some advanced malwares try to compromise the, the, the .NET framework. Uh, try to insert some hookings. Uh, even trying to compromise the JIT. So, it's, uh, uh, this kind of malware are more dangerous. Uh, here, at, at bottom, I show you the complete procedure. For example, I can copy a DLL from GAC, modify, and, and, and, and, and, and, and, and, and, and, and, and, and, and, and, and, and, uh, recompile, uh, to the native code and copy back again to the GAC. It's so, it's so easy, pretty easy to do that. Of course, uh, nothing is so simple to do that. I need to, I, I need to, uh, have, uh, sign assembly. And as I don't have the private uh key from the Microsoft, it's so complicated to do that. Uh other approach it would be to uh resign all the .NET framework it's so hard to do that. I can try to uh copy the the modified DLL to GAC but if uh some applications or services uh are using this uh DLL it's not possible. I can try to force a reboot to make this copy and even that I get it to do that I need to force uh my current applications using this modified DLL. So in this case I need to uninstall and remove the native uh uh code from the native directory. KSmith uh show a very uh interesting way to uh to do that. KSmith uh uh uh show a very nice trick by by copying by dropping assembly to gpath-tasks.dll to force uh applications using my modified DLL. I can try to for example I have uh I have a register uh composed by two entries. One entry uh is pointed to uh the assembly code. The other entry is pointed to the native code. In this case I try to change the second entry to uh to point to my modified DLL is uh another approach here. Some uh some .NET malwares try to co-compromise services, try to uh introduce some hooking and git. Some DLL uh some .NET DLL try to load the external assemblies. So there are several tricks. Uh I can try to uh force uh I've been using win-the-back to analyze uh this kind of .NET malwares uh using uh an extension like sl-sax. And uh here I show you a complete approach for example here here I loaded the extension sls uh uh sos.dll. I found the native enterprise uh uh the uh the the manage entry point. I disassembled the entry point. I found the PE entry point there by using dump bin. Uh I try to display the pointer and finally I find uh the core easy man uh function by disassembly. I can dump the domain. In this case uh I see the system domain and I see the shared domain. I see our domain that uh from our malware. I see several calls to external assemblies, visual base, sitting the DLLs, forms and so on. I list uh some manage exception. I can try to switch uh the track for example. In this case I switch to thread zero. I dump the the manage stack trace here. I found our dot net uh uh malware at bottom. I list some manage tracks by using uh bank tracks here. As you see you can see that three uh tracks uh tracks zero, two, five, ten and fourteen are manage tracks. Uh all of them are managed by garbage call collector. I list the native the native uh stack at bottom using key n. I pick up uh an entry there. I check that if this address belongs to the JIT code. Yes in this case belongs. I pick up the method descriptor reference. Uh uh uh I disassemble it. So we have the disassembly uh uh function here. I check the manage stack again. I pick up uh object address. I check the again uh uh if this address belongs to the JIT. Yes again. I dump the method descriptor information here. I pick up the class reference. At top I dump the the EE class uh structure information. I found the I found that this specific class has six methods, twenty-eight instance fields and five static fields. I click on it. I see my methods here. I see my methods here. I see my methods. I see my methods. I see can check one of these fields for example in this case a value type field by using dumpvc. I found a hash table class composed by several uh several fields here at right. I dumped the math table information. I dumped the math table information again but uh detailed output as you see several uh methods some of them predicted, pre-compiled. I dumped information about uh a specific model. I found the assembly reference. I dumped the assembly uh information. I found the parent domain information. I dumped the domain information at bottom. It's pretty easy to do that. Well of course here, here you can see it's pretty easy to do that. You can see it's a bit more complicated. Uh finally I start again. I I found the math table structure and uh e structure here. I found some interesting uh classes uh possible malicious classes. Possible malicious classes. I I take one, testprint dot client. I dumped some information from this class. As you see there are several methods there. I pick up one. I dumped the class and it's very interesting because there I I found some uh some uh fields in English, Portuguese and German language. I I took the German one, changed it to English and then to check the information. As you see uh this part uh wasn't executed by git because at bottom is gitted no. So I tried to put a breakpoint to a future execution. I dumped again all all of the stack objects. I saw uh uh a function using uh an array. I dumped this array. This array is composed by one element that in this case is a structure as you see at bottom. I dumped okay that's that's the same slide. I checked the heap information. I checked any kind of references to these objects by using gc root. References from uh references from the stack uh from the handle tables. I check the finalized heap to find something interesting. I check the find a string inside the hip, the manager hip. Uh, at bottom I dumped the, the memory from memory. And finally, finally, I checked the thread environment block. I dumped some strings from there. I found some nice strings at bottom. Uh, I dumped all of the hip. I found, I found, uh, some, I, I, I found some, uh, classes. I took one. Keyboard hook. And so nice. I dumped methods from this class. There are several methods. I took the last one. Tried to disassemble it. In, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in, in. I found integrated language. Tried to dump all strings from the memory related to this, uh, method. And it's nice. I found several ULLs related to banks. So, this is a kind of Trojan banker. Uh, I, I'm leaving some break points for, for you here, these are, are very nice breakpoints to test, to gather some additional information. And I test the, the URL, uh, in virus total by using my overview. I can see that is a malicious site. And I test the domain and I found several related things, several malicious things, uh, associated to this domain. It was a very, a long presentation, I know about that. If you want, I can, uh, I can expand details for you. Uh, thank, thank you so much to the DEFCON staff, as usual, for you, uh, who, who reserved some time. And have a nice day. Thank you. Wonderful job.