01 BitsOfBinary
Search

Days 46–57 · 10 min read

YARA Command-Line Flags & Performance

Practical tour of YARA CLI options for rule compilation, timeouts, fast-scan mode, file-size limits, multithreading, memory scanning, and debugging.

On this page (13)

Day 46 - YARA Command Line Flags - Intro

Let’s look at something a bit different from talking about writing rules themselves, and instead think about how we run them. YARA comes with a variety of flags that can be added when running the tool from the command line.

Using the pre-release for YARA 4.3.0, the following commands are available:

YARA 4.3.0, the pattern matching swiss army knife.
Usage: yara [OPTION]... [NAMESPACE:]RULES_FILE... FILE | DIR | PID
Mandatory arguments to long options are mandatory for short options too.
--atom-quality-table=FILE path to a file with the atom quality table
-C, --compiled-rules load compiled rules
-c, --count print only number of matches
-d, --define=VAR=VALUE define external variable
--fail-on-warnings fail on warnings
-f, --fast-scan fast matching mode
-h, --help show this help and exit
-i, --identifier=IDENTIFIER print only rules named IDENTIFIER
--max-process-memory-chunk=NUMBER set maximum chunk size while reading process memory (default=1073741824)
-l, --max-rules=NUMBER abort scanning after matching a NUMBER of rules
--max-strings-per-rule=NUMBER set maximum number of strings per rule (default=10000)
-x, --module-data=MODULE=FILE pass FILE's content as extra data to MODULE
-n, --negate print only not satisfied rules (negate)
-N, --no-follow-symlinks do not follow symlinks when scanning
-w, --no-warnings disable warnings
-m, --print-meta print metadata
-D, --print-module-data print module data
-M, --module-names show module names
-e, --print-namespace print rules' namespace
-S, --print-stats print rules' statistics
-s, --print-strings print matching strings
-L, --print-string-length print length of matched strings
-X, --print-xor-key print xor key of matched strings
-g, --print-tags print tags
-r, --recursive recursively search directories
--scan-list scan files listed in FILE, one per line
-z, --skip-larger=NUMBER skip files larger than the given size when scanning a directory
-k, --stack-size=SLOTS set maximum stack size (default=16384)
-t, --tag=TAG print only rules tagged as TAG
-p, --threads=NUMBER use the specified NUMBER of threads to scan a directory
-a, --timeout=SECONDS abort scanning after the given number of SECONDS
-v, --version show version information
Send bug reports and suggestions to: vmalvarez@virustotal.com.

In the next few posts we’ll talk about some tips for using the different command lines flags! I’ll be running commands on Windows, which means the binary for YARA will be named yara64 (you may just have a binary called yara if you’re running on Linux/macOS).

I’ll aim to put these tips into the categories of utility (i.e. helping those who are writing the rules) and performance (i.e. options to make YARA perform better).

First tip is to check on occasion that you’re using an up to date version of YARA. You can check this with yara64 --version.

Day 47 - YARA Command line: Performance - Compiled YARA Rules

One of the first flags you may spot for YARA is -C to load compiled rules. Before YARA can start evaluating rules against files, it will compile them into a format that can be read by the tool.

You may have noticed that when you download/compile YARA that you have an additional binary called yarac64 (or just yarac), which has the following command line arguments:

Usage: yarac [OPTION]... [NAMESPACE:]SOURCE_FILE... OUTPUT_FILE
--atom-quality-table=FILE path to a file with the atom quality table
-d, --define=VAR=VALUE define external variable
--fail-on-warnings fail on warnings
-h, --help show this help and exit
--max-strings-per-rule=NUMBER set maximum number of strings per rule (default=10000)
-w, --no-warnings disable warnings
-v, --version show version information
Send bug reports and suggestions to: vmalvarez@virustotal.com

