Hardening a Public VPS: CrowdSec, ModSecurity & Egress Firewalls

Layered defense for a box that's under attack the moment it gets a public IP.

Hardening a Public VPS: CrowdSec, ModSecurity & Egress Firewalls

A public VPS gets scanned within hours of going online.

I assume the box will be breached someday, so I stack cheap layers. Each one costs little. Together they limit what an intruder can reach.

Layer 1: SSH with keys only

Three settings in sshd_config:

PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no

Check the running values with sshd -T | grep -Ei 'passwordauth|kbdinter|permitroot'.

Layer 2: CrowdSec and a Traefik bouncer

CrowdSec parses the reverse proxy’s logs and flags abusive addresses. A bouncer in Traefik then blocks them. The Traefik bouncer is a community plugin, maxlerebourg/crowdsec-bouncer-traefik-plugin, listed as third party. It supports stream mode and AppSec.

Three settings matter:

  • Put the bouncer key in a file with crowdsecLapiKeyFile, not inline in the config.
  • Behind another proxy, set forwardedHeadersTrustedIps. Without it the plugin sees the proxy’s address for every client.
  • In stream mode the plugin refuses every request when it cannot reach the CrowdSec local API. That is a general property of the plugin: it fails closed. Plan for it.

CrowdSec also ships a community blocklist. The tiers:

Tier Size Who gets it
Lite up to 3,000 IPs free users who rarely send signals
Community about 15,000 free users who send signals regularly
Premium up to 60,000 paid

Only signals from unmodified Hub scenarios count toward sharing.

Layer 3: ModSecurity in detection mode

Trustwave’s commercial support for ModSecurity ended on 1 July 2024. The project moved to OWASP in January 2024, and the Core Rule Set docs date that to 25 January 2024.

The engines differ in how well they pass the Core Rule Set tests:

  • ModSecurity v2 on Apache passes 100% of the CRS unit tests.
  • libmodsecurity v3 fails 2 to 4%.
  • Coraza, the OWASP Go engine, passes 100% of the CRS v4 tests and runs on Traefik and Caddy. It is the alternative to name.

Traefik has no built-in ModSecurity. Use a plugin that forwards each request to a separate OWASP modsecurity-crs container. The settings I use:

MODSEC_RULE_ENGINE=DetectionOnly
PARANOIA=1
ANOMALY_INBOUND=5

The rollout has four steps. Run in DetectionOnly. Read the audit log. Tune exclusions. Then set MODSEC_RULE_ENGINE=On.

My engine is still in DetectionOnly, so it logs suspicious requests and blocks nothing yet.

Layer 4: egress rules

Egress rules stop an untrusted workload from reaching other services, the cloud metadata endpoint or SMTP. Docker complicates this in three ways:

  1. Docker writes its rules in the nat table, so ufw does not filter published ports.
  2. Put your rules in the DOCKER-USER chain, which runs before Docker’s own chains.
  3. Packets arrive after DNAT. To match the original destination of a published port, use -m conntrack --ctorigdst.

The metadata address, 169.254.169.254, sits inside 169.254.0.0/16, so one drop rule covers it.

A generic example for an untrusted network on 198.51.100.0/24, using a documentation range:

iptables -N UNTRUSTED_EGRESS
iptables -I DOCKER-USER -s 198.51.100.0/24 -j UNTRUSTED_EGRESS
iptables -A UNTRUSTED_EGRESS -d 198.51.100.0/24 -j RETURN
iptables -A UNTRUSTED_EGRESS -d 10.0.0.0/8 -j DROP
iptables -A UNTRUSTED_EGRESS -d 172.16.0.0/12 -j DROP
iptables -A UNTRUSTED_EGRESS -d 192.168.0.0/16 -j DROP
iptables -A UNTRUSTED_EGRESS -d 169.254.0.0/16 -j DROP
iptables -A UNTRUSTED_EGRESS -p tcp -m multiport --dports 25,465,587 -j DROP
iptables -A UNTRUSTED_EGRESS -j RETURN

The chain returns traffic inside the subnet, drops the private ranges and link-local range, and drops SMTP ports 25, 465 and 587. Everything else falls through.

The IPv6 gap

My first version of this post ignored IPv6. iptables rules leave a dual-stack host unfiltered on IPv6. The Defensive Security Handbook, 2nd edition, chapter 16 (Egress Filtering and IPv6: A Cautionary Note), covers this. Either write matching ip6tables rules or say IPv6 is off.

IPv6 is off on my untrusted network, so the IPv4 rules above cover it.

Why layers

The Defensive Security Handbook and the CISSP Official Study Guide, 9th edition, chapter 1, call this defense in depth. Chapter 16 of the CISSP guide covers least privilege, which is what the egress rules enforce for a container. Each layer fails on its own, so the next one has to catch what the last missed.

Subscribe to Just another Geek

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe