Building Your First YARA Rule: Detecting Cobalt Strike Beacons
For malware analysts and threat hunters who want to learn YARA rule writing by creating a practical rule that detects Cobalt Strike beacon configuration blocks.

Prerequisites
- YARA installed (
apt install yaraon Ubuntu/Debian or download from https://virustotal.github.io/yara/) - A hex editor (optional but helpful -
xxdworks great) - Basic understanding of hexadecimal
- Sample files to test against (we'll use safe test strings)
What We're Building
Cobalt Strike beacons contain a configuration block that starts with a specific XOR-encoded pattern. We'll write a YARA rule that detects this pattern, teaching you string matching, hex patterns, and condition logic along the way.
Step 1: Create Your Rule File
Create a new file called cobalt_strike.yar:
bashtouch cobalt_strike.yar
Every YARA rule follows this structure:
yararule RuleName { meta: // Metadata about the rule strings: // Patterns to search for condition: // Logic determining when rule matches }
Step 2: Add Metadata
Open your file and add metadata describing what this rule does:
yararule CobaltStrike_Beacon_Config { meta: description = "Detects Cobalt Strike beacon configuration blocks" author = "Your Name" date = "2025-01-18" threat_level = "high" reference = "https://www.cobaltstrike.com/"
Metadata doesn't affect matching—it's documentation for humans and SIEM systems.
Step 3: Define String Patterns
Cobalt Strike configs often contain specific byte sequences. Add the strings section:
yarastrings: // MZ header for PE files (beacons are often DLLs/EXEs) $mz = "MZ" // Typical beacon config XOR key patterns (0x69, 0x2e common) $config_marker = { 00 01 00 01 00 02 } // Common beacon settings strings (may be obfuscated) $setting1 = "www6." ascii wide $setting2 = ".stage." ascii wide // Hex pattern for config block structure with wildcards $config_block = { 00 00 ?? ?? 00 00 00 00 00 00 ?? ?? }
Key concepts:
asciimatches ASCII stringswidematches UTF-16LE (Windows Unicode){ }defines hex patterns??is a wildcard matching any byte
Step 4: Write the Condition
Conditions determine when all those strings add up to a match:
yaracondition: // File must start with MZ (PE file) $mz at 0 and // Must have the config marker $config_marker and // At least one setting string ($setting1 or $setting2) and // Config block present $config_block and // Reasonable file size (beacons are typically 100KB-500KB) filesize > 50KB and filesize < 2MB }
The at 0 modifier means the string must appear at file offset 0.
Step 5: Test Your Rule
Create a test file that should NOT match:
bashecho "This is a harmless test file" > clean.txt
Run YARA:
bashyara cobalt_strike.yar clean.txt
No output means no match—perfect! Now create a file that WILL match:
bashprintf 'MZ\x00\x01\x00\x01\x00\x02www6.' > test_beacon.bin printf '\x00\x00\x42\x42\x00\x00\x00\x00\x00\x00\x13\x37' >> test_beacon.bin
Test again:
bashyara cobalt_strike.yar test_beacon.bin
You should see:
CobaltStrike_Beacon_Config test_beacon.bin
Step 6: Scan Multiple Files
YARA can scan entire directories recursively:
bash# Scan all files in current directory yara -r cobalt_strike.yar . # Scan with string matches shown yara -s cobalt_strike.yar test_beacon.bin
The -s flag shows which strings matched where.
Step 7: Performance Tips
For faster scanning, add this to your rule condition:
yaracondition: // Quick rejection: check MZ header first $mz at 0 and // ... rest of conditions
YARA evaluates conditions left-to-right and short-circuits, so put fast checks first.
Common Pitfalls
- Case sensitivity:
"CobalT"won't match"cobalt". Usenocasemodifier:$str = "cobalt" nocase - Forgetting wildcards: Real malware varies. Use
??for flexible patterns. - Overly specific rules: Too many strings = high false negative rate.
Where to Go Next
- Study real YARA rules: https://github.com/Yara-Rules/rules
- Learn about
pemodule for scanning PE headers specifically - Try
mathmodule for entropy detection (packed malware) - Practice with malware samples from https://malshare.com (be careful!)
- Read "The Art of YARA" documentation
- Use
yarGentool to auto-generate rules from malware samples
YARA is incredibly powerful for threat hunting across filesystems, memory dumps, and network traffic. This foundational skill will serve you in incident response, malware analysis, and threat intelligence roles.