Skip to content

Commit 007db2d

Browse files
authored
Merge pull request #6 from crowdsecurity/opnsense
opnsense update
2 parents ff76a0c + 5e8bc80 commit 007db2d

4 files changed

Lines changed: 174 additions & 19 deletions

File tree

‎CLAUDE.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ Conventions for authoring this skill. This governs how skill content is **writte
3232
never against the authoring machine.
3333
- **If you cannot verify something, mark it `untested` or leave it out.** Never present unconfirmed
3434
behavior as confirmed.
35+
- **Avoid duplicates** by checking in the existing skill documentation if the information is already present. If it is, ensure it's properly linked/routed, but don't duplicate instruction, code blocks or configurations.
36+
37+

‎crowdsec/references/appsec/deploy.md‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,33 @@ The smoke test above proves the WAF works. For production you point a real bounc
113113
|---|---|
114114
| `crowdsec-nginx-bouncer` (lua module) | `APPSEC_URL=http://127.0.0.1:7422` in `/etc/crowdsec/bouncers/crowdsec-nginx-bouncer.conf` (shell-style `KEY=VALUE`, empty by default = WAF off). The self-registered `API_KEY` already serves AppSec — reuse it. |
115115
| Traefik (`maxlerebourg/crowdsec-bouncer-traefik-plugin`) | Flat plugin options: `crowdsecAppsecEnabled: true` (default false), `crowdsecAppsecHost: crowdsec:7422` (host:port, no scheme), and the bouncer key in `crowdsecLapiKey`. Full recipe in [../configure/bouncers/web-servers.md](../configure/bouncers/web-servers.md) § Traefik. |
116-
| `crowdsec-caddy-bouncer` (Caddy module) | Equivalent `appsec_url` directive on the bouncer block. |
116+
| `github.com/hslatman/caddy-crowdsec-bouncer` (Caddy module) | Two handlers required in the Caddy route — **`appsec` AND `crowdsec`** (see critical note below). The `appsec_url` field goes in the top-level `crowdsec` app config block. |
117117
| Any other AppSec-aware bouncer | Look for an `appsec_url` / `appsec.url` field; auth is always the bouncer's existing API key. |
118118

119119
After wiring: send a request through the real web server (not directly to 7422) and confirm the verdict propagates. The bouncer's own log should show one line per consultation; AppSec's `cscli metrics show appsec` increments.
120120

121+
> **Critical — Caddy (`hslatman/caddy-crowdsec-bouncer`) requires TWO handlers:**
122+
> the `crowdsec` handler only enforces IP-level bans from LAPI — it does **not** forward requests to port 7422. WAF inspection requires the separate `appsec` handler. Both must be in the route, `appsec` first. If only `crowdsec` is present, AppSec metrics will always show 0 processed and no requests are ever blocked by WAF rules. The `appsec_url` field lives in the top-level `crowdsec` app block. Full Caddyfile and JSON recipes: [../configure/bouncers/web-servers.md](../configure/bouncers/web-servers.md) § Caddy.
123+
121124
See also: [../configure/bouncers/web-servers.md](../configure/bouncers/web-servers.md) for installing the bouncer in the first place.
122125

123126
## Per-environment notes
124127

125128
| Env | What changes |
126129
|---|---|
127130
| **systemd / bare-metal** | The recipe above as-is. |
131+
| **OPNsense / FreeBSD** | Config root is `/usr/local/etc/crowdsec/`; drop acquisition in `acquis.d/appsec.yaml`. The `os-crowdsec` plugin manages the engine — reload with `service crowdsec reload`. No Lua module in the OPNsense nginx package: use the Caddy-based bouncer instead (see [../configure/bouncers/web-servers.md](../configure/bouncers/web-servers.md) § Caddy). Note the LAPI port conflict below. |
128132
| **Docker / compose** | AppSec runs inside the crowdsec container and must `listen_addr: 0.0.0.0:7422`. Bouncer containers reach it via the service name + internal port (`appsec_url: http://crowdsec:7422`), not the published port. The acquisition file is mounted from the host or baked into a customised image. `docker compose exec crowdsec cscli appsec-*` for management commands. **Containerized lua bouncers need a DNS `resolver` — see [../install/docker.md](../install/docker.md) § Bouncer key bootstrap.** |
129133
| **Kubernetes / Helm** | The official chart has `appsec.enabled: true` plus values for `appsec.listen_addr`, `appsec.config`, and a separate `appsec` Service. Bouncers target the AppSec Service DNS name. Required collections/rules can be listed in the chart's hub config. |
130134

