01 BitsOfBinary
Search

Days 35–37 · 3 min read

Understanding YARA Atoms

How YARA extracts and scores 4-byte atoms from strings for fast Aho-Corasick matching, and how to write high-scoring strings.

On this page (3)

Day 35 - YARA Atoms - Introduction

As you start getting more advanced in your usage of YARA, you will want to make sure you’re writing efficient rules. A good concept to understand in YARA is that of atoms, which are well explained in the code that implements them: https://github.com/VirusTotal/yara/blob/2b631d0ee47650923955398921c1ceccc3e38cb1/libyara/atoms.c

As the code points out:

Atoms are undivided substrings found in a regexps and hex strings. … When searching for regexps/hex strings matching a file, YARA uses these atoms to find locations inside the file where the regexp/hex string could match.

As such, if you are using using hex/regex strings which use wildcards/variable ranges, YARA will do some work behind the scenes to work out the best substrings to try and match the whole string you have specified. The code points out that sometimes this is straightforward, and you only need to search for one atom in a string to evaluate whether it matches. However, the more alternative sequences you add into your hex/regex strings (e.g. {ab cd (ef | 00)}), the more atoms may be needed to successfully evaluate a string match.

YARA will create an atom tree which it will then use to try and optimise using the least number of high quality atoms for searching in files. In the next post, we’ll discuss what makes good quality atoms.

Day 36 - YARA Atoms - Scoring atoms

The YARA code helpfully describes the heuristics used to score atoms, and provides some examples: https://github.com/VirusTotal/yara/blob/2b631d0ee47650923955398921c1ceccc3e38cb1/libyara/atoms.c#L117

It is worth noting before discussing this that by default the maximum atom length (set in the variable YR_MAX_ATOM_LENGTH) is set to 4, and as such the code discusses examples which are 4 bytes in length.

In ascending order, the scores given to each byte in an atom are as follows:

  • Any fully masked byte (i.e. 0x??): -10 points
  • Any partially masked byte (e.g. 0x0?, 0x?F): 4 points
  • Common bytes (i.e. 0x00, 0x20, 0xFF): 12 points
  • Letters (i.e. [A-Za-z]): 18 points
  • All other bytes: 20 points

An extra score is added at the end which consists of twice the number of unique bytes in the atom. There is also a hidden check which heavily penalises atoms if all the bytes are equal and common (e.g. {00 00 00 00} is worth 12 + 12 + 12 + 12 + 2 = 50, but then has 10 * atom->length = 40 subtracted, meaning it’s only worth 10 points overal).

Example scores can be seen in the docs: Example YARA atom scores from documentation

A warning threshold is set to 38 points (a set value found here, which is used to compare against atom qualities here). Any strings with the “best” atom falling below this threshold will yield a warning from YARA, which will look like: string "$" may slow down scanning

Day 37 - YARA Atoms - Writing rules using strings with good atoms

So what’s the takeaway from this discussion of atoms? You want good atoms in all your strings, and this will be focused around substrings that are of maximum length 4 and scored according to the rules specified in the previous post.

So to ensure you’re writing efficient rules, make sure to:

  • Avoid strings with very short substrings (e.g. {AB ?? ?? ?? CD} will only be able to use AB ?? ?? ?? or ?? ?? ?? CD as an atom, which are below the threshold)
  • Avoid relying on short substrings of only common bytes (e.g. {00 20} is below threshold)
  • Avoid too many partial masked bytes (e.g. {A? ?B C? ?D} is below threshold)

Instead, try to use strings that you know have good length atoms, and that have unique bytes.

It’s okay in some instances to have strings with bad atoms in them, provided that you have good atoms to make up for it. E.g. {00 20} is a bad string on its own, but {AB CD EF 00 20} is a good string, as YARA can use the atom AB CD EF 00.

YARA will still work even if you have bad atom strings, but don’t ignore those string "$" may slow down scanning warnings! Hunt down the rules which are causing issues, and see if you can fix those strings - it’ll help you out in the long run.