Skip to content
lopesPublic

Latest commit

Β 

History

142 Commits

Folders and files

Graft Logo

graft

Unified Detection-as-Code engine for Google Security and beyond.
Write once, defend everywhere β€” author, verify, test, and reconcile threat detections across Google SecOps, Google Threat Intelligence (GTI), and modern enterprise security platforms.

Status: Active License: Apache 2.0 Python Version Code Style: ruff Type Checked: mypy Google: SecOps Google: GTI

Warning

  • Keep Your Production Fork Private: If you clone or fork Graft for operational use, keep your repository private to your organization. Never commit live production rule states, tuning exclusions, or internal indicator datasets to a public repository.
  • Disclaimer: Graft is personal research and not an officially supported Google product. All code and configurations are provided as-is under the Apache 2.0 License without warranty or official SLA.

The Philosophy

In horticulture, grafting joins a shoot from one plant onto the rootstock of another, enabling distinct varieties to thrive as a single, resilient organism.

Project Graft brings this discipline to detection engineering. Security teams often find their threat detection logic fragmented across proprietary rule consoles, incompatible query syntaxes, and siloed pipelines.

Graft acts as the unified trunk:

  • Standardized Core: Author, document, and test detection rules (metadata, logic, deployment, runbook, tests) alongside reusable, engine-agnostic contextual datasets (datasets/<name>.yaml) in a unified, version-controlled repository.
  • Resilient Branches (Ports & Adapters): Seamlessly "graft" rules and datasets into production Detection Enginesβ€”defined in Graft as any system that processes raw telemetry and generates security alerts from it (spanning SIEM, CTI hunting platforms, EDR/XDR, CNAPP, NDR, and identity security platforms). Deploy natively into Google SecOps (YARA-L 2.0, Data Tables & Curated Rule Sets) and Google Threat Intelligence (VirusTotal v3 YARA-X Livehunt & Retrohunt) today, and branch into auxiliary cloud, endpoint, or SIEM engines tomorrow without refactoring engineering workflows.
  • Multi-Track Governance: Manage reusable string datasets (datasets/<name>.yaml), bespoke organizational detections (rulesets/<engine>/custom/), and vendor-managed detections (rulesets/<engine>/managed/index.yaml and optional registered rule envelopes rulesets/<engine>/managed/<rule_name>.yaml) under GitOps plan/apply reconciliation.
  • Frictionless CI/CD: Decouple detection authoring from manual UI workflows with automated schema validation, dataset cross-reference checks, pre-merge server-side syntax verification (graft secops verify & graft gti verify), and deterministic synthetic replay testing (graft secops test & graft gti test).

Architecture at a Glance

Graft follows strict Hexagonal Architecture (Ports & Adapters) structured into four decoupled layers:

Graft Architecture: CLI, GitOps, Core Hexagonal, and Pluggable Engines


Key Features

1. Modular Engine Architecture & Pluggable Adapters

Graft strictly decouples detection engineering logic from downstream Google Security and enterprise detection platforms using Hexagonal Architecture (Ports & Adapters):

  • Self-Contained Engine Packages: Every detection engine lives in an isolated directory under src/graft/engines/<engine>/ housing its concrete API client, deployment adapter, engine-specific Draft 2020-12 schemas, and documentation.
  • Strict Protocol Boundaries: Engines implement explicit standard library typing.Protocol ports (DatasetPort, RuleDeployerPort, ManagedEnginePort, RuleCompilerPort, ReplayHarnessPort). Core never adapts to an engine; engines adapt to Core.
  • Zero Core Cloud Dependencies: The core substrate imports zero third-party SIEM SDKs or cloud libraries. It relies strictly on Python standard library modules (urllib.request, dataclasses, argparse, subprocess), guaranteeing fast, lightweight, and auditable execution.
  • Engine Scaffolding in Seconds: Adding a new detection engine (e.g. Wiz, Google Cloud SCC, Microsoft Sentinel, CrowdStrike) requires no core refactoring. Running graft new engine <name> generates the complete directory structure, schema definitions, unit test scaffolds, and documentation templates instantly.

2. Actionable Detection Envelopes & Factual Lifecycle

