Project Phoenyx – Part 7: The Bodyguard (Crowdsec Deep Dive)
Introduction: The Internet is a Battlefield
As soon as you expose a server with port 80 or 443 to the internet, it takes less than 5 minutes for the first bots to knock. They are looking for outdated WordPress versions, open admin interfaces, or trying to guess passwords (brute force).
A normal firewall is not enough here, because these attacks come in via normal web traffic. We need intelligent analysis. In the past, people used Fail2Ban for this. Today we use Crowdsec.
Crowdsec works similarly to the navigation app Waze: If one user reports a traffic jam (or here: an attack), everyone else is warned.
If an IP address attacks my server, my Crowdsec reports this to the central database. As soon as there are enough reports, this IP lands on a global blocklist. So your server benefits from attacks on thousands of other servers and blocks bad guys before they even scan you for the first time.
The Architecture: Brain and Muscle
Crowdsec strictly separates detection and defense. That's what makes it so flexible.
1. The Agent (LAPI) - The Brain
The Crowdsec container is our detective. It reads log files (e.g., from Traefik) and analyzes them in real-time.
- Parsers: Understand the log format ("Ah, this is a Traefik log, here is the IP, there is the status code").
- Scenarios: Recognize patterns ("This IP accessed a non-existent page 50 times in 10 seconds -> That's a web scanner!").
If a scenario matches, the agent makes a "Decision": IP 1.2.3.4 is banned for 4 hours.
2. The Bouncer - The Muscle
The agent itself does not block anything. It only says "This is bad". To block, we need a Bouncer. In our case, this is a plugin directly in Traefik. Traefik asks the agent for every request: "Is this IP allowed in?"
Installation (crowdsec-docker-compose.yml)
Let's look at our configuration in detail:
services:
crowdsec:
image: crowdsecurity/crowdsec:v1.7.3
container_name: crowdsec_lapi
restart: always
environment:
# We tell Crowdsec which rule packages to load
- COLLECTIONS=crowdsecurity/traefik crowdsecurity/http-cve
networks:
- crowdsec
volumes:
# Store configuration and database persistently
- /opt/containers/crowdsec/config:/etc/crowdsec
- /opt/containers/crowdsec/data:/var/lib/crowdsec/data
# IMPORTANT: Access to the logs of other containers!
- /var/log/traefik:/var/log/traefik:ro
healthcheck:
test: ["CMD", "cscli", "lapi", "status"]
Important Details:
- COLLECTIONS: Here we download ready-made intelligence.
crowdsecurity/traefikbrings everything needed to understand Traefik logs.http-cvedetects attempts to exploit known vulnerabilities. - Volumes (Logs): Crowdsec must be able to read the logs. That's why we mount
/var/log/traefikinto the container. (Note: This assumes Traefik is configured to write logs to a file, not just stdout).
Configuration: Traefik as Log Writer
For Crowdsec to have something to read, we need to tell Traefik to write logs. A look back at our traefik.yml:
accessLog: filePath: "/var/log/traefik/access.log" format: json bufferingSize: 50
We use the JSON format because Crowdsec can parse it extremely fast and without errors. The bufferingSize ensures that the hard drive is not stressed with every single request.
Configuration: Traefik as Bouncer
Now we connect the muscle. In the traefik-docker-compose.yml, we set these labels:
labels:
- "traefik.http.middlewares.crowdsec.plugin.crowdsec.crowdsecLapiUrl=http://crowdsec_lapi:8080"
- "traefik.http.middlewares.crowdsec.plugin.crowdsec.crowdsecLapiKey=${CROWDSEC_LAPI_KEY}"
- "traefik.http.routers.traefik.middlewares=crowdsec"
Here we define a middleware called crowdsec. It connects to our Crowdsec container (LAPI).
Important: For this to work, both containers must be in the same Docker network (crowdsec)!
Generating the API Key
You are probably asking: "Where do I get the CROWDSEC_LAPI_KEY?" We have to generate it once.
1. Start the Crowdsec container: docker compose up -d crowdsec
2. Enter the container: docker exec -it crowdsec_lapi bash
3. Generate the key: cscli bouncers add traefik-bouncer
Add the key you receive to your .env file.
Crowdsec in Daily Life: Important Commands
Crowdsec runs in the background, but sometimes you want to know what's happening. Here are the most important commands for the console (inside the container):
1. Who was banned?
cscli decisions list
Shows you a list of all currently blocked IPs and the reason (e.g., "http-crawl-non_statics").
2. What is the status?
cscli metrics
Shows you statistics: How many logs were read? How many attacks detected?
3. Unbanning a friend
Locked yourself out? No problem:
cscli decisions delete --ip 1.2.3.4
Conclusion & Outlook
With Crowdsec, we have an active defense line. Our server "learns" from attacks and protects itself.
Now our infrastructure is secure and stable. In the next part, we will take care of "Identity". We will install Authentik so we don't have to remember a new password for every service.