meltedinhex/analyst-ai-pack / skills/scanning-samples-with-yara
scanning-samples-with-yara
VerifiedUses YARA to classify and triage samples at scale: applying rule sets, reading matches and string offsets, tuning for false positives, and organizing rules for malware family identification. Activates for requests to scan files with YARA, apply YARA rules, or classify samples by signature.
| File | Kind | Size |
|---|---|---|
| LICENSE | other | 340 B |
| SKILL.md | skill md | 3.2 KB |
| references/api-reference.md | markdown | 1.2 KB |
| scripts/analyst.py | script | 2.6 KB |
scripts/analyst.py
View on GitHub#!/usr/bin/env python3
"""Scan files and directories with YARA rules and summarize matches.
Wraps ``yara-python`` to compile a rules path and report matched rule names, tags, and string
offsets. Reads files as data; it never executes a sample.
"""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
def compile_rules(rules_path: str):
import yara # type: ignore
p = Path(rules_path)
if p.is_dir():
filepaths = {f.stem: str(f) for f in p.rglob("*.yar")}
filepaths.update({f.stem: str(f) for f in p.rglob("*.yara")})
if not filepaths:
raise FileNotFoundError(f"no .yar/.yara files under {rules_path}")
return yara.compile(filepaths=filepaths)
return yara.compile(filepath=str(p))
def scan_file(rules, filepath: str) -> dict:
matches = rules.match(filepath, timeout=60)
return {
"file": filepath,
"matches": [{
"rule": m.rule,
"tags": list(m.tags),
"strings": [
{"id": s.identifier, "offset": inst.offset}
for s in m.strings for inst in s.instances
] if hasattr(m, "strings") else [],
} for m in matches],
}
def scan_path(rules_path: str, target: str, recursive: bool) -> list[dict]:
rules = compile_rules(rules_path)
target_p = Path(target)
files = ([f for f in target_p.rglob("*") if f.is_file()] if recursive and target_p.is_dir()
else [f for f in target_p.iterdir() if f.is_file()] if target_p.is_dir()
else [target_p])
return [scan_file(rules, str(f)) for f in files]
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="Scan with YARA")
parser.add_argument("rules", help="rule file or directory")
parser.add_argument("target", help="file or directory to scan")
parser.add_argument("--recursive", action="store_true")
parser.add_argument("--summary", action="store_true")
args = parser.parse_args(argv)
try:
results = scan_path(args.rules, args.target, args.recursive)
except ImportError:
print("yara-python not installed: pip install yara-python", file=sys.stderr)
return 2
if args.summary:
hit = [r for r in results if r["matches"]]
print(json.dumps({
"scanned": len(results),
"with_matches": len(hit),
"files": [{"file": r["file"],
"rules": [m["rule"] for m in r["matches"]]} for r in hit],
}, indent=2))
else:
print(json.dumps(results, indent=2))
return 0
if __name__ == "__main__":
sys.exit(main())
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |