Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Writing YARA rules to find malware

After this lesson: I can write a YARA rule that finds a sample and test it for false alarms.

65 min lesson8 commandsReverse engineering lab: reading programs with gdb, radare2, strace and ltrace on one VM

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

One sample found, how many more?

YARA is a free tool that searches files for patterns described in rules. Once an analyst knows one sample, the next question is where else it is hiding: on other machines, in mail, in old backups.

A YARA rule describes the sample in a few lines, and the same rule can be shared and run anywhere.

By the end of this lesson

Note

I can write a YARA rule that finds a sample and test it for false alarms.

Key words

WordWhat it meansExample
YARAa tool that matches files against written rulesyara lab.yar .
Rulea named description of what to look for
Conditionthe test that decides whether a rule matches
False positivea clean file that a rule wrongly matches
Modulean add-on that lets a rule read a file formatpe

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

The one rule

Important

Scan only your own lab VM. Scanning somebody else's computers or files needs their written permission, under the Computer Misuse Act 1990 in the UK. The samples are the harmless ones you built.

A rule has strings and a condition

A rule has a name, an optional meta part with notes, a strings part and a condition. Each string gets a name that starts with a dollar sign. The condition combines them.

This rule wants three of the sample's four strings, and only in a file whose first four bytes are ELF's magic number. uint32(0) reads those four bytes as one little-endian number.

Write the first rule

Command
cd ~/malab && ls -l updater && cat > lab.yar <<'END'
rule lab_updater
{
    meta:
        description = "The lab updater sample, by its strings"
    strings:
        $mark = "MT-LAB-SAMPLE-7F3A"
        $agent = "LabBeacon/"
        $server = "127.66.0.23"
        $persist = ".config/autostart/"
    condition:
        uint32(0) == 0x464c457f and 3 of them
}
END

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Scan the folder with it

Command
yara lab.yar .

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Show which strings matched, and where

Command
yara -s lab.yar updater

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A good rule stays quiet on clean files

A rule that matches clean files wastes everyone's time, so test it against files you know are clean. A false positive found now is one nobody chases later.

The folder /usr/bin holds hundreds of ordinary programs. The first rule should match none of them.

Count matches among clean programs

Command
yara -r lab.yar /usr/bin | wc -l

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Packing beats a strings rule

The first rule missed updater.upx, because packing hid every string. One answer is to describe the packing itself: UPX's marker, and entropy near 8.

YARA's math module works out entropy over any part of a file. filesize is the whole file.

Write a rule for packed programs

Command
cat > packed.yar <<'END'
import "math"

rule upx_packed_elf
{
    strings:
        $upx = "UPX!"
    condition:
        uint32(0) == 0x464c457f and $upx and math.entropy(0, filesize) >= 7.0
}
END

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Scan the folder and the clean programs

Command
yara packed.yar . && yara -r packed.yar /usr/bin | wc -l

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A module reads the file format

Strings are not the only thing a rule can test. The pe module reads a Windows file's headers, so a rule can ask about its imports, sections or time stamp.

This rule finds any PE file that imports connect from WS2_32.dll, like invoice.exe from the last lesson.

Write and run a rule on imports

Command
cat > pe.yar <<'END'
import "pe"

rule pe_imports_connect
{
    condition:
        pe.is_pe and pe.imports("WS2_32.dll", "connect")
}
END
yara pe.yar .

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

A common mistake

Warning

Many people think a rule that matches the sample is finished. It is finished only when it also stays quiet on clean files and still finds a changed copy. Test both before you share it, and keep the test files.

Practise: ask for all four strings

Command
sed -i "s/3 of them/all of them/" lab.yar && yara lab.yar .

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Assignment: match rules in Python

Write a tiny rule matcher. A rule is a dictionary: "magic" holds the bytes a file must start with (b"" for any file), "strings" a list of byte strings, and "need" how many of them must be found (0 means all of them).

found(rule, data) returns the strings of the rule that occur in data, in the rule's order.

matches(rule, data) returns True when the data starts with the magic and enough strings are found.

scan(rule, files) takes a dictionary of names to bytes and returns the names that match, in alphabetical order.

Code: python
def found(rule, data):
    return []

def matches(rule, data):
    return False

def scan(rule, files):
    return []

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.