Back

DevSecOps Labs

Runtime Security with Falco

45 minRequires: docker

Scanning finds problems before you ship - but a clean image can still be attacked once it is running. Someone exploits a web app, opens a shell inside the container, reads a secret file, installs a tool. None of that shows up in an image scan, because the image was fine; the behaviour is the attack.

Falco is a runtime security tool. It watches the Linux kernel's system calls and raises an alert when something matches a rule - a shell spawned in a container, a write below /etc, an outbound connection from a process that should never make one. It is a CNCF graduated project and the de-facto standard for runtime threat detection on Kubernetes and plain Docker alike.

Why this lab matters

A scan is a snapshot of what your image looked like when it was built. It tells you nothing about what the container does once it is live. Runtime detection is the other half: it watches actual behaviour and tells you when a container does something it never should, while it is happening.

After this lab you will be able to:

  • run Falco against your container engine and read its alerts
  • understand how a Falco rule maps kernel syscalls to a human-readable warning
  • trigger a detection on purpose and find the alert it produces
  • write your own rule for a behaviour you care about

How this lab works

Read this once - every lab in this track follows the same rules.

Where you type the commands. Open a terminal and go to the folder where you cloned this repository. On Windows use PowerShell (search for it in the Start menu), on macOS or Linux use Terminal. Every path in this lab is written from the repository root, so stay there unless a step says otherwise.

Lines starting with > are explanations, not commands. Don't paste them. Only the text inside grey code boxes gets typed.

The 📋 sign means it's your turn. When you see it, try the step yourself first. Underneath it there is a collapsed "Not sure how?" arrow with the answer - open it after you have tried, not before. The labs are written this way on purpose: you remember what you worked out, not what you pasted.

If a command fails, that is normal. Some steps fail deliberately to show you what the error looks like. Read the sentence above the command before you assume something is broken.

When you get stuck, in this order:

  1. Re-read the paragraph above the command - the answer is usually there.
  2. Open the "Not sure how?" arrow.
  3. Open hints.md for a nudge.
  4. Open solution.md for the full answer. Using it is fine; copying it without reading is not.

When you finish, run the Cleanup section at the bottom. Labs share ports and container names, so leftovers from one lab break the next one.

Before you start

Takes about 45 minutes.

Docker Desktop must be running. Check it:

docker version

A Server: section in the output means you are ready. If you get an error instead, start Docker Desktop and wait until the whale icon stops animating.

Run every command from the root of this repository - the paths in this lab are written relative to it.

References

Running Falco

Falco runs as a privileged container so it can see every process the kernel starts. This Docker Compose file starts two things: Falco itself, and a small alpine container called target that we will poke at to set off alerts.

Falco would normally download its default rule set from the internet on the first run. To keep this lab self-contained, we point it at our own rules file through a minimal config, so it loads exactly one rule and nothing else.

Start both containers and leave Falco running in the foreground so you can watch it:

docker compose -f labs/falco/docker-compose.yml up

The first time, Docker pulls the Falco image, which is a few hundred MB. After that it is cached.

Falco prints its startup log. Among the lines you should see it load the modern eBPF probe and then read the rules directory. The last useful line looks like Rules match: ... or Loading rules from file /etc/falco/rules.d/..., followed by Starting health webserver and then silence - silence is good, it means nothing suspicious has happened yet.

📋 Confirm Falco started cleanly and loaded exactly one rule.

Not sure how?

Look near the top of the output for a line reporting the rules it loaded. If you scrolled past it, open a second terminal and read the recent log:

docker logs falco | grep -i rule

You should see it loaded the rules from /etc/falco/rules.d and no errors. If instead you see Runtime error or a driver complaint, jump to If something goes wrong at the bottom.

Triggering an alert

Our one rule fires when a shell starts inside a container. The target container is just sleeping, so let's open a shell in it.

Leave Falco running and open a second terminal. Exec into the target container:

docker exec -it falco-target sh

Switch back to the terminal where Falco is running. You should see a new line appear the moment the shell opened - a Warning with the container name, the image and the command line.

📋 Find the alert Falco produced and read which container and process it names.

Not sure how?

In the Falco terminal, the line looks like this:

Warning Container icinde kabuk acildi (user=root container=falco-target image=alpine shell=sh parent=... cmdline=sh)

The container=falco-target and shell=sh fields are Falco telling you exactly what it saw. Type exit in the second terminal to leave the shell.

This is the core idea: you did not change the image, you did not open a port. You ran a process, and Falco reported it because a rule said that process is interesting. Everything Falco does is this pattern - a condition over syscall fields, and an output line.

Reading a Falco rule

Open the rule file. A rule has four parts that matter:

  • condition - the filter over syscall fields. Ours says: an execve call inside a container (container.id != host), where the program is one of the common shells.
  • output - the message, with %fields filled in from the event.
  • priority - how loud it is (WARNING, ERROR, ...). This is what you alert or page on.
  • tags - labels for grouping and filtering rules.

Real deployments load hundreds of community rules covering known attack patterns. The skill you are building is reading and writing these conditions, because the valuable rules are the ones specific to your workloads.

Lab

A classic sign of a container being tampered with at runtime is a package manager running inside it - nobody should be doing apk add in a container that was built days ago. That is exactly the kind of behaviour a runtime rule should catch.

Write a new rule that fires when a package manager runs inside a container, add it to the rules file, restart Falco, and prove it works by installing a package in the target container. Give it priority ERROR so it stands out from the shell warning.

Stuck? Try hints or check the solution.


If something goes wrong

Cannot connect to the Docker daemon - Docker Desktop is not running. Start it and wait until the whale icon stops animating.

port is already allocated - a container from an earlier lab is still holding that port. See what is running with docker ps, then remove the old container with docker rm -f <name>.

The container name "/..." is already in use - same cause, same fix: docker rm -f <name>, then run the command again.

Falco exits with a driver or eBPF error - the modern eBPF probe needs a recent kernel. On Docker Desktop, make sure you are up to date (newest Docker Desktop, and on Windows the WSL2 backend). The privileged flag and the /proc and /dev mounts in the compose file are all required - do not remove them.

No alert appears when you open the shell - check Falco actually loaded the rule (docker logs falco | grep -i rule) and that you exec'd into falco-target, not some other container.

Still stuck? The Cleanup section below resets everything, so you can start the lab again from a clean machine.

Cleanup

Stop Falco with Ctrl+C in its terminal, then remove this lab's containers:

docker compose -f labs/falco/docker-compose.yml down

This removes only the containers this lab started. If you want a clean slate, docker ps will show anything still running.

View source on GitHub