Detection logic is only as effective as the context and operational response it enables:

  • Normalized 5-Block Rule Envelope: Detections are authored in a standardized YAML envelope separating metadata, logic, deployment, runbook, and tests.
  • Embedded Operational Runbooks: Every rule embeds mandatory, actionable triage playbooks directly alongside the detection logic (context, triage, and response), eliminating tribal knowledge and context switching for SOC and CTI analysts during live investigations.
  • Factual Lifecycle vs. "Maturity Score" Theater: Graft rejects arbitrary 0–100 maturity guesses and manual status tags that rapidly become stale. Instead, Graft empirically computes factual lifecycle indicators directly from Git version history (first committer author, UTC YYYY-MM-DD created_at and last_modified_at, review_count, and unique contributor_count) combined with accountable rule ownership (owners, owner_count) and active engine deployment health (enabled | silent | disabled).

3. Reusable Datasets & Self-Expiring Suppressions (ttl:YYYY-MM-DD)

Contextual allowlists and temporary suppressions are notoriously hard to keep cleanβ€”a two-week penetration test IP added to a lookup table often stays there for a year because everyone forgets to remove it after the engagement ends:

  • Engine-Agnostic Datasets (datasets/<name>.yaml): Track scanner IPs, assessment ranges, and privileged principals in a 2-block string-only YAML envelope (metadata, values of 0..1,000 literal strings) synchronized to engine lookup tables (e.g., Google SecOps Data Tables with a canonical .value column) via additive coexistence.
  • Inline Auto-Expiration (ttl:YYYY-MM-DD): Append ttl:YYYY-MM-DD inside any value's inline # comment. The entry stays active through YYYY-MM-DD (UTC); starting the next UTC day, Graft omits it and the daily scheduled deployment removes it from the target engine automaticallyβ€”while preserving the line in Git as an audit trail.
  • Safe Deprecation (datasets/_archived/): Moving a retired dataset to datasets/_archived/ never breaks active rules referencing %<name>.value. Instead, Graft zeroes its remote rows and sets its description to "Deprecated on Graft" until you remove the rule references and delete the table remotely.
# datasets/security_assessment_ips.yaml
values:
  - "192.0.2.10"  # External red team assessment jumpbox (TEST-NET-1) ttl:2026-12-31
  - "198.51.100.25"  # Authorized third-party pentest egress node (TEST-NET-2)

4. Multi-Track GitOps Drift Reconciliation

Modern security operations combine internal SIEM detections, external CTI horizon hunting, contextual lookup tables, and vendor-managed curated rulesets. Graft reconciles all three tracks in strict dependency order (1. Datasets $\rightarrow$ 2. Custom Rules $\rightarrow$ 3. Managed Content) under unified version control:

  • Additive Dataset Synchronization: For engines supporting DatasetPort (such as secops), Graft synchronizes datasets/<name>.yaml before rules are deployed, recreating active datasets if accidentally deleted while never flagging or deleting unmanaged external tables.
  • Custom Rule & Hunting Synchronization: Authors maintain declarative custom rules in Git (rulesets/<engine>/custom/). Graft calculates precise diffs between local state and live tenant APIsβ€”reconciling Google SecOps YARA-L 2.0 rules in-place without resetting detection history, and synchronizing Google Threat Intelligence YARA-X Livehunt rulesets (with 1-to-1 metadata.tags sync) and non-destructive Retrohunt historical corpus jobs scoped via owner:self.
  • Vendor-Managed Curated Content Control & Registration: Manage vendor curated rule sets (rulesets/<engine>/managed/index.yaml) directly in code (e.g., PRECISE vs BROAD deployment tiers and findingsRefinements exclusions in Google SecOps), and optionally register managed rules (rulesets/<engine>/managed/<rule_name>.yaml) to map vendor coverage into MITRE ATT&CK matrices and catalogs.
  • Dual-Mode Drift Reconciliation:
    • Scoped PR Reconciliation (Default): Evaluates only files modified in the active Git branch against tenant state, enabling lightning-fast pull request validations in CI/CD.
    • Full Catalog Convergence (--all): Scans the entire tenant catalog (on merge to main and daily at 00:00 UTC) to purge expired ttl:YYYY-MM-DD dataset entries and heal out-of-band console drift.
  • Day 0 Brownfield Ingestion: Teams can adopt Graft on existing Google SecOps and Google Threat Intelligence tenants in minutes without disruption. Running graft <engine> pull reverse-synchronizes live custom rules, Livehunt/Retrohunt rulesets, and curated content into clean Git-managed envelopes.
$ graft secops diff --env production
=== Datasets Diff ===
[+] Dataset to create: known_scanner_ips (3 values)
[~] Dataset to update: security_assessment_ips (2 values)

=== Custom Rules Diff ===
[+] Custom rule to create: multiple_hosts_scanned
[~] Custom rule to update: workspace_nrd_email_opened (ID: ru_12345678-abcd-ef01-2345-6789abcdef01)

