What You'll Learn
- Execute YARA scans against files, directories, disk images, memory dumps, and live process memory
- Apply performance optimization flags (-p, --timeout, compiled rules) for large-scale scanning
- Interpret YARA scan output including match details, string offsets, and metadata
- Organize multi-rule scanning with rule files, include directives, and tag filtering
- Explain when to use each scan target type during an incident response investigation
- Connect scanning techniques to the ransomware indicator hunting exercise in Lab 7.4
From Writing Rules to Hunting with Them
You can write precise YARA rules with sophisticated strings and airtight conditions. Now it is time to deploy them. Writing a rule is only half the job — the other half is knowing where to point it, how to optimize scanning performance, and how to interpret the results.
In a real SOC, you will scan everything from a single suspicious file to entire disk images seized during incident response. Each scan target has different characteristics, different performance considerations, and different operational contexts.
Scan Targets: What Can YARA Scan?
YARA is remarkably flexible in what it can scan. Every target is treated as a sequence of bytes — YARA does not care about file formats, operating systems, or encodings. If it is bytes, YARA can scan it.
Single File Scans
The simplest use case: scan one file with one or more rules.
yara rules.yar suspect.exe
When to use: Initial triage of a suspicious file. Someone reports a strange attachment, your email gateway quarantines a binary, or Velociraptor collects a file from an endpoint. You scan it immediately to see if it matches any known indicators.
Pro tip: Always use -s to see which strings matched:
yara -s rules.yar suspect.exe
This shows not just whether the file matches but exactly which patterns triggered and at what byte offsets — essential for understanding what makes this file suspicious.
Directory Scans (Recursive)
Scan every file in a directory and its subdirectories:
yara -r rules.yar /uploads/
yara -r rules.yar /var/www/
yara -r rules.yar /tmp/
The -r flag enables recursive scanning. Without it, YARA only scans files in the specified directory, not subdirectories.
When to use: Hunting for webshells in web directories, scanning upload folders for malicious content, checking temp directories for attacker tools, or sweeping a user's home directory after a compromise is confirmed.
Common scan targets during investigations:
| Directory | Why Scan It |
|---|---|
/var/www/ or C:\inetpub\wwwroot\ | Webshell hunting — attackers plant shells in web-accessible paths |
/tmp/, /var/tmp/, /dev/shm/ | Attacker staging — tools and payloads dropped in world-writable dirs |
C:\Users\Public\, C:\ProgramData\ | Malware hiding — directories accessible to all users, often overlooked |
C:\Users\<user>\AppData\ | User-context malware — droppers and RATs in user profile directories |
/home/<user>/ | Linux user compromise — check for dropped tools, modified dotfiles |
| Email quarantine directory | Scanning quarantined attachments against latest threat intel rules |
Disk Image Scans
When you receive a forensic disk image (dd, E01, or raw format), mount it and scan:
# Mount the forensic image (read-only)
sudo mount -o ro,loop evidence.dd /mnt/evidence/
# Scan the mounted image
yara -r rules.yar /mnt/evidence/
When to use: Digital forensics and incident response. Law enforcement or your IR team provides a disk image from a seized or compromised system. You scan the entire filesystem to find every instance of known malware, attacker tools, and suspicious files without booting the compromised OS.
The advantage of scanning a mounted image over a live system is safety — the compromised OS is not running, so the attacker cannot detect your investigation, modify evidence, or trigger anti-forensics mechanisms.
Memory Dump Scans
YARA can scan raw memory dumps captured from running systems:
yara rules.yar memdump.raw
yara rules.yar process_memory.dmp
When to use: Detecting fileless malware, process injection, and packed/encrypted payloads that only exist in memory. A malware sample might be packed on disk (no recognizable strings) but unpacked in memory (all strings visible). YARA scanning the memory dump catches what disk scanning misses.
Memory dumps also capture:
- Decrypted C2 configurations
- Injected shellcode in legitimate process memory
- Unpacked malware after runtime deobfuscation
- Encryption keys that exist only in memory
Memory dumps can be very large (gigabytes for a full system dump). Scanning a 16GB memory dump with hundreds of rules can take significant time. Use --timeout to prevent individual rules from hanging, and -p for parallel scanning threads to utilize all CPU cores.
Network Capture (Extracted Files)
Extract files from network captures (PCAP) and scan them:
# Extract files from PCAP using tshark or NetworkMiner
tshark -r capture.pcap --export-objects http,/extracted/
# Scan extracted files
yara -r rules.yar /extracted/
When to use: When you have network traffic captures and want to identify malicious files that were transferred. HTTP downloads, email attachments, SMB file transfers — any file that traversed the network can be extracted and scanned.
Live Process Memory (Linux)
On Linux, you can scan the memory of running processes:
# Scan all running process memory
sudo yara -r rules.yar /proc/*/
# Scan a specific process
sudo yara rules.yar /proc/1234/mem
When to use: When you suspect fileless malware or process injection on a live system. The attacker's code may exist only in the memory space of a legitimate process (like svchost.exe or apache2) with no file on disk to scan. Scanning /proc/ catches these in-memory threats.
Performance Optimization
When you move from scanning a single file to scanning thousands of files with hundreds of rules, performance becomes critical. A poorly optimized scan can take hours instead of minutes.
Parallel Scanning (-p)
yara -r -p 8 rules.yar /target/
The -p N flag uses N parallel threads. Set N to the number of CPU cores available. On an 8-core system, -p 8 provides near-linear speedup for directory scans.
Timeout (--timeout)
yara -r --timeout=60 rules.yar /target/
The --timeout=N flag skips files that take longer than N seconds to scan. Some files (highly compressed archives, files with patterns that trigger regex backtracking) can cause YARA to hang. A 60-second timeout prevents any single file from blocking the entire scan.
Compiled Rules
# Compile rules (one-time operation)
yarac all_rules.yar compiled_rules.yarc
# Use compiled rules (faster loading)
yara -r compiled_rules.yarc /target/
Compiling rules converts them from text format to a binary format that loads significantly faster. If you have 500+ rules, compiling them saves several seconds of parsing time on every scan invocation. Compile once, scan many times.
Performance Best Practices
| Technique | Impact | When to Apply |
|---|---|---|
-p N (parallel threads) | Major speedup | Always for directory scans |
--timeout=60 | Prevents hangs | Always for untrusted targets |
Compiled rules (yarac) | Faster rule loading | When using 50+ rules |
filesize in conditions | Major speedup | Always — skips files before string matching |
| Limit regex complexity | Prevents backtracking | Rules with complex regex patterns |
Tag filtering (-t) | Skip irrelevant rules | When hunting for a specific threat type |
-c (count only) | Skip output formatting | When you only need match counts |
The filesize condition check is also a performance optimization. When YARA evaluates a condition that starts with filesize < 500KB, it checks the file size before loading the file into memory for string matching. Files that exceed the size limit are skipped instantly, without reading a single byte of content. For a scan of /var/www/ that includes a 2GB database backup, the filesize check saves the time of reading and scanning those 2 billion bytes.
Interpreting YARA Output
Understanding scan output is as important as writing the rules. YARA's output format varies based on the flags you use.
Default Output
$ yara malware_rules.yar /samples/
LockBit3_Ransomware /samples/ransom.exe
CobaltStrike_Beacon /samples/beacon.dll
Format: rule_name matched_file_path
With String Details (-s)
$ yara -s malware_rules.yar /samples/ransom.exe
LockBit3_Ransomware /samples/ransom.exe
0x1a3:$note1: your data are stolen and encrypted
0x2f1:$ext: .lockbit
0x445:$shadow: vssadmin delete shadows
0x89a:$mutex: Global\lockbit
Each matching string shows:
- Byte offset (
0x1a3) — where in the file the string was found - Variable name (
$note1) — which string from the rule matched - Matched content — the actual text that was found
Negated Output (-n)
yara -rn clean_file_rules.yar /uploads/
The -n flag shows files that do NOT match. This is useful for finding files that are not recognized by any of your rules — potential unknown threats.
Count Output (-c)
$ yara -rc malware_rules.yar /samples/
LockBit3_Ransomware /samples/: 3
CobaltStrike_Beacon /samples/: 1
Webshell_PHP /samples/: 7
Shows only the count of files matching each rule. Useful for getting a quick overview of a scan without detailed output.
Organizing Rules for Real Operations
In production, you will not run a single rule against a single file. You will maintain a library of hundreds of rules and need to organize them effectively.
Rule Files and Include Directives
// master_rules.yar
include "malware/ransomware.yar"
include "malware/trojans.yar"
include "webshells/php_shells.yar"
include "webshells/jsp_shells.yar"
include "tools/hacktool_indicators.yar"
include "apt/apt29.yar"
include "apt/apt28.yar"
Use include to compose a master rule file from categorized sub-files. This keeps individual rule files focused and manageable.
Tag Filtering (-t)
YARA rules can have tags:
rule Webshell_PHP : webshell php
{
// ...
}
Scan for only rules with a specific tag:
yara -r -t webshell all_rules.yar /var/www/
This runs only rules tagged webshell, skipping malware, APT, and other rules. Tag filtering reduces scan time and keeps output focused.
CyberBlueSOC Rule Organization
CyberBlueSOC ships with 523+ YARA rules at /opt/yara-rules/, organized by category:
/opt/yara-rules/
├── malware/ # Commodity malware families
├── webshell/ # PHP, ASP, JSP web shells
├── cve_rules/ # Exploit and vulnerability indicators
├── packers/ # Packer and protector detection
├── email/ # Malicious email attachment patterns
├── crypto/ # Cryptocurrency miner indicators
├── apt/ # Advanced persistent threat rules
└── index.yar # Master include file for all rules
You can scan with all rules at once:
yara -r /opt/yara-rules/index.yar /target/
Or scan with a specific category:
yara -r /opt/yara-rules/webshell/*.yar /var/www/
Real-World Scanning Workflow
Here is how an experienced analyst uses YARA during an incident:
Phase 1 — Quick triage. A single suspicious file arrives. Scan with all rules:
yara -s /opt/yara-rules/index.yar suspect_file.exe
Phase 2 — Scope assessment. The file matched a known ransomware family. Now scan the entire affected host for related indicators:
yara -r -p 4 --timeout=60 ransomware_rules.yar /mnt/affected_host/
Phase 3 — Fleet hunting. Deploy the rule across all endpoints using Velociraptor (covered in Lesson 7.5). Hunt at scale to determine if any other systems are infected.
Phase 4 — Retroactive analysis. Scan historical email quarantine, file upload archives, and network capture extractions to determine when the malware first entered the environment:
yara -r -p 8 ransomware_rules.yar /archive/email_quarantine/
yara -r -p 8 ransomware_rules.yar /archive/uploads/2026/
Build a scanning wrapper script. Create a bash script that handles common scanning patterns: timestamp the output, log to a file, use parallel threads, set timeouts, and format results. This saves time during incidents when speed matters:
#!/bin/bash
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
yara -r -s -p 8 --timeout=60 "$1" "$2" | tee "scan_${TIMESTAMP}.log"
echo "Scan complete. Results in scan_${TIMESTAMP}.log"
./yara_scan.sh /opt/yara-rules/index.yar /var/www/
Key Takeaways
- YARA scans six target types: single files, directories (recursive), mounted disk images, memory dumps, network capture extractions, and live process memory
- Use
-rfor recursive directory scans,-sfor string details,-p Nfor parallel threads, and--timeout=Nto prevent hangs - Compile rules with
yaracfor faster loading when using 50+ rules in production - The
filesizecondition is both a detection and performance optimization — files exceeding the limit are skipped without reading content - Memory dump scanning catches fileless malware and packed payloads that only exist in memory
- Organize rules with
includedirectives, tags, and categorized directories for manageable operations - During incidents, follow the workflow: quick triage → scope assessment → fleet hunting → retroactive analysis
- In Lab 7.4, you will scan pre-loaded ransomware samples to identify indicators and write a rule that detects the ransomware family across a file corpus
What's Next
You can now write rules, optimize conditions, and deploy scans at scale on individual systems. But what about scanning across your entire fleet — every endpoint in your organization? In Lesson 7.5, you will learn how to deploy YARA rules through Velociraptor to hunt across all connected endpoints simultaneously, turning a single rule into an enterprise-wide detection capability.
Knowledge Check: Hunting with YARA
10 questions · 70% to pass
Which YARA command-line flag enables recursive scanning of all files in a directory and its subdirectories?
Why is scanning a mounted forensic disk image preferable to scanning a live compromised system?
What type of malware is best detected by scanning memory dumps rather than files on disk?
What does the --timeout=60 flag do when running a YARA scan?
What is the benefit of compiling YARA rules with yarac before scanning?
In YARA output with the -s flag, what does '0x1a3:$note1: your data are stolen' tell you?
How does the filesize condition check also serve as a performance optimization?
In Lab 7.4, you will analyze ransomware samples. Which YARA scan target types would you use to detect ransomware indicators on a compromised system during an active incident?
What does the YARA -n flag do, and why is it useful for threat hunting?
What is the correct order of YARA scanning phases during an incident response engagement?
0/10 answered