YARA
YARA is the pattern-matching language of malware analysis. You describe what a piece of malware looks like, and YARA tells you which files on a disk — or in a memory dump — match that description.
What it is
A YARA rule has three parts: meta (author, description, reference — for humans), strings (what to look for: text, hex, or regex), and condition (the logic that decides a match).
Strings can be text ("cmd.exe /c"), hex with wildcards ({4D 5A ?? ?? 50 45}), or regular expressions. The condition is where quality lives: all of them, 2 of ($s*), $a and #b > 5, or file-size and offset constraints.
How to use YARA
Follow it exactly as you would in a real session — every command, every line of output, and what each result actually tells you. This is a terminal walkthrough.
Start with the smallest rule that could work
kali@errordex:~$ cat webshell.yar
rule Suspicious_PHP_Webshell
{
strings:
$php = "<?php"
$eval = "eval("
condition:
$php and $eval
}eval somewhere — but it is the starting point you then tighten.Test it against a clean system FIRST
kali@errordex:~$ yara -r webshell.yar /usr/share/php/ | head
Suspicious_PHP_Webshell /usr/share/php/PEAR/Task/Postinstall.php Suspicious_PHP_Webshell /usr/share/php/php-gettext/gettext.inc … 47 more matches # 49 false positives. The rule is useless as written.
Tighten until the clean scan is silent
kali@errordex:~$ cat webshell2.yar
rule Suspicious_PHP_Webshell
{
strings:
$php = "<?php" ascii
$eval = "eval(" ascii
$b64 = /base64_decode\s*\(/ ascii
$post = "$_POST" ascii
$req = "$_REQUEST" ascii
condition:
filesize < 50KB and $php and $eval and 2 of ($b64, $req, $post)
}Verify: clean system silent, sample matched
kali@errordex:~$ yara -r webshell2.yar /usr/share/php/ | wc -l && yara -s webshell2.yar shell.php
0 Suspicious_PHP_Webshell shell.php 0x12:$php: <?php 0x34:$eval: eval( 0x51:$b64: base64_decode( 0x6e:$post: $_POST
Flags & options
| FLAG | WHAT IT DOES |
|---|---|
| -r | Recurse into directories. |
| -s | Print the matching strings and their offsets. |
| -f file | Scan a file containing a list of paths. |
| -w | Suppress warnings. |
| -p N | Run N threads. |
| -d name=value | Define an external variable used in a rule. |
| -x module | Import a module (pe, elf, math, hash). |
| meta / strings / condition | The three parts of every rule. |
| filesize < 50KB | Size constraint — cuts noise dramatically. |
| 2 of ($s*) | Require two strings from a group. |
| uint16(0) == 0x5A4D | Match the MZ header — identify Windows executables. |
Why it matters
When a new malware family appears, someone has to describe it in a way a scanner can use. YARA is that description language — used by VirusTotal, every EDR vendor, and incident responders scanning disks for a known implant. Writing your own rule for something you found is how you turn one analysis into detection for the whole organisation.
How it works
YARA compiles each rule into a set of string matchers and a condition expression, then scans the target bytes. Text strings use an efficient multi-pattern matcher; hex and regex patterns are compiled into automata. The condition is evaluated only when enough strings have matched, which is why a strict condition also makes scanning faster.
Because it matches bytes, YARA is fast and precise but blind to behaviour: packed or encrypted malware will not match until it is unpacked in memory.
Commands & syntax
rule Suspicious_PHP_Webshell
{
meta:
author = "Akshay Kumar"
description = "PHP webshell with eval + base64 pattern"
reference = "internal IR 2026-11"
rev = 1
strings:
$php = "# single file yara webshell.yar suspicious.php # whole directory, recursive, show rule names yara -r webshell.yar /var/www/html/ # print matching strings too yara -s -r webshell.yar /var/www/html/
# the test that matters — a good rule matches nothing here yara -r webshell.yar /usr/bin/ 2>/dev/null | head # and against a Windows system folder if you have one yara -r webshell.yar /mnt/windows/System32/
all of them // strict, low FP any of them // loose, noisy 2 of ($s*) // balanced $a at 0 and $b in (0..1024) // position-aware #eval > 3 // count occurrences filesize < 1MB and uint16(0) == 0x5A4D // MZ header
analyst@errordex:~$ yara -s webshell.yar /var/www/html/uploads/ Suspicious_PHP_Webshell /var/www/html/uploads/shell.php 0x12:$php: <?php 0x34:$eval: eval( 0x51:$b64: base64_decode( 0x6e:$post: $_POST # Four strings matched, condition required $php + $eval + 2 of the rest. # Named match: you can now block, quarantine or hunt for this elsewhere. analyst@errordex:~$ yara -r webshell.yar /usr/bin/ | head -3 (no output) # No matches on a clean system = the rule is specific enough to deploy.
Common mistakes
Generic strings
"CreateFile" or "cmd.exe" appear in thousands of legitimate files. Use distinctive combinations.
Using "any of them"
It guarantees false positives. Require at least two, or a positional constraint.
No file-size limit
A 4 GB archive will be scanned and matched on incidental bytes.
Never testing against clean files
This is the only test that matters and it is the one most often skipped.
Expecting YARA to find behaviour
It matches bytes. Packed or encrypted malware needs unpacking first.
One rule for a whole family
Families share code but differ in details. Write per-variant rules and tag the family in meta.
Practice safely
Start with the official YARA documentation and the "yara-rules" community repository. The best practice corpus is your own machine: write a rule for a known tool, then run it across /usr/bin and see how many false positives you get.