writing-guarddog-rules
GitHub用于编写、编辑和审查 GuardDog YARA 源代码检测规则。涵盖能力/威胁/风险模型的分类,遵循特定工作流生成 .yar 规则文件及元数据,并处理误报调试与测试用例编写。
Trigger Scenarios
Install
npx skills add DataDog/guarddog --skill writing-guarddog-rules -g -y
SKILL.md
Frontmatter
{
"name": "writing-guarddog-rules",
"description": "Author, edit, and review GuardDog YARA source-code detection rules (.yar) that follow the capability\/threat\/risk model. Use when adding a new detection rule, changing an existing rule's patterns or metadata, splitting capabilities from threats, debugging false positives, or writing rule test cases under guarddog\/analyzer\/sourcecode\/."
}
Writing GuardDog Rules
GuardDog detects supply-chain malware with a two-layer model. The full reference,
including the philosophy, metadata schema, and worked examples, lives in
WRITING_RULES.md at the repository root (the skill directory is
.claude/skills/writing-guarddog-rules/, so the doc is three levels up). This
skill is the procedural layer: it captures the mental model and the workflow,
and it points to the reference for detail. Read WRITING_RULES.md when you need
the schema specifics, field definitions, or longer examples; do not duplicate it.
The model (memorize this, skip the doc for simple calls)
- Capability = "CAN DO" — a function call that enables an action
(
requests.get(,.readFileSync(,subprocess.Popen(). Match calls, not imports. - Threat = "SUSPICIOUS" — an attacker indicator (
/etc/passwd,discord.com/api/webhooks,base64.decode+exectogether). Not a bare function call. - Risk = capability + threat in the same file with matching category. A capability alone is benign; a threat indicator alone is often a false positive; together they are a risk.
identifiesformat:{type}.{category}[.{detail}]where type iscapabilityorthreat, category is one ofnetwork,filesystem,process,runtime,system,metadata.- Categories must match for a risk to form.
capability.process.*only pairs withthreat.process.*. General detail matches specific (threat.network+capability.network.outbound), but conflicting details do not (...outbound+...inbound). threat.runtime.*andthreat.metadata.*auto-form risks without needing a capability (obfuscation, install hooks, typosquatting, maintainer compromise).
When unsure whether a pattern is a capability or threat: does it show what code can do (capability) or a suspicious indicator (threat)? Does it stand alone without a capability (runtime/metadata threat)?
Authoring workflow
- Decide type and category. Pick
capabilityvsthreat, then category and optional detail. Confirm the matching counterpart exists or will exist so a risk can form. - Write patterns as YARA (
.yar). Source-code rules are YARA-only and language-agnostic (loaded for every ecosystem). Follow the best practices inWRITING_RULES.md(word boundaries\b, match method calls not object names, require quote context for bare strings, establish context with private rules before matching threats). Extract shared building blocks (LOLBAS, hooks) into.metafiles instead of repeating them. - Add metadata. Required:
identifies,severity,description. Threat rules also need a singlemitre_tactics. Optionalspecificity/sophisticationdefault tomedium. Also available:max_hits,path_include. See the schema section ofWRITING_RULES.md. - Name and place the file under
guarddog/analyzer/sourcecode/as{type}-{category}-{detail}.yar..metafiles (shared private rules) are{pattern-name}.meta. - Write test cases (see below).
- Run the checklist at the end of
WRITING_RULES.mdbefore finishing.
Testing rules (the real harness)
The harness is tests/analyzer/sourcecode/test_sourcecode_yara.py. Key points:
- The YARA rule's internal name must be the file id with hyphens replaced by underscores.
File
capability-filesystem-read.yarmust containrule capability_filesystem_read. The no-false-positive test filters matches to this exact name, so a mismatch silently skips coverage. - Positive test: add a file
tests/analyzer/sourcecode/<rule-id>.<ext>containing code the rule should flag (e.g.<rule-id>.py,.js,.go,.rb). The harness asserts the rule matches it. These are matched by filename prefix. - Negative test (false positives): add
tests/analyzer/sourcecode/benign/<rule-id>.<ext>with legitimate code that must NOT trigger the main rule. Every new or changed rule should have one. - Compilation: all
.yarfiles must compile, includinginclude "...meta"references resolving.
Run the suite:
make test-yara-rules
# or directly:
uv run pytest tests/analyzer/sourcecode -k <rule-id>
Scan a real package or local path to sanity-check end to end (use uv run):
uv run guarddog pypi scan <package-name> --rules <rule-id>
uv run guarddog pypi scan /path/to/package --output-format json
Common patterns and pitfalls
- Install hooks are a capability, not a threat. A hook is process-spawning ability like
subprocess.call(). The threat is the LOLBAS tool inside the hook. Paircapability.process.hookswiththreat.process.hooks(hook context + curl/wget). See the Advanced Patterns section ofWRITING_RULES.md. - Split LOLBAS by purpose:
lolbas-proc.meta(bash, python, node) vslolbas-net.meta(curl, wget, nc). YARA cannot reliably use multiple private rules from one include, so keep them separate and compose includes per rule. - Avoid false positives from shebangs, READMEs, and non-hook code by establishing context with private rules rather than matching a bare keyword anywhere.
- After adding or editing rules, regenerate docs if the repo expects it:
make docs.
Version History
- 12d036e Current 2026-08-20 14:02


