Rusty Noob Blog

A place where I can keep track of my blogs

View on GitHub
7 April 2024

100 days of YARA Challenge

by Yashraj Solanki

image

Background

Recently, I completed the 100 days of YARA challenge which was originally started by Greg Lesnewich (Twitter Handle: @greglesnewich) and has been going on since 2022. I thought of sharing my experience and maybe motivate a few to take on the challenge next year. For those who do not know about the challenge, it is essentially that each day for a total of 100 days, you write and contribute a YARA rule that you have written. Feel free to check out more details about the challenge here.

Prior to this challenge, I had only heard about YARA and never wrote a single rule. To make things even more challenging, I only started the challenge 16 days later and had plenty of catch up to do (backlog of 16 rules). At this point, I was simultaneously trying to learn YARA and write a rule each day along with my work which was pretty difficult to begin with. Having said that, I would highly recommend everyone to give it a go. By the end of the challenge, there were only three people who successfully completed it.

In my opinion, as far as YARA goes or for that matter anything new that you want to learn, you can go through the entirety of the official documentation, watch videos on it, read blogs and what not. But if you do not start writing your own YARA rules, you will not get any better at it and this is exactly what the challenge enabled me to do. By pushing a commitment to write a rule each day, you are building an appetite for it. When I look back at the rules I started with versus the rules written towards the rest of the challenge, I could see myself improve bit by bit. From writing string based rules to using various modules in YARA and then transitioning towards writing rules based on malicious behaviour (TTPs).

This is not a guide to writing YARA from scratch (there is plenty of content available online talking about the syntax, examples and much more) but rather special considerations while writing YARA and hopefully pumping you up to get cracking with it.

Here is my advice to those starting fresh with YARA:

There are a bunch of guides available which can give a big boost to your YARA rules. I have shortlisted some of the ones that you can easily add to your rules to boost their quality.

I would highly recommend checking out Florian Roth’s Part 1 and Part 2 blogs for a more in-depth understanding on writing better YARA rules.

Just by doing the above, you can improve the quality of your YARA rules

Considerations while writing rules

There are several factors that will influence the kind of preference you will have while writing your rules. To break down some of the main ones, have a look at these:

Side note: I had to trouble downloading the samples from VX underground onto my windows machine because Windows does not like filenames containing non-ASCII characters which was quite annoying. I also eventually ran out of storage on my VM which had around 80 GB allocated to it. It might be worth considering deploying YARA on a linux system. This is also a safer approach since most of the malware would be for Windows. I have heard that it is more performance friendly (not sure) but more importantly might fix the issues I had while running YARA on Windows10.

Work Flow

There is this one meme that comes to my mind when thinking about the work flow and what would be considered a successful YARA rule (Meme credits @lauriewired ).

image

At a very high level, your YARA rule should be :

  1. Write a YARA: Yup, this is a very obvious step.
  2. Error Free: It should compile without any errors, you really do not want to be distributing YARA rules that won’t even run in the first place.
  3. Match against more than one sample at least: If your YARA only matches against one file, you got to ask yourself, what is the difference between my rule and a file hash.
  4. Test against Good stuff: It is one thing that your YARA matches against sus files, but does it also match against the good stuff (system files which are not malicious in nature). This is a common beginner mistake as you try to familiarize yourself with YARA and what bad looks like. For instance, in the beginning, I picked strings for my rule which were found in good ware or those which are generated during run time (these wont match again on other samples). By being aware that these things can happen, you can very effectively reduce FPs.

Write a Rule => Check for Errors => Test against Bad => Test against good

Tooling

While I will be talking about the tools that can assist you in writing YARA rules depending on what you have access to and the time constraints, it is crucial to understand that methodology is primary and these tools are secondary. Without having a game plan in place, you might not be able to benefit from the different tooling that are available. Focus on your approach and try to think of a methodology that you can build which aligns with the work flow.

I might write a simple blog highlighting what approach I had taken towards writing YARA and also share a high level methodology behind the approach taken

Old School

As I briefly mentioned above, I would highly recommend to install YARA locally. But this can be cumbersome. You need the time, patience and storage to accommodate the requirements that come with this setup. I am pretty old school when it comes to pretty much anything. I prefer holding a book rather than reading the same material online (even though I read slowly and the average number of books I have read in a year is less than 5). This setup really allows us to understand the true capability that YARA brings to the table. But it does put you in a tight spot when it comes to the initial setup and the time taken to complete it.

Pros:

Cons:

YARAify by Abuse Ch

Wouldn’t it be cool to see how your rule performs against the samples found in the wild beyond your malware corpus. This is definitely within your arms reach. This amazing service by the same guys who brought you malware bazaar is really handy to test your rules out. Having used malware bazaar to gain access to malware samples and using threat fox to grab indicators for C2 hunting, I have always been a huge fan of Abuse Ch and the fact that they are community driven is absolutely amazing.

