YARA Rule Syntax
Strings, conditions and metadata.
YARA Rule Syntax is a free Cyber Security Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cyber Security Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Three Sections
A YARA rule has a name and up to three sections: meta, strings, and condition. The condition is the only mandatory section, but real rules use all three.
rule Example_Malware
{
meta:
author = "analyst"
description = "Detects Example family"
strings:
$a = "EvilConfig"
$b = { 6A 40 68 00 30 00 00 }
condition:
$a and $b
}The meta Section
meta holds descriptive metadata that does not affect matching but is vital for triage and maintenance.
author,date,descriptionreference— report or advisory linkhash— sample(s) the rule was built fromtlp— sharing classification
Values are strings, integers, or booleans. Treat meta as documentation: the next responder relies on it.
Text Strings
The strings section defines identifiers (starting with $) bound to patterns. The simplest is a quoted text string.
Modifiers refine matching:
nocase— case-insensitivewide— UTF-16 (two bytes per char, common in Windows)ascii— ASCII (default; combine with wide to match both)fullword— match only as a whole word
strings:
$s1 = "cmd.exe /c" nocase
$s2 = "InjectDll" wide ascii
$s3 = "admin" fullwordHex Strings
For binary patterns, use a hex string in braces. These match raw bytes, ideal for opcodes, magic numbers, and structures.
Hex strings support flexibility:
??— wildcard nibble/byte[n-m]— a jump of n to m arbitrary bytes( A | B )— alternatives
strings:
$h1 = { 4D 5A 90 00 }
$h2 = { E8 ?? ?? ?? ?? 83 C4 04 }
$h3 = { 6A 00 [2-6] FF 15 ?? ?? ?? ?? }
$h4 = { ( B8 | B9 ) 00 00 00 00 }Regular Expressions
YARA supports regex patterns delimited by slashes, with the same modifiers as text strings. Use regex when a pattern varies but follows a structure, like a config marker or a generated mutex name.
Regex is powerful but costly; prefer fixed strings or hex when possible, and anchor regex tightly to avoid scanning the whole file.
strings:
$re1 = /mutex_[a-f0-9]{8}/ nocase
$re2 = /https?:\/\/[a-z0-9]{12,20}\.top\// asciiThe condition Section
condition is a boolean expression that decides a match. Reference string identifiers directly; a bare $a is true if the string was found.
Combine with and, or, not, and parentheses. This is where you encode the logic that separates a real family hit from a coincidence.
condition:
$a and ($b or $c) and not $benignCounting and Sets
YARA offers expressive quantifiers over string sets:
any of them/all of them2 of ($s*)— at least two of a group#a > 3— count of matches of $a exceeds 3
These let you require, say, three of five family markers, balancing sensitivity against false positives.
condition:
3 of ($str*) and #marker >= 2Offsets and File Size
Conditions can reference where a match occurs and the file's size:
$a at 0— match must be at offset 0$a in (0..1024)— within a byte rangefilesize— total size, e.g.filesize < 200KBuint16(0) == 0x5A4D— read an integer at an offset (here the MZ header)
Anchoring matches to position dramatically cuts false positives and speeds scanning.
condition:
uint16(0) == 0x5A4D and filesize < 500KB and $payloadModules
YARA modules expose structured, format-aware data so conditions can reason about parsed fields instead of raw bytes.
pe— PE imports, sections, entry point, signatures, imphashhash— compute md5/sha256 of regionsmath— entropy and statisticself,dotnet,magic
Importing pe, for example, lets you match on imported functions or a specific imphash, far more robust than a literal string.
import "pe"
condition:
pe.imphash() == "a1b2c3d4e5f6..." and
pe.imports("kernel32.dll", "VirtualAllocEx")Iterating Over Sets
For richer logic, YARA supports loops over collections with for. You can express conditions like at least two of these offsets satisfy a property, or any section name matches a pattern.
This is essential when working with module data such as PE sections or imports, where you must inspect a variable-length list rather than a single value.
import "pe"
condition:
for any i in (0..pe.number_of_sections - 1) : (
pe.sections[i].name == ".evil"
)Global, Private, and Tags
Rule-level keywords organize a ruleset:
global rule— its condition gates all other rules in the file (e.g. only scan PE files)private rule— reusable building block, not reported on its own but referenceable by other rules- tags after the rule name classify hits (e.g. for filtering output)
global private rule IsPE { condition: uint16(0) == 0x5A4D }
rule Dropper : trojan dropper {
strings: $a = "drop_payload"
condition: $a
}Quick Check
Reason about a condition's robustness.
Recap
YARA rule syntax in one view:
- A rule =
meta+strings+condition - Text strings with nocase/wide/ascii/fullword modifiers
- Hex strings with wildcards
??, jumps[n-m], alternatives - Regex for structured variable patterns (use sparingly)
conditioncombines strings with and/or/not- Quantifiers: any/all/N of, and count
#a - Anchoring: at, in, filesize, uint reads
- Modules (pe, hash, math) match structured data and imphash
global,private, and tags organize rulesets
Next: practical hunting with strings and hex.
Frequently asked questions
Is the “YARA Rule Syntax” lesson free?
Yes — the full text of “YARA Rule Syntax” is free to read here on the web, and the Cyber Security Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cyber Security Academy course, upgrade to CoddyKit PRO.
What will I learn in “YARA Rule Syntax”?
Strings, conditions and metadata. You practise Cyber Security Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Cyber Security Academy?
No prior experience is required. Cyber Security Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “YARA Rule Syntax” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Cyber Security Academy lesson?
Yes. Every Cyber Security Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.