Skip to content
Proyek

wolffcatskyy/crowdsec-unifi-bouncer

Lihat di GitHub

Tentang

Install and persist the official CrowdSec firewall bouncer on UniFi OS devices (UDM, UDM SE, UDR) with automatic recovery from firmware updates

Keamanan dan privasi

Cara angka ini diukur

Yang dapat Anda lakukan

  • One-line install — curl | bash bootstrap onto any supported UniFi device
  • Persistence — survives reboots and controller reprovisioning; recovers after UniFi OS updates via an on_boot.d hook (see Firmware updates)
  • Auto-detection — identifies your device model and applies safe ipset limits automatically
  • Sidecar proxy — scores 120K+ decisions across 8 factors, fits the highest-priority threats into your device's capacity

Pertumbuhan bintang terukur

dalam 24 jam
0
dalam 7 hari
+1
dalam 15 hari
+2
dalam 1 bulan
+8

Perubahan bersih pada periode yang tumpang tindih, bukan total harian. Pengukuran yang belum tersedia dibiarkan kosong. Bintang menunjukkan perhatian, bukan kualitas.

README.md

Asli

CrowdSec Firewall Bouncer for UniFi OS

GitHub Sponsors Ko-fi GitHub Release GitHub Stars License: MIT ShellCheck Docker Publish Mentioned in Awesome UniFi