As such, you can use this to pre-compile your rules for YARA. There are some downsides to this, including:

  • the compiled rules will only work with the version of YARA that they’ve been compiled for
  • the filesize of the compiled rules will be signficantly bigger than the original rule files
  • you won’t be able to YARA rules directly in the compiled ruleset

However, the big advantages come into play if you’re constantly running YARA rules against samples (i.e. in some kind of automated fashion), as by compiling them in advance you are saving a lot of processing that YARA needs to do each time it is run.

Day 48 - Decompiling YARA rules

As an aside, don’t consider it impossible for the compiled YARA rulesets to be “decompiled”. For example, Hilko Bengen (@_hillu) gave a great talk on the YARA compiler itself (reference: https://www.youtube.com/watch?v=zPRLxNq8XbQ) which included tools for disassembling YARA bytecode. There is also an old (archived) project for decompiling YARA rules, although I haven’t tested whether this still works as expected.

Why is this important? If you’re using open source rules to scan files, then this isn’t going to be a concern for you. However, if you’re running proprietary rulesets, and assume that compiling them means they cannot be returned to source code, then that is a bad assumption. If someone really wanted to, they could go to the effort of reversing them.

Day 49 - YARA Command line: Performance - Timeout

If you don’t want your YARA rules to run for too long, you can manually provide a timeout setting with the -a flag. From my testing, this value in seconds will give a timeout to all rules input. If you’re running against an individual file, each rule will sequentially evaluate against the file, and stop if the timeout is reached. If running against a directory, then files will be processed one at a time, evaluating each rule, and will stop if the timeout is reached.

Therefore, you may get partial results against directories/subdirectories, but the timeout will cut off all further processing once reached.

This flag is useful if, for instance, you want to run against a live system and don’t want to tank the CPU for too long, but only use it if you’re comfortable not getting a full set of results if the timeout is reached!

Day 50 - YARA Command line: Performance - Fast scan

The -f flag enables YARA to run in fast scan mode, but it isn’t immediately clear what this means. However, there is some documentation on this feature (search for SCAN_FLAGS_FAST_MODE on the following page): https://yara.readthedocs.io/en/stable/capi.html?highlight=fast#scanning-data-1

As the docs point out:

The SCAN_FLAGS_FAST_MODE flag makes the scanning a little faster by avoiding multiple matches of the same string when not necessary. Once the string was found in the file it’s subsequently ignored, implying that you’ll have a single match for the string, even if it appears multiple times in the scanned data. This flag has the same effect of the -f command-line option described in Running YARA from the command-line.

For strings that are common and appear multiple times in files, this flag will speed things up/possibly prevent errors. However, as the description mentions, this comes at a cost: implying that you'll have a single match for the string, even if it appears multiple times in the scanned data

As such, any rules that rely on the count of a string (i.e. the count of string $test can be obtained via #test), or iterating over offsets to string (i.e. the 5th occurance of $test would be @test[5] - yes, they are indexed from 1) will not properly evaluate.

Edit

CORRECTION: Wesley Shields pointed out a threat to me that explains some of the nuance of fast mode better: https://twitter.com/wxs/status/1627278414926184450

In short, fast mode will still mean rules evaluate correctly no matter what condition you use; it’s more about the fact that YARA won’t need to store references to every string depending on the condition, which can save some processing at the expense of not displaying those strings to the user.

For example, the following rule:

rule test {
strings:
$foo = "foo"
condition:
any of them
}

when run on the following data:

foobarfoo

will give the following output with -s:

0x0:$foo: foo
0x6:$foo: foo

and the following with -f -s:

0x0:$foo: foo

As you can see, it doesn’t return the second instance of foo. But, if we update the rule to the following:

rule test {
strings:
$foo = "foo"
condition:
#foo == 2
}

it will still evaluate correctly whether in fast mode or not, and give the following strings as output:

0x0:$foo: foo
0x6:$foo: foo

I.e. you’re still running in fast mode, but getting every string returned, as the condition still needs to rely on the count of them!

Thank you Wesley for pointing this out, I feel like I understand this mode a lot better now!

Day 51 - YARA Command line: Performance - Skip larger

If you’re running YARA rules over a large file system, a challenge you may run into is that you may scan very large files. While this may be what you want, depending on your rules this could also lead to a lot of processing being necessary.

Since YARA v4.2.0, there is a --skip-larger (or -z) flag, which can be used to ignore files past a specified size when running in recursive mode (i.e. with the -r flag). If you use this flag, for every file larger than the specified size you will get an error message sent to stderr with the following format:

skipping FILENAME (SIZE_OF_FILE bytes) because it's larger than SPECIFIED_SKIP_SIZE bytes.

Consider using this to save some processing time, accepting the fact that you might miss some files in the process!

Day 52 - YARA Command line: Performance - Multi-threading

When scanning directories (or scanning a list of files), YARA can run in a multi-threading mode. You can use the --threads (or -p) to specify the maximum number of threads you want to use.

The maximum number of threads is set in the variable YR_MAX_THREADS, which is set to 32 by default. In reading the code, it appears that YARA will set the default threads count to be that of the maximum, so this flag is more so used for if you want to set a lower amount of threads to run (which may be advisable if you’re running on a live system that you don’t want to slow down too much, for example).

Day 53 - YARA Command line: Performance - Memory scanning

Did you know that YARA scan be used to scan the memory of a system? You don’t need to necessarily aquire a copy of the memory to run rules over it; rather you can specify the process ID (PID) as specified in the usage:

Usage: yara [OPTION]... [NAMESPACE:]RULES_FILE... FILE | DIR | PID

You can limit the size of the largest memory chunk with the --max-process-memory-chunk=NUMBER flag (which by default is 1GB).

In practice I don’t find myself using YARA to scan memory that often, but it’s a nice option to have, especially if you want to test things like in-memory config extraction!

Day 54 - YARA Command line: Utility - Count flag

A flag I forget about a lot is the -c flag (note that it’s lowercase this time). Rather than displaying the rulename + filepath of each rule hit, it will instead display each file that has been scanned, and return the number of rules that have hit against it.

While you lose the context of which rules have evaluated to true against which files, it can be a useful way of getting an idea of if you’re missing any samples with the rules you’ve written. E.g. if you’ve collected your samples of interest in a specific directory, and all your rules in one file, you can quickly an easily see what samples you’re missing rules for/still need to signature.

Day 55 - YARA Command line: Utility - Negate

A really useful flag to use when testing whether your rules are working properly is the negate flag. By using --negate (or -n), YARA will return every rule that doesn’t hit on a file.

I use this option for if I’m testing a rule, and can’t figure out why a more complicated condition isn’t hitting on a file. If the rule doesn’t hit, I combine -n with -s to see which string are found in the file, which means I can then go and fix the ones that potentially aren’t working as expected.

Day 56 - YARA Command line: Utility - String lengths

So you probably know you can displayed matched strings with the -s flag. But did you know, that you can display the matched string lengths with with --print-string-length (or -L) flag?

This is a pretty cool flag, that may not have immediately understandable benefits. You might be thinking “don’t I already know the length of my strings?”. And the answer is yes, but not in all cases. In fact, if you’ve used wildcards/ranges in hex/regex strings, the length of your string may be variable!

You can retrieve the length of a matched string by using the ! operator (i.e. the first occurance string of $s will have length !s[1]).

Day 57 - YARA Command line: Utility - Scan list

If you want to use YARA to scan only a specific set of files, rather than every file/subdirectory of directory, you can use the --scan-list flag. Here you can pass individual files, or directories themselves, as a newline separated list, which YARA will then run through.

This could be really useful if you want to do a targeted scan of an endpoint. Be pre-determining the directories you want to scan (or not scan), this could make your life a lot easier!

At the time of writing, I’m having some issues getting this to work as expected on Windows, but it works fine on Linux. Might be on that needs a pull request to fix for Windows!