diff --git a/README.md b/README.md index e487c39..affcd47 100644 --- a/README.md +++ b/README.md @@ -6,58 +6,70 @@ A self-healing synchronization agent designed to ensure the local `nftables` ker ## 🎯 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. +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`) -- **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. +- **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 (`agent.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 ```text threat-engine-agent/ -├── main.py # Logic loop and orchestration -├── auditor.py # The "Brain": Recursive parsing and kernel scanning +├── agent.py # Main loop and heal orchestration +├── auditor.py # LAPI vs nft reconciliation and drift fingerprinting +├── reporter.py # Local state and heal history writer +├── config.yaml # Static config (currently not wired into runtime) ├── systemd/ # Deployment configurations │ └── threat-agent.service -└── logs/ # Local audit history (Git ignored) +└── logs/ # Runtime logs ``` ## 🚀 Installation & Deployment 1. Environment Setup: -```chmod +x main.py -mkdir logs +```bash +mkdir -p /root/threat-engine-agent/logs +cd /root/threat-engine-agent ``` 2. Systemd Integration: -```sudo cp systemd/threat-agent.service /etc/systemd/system/ +```bash +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 +```bash +# 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. **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. +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`.