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
| Word | What it means | Example |
|---|---|---|
| YARA | a tool that matches files against written rules | yara lab.yar . |
| Rule | a named description of what to look for | |
| Condition | the test that decides whether a rule matches | |
| False positive | a clean file that a rule wrongly matches | |
| Module | an add-on that lets a rule read a file format | pe |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsThe 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
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
}
ENDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Scan the folder with it
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
yara -s lab.yar updaterShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA 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
yara -r lab.yar /usr/bin | wc -lShown 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
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
}
ENDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Scan the folder and the clean programs
yara packed.yar . && yara -r packed.yar /usr/bin | wc -lShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA 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
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
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.
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