The LabDetection

Falco

Runtime threat detection on the lab's servers, with rules tuned to cut noise.

How it connects

Sends to Falco

  • Linux kernelThe system calls of every process, read through eBPF
  • AnsibleInstalls Falco and rolls out its rules
Falco

Receives from Falco

  • Grafana LokiEvery event, forwarded by Falcosidekick and kept as history
  • AlertmanagerEvery event, forwarded by Falcosidekick and sent as a notification
  • PrometheusFalco's own health metrics

What it does here

Falco is the lab’s runtime detection. It watches what programs do while they run, on the lab’s servers and inside their containers.

Every program has to ask the Linux kernel for anything outside its own memory: to open a file, to start another program, to make a network connection. Each request is a system call. The kernel is like the only service counter in a building: whatever a program wants, it has to ask there. Falco stands next to the counter, reads every request and compares it with a set of rules. It reads the requests through eBPF, a way to run small, verified programs inside the kernel without loading a kernel module.

A rule describes behaviour that should not happen, and a match becomes an event. The rules in use catch, among other things:

  • a container that runs a program which was not part of its image;
  • a program with no reason to do so reading a file such as /etc/shadow, where password hashes are kept;
  • an interactive shell opened inside a container;
  • a web server or PHP process starting a shell, a common sign that someone is running commands through a web application.

The last of these is a rule written for the lab:

# The programs a web application runs as.
- list: web_shell_parents
items: [apache2, httpd, nginx, php, php-fpm, php-cgi]
- rule: Shell spawned by web service
desc: A web server or PHP process started a shell.
# spawned_process and shell_procs are macros from Falco's default rules.
condition: >
spawned_process and shell_procs
and proc.pname in (web_shell_parents)
output: >
Shell spawned by web service | parent=%proc.pname
process=%proc.name command=%proc.cmdline user=%user.name
priority: WARNING
tags: [shell, mitre_execution, T1059.004]

Each event goes to Falcosidekick, a small program that passes Falco events on to other systems. It sends every event to Loki, which keeps the history, and to Alertmanager, which sends a notification for it: critical events loudly, the rest quietly. Only events of warning priority or higher leave Falco, so informational rules add no noise.

The rules are kept in a repository and reach the servers only through Ansible. Every minute, each server also runs a harmless program that one rule is written to catch, so a test event is always travelling the same path as a real one. A scheduled check confirms that these events arrive, and alerts when they stop.

Why Falco

A log holds what a program chose to write down, and an intruder’s tools write nothing. A system call cannot be skipped: whatever a process does on a server passes through the kernel. That makes Falco the counterpart to the other two detection tools. Trivy shows what could be exploited, the rules in Loki watch what the logs say, and Falco shows what is happening.

It suits a lab that runs on containers:

  • One install per server covers every container. Nothing is added to an image, and an event still names the container and the image it came from.
  • No kernel module. The eBPF engine works with the kernel as it is shipped, so a kernel upgrade does not break it. The kernel has to expose its type information (/sys/kernel/btf/vmlinux), which current distribution kernels do.
  • Rules are plain text. A rule is a readable condition in YAML, the project maintains a default set to start from, and an exception is a few lines that can be reviewed and tested.

The cost is the tuning. The default rules are written for any environment, and on an ordinary Docker host some of them fire all day until they are adjusted. Falco also only detects. It does not block or stop anything, so what happens after an event is still up to a person.

How the rules are tuned

Exceptions as narrow as the noise allows

A database health check is a typical source of noise. Docker runs pg_isready every few seconds to see whether PostgreSQL is up, and that program reads /etc/shadow on the way. Falco’s rule for sensitive files fires each time.

The exception names the program and the file together. An exception for the program alone would also hide pg_isready reading anything else.

falco_rules.local.yaml
- rule: Read sensitive file untrusted
override:
exceptions: append
exceptions:
- name: pg_isready_reads_shadow
fields: [proc.name, fd.name]
comps: [=, =]
values:
- [pg_isready, /etc/shadow]

An exception is added only for behaviour that was looked at and found normal. When a rule fires on a container that has no reason to do that, the event is investigated first.

Automation that does the suspicious thing on purpose

CI jobs and update tools do what one of the most useful rules looks for: they build or download a program inside a container and run it. Silencing the rule for those containers would open a gap exactly where third-party code runs.