Drop-in install of the official CrowdSec firewall bouncer on UniFi OS devices — with persistence through reboots and controller reprovisioning, and automatic recovery after firmware updates (via on-boot-script-2.x, which the installer sets up if it's missing). Includes an intelligent sidecar proxy that scores and prioritizes threats when you have more decisions than your device can hold.

[!TIP] v2.5.4 Released — the bouncer now comes back on its own after a UniFi OS firmware update (beta: not yet verified on real hardware, reports welcome). Updates reset /etc and root's crontab, which used to leave the bouncer silently stopped; the installer now enables the service and, with on-boot-script-2.x, hooks boot-restore.sh to put everything back on every boot. Thanks @RichBrew (#46). v2.5.3 fixed the capacity monitor reading 0% on ipset 7.x (thanks @ahmaddxb, #63). Release notes

[!CAUTION] Beware of impostor repositories. The official CrowdSec UniFi Bouncer is hosted at wolffcatskyy/crowdsec-unifi-bouncer. We do not distribute ZIP file downloads or executable installers. If you see a repo offering "one-click downloads" of this project, it may contain malware. Always install via the official instructions below.

New to CrowdSec? CrowdSec is a free, open-source security engine that detects and blocks malicious IPs using crowd-sourced threat intelligence. Get started with the official install guide.

Key Features

  • One-line install — curl | bash bootstrap onto any supported UniFi device
  • Persistence — survives reboots and controller reprovisioning; recovers after UniFi OS updates via an on_boot.d hook (see Firmware updates)
  • Auto-detection — identifies your device model and applies safe ipset limits automatically
  • Sidecar proxy — scores 120K+ decisions across 8 factors, fits the highest-priority threats into your device's capacity
  • Stream-aware capping (v2.3.0) — prevents ipset overflow on high-churn CAPI streams with configurable eviction
  • Prometheus metrics — monitor ipset fill ratio, dropped decisions, and sidecar effectiveness
  • Grafana dashboard — included, ready to import
  • AbuseIPDB reporting (v2.4.0) — automatically reports locally-banned IPs to AbuseIPDB, contributing to community threat intelligence
  • LOG rules for detection (v2.5.0) — iptables LOG rules make dropped packets visible to CrowdSec, enabling port scan detection and AbuseIPDB reporting
  • Lightweight — 15 MB RAM for the bouncer, 8 MB for the sidecar
  • IPv4 + IPv6 (v2.6, beta) — separate inet6 ipset and ip6tables rules mirror the v4 path, with independent capacity limits. Existing configs retain disable_ipv6: true on upgrade and must be changed explicitly to enable IPv6; setup.sh warns while it is off. On v2.5.x and earlier the bouncer is IPv4 only: IPv6 decisions are not enforced

Table of Contents

The Problem

Two problems, one project.

Problem 1: Persistence. The official bouncer binary works perfectly on UniFi devices, but UniFi OS doesn't make it easy to keep it running. Firmware updates wipe your iptables rules. Controller reprovisioning silently removes custom firewall rules. Systemd service links disappear. You install it, it works, then one day it's quietly stopped blocking anything.

Problem 2: Capacity. CrowdSec's community blocklist (CAPI) can push 100,000+ decisions to your bouncer. UniFi devices have hardware-limited ipset capacity — safe limits range from ~15K entries on consumer hardware to 120K measured on a UDM SE (see the measured capacity table). When the ipset fills up, new IPs fail silently — "Hash is full" errors buried in the log, zero alerting, and your most dangerous new threats get dropped while stale entries from last month sit in the set.

The solution: An installer, persistence scripts, and an optional sidecar proxy that handles all of this. Install once, forget about it.

Quick Start

Prerequisites: A running CrowdSec LAPI instance and SSH root access to your UniFi device.

# 1. On your CrowdSec host — create a bouncer API key
cscli bouncers add my-unifi-bouncer
# Save the API key output

# 2. SSH into your UniFi device and install
ssh root@YOUR_UNIFI_IP
curl -sSL https://raw.githubusercontent.com/wolffcatskyy/crowdsec-unifi-bouncer/main/bootstrap.sh | bash

# 3. Configure — set your LAPI address and API key
$EDITOR /data/crowdsec-bouncer/crowdsec-firewall-bouncer.yaml

# 4. Start
systemctl start crowdsec-firewall-bouncer

# 5. Verify
ipset list crowdsec-blacklists -t | grep "Number of entries"

That's it. The bouncer auto-detects your device, sets safe ipset limits, and starts on every boot. To have it come back on its own after firmware updates, see Firmware updates.

Installation

One-Line Install (Recommended)

SSH into your UniFi device and run:

curl -sSL https://raw.githubusercontent.com/wolffcatskyy/crowdsec-unifi-bouncer/main/bootstrap.sh | bash

This downloads all required files and runs the installer automatically.

Manual Install

# Clone this repo
git clone https://github.com/wolffcatskyy/crowdsec-unifi-bouncer.git
cd crowdsec-unifi-bouncer

# Copy files to your UniFi device
scp install.sh setup.sh detect-device.sh detect-sidecar.sh ensure-rules.sh \
    log-rules.sh ipset-capacity-monitor.sh metrics.sh \
    crowdsec-firewall-bouncer.service crowdsec-unifi-metrics.service \
    crowdsec-firewall-bouncer.yaml.example \
    root@<UNIFI_IP>:/tmp/

# SSH in and run installer
ssh root@<UNIFI_IP>
cd /tmp && bash install.sh

Configuration

Edit /data/crowdsec-bouncer/crowdsec-firewall-bouncer.yaml and set your api_url and api_key. If using the sidecar proxy, point api_url at port 8084 instead of the LAPI port 8080.

See the full configuration reference for all settings, startup commands, and verification steps.

Architecture

Persistence scripts keep the bouncer running through reboots and controller reprovisioning, and restore it after firmware updates. An optional sidecar proxy scores 120K+ decisions across 8 factors so your device's limited ipset always holds the highest-priority threats.

See docs/architecture.md for the full diagram and persistence mechanism details.

Firmware updates

[!WARNING] Beta. This recovery path was added in v2.5.4 and has only been tested in a sandbox, not yet across a real UniFi OS firmware update. If you update firmware with it in place, please say how it went in discussion #46, working or not: your device and firmware version, systemctl status crowdsec-firewall-bouncer, and crontab -l after the update. The beta label comes off once it's confirmed on hardware.

UniFi OS firmware updates keep /data/ but reset /etc/ and root's crontab. That removes the bouncer's systemd service, its "enabled" link, and its cron jobs, so without help the bouncer stays stopped after an update, with no warning.

boot-restore.sh puts all of that back and starts the bouncer. It runs automatically on every boot through unifi-utilities' on-boot-script-2.x (the udm-boot service, now maintained in unifi-common), and the installer handles that dependency:

  • Already installed? The installer leaves it alone and just adds /data/on_boot.d/99-crowdsec-bouncer.sh.
  • Missing? The installer downloads udm-boot.service from a pinned unifi-common commit, checks its SHA-256, installs and enables it, then adds the hook. If the checksum doesn't match, the install stops before changing anything and tells you why. The pinned commit and checksum sit in a clearly marked block at the top of install.sh. No copy of on-boot-script is kept in this repo.
  • Don't want it installed? Run the installer with ONBOOT_AUTO_INSTALL=0 (for example curl -sSL .../bootstrap.sh | ONBOOT_AUTO_INSTALL=0 bash). You'll get a warning and the manual command below instead.

If a firmware update ever removes on-boot-script itself, re-run the installer and it will put the pinned version back.

Without on-boot-script, run this after each firmware update:

/data/crowdsec-bouncer/boot-restore.sh --boot

Device Compatibility

The bouncer auto-detects your UniFi device model and applies safe ipset limits. Supported tiers: Enterprise (80K), Pro (50K), Consumer (15K). UX is not supported. UXG-Lite is unvalidated: it has ipset, but there is no tested on-device install/persistence path yet.

See docs/device-compatibility.md for the full device matrix, environment variables, and usage scenarios.

Sidecar Proxy (Optional but Recommended)

The sidecar scores all LAPI decisions across 8 factors (scenario severity, origin, freshness, recidivism, blocklist feed confidence, and more) and returns only the top N that fit your device. Your local detections and manual bans always survive — only low-signal bulk imports are dropped.

Deploy it if your LAPI has more than ~15K-30K decisions, or if you subscribe to community blocklists. See docs/sidecar.md for setup and scoring details.

AbuseIPDB Reporting (Optional)

Enable in the sidecar to automatically report locally-detected bans to AbuseIPDB. Reporting is fire-and-forget and never affects bouncer operation. Only local detections are reported — CAPI and blocklist-import decisions are skipped to avoid circular reporting.

See docs/abuseipdb.md for configuration and scenario-to-category mapping.

LOG Rules for Detection

The bouncer blocks malicious IPs, but by default UniFi routers silently drop packets with no log entry. Without visibility into dropped traffic, CrowdSec cannot detect patterns like port scans from already-blocked IPs, and has nothing to report to AbuseIPDB or CAPI.

The log-rules.sh script solves this by inserting iptables LOG rules immediately before every DROP rule in UniFi's WAN firewall chains. Dropped packets are logged to syslog with structured prefixes like [UNIFI-WAN_LOCAL-D-ALL], making them parseable by crowdsec-unifi-parser.

What this enables:

  • CrowdSec detection of port scans and brute force from blocked IPs
  • AbuseIPDB reporting of detected attacks (via the sidecar)
  • CAPI contribution (sharing your detections with the CrowdSec community)

LOG rules are automatically maintained by ensure-rules.sh (runs every 5 minutes via cron), so they survive reboots and rule flushes (after a firmware update the cron job is restored by boot-restore.sh). They are rate-limited (10/min burst 20) to avoid log flooding.

Setup: LOG rules are deployed automatically during installation. To check status or manage manually:

# Check current LOG rule status
/data/crowdsec-bouncer/log-rules.sh --status

# Manually redeploy
/data/crowdsec-bouncer/log-rules.sh

# Remove all LOG rules
/data/crowdsec-bouncer/log-rules.sh --remove

Syslog forwarding: To complete the detection loop, configure your UniFi device to forward syslog to your CrowdSec instance. See the crowdsec-unifi-parser for parser configuration.

Prometheus Metrics

The bouncer exposes metrics on port 9101 (ipset fill ratio, blocked IPs, dropped decisions). The sidecar exposes effectiveness metrics on port 8084. A Grafana dashboard is included at grafana/crowdsec-unifi-bouncer-dashboard.json.

See docs/metrics.md for the full metrics reference.

Troubleshooting

Common issues: bouncer starts but no IPs blocked, iptables rules disappearing, service lost after firmware update, sidecar 502s, ipset full.

See docs/troubleshooting.md for diagnostics and solutions.

Uninstalling

/data/crowdsec-bouncer/uninstall.sh

Removes the services, cron jobs, firmware-update hook, iptables rules, ipset, and /data/crowdsec-bouncer. Use --keep-config to keep your config and logs, and --dry-run to preview. Remember to also delete the bouncer from your LAPI (cscli bouncers delete <name>).

Migration from Python Bouncer

If upgrading from v1.x:

  1. Stop and remove the old Docker container
  2. Delete old firewall groups from UniFi controller (named crowdsec-ban-*)
  3. Delete old firewall rules (indices 20000-20013)
  4. Follow the Installation steps above

The native bouncer uses ipset/iptables directly:

  • No MongoDB thrashing -- v1.x API approach caused router freezes at 2000+ IPs
  • No UniFi credentials needed
  • No Docker overhead -- single Go binary, 15 MB RAM
  • Faster response -- 10s polling vs 60s

Part of the CrowdSec UniFi Ecosystem

Project Description
crowdsec-unifi-suite One-command installer for the full stack
crowdsec-blocklist-import Import 28+ threat feeds into CrowdSec
crowdsec-unifi-parser Parse UniFi firewall logs for CrowdSec

Contributing

Contributions are welcome. Please open an issue to discuss significant changes before submitting a PR.

  • Shell scripts are linted with ShellCheck via CI
  • Go code (sidecar) follows standard Go conventions
  • All PRs run through CI before merge

Support

License

MIT -- see LICENSE

Featured In

Changelog Highlights

v2.4.0 -- AbuseIPDB reporting. Automatically reports locally-banned IPs to AbuseIPDB with scenario-to-category mapping, daily rate limiting, and smart origin filtering. Fire-and-forget, never affects bouncer operation.

v2.3.0 -- Stream-aware decision capping. MAX_DECISIONS and EVICTION_MODE control CAPI decision limits. Local decisions always pass through.

v2.2.0 -- Effectiveness metrics for the sidecar proxy: per-origin kept/dropped counters, score distribution, recidivism stats, false-negative detection. Multi-arch Docker images published to GHCR.

v2.1.0 -- Intelligent sidecar proxy with 7-factor scoring. Prioritizes decisions so the most dangerous threats always make it into your ipset.

v2.0.0 -- Complete rewrite. Replaced the Python/Docker bouncer (UniFi controller API) with the official Go binary using ipset/iptables directly. No MongoDB thrashing, no credentials needed, 15 MB RAM.

See the full changelog for all releases.

Credits

Jelajahi proyek terkait

Memuat…