# 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 5 minutes. 2. **Heal:** Automatically restart remediation services if a desync is detected. 3. **Filter:** Handle protocol mismatches (filtering IPv6 noise) and expired "zombie" decisions. ## 🛠️ Technical Architecture ### The Auditor (`auditor.py`) - **Deep-Harvest Parser:** Recursively scans the `cscli` Alert-style JSON. This bypasses schema variations where IPs might be hidden in `source`, `decisions`, or `meta` blocks. - **Protocol Filtering:** Automatically ignores IPv6 addresses (detected by `:`) to prevent insertion errors in IPv4-only `nft` tables. - **Global Table Scan:** Rather than checking a single set, it scans the **entire** `ip crowdsec` table. This ensures that IPs placed in different "drawers" (like Range sets) are still accounted for. - **Temporal Awareness:** Filters out decisions with negative durations (`startswith('-')`) to ignore bans that are technically expired but not yet vacuumed from the database. ### The Service - **Root-Level Execution:** Necessary for direct interaction with `nftables` and the `cscli` local socket. - **Dependency Management:** Configured to start only after `crowdsec.service` is initialized to prevent false-positive desyncs on boot. ## 📂 Project Structure ```text threat-engine-agent/ ├── main.py # Logic loop and orchestration ├── auditor.py # The "Brain": Recursive parsing and kernel scanning ├── systemd/ # Deployment configurations │ └── threat-agent.service └── logs/ # Local audit history (Git ignored) ``` ## 🚀 Installation & Deployment 1. Environment Setup: ```chmod +x main.py mkdir logs ``` 2. Systemd Integration: ```sudo cp systemd/threat-agent.service /etc/systemd/system/ sudo systemctl daemon-reload sudo systemctl enable --now threat-agent ``` 3. Monitoring: ```# Watch the auditor work in real-time tail -f logs/threat-agent.log ``` ## 🔍 Troubleshooting If you see a "Heal Loop" in the logs: 1. **Manual Check**: Run `/usr/bin/cscli decisions list -a -o json` and check if the count matches `nft list table ip crowdsec`. 2. **IPv6 Ghosting**: Verify if the "Missing from Kernel" IPs are actually IPv6. The auditor should filter these, but new formats may require a regex update. 3. **Set Naming**: Ensure the bouncer hasn't created a new table name outside of the `crowdsec` namespace.