Their exceptions therefore match the whole context of a known step: the server, the user, the absence of a terminal, the job’s container and image, the program, its parent processes, the working directory and the command line. A step that differs in one of them still raises a critical event.

Steps that run third-party code, such as a freshly built test program, are not silenced at all. They move to a rule of their own at warning priority, so they are recorded and posted quietly, not raised as critical:

- rule: Reviewed CI test execution
desc: A test program built and run by a reviewed CI job; kept for audit.
# reviewed_ci_test_command: a macro for the context described above.
condition: >
spawned_process and container
and proc.is_exe_upper_layer = true
and reviewed_ci_test_command
output: >
Reviewed CI test execution | process=%proc.name
parent=%proc.pname command=%proc.cmdline
priority: WARNING

Exceptions are tested like code

Each exception is tested against the real Falco engine, not against a reading of its condition. Harmless stand-in programs reproduce every allowed context and must stay quiet. Copies with one detail changed, such as a different argument, user, image or parent process, must still raise the critical event.

One limit remains. An exception matches names, paths and arguments, not the contents of a program. Whoever reproduces an allowed context exactly gets past that one rule, while the other rules still apply.

Notes from running it

Falcosidekick receives nothing without JSON output

Falcosidekick expects each event as JSON. With Falco’s default text output it drops every event.

falco.yaml
json_output: true
http_output:
enabled: true
url: "http://falcosidekick.example.com:2801"

For Alertmanager, set Falcosidekick’s ALERTMANAGER_APIVERSION to v2. Current Alertmanager releases no longer have the older API.

Container names come from a plugin

Recent Falco versions get the details of a container from a plugin. Without it, an event from inside a container carries no container name and no image.

falco.yaml
plugins:
- name: container
library_path: /usr/share/falco/plugins/libcontainer.so
load_plugins:
- container
# Add the container to the text of every event.
append_output:
- match:
source: syscall
extra_output: >-
container_name=%container.name
container_image=%container.image.repository

Keep one source for the rules

The Falco package installs a service, falcoctl-artifact-follow, that downloads rule updates on its own. With the rules in a repository, that is a second source which nobody reviews.

In this lab it also restarted in a loop, because it asks Falco’s web server on localhost while the web server listens on another address.

Terminal window
# Stop the updater and keep it from starting again.
# A package upgrade leaves the mask in place.
sudo systemctl mask --now falcoctl-artifact-follow.service

A package upgrade can leave Falco stopped

The package’s install script stops and disables every Falco service, and starts one again only when it is told which driver to use. An upgrade by hand without that setting ends with no detector running.

Terminal window
# Upgrade only Falco and keep the existing configuration.
# FALCO_DRIVER_CHOICE makes the script start the eBPF service again.
sudo env DEBIAN_FRONTEND=noninteractive FALCO_FRONTEND=noninteractive \
FALCO_DRIVER_CHOICE=modern_ebpf \
apt-get -y \
-o Dpkg::Options::=--force-confdef \
-o Dpkg::Options::=--force-confold \
install --only-upgrade falco
# Confirm that the detector is running again.
systemctl is-active falco-modern-bpf.service

Prove the whole path with a harmless event

A detection that has stopped working looks the same as a quiet day. To tell them apart, a timer runs a copy of /usr/bin/true under a path of its own once a minute, as an unprivileged user, and a rule matches exactly that path:

- rule: Detection canary
desc: Deliberate harmless event that proves the detection path works.
condition: >
spawned_process and not container
and proc.exepath = /usr/local/libexec/falco-canary
output: "FALCO_CANARY | process=%proc.name user_uid=%user.uid"
priority: WARNING
tags: [canary]

In Alertmanager, a route sends this one rule to a receiver that notifies nobody, so the test events do not flood the notifications:

alertmanager.yml
routes:
- matchers: ['source="falco"', 'alertname="Detection canary"']
# A receiver without a notification target.
receiver: detection-canary

A scheduled check then looks for each server’s test event in Loki and in Alertmanager, and alerts when one is missing.

Finding the events in Loki

Falcosidekick stores the events in Loki with the labels source, hostname, rule and priority:

{source="syscall", priority="Critical"}
{source="syscall", rule="Read sensitive file untrusted"}
{source="syscall", hostname="server-1"}

Two details save time when reading them. The label for the server is hostname, which can differ from the label other logs use (host in this lab). And the time printed at the start of a Falco message is the server’s local time, while the timestamp Loki shows is in UTC.