Yara Rule Guide

YARA Rule Guide

YARA is a tool used by malware researchers, security engineers, and threat analysts to identify and classify malware. YARA uses rules, each consisting of a set of strings and a boolean expression, to identify malware.

This guide will walk you through creating a YARA rule.

Structure of a YARA Rule

A basic YARA rule consists of:

  1. Rule Name
  2. Meta Section
  3. Strings Section
  4. Condition Section

Here is the basic structure:

rule Rule_Name {
        description = "Your description here"
        author = "Author's name"
        date = "Creation date"

        $string1 = "string to be matched"
        $string2 = {hex to be matched}

        $string1 or $string2

Rule Name

The rule name is simply the name of your rule. It must start with a letter and can include numbers, _, and -.

Meta Section

The meta section provides additional information about the rule. This information doesn’t influence the rule’s operation and is entirely optional.

Strings Section

The strings section is where you define the strings and hexadecimal bytes that you want YARA to search for. You can define as many or as few as you want.

Condition Section

The condition section is where you define the logic that YARA will use when searching for matches. If the condition is met, then the rule is considered a match.

Writing a YARA Rule

Let’s walk through writing a basic YARA rule.

rule HelloWorld {
        description = "Detects a simple HelloWorld string"
        author = "Your Name"
        date = "2023-06-02"

        $hw_string = "HelloWorld"


This rule looks for the string “HelloWorld”. If it’s found in the target, the rule is considered a match.

Operators and Functions

YARA provides a variety of operators and functions to use in your rules.

Boolean Operators

YARA supports standard boolean operators: and, or, not.

For example:

rule BooleanExample {
        $string1 = "Hello"
        $string2 = "World"

        $string1 and $string2

This rule will only match if both “Hello” and “World” are found.


You can use quantifiers to specify the number of instances of a string.

For example:

rule QuantifierExample {
        $string1 = "Hello"

        2 of them

This rule will only match if “Hello” is found at least twice.

Positional Operators

You can use positional operators to look for strings at specific positions.

For example:

rule PositionalExample {
        $string1 = "Hello"

        $string1 at 0

This rule will only match if “Hello” is found at the beginning of the file.


YARA provides a variety of functions to use in your rules, such as filesize(), int8(), uint16(), etc.

For example:

rule FunctionExample {
        filesize > 100KB

This rule will only match if the file size is greater than 100KB.

Tips for Writing Effective YARA Rules

  1. Be Specific: The more specific your rules are, the fewer false positives you’ll have.

  2. Test

Your Rules: Always test your rules on multiple samples to ensure they’re working correctly.

  1. Use Meta Fields: The meta fields can provide useful context about what the rule is meant to detect.

  2. Comment Your Rules: Commenting your rules can make it easier for others (or you in the future) to understand what the rule is doing.

  3. Keep Up to Date with YARA’s Capabilities: YARA is constantly being updated and improved. Make sure you’re familiar with its latest capabilities to write the best possible rules.

  4. Use Hex Strings for Binary Values: Using hexadecimal strings can be more reliable when matching binary data.

That’s it! You’re now ready to start writing your own YARA rules. Happy hunting!