performing-threat-hunting-with-yara-rules について
YARA でファイル、ディレクトリ、メモリダンプをスキャンする脅威ハンティング用スキルです。カスタムルールやコミュニティルールを使ったマルウェアのトリアージとスキャン自動化を扱います。
主な機能
- 01ファイルとディレクトリのスキャン
- 02プロセスメモリダンプの検索
- 03YARA モジュールによるルール作成
- 04yarGen によるルール生成
- 05コミュニティルールセットの読み込み
- 06新規ファイルのスキャン自動化
活用シーン
- 01インシデント対応ファイルのトリアージ
- 02隔離サンプルの分類
- 03収集サンプルを IOC と照合
- 04受信ファイルを自動スキャン
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill performing-threat-hunting-with-yara-rulesインストール後、Claude Code で/performing-threat-hunting-with-yara-rulesを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
LICENSEreferences/api-reference.mdscripts/agent.py詳細
YARA ルールを使用した脅威ハンティング
YARA ルールを使用してファイル、ディレクトリ、メモリダンプをスキャンし、 マルウェアファミリー、不審なパターン、IOC の一致を特定します。
使用する場面
- ネットワーク共有、エンドポイント、メールの添付ファイル全体から、未知のマルウェア亜種をプロアクティブにハンティングする
- 隔離ディレクトリやサンドボックスの出力をスキャンし、マルウェアファミリーを分類する
- プロセスメモリダンプを検索し、インジェクションされたコードやメモリ内のみに存在するペイロードを探す
- 収集したサンプルの大規模なコーパスに対して、脅威インテリジェンスの IOC を検証する
- インシデント対応のアーティファクトをトリアージし、既知のマルウェアファミリーをすばやく特定する
- 新しいファイルの取り込み時にスキャンする自動検出パイプラインを構築する
リアルタイムのエンドポイント保護には使用しないでください(代わりに EDR エージェントを使用してください)。YARA スキャンは、スキャンの遅延を許容できるバッチハンティング、トリアージ、収集後の分析に最適です。
前提条件
- YARA 4.x がインストールされていること(Debian/Ubuntu では
apt install yara、macOS ではbrew install yara) yara-pythonを含む Python 3.8 以降(pip install yara-python)- ルールを自動生成するための
yarGen(git clone https://github.com/Neo23x0/yarGen) - スキャン用のマルウェアコーパスまたは不審なファイル(マルウェアリポジトリ、VT、インシデントアーティファクトなどから入手)
- 任意:PE ヘッダー分析用の
pefile、メモリのカービング用のmalduck - 脅威インテリジェンスの YARA ルールセット(例:YARA-Rules コミュニティリポジトリ、Florian Roth の signature-base)
ワークフロー
ステップ 1:YARA と Python バインディングをインストールする
# Linux
sudo apt update && sudo apt install -y yara
# Python bindings
pip install yara-python
# Verify installation
yara --version
python3 -c "import yara; print(yara.YARA_VERSION)"
ステップ 2:基本的な YARA ルールを作成する
文字列、16 進数パターン、ファイルメタデータに一致するルールを作成します。
// File: rules/emotet_loader.yar
rule Emotet_Loader_2026 {
meta:
author = "Threat Intel Team"
description = "Detects Emotet first-stage loader DLL"
date = "2026-01-20"
reference = "https://attack.mitre.org/software/S0367/"
mitre_attack = "T1059.001, T1055.001"
severity = "critical"
strings:
// Emotet export function name patterns
$export1 = "DllRegisterServer" ascii
$export2 = "RunDLL" ascii nocase
// Obfuscated string decryption routine
$decrypt_loop = { 8B 45 ?? 33 45 ?? 89 45 ?? 8B 4D ?? 03 4D ?? }
// PowerShell download cradle in embedded script
$ps_cradle = /powershell[^\n]{0,50}-e(nc|ncodedcommand)/i
// Known C2 URI patterns
$uri1 = "/wp-content/uploads/" ascii
$uri2 = "/wp-admin/css/" ascii
$uri3 = "/wp-includes/" ascii
// PE characteristics
$mz = "MZ" at 0
condition:
$mz and
filesize < 2MB and
(
($export1 and $decrypt_loop) or
($ps_cradle and any of ($uri*)) or
(2 of ($uri*) and $decrypt_loop)
)
}
ステップ 3:モジュールを使用して高度なルールを作成する
YARA モジュールを使用して、PE ヘッダーを調査し、数式に基づくエントロピーチェックを行います。
import "pe"
import "math"
rule Suspicious_Packed_Executable {
meta:
author = "Threat Hunting Team"
description = "Detects PE files with high entropy sections indicating packing or encryption"
severity = "medium"
condition:
pe.is_pe and
pe.number_of_sections > 0 and
for any section in pe.sections : (
math.entropy(section.offset, section.size) > 7.2 and
section.size > 1024
) and
pe.imports("kernel32.dll", "VirtualAlloc") and
pe.imports("kernel32.dll", "VirtualProtect")
}
rule Suspicious_UPX_Modified {
meta:
description = "Detects UPX-packed binaries with tampered section names"
severity = "medium"
strings:
$upx_magic = { 55 50 58 21 } // UPX!
condition:
pe.is_pe and
$upx_magic and
not (
pe.sections[0].name == "UPX0" and
pe.sections[1].name == "UPX1"
)
}
ステップ 4:yara-python でファイルとディレクトリをスキャンする
import yara
import os
import json
from datetime import datetime
from pathlib import Path
def compile_rules(rule_paths):
"""Compile YARA rules from one or more .yar files."""
rule_files = {}
for i, path in enumerate(rule_paths):
namespace = Path(path).stem
rule_files[namespace] = path
return yara.compile(filepaths=rule_files)
def scan_directory(rules, target_dir, recursive=True):
"""Scan a directory for matches and return structured results."""
results = []
scan_count = 0
error_count = 0
for root, dirs, files in os.walk(target_dir):
for filename in files:
filepath = os.path.join(root, filename)
scan_count += 1
try:
matches = rules.match(filepath, timeout=60)
if matches:
for match in matches:
result = {
"file": filepath,
"rule": match.rule,
"namespace": match.namespace,
"tags": match.tags,
"meta": match.meta,
"strings": [],
"scan_time": datetime.utcnow().isoformat()
}
for offset, identifier, data in match.strings:
result["strings"].append({
"offset": hex(offset),
"identifier": identifier,
"data": data.hex() if isinstance(data, bytes) else data
})
results.append(result)
print(f" MATCH: {match.rule} -> {filepath}")
except yara.TimeoutError:
error_count += 1
print(f" TIMEOUT scanning {filepath}")
except yara.Error as e:
error_count += 1
if not recursive:
break
print(f"\nScan complete: {scan_count} files scanned, "
f"{len(results)} matches, {error_count} errors")
return results
# Compile and scan
rules = compile_rules([
"rules/emotet_loader.yar",
"rules/suspicious_packed.yar"
])
matches = scan_directory(rules, "/mnt/evidence/collected_samples/")
# Export results
with open("yara_scan_results.json", "w") as f:
json.dump(matches, f, indent=2)
ステップ 5:プロセスメモリダンプをスキャンする
実行中のプロセスにのみ存在する、メモリ内の痕跡をハンティングします。
import yara
def scan_memory_dump(rules, dump_path):
"""Scan a process memory dump for YARA matches."""
matches = rules.match(dump_path, timeout=120)
for match in matches:
print(f"Rule: {match.rule}")
print(f" Severity: {match.meta.get('severity', 'unknown')}")
for offset, identifier, data in match.strings:
# Show context around the match
print(f" String {identifier} at offset {hex(offset)}")
if len(data) <= 64:
print(f" Data: {data.hex()}")
return matches
# Rules targeting in-memory artifacts
memory_rules = yara.compile(source="""
rule Cobalt_Strike_Beacon_Memory {
meta:
description = "Detects Cobalt Strike beacon in process memory"
severity = "critical"
strings:
$config_start = { 2E 2F 2E 2F 2E 2C }
$sleep_mask = { 48 8B 44 24 ?? 48 89 44 24 ?? 48 8B 44 24 }
$named_pipe = "\\\\\\\\.\\\\pipe\\\\msagent_" ascii
$watermark = { 00 00 00 00 00 00 ?? ?? 00 00 }
condition:
2 of them
}
""")
scan_memory_dump(memory_rules, "/mnt/evidence/lsass_dump.dmp")
ステップ 6:yarGen でルールを自動生成する
マルウェアサンプルから固有の文字列を抽出し、yarGen を使用してルールを作成します。
# Clone and set up yarGen
git clone https://github.com/Neo23x0/yarGen.git
cd yarGen
pip install -r requirements.txt
# Download the string databases (run once)
python3 yarGen.py --update
# Generate rules from a directory of malware samples
python3 yarGen.py \
-m /mnt/evidence/malware_samples/ \
-o generated_rules.yar \
--excludegood \
-p "AutoGen" \
-a "Threat Hunting Team" \
--score 50
# Generate rules for a single sample with maximum detail
python3 yarGen.py \
-m /mnt/evidence/malware_samples/suspicious.exe \
-o single_sample_rule.yar \
--opcodes \
--debug
ステップ 7:コミュニティのルールセットを統合する
公開されている脅威インテリジェンスのリポジトリからルールをダウンロードして結合します。
# Clone Florian Roth's signature-base (large community rule set)
git clone https://github.com/Neo23x0/signature-base.git
# Clone YARA-Rules community repository
git clone https://github.com/Yara-Rules/rules.git yara-community-rules
# Clone ReversingLabs YARA rules
git clone https://github.com/reversinglabs/reversinglabs-yara-rules.git
import yara
from pathlib import Path
def load_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 collisions
if 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})")
ステップ 8:継続的なハンティングパイプラインを構築する
ファイルシステムの監視を使用し、新しいファイルが届いたときに自動でスキャンします。
import yara
import time
import json
import hashlib
from pathlib import Path
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class YaraHuntingHandler(FileSystemEventHandler):
def __init__(self, rules, alert_file="yara_alerts.jsonl"):
self.rules = rules
self.alert_file = alert_file
self.scanned_hashes = set()
def on_created(self, event):
if event.is_directory:
return
self._scan_file(event.src_path)
def _scan_file(self, filepath):
# Deduplicate by file hash
try:
file_hash = hashlib.sha256(Path(filepath).read_bytes()).hexdigest()
except (PermissionError, FileNotFoundError):
return
if file_hash in self.scanned_hashes:
return
self.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
]
}
with open(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/ ...")
検証
- すべてのカスタムルールが構文エラーなしでコンパイルされることを確認します:
yara -w rules/*.yar /dev/null - テストコーパスの既知のマルウェアサンプルにルールが一致することを確認します(真陽性の検証)
- 一般的なシステムファイルを含む正常なファイルのコーパスにルールが一致しないことを確認します(誤検知のテスト)
- スキャン性能をテストします:単一ファイルのスキャンがタイムアウトのしきい値以内に完了すること
- yarGen の出力ルールがコンパイルされ、入力サンプルに対して有意な一致を検出することを検証します
- コミュニティのルールセットをフィルタリングした後、重大な構文エラーなしで読み込めることを確認します
- テストファイルを配置したときに、継続的なハンティングパイプラインが JSONL 形式でアラートを生成することを確認します
- YARA の一致結果を VirusTotal またはサンドボックスの結果と照合し、検出精度を検証します
コメント
Markdown 対応