Use YARA pattern-matching rules to hunt for malware, suspicious files, and indicators of compromise across filesystems and memory dumps. Covers rule authoring, yara-python scanning, and integration with threat intel feeds.
Instalar com Codex ou Claude Copie este prompt, cole no Codex, Claude ou outro assistente e deixe que ele revise a página da skill e instale para você.
Um comando direto ignora o prompt de revisão. Verifique a origem antes de executá-lo.
Instruções da origem · Visualização somente leitura
name
performing-threat-hunting-with-yara-rules
description
Use YARA pattern-matching rules to hunt for malware, suspicious files, and indicators of compromise across filesystems and memory dumps. Covers rule authoring, yara-python scanning, and integration with threat intel feeds.
["Executable Denylisting","Execution Isolation","File Metadata Consistency Validation","Content Format Conversion","File Content Analysis"]
nist_csf
["DE.CM-01","DE.AE-02","DE.AE-07","ID.RA-05"]
Performing Threat Hunting with YARA Rules
Scan files, directories, and memory dumps using YARA rules to identify
malware families, suspicious patterns, and IOC matches.
When to Use
Proactively hunting for unknown malware variants across network shares, endpoints, and email attachments
Scanning quarantine directories or sandbox outputs for malware family classification
Searching process memory dumps for injected code or in-memory-only payloads
Validating threat intelligence IOCs against a large corpus of collected samples
Triaging incident response artifacts to identify known malware families quickly
Building automated detection pipelines that scan new files on ingestion
Do not use for real-time endpoint protection (use EDR agents instead); YARA scanning is best suited for batch hunting, triage, and post-collection analysis where scan latency is acceptable.
Detection Gaps & Validation
Condition pitfalls cause silent false-negatives: omitting a filesize bound makes large files hit yara.TimeoutError (skipped, not matched); $mz at 0 fails on packed/prepended or memory-carved samples; hardcoded offsets break after repacking.
Packers defeat string rules: UPX/Themida and .NET obfuscators mangle the exact strings yarGen extracted, so the rule never fires on the live variant — pair string sets with pe/math.entropy heuristics and scan the unpacked process in memory.
Memory-scan timing: sleep-mask/encryption re-hides beacon config between callbacks — scan during active C2 or from an unhooked full dump, not a quiesced one.
Community rule hazards: one syntax error fails the whole compile (use per-file fallback loading), and over-broad rules flood false positives.
Validate: run each rule against a known-positive sample AND a goodware corpus; confirm it fires on the former and stays silent on the latter.
FP tuning: raise yarGen --score, use --excludegood, and require multiple string hits (2 of ($s*)) in condition.
Prerequisites
YARA 4.x installed (apt install yara on Debian/Ubuntu, brew install yara on macOS)
Python 3.8+ with yara-python (pip install yara-python)
for automated rule generation ()
yarGen
git clone https://github.com/Neo23x0/yarGen
Sample malware corpus or suspicious files for scanning (from malware zoos, VT, or incident artifacts)
Optional: pefile for PE header analysis, malduck for memory carving
import yara
from pathlib import Path
defload_rule_directory(rule_dir, extensions=(".yar", ".yara")):
"""Load all YARA rules from a directory tree."""
rule_files = {}
for ext in extensions:
for rule_file in Path(rule_dir).rglob(f"*{ext}"):
namespace = rule_file.stem
# Avoid namespace collisionsif namespace in rule_files:
namespace = f"{rule_file.parent.name}_{namespace}"
rule_files[namespace] = str(rule_file)
print(f"Loading {len(rule_files)} rule files from {rule_dir}")
try:
compiled = yara.compile(filepaths=rule_files)
return compiled
except yara.SyntaxError as e:
print(f"Syntax error in rules: {e}")
# Fall back to loading rules one by one, skipping broken ones
valid_rules = {}
for ns, path in rule_files.items():
try:
yara.compile(filepath=path)
valid_rules[ns] = path
except yara.SyntaxError:
print(f" Skipping broken rule: {path}")
return yara.compile(filepaths=valid_rules)
# Load and scan with community rules
community_rules = load_rule_directory("signature-base/yara/")
matches = community_rules.match("/mnt/evidence/suspicious_file.exe", timeout=120)
for m in matches:
print(f"Matched: {m.rule} (namespace: {m.namespace})")
Step 8: Build a Continuous Hunting Pipeline
Automate scanning of new files as they arrive using filesystem monitoring:
import yara
import time
import json
import hashlib
from pathlib import Path
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
classYaraHuntingHandler(FileSystemEventHandler):
def__init__(self, rules, alert_file="yara_alerts.jsonl"):
self.rules = rules
self.alert_file = alert_file
self.scanned_hashes = set()
defon_created(self, event):
if event.is_directory:
returnself._scan_file(event.src_path)
def_scan_file(self, filepath):
# Deduplicate by file hashtry:
file_hash = hashlib.sha256(Path(filepath).read_bytes()).hexdigest()
except (PermissionError, FileNotFoundError):
returnif file_hash inself.scanned_hashes:
returnself.scanned_hashes.add(file_hash)
matches = self.rules.match(filepath, timeout=60)
if matches:
alert = {
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"file": filepath,
"sha256": file_hash,
"matches": [
{"rule": m.rule, "severity": m.meta.get("severity", "unknown")}
for m in matches
]
}
withopen(self.alert_file, "a") as f:
f.write(json.dumps(alert) + "\n")
print(f"ALERT: {filepath} matched {len(matches)} rules")
# Set up continuous monitoring
rules = yara.compile(filepaths={"hunting": "rules/all_hunting_rules.yar"})
handler = YaraHuntingHandler(rules)
observer = Observer()
observer.schedule(handler, path="/mnt/quarantine/", recursive=True)
observer.start()
print("YARA hunting pipeline active. Monitoring /mnt/quarantine/ ...")
Verification
Compile all custom rules without syntax errors: yara -w rules/*.yar /dev/null
Confirm rules match known-good malware samples from your test corpus (true positive validation)
Verify rules do NOT match a goodware corpus of common system files (false positive testing)
Test scanning performance: single file scan should complete within timeout threshold
Validate yarGen output rules compile and produce meaningful matches against the input samples
Check that community rule sets load without critical syntax errors after filtering
Confirm the continuous hunting pipeline generates alerts in JSONL format when test files are dropped
Cross-reference YARA matches against VirusTotal or sandbox results to validate detection accuracy