hello everyone good morning good afternoon good evening good night from the different parts of the world you are tuning into my talk welcome to my talk on your rules to rule them all thank you so much blue team village DEF CON for giving me an opportunity to present so today we're gonna be talking about writing yaras which can rule them all my name is Saurabh Chaudhry I'm currently doing my master's in cybersecurity I'm a published cyber threat researcher with around five years of experience in banking and financial domain I have multiple published research papers in IEEE Scopus etc I've been a speaker and trainer at multiple conferences like besides San Antonio Texas besides Budapest APSEC Indonesia Texas cybercon etc etc I have a background in red team in malware analysis and threat intelligence while I'm trying to buy with my career more into threat intelligence I love motorcycles and all the types of adventure sports which gives me adrenaline rush that's my passion so let's dive into the talk so today what we're going to be talking about is what is your rules how can we make Yara what are string and code based here rules and why code-based Yara rule is better and what kind of rule we will write which will rule them all in case you don't know to take it from the scratch. So malware comes into different forms it can come as a macros embedded in your Word document or a VB script PDF as a file and the list goes on and on and on and on. Malwares are deceptive and you never know what's coming and what file is what. Which is clean, which is malware, you don't know. So is there a one-stop-shop tool to deal with them all? Yes there is. Yara is your answer. So what is Yara? It's an open source tool to identify and classify malware samples or anything. It's a Swiss army knife for malware analysis. Threat researcher. It is free and open source. It was created by Mr. Victor M. Alvarez and it's maintained by VirusTotal. Modern-day IPS, IDS, prevention, firewalls, CMs, they all support ingestion of Yara rules to find unusual activities and matters. And you can do almost anything with Yara rules. So each Yara rule consists of a set of strings and a Boolean expression which determines its logic. So what can you do with Yara? There's a lot of things you can do with Yara. You can identify and classify files and malwares. You can find new malware samples. You can scan online. You can find live data streams and live network streams. You can help speeding up the incident response process. You can track future malwares as well based on the code reuse pattern, which we're going to be talking about later in the talk. You can use Yara rules to track APD groups. You can build your own anti-malware product with the help of Yara rules. So it's a one-stop shop if you are into malware analysis or threat hunting. So how does Yara rule looks like? In case you have never seen a Yara rule, this is how it looks like. This is how it's run. So name of the rule and the first directory against which it needs to be searched for. And there are different optional, there's a lot of different options. There's a lot of different options. There's a lot of different optional arguments, which you can use, like finding it recursively, multiple threats, et cetera, et cetera. In case you have never seen a Yara rule, this is what it looks like. The rule, name of the rule, the metadata, which is important so that you can keep a track of what's what and the strings. Now, there's a lot of things that you need to know about Yara rules. There are different kinds of string, which we can use. We'll talk about that later and the conditions. So what's happening here in the rule? So in this rule, they are, this is, this rule is trying to find the word DEFCON in any case, jumbled case, upper case, lower case. And if anything has this S key, it will flag. What Yara is consist of? It consists of three things, as I told. A metadata to keep things in track, strings and conditions. So strings here, you can use different, different types of strings here. You can use text strings to identify, you can use hexadecimal strings, hex, that is the fun part. And the regex, which is not generally recommended. And at the last of the rule, there are conditions which you define for the matching based on the strings you have written. So writing a Yara rule based on a unique string identifier doesn't take more than 10 minutes. A good Yara rule will consist of bytes and not just strings. And you will find those juicy bytes based on the critical thinking, thinking of the functions and the code reuse and the program flow. If you want your Yara rule, you can use the code reuse and the program flow rules to last generation. Write rules based on code reuse. A rule which only matches a single malware files is no better than a hash. For example, Gantt-Krav has like five different versions and with one rule based on function reuse, you can find all those different version of Gantt-Krav. So when a malware mutates or the threat actor writes a new version of malware, they reuse code and functions. And that is what we going to leverage to write Yara rules. Things can change, but code reuse are more probable to hit. Okay, you cannot use these keywords as they are preserves. Most of them are reserved for providing the Boolean logical expressions like SK, N32, UNH16PE, Y, XOR, et cetera, et cetera. Okay, how you use commands. So commands on Yara rules is just like how you command on C. Both single and multi-line commands are supported. C style commands. Basically it's C style commands that are supported. You can see this is one single line command and double line commands. Multi-line commands on Yara rule. Yara is case sensitive, so you have to take care of case. In case you you want you want to write a Yara rule and you are providing a you are providing a word and you want both the strings about the both the case to be supported you have to write no case this is how you this is how you add text strings horizontal tab so it can contain these following subsets of the escape sequence available in the C language double quotes backslash horizontal tab new line FDD for adding Biden hexadecimal notations as we talk the no case modifier for example here you can see the rule trying to identify Fuber so here adding no case will identify Fuber Fuber with uppercase and Fuber with jumble case coming to the white character strings a white character is a computer character data type that generally has a greater size than the traditional 8-bit characters so the increased data type size allows for the use of larger coded character sets so using white will solve your problem here XOR strings coming to the XOR strings there are two rules so you can see these are two rules which are basically the same so if you write XOR beside the string which you have written over here this will be similar to this strings so remember for an efficient rule write small and write logically for better and long-term detections if it your rule you can only detect one single file then there's no better than using a hash write it in such a way it detects logically we have base 64 strings it is used in rule just like the XOR it will detect even if it finds at the byte level of the code as well so let's see if a CNC address that is base64 encoder and you can use the expression like this so this real will detect all permutation of base64 encoded string hexadecimal strings so hexadecimal strings allows three special constructions that make them more flexible this is the fun part 0 to 9 a to F as you know hexadecimal they allow us to use special constructions in the rule to accommodate more and more logic wildcards jumps alternatives so we will try to focus our rule writing more on hexadecimal strings than normal as key strings writing an efficient error rule is ragged is not so recommended for writing because it it comes with a lot of false positive so we'll try to avoid reg X writing rules on string or bytes not right X so I mean write rules write rules but do not include much reg X because it will provide you a lot of it will give you a lot of false positive so efficient rule doesn't use reg X coming to the conditions so most of the malware target window systems they are PE files and you can dissect them you'll find they're having headers sections so you can can you can give arguments and conditions to your errors based on the executable entry the string offset or their virtual offset their file size their entry points and a lot of other things so for example if if if there is a PE file which is less than X Y Zed KB so you can define match this now X this is the end at the entry point match the file which has an entry point like this and the file size less than X Y Zed kilobytes or based on magic number based on there's a lot of conditions which you can implement so why code base rules so as you can see the virus mutates and with every mutation and it has changes and so are their strengths so these malicious threat actors work just like software development companies they reused codes codes functions logic program flows so writing one efficient code base rule will also detect future malware so you can see these two these two programs here they are printing hello so here the hello is directly written in the first program the first first picture and the second the hello is broken down into different string and then it is adding together and printing it so if this if I write this rule based on strings it will only detect the first one the first program not the second program so that is why we need to focus more on writing error rules based on code reuse so for example GrandCab has like five version and if you if you made a error rule based on when GrandCab 1 came out based on the code reuse of the GrandCab the function reuse you can detect even GrandCab 1 2 3 2 3 4 & 5 with one single rule so you can find future malwares as well with with your rules if you write it efficiently you can run rule which will rule them all on the basis of their code reuse pattern code based rule requires a little bit of understanding will lead a little understanding disassembler and debugger to write code base real code based rule last for generations so that's what it is recommended so rules that match only to the specific samples are not much better than the hash value making efficient error rules which can last and you can track the future malwares from same malwares creators writing one rule which will rule them all so things to take care while creating code based rule so compilation flags different compilers work differently so use wild cards use use wild cards where you can and one thing to remember is XOR and EAX can produce different opcodes so write it accordingly coming to testing era you should definitely do the following checks to reduce false positive like scanning the malware samples than scanning them big good wear sample if the rule match to the malicious sample and did not generate a match on the good archive your rule is not good enough to go into practice because it has a false positive so how do you test this sample? creates Kaspersky great team has a project named Clara this is a very nice project if you want to test your before making go to production this is the lifecycle of your rule analyze identify write rules test rules deploy your era and enjoy so where you can hunt your arrow hybrid analysis malpedia one is total otherwise total will need a premium license for that hybrid analysis you can do it from free and there's a new one which came into the picture recently era 5 from a build CH product project so this is really good this is where you can't hurt so what you can do with era again guys find next-generation melaers hunt for a PD zero days you can monitor a PD groups you can make your own area you can combine it with Zeek two you can create your own AV you can combine it with Zeek to monitor it with make an intrusion detection systems so there's numerous case in the past where people have identified zero days with the help of your rules so conclusion writing code based rules needs understanding of debugger but it's the efficient one string is not the best thing to write while writing your rule cause enough and in some time it will become absolute so always look for code reuse functionality and bill your arrows on that thank you so much guys I'll take questions and feedbacks thank you so much blue team village for this opportunity please let me know if you have any questions and feedback thank you so much