YARA Rules Cheatsheet for Malware Analysis
# Using package manager
sudo apt-get install yara # Debian/Ubuntu
brew install yara # macOS
# From source
git clone https://github.com/VirusTotal/yara.git
cd yara
./bootstrap.sh
./configure
make
sudo make install
# Using Chocolatey
choco install yara
# Using Python (platform-independent)
pip install yara- python
rule RuleName
{
meta :
author = " Analyst Name "
description = " Malware description "
date = " 2024-01-01 "
hash = " SHA256 hash "
strings :
$ string1 = " suspicious string "
$ hex1 = { 4D 5A 90 00 }
$ regex1 = /pattern [ 0 - 9 ] {4 } /
condition :
$ string1 or $ hex1 or $ regex1
}
Type
Example
Description
Text
$s1 = "malware"
Plain text string
Hex
$h1 = { 4D 5A }
Hexadecimal pattern
Regex
$r1 = /mal[0-9]+/
Regular expression
Modifier
Example
Purpose
nocase
$s1 = "malware" nocase
Case-insensitive
wide
$s1 = "malware" wide
Unicode strings
ascii
$s1 = "malware" ascii
ASCII strings
fullword
$s1 = "mal" fullword
Full word match
Operator
Example
Description
and
$s1 and $s2
Both conditions
or
$s1 or $s2
Either condition
not
not $s1
Negation
at
$s1 at 0x1000
Position match
condition :
# s * > 5 // More than 5 strings
# s1 > 2 // String appears more than twice
@ s1 [1 ] < @ s2 [1 ] // Position comparison
condition :
filesize < 1MB
entrypoint == 0x1000
uint16 (0 ) == 0x5A4D // MZ header
private rule InternalRule
{
condition :
true
}
rule PublicRule
{
condition :
InternalRule
}
global rule GlobalRule
{
condition :
true
}
include " ./other_rules.yar "
rule SetExample
{
condition :
OtherRule and ThisRule
}
# Scan single file
yara rule.yar target_file
# Scan directory
yara -r rule.yar directory/
# Output matches only
yara -c rule.yar target
Option
Usage
Purpose
-s
yara -s rule.yar file
Print matching strings
-m
yara -m rule.yar file
Print metadata
-d
yara -d var=value
Define external variable
-t
yara -t rule.yar file
Print tags
rule FastRule
{
strings :
$ mz = { 4D 5A }
condition :
$ mz at 0 and filesize < 1MB
}
# Limit memory usage
yara --stack-size=32MB rule.yar target
import yara
# Compile rules
rules = yara .compile (filepath = 'rules.yar' )
# Match file
matches = rules .match ('target_file' )
# Process matches
for match in matches :
print (f"Rule: { match .rule } " )
print (f"Tags: { match .tags } " )
print (f"Strings: { match .strings } " )
# Pipe results to other tools
yara rules.yar suspicious_file | grep " DETECTED"
# Use with find
find . -type f -exec yara rules.yar {} \;
Use descriptive rule names
Include comprehensive metadata
Start with specific patterns
Use condition combinations
Test against known samples
Use at
operator when possible
Limit string count
Use filesize checks early
Avoid complex regex
Use private rules for common patterns