---
name: malware-yara
description: Guides writing and running YARA rules to detect a malware family or hunt for related samples across a set, including building a rule from a sample, tuning for hit quality, and scaling with yara. Use when the user asks to write, tune or test a YARA rule, detect a malware family, or scan files for related samples.
license: Apache-2.0
metadata:
  verified: "2026-11-17"
---

# YARA hunting

Turn what you learned from one sample into detection for the whole family.
Stage rules and targets in your own isolated analysis sandbox (marq runs none of these tools; work on a copy of the sample); scan with `yara`.

**Engine note:** since **YARA-X** 1.0 (2025), classic YARA 4.x is in maintenance
mode (bug-fixes only) and YARA-X is the reference implementation VirusTotal runs at
scale. The rules below are standard YARA syntax and run on either, but YARA-X is
stricter: it rejects some sloppy patterns 4.x tolerated (unescaped `{` or invalid
escapes in regexes, `base64` strings under 3 chars, duplicate rule modifiers), and
it adds a few things 4.x lacks (`.len()`, hex/octal jump bounds), so passing one
engine doesn't guarantee the other. Classic `yara` is 4.x; if you
author for YARA-X, check with `yr check` and keep
strings/conditions tight.

## Write a rule from a sample

- Pull candidate signatures from `rabin2 -z` strings: distinctive strings
  (C2 templates, mutex names, PDB paths, unique error messages) and stable byte
  sequences (decryption stubs, config markers).
- Prefer **few strong, specific** strings over many weak ones. A good rule
  matches the family, not the packer or the compiler boilerplate.

```
rule FamilyName {
  meta:
    author = "marq"
    ref = "sha256 of the pivot sample"
  strings:
    $a = "unique-c2-template-%s.php" ascii
    $b = { 8B 45 ?? 33 C9 8A 4C 05 ?? }   // decrypt stub, wildcards for variety
  condition:
    uint16(0) == 0x5A4D and 2 of them
}
```

## Tune for hit quality

- Anchor on `uint16(0) == 0x5A4D` (PE) / `0x457f` (ELF) to cut false positives.
- Wildcard volatile bytes (`??`) so minor variants still match.
- Test against both the sample (must hit) and known-good files (must not):
  scan `/usr/bin` with `yara` to sanity-check for false positives.

## Run & scale

- `yara` with `-r` to recurse a directory of samples; `-s` to print the
  matching strings (proof). Cluster the hits; a rule that catches N unrelated
  samples is too broad.
- Good rules are an IOC of the most durable kind (TTP/tooling on the pyramid of
  pain). Record the rule itself in the finding evidence, and feed family names
  back into `ORKL` (`load_skill ioc-pivoting`) for existing reporting.