=== Google SecOps Managed Content Diff ===
[~] Deployment: ur_cloud_threats (PRECISE) | enabled: False -> True, alerting: False -> True
[+] Exclusion to create: excl_cloud_functions_pipeline_sa

$ graft gti diff --env production
=== Custom Rules Diff ===
[+] Custom rule to create: lh_americas_financial_malware_family_uploaded
[~] Custom rule to update: lh_rogue_ssl_certificate_brand_impersonated (ID: 1859374620192837465)

5. Threat Visibility, Audit-Ready Catalogs & ATT&CK v20.0 Intelligence

Bridge the gap between detection engineering code, CTI analysis, SOC operations, and leadership reporting:

  • Multi-Format Detection Catalogs: Export unified catalogs across all engines in interactive terminal tables, CSV spreadsheets, Markdown documentation (docs/RULE_CATALOG.md), or JSON for ingestion into security data lakes, BigQuery, Polars, or BI dashboards.
  • MITRE ATT&CK Enterprise v20.0 Matrix & CTI Intelligence (graft mitre): Offline evaluation of tactics, sub-techniques (TAxxxx:Tyyyy.zzz), Detection Strategies (DETxxxx), Analytics (ANxxxx), Data Components (DCxxxx), Threat Groups (Gxxxx), and Mitigations (Mxxxx), plus 3-state gap analysis (covered, partial, uncovered) and one-command taxonomy upgrades (graft mitre update).
  • Official Navigator Layer Generation: Generate color-graded MITRE ATT&CK Navigator v4.5/v5.2 layer files (exports/graft_coverage.json or per-engine layers) with embedded rule metadata, deployment status, optional STIX enrichments (--enrich), uncovered gap overlays (--include-gaps), and direct source links for interactive heatmap visualization in the official ATT&CK Navigator.
$ graft export catalog
Rule Name                                      Engine  Type     Status    MITRE ATT&CK                        Owners     Reviews  Updated   
---------------------------------------------  ------  -------  --------  ----------------------------------  ---------  -------  ----------
lh_americas_financial_malware_family_uploaded  gti     custom   disabled  TA0002:T1204.002, TA0006:T1555.003  Joe Lopes  4        2026-11-12
lh_rogue_ssl_certificate_brand_impersonated    gti     custom   disabled  TA0005:T1036.005, TA0042:T1587.003  Joe Lopes  3        2026-11-12
rh_corporate_secret_key_leak_swept             gti     custom   disabled  TA0006:T1552.001, TA0010:T1567.002  Joe Lopes  3        2026-11-12
gcp_service_account_key_created                secops  custom   enabled   TA0003:T1098.001, TA0004:T1098.001  Joe Lopes  13       2026-10-01
gcp_storage_bucket_public_access_granted       secops  custom   enabled   TA0001:T1078.004, TA0112:T1685      Joe Lopes  11       2026-10-01
multiple_hosts_scanned                         secops  custom   enabled   TA0007:T1018, TA0043:T1595.001      Joe Lopes  3        2026-11-12
workspace_nrd_email_opened                     secops  custom   enabled   TA0001:T1566.002                    Joe Lopes  9        2026-10-01
gcti_breach_network_indicator_matched          secops  managed  silent    TA0011:T1071.001                    Joe Lopes  2        2026-10-01

MITRE ATT&CK Navigator Coverage Heatmap


Choose Your Journey

Graft's documentation is organized around three core engineering personas:

Persona Focus & Deliverables Primary Guide
🎯 Detection Engineer / Analyst Authors, modifies, lints, tests, and diffs detection rules on a daily basis. Previews pull requests and consults operational runbooks. Analyst Guide
πŸ“– Detection Recipes Cookbook
πŸ› οΈ Platform & Security Engineer Bootstraps Graft into existing Google Security tenants, configures CI/CD pipelines, handles authentication (WIF/OIDC & API keys), and maintains production sync. Operator Guide
πŸ”„ Day 0 Ingestion & Reverse Sync
πŸ’» Core Developer Fixes bugs in core services, extends port protocols, builds new engine adapters, and enforces software architecture quality gates. Developer Guide
πŸ”Œ Pluggable Adapter Framework

For the complete architectural overview and philosophy, visit the Graft Documentation Hub (docs/README.md).


Quick Start & CLI Reference

Prerequisites

Installation & Quality Verification

# Clone repository
git clone https://github.com/lopes/graft.git
cd graft

# Install virtualenv and dev dependencies
uv sync

# Configure local environment templates (platform & engine credentials)
cp .env.example .env
cp src/graft/engines/secops/.env.example src/graft/engines/secops/.env
cp src/graft/engines/gti/.env.example src/graft/engines/gti/.env

# Run static quality gates & test suite
uv run ruff check .
uv run ruff format --check .
uv run mypy --strict src tests
uv run pytest

# Optional: Enable native pre-commit hook (runs fast offline gates on git commit)
git config core.hooksPath .githooks

Brownfield Adoption: Fastest Path (Day 0 Ingestion)

When adopting Graft on existing Google SecOps or Google Threat Intelligence tenants, follow the 3-step bootstrap workflow (see docs/operators/adoption.md for full architectural specification):

# 1. Reverse-sync existing detections from live Google Security tenants
uv run graft secops pull --env production
uv run graft gti pull --env production

# 2. Validate all imported envelopes against schemas & MITRE ATT&CK matrix
uv run graft lint

# 3. Commit baseline and declare Git as authoritative Source of Truth
git add rulesets/
git commit -m "rulesets: import secops and gti detection baselines"
git push origin main

Essential CLI Commands

1. Rule & Dataset Validation & Taxonomy Linting

# Lint all datasets and rules across the repository offline (<100ms)
graft lint

# Lint a specific rule or dataset file
graft lint rulesets/secops/custom/workspace_nrd_email_opened.yaml
graft lint rulesets/gti/custom/lh_rogue_ssl_certificate_brand_impersonated.yaml
graft lint datasets/known_scanner_ips.yaml

# Output structured JSON diagnostics for CI/CD pipelines
graft --json lint

2. Rule, Dataset & Engine Scaffolding

# Bootstrap a new custom detection rule (creates 5-block envelope with schema defaults)
graft secops new powershell_payload_downloaded
graft gti new lh_brand_sso_portal_impersonated
# or using the engine-agnostic router:
graft new rule rh_leaked_cloud_credentials_swept --engine gti

# Bootstrap a new reusable string dataset (creates 2-block envelope in datasets/<name>.yaml)
graft new dataset known_scanner_ips

# Register an enabled vendor-managed rule from rulesets/<engine>/managed/index.yaml
graft secops new gcti_breach_network_indicator_matched --managed 433faf9e-4d51-f284-c35b-009528ecff05

# Bootstrap an entirely new detection engine adapter (code, schema, rules, tests)
graft new engine wiz

Graft also includes repository-native AI agent skills under .agents/skills/ for composable rule, dataset, and CTI coverage engineering across secops and gti (see Rule Authoring Guide):

  • /scaffold-dataset <context_or_path>: Runs graft new dataset and populates metadata and values for a reusable string dataset.
  • /scaffold-rule <context_or_path>: Runs graft new rule (--engine secops or --engine gti), inspects MITRE analytics via graft --json mitre inspect, and populates metadata and runbook while leaving logic, deployment, and tests untouched.
  • /scaffold-tests <rule_path>: Parses authored logic predicates (YARA-L 2.0 or YARA-X) to generate positive (match_*) and negative (ignore_*) synthetic event vectors in tests.
  • /review-rule [path_or_dir]: Runs graft lint and performs a read-only 5-block engineering audit across secops and gti rules (including %<dataset>.value cross-checks, Livehunt/Retrohunt deployment schemas, and MITRE analytic alignment).
  • /analyze-coverage [scope_or_question]: Chains graft --json mitre coverage, gaps, and inspect to audit procedural depth (TT vs. TTP), surface cross-perimeter and multi-engine blind spots, and produce prioritized detection blueprints.

3. Engine Introspection, Offline Rendering, Pre-Merge Verification & Replay Testing

# List registered detection engines, capability flags, environments, and schemas
graft engines
graft --json engines

# Synthesize engine-native query text (full YARA-L 2.0 or YARA-X rule with meta:) completely offline
graft secops render rulesets/secops/custom/workspace_nrd_email_opened.yaml
graft gti render rulesets/gti/custom/lh_rogue_ssl_certificate_brand_impersonated.yaml

# Dry-run syntax against Google SecOps verifyRuleText or GTI's server-side YARA-X compiler
graft secops verify --all
graft gti verify --all

# Execute synthetic replay tests (Staging SIEM quarantine for secops; offline YARA-X / Retrohunt for gti)
graft secops test
graft gti test

# Test only rules modified in the current Git branch or working tree
graft secops test --changed-only
graft gti test --changed-only