135+
### OPNsense / FreeBSD: LAPI port conflict
136+
137+
CrowdSec LAPI binds to `127.0.0.1:8080` by default. Any bouncer that listens on `*:8080` (wildcard) will shadow the LAPI only from external IPs — loopback (`127.0.0.1`) always routes to LAPI. This means:
138+
139+
- **Do not test via `127.0.0.1:8080`** — you will hit LAPI, not the bouncer.
140+
- Use the host's internal or external IP for end-to-end testing (e.g. `172.31.x.x:8080` on an AWS instance).
141+
- If both LAPI and the bouncer bind `:8080`, move the **bouncer's backend** to another port (e.g. `:8888`) and keep the bouncer frontend on `:8080` listening on `0.0.0.0` only — LAPI stays on its loopback bind and the conflict is resolved.
142+
131143
## Advanced shapes
132144

133145
- **AppSec on a separate host** from the engine: set `listen_addr` to the private interface and protect with mTLS (cert configuration on both bouncer and AppSec sides; documented at <https://docs.crowdsec.net/docs/next/appsec/advanced_deployments>).

‎crowdsec/references/configure/acquisition.md‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ problem is here.
1515
|---|---|
1616
| Single legacy file | `/etc/crowdsec/acquis.yaml` (`acquisition_path` in `config.yaml`) |
1717
| Drop-in dir (preferred) | `/etc/crowdsec/acquis.d/*.yaml` (`acquisition_dir` in `config.yaml`) — one file per source set |
18+
| OPNsense / FreeBSD | `/usr/local/etc/crowdsec/acquis.d/` — same drop-in model; `os-crowdsec` plugin drops files here automatically |
1819
| Docker | Bind-mount or env (`COLLECTIONS`, plus a mounted `acquis.d`); see Per-environment notes |
1920
| Kubernetes | The chart's `config.acquisition` values render into the same `acquis.d` files |
2021

@@ -50,6 +51,38 @@ Glob expansion is evaluated at startup; files created later that match are **not
5051
up until reload. For high-rotation logs prefer the directory plus a glob over naming each
5152
file.
5253
54+
| Field | Notes |
55+
|---|---|
56+
| `filenames` | List of paths; globs are supported. |
57+
| `labels.type` | **Required.** The parser chain selector. |
58+
| `force_inotify` | Set `true` to force inotify-based watching on filesystems that don't advertise it (some NFS mounts, older kernels). |
59+
| `poll_without_inotify` | Set `true` when the file is a **symlink** that rotates by relinking (e.g. OPNsense's `latest.log` pattern). inotify watches the inode, not the path — without polling the engine misses rotation events. |
60+
61+
### OPNsense log paths
62+
63+
OPNsense writes logs in RFC 5424 syslog format. The path pattern is `/var/log/<facility>/latest.log` where `latest.log` is always a symlink to the newest rotation file.
64+
65+
| Log | Path | `labels.type` |
66+
|---|---|---|
67+
| SSH authentication | `/var/log/system/latest.log` | `syslog` |
68+
| nginx access/error | `/var/log/nginx/access.log` | `nginx` |
69+
| pf firewall | `/var/log/filter/latest.log` | `pf` |
70+
| OPNsense GUI / API | `/var/log/audit/latest.log` | (configd audit — **not SSH auth**) |
71+
72+
> **Pitfall — SSH log path on OPNsense:** The `os-crowdsec` plugin ships `acquis.d/opnsense.yaml` which maps `/var/log/audit/latest.log` to the `sshd` collection. This file is the **configd** audit log (web admin actions), not SSH auth. Real SSH authentication events land in `/var/log/system/latest.log` (syslog format). Add a separate `acquis.d/ssh.yaml` pointing at the right path.
73+
74+
Because OPNsense's `latest.log` files are symlinks that rotate, always include both options:
75+
76+
```yaml
77+
# acquis.d/ssh.yaml — SSH auth on OPNsense/FreeBSD
78+
filenames:
79+
- /var/log/system/latest.log
80+
labels:
81+
type: syslog
82+
force_inotify: true
83+
poll_without_inotify: true
84+
```
85+
5386
## journald datasource
5487

5588
```yaml
@@ -64,6 +97,8 @@ The filter strings are passed straight to `journalctl`. After reload the source
6497
metrics as `journalctl:journalctl-_SYSTEMD_UNIT=ssh.service`. A typo in the unit name is
6598
silent — the source reads **0 lines** rather than erroring.
6699

100+
Not available on FreeBSD/OPNsense (no systemd).
101+
67102
## docker datasource
68103

69104
For a CrowdSec **container** reading other containers' stdout/stderr via the Docker socket:
@@ -80,6 +115,28 @@ Requires `/var/run/docker.sock` mounted into the CrowdSec container. The source
80115
`docker:<container-name>`. Use this instead of a file source when apps log to stdout (the
81116
12-factor norm in Docker/compose) — there is no log file to bind-mount.
82117

118+
## AppSec datasource
119+
120+
Tells the engine to spin up an inline WAF listener. This is **not a log reader** — it opens a TCP port that bouncers forward HTTP requests to. The CrowdSec agent evaluates them in-process and returns an allow/block verdict.
121+
122+
```yaml
123+
# acquis.d/appsec.yaml
124+
source: appsec
125+
appsec_config: crowdsecurity/appsec-default
126+
labels:
127+
type: appsec
128+
listen_addr: 127.0.0.1:7422
129+
```
130+
131+
| Field | Notes |
132+
|---|---|
133+
| `source: appsec` | **Required.** Identifies the datasource type. Without it the engine ignores the block. |
134+
| `appsec_config` | Hub-installed config name. `crowdsecurity/appsec-default` is included by both canonical WAF collections and carries the health-check test rule. |
135+
| `listen_addr` | `127.0.0.1:7422` for loopback (single-host bouncer); `0.0.0.0:7422` for cross-host bouncers. |
136+
| `labels.type: appsec` | Used by out-of-band scenarios. Keep as `appsec`. |
137+
138+
The WAF collections must be installed before the engine will start with this config — see [../appsec/deploy.md](../appsec/deploy.md).
139+
83140
## When to pick which source
84141

85142
| Logs come from… | `source:` |
@@ -115,6 +172,14 @@ sudo cscli explain --log 'May 21 09:00:00 host sshd[123]: Failed password for in
115172
sudo cscli explain --file /var/log/nginx/access.log --type nginx # replay a whole file
116173
```
117174

175+
On OPNsense/FreeBSD:
176+
177+
```bash
178+
service crowdsec configtest
179+
service crowdsec reload
180+
cscli metrics show acquisition
181+
```
182+
118183
## Pitfalls
119184

120185
- **Missing/wrong `labels.type`:** lines read but all `unparsed`. The single most common
@@ -130,11 +195,16 @@ sudo cscli explain --file /var/log/nginx/access.log --type nginx # replay a wh
130195
- **Globs are startup-only:** new files matching a glob need a reload to be acquired.
131196
- **Edited but not applied:** `crowdsec -t` validates the file but does not load it — you
132197
still need `systemctl reload crowdsec` (or recreate the container / `helm upgrade`).
198+
- **Symlinked `latest.log` stops updating after rotation:** inotify watches the old inode — add `poll_without_inotify: true` (and optionally `force_inotify: true`).
199+
- **SSH not detected on OPNsense:** acquisition points at `/var/log/audit/latest.log` (configd, not auth) — point at `/var/log/system/latest.log` with `labels.type: syslog`.
200+
- **AppSec source fails to start:** WAF collections not installed — `cscli collections install crowdsecurity/appsec-virtual-patching crowdsecurity/appsec-generic-rules`.
133201

134202
## Per-environment notes
135203

136204
| Env | What changes |
137205
|---|---|
138206
| **systemd / bare-metal** | Recipes above as-is. Edit `acquis.d/*.yaml`, `crowdsec -t`, `systemctl reload crowdsec`. |
207+
| **OPNsense / FreeBSD** | Config root is `/usr/local/etc/crowdsec/`; drop-in dir is `/usr/local/etc/crowdsec/acquis.d/`. Use `file` source with `poll_without_inotify: true` for symlinked `latest.log` files. No journald. Reload with `service crowdsec reload`. |
139208
| **Docker / compose** | Mount `./acquis.d:/etc/crowdsec/acquis.d` (and `/var/run/docker.sock` for the docker source). `COLLECTIONS=`/`PARSERS=` env install hub items at start. Run cscli with `docker exec <name> cscli metrics show acquisition`. Recreate the container to apply (a reload signal also works). |
140209
| **Kubernetes / Helm** | Define sources under `config.acquisition` in values; `helm upgrade --reset-then-reuse-values`. Inspect with `kubectl exec -n <ns> <agent-pod> -- cscli metrics show acquisition`. The `k8s_audit` source needs the API server's audit webhook pointed at the agent. |
210+

‎crowdsec/references/configure/bouncers/web-servers.md‎

Lines changed: 88 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,16 @@ docker exec crowdsec cscli metrics show appsec # Processed/Blocked increment
330330
- **`stream` lag:** a fresh ban lands within `updateIntervalSeconds`; immediate ban-then-curl
331331
looks like a failure. (See [../../debug/bouncer-not-blocking.md](../../debug/bouncer-not-blocking.md).)
332332

333-
## Caddy — `caddy-crowdsec-bouncer`
333+
## Caddy — `github.com/hslatman/caddy-crowdsec-bouncer`
334334

335335
WAF-capable Caddy module ([`hslatman/caddy-crowdsec-bouncer`](https://github.com/hslatman/caddy-crowdsec-bouncer)).
336336
Caddy has no plugin loader, so the module must be **compiled in** — build a custom binary/image
337337
with `xcaddy`.
338338

339339
### Build with the module
340340

341+
**Docker / Linux (Dockerfile):**
342+
341343
```dockerfile
342344
FROM caddy:2.10-builder AS builder
343345
RUN xcaddy build \
@@ -350,6 +352,24 @@ COPY --from=builder /usr/bin/caddy /usr/bin/caddy
350352
(`/http` enforces decisions; `/appsec` adds the WAF handler. Add `/layer4` only for L4
351353
proxying.) Mint a bouncer key with `cscli bouncers add caddy -o raw`.
352354

355+
**FreeBSD/OPNsense** (no Go in base, no Docker):
356+
357+
```bash
358+
# Download Go binary for freebsd-amd64
359+
fetch https://go.dev/dl/go1.22.4.freebsd-amd64.tar.gz # or latest
360+
tar -C /usr/local -xzf go*.tar.gz
361+
export PATH=$PATH:/usr/local/go/bin
362+
363+
sudo pkg install -y git # xcaddy needs git for module resolution
364+
365+
GOPATH=$HOME/go go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
366+
GOPATH=$HOME/go $HOME/go/bin/xcaddy build \
367+
--with github.com/hslatman/caddy-crowdsec-bouncer \
368+
--output /tmp/caddy-cs
369+
370+
sudo cp /tmp/caddy-cs /usr/local/bin/caddy-cs
371+
```
372+
353373
### Caddyfile
354374

355375
```caddyfile
@@ -377,26 +397,76 @@ proxying.) Mint a bouncer key with `cscli bouncers add caddy -o raw`.
377397
}
378398
```
379399

380-
### Verify end-to-end (through Caddy)
400+
### Config (JSON API — for OPNsense/FreeBSD or programmatic use)
401+
402+
The bouncer exposes two handlers and one top-level app:
403+
404+
```json
405+
{
406+
"apps": {
407+
"crowdsec": {
408+
"api_url": "http://127.0.0.1:8080",
409+
"api_key": "<bouncer-key>",
410+
"appsec_url": "http://127.0.0.1:7422",
411+
"ticker_interval": "15s",
412+
"enable_streaming": true,
413+
"appsec_fail_open": false
414+
},
415+
"http": {
416+
"servers": {
417+
"demo": {
418+
"listen": [":8080"],
419+
"routes": [
420+
{
421+
"handle": [
422+
{"handler": "appsec"},
423+
{"handler": "crowdsec"},
424+
{
425+
"handler": "reverse_proxy",
426+
"upstreams": [{"dial": "127.0.0.1:8888"}]
427+
}
428+
]
429+
}
430+
]
431+
}
432+
}
433+
}
434+
}
435+
}
436+
```
437+
438+
| Handler | Function |
439+
|---|---|
440+
| `http.handlers.appsec` | Forwards each request to AppSec (port 7422) for WAF inspection — returns 403 on inband rule match |
441+
| `http.handlers.crowdsec` | Checks the LAPI decision list for the client IP — returns 403 on active ban |
442+
443+
> **Both handlers are required.** `crowdsec` alone silently skips WAF inspection — `cscli metrics show appsec` will show 0 processed. Put `appsec` first in the route.
444+
445+
### Verify end-to-end
381446

382447
```bash
383-
curl -sS -o /dev/null -w 'normal: %{http_code}\n' http://127.0.0.1:8082/ # 200
384-
curl -sS -o /dev/null -w 'appsec block: %{http_code}\n' 'http://127.0.0.1:8082/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php' # 403
385-
docker exec crowdsec cscli decisions add --ip 198.51.100.200 --duration 5m --reason test
386-
sleep 12 # ticker_interval=10s
387-
curl -sS -o /dev/null -w 'banned XFF: %{http_code}\n' -H 'X-Forwarded-For: 198.51.100.200' http://127.0.0.1:8082/ # 403
388-
curl -sS -o /dev/null -w 'clean XFF: %{http_code}\n' -H 'X-Forwarded-For: 203.0.113.9' http://127.0.0.1:8082/ # 200
389-
docker exec crowdsec cscli decisions delete --ip 198.51.100.200
448+
# AppSec block (CVE-2017-9841) → 403
449+
curl -sS -o /dev/null -w 'appsec block: %{http_code}\n' \
450+
'http://<host-ip>:8080/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php'
451+
452+
# LAPI ban block
453+
cscli decisions add --ip <test-ip> --duration 2m --reason test
454+
sleep 16 # wait for streaming ticker
455+
curl -sS -o /dev/null -w 'banned: %{http_code}\n' http://<host-ip>:8080/
456+
457+
cscli metrics show appsec # confirm processed/blocked counters
390458
```
391459

460+
> **Do not test via `127.0.0.1:<bouncer-port>`** if LAPI is on `127.0.0.1:8080` and the bouncer frontend shares port `8080`. Loopback traffic routes to LAPI, not the bouncer. Use the host's internal/external IP instead.
461+
392462
### Pitfalls
393463

394-
- **Module not compiled in:** the stock `caddy` image has no `crowdsec` directive — Caddy
395-
errors on the Caddyfile. You must `xcaddy build` (above) or use a prebuilt image that bundles
396-
the module.
397-
- **Real IP:** without `trusted_proxies` + `client_ip_headers`, Caddy treats the proxy/Docker
398-
hop as the client and bans never match. Set both in the global `servers` block.
399-
- **Handler order:** put `appsec` before `crowdsec` in the route so WAF inspection runs ahead of
400-
decision enforcement.
401-
- **WAF off:** omit `appsec_url` and the module enforces decisions only. AppSec must listen on
402-
`0.0.0.0:7422` for a containerized Caddy to reach it.
464+
- **`crowdsec-caddy-bouncer` / `crowdsecurity/caddy-cs-bouncer` do not exist** — these names return 404 on GitHub and pkg.go.dev. The correct module is `github.com/hslatman/caddy-crowdsec-bouncer`.
465+
- **Module not compiled in:** the stock `caddy` image has no `crowdsec` directive — Caddy errors on the Caddyfile. You must `xcaddy build` (above) or use a prebuilt image that bundles the module.
466+
- **xcaddy needs `git`** — the build fails with "git not found" on a minimal FreeBSD install. `sudo pkg install -y git` first.
467+
- **Build to `/tmp`, then copy** — xcaddy may not have write access to `/usr/local/bin` directly; build to `/tmp/caddy-cs`, then `sudo cp`.
468+
- **Real IP:** without `trusted_proxies` + `client_ip_headers`, Caddy treats the proxy/Docker hop as the client and bans never match. Set both in the global `servers` block.
469+
- **Handler order:** put `appsec` before `crowdsec` in the route so WAF inspection runs ahead of decision enforcement.
470+
- **WAF off:** omit `appsec_url` and the module enforces decisions only. AppSec must listen on `0.0.0.0:7422` for a containerized Caddy to reach it.
471+
- **LAPI port conflict** — see [../../appsec/deploy.md](../../appsec/deploy.md) § OPNsense / FreeBSD: LAPI port conflict.
472+

0 commit comments

Comments
 (0)