Threat Engine Agent (Triton Edition)

A self-healing synchronization agent designed to ensure the local nftables kernel state perfectly matches the CrowdSec Local API (LAPI) desired state.

🎯 Purpose

The standard crowdsec-firewall-bouncer can occasionally experience "ghost" desynchronization where the LAPI removes a ban, but the kernel retains it (or vice-versa). This agent acts as a high-integrity supervisor to:

  1. Audit: Compare LAPI "Truth" vs. nftables "Reality" every cycle.
  2. Heal: Restart crowdsec-firewall-bouncer on desync to force a fresh LAPI pull.
  3. Verify: Re-check state after healing and record success/failure telemetry.

🛠️ Technical Architecture

The Auditor (auditor.py)

  • Schema-tolerant parsing: Supports both flat decision JSON and nested alert-style JSON from cscli.
  • State reconciliation: Compares LAPI decision IP set against kernel IP set and computes drift as missing and extra.
  • Drift fingerprinting: Generates a deterministic fingerprint for flap suppression and heal history.
  • Metadata drift check: Flags long-duration decisions that are near expiry in kernel state.
  • Kernel scan scope: Scans ip crowdsec and ip6 crowdsec6 tables via nft -j.

The Orchestrator (main.py)

  • Runs the reconcile loop (default 600s interval).
  • Triggers heal when any drift is detected.
  • Applies flap suppression using drift fingerprint, not raw kernel count.
  • Performs post-heal verification after a short delay (default 5s).

The Reporter (reporter.py)

  • Writes local health state JSON atomically.
  • Persists structured heal history, including pre/post fingerprints and heal result.
  • Default state path: /root/threat-engine-agent/threat-agent-state.json.

📂 Project Structure

threat-engine-agent/
├── agent.py             # Compatibility entrypoint used by systemd
├── main.py              # Main loop and orchestration
├── config.py            # Runtime settings + logging setup
├── healing.py           # Heal gating + post-heal verification
├── auditor.py           # LAPI vs nft reconciliation and drift fingerprinting
├── reporter.py          # Local state and heal history writer
├── config.yaml          # Runtime defaults (overridable by env)
├── systemd/             # Deployment configurations
│   └── threat-agent.service
└── logs/                # Runtime logs

🚀 Installation & Deployment

  1. Environment Setup:
mkdir -p /root/threat-engine-agent/logs
cd /root/threat-engine-agent
cp .env.example .env
  1. Systemd Integration:
sudo cp systemd/threat-agent.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now threat-agent

Configuration Precedence

  1. Environment variables (from systemd environment and/or .env)
  2. config.yaml
  3. Built-in defaults in code

Supported env overrides:

  • SERVER_NAME
  • CHECK_INTERVAL
  • HEAL_VERIFY_DELAY
  • HEAL_MIN_INTERVAL
  • LOG_PATH
  • STATE_FILE
  • THREAT_AGENT_CONFIG
  1. Monitoring:
# Watch the agent in real time
tail -f /root/threat-engine-agent/logs/threat-agent.log

🔍 Troubleshooting

If you see a "Heal Loop" in the logs:

  1. Check decision source: Run /usr/bin/cscli decisions list -a -o json and confirm output shape/contents are valid.
  2. Check kernel state: Run nft -j list table ip crowdsec and nft -j list table ip6 crowdsec6.
  3. Check drift telemetry: Review missing, extra, drift_fingerprint, heal_result, and post_drift_fingerprint in logs/history.
  4. Check service health: systemctl status threat-agent crowdsec-firewall-bouncer.
S
Description
Designed to ensure the LAPI and firewall are in sync with the proper decision count, and if not - restarts the bouncer automatically to restore sync.
Readme
154 KiB
0 Stars 1 Watchers 0 Forks
Languages
Python 100%