From 41e94284125aeca66d836a08cae98b95b7c68af2 Mon Sep 17 00:00:00 2001 From: Phil Date: Fri, 27 Nov 2026 13:26:00 -0700 Subject: [PATCH] crowdsec: host sshd + Gitea logs, nftables firewall bouncer (SSH bans only for now), weekly hub update - crowdsec image -> latest-debian (journalctl); reads host journal (sshd) and Gitea's log file (LePresidente/gitea); LAPI on 127.0.0.1:8080 - gitea also logs to /data/gitea/log/gitea.log (router lines console only) - host: crowdsec-firewall-bouncer-nftables from packagecloud any/any, own nft tables on input+forward hooks; limited to local ssh detections and manual bans until crowdsec_firewall_all_decisions - weekly cscli hub update/upgrade + SIGHUP Co-Authored-By: Claude Opus 5.5 --- group_vars/all/infrastructure.yml | 4 ++ roles/docker/handlers/main.yml | 5 +++ .../crowdsec-firewall-bouncer.yml | 39 +++++++++++++++++++ .../docker/tasks/infrastructure/crowdsec.yml | 19 ++++++++- .../templates/crowdsec-acquis-gitea.yaml.j2 | 5 +++ .../templates/crowdsec-acquis-sshd.yaml.j2 | 9 +++++ .../docker/templates/crowdsec-compose.yml.j2 | 24 ++++++++++-- roles/docker/templates/crowdsec-env.j2 | 1 + .../crowdsec-firewall-bouncer.yaml.local.j2 | 23 +++++++++++ roles/docker/templates/gitea-compose.yml.j2 | 4 ++ 10 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 roles/docker/tasks/infrastructure/crowdsec-firewall-bouncer.yml create mode 100644 roles/docker/templates/crowdsec-acquis-gitea.yaml.j2 create mode 100644 roles/docker/templates/crowdsec-acquis-sshd.yaml.j2 create mode 100644 roles/docker/templates/crowdsec-firewall-bouncer.yaml.local.j2 diff --git a/group_vars/all/infrastructure.yml b/group_vars/all/infrastructure.yml index a01a62c..d1927b4 100644 --- a/group_vars/all/infrastructure.yml +++ b/group_vars/all/infrastructure.yml @@ -53,3 +53,7 @@ crowdsec_allowlist: - "100.64.0.0/10" # Tailscale IPv4 - "fd7a:115c:a1e0::/48" # Tailscale IPv6 - "{{ network.netcup_ip }}" # the server itself + +# Firewall bouncer: false = only SSH bans (local detections + manual) are +# enforced at the firewall; true = every CrowdSec decision, on all ports. +crowdsec_firewall_all_decisions: false diff --git a/roles/docker/handlers/main.yml b/roles/docker/handlers/main.yml index b74c458..f89a0e2 100644 --- a/roles/docker/handlers/main.yml +++ b/roles/docker/handlers/main.yml @@ -11,6 +11,11 @@ - compose.yml state: restarted +- name: restart crowdsec-firewall-bouncer + ansible.builtin.systemd_service: + name: crowdsec-firewall-bouncer + state: restarted + - name: restart obsidian-livesync community.docker.docker_compose_v2: project_src: /opt/stacks/obsidian-livesync diff --git a/roles/docker/tasks/infrastructure/crowdsec-firewall-bouncer.yml b/roles/docker/tasks/infrastructure/crowdsec-firewall-bouncer.yml new file mode 100644 index 0000000..690e3f8 --- /dev/null +++ b/roles/docker/tasks/infrastructure/crowdsec-firewall-bouncer.yml @@ -0,0 +1,39 @@ +# CrowdSec firewall bouncer (nftables) on the host. It reads decisions from +# the crowdsec container's LAPI (127.0.0.1:8080). If it stops, its tables go +# away (nothing is blocked); it never touches ufw's or Docker's rules. +# packagecloud has no trixie dist, so use the generic any/any repo. +- name: crowdsec apt repository + ansible.builtin.deb822_repository: + name: crowdsec + types: deb + uris: https://packagecloud.io/crowdsec/crowdsec/any + suites: any + components: main + signed_by: https://packagecloud.io/crowdsec/crowdsec/gpgkey + +# Written before the package: its postinst starts the service right away. +- name: crowdsec bouncer config directory + ansible.builtin.file: + path: /etc/crowdsec/bouncers + state: directory + mode: '0755' + +- name: firewall bouncer config overrides + ansible.builtin.template: + src: crowdsec-firewall-bouncer.yaml.local.j2 + dest: /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml.local + owner: root + mode: '0600' + diff: false # contains the bouncer key + notify: restart crowdsec-firewall-bouncer + +- name: install the nftables firewall bouncer + ansible.builtin.apt: + name: crowdsec-firewall-bouncer-nftables + update_cache: true + +- name: firewall bouncer running + ansible.builtin.systemd_service: + name: crowdsec-firewall-bouncer + state: started + enabled: true diff --git a/roles/docker/tasks/infrastructure/crowdsec.yml b/roles/docker/tasks/infrastructure/crowdsec.yml index 6ae1992..18c3e0a 100644 --- a/roles/docker/tasks/infrastructure/crowdsec.yml +++ b/roles/docker/tasks/infrastructure/crowdsec.yml @@ -29,7 +29,7 @@ dest: "/opt/stacks/crowdsec/acquis.d/{{ item }}.yaml" owner: root mode: '0644' - loop: [caddy, appsec] + loop: [caddy, appsec, sshd, gitea] notify: restart crowdsec - name: deploy crowdsec stack @@ -66,3 +66,20 @@ when: >- not ansible_check_mode and (crowdsec_allowlist_current.rc != 0 or item not in crowdsec_allowlist_current.stdout) + +# The entrypoint only updates hub items (parsers, scenarios, vpatch rules) on +# start; dockcheck restarts crowdsec only when a new image ships. +- name: weekly crowdsec hub update + ansible.builtin.cron: + name: crowdsec hub upgrade + weekday: "1" + hour: "4" + minute: "17" + user: root + job: >- + docker exec crowdsec cscli hub update >/dev/null && + docker exec crowdsec cscli hub upgrade >/dev/null && + docker kill -s HUP crowdsec >/dev/null + +- name: firewall bouncer on the host + ansible.builtin.import_tasks: crowdsec-firewall-bouncer.yml diff --git a/roles/docker/templates/crowdsec-acquis-gitea.yaml.j2 b/roles/docker/templates/crowdsec-acquis-gitea.yaml.j2 new file mode 100644 index 0000000..5f29f35 --- /dev/null +++ b/roles/docker/templates/crowdsec-acquis-gitea.yaml.j2 @@ -0,0 +1,5 @@ +# Gitea's log file: failed web logins (collection LePresidente/gitea). +filenames: + - /var/log/gitea/gitea.log +labels: + type: gitea diff --git a/roles/docker/templates/crowdsec-acquis-sshd.yaml.j2 b/roles/docker/templates/crowdsec-acquis-sshd.yaml.j2 new file mode 100644 index 0000000..5774410 --- /dev/null +++ b/roles/docker/templates/crowdsec-acquis-sshd.yaml.j2 @@ -0,0 +1,9 @@ +# Host sshd (Debian 13 logs as sshd-session) from the host journal. +# -D and the path must be separate items. +source: journalctl +journalctl_filter: + - "-D" + - "/var/log/journal" + - "_SYSTEMD_UNIT=ssh.service" +labels: + type: syslog diff --git a/roles/docker/templates/crowdsec-compose.yml.j2 b/roles/docker/templates/crowdsec-compose.yml.j2 index 7c45dad..fee9d92 100644 --- a/roles/docker/templates/crowdsec-compose.yml.j2 +++ b/roles/docker/templates/crowdsec-compose.yml.j2 @@ -1,9 +1,10 @@ -# CrowdSec: reads Caddy's JSON access log, decides which IPs to ban, and -# serves those decisions (LAPI :8080) and AppSec checks (:7422) to Caddy over -# lava. Nothing is published to the host. +# CrowdSec: reads Caddy's access log, the host's sshd journal and Gitea's +# log, decides which IPs to ban, and serves those decisions to Caddy (lava) +# and to the host firewall bouncer (LAPI on 127.0.0.1:8080 only). services: crowdsec: - image: crowdsecurity/crowdsec:{{ crowdsec_version | default('latest') }} + # -debian: the Alpine image has no journalctl (needed for host sshd logs) + image: crowdsecurity/crowdsec:{{ crowdsec_version | default('latest') }}-debian container_name: crowdsec restart: unless-stopped env_file: .env # BOUNCER_KEY_caddy @@ -11,7 +12,10 @@ services: COLLECTIONS: >- crowdsecurity/caddy crowdsecurity/http-cve crowdsecurity/base-http-scenarios crowdsecurity/appsec-virtual-patching + crowdsecurity/linux crowdsecurity/sshd LePresidente/gitea TZ: Etc/UTC + ports: + - "127.0.0.1:8080:8080" # LAPI, for the host firewall bouncer only volumes: - crowdsec_config:/etc/crowdsec - crowdsec_data:/var/lib/crowdsec/data @@ -23,6 +27,15 @@ services: read_only: true volume: subpath: logs + # Host journal (sshd), read with journalctl -D + - /var/log/journal:/var/log/journal:ro + # Only Gitea's log folder from its data volume + - type: volume + source: gitea_data + target: /var/log/gitea + read_only: true + volume: + subpath: gitea/log security_opt: - no-new-privileges:true labels: @@ -34,6 +47,9 @@ volumes: caddy_data: external: true name: caddy_caddy_data + gitea_data: + external: true + name: gitea_gitea networks: default: diff --git a/roles/docker/templates/crowdsec-env.j2 b/roles/docker/templates/crowdsec-env.j2 index 22ef27d..ee265e4 100644 --- a/roles/docker/templates/crowdsec-env.j2 +++ b/roles/docker/templates/crowdsec-env.j2 @@ -1,4 +1,5 @@ BOUNCER_KEY_caddy={{ vault_crowdsec.bouncer_key }} +BOUNCER_KEY_firewall={{ vault_crowdsec.firewall_bouncer_key }} # CrowdSec console (app.crowdsec.net): enrolls on start; accept it in the console once. ENROLL_KEY={{ vault_crowdsec.enroll_key }} ENROLL_INSTANCE_NAME=cogitator-01 diff --git a/roles/docker/templates/crowdsec-firewall-bouncer.yaml.local.j2 b/roles/docker/templates/crowdsec-firewall-bouncer.yaml.local.j2 new file mode 100644 index 0000000..97dcaa9 --- /dev/null +++ b/roles/docker/templates/crowdsec-firewall-bouncer.yaml.local.j2 @@ -0,0 +1,23 @@ +# Overrides for /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml (ansible) +# nftables: own tables, input hook (host, e.g. ssh) and forward hook (traffic +# DNATed to containers: 80/443/222), before Docker's and ufw's chains. +mode: nftables +api_url: http://127.0.0.1:8080/ +api_key: {{ vault_crowdsec.firewall_bouncer_key }} +# Keep retrying if crowdsec isn't up yet instead of exiting. +retry_initial_connect: true +update_frequency: 10s +deny_action: DROP +nftables_hooks: + - input + - forward +{% if not crowdsec_firewall_all_decisions %} +# Until crowdsec_firewall_all_decisions: only local SSH detections and manual +# bans. Afterwards every decision (HTTP, Gitea, community blocklist) is +# enforced on all ports. +scenarios_containing: + - ssh +origins: + - crowdsec + - cscli +{% endif %} diff --git a/roles/docker/templates/gitea-compose.yml.j2 b/roles/docker/templates/gitea-compose.yml.j2 index 39f43b0..23892a4 100644 --- a/roles/docker/templates/gitea-compose.yml.j2 +++ b/roles/docker/templates/gitea-compose.yml.j2 @@ -34,6 +34,10 @@ services: # docker exec -u git gitea gitea admin ... - GITEA__service__ENABLE_PASSWORD_SIGNIN_FORM=false - GITEA__service__ENABLE_PASSKEY_AUTHENTICATION=false + # Also log to /data/gitea/log/gitea.log for CrowdSec (failed logins); + # request lines stay on the console only. + - GITEA__log__MODE=console,file + - GITEA__log__logger_0X2E_router_0X2E_MODE=console restart: unless-stopped labels: mag37.dockcheck.update: true