Here, have a look at the stats of my YARA rules from the challenge, you can also check out my YARAify user profile here

image

I am planning to release a separate blog on YARAify as I am absolutely in love with the platform and think that it will be a gold mine for beginners.

Pros:

Cons:

Hybrid Analysis

Similar capabilities to YARAify, this service allows you to scan your YARA rule instantly (retro lookup). However this comes with a drawback, no modules are supported using this service which makes the usage quite limited to string based rules only. You could treat this as a reinforcer that string based rules are still OP (Over Powered). In fact why not test your skills out by converting some of the module based rules into string based ones.

Pros:

Cons:

Klara by Kaspersky

This utility allows a hybrid setup where you install the python based tool on your system and not having to worry about malware compromising your machine. This is basically a querying service but installed on your system. I personally have not tested it yet, but looks very promising. Here is the link to their GitHub.

Pros:

Cons:

Virus Total Intelligence (Premium)

This is a very convenient alternative to most of the tooling mentioned so far only if you have this service deployed in your organisation. Virus Total has both the Retro hunt and Live hunt capabilities. Both have the same online editors to write your YARA rules in and also validate the syntax. In order to test your rules, all you need is the file hash. You can test up to 50 files in a single scan which is plenty. In order to test the syntax and primary matching against known bad as well as good, this is a really solid option.

They also have a good ware corpus and running YARA against it does not cost credits neither does testing your rule in their editor. VT has the capability to fetch the strings (ASCII and Unicode) from the sample along with a Hex view. VT also has a custom module “vt” which has opens up YARA to a whole lot of other stuff such as dynamic analysis, network based characteristics, pivoting between different file types and much more.

Even beyond VT intelligence, Virus Total can be used t conduct initial recon against files such as looking at their PE characteristics (provided they are a windows exe or Dll). This is a useful feature which eliminates the need to run the PE module against a sample locally and the best part is that this feature does not require for users to log into VT.

These are some of the VT based YARA rules that I wrote during the challenge:

I might cover the capabilities of VT Intel for YARA in the future as it has a customr VT module which opens YARA to a lot of interesting use cases, let me know if this might be useful.

Pros:

Cons:

As you can see from the above, each tool brings something different to the table and ideally it would be a fantastic idea to run all the above tooling in order to cover maximum ground and capitalise on the various limitations. I would like to propose a more practical setup and this goes back to the work flow that I briefly mentioned.

  1. Error Free: Grab any text editor that you prefer to write YARA on. For me, the winner is VS Studio Code since it has a neat YARA extension that you can install which makes syntax editing easier which is very useful to debug any errors in your YARA rules.

  2. Double Tap: The YARA toolkit by @Frogger so very useful to write your YARA rule online and also validate it (more on this later). It also provides small snippets of YARA rules which are very easy to look at and maybe even get some ideas for your own rules.

  3. Test against Bad: Ideally this would be on your own system running YARA to validate that at a bare minimum your rule matches with the sample you used to create your rule in the first place. If you do not have this, then your next best bet would be YARAify. It also enables you to upload the malware sample or a Zip file (packed) containing your sample to the platform and then run your YARA rule against it.

  4. Test against Good: The easiest way to accomplish this would be to simply install YARA on your own machine and run it against a specified file directory. This is perfectly safe and if your rule triggers on your own system then you better have the incident response on your speed dial.

  5. Optional but Fun: Upload your YARA rule to YARAify to monitor the file matches against your rules over a period of time. Do be careful as this can be super addictive, I nearly ended up staying up the whole night just watching the positive hits on my YARA rules.

Other Sources:

Finally to end this blog, I would like to share some really helpful resources which made writing YARA even easier

YARA official docs: These official docs will be your best friend while you get familiarised with the syntax and the modules that YARA has to offer. I found myself referencing these through out the challenge particularly for the PE module.

YARA Toolkit This is an absolute gold mine of a resource created by Thomas Roccia @fr0gger_ . The YARA Toolkit contains some cool utilities which can help speed up the process of creating YARA rules as well as carry out more complex tasks, all from the online tool. Bonus: it also contains a collection of small snippets of YARA rules which can be added to your rules such as the PE file header check YARA.

image

Malapi: The last resource is the MalAPI.io which was created by Mr. Dox @mrd0x and was originally intended for malware analysts and reverse engineers. Understanding import functions and the libraries that malware authors use can be challenging and this is the problem that this resource attempts to solve by mapping the suspicious API functions to the tactics that the threat actors are attempting to complete.

Bonus YARA Rule

image

This is all for now, please feel free to reach out to me on Twitter @RustyNoob619 and let me know what topic would you like my next blog to cover. I will be dropping a poll on Twitter soon to see what I should write next, until then, stay frosty…

tags: