Yara Rule Authoring
Trail of BitsOfficialPublished by Trail of Bits
- Skill
- Guidelines
- Security
- CC-BY-SA-4.0
- Medium risk
- Has scripts
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a framework for creating and maintaining YARA-X detection rules. It covers best practices for string selection, condition design, and metadata, while offering specific guidance for analyzing Windows, macOS, JavaScript, Chrome extensions, and Android DEX files. The skill includes instructions for using provided scripts to lint rules, analyze string atoms, and perform comprehensive reviews to ensure rules meet established authoring standards.
What it does
- Provides conventions for naming, metadata, and rule structure.
- Offers techniques for string selection and false positive reduction.
- Details how to use YARA-X modules for CRX and DEX file analysis.
- Includes scripts for linting, atom analysis, and rule validation.
- Explains how to migrate legacy YARA rules to the YARA-X format.
- Outlines methods for testing rules against goodware corpora.
When to use it
- When writing new YARA-X rules for malware detection.
- When reviewing or optimizing existing YARA-X rulesets.
- When converting threat intelligence or IOCs into detection signatures.
- When debugging false positives in existing detection rules.
- When analyzing Chrome extensions or Android applications for malicious patterns.
Advantages
- Standardizes rule quality through linting and atom analysis.
- Reduces false positives by enforcing specific string and condition requirements.
- Provides clear guidance on YARA-X module usage for complex file formats.
- Facilitates consistent rule maintenance and peer review.
Usage examples
- “How do I check my YARA-X rule for syntax errors and formatting?”
- The agent will instruct you to use 'yr check rule.yar' for syntax validation and 'yr fmt -w rule.yar' to standardize the rule's formatting.
- “What is the best way to review a YARA-X rule for performance and style?”
- The agent will guide you to use the provided review script, which runs linting and atom analysis to identify issues like short strings or unbounded regex.
- “How can I detect malicious Chrome extensions using YARA-X?”
- The agent will explain how to import the 'crx' module and use fields like 'crx.permissions' to flag high-risk extension behaviors.
- “How do I migrate a legacy YARA rule to YARA-X?”
- The agent will provide steps to use 'yr check --relaxed-re-syntax' to identify compatibility issues and explain how to fix them manually.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/trailofbits/yara-rule-authoring/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip yara-rule-authoring.zip -d .agents/skills
Documentation by Trail of Bits
YARA-X Rule Authoring
Write detection rules that catch malware without drowning in false positives.
Resolve the linter, atom analyzer,
and review helper from this installed SKILL.md's directory.
In commands below, replace each <resolved-...-path> with that script's absolute
path and keep the quotes. The rule file or directory is still relative to your
working directory unless you pass an absolute input path.
This skill targets YARA-X, the Rust-based successor to legacy YARA — 5-10x faster regex, better errors, built-in formatter, stricter validation, new modules (crx, dex), 99% rule compatibility. It powers VirusTotal's production systems. Install with brew install yara-x or cargo install yara-x; the CLI is yr. See Migrating from Legacy YARA for existing rules.
Core Principles
Strings must generate good atoms — YARA extracts 4-byte subsequences for fast matching. Strings with repeated bytes, common sequences, or under 4 bytes force slow bytecode verification on too many files.
Target specific families, not categories — "Detects ransomware" catches everything and nothing. "Detects LockBit 3.0 configuration extraction routine" catches what you want.
Test against goodware before deployment — A rule that fires on Windows system files is useless. Validate against VirusTotal's goodware corpus or your own clean file set.
Short-circuit with cheap checks first —
filesize(instant), then magic bytes (nearly instant), then strings (cheap), then modules (expensive).Metadata is documentation — Future you (and your team) need to know what this catches, why, and where the sample came from.
When to Use
Writing new YARA-X rules for malware detection
Reviewing existing rules for quality or performance issues
Optimizing slow-running rulesets
Converting IOCs or threat intel into detection signatures
Debugging false positive issues
Preparing rules for production deployment
Migrating legacy YARA rules to YARA-X
Analyzing Chrome extensions (crx module) or Android apps (dex module)
When NOT to Use
Static analysis requiring disassembly → use Ghidra/IDA skills
Dynamic malware analysis → use sandbox analysis skills
Network-based detection → use Suricata/Snort skills
Memory forensics with Volatility → use memory forensics skills
Simple hash-based detection → just use hash lists
Platform Considerations
YARA works on any file type. Adapt patterns to your target:
| Platform | Magic Bytes | Bad Strings | Good Strings |
|---|---|---|---|
| Windows PE | uint16(0) == 0x5A4D | API names, Windows paths | Mutex names, PDB paths |
| macOS Mach-O | uint32(0) == 0xFEEDFACE (32-bit), 0xFEEDFACF (64-bit), uint32be(0) == 0xCAFEBABE (universal) | Common Obj-C methods | Keylogger strings, persistence paths |
| JavaScript/Node | (none needed) | require, fetch, axios | Obfuscator signatures, eval+decode chains |
| npm/pip packages | (none needed) | postinstall, dependencies | Suspicious package names, exfil URLs |
| Office docs | uint32(0) == 0x04034B50 | VBA keywords | Macro auto-exec, encoded payloads |
| VS Code extensions | (none needed) | vscode.workspace | Uncommon activationEvents, hidden file access |
| Chrome extensions | Use crx module | Common Chrome APIs | Permission abuse, manifest anomalies |
| Android apps | Use dex module | Standard DEX structure | Obfuscated classes, suspicious permissions |
uintNN()reads little-endian. Write the constant as the bytes reversed, or useuintNNbe()and write them in file order. A ZIP/OOXML file starts with bytes50 4B 03 04, so it isuint32(0) == 0x04034B50—uint32(0) == 0x504B0304compiles cleanly and never matches anything. The same trap catches Mach-O universal binaries: on disk they areCA FE BA BE, souint32(0) == 0xCAFEBABEis a dead branch; writeuint32be(0) == 0xCAFEBABEoruint32(0) == 0xBEBAFECA. Verify withyr scanagainst one known-good sample before trusting any magic-byte check.
macOS Malware Detection
No dedicated Mach-O module exists yet — use magic bytes plus string patterns. Good indicators:
Keylogger artifacts:
CGEventTapCreate,kCGEventKeyDownSSH tunnel strings:
ssh -D,tunnel,socksPersistence paths:
~/Library/LaunchAgents,/Library/LaunchDaemonsCredential theft:
security find-generic-password,keychain
// Pattern from Airbnb BinaryAlert
rule SUSP_Mac_ProtonRAT
{
strings:
$lib1 = "SRWebSocket" ascii // Library indicators
$lib2 = "SocketRocket" ascii
$behav1 = "SSH tunnel not launched" ascii // Behavioral indicators
$behav2 = "Keylogger" ascii
condition:
(uint32(0) == 0xFEEDFACF or uint32be(0) == 0xCAFEBABE) and
any of ($lib*) and any of ($behav*)
}JavaScript Detection
| Target | Approach |
|---|---|
| npm package | package.json patterns, postinstall/preinstall hooks, exfil combination: fetch + env access + credential paths |
| Chrome extension | crx module |
| Other extension | Manifest patterns, background script behaviors |
| Standalone JS | Obfuscation markers (eval+atob, fromCharCode chains), unique function/variable names, packed payloads |
| Minified/webpack bundle | Unique strings that survive bundling (URLs, magic values); avoid function names — they get mangled |
Good JS strings: Ethereum function selectors — { a9 05 9c bb } (transfer(address,uint256)), { 70 a0 82 31 } (balanceOf(address)); zero-width characters for steganography — { E2 80 8B E2 80 8C }; obfuscator signatures — _0x, var _0x; specific C2 domains and webhook URLs.
Bad JS strings: require, fetch, axios (too common); Buffer, crypto (legitimate uses everywhere); process.env alone (need specific env var names).
String Selection
Value ranking: mutex names are gold, C2 paths silver, error messages bronze. Stack strings are almost always unique. If you need more than 6 strings, you're over-fitting.
Reject a candidate string when any of these holds:
| Test | Why it fails | Do instead |
|---|---|---|
| Under 4 bytes | No atom | Find a longer string |
Repeated bytes (0000, 9090) | Weak atom | Add surrounding context |
API name (VirtualAlloc, CreateRemoteThread) | Every packer and installer calls it | Hex pattern of the call site plus a unique marker |
| Appears in Windows system files | Guaranteed FPs | Find something family-specific |
Common path (C:\Windows\, cmd.exe) | Ubiquitous | Find malware-specific paths |
| Appears in other malware families | Not identifying this family | Combine with a family-specific marker |
Everything left — unique to this family — is what the rule should rest on.
Choosing a String Type
| Need | Use |
|---|---|
| Exact ASCII/Unicode text | $s = "MutexName" ascii wide |
| Specific byte sequence | $h = { 4D 5A 90 00 } |
| Byte sequence with variation | Hex wildcards: { 4D 5A ?? ?? 50 45 } |
| Pattern with structure (URLs, paths) | Bounded regex: /https:\/\/[a-z]{5,20}\.onion/ |
| Unknown encoding (XOR, base64) | Modifier: $s = "config" xor(0x00-0xFF) |
Modifier discipline: never use nocase or wide speculatively — only with confirmed evidence that case or encoding varies across samples. nocase doubles atom generation; wide doubles string matching. "If you don't have a clear reason for using those modifiers, don't do it" — Kaspersky Applied YARA.
Condition Design
Order for short-circuit: filesize <, magic bytes, strings, modules. If the condition runs past 5 lines, split into multiple rules.
all of vs any of
| Situation | Use |
|---|---|
| Strings are individually unique to the malware | any of them — each alone is suspicious |
| Strings are common but the combination is suspicious | all of them — require the full pattern |
| Strings have different confidence levels | Group: all of ($core_*) and any of ($variant_*) |
| Seeing false positives | Tighten: any → all, add more required strings |
Lesson from production: rules using any of ($network_*) where the strings included fetch, axios, and http matched virtually all web applications. Switching to require a credential path AND a network call AND an exfil destination eliminated the FPs.
Grouping by Confidence
Different indicator types carry different weight — a C2 domain might be definitive while library imports need corroboration. Grouping by prefix lets you express graduated requirements:
strings:
$a1 = "SRWebSocket" ascii // Category A: library indicators
$a2 = "SocketRocket" ascii
$b1 = "SSH tunnel" ascii // Category B: behavioral
$b2 = "keylogger" ascii nocase
$c1 = /https:\/\/[a-z0-9]{8,16}\.onion/ // Category C: C2
condition:
filesize < 10MB and
any of ($a*) and any of ($b*) // Evidence from BOTH categoriesModules vs Byte Checks
| Need | Use |
|---|---|
| imphash, rich header, authenticode | PE module — too complex to replicate |
| Magic bytes or simple offsets | uint16/uint32 — faster, no module overhead |
| Section names/sizes | PE module, but put the magic-byte filter FIRST |
| Chrome extension permissions | crx module — string parsing is fragile |
| LNK target paths | lnk module — the format is complex |
"Avoid the magic module — use explicit hex checks instead" — Neo23x0. Generalize it: if uint32() can do the job, don't load a module.
Performance
Regex must be anchored to a 4+ byte literal. Without one it evaluates at every file offset — catastrophic. Write
/mshta\.exe http:\/\/.../, not/http:\/\/.../. If you can't anchor, use a hex pattern with wildcards.Bound every regex quantifier —
.{0,30}, never.*. Unbounded regex is both a performance disaster and a memory explosion.Bound loops with filesize —
filesize < 100KB and for all i in (1..#a) : .... Unbounded#acan reach thousands in large files.Prefer hex over regex where the bytes are fixed.
Before Writing: Is the Sample Packed?
| Signal | What to do |
|---|---|
| Entropy > 7.0 | Likely packed — find the unpacked layer first |
| Few or no readable strings | Likely packed — use entropy, PE structure, or packer signatures |
| UPX/MPRESS/custom packer detected | Target the unpacked payload OR detect the packer itself |
| Readable strings available | Proceed with string-based detection |
Don't write rules against packed layers. The packing changes; the payload doesn't.
When Strings Fail, Pivot to Structure
If extraction returns only API names and generic paths:
| Available signal | Use |
|---|---|
| High entropy sections | math.entropy() on specific sections |
| Unusual import pattern | pe.imphash() for import-hash clustering |
| PE structure anomalies | Section names, sizes, characteristics |
| Metadata present | Version info, timestamps, resources |
| Nothing unique | This sample may not be detectable with YARA alone |
"One can try to use other file properties, such as metadata, entropy, import hashes or other data which stays constant." — Kaspersky Applied YARA Training
Debugging False Positives
Which string matched? —
yr scan -s rule.yar false_positive.exeIn a legitimate library? — add a
not $fp_vendor_stringexclusionA common development pattern? — replace the string with something more specific
Multiple generic strings matching together? — tighten to require all, plus a unique marker
Malware using a common technique? — target its specific implementation details, not the technique
When to Abandon the Approach
Extraction returns only API names and paths → pivot to structure
Can't find 3 unique strings → probably packed; target the unpacked version or detect the packer
Rule matches goodware → 1-2 matches: investigate and tighten; 3-5: find different indicators; 6+: start over
Performance is terrible after optimization → architecture problem; split into focused rules or add strict pre-filters
The description is hard to write → the rule is too vague. If you can't explain what it catches, it catches too much
Rationalizations to Reject
When you catch yourself thinking these, stop and reconsider.
| Rationalization | Expert Response |
|---|---|
| "This generic string is unique enough" / "This hex pattern is unique" | Unique in one sample ≠ unique across the ecosystem. Test against goodware; your intuition is wrong. |
| "yarGen gave me these strings" | yarGen suggests, you validate. Check each one manually — expect to discard 80%. |
| "It works on my 10 samples" | 10 samples ≠ production. Use a goodware corpus. |
| "One rule to catch all variants" | Causes FP floods. Target specific families. |
| "I'll make it more specific if we get FPs" / "I'll add more conditions later" | Write tight rules upfront. A weak rule deployed is damage done, and FPs burn trust. |
| "This is just for hunting" | Hunting rules become detection rules. Same quality bar. |
| "The API name makes it malicious" | Legitimate software uses the same APIs. Need behavioral context. |
"any of them is fine for these common strings" | Common strings + any = FP flood. Use any of only for individually unique strings. |
| "This regex is specific enough" | /fetch.*token/ matches all auth code. Add an exfil destination requirement. |
"I'll use .* for flexibility" | Unbounded regex = performance disaster plus memory explosion. Use .{0,30}. |
| "The JavaScript looks clean" | Attackers poison legitimate code with injects. Check for eval+decode chains. |
| "Performance doesn't matter" | One slow rule slows the entire ruleset. Optimize atoms. |
"I'll use --relaxed-re-syntax everywhere" | Masks real bugs. Fix the regex instead of hiding the problem. |
| "PEiD rules still work" | Obsolete. 32-bit packers aren't relevant. |
Toolkit
| Tool | Purpose |
|---|---|
| yr CLI | yr check (validate), yr fmt (format), yr scan -s (scan, show strings), yr dump -m pe (inspect structure) |
| yarGen | Extract candidate strings: yarGen.py -m samples/ --excludegood |
| FLOSS | Extract obfuscated/stack strings: floss sample.exe — when yarGen comes up empty |
| signature-base | Study quality examples |
| YARA-CI | Goodware corpus testing before deployment |
Master these five. Don't get distracted by tool catalogs.
Development cycle:
yr check rule.yar # syntax, with precise line numbers
yr fmt -w rule.yar # standardize formatting
yr dump -m pe sample.exe --output-format yaml # inspect structure, no dummy rule needed
time yr scan -s rule.yar corpus/ # scan with timingReach for yr dump when investigating which module fields are available, debugging why a module condition isn't matching, or exploring a new module (crx, lnk, dotnet) before writing against it. YARA-X error messages carry precise source locations — if yr check says line 15, the problem is on line 15.
Version-gated features: private $helper = "pattern" matches but stays out of output (v1.3.0+); // suppress: slow_pattern silences a specific warning inline (v1.4.0+); filesize < 10_000_000 numeric underscores (v1.5.0+). $_unused also suppresses unused-string warnings.
Chrome Extension Analysis (crx module)
Requires YARA-X v1.5.0+, or v1.11.0+ for permhash().
Key APIs: crx.is_crx, crx.permissions, crx.permhash()
Red flags: nativeMessaging + downloads, debugger permission, content scripts on <all_urls>
import "crx"
rule SUSP_CRX_HighRiskPerms {
condition:
crx.is_crx and
for any perm in crx.permissions : (perm == "debugger")
}See crx-module.md for the full API, permission risk assessment, and example rules.
Android DEX Analysis (dex module)
Requires YARA-X v1.11.0+. Not compatible with legacy YARA's dex module — the API is completely different.
Key APIs: dex.is_dex, dex.contains_class(), dex.contains_method(), dex.contains_string()
Red flags: single-letter class names (obfuscation), DexClassLoader reflection, encrypted assets
import "dex"
rule SUSP_DEX_DynamicLoading {
condition:
dex.is_dex and
dex.contains_class("Ldalvik/system/DexClassLoader;")
}See dex-module.md for the full API, obfuscation detection, and example rules.
Migrating from Legacy YARA
99% rule compatibility, but stricter validation:
yr check --relaxed-re-syntax rules/ # identify issues
# fix each one, then verify without relaxed mode:
yr check rules/| Issue | Legacy | YARA-X Fix |
|---|---|---|
Literal { in regex | /{/ | /\{/ |
| Invalid escapes | \R silently literal | \\R or R |
| Base64 strings | Any length | 3+ chars required |
| Negative indexing | @a[-1] | @a[#a - 1] |
| Duplicate modifiers | Allowed | Remove duplicates |
--relaxed-re-syntax is a diagnostic, not a destination. Fix the regex.
Naming and Metadata
{CATEGORY}_{PLATFORM}_{FAMILY}_{VARIANT}_{DATE} e.g. MAL_Win_Emotet_Loader_Jan25Categories: MAL_ (malware), HKTL_ (hacking tool), WEBSHELL_, EXPL_, SUSP_ (suspicious), GEN_ (generic). Platforms: Win_, Lnx_, Mac_, Android_, CRX_.
Every rule needs description (starting with "Detects"), author, reference, and date:
meta:
description = "Detects Example malware via unique mutex and C2 path"
author = "Your Name <[email protected]>"
reference = "https://example.com/analysis"
date = "2025-01-29"See style-guide.md for full conventions.
Workflow
Gather samples — multiple; single-sample rules are brittle
Extract candidates —
yarGen -m samples/ --excludegoodValidate quality — apply the string selection tests; expect to discard 80% of yarGen output
Write the rule — proper metadata, cheap checks first
Lint and test —
yr check,yr fmt, the linter scriptGoodware validation — VirusTotal corpus or local clean files
Deploy — full metadata, then monitor for FPs
Quality signals along the way: a rule matching under 50% of known variants is too narrow; one matching goodware is too broad.
Reviewing a rule someone else wrote — run both scripts before reading the rule by eye, and quote the codes they emit:
uv run "<resolved-yara-lint-path>" suspect.yar # style, metadata, YARA-X compatibility
uv run "<resolved-atom-analyzer-path>" suspect.yar # atom quality per stringShortcut: "<resolved-review-path>" suspect.yar (a rule file or a directory)
runs yr check, yr fmt --check, the linter and the atom analyzer in one call
and prints one JSON object, {"target": ..., "checks": {"yr_check", "yr_format", "lint", "atoms"}}, each with status (the tool's exit code) and its output.
It exits 0 when every check passes, 1 when any check fails, and 2 for a missing,
empty or unreadable input (nothing was inspected). Quote the codes from
checks.lint.output exactly as with the separate scripts.
They catch the mechanical faults — short strings, FP-prone substrings, unbounded quantifiers, expensive terms ahead of cheap ones — so your attention goes to the judgement calls they cannot make: whether the strings identify this family, and whether the condition can fire on generic strings alone. Report findings by code (E002, W009) so the author can look each one up in style-guide.md.
See testing.md for the validation workflow and rule-development.md for the full step-by-step guide.
Common Mistakes
| Mistake | Bad | Good |
|---|---|---|
| API names as indicators | "VirtualAlloc" | Hex pattern of call site + unique mutex |
| Unbounded regex | /https?:\/\/.*/ | /https?:\/\/[a-z0-9]{8,12}\.onion/ |
| Missing file type filter | pe.imports(...) first | uint16(0) == 0x5A4D and filesize < 10MB first |
| Short strings | "abc" (3 bytes) | "abcdef" (4+ bytes) |
| Unescaped braces (YARA-X) | /config{key}/ | /config\{key\}/ |
| Wrong-endian magic bytes | uint32(0) == 0xCAFEBABE | uint32be(0) == 0xCAFEBABE |
Quality Checklist
Before deploying any rule:
[ ] Name follows
{CATEGORY}_{PLATFORM}_{FAMILY}_{VARIANT}_{DATE}[ ] Description starts with "Detects" and explains what/how
[ ] All required metadata present (author, reference, date)
[ ] Strings are unique — not API names, common paths, or format strings
[ ] All strings 4+ bytes with good atom potential
[ ] Base64 modifier only on strings with 3+ characters
[ ] Regex bounded, anchored to a literal, with
{escaped[ ] Condition starts with cheap checks (filesize, magic bytes)
[ ] Magic-byte constants verified against a known-good sample
[ ] Rule matches all target samples
[ ] Rule produces zero matches on the goodware corpus
[ ]
yr checkandyr fmt --checkpass[ ] Linter passes with no errors
[ ] Peer review completed
Scripts
uv run "<resolved-yara-lint-path>" rule.yar # validate style/metadata
uv run "<resolved-atom-analyzer-path>" rule.yar # check string quality
"<resolved-review-path>" rule.yar # all four checks, one JSON resultSee README.md for detailed script documentation.
Further Reading
| Topic | Document |
|---|---|
| Naming and metadata conventions | style-guide.md |
| Performance and atom optimization | performance.md |
| String types and judgment | strings.md |
| Testing and validation | testing.md |
| Chrome extension module (crx) | crx-module.md |
| Android DEX module (dex) | dex-module.md |
| Complete rule development process | rule-development.md |
The examples/ directory holds real, attributed rules worth reading before writing your own:
| Example | Demonstrates | Source |
|---|---|---|
| MAL_Win_Remcos_Jan25.yar | PE malware: graduated string counts, multiple rules per family | Elastic Security |
| MAL_Mac_ProtonRAT_Jan25.yar | macOS: Mach-O magic bytes, multi-category grouping | Airbnb BinaryAlert |
| MAL_NPM_SupplyChain_Jan25.yar | npm supply chain: real attack patterns, ERC-20 selectors | Stairwell Research |
| SUSP_JS_Obfuscation_Jan25.yar | JavaScript: obfuscator detection, density-based matching | imp0rtp3, Nils Kuhnert |
| SUSP_CRX_SuspiciousPermissions.yar | Chrome extensions: crx module, permissions | Educational |
Rule repositories to learn from: Neo23x0/signature-base (17,000+ production rules), elastic/protections-artifacts (endpoint-tested), imp0rtp3/js-yara-rules (JavaScript), InQuest/awesome-yara (curated index).
Guides: YARA Style Guide and YARA Performance Guidelines (Neo23x0), YARA-X documentation.
macOS specifics: Apple's own production rules ship at /System/Library/CoreServices/XProtect.bundle/; objective-see publishes macOS malware research and samples.
Files
- ATTRIBUTION.mdAdded by AutoGineer946 B
- LICENSELicence file added by AutoGineer19.7 KiB
- SKILL.md24.2 KiB
- agents/openai.yaml236 B
- assets/trail-of-bits-mark.svg3.0 KiB
- examples/MAL_Mac_ProtonRAT_Jan25.yar3.2 KiB
- examples/MAL_NPM_SupplyChain_Jan25.yar5.3 KiB
- examples/MAL_Win_Remcos_Jan25.yar3.1 KiB
- examples/SUSP_CRX_SuspiciousPermissions.yar4.2 KiB
- examples/SUSP_JS_Obfuscation_Jan25.yar5.7 KiB
- references/crx-module.md7.3 KiB
- references/dex-module.md10.1 KiB
- references/performance.md8.7 KiB
- references/strings.md11.6 KiB
- references/style-guide.md9.3 KiB
- references/testing.md13.1 KiB
- scripts/atom_analyzer.pyScript4.6 KiB
- scripts/pyproject.toml553 B
- scripts/review.shScript2.9 KiB
- scripts/test_yara_rules.pyScript17.7 KiB
- scripts/uv.lock3.4 KiB
- scripts/yara_lint.pyScript4.7 KiB
- scripts/yara_rules.pyScript30.4 KiB
- workflows/rule-development.md13.2 KiB
scripts/yara_rules.py
"""Dependency-free parsing and analysis of YARA-X rule sources.
Shared by yara_lint.py and atom_analyzer.py. Deliberately imports nothing outside
the standard library so it can be unit tested without the yara-x wheel; the two
CLI wrappers add YARA-X compilation on top of what lives here.
The parsers are hand-written scanners rather than regexes because YARA string
values routinely contain the delimiters a regex would trip over -- `\\/` inside a
regex pattern, `\\"` inside a text string, `{0,30}` inside a quantifier.
"""
from __future__ import annotations
import re
from dataclasses import dataclass
from dataclasses import field
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Iterator
# A string this long or longer makes `nocase` expensive enough to mention.
NOCASE_LENGTH_THRESHOLD = 20
# Minimum bytes for YARA to extract a usable 4-byte atom.
MIN_ATOM_BYTES = 4
DESCRIPTION_MIN_CHARS = 60
DESCRIPTION_MAX_CHARS = 400
VALID_CATEGORY_PREFIXES = frozenset(
{
"MAL",
"HKTL",
"WEBSHELL",
"EXPL",
"VULN",
"SUSP",
"PUA",
"GEN",
"APT",
"CRIME",
"RANSOM",
"RAT",
"MINER",
"STEALER",
"LOADER",
"C2",
}
)
# Substrings that show up in goodware often enough to be worth flagging.
FP_PRONE_STRINGS = (
"CreateRemoteThread",
"WriteProcessMemory",
"ReadProcessMemory",
"NtCreateThread",
"VirtualAlloc",
"VirtualProtect",
"powershell.exe",
"explorer.exe",
"notepad.exe",
"ADVAPI32.dll",
"KERNEL32.dll",
"C:\\Windows\\System32",
"C:\\Windows",
"USER32.dll",
"ntdll.dll",
"cmd.exe",
"https://",
"http://",
"%08x",
"%s",
"%d",
"%x",
)
DEPRECATED_PATTERNS = {
"entrypoint": "Use pe.entry_point instead of deprecated entrypoint",
"PEiD": "PEiD-style signatures are obsolete; use modern detection",
}
# Checks that cost close to nothing and belong at the front of a condition.
CHEAP_CHECK_RE = re.compile(
r"\bfilesize\b|\bu?int(?:8|16|32)(?:be)?\s*\(|\b\w+\.is_\w+\b",
)
# Anything that reads pattern-match state or a parsed module structure.
EXPENSIVE_CHECK_RE = re.compile(
r"[$#@!]\w*|\bfor\b|\b(?:any|all|none|\d+)\s+of\b|\b\w+\.\w+",
)
# The complete set of codes yara_lint.py can emit. references/style-guide.md
# publishes this table for users; test_yara_rules.py asserts the two agree, so a
# check added here without a doc entry (or vice versa) fails the build.
#
# E007 (unescaped `{` in regex) is retired: YARA-X's own compiler reports it as
# E000 with the exact source location and a suggested fix.
ISSUE_CODES = {
"E000": "YARA-X compilation error",
"E001": "Missing required metadata field (description, author, date)",
"E002": "Text string shorter than 4 bytes",
"E003": "Hex string shorter than 4 bytes",
"E006": "base64 modifier on a string shorter than 3 characters",
"E008": "Negative array indexing, unsupported in YARA-X",
"W001": "Rule name does not follow CATEGORY_PLATFORM_FAMILY_DATE",
"W002": "Description does not start with 'Detects'",
"W003": "Description shorter than 60 characters",
"W004": "Missing 'reference' metadata",
"W005": "String contains an FP-prone substring",
"W006": "Hex string starts with a wildcard",
"W007": "Deprecated feature used in condition",
"W008": "Unbounded regex quantifier (.* or .+)",
"W009": "Condition reaches pattern or module state before any cheap pre-filter",
"I001": "Unrecognized category prefix",
"I002": "Description longer than 400 characters",
"I003": "nocase on a string longer than 20 characters",
"I004": "xor without an explicit key range",
}
@dataclass
class Issue:
"""A linting issue."""
rule: str
severity: str # error, warning, info
code: str
message: str
line: int | None = None
def to_dict(self) -> dict:
return {
"rule": self.rule,
"severity": self.severity,
"code": self.code,
"message": self.message,
"line": self.line,
}
@dataclass
class LintResult:
"""Result of linting a file."""
file: str
issues: list[Issue] = field(default_factory=list)
parse_error: str | None = None
rules: int = 0
@property
def error_count(self) -> int:
return sum(1 for i in self.issues if i.severity == "error")
@property
def warning_count(self) -> int:
return sum(1 for i in self.issues if i.severity == "warning")
@dataclass
class AtomIssue:
"""An issue with atom quality."""
string_id: str
severity: str # error, warning, info
message: str
suggestion: str | None = None
@dataclass
class StringAnalysis:
"""Analysis of a single string's atom quality."""
string_id: str
string_type: str
raw_value: str
byte_count: int
issues: list[AtomIssue]
best_atom: str | None = None
@dataclass
class YaraString:
"""One entry from a rule's `strings:` section."""
name: str
value: str
type: str # text, byte, regex
modifiers: list[str] = field(default_factory=list)
# --------------------------------------------------------------------------- #
# Scanners
# --------------------------------------------------------------------------- #
def _scan_quoted(text: str, start: int) -> tuple[str | None, int]:
"""Scan a double-quoted YARA text string starting at `start`.
Honours backslash escapes so `"say \\"hi\\""` reads as one value.
"""
pos = start + 1
while pos < len(text):
char = text[pos]
if char == "\\":
pos += 2
continue
if char == '"':
return text[start + 1 : pos], pos + 1
if char == "\n":
break
pos += 1
return None, start + 1
def _scan_regex(text: str, start: int) -> tuple[str | None, int]:
"""Scan a `/pattern/` regex literal starting at `start`.
Escaped slashes and slashes inside a character class do not terminate the
pattern -- the reason a naive `/[^/]*/` regex mis-parses most URL patterns.
"""
pos = start + 1
in_class = False
while pos < len(text):
char = text[pos]
if char == "\\":
pos += 2
continue
if char == "\n":
break
if in_class:
if char == "]":
in_class = False
elif char == "[":
in_class = True
elif char == "/":
return text[start + 1 : pos], pos + 1
pos += 1
return None, start + 1
def _scan_hex(text: str, start: int) -> tuple[str | None, int]:
"""Scan a `{ ... }` hex string starting at `start`."""
depth = 0
pos = start
while pos < len(text):
char = text[pos]
if char == "{":
depth += 1
elif char == "}":
depth -= 1
if depth == 0:
return text[start + 1 : pos], pos + 1
pos += 1
return None, start + 1
def _modifiers_after(text: str, pos: int) -> list[str]:
"""Read the modifier tokens trailing a string value, minus any comment."""
newline = text.find("\n", pos)
rest = text[pos:] if newline == -1 else text[pos:newline]
rest = re.sub(r"/\*.*?\*/", " ", rest)
rest = rest.split("//", 1)[0]
return rest.split()
def strip_comments(source: str) -> str:
"""Blank out `//` and `/* */` comments, preserving offsets and line breaks."""
out = list(source)
pos = 0
while pos < len(source):
char = source[pos]
if char == '"':
_, pos = _scan_quoted(source, pos)
continue
if char == "/" and pos + 1 < len(source):
following = source[pos + 1]
if following == "/":
end = source.find("\n", pos)
end = len(source) if end == -1 else end
for i in range(pos, end):
out[i] = " "
pos = end
continue
if following == "*":
end = source.find("*/", pos + 2)
end = len(source) if end == -1 else end + 2
for i in range(pos, end):
if out[i] != "\n":
out[i] = " "
pos = end
continue
pos += 1
return "".join(out)
# --------------------------------------------------------------------------- #
# Rule structure
# --------------------------------------------------------------------------- #
def extract_rule_names(content: str) -> list[str]:
"""Extract rule names from YARA source."""
return re.findall(r"(?:private\s+|global\s+)*rule\s+(\w+)\s*[:{]", strip_comments(content))
def extract_rule_body(content: str, rule_name: str) -> str:
"""Return the body of `rule_name`, between its outermost braces.
Brace counting skips text strings, regex literals and comments, so a `}`
inside `$s = "}"` or a `{0,30}` quantifier does not truncate the body.
"""
clean = strip_comments(content)
header = re.search(rf"\brule\s+{re.escape(rule_name)}\s*(?::[^{{]*)?\{{", clean)
if not header:
return ""
pos = header.end()
depth = 1
last_significant = "{"
while pos < len(clean) and depth > 0:
char = clean[pos]
if char == '"':
_, pos = _scan_quoted(clean, pos)
last_significant = '"'
continue
if char == "/" and last_significant == "=":
_, pos = _scan_regex(clean, pos)
continue
if char == "{":
depth += 1
elif char == "}":
depth -= 1
if depth == 0:
return clean[header.end() : pos]
if not char.isspace():
last_significant = char
pos += 1
return clean[header.end() : pos]
def _section(body: str, name: str, followers: tuple[str, ...]) -> str:
"""Return the text of a `name:` section, up to the next section keyword."""
stop = "|".join(rf"{f}\s*:" for f in followers)
match = re.search(rf"\b{name}\s*:\s*(.*?)(?={stop}|$)", body, re.DOTALL)
return match.group(1) if match else ""
def extract_metadata(content: str, rule_name: str) -> dict[str, str]:
"""Extract the `meta:` key/value pairs of a rule."""
section = _section(extract_rule_body(content, rule_name), "meta", ("strings", "condition"))
return dict(re.findall(r'(\w+)\s*=\s*"([^"]*)"', section))
def extract_strings(content: str, rule_name: str) -> list[YaraString]:
"""Extract the `strings:` entries of a rule, in source order."""
section = _section(extract_rule_body(content, rule_name), "strings", ("condition",))
strings: list[YaraString] = []
for match in re.finditer(r"(\$\w*)\s*=\s*", section):
pos = match.end()
if pos >= len(section):
break
delimiter = section[pos]
if delimiter == '"':
value, end = _scan_quoted(section, pos)
kind = "text"
elif delimiter == "{":
value, end = _scan_hex(section, pos)
kind = "byte"
elif delimiter == "/":
value, end = _scan_regex(section, pos)
kind = "regex"
else:
continue
if value is None:
continue
strings.append(
YaraString(
name=match.group(1),
value=value.strip() if kind == "byte" else value,
type=kind,
modifiers=_modifiers_after(section, end),
)
)
return strings
def extract_condition(content: str, rule_name: str) -> str:
"""Return a rule's condition text, comments already stripped."""
body = extract_rule_body(content, rule_name)
match = re.search(r"\bcondition\s*:\s*(.*)", body, re.DOTALL)
return match.group(1).strip() if match else ""
# --------------------------------------------------------------------------- #
# Lint checks
# --------------------------------------------------------------------------- #
def check_naming_convention(rule_name: str) -> Iterator[Issue]:
"""Check if rule name follows the style guide convention."""
parts = rule_name.split("_")
if len(parts) < 3:
yield Issue(
rule=rule_name,
severity="warning",
code="W001",
message=f"Rule name '{rule_name}' should follow CATEGORY_PLATFORM_FAMILY_DATE format",
)
return
if parts[0] not in VALID_CATEGORY_PREFIXES:
valid = ", ".join(sorted(VALID_CATEGORY_PREFIXES))
yield Issue(
rule=rule_name,
severity="info",
code="I001",
message=f"Unrecognized category prefix '{parts[0]}'; expected one of: {valid}",
)
def check_metadata(rule_name: str, metadata: dict[str, str]) -> Iterator[Issue]:
"""Check for required and well-formed metadata."""
for field_name in ("description", "author", "date"):
if field_name not in metadata:
yield Issue(
rule=rule_name,
severity="error",
code="E001",
message=f"Missing required metadata field: {field_name}",
)
if "description" in metadata:
desc = metadata["description"]
if not desc.startswith("Detects"):
yield Issue(
rule=rule_name,
severity="warning",
code="W002",
message="Description should start with 'Detects'",
)
if len(desc) < DESCRIPTION_MIN_CHARS:
yield Issue(
rule=rule_name,
severity="warning",
code="W003",
message=f"Description too short ({len(desc)} chars); aim for 60-400 characters",
)
if len(desc) > DESCRIPTION_MAX_CHARS:
yield Issue(
rule=rule_name,
severity="info",
code="I002",
message=f"Description quite long ({len(desc)} chars); consider trimming to <400",
)
if "reference" not in metadata:
yield Issue(
rule=rule_name,
severity="warning",
code="W004",
message="Missing 'reference' metadata; add URL to analysis or source",
)
def check_strings(rule_name: str, strings: list[YaraString]) -> Iterator[Issue]:
"""Check strings for anti-patterns and quality issues."""
for string in strings:
if string.type == "text":
yield from _check_text_string(rule_name, string)
elif string.type == "byte":
yield from _check_hex_string(rule_name, string)
elif string.type == "regex":
yield from _check_regex_string(rule_name, string)
def _check_text_string(rule_name: str, string: YaraString) -> Iterator[Issue]:
if len(string.value) < MIN_ATOM_BYTES:
yield Issue(
rule=rule_name,
severity="error",
code="E002",
message=f"String {string.name} is only {len(string.value)} bytes; "
f"minimum {MIN_ATOM_BYTES} bytes for good atoms",
)
lowered = string.value.lower()
for fp_string in FP_PRONE_STRINGS:
if fp_string.lower() in lowered:
yield Issue(
rule=rule_name,
severity="warning",
code="W005",
message=f"String {string.name} contains FP-prone pattern '{fp_string}'",
)
break
if "base64" in string.modifiers and len(string.value) < 3:
yield Issue(
rule=rule_name,
severity="error",
code="E006",
message=f"String {string.name} uses 'base64' but is only {len(string.value)} "
"chars; YARA-X requires 3+ characters for base64 modifier",
)
def _check_hex_string(rule_name: str, string: YaraString) -> Iterator[Issue]:
byte_count = sum(len(run) for run in hex_string_runs(string.value))
if byte_count < MIN_ATOM_BYTES:
yield Issue(
rule=rule_name,
severity="error",
code="E003",
message=f"Hex string {string.name} has only {byte_count} bytes; "
f"minimum {MIN_ATOM_BYTES} for good atoms",
)
if re.match(r"^\s*\?\?", string.value):
yield Issue(
rule=rule_name,
severity="warning",
code="W006",
message=f"Hex string {string.name} starts with wildcard; "
"move fixed bytes first for better atoms",
)
def _check_regex_string(rule_name: str, string: YaraString) -> Iterator[Issue]:
if re.search(r"(?<!\\)\.[*+](?!\?)", string.value):
yield Issue(
rule=rule_name,
severity="warning",
code="W008",
message=f"Regex {string.name} has an unbounded quantifier (.* or .+); "
"use a bounded repetition such as .{0,100}",
)
def check_string_modifiers(rule_name: str, strings: list[YaraString]) -> Iterator[Issue]:
"""Check string modifiers for performance concerns."""
for string in strings:
if "nocase" in string.modifiers and len(string.value) > NOCASE_LENGTH_THRESHOLD:
yield Issue(
rule=rule_name,
severity="info",
code="I003",
message=f"String {string.name} uses 'nocase' on long string; performance impact",
)
if "xor" in string.modifiers:
yield Issue(
rule=rule_name,
severity="info",
code="I004",
message=f"String {string.name} uses 'xor' without a range; "
"generates 255 patterns -- prefer xor(0xNN) when the key is known",
)
def check_condition(rule_name: str, condition: str) -> Iterator[Issue]:
"""Check a condition for deprecated features and YARA-X incompatibilities."""
if not condition:
return
for pattern, message in DEPRECATED_PATTERNS.items():
if pattern.lower() in condition.lower():
yield Issue(rule=rule_name, severity="warning", code="W007", message=message)
if re.search(r"@\w+\s*\[\s*-\d+\s*\]", condition):
yield Issue(
rule=rule_name,
severity="error",
code="E008",
message="Negative array indexing (e.g., @a[-1]) not supported in YARA-X; "
"use @a[#a - 1] instead",
)
def check_condition_order(rule_name: str, condition: str) -> Iterator[Issue]:
"""Flag conditions that reach pattern or module state before any cheap pre-filter.
Cheap means `filesize`, a `uintNN()` magic-byte read, or a module's `is_*`
file-type gate. If an expensive term comes first -- or there is no cheap
filter at all -- every scanned file pays for it.
"""
if not condition:
return
cheap = CHEAP_CHECK_RE.search(condition)
redacted = CHEAP_CHECK_RE.sub(lambda m: " " * len(m.group()), condition)
expensive = EXPENSIVE_CHECK_RE.search(redacted)
if expensive is None:
return
if cheap is None or expensive.start() < cheap.start():
term = expensive.group().strip()
yield Issue(
rule=rule_name,
severity="warning",
code="W009",
message=f"Condition reaches '{term}' before any cheap pre-filter; "
"lead with filesize or a uintNN magic-byte check",
)
def lint_source(content: str) -> list[Issue]:
"""Run every style check over a YARA source string."""
issues: list[Issue] = []
for rule_name in extract_rule_names(content):
issues.extend(check_naming_convention(rule_name))
issues.extend(check_metadata(rule_name, extract_metadata(content, rule_name)))
strings = extract_strings(content, rule_name)
issues.extend(check_strings(rule_name, strings))
issues.extend(check_string_modifiers(rule_name, strings))
condition = extract_condition(content, rule_name)
issues.extend(check_condition(rule_name, condition))
issues.extend(check_condition_order(rule_name, condition))
return issues
# --------------------------------------------------------------------------- #
# Atom analysis
# --------------------------------------------------------------------------- #
# Repeated byte patterns that generate poor atoms.
REPEATED_PATTERNS = (
(b"\x00\x00\x00\x00", "null bytes (0x00000000)"),
(b"\x90\x90\x90\x90", "NOP sled (0x90909090)"),
(b"\xcc\xcc\xcc\xcc", "INT3 padding (0xCCCCCCCC)"),
(b"\xff\xff\xff\xff", "all 0xFF bytes"),
(b"\x20\x20\x20\x20", "spaces (0x20202020)"),
)
# Common 4-byte sequences that appear in many files.
COMMON_SEQUENCES = (
b"This",
b"prog",
b"MODE",
b"rich",
b".tex",
b".dat",
b".rsr",
b"MZ\x90\x00",
b"http",
b"HTTP",
)
# One hex token: wildcard byte, nibble wildcard, literal byte, jump, or grouping.
_HEX_TOKEN_RE = re.compile(r"\?\?|[0-9A-Fa-f]\?|\?[0-9A-Fa-f]|[0-9A-Fa-f]{2}|\[[^\]]*\]|[()|]")
@dataclass
class HexRun:
"""A contiguous stretch of a hex string with no jump or alternation in it."""
data: bytes
wildcards: list[int]
def __len__(self) -> int:
return len(self.data)
def hex_string_runs(hex_str: str) -> list[HexRun]:
"""Split a YARA hex string into contiguous runs of adjacent bytes.
Jumps (`[2-4]`) and alternations (`( 90 00 | 00 00 )`) break adjacency, so the
bytes on either side are not a candidate atom together and must not be
concatenated into one.
"""
runs: list[HexRun] = []
data = bytearray()
wildcards: list[int] = []
def flush() -> None:
if data:
runs.append(HexRun(bytes(data), list(wildcards)))
data.clear()
wildcards.clear()
for match in _HEX_TOKEN_RE.finditer(hex_str.strip().strip("{}")):
token = match.group()
if token == "??" or "?" in token:
wildcards.append(len(data))
data.append(0x00)
elif re.fullmatch(r"[0-9A-Fa-f]{2}", token):
data.append(int(token, 16))
else:
flush()
flush()
return runs
def score_atom(atom: bytes) -> int:
"""Score a 4-byte atom for quality (0-100)."""
if len(atom) != MIN_ATOM_BYTES:
return 0
score = 100
unique = len(set(atom))
if unique == 1:
score -= 80
elif unique == 2:
score -= 40
score -= atom.count(0x00) * 15
for pattern, _ in REPEATED_PATTERNS:
if pattern in atom:
score -= 60
break
for seq in COMMON_SEQUENCES:
if seq in atom:
score -= 30
break
if all(0x20 <= b <= 0x7E for b in atom):
score -= 10
return max(0, score)
def find_best_atom(data: bytes, wildcard_positions: list[int]) -> tuple[str | None, int]:
"""Find the highest-scoring wildcard-free 4-byte window in a byte sequence."""
best_atom = None
best_score = 0
wildcards = set(wildcard_positions)
for i in range(len(data) - MIN_ATOM_BYTES + 1):
if wildcards & set(range(i, i + MIN_ATOM_BYTES)):
continue
atom = data[i : i + MIN_ATOM_BYTES]
score = score_atom(atom)
if score > best_score:
best_score = score
best_atom = atom.hex().upper()
return best_atom, best_score
def _atom_score_issues(string_id: str, score: int) -> Iterator[AtomIssue]:
if score < 30:
yield AtomIssue(
string_id=string_id,
severity="error",
message=f"Best atom score is {score}/100; string will cause slow scanning",
suggestion="Choose a more unique string or add distinguishing bytes",
)
elif score < 60:
yield AtomIssue(
string_id=string_id,
severity="warning",
message=f"Best atom score is {score}/100; may cause performance issues",
)
def analyze_text_string(string_id: str, value: str, modifiers: list[str]) -> StringAnalysis:
"""Analyze a text string for atom quality."""
issues: list[AtomIssue] = []
byte_count = len(value)
if byte_count < MIN_ATOM_BYTES:
issues.append(
AtomIssue(
string_id=string_id,
severity="error",
message=f"String is only {byte_count} bytes; no valid 4-byte atom possible",
suggestion="Use a longer string (4+ bytes minimum)",
)
)
return StringAnalysis(string_id, "text", value, byte_count, issues)
if "base64" in modifiers and byte_count < 3:
issues.append(
AtomIssue(
string_id=string_id,
severity="error",
message=f"String uses 'base64' but is only {byte_count} chars; "
"YARA-X requires 3+ characters for base64 modifier",
suggestion="Use a string of 3+ characters with base64 modifier",
)
)
data = value.encode("utf-8", errors="surrogateescape")
best_atom, score = find_best_atom(data, [])
issues.extend(_atom_score_issues(string_id, score))
if "nocase" in modifiers and byte_count > NOCASE_LENGTH_THRESHOLD:
issues.append(
AtomIssue(
string_id=string_id,
severity="info",
message="'nocase' on long string doubles atom generation",
suggestion="Consider if case-insensitivity is truly needed",
)
)
if "wide" in modifiers and "ascii" in modifiers:
issues.append(
AtomIssue(
string_id=string_id,
severity="info",
message="'wide ascii' doubles matching; ensure both encodings are needed",
)
)
return StringAnalysis(string_id, "text", value, byte_count, issues, best_atom)
def analyze_hex_string(string_id: str, value: str) -> StringAnalysis:
"""Analyze a hex string for atom quality."""
issues: list[AtomIssue] = []
runs = hex_string_runs(value)
byte_count = sum(len(run) for run in runs)
if byte_count < MIN_ATOM_BYTES:
issues.append(
AtomIssue(
string_id=string_id,
severity="error",
message=f"Hex string is only {byte_count} bytes; no valid 4-byte atom possible",
suggestion="Use a longer hex pattern (4+ bytes minimum)",
)
)
return StringAnalysis(string_id, "byte", value, byte_count, issues)
first = runs[0]
if {0, 1} <= set(first.wildcards):
issues.append(
AtomIssue(
string_id=string_id,
severity="warning",
message="Hex string starts with wildcards; atoms will be extracted from middle/end",
suggestion="Move fixed bytes to the beginning if possible",
)
)
wildcard_count = sum(len(run.wildcards) for run in runs)
if wildcard_count and wildcard_count / byte_count > 0.5:
issues.append(
AtomIssue(
string_id=string_id,
severity="warning",
message=f"High wildcard density ({wildcard_count / byte_count:.0%}); "
"may limit atom options",
)
)
best_atom, score = None, 0
for run in runs:
atom, run_score = find_best_atom(run.data, run.wildcards)
if atom is not None and run_score > score:
best_atom, score = atom, run_score
if best_atom is None:
issues.append(
AtomIssue(
string_id=string_id,
severity="error",
message="No valid 4-byte atom found (wildcards or jumps break every window)",
suggestion="Reduce wildcards or add a longer run of fixed bytes",
)
)
else:
issues.extend(_atom_score_issues(string_id, score))
return StringAnalysis(string_id, "byte", value, byte_count, issues, best_atom)
def analyze_rule(rule_name: str, content: str) -> Iterator[StringAnalysis]:
"""Analyze the atom quality of every text and hex string in a rule.
Regex patterns are skipped: their atoms come out of the compiled automaton,
not the source text, so scoring them here would be guesswork.
"""
for string in extract_strings(content, rule_name):
if string.type == "text":
yield analyze_text_string(string.name, string.value, string.modifiers)
elif string.type == "byte":
yield analyze_hex_string(string.name, string.value)
# --------------------------------------------------------------------------- #
# Scan targets
# --------------------------------------------------------------------------- #
RULE_SUFFIXES = (".yar", ".yara")
def collect_rule_files(path: Path) -> tuple[list[Path], str | None]:
"""Resolve a CLI path into the rule files to inspect.
Returns `(files, error)`. `error` is set whenever there is nothing to
inspect, so a directory holding no rules fails loudly instead of reporting
a clean run over zero files.
"""
if not path.exists():
return [], f"{path} does not exist"
if path.is_file():
return [path], None
if not path.is_dir():
return [], f"{path} is neither a file nor a directory"
files = sorted(p for p in path.rglob("*") if p.suffix in RULE_SUFFIXES and p.is_file())
if not files:
suffixes = " or ".join(RULE_SUFFIXES)
return [], f"no {suffixes} files found under {path}; nothing was inspected"
return files, None
def coverage_error(rule_counts: dict[str, int]) -> str | None:
"""Return an error message when a run inspected no rules at all.
Judged over the whole run, not per file. A rule-less file inside a larger
run is normal -- rulesets commonly keep a shared `import "pe"` header in its
own file -- so failing per file would reject a healthy directory. Only a run
that found nothing anywhere is reporting a clean result over zero items.
"""
if not rule_counts:
return "no files were inspected"
if sum(rule_counts.values()) == 0:
names = ", ".join(sorted(rule_counts))
return f"no rules found in {names}; nothing was inspected"
return None
def rule_less_files(rule_counts: dict[str, int]) -> list[str]:
"""Files that held no rules, for reporting as a note rather than a failure."""
return sorted(name for name, count in rule_counts.items() if count == 0)
def select_exit_code(results: list[LintResult], *, strict: bool) -> int:
"""Exit 1 on any error, any unreadable file, or -- under --strict -- any warning."""
if any(r.parse_error for r in results):
return 1
if sum(r.error_count for r in results) > 0:
return 1
if strict and sum(r.warning_count for r in results) > 0:
return 1
return 0
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Needs Python
Security
What our scanner noticed· Medium risk
- References to credential files
- Scripts that your agent may run
This skill includes scripts that your agent may run. Read them before you use it.
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Published by
- Trail of Bits
- Publisher
- Trail of Bits (Official)
- Source
- trailofbits/skills · plugins/yara-authoring/skills/yara-rule-authoring · commit e4dd0bb
- Licence
- Creative Commons Attribution-ShareAlike 4.0 International (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by Trail of Bits or Trail of Bits.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download yara-rule-authoring.zip