Skip to content

YARA Rule Syntax: How to Write a YARA Rule

YARA rule syntax with a full annotated example: rule header, meta, strings, condition, imports, private and global rules, and the compile errors you will hit.

Published Updated 5 min read

A YARA rule is a small declarative program: a name, some optional documentation, the byte patterns you care about, and one boolean condition that decides whether a file matches. That is the whole grammar. Most of the pain comes later, from conditions that are too loose or patterns that are too short, not from the syntax itself.

Here is a complete rule in the shape you will see in public sets, then each part on its own. Everything on this page compiles with YARA-X; paste any block into the online editor to check it.

A complete rule, read top to bottom

import "pe"

rule MAL_Win_Loader_GateURL : windows loader
{
    meta:
        description = "Loader that beacons to a gate.php panel"
        author = "yararules.org"
        date = "2026-11-30"
        reference = "internal triage note"
        score = 75

    strings:
        $url = /https?:\/\/[a-z0-9.\-]{4,64}\/gate\.php/ ascii
        $ua  = "Mozilla/4.0 (compatible; MSIE 8.0)" ascii wide
        $mz_stub = { 4D 5A 90 00 03 00 00 00 }

    condition:
        uint16(0) == 0x5A4D
        and filesize < 2MB
        and pe.number_of_sections <= 6
        and $url
        and ($ua or $mz_stub at 0)
}

Read it the way the scanner does. import "pe" makes the PE parser available. The header gives the rule a name and two tags. meta is documentation that rides along with every hit. strings declares three patterns: a regex, a text string matched in both ASCII and UTF-16LE, and a hex string. The condition is checked last and it is ordered on purpose: the cheap tests (uint16(0), filesize) come first, so a 40 GB disk image never gets as far as the regex.

Two details that bite people on their first real rule. Every string you declare has to appear in the condition, directly or through a set like any of them, or the compiler refuses the rule. And $mz_stub at 0 is not the same as $mz_stub: without the offset the pattern matches anywhere, including inside an embedded PE in a document, which may be exactly what you want or a source of false positives.

To run it from a shell:

yr check loader.yar                 # YARA-X: compile only, report errors with line and column
yr scan loader.yar sample.exe       # YARA-X: scan one file
yara -s loader.yar sample.exe       # classic YARA 4: scan and print the matched strings

The rule header

Every rule starts with the rule keyword and an identifier:

rule MAL_Example_Stealer : windows stealer
{
    condition:
        false
}
  • Identifier. Letters, digits and underscores, not starting with a digit, at most 128 characters, and not a keyword (rule, and, them, filesize and the rest). Identifiers are case-sensitive. Public rule sets usually follow a PREFIX_Family_Detail convention described in the style guide.
  • Tags. Optional words after a colon. They do not affect matching; scanners use them to filter or label results (yr scan can print them).
  • Body. Everything between braces.

The meta section

meta stores documentation as key/value pairs. Values can be strings, integers or booleans. Keys may repeat, and many rules list several hash entries.

rule Meta_Example
{
    meta:
        description = "Shows the three meta value types"
        author = "yararules.org"
        date = "2026-09-29"
        score = 70
        experimental = true
    condition:
        true
}

Meta values cannot be used in the condition, but they travel with every match: scanners print them, and the Detection Rule License asks that the author field stays visible when you report matches.

The strings section

Patterns are declared as $identifier = value, optionally followed by modifiers. There are three kinds: text strings in double quotes, hexadecimal strings in braces, and regular expressions between slashes.

rule Strings_Example
{
    strings:
        $text = "cmd.exe /c" ascii wide nocase
        $hex = { 4D 5A 90 00 ?? 00 00 00 }
        $re = /https?:\/\/[a-z0-9.\-]{4,64}\/gate\.php/
    condition:
        any of them
}

Identifiers may be reused as prefixes to build sets ($x1, $x2 → $x*). A bare $ declares an anonymous string, useful in large lists. The strings guide covers escapes and modifiers; every declared string must be used by the condition, directly or through a set such as them, otherwise YARA reports an error.

The condition section

The condition is a boolean expression. It can reference patterns ($a), counts (#a), offsets (@a[1]), file properties (filesize), integers read from the data (uint16(0)), module fields (pe.number_of_sections) and other rules. The conditions guide goes through each operator.

Imports and modules

Modules extend the condition language with parsed file formats and helper functions. They must be imported at the top of the file:

import "pe"

rule PE_Small_Dll
{
    condition:
        pe.is_pe and pe.is_dll() and filesize < 200KB
}

The browser build of YARA-X used on this site supports pe, elf, macho, dotnet, lnk, math, hash, time, console, string, dex, crx, cuckoo and vt. See the modules guide.

Comments

YARA accepts // line comments and /* … */ block comments anywhere whitespace is allowed. Use them for the reasoning behind a tricky condition; use meta for anything a scanner should report.

Private and global rules

Two modifiers change how a rule behaves:

  • private rule: the rule is evaluated and can be referenced by other rules, but it is never reported as a match on its own. Useful for shared building blocks such as "is a small PE file".
  • global rule: the rule acts as a filter: if it is false, no other rule in the same namespace can match. Use it sparingly; it silently hides every other rule.
private rule Is_Small_PE
{
    condition:
        uint16(0) == 0x5A4D and filesize < 3MB
}

rule MAL_Example_Uses_Helper
{
    strings:
        $a = "EvilConfigMarker" ascii
    condition:
        Is_Small_PE and $a
}

A rule can reference another rule only if that rule is declared before it.

Namespaces and includes

When several files are compiled together, each can live in its own namespace so that two packs may reuse a rule name. The include "other.yar" directive pulls another file in at compile time; support depends on the front-end (the command-line tools resolve relative paths, the browser tools on this site compile each file separately instead).

External variables

Rules can read values supplied at scan time instead of hard-coding them. The variable is defined when rules are compiled or scanned (for example yara -d env=prod rules.yar target with YARA 4, or --define with the YARA-X command line) and used like any identifier in the condition. This lets one rule file behave differently per environment without editing it.

Common syntax errors

YARA-X reports errors with a code, a message and the exact line and column. The most frequent ones are an undeclared string in the condition, a declared but unused string, a missing condition: section, and an invalid escape sequence. Duplicate rule names in the same namespace are another classic when you concatenate two public packs into one file.

rule Broken_Example
{
    strings:
        $a = "one"
        $b = "two"
    condition:
        $a and $c    // does not compile: $c is not declared, $b is unused
}

Paste this rule into the editor to see YARA-X point at $c. For style and performance issues that are not errors, run the linter.

Where to go next

Syntax is the easy part. A rule that compiles can still be useless: a four-byte string that hits every second binary, or a regex with no fixed prefix that drags a scan to a crawl. The strings guide is about choosing patterns that are specific and fast, the conditions guide covers counts, offsets and loops, and the cheat sheet is the one-page version of all of it.

FAQ

Which sections does a YARA rule need?
Only the condition section is mandatory. The meta and strings sections are optional, but when present they must appear in the order meta, strings, condition.
Are YARA rule names case-sensitive?
Yes. Identifiers are case-sensitive, may contain letters, digits and underscores, cannot start with a digit and cannot be a keyword.
How do I check a rule for syntax errors?
Run yr check rules.yar with the YARA-X command-line tool, or paste the rule into the online editor on this site, which compiles it with YARA-X as you type.