# Enforce hard failure if staging credentials are missing (used in strict CI)
graft secops test --require-staging
graft gti test --require-staging

4. GitOps Drift Detection & State Reconciliation

# Preview changes for datasets and rules modified in current branch (Scoped Reconciliation: default)
graft secops diff --env production
graft gti diff --env production

# Scan entire tenant catalog for out-of-band console drift (Full Catalog Reconciliation: --all)
graft secops diff --all --env production
graft gti diff --all --env production

# Apply scoped branch changes to target tenant
graft secops apply --env production
graft gti apply --env production

# Force complete tenant convergence back to Git state, healing any console drift
graft secops apply --all --env production
graft gti apply --all --env production

# Target specific tracks on multi-track engines (datasets, custom rules, or managed content)
graft secops diff --target datasets --env production
graft secops apply --target managed --env production

5. Reverse Synchronization (Tenant Ingestion)

# Reverse-sync live custom rules and managed content from Google SecOps or GTI (owner:self)
graft secops pull --env production
graft gti pull --env production

# Migrate colleague-owned GTI Livehunt rulesets matching a tag into Git envelopes
GRAFT_GTI_FILTER="tag:migration" graft gti pull --env production

# Reverse-sync only custom rules (with overwrite protection or --force) or opt-in datasets
graft secops pull --target custom --env production --force
graft secops pull --target datasets --env production

6. MITRE Intelligence, Threat Matrix, Dataset & Catalog Exports

# Render on-screen detection catalog table across all engines (metadata alias supported)
graft export catalog

# Export active reusable datasets (after UTC ttl:YYYY-MM-DD filtering) as table, CSV, or JSON
graft export datasets
graft export datasets --format csv --datasets-dir datasets

# Inspect MITRE ATT&CK coverage, 3-state gaps, and targeted analytics/threat groups
graft mitre coverage --enrich
graft mitre gaps --platform IaaS --top 10
graft mitre inspect T1098.001 --analytics --platform IaaS
graft mitre update --check

# Generate official MITRE ATT&CK Enterprise Navigator layers (unified or per-engine)
graft export navigator --out exports/graft_coverage.json
graft export navigator --engine secops --color "#4285F4" --out exports/secops_coverage.json
graft export navigator --engine gti --color "#34A853" --out exports/gti_coverage.json

# Export detection catalog for audits and documentation pipelines
graft export catalog --format csv --out exports/detection_catalog.csv
graft export catalog --format markdown --out docs/RULE_CATALOG.md
graft export catalog --format json

Note

Factual Lifecycle Indicators vs. Static "Maturity" Fields: Graft intentionally omits static metadata.status or maturity fields from rules. In real-world detection engineering, static maturity labels rapidly become stale, creating administrative toil and a false sense of security ("security theater"). Similarly, Graft avoids arbitrary 0–100 synthetic maturity scores.

Instead, graft export catalog computes verified, objective indicators directly from version control, the rule envelope, and engine deployment adapters:

  • VCS Lifecycle & Provenance: First committer (author), first committed date (created_at), and latest revision date (last_modified_at) normalized to UTC YYYY-MM-DD.
  • Peer Review Scrutiny: Total git commit count across renames (review_count) and unique contributor count (contributor_count).
  • Operational Ownership & Context: Accountable rule owners (owners) and bus-factor indicator (owner_count), unified ATT&CK mappings (mitre_attack: TAxxxx:Tyyyy.zzz), and engine-evaluated deployment health (status: enabled | silent | disabled).

Operators can pipe these indicators into data pipelines (e.g. Polars, BigQuery, Google Sheets, BI dashboards) and combine them with live operational telemetry (alert volume, true-positive precision, mean time to triage) to assess true detection maturity objectively.

Rule & Dataset Decommissioning & Archiving: When retiring a rule or dataset, move it to rulesets/<engine>/_archived/ or datasets/_archived/ rather than immediately deleting it. Rules in _archived/ are excluded from loading, linting, matrix exports, and sync. Datasets in datasets/_archived/ are skipped by graft lint and automatically emptied (0 rows) and marked "Deprecated on Graft" during diff/apply, ensuring rules referencing %<name>.value never break before you remove the reference and delete the remote table.


Platform Architecture & Governance

Comprehensive documentation tracks for Detection Engineers, Platform Operators, and Core Developers are available in the Graft Documentation Hub (docs/README.md).

Operational directives, strict stdlib-first constraints, coding standards, and agent guidelines are documented in AGENTS.md.


License

This project is licensed under the Apache License 2.0. See the LICENSE file for full license terms.