YARA Rules
YARA is the standard language to describe and detect malware by patterns: you define rules that look for byte sequences, strings, or conditions in files and in memory, and YARA tells you which samples match. It’s the bridge between analysis (see static / dynamic) and detection: once you understand a sample, you write a rule that detects that family across the fleet. AV, EDR, sandboxes, and threat hunters all use it. It’s purely defensive: a rule describes to recognize, not to create.
Threat model
Section titled “Threat model”A signature is only as good as its balance between coverage and false positives. Signing on strings that change every build (hash, timestamp) detects a single sample and ages in hours; signing on what’s stable about the family (own code, imphash, config) climbs the Pyramid of Pain and resists repacking. The adversary, meanwhile, obfuscates and packs to break disk signatures — which is why mature detection also signs in memory, where the sample is already unpacked.
flowchart LR
A[Sample analysis] --> P[Stable patterns<br/>strings · bytes · imphash]
P --> R[YARA rule]
R --> V{False positives?<br/>test vs goodware}
V -->|Yes| P
V -->|No| D[Deploy<br/>disk + memory + retrohunt]
Anatomy of a rule
Section titled “Anatomy of a rule”rule FamilyX_Variant{ meta: author = "theoffsecgirl" description = "Detects family X by its config strings" reference = "https://..." hash = "sha256..." date = "2026-01-01" strings: $s1 = "characteristic_string" ascii $s2 = "C:\\path\\to\\artifact" wide $hex = { 48 8B ?? ?? E8 ?? ?? ?? ?? } // bytes with wildcards $re = /mutex_[a-f0-9]{8}/ // regular expression condition: uint16(0) == 0x5A4D and // it's a PE (MZ header) 2 of ($s*) and $hex // match logic}Components:
- meta — metadata (author, description, hash, reference): rule documentation.
- strings — patterns to look for:
"text" ascii/wide/nocase(strings),{ 48 8B ?? }(hex bytes,??wildcard,[n-m]jump),/regex/(regular expressions). - condition — the boolean logic:
any of them,N of ($s*), file checks (uint16(0)==0x5A4D,filesize < 100KB), and modules (pe.imphash,math.entropy).
Writing a good rule
Section titled “Writing a good rule”- From the analysis, extract the family’s unique patterns: config strings (messages, paths, mutexes, user-agents), byte sequences of own code (not common libraries), and imports/structure (the
pemodule). - Goal: detect the family (variants included), not a single sample → patterns stable across builds.
- Avoid false positives: no generic strings (
"error","http") or compiler/libc code bytes; test the rule against a benign corpus (goodware) before deploying.
Defensive example: detection by imphash
Section titled “Defensive example: detection by imphash”import "pe"
rule Loader_By_Imphash{ meta: description = "Groups loader variants by their imphash" author = "theoffsecgirl" condition: pe.imphash() == "a1b2c3d4e5f6..." // imphash obtained from analysis}The pe module lets you sign by structure (imports, sections, entropy) instead of volatile strings: far more resistant to variants.
Using YARA
Section titled “Using YARA”yara rule.yar sample # scan a fileyara -r rules/ /path/to/scan/ # recursiveyara rule.yar -p <pid> # scan a process's MEMORYAt scale: YARA lives in EDR, sandboxes, IOC scanners (LOKI/THOR), VirusTotal Retrohunt, and the modern yara-x engine.
YARA in memory (very powerful)
Section titled “YARA in memory (very powerful)”A packed binary doesn’t match disk signatures because its real code is encrypted. But in memory, already unpacked, it does: scanning process memory with YARA detects malware that evades static detection (see Obfuscation and packing).
YARA + Sigma + IOCs: layered detection
Section titled “YARA + Sigma + IOCs: layered detection”The three signatures complement each other: YARA describes files and memory (the what it is), Sigma describes logs/events (the what it did), and atomic IOCs (hash, domain, IP) allow direct blocking. A complete analysis produces all three.
# Sigma: suspicious execution derived from behavior observed in dynamic analysistitle: Office process launches a script interpreterlogsource: product: windows category: process_creationdetection: selection: ParentImage|endswith: - '\winword.exe' - '\excel.exe' Image|endswith: - '\powershell.exe' - '\wscript.exe' - '\mshta.exe' condition: selectionlevel: highFor the defense
Section titled “For the defense”- Write rules from analysis: each studied sample → a rule detecting its family.
- Deploy YARA in memory and on disk (EDR, scanners like LOKI/THOR) fleet-wide.
- Share and consume community rules (public YARA rules, threat intel).
- Validate against false positives with a benign corpus before production.
- Combine with Sigma (behavior) and IOCs for layered detection (see Intelligence and classification).
CVEs and real-world cases
Section titled “CVEs and real-world cases”- Public YARA rules from vendors and CERTs (Mandiant, ESET, CISA) accompany nearly every major campaign report — the standard detection deliverable.
- yara-x (a Rust rewrite of the engine, by VirusTotal) is the 2026 reference evolution: faster and safer.
- VirusTotal Retrohunt lets you run a rule against the historical sample set to discover old variants of a freshly analyzed family.
Testing checklist
Section titled “Testing checklist”- From analysis, extract unique patterns (strings/bytes/imports)
- Write the rule with meta/strings/condition
- Prioritize detecting the FAMILY (variants), not one sample
- Test against goodware (avoid false positives)
- Scan on disk and in MEMORY (unpacked)
- Use modules (pe.imphash, math.entropy) if it helps
- Deploy to EDR/scanners at scale
- Combine with Sigma and IOCs