b48dabed8504986a97bbfb49a1c09596d0fc213e
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 5 minutes.
- Heal: Automatically restart remediation services if a desync is detected.
- Filter: Handle protocol mismatches (filtering IPv6 noise) and expired "zombie" decisions.
🛠️ Technical Architecture
The Auditor (auditor.py)
- Deep-Harvest Parser: Recursively scans the
cscliAlert-style JSON. This bypasses schema variations where IPs might be hidden insource,decisions, ormetablocks. - Protocol Filtering: Automatically ignores IPv6 addresses (detected by
:) to prevent insertion errors in IPv4-onlynfttables. - Global Table Scan: Rather than checking a single set, it scans the entire
ip crowdsectable. 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
nftablesand thecsclilocal socket. - Dependency Management: Configured to start only after
crowdsec.serviceis initialized to prevent false-positive desyncs on boot.
📂 Project Structure
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
- Environment Setup:
mkdir logs
- Systemd Integration:
sudo systemctl daemon-reload
sudo systemctl enable --now threat-agent
- Monitoring:
tail -f logs/threat-agent.log
🔍 Troubleshooting
If you see a "Heal Loop" in the logs:
- Manual Check: Run
/usr/bin/cscli decisions list -a -o jsonand check if the count matchesnft list table ip crowdsec. - 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.
- Set Naming: Ensure the bouncer hasn't created a new table name outside of the
crowdsecnamespace.
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%