SOC Lab 4: YARA Rules
Toolchain: YARA 4.5.8 CLI, yarac, PE module · Environment: Linux Series: Part 1 ran Snort passive, Part 2 pushed it inline, Part 3 wrote Sigma rules. This part stops reading events and reads the bytes.
The first three posts watched things go by: packets on the wire, then log events in a SIEM. Neither sees the artifact itself. When a sample lands in quarantine or gets pulled off a disk, the SOC has to answer one question fast: what am I holding? That is YARA’s job.
YARA sits one layer deeper than everything built so far. Each detection layer watches a different thing:
1
2
3
4
5
event to describe detection content engine that runs it
--------------------- ---------------------- -------------------
packets on the wire Snort rules (parts 1-2) Snort 3
log events Sigma rules (part 3) your SIEM search
files / memory regions YARA rules (this post) yara / yarac
YARA is an open source pattern matching engine written by Victor M. Alvarez and used at scale by VirusTotal [1][2]. You write rules that describe a file by the bytes and strings inside it, and the engine scans files, directories or process memory against them. It is the de facto standard for file-based triage and detection content, the way Snort and Sigma cover their own layers.
Three properties make YARA different from what the blog has built so far [1]:
- YARA reads artifacts, not events. Snort consumes packets, Sigma consumes logs, YARA consumes the file, the memory dump or the running process itself. It works offline and on the box, which makes it the tool of choice for triage, sandbox verdicts and endpoint scans.
- YARA is content and it is also the runtime. Snort needs its engine, Sigma needs a SIEM backend. YARA ships as a scanner (
yara), a rules compiler (yarac) and a library (yara-python) [3]. The same rule text runs in all three, which is why it embeds cleanly in EDR agents and upload pipelines. - YARA is a pattern language, not a query language. There is no log schema underneath it. You declare the strings and the arithmetic that prove a match, and the engine does substring and byte matching over raw data. That is more primitive than Sigma, and far more precise where it applies.
The practical uses in a SOC flow from that: classify a quarantined file as a known family before detonating it, turn an IoC into a hunt for other copies across the fleet, and cluster malware by shared code instead of by filename or hash. A hash identifies a byte-for-byte copy; a YARA rule identifies the code inside it, which survives a rename, a re-pack or a rebuild.
This post scans a real mimikatz.exe plus an EICAR control, and authors four rules that get progressively better. Every output below is from a real run. No sample is committed to the blog repository.
Roadmap
Eleven stages, in the order they were run. Each one ends with the evidence it produced, so you can tell whether yours worked before moving on.
- Reproduce the Lab. The
yara-labtree, five rule files, three test samples, and a realmimikatz.exefrom the Kali package, pinned by sha256. - Toolchain and Sample Set.
yara --version, theyaraflag table, andfileon the sample to confirm what you are actually scanning. - Rule Anatomy. The fixed
meta/strings/conditionorder, and the first match:soclab_eicar_stringagainsteicar.com. - String Types and Modifiers. Text, hex and regex strings, and the modifier table. The rule that matters here: no modifier means ASCII only.
- The Encoding Trap.
soclab_mimikatz_logon_asciiprints nothing and exits 0 while itswidetwin matches, which isolates the single most common YARA false negative. - Conditions.
any of them,all of ($a*),#a > 3,$m at 0,uint16(0) == 0x5A4D. The grammar that turns strings into a verdict. - False Positives: Name vs. Container.
soclab_mimikatz_namefires on a text file that merely mentions mimikatz, andsoclab_mimikatz_pe.yargates on the PE header to kill it. - The PE Module.
soclab_mimikatz_full.yar: the version resource, the amd64 machine type and threewidestrings, which survive a rename tosvchost.exe. - Rule Tags. Tags belong in the header, identifiers cannot contain dots, and
attack_t1003_001makes tag-scoped scanning possible. - Running a Rule Pack. All five files over the whole sample directory for the full verdict, then
-cfor per-file counts. - Compiled Rules.
yarac rules/*.yar compiled.yarcdown to a single shippable file, and the same six matches read back withyara -C.
Then two closing sections: Community Rules, on the two shared libraries worth knowing and the habit of proving a rule fires before trusting it, and Production Notes, on hunting in strings first, respecting encodings, and scanning where files land plus in memory.
Reproduce the Lab
To follow along, build the same directory. One copy-paste session creates everything, including the real sample, which comes from the Kali mimikatz package (that is what the windows-resources path in the tree below points at):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
mkdir -p ~/Documents/Hacking/SOC/yara-lab/{rules,samples}
cd ~/Documents/Hacking/SOC/yara-lab
# rule 00: EICAR control
cat > rules/00_eicar.yar <<'EOF'
rule soclab_eicar_string {
meta:
author = "Nassim (k1tch0p)"
description = "Detects the EICAR standard antivirus test string"
date = "2026/09/08"
strings:
$eicar = "EICAR-STANDARD-ANTIVIRUS-TEST-FILE"
condition:
$eicar
}
EOF
# rule 01: naive name match (the rule later sections break)
cat > rules/01_mimikatz_name.yar <<'EOF'
rule soclab_mimikatz_name {
meta:
author = "Nassim (k1tch0p)"
description = "Naive name match, fires on any file that mentions mimikatz"
date = "2026/09/08"
strings:
$name = "mimikatz" ascii
condition:
$name
}
EOF
# rule 02: the encoding trap (ascii misses, wide matches)
cat > rules/02_mimikatz_sekurlsa.yar <<'EOF'
rule soclab_mimikatz_logon_ascii {
meta:
description = "Looks for the logonpasswords command token as plain ASCII. Misses this sample."
strings:
$cmd = "logonpasswords" nocase
condition:
$cmd
}
rule soclab_mimikatz_logon_wide {
meta:
description = "Same token with the wide flag, matching the UTF-16LE encoding this sample stores."
strings:
$cmd = "logonpasswords" wide ascii nocase
condition:
$cmd
}
EOF
# rule 03: name gated on a real PE32+ amd64 container
cat > rules/03_mimikatz_pe.yar <<'EOF'
import "pe"
rule soclab_mimikatz_pe_facts {
meta:
author = "Nassim (k1tch0p)"
description = "Name string, gated on a real PE32+ amd64 container. Kills the text-file false positive."
date = "2026/09/08"
strings:
$name = "mimikatz" ascii nocase
condition:
uint16(0) == 0x5A4D and
pe.machine == pe.MACHINE_AMD64 and
$name
}
EOF
# rule 04: container, version resource, module table
cat > rules/04_mimikatz_full.yar <<'EOF'
import "pe"
rule soclab_mimikatz_full : tool_mimikatz attack_t1003_001 {
meta:
author = "Nassim (k1tch0p)"
description = "High confidence mimikatz: amd64 PE whose version resource names it and that carries the sekurlsa module and its logonpasswords command table."
date = "2026/09/08"
reference = "https://attack.mitre.org/techniques/T1003/001/"
strings:
$s_version = "mimikatz 2.2.0" wide ascii
$s_sekurlsa = "sekurlsa" wide nocase
$s_logon = "logonpasswords" wide nocase
condition:
uint16(0) == 0x5A4D and
pe.machine == pe.MACHINE_AMD64 and
pe.version_info["OriginalFilename"] == "mimikatz.exe" and
all of ($s*)
}
EOF
# benign sample controls
cat > samples/eicar.com <<'EOF'
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
EOF
cat > samples/edr_notes.txt <<'EOF'
2026-09-08 09:14 analyst note, SOC shift
FIN-WS-07 spawned cmd.exe from Excel. EDR flagged the child and we pulled
the host offline, then collected a memory dump for triage. Suspected
mimikatz use on the host; carve the dump before the image expires.
EOF
cat > samples/readme.txt <<'EOF'
This directory holds the sample set for the SOC YARA lab. readme.txt and
the notes file are benign controls; only the executable is a real artifact.
EOF
The real sample is not something you download from the blog. Kali packages a prebuilt Windows x64 build, so install it and copy it out:
1
2
3
4
# Kali only: installs /usr/share/windows-resources/mimikatz/x64/mimikatz.exe
sudo apt update && sudo apt install -y mimikatz
cp /usr/share/windows-resources/mimikatz/x64/mimikatz.exe samples/mimikatz.exe
sha256sum samples/mimikatz.exe
1
61c0810a23580cf492a6ba4f7654566108331e7a4134c968c2d6a05261b2d8a1 samples/mimikatz.exe
If the hash differs, the package moved on; the rules below target this exact 2022 build and may not fire on a newer one. Rules 00, 02, 03 and 04 appear again in full where they are explained; rule 01 only lives here, because the later sections exist to break it.
Toolchain and Sample Set
YARA is a small C project and Kali ships it with the PE and ELF modules compiled in, which is all this lab needs:
1
2
➜ yara-lab yara --version
4.5.8
Keep one working directory:
1
2
3
4
5
6
7
8
9
10
11
12
yara-lab/
├── rules/
│ ├── 00_eicar.yar
│ ├── 01_mimikatz_name.yar
│ ├── 02_mimikatz_sekurlsa.yar
│ ├── 03_mimikatz_pe.yar
│ └── 04_mimikatz_full.yar
└── samples/
├── eicar.com EICAR antivirus test file
├── edr_notes.txt analyst note that mentions mimikatz (the trap)
├── mimikatz.exe real sample, PE32+ amd64
└── readme.txt benign control, matches nothing
The real sample is the x64 mimikatz.exe from the windows-resources package, SHA256 61c0810a23580cf492a6ba4f7654566108331e7a4134c968c2d6a05261b2d8a1:
1
2
➜ yara-lab file samples/mimikatz.exe
samples/mimikatz.exe: PE32+ executable for MS Windows 5.02 (console), x86-64, 6 sections
Two samples matter beyond that. eicar.com is the industry standard antivirus test file, a benign string every scanner should flag, ideal for validating a pipeline end to end [6]. edr_notes.txt is a plain text analyst note that contains the word “mimikatz”. It exists to make a false positive real instead of hypothetical.
A flag reference for the yara scanner, because the post uses most of them:
| flag | effect |
|---|---|
-i <identifier> | scan only the rule whose name contains <identifier> (test one rule in a multi-rule file) |
-s | print every matched string with its file offset |
-g | print the tags carried by each matching rule |
-t <tag> | scan only rules carrying <tag> |
-c | print a per-file match count instead of match lines |
-C | read a compiled rule set (produced by yarac) |
-p <n> | run <n> scanning threads |
Rule Anatomy
A rule is a flat text file with three blocks in a fixed order: meta, strings and condition. The schema and how the engine treats each block:
1
2
3
4
5
6
7
8
9
10
YARA rule file (.yar) target bytes (file, dir, memory)
+--------------------------------+ |
| rule soclab_eicar_string | |
| meta: key/value pairs | | never evaluated, travel as metadata
| strings: $s = "evidence" | | searched as literal bytes / regex / hex
| condition: $s | | boolean proof, decides the match
+--------------------------------+ |
| yarac: compiles to .yarc |
v v
rule pack condition true -> "rule_name target_path"
Our first rule, the simplest honest detection, flags the EICAR string:
1
2
3
4
5
6
7
8
9
10
11
# rules/00_eicar.yar
rule soclab_eicar_string {
meta:
author = "Nassim (k1tch0p)"
description = "Detects the EICAR standard antivirus test string"
date = "2026/09/08"
strings:
$eicar = "EICAR-STANDARD-ANTIVIRUS-TEST-FILE"
condition:
$eicar
}
A rule starts with rule and a name that must be a valid identifier. meta holds key/value pairs that travel with the rule and are never evaluated. strings declares named patterns prefixed with $. condition is a boolean expression evaluated against the target; the shorthand $eicar means “the string $eicar was found at least once”. No if, no loop, no output logic. When the condition is true the engine prints the rule name and the scanned path:
1
2
➜ yara-lab yara rules/00_eicar.yar samples/eicar.com
soclab_eicar_string samples/eicar.com
That is the entire contract. Point yara at a rule file and a target, and every line of output is one rule that fired on one target. The header is also the crucial asymmetry with Snort: Snort identifies a signature with a numeric sid, YARA has no registry, and the rule’s file and name are its identity. Version-control the rule text and you version the detection.
String Types and Modifiers
Every piece of evidence in a strings block is one of three types, and the type decides how literal the match is:
| type | example | matches |
|---|---|---|
| text string | "mimikatz" | literal bytes, refined by encoding modifiers |
| hex string | { 4D 5A } | exact bytes; ?? is a wildcard byte, [4-8] a gap of 4 to 8 bytes |
| regex | /kuhl_m_\w+/i | PCRE over the file, flags after the closing slash |
Hex strings are how you express raw structure. { 4D 5A } is the byte pair for MZ, the start of every PE file, and { 4D 5A ?? ?? 00 00 } would allow two unknown header bytes in between. Text strings are the workhorse. They take modifiers that hang off the closing quote and refine how the bytes are searched:
| modifier | effect |
|---|---|
| (none) | search the ASCII form only |
ascii | search the ASCII form only, explicit |
wide | search the UTF-16LE form only |
ascii wide | search both forms |
nocase | case insensitive |
fullword | only match at word boundaries |
xor | match the string under single-byte XOR |
base64 | match the string as it would appear inside base64 |
The encoding flags are where the semantics bite. With no modifier at all the engine searches ASCII only. wide alone means UTF-16LE only. To search both you must write ascii wide, which is exactly the shape of the trap in the next section.
Why the wide form at all? Windows applications are Unicode-native: the C runtime and most Win32 string APIs operate on UTF-16, so a compiler stores string literals wide, each character followed by a \x00. Malware authored for Windows therefore carries most of its telltale strings in UTF-16LE, not ASCII.
The Encoding Trap
mimikatz stores its command table and its banners as UTF-16LE, not ASCII. Every logonpasswords token in the binary is wide, each ASCII character followed by a null byte. A rule that searches for the ASCII form of the token is looking for bytes that are not there, and it fails silently. Two rules, one token, different encodings:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# rules/02_mimikatz_sekurlsa.yar
rule soclab_mimikatz_logon_ascii {
meta:
description = "Looks for the logonpasswords command token as plain ASCII. Misses this sample."
strings:
$cmd = "logonpasswords" nocase
condition:
$cmd
}
rule soclab_mimikatz_logon_wide {
meta:
description = "Same token with the wide flag, matching the UTF-16LE encoding this sample stores."
strings:
$cmd = "logonpasswords" wide ascii nocase
condition:
$cmd
}
The ascii rule carries only nocase, so it searches ASCII. Because both rules share one file, -i selects the one to test in isolation:
1
➜ yara-lab yara -i soclab_mimikatz_logon_ascii rules/02_mimikatz_sekurlsa.yar samples/mimikatz.exe
Empty output, and the exit code is 0. That is the footgun: exit 0 means “rule compiled, nothing matched”, not “something matched”. A scheduled scan with an encoding-blind rule does not error; it quietly reports zero hits forever. The wide variant finds it immediately:
1
2
➜ yara-lab yara -i soclab_mimikatz_logon_wide rules/02_mimikatz_sekurlsa.yar samples/mimikatz.exe
soclab_mimikatz_logon_wide samples/mimikatz.exe
Ask the scanner which string fired and the reason is visible as raw bytes. -s prints every match with its offset:
1
2
3
4
5
6
7
➜ yara-lab yara -s -i soclab_mimikatz_logon_wide rules/02_mimikatz_sekurlsa.yar samples/mimikatz.exe
soclab_mimikatz_logon_wide samples/mimikatz.exe
0x11feb8:$cmd: l\x00o\x00g\x00o\x00n\x00p\x00a\x00s\x00s\x00w\x00o\x00r\x00d\x00s\x00
0x120620:$cmd: l\x00o\x00g\x00o\x00n\x00p\x00a\x00s\x00s\x00w\x00o\x00r\x00d\x00s\x00
0x120690:$cmd: l\x00o\x00g\x00o\x00n\x00p\x00a\x00s\x00s\x00w\x00o\x00r\x00d\x00s\x00
0x120710:$cmd: l\x00o\x00g\x00o\x00n\x00p\x00a\x00s\x00s\x00w\x00o\x00r\x00d\x00s\x00
0x12b978:$cmd: l\x00o\x00g\x00o\x00n\x00P\x00a\x00s\x00s\x00w\x00o\x00r\x00d\x00s\x00
Every \x00 is the UTF-16LE encoding showing through, and nocase even surfaces the camel-case variant logonPasswords at 0x12b978. This is the single most common reason a freshly written Windows YARA rule is dead on arrival: ASCII search against a wide-storing binary. When the tool is Windows-native, default to wide ascii, and check with -s that a rule fires on a known sample before you trust a single “no match”.
Conditions
A condition is a boolean expression, and it decides when all that evidence actually means something. Beyond the bare $name shorthand, the grammar you will actually use in the field is small [1]:
1
2
3
4
5
6
7
8
9
10
$s1 and $s2 both strings present
any of them any string in the rule present
all of them every string in the rule present
all of ($a*) every string whose identifier starts with $a
#a > 3 string $a matched more than 3 times
@a < 0x2000 first match of $a is before offset 0x2000
$m at 0 string $m at file offset 0 (a classic MZ check)
filesize < 5MB file size bound
uint16(0) == 0x5A4D read 2 bytes at offset 0 and compare
pe.machine == ... a module call, see the PE Module section
#, @, filesize and uint16 are what make a condition arithmetic instead of a grep: you can count occurrences, bound offsets, gate on size, and read integers straight out of the file. The rule body from the anatomy section used the default shorthand. Ask the scanner to reveal what fired on the real binary and you get the evidence trail:
1
2
3
➜ yara-lab yara -s rules/01_mimikatz_name.yar samples/mimikatz.exe
soclab_mimikatz_name samples/mimikatz.exe
0xfa1d8:$name: mimikatz
One ASCII hit at offset 0xfa1d8. Note what is absent: the rule matched because the banner string “mimikatz” exists in the binary, not because the binary is mimikatz. Any file that mentions the tool by name trips it. That is the subject of the next two sections, where the condition stops being a single string and starts encoding analyst judgment.
False Positives: Name vs. Container
The naive name rule from the previous section is useless in a real SOC, because analysts write notes about mimikatz all the time. Scan the whole sample directory and the rule cannot tell a binary from a text file:
1
2
3
➜ yara-lab yara rules/01_mimikatz_name.yar samples
soclab_mimikatz_name samples/edr_notes.txt
soclab_mimikatz_name samples/mimikatz.exe
There is the trap: an investigation note about a mimikatz sighting fires the same detection as the tool itself. Every false positive like this teaches the same lesson, which Part 3 made for Sigma filters: detections need container context. A mimikatz name inside a plain text note is noise; the same name inside a Windows executable is signal. The condition can say so, because YARA can read the container:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# rules/03_mimikatz_pe.yar
import "pe"
rule soclab_mimikatz_pe_facts {
meta:
author = "Nassim (k1tch0p)"
description = "Name string, gated on a real PE32+ amd64 container. Kills the text-file false positive."
date = "2026/09/08"
strings:
$name = "mimikatz" ascii nocase
condition:
uint16(0) == 0x5A4D and
pe.machine == pe.MACHINE_AMD64 and
$name
}
Three predicates, all now required. uint16(0) == 0x5A4D reads the first two bytes little-endian and checks for the MZ DOS header, the signature of every PE file. pe.machine == pe.MACHINE_AMD64 asks the PE module to parse the header and confirm an x64 build. $name requires the string. A text file fails the first check and drops out:
1
2
➜ yara-lab yara rules/03_mimikatz_pe.yar samples
soclab_mimikatz_pe_facts samples/mimikatz.exe
One hit. The note is gone, the false positive closed by context instead of by deleting the rule.
The PE Module
The rule is correct but still leans on one weak string. Let us build the version an analyst would actually run: it confirms the container, reads the file’s version resource for its original filename, and requires the evidence that only a real mimikatz carries, the sekurlsa module table and its logonpasswords command, stored wide.
import "pe" loads the PE module, which parses the executable header and exposes structured fields the raw bytes hide: section headers, imports, exports and version resources, plus helpers such as pe.imports("dllname") and machine type constants [4]. That is how a condition can speak about the file’s structure instead of its raw bytes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# rules/04_mimikatz_full.yar
import "pe"
rule soclab_mimikatz_full : tool_mimikatz attack_t1003_001 {
meta:
author = "Nassim (k1tch0p)"
description = "High confidence mimikatz: amd64 PE whose version resource names it and that carries the sekurlsa module and its logonpasswords command table."
date = "2026/09/08"
reference = "https://attack.mitre.org/techniques/T1003/001/"
strings:
$s_version = "mimikatz 2.2.0" wide ascii
$s_sekurlsa = "sekurlsa" wide nocase
$s_logon = "logonpasswords" wide nocase
condition:
uint16(0) == 0x5A4D and
pe.machine == pe.MACHINE_AMD64 and
pe.version_info["OriginalFilename"] == "mimikatz.exe" and
all of ($s*)
}
Read the parts that are new. pe.version_info["OriginalFilename"] reads a value out of the version resource, the metadata the compiler baked in when the binary was built; a renamed file keeps that value. The three $s_* strings use the wide ascii lesson from the encoding section. all of ($s*) demands every one of them. The MITRE reference in meta and the attack_t1003_001 tag tie the rule to LSASS credential dumping [5].
The payoff of content-based detection: rename the binary and it still fires, because a rename changes the filename, not the code or the version resource inside it:
1
2
3
4
➜ yara-lab cp samples/mimikatz.exe samples/svchost.exe
➜ yara-lab yara rules/04_mimikatz_full.yar samples/svchost.exe
soclab_mimikatz_full samples/svchost.exe
➜ yara-lab rm samples/svchost.exe
An analyst who sees svchost.exe in a scan result and trusts the name is the exact person this rule protects. The file calls itself mimikatz in its own metadata and in its own strings. Hash-based IoCs cannot do this: a renamed copy has a different hash and slips any hash watchlist, while the YARA rule catches it at the content layer.
Rule Tags
Two authoring gotchas lived in this exact file, and they cost minutes. Tags go in the header, after the rule name and a colon, never in meta; the parser rejects tags in meta, or you get a rule that carries no tags at all. Tag identifiers cannot contain dots. tool.mimikatz is a syntax error, write tool_mimikatz. Same rule, valid identifiers.
-g shows the tags on a match, which is the fastest way to confirm a tag parsed:
1
2
➜ yara-lab yara -g rules/04_mimikatz_full.yar samples/mimikatz.exe
soclab_mimikatz_full [tool_mimikatz,attack_t1003_001] samples/mimikatz.exe
Tags let you slice a rule pack the way Sigma’s attack.* tags slice a fleet of rules. Run only the credential-dumping set:
1
2
➜ yara-lab yara -t attack_t1003_001 rules/04_mimikatz_full.yar samples
soclab_mimikatz_full samples/mimikatz.exe
Running a Rule Pack
A rule is a unit of content, but a SOC runs a rule pack, dozens or hundreds of rules over every file that enters a pipeline. Feed all the rules to one scan and read every hit in one pass:
1
2
3
4
5
6
7
➜ yara-lab yara rules/*.yar samples
soclab_eicar_string samples/eicar.com
soclab_mimikatz_name samples/edr_notes.txt
soclab_mimikatz_name samples/mimikatz.exe
soclab_mimikatz_logon_wide samples/mimikatz.exe
soclab_mimikatz_pe_facts samples/mimikatz.exe
soclab_mimikatz_full samples/mimikatz.exe
This is where the pack earns its keep. The EICAR rule confirms the pipeline works, the naive name rule still shows its false positive on the analyst note, and the two serious mimikatz rules agree on the real binary. The output is the whole fleet’s verdict at a glance.
-c collapses a directory scan into a per-file match count, the output a monitoring job wants:
1
2
3
4
5
➜ yara-lab yara -c rules/04_mimikatz_full.yar samples
samples/edr_notes.txt: 0
samples/readme.txt: 0
samples/eicar.com: 0
samples/mimikatz.exe: 1
A clean file gets a 0, a hit gets a count; a supervisor only cares whether the number moved.
Compiled Rules
Scanning splits into a compile step and a scan step, and production uses the split so it never re-parses rule text per file:
1
2
3
4
5
6
rules/*.yar (text) sensor
| yarac : compile once |
v | yara -C : load pack, scan
compiled.yarc (binary pack) v
| 13 KB, shippable, rule_name target_path
+---------------------> (no rule text on disk)
yarac compiles a pack once into a binary file; yara -C loads it and scans:
1
2
3
4
5
6
7
8
9
10
➜ yara-lab yarac rules/*.yar compiled.yarc
➜ yara-lab ls -l compiled.yarc
-rw-rw-r-- 1 s1m s1m 13560 Sep 8 00:32 compiled.yarc
➜ yara-lab yara -C compiled.yarc samples
soclab_eicar_string samples/eicar.com
soclab_mimikatz_name samples/edr_notes.txt
soclab_mimikatz_name samples/mimikatz.exe
soclab_mimikatz_logon_wide samples/mimikatz.exe
soclab_mimikatz_pe_facts samples/mimikatz.exe
soclab_mimikatz_full samples/mimikatz.exe
The compiled pack is a 13 KB file you can ship to a sensor with no rule text on disk. Throughput is the reason to bother. Scanning a whole host directory of 3,623 system binaries with the single rule above took about 11.5 seconds across 4 threads on this box and matched nothing, which is the normal shape of a triage scan: most of the time is I/O, most files are clean, and the pack still has to touch every byte. Threads (-p), compiled rules (-C), and extension or size filtering before the scan are how you scale it to a fleet [3].
Community Rules
You do not start every rule from zero. The ecosystem around YARA is deep because two big shared libraries are free:
- YARA-Rules/rules organizes community rules by family under folders like
malware/,packers/,crypto/andmobile_malware/, each file one detection with its ownmeta[7]. - signature-base by Florian Roth carries detection-focused YARA content tuned for scanners and hunts, including a well known mimikatz rule, alongside Sigma and IOC collections [8].
Community rules read exactly like the ones we wrote, because the format is that fixed. A shared rule for a family will have a descriptive file name, meta with attribution and a reference, and a condition that reads like an analyst’s reasoning. Treat them like Sigma library rules from Part 3: status: stable, high severity, and scoped to logs you actually have. Compile before you trust: yarac on an imported file catches syntax problems the instant you add it to the pack, and -s against a known sample proves it fires before it ever sees a live queue.
One more thing worth knowing. The original C engine we used here is being joined by YARA-X, a from-scratch Rust implementation that is largely drop-in for existing rule sets and is where upstream is investing [9]. The rule language you just learned carries over; the mental model does not change.
Production Notes
Four habits to carry out of the lab before wiring YARA into a real workflow.
Hunt in strings, then harden into a rule. The naive name rule had a false positive and the PE-gated rule did not. That progression is the job: start broad to find the corpus, then add container and count context until the detection only fires on real code. Revisit meta (status, level, references) as a rule matures, the way Sigma rules carry experimental to stable.
Respect encodings and versions. Our 2022 mimikatz build stores its command table wide and does not contain a contiguous sekurlsa::logonpasswords string, which older detections assume. We anchored on the module name and command token separately instead. Test every rule against fresh samples when a family ships a new build, or it will quietly rot into a false negative, exit code 0 the whole time.
Scan where files land, and in memory. The cheap wins are scan-on-ingest for email attachments and downloads, scheduled scans of file shares and web roots, and scanning quarantine before detonation. Endpoint agents go further and scan process memory, where YARA maps the process address space and catches payloads that never touched disk. That is how the byte-level view finally meets the endpoint.
Trust the rule, not the name or the hash. We renamed a mimikatz copy svchost.exe and the rule still named it. Hash IoCs cannot follow a renamed or rebuilt binary, and filenames lie on purpose. Content detection is the layer that stays honest, which is exactly where it slots into the SOC stack: Snort on the wire, Sigma in the logs, YARA on the artifact.
The skill that carries over from every part of this series is the same one: detection content is analyst judgment made executable. In Snort it was packet context, in Sigma it was log context, here it is byte and container context. Write the strings, gate them on what a real artifact must be, and test against something real before you schedule it. A rule that cannot survive a rename and a recompile was never detecting the malware, only its name.
References
- YARA documentation: rule structure, string modifiers and the condition grammar used throughout this post.
- YARA source code (VirusTotal): the engine used by VirusTotal and the origin of the project.
- YARA command line tool:
yarascan flags and theyaracrules compiler. - YARA PE module reference:
pe.machine,pe.version_infoand structured executable header access. - MITRE ATT&CK T1003.001, OS Credential Dumping: LSASS Memory: the technique mimikatz
sekurlsa::logonpasswordsserves, used as the rule tag and reference. - EICAR standard antivirus test file: the benign test string used to validate scanning pipelines.
- YARA-Rules/rules: community YARA rule repository organized by malware family and packer.
- signature-base (Neo23x0): detection content collection including YARA rules, Sigma rules and IOC lists.
- YARA-X (VirusTotal): the from-scratch Rust implementation of YARA.
Tags
#YARA #YARARules #DetectionEngineering #MalwareAnalysis #MalwareTriage #ThreatHunting #PE #mimikatz #SOC #BlueTeam
