Detect Shells in Pods With Falco
Scenario
Attackers often run single commands in a container through a shell, for example bash -c.
Nobody on your team notices this today.
The security team wants an alert for it and asked you to set up Falco.
The built-in rules of Falco already report interactive shells, so the team wrote a rule for the other ones.
The cluster has two nodes:
cplane-01runs the control plane and has aNoScheduletaint.node-01is the only worker node. Your Pods run here, so Falco must run here too.
kubectl works on both nodes.
This is the rule of the security team:
- rule: Shell Spawned in Pod
desc: Detect a shell started inside a Kubernetes pod
condition: spawned_process and container and proc.name in (bash, sh)
output: Shell in pod (ns=%k8s.ns.name pod=%k8s.pod.name container=%container.name user=%user.name cmd=%proc.cmdline)
priority: WARNING
Task
- Install Falco on
node-01with the modern eBPF driver, as described in Install on a host (DEB, RPM). The service must run and start again after a reboot. - Add the rule, unchanged, on
node-01in a file that a Falco upgrade does not overwrite. Leave the default rules file as it is. - Validate both rules files, and make sure that the running Falco raises the alert of the custom rule.
- Create a Pod named
falco-testin thedefaultnamespace with the imagenginx:stable. Run a single command in it throughbashorsh, without a terminal. - Save the Falco service logs from the systemd journal to
/home/laborant/falco.logonnode-01. The file must contain the alert of the custom rule for your shell.
Hint 1: Installing Falco
The Falco docs show how to trust the key of the Falco repository, add the repository, and install the package.
During the installation, Falco asks which driver to use.
The modern eBPF driver needs a recent kernel, so check the kernel of node-01 with uname -r first.
The name of the systemd service that Falco runs depends on the driver you choose.
Docs:
- Install on a host (DEB, RPM) (Falco)
- Kernel Events (Falco)
Hint 2: Custom rules
The rules_files setting in /etc/falco/falco.yaml lists the files that Falco loads, in order.
One of them is meant for your own rules.
Validate it with falco together with the default rules file, because the rule uses macros from there.
Docs:
- Basic Elements of Falco Rules (Falco)
- Custom Ruleset (Falco)
Hint 3: Reading the alerts
journalctl shows the logs of a single systemd unit.
Look for the text that starts the output of the custom rule.
A shell opened with kubectl exec -it matches a built-in rule first, so by default you see only the built-in alert for it.
The -c flag of a shell runs a single command.
Docs:
- Falco Outputs (Falco)
- Get a Shell to a Running Container (Kubernetes)
- journalctl(1) (Linux manual page)