ERROR DEXby Akshay kumar
STUDY MODE / TOOLS / YARA
DEFENSIVE · MALWARE PATTERN MATCHING

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.

A rule is strings + condition. The condition is what makes it good — "2 of them" beats "any of them".
False positives kill a rule set. One noisy rule gets the whole feed disabled.
YARA matches bytes, not behaviour. It finds files; it does not tell you what they do.
HOW TO USE IT → WHY IT MATTERSCOMMANDS HOW IT WORKSMISTAKES
LICENCEOpen source (Apache 2)
PLATFORMLinux · Windows · macOS
WRITTEN INC
CREATED BYVictor Alvarez (VirusTotal)

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.

WALKTHROUGH

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.

01

Start with the smallest rule that could work

KALI LINUX
kali@errordex:~$ cat webshell.yar
rule Suspicious_PHP_Webshell
{
  strings:
    $php  = "<?php"
    $eval = "eval("
  condition:
    $php and $eval
}
WHAT THIS MEANSTwo strings and an AND. This will match far too much — every legitimate PHP app uses eval somewhere — but it is the starting point you then tighten.
02

Test it against a clean system FIRST

KALI LINUX
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.
WHAT THIS MEANSThis is the step people skip and then wonder why their rule set got disabled. A rule that fires on the operating system itself is worse than no rule.
03

Tighten until the clean scan is silent

KALI LINUX
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)
}
WHAT THIS MEANSNow it needs PHP + eval + two of three suspicious patterns, inside a 50KB file. That combination is rare in legitimate code and common in webshells.
04

Verify: clean system silent, sample matched

KALI LINUX
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
WHAT THIS MEANSZero false positives, one true match, with the exact byte offsets printed. That is a rule you can deploy — and the offsets tell the analyst where to look in the file.

Flags & options

FLAGWHAT IT DOES
-rRecurse into directories.
-sPrint the matching strings and their offsets.
-f fileScan a file containing a list of paths.
-wSuppress warnings.
-p NRun N threads.
-d name=valueDefine an external variable used in a rule.
-x moduleImport a module (pe, elf, math, hash).
meta / strings / conditionThe three parts of every rule.
filesize < 50KBSize constraint — cuts noise dramatically.
2 of ($s*)Require two strings from a group.
uint16(0) == 0x5A4DMatch 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.

FILE / MEMORYtarget dataSTRINGStext · hex · regexCONDITION2 of ($s*)MATCHrule firedNO MATCHcleanTEST vs CLEANkill false positives
Strings describe the malware; the condition decides when the description is specific enough to be useful

Commands & syntax

A RULE, ANNOTATED
rule Suspicious_PHP_Webshell
{
  meta:
    author = "Akshay Kumar"
    description = "PHP webshell with eval + base64 pattern"
    reference = "internal IR 2026-11"
    rev = 1

  strings:
    $php   = "
SCAN WITH IT
# 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/
CHECK FOR FALSE POSITIVES
# 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/
USEFUL CONDITION PATTERNS
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
KALI LINUX
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.

Legal note. Only use YARA on systems you own or have explicit written permission to test. In India, unauthorised access is punishable under sections 43 and 66 of the IT Act, 2000. Every example on this page assumes a lab environment.

Related