YARA
Table of Contents
Pattern matching tool for identifying malware and suspicious files based on text strings, byte sequences, and regular expressions. Write a rule describing what a malware family looks like; scan files, directories, or memory dumps against it. Used in malware analysis, threat hunting, and DFIR to flag known-bad artefacts without needing a full AV engine.
Related: Malware Analysis REMnux Indicators of Compromise windows client DFIR
Installation
# Ubuntu / Debian apt install yara # macOS brew install yara # From source (for latest version) git clone https://github.com/VirusTotal/yara cd yara && autoreconf -fiv && ./configure && make && make install
Rule Structure
rule RuleName
{
meta:
description = "What this rule detects"
author = "Name"
date = "2025-01-01"
hash = "sha256 of a known sample"
strings:
$text = "malicious_function_name"
$text2 = "another_indicator" nocase // case-insensitive
$hex = { 4D 5A 90 00 03 00 } // byte sequence
$hex2 = { 6A ?? 68 ?? ?? ?? ?? E8 } // ?? = wildcard byte
$regex = /https?:\/\/[0-9]{1,3}\.[0-9]{1,3}/ // regex pattern
condition:
$text and $hex
}
String Modifiers
| Modifier | Effect |
|---|---|
nocase |
Case-insensitive match |
wide |
Match UTF-16LE encoded strings (Windows) |
ascii |
Match ASCII (default) |
wide ascii |
Match both encodings |
fullword |
Only match if surrounded by non-alphanumeric chars |
xor |
Match XOR-obfuscated variants |
xor(0x01-0xff) |
XOR with specific key range |
base64 |
Match base64-encoded variants |
Condition Expressions
condition:
$a // string must be present
$a and $b // both must be present
$a or $b // either must be present
not $a // string must be absent
any of ($a, $b, $c) // at least one
all of ($a, $b, $c) // all must be present
2 of ($*) // at least 2 of any defined strings
1 of ($hex*) // at least 1 string starting with $hex
#a > 3 // string $a must appear more than 3 times
@a < 100 // $a must appear within first 100 bytes
filesize < 500KB // file size constraint
filesize > 1MB and filesize < 5MB
PE Module
For Windows executables — import the PE module for richer conditions based on PE header properties:
import "pe"
rule SuspiciousPE
{
condition:
pe.number_of_sections > 8 // unusual section count
or pe.number_of_imports < 3 // very few imports (packed?)
or pe.entry_point_in_section(".text") == false // EP not in .text
or pe.timestamp == 0 // invalid compile timestamp
}
rule KnownImports
{
condition:
pe.imports("kernel32.dll", "VirtualAlloc") and
pe.imports("kernel32.dll", "WriteProcessMemory") and
pe.imports("kernel32.dll", "CreateRemoteThread")
// Process injection triad
}
rule HasSection
{
condition:
pe.sections[0].name == ".text" and
pe.sections[0].characteristics & pe.SECTION_WRITE // writable .text (suspicious)
}
Scanning
# Scan a single file yara rule.yar sample.exe # Scan a directory recursively yara -r rule.yar /path/to/samples/ # Scan with multiple rule files yara rule1.yar rule2.yar sample.exe # Scan all rules in a directory yara /path/to/rules/*.yar sample.exe # Verbose output (show matching strings) yara -s rule.yar sample.exe # Scan a memory process (requires root) yara -p rule.yar <pid> # Output only filenames (no rule names) yara -l rule.yar /samples/ # Scan and write results to file yara -r rule.yar /samples/ > hits.txt # Negate — show files that do NOT match yara -n rule.yar /samples/
Writing Effective Rules
// Target unique strings — avoid common strings present in benign files
// Bad: $s = "GetProcAddress" (in almost every PE)
// Good: $s = "MalwareFamilySpecificMutex"
// Combine indicators for higher confidence, lower false positives
rule ConfidentDetection
{
strings:
$mutex = "SPECIFIC_MUTEX_NAME"
$c2 = "evil.c2domain.com"
$func = "anti_debug_function_name"
condition:
2 of them // require 2 of 3 — tolerates minor variants
}
// Use hex for binary sequences that may vary in string representation
rule ShellcodeIndicator
{
strings:
$push_ret = { 68 ?? ?? ?? ?? C3 } // PUSH imm32; RET pattern
$nop_sled = { 90 90 90 90 90 90 } // NOP sled
condition:
any of them
}
Community Rule Sources
# YARA rules from threat intel community git clone https://github.com/Yara-Rules/rules git clone https://github.com/Neo23x0/signature-base # Florian Roth's rules (comprehensive) # Scan with community rules yara -r signature-base/yara/ suspicious_file.exe
Integration
# Use with Python (yara-python) pip install yara-python --break-system-packages python3 << 'EOF' import yara rules = yara.compile('rule.yar') matches = rules.match('sample.exe') for m in matches: print(f"Rule: {m.rule}") for s in m.strings: print(f" Match: {s}") EOF # Clamav can convert YARA rules to ClamAV signatures # Wazuh supports YARA integration for file scanning on agents # Velociraptor has built-in YARA hunt capability
Tips
- Start with
2 of themrather thanall of themfor resilience against minor malware variants that alter one indicator - The XOR modifier catches a very common obfuscation technique in malware — try it on string indicators that might be encoded
- Florian Roth's
signature-baserepository is the most comprehensive community rule set — use it as a reference when writing your own rules pe.imports()conditions are more reliable indicators than string matches — the PE import table is harder for malware to meaningfully alter without breaking functionality- Test rules against benign files as well as malicious ones before deploying — a rule that fires on legitimate software is worse than no rule