28267a0cf440323c643638379e8900ec5e71d8a4
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:
- Audit: Compare LAPI "Truth" vs.
nftables"Reality" every cycle. - Heal: Restart
crowdsec-firewall-bounceron desync to force a fresh LAPI pull. - 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
missingandextra. - 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 crowdsecandip6 crowdsec6tables vianft -j.
The Orchestrator (main.py)
- Runs the reconcile loop (default
600sinterval). - 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
- Environment Setup:
mkdir -p /root/threat-engine-agent/logs
cd /root/threat-engine-agent
cp .env.example .env
- Systemd Integration:
sudo cp systemd/threat-agent.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now threat-agent
Configuration Precedence
- Environment variables (from systemd environment and/or
.env) config.yaml- Built-in defaults in code
Supported env overrides:
SERVER_NAMECHECK_INTERVALHEAL_VERIFY_DELAYHEAL_MIN_INTERVALLOG_PATHSTATE_FILETHREAT_AGENT_CONFIG
- 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:
- Check decision source: Run
/usr/bin/cscli decisions list -a -o jsonand confirm output shape/contents are valid. - Check kernel state: Run
nft -j list table ip crowdsecandnft -j list table ip6 crowdsec6. - Check drift telemetry: Review
missing,extra,drift_fingerprint,heal_result, andpost_drift_fingerprintin logs/history. - Check service health:
systemctl status threat-agent crowdsec-firewall-bouncer.
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.
154 KiB
0 Stars
1 Watchers
0 Forks
Languages
Python
100%