Sub topic 15
Auditing with the CIS Docker Benchmark and monitoring at runtime
This chapter assumes you have worked through the earlier chapters. You should know how to harden images and containers, how the Docker daemon is exposed, and how Linux users and processes work. You should also be comfortable reading daemon.json and basic shell output.
Hardening is only useful if you can show it is in place and notice when it stops holding. Two practices cover this. An audit compares a host against a written standard. Runtime monitoring watches what containers do after they start.
What the CIS Docker Benchmark is
The Center for Internet Security (CIS) publishes a Docker Benchmark. It is a numbered checklist of configuration recommendations, each with a rationale, an audit command, and a remediation. The recommendations are grouped into sections.
| Section | Covers | Example recommendation |
|---|---|---|
| 1. Host configuration | The Linux host under Docker | Use a separate partition for `/var/lib/docker`; audit Docker files and directories |
| 2. Daemon configuration | `dockerd` settings | Restrict traffic on the default bridge; enable `live-restore`; avoid an unauthenticated TCP socket |
| 3. Daemon configuration files | Ownership and permissions | `/etc/docker` and `docker.sock` owned by `root:root` or `root:docker` with tight modes |
| 4. Images and build files | Your Dockerfiles and images | Create a non-root user; use trusted base images; do not store secrets in Dockerfiles |
| 5. Container runtime | How containers are started | No `--privileged`; drop capabilities; set memory and CPU limits; read-only root filesystem |
| 6. Security operations | Process around Docker | Keep images current; avoid image sprawl |
| 7. Swarm configuration | Docker Swarm only | Skip it if you do not run Swarm |
Each recommendation is marked Level 1 or Level 2. Level 1 items are practical on most systems. Level 2 items give defense in depth but may cost functionality or effort. Start with Level 1.
The Benchmark is versioned and tied to Docker releases. Check that the version you audit against matches the Docker Engine you run. Otherwise a check may flag a setting that no longer exists or miss a newer one.
Running an automated audit with Docker Bench for Security
Docker publishes a shell script, Docker Bench for Security, that automates many of the checks. It reads the host and the running containers and prints one result per recommendation. Run it on the Docker host as root, because many checks read protected files.
git clone https://github.com/docker/docker-bench-security.git
cd docker-bench-security
sudo sh docker-bench-security.sh -l /tmp/docker-bench.logThe -l option writes the output to a log file. Run sh docker-bench-security.sh -h to list the other options, such as running only selected checks.
Each line starts with a status label:
[PASS]means the check succeeded.[WARN]means the setting does not meet the recommendation.[INFO]means the script cannot decide and a person must review it.[NOTE]means a manual review is advised.
A fresh host often prints many warnings. Do not try to clear them all in one pass.
Triage the results
Treat the output as a work list, not a grade.
- Read the rationale for each
[WARN]. Some recommendations do not apply, for example a Swarm check on a host that does not use Swarm. - Rank the rest by exposure. A container started with
--privilegedor a mounted Docker socket matters more than a missing log-rotation option. - Fix the item, then record the fix as code: a
daemon.jsonchange, a Compose file, or a Dockerfile edit. A manual fix on one host will be lost on the next one. - For any item you decide not to fix, write down the reason. An accepted risk with a recorded reason can be reviewed later. An unexplained warning cannot.
Common benchmark findings and fixes
Many daemon-level warnings are fixed in /etc/docker/daemon.json. This example addresses several Section 2 recommendations:
{
"icc": false,
"live-restore": true,
"userland-proxy": false,
"no-new-privileges": true,
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}"icc": falsestops containers on the default bridge network from talking to each other unless you link them or use a user-defined network."live-restore": truekeeps containers running while the daemon restarts or is upgraded."userland-proxy": falseuses iptables rules instead of a userspace proxy process for published ports."no-new-privileges": truemakes that setting the default for new containers.- The
log-optsblock rotates logs. Without it, a chatty container can fill the disk.
Restart the daemon after editing the file with sudo systemctl restart docker. Unless live-restore was already active, containers stop and start again. Do this in a maintenance window.
Host-level recommendations (Section 1) ask you to audit changes to Docker's files. On Linux hosts running the audit daemon (auditd), add watch rules. Put them in /etc/audit/rules.d/docker.rules:
-w /usr/bin/dockerd -k docker
-w /var/lib/docker -k docker
-w /etc/docker -k docker
-w /etc/docker/daemon.json -k dockerLoad them with sudo augenrules --load. Afterward, sudo ausearch -k docker shows who touched those paths and when. Adjust the dockerd path if your distribution installs it elsewhere. Check with command -v dockerd.
Limits of automated audits
- The script checks configuration, not behavior. A host can pass every check and still run a vulnerable application.
- It sees only the state at the moment you run it. A container started with
--privilegedan hour later will not appear until the next run. - It can report
[PASS]for a setting that exists but is ineffective, such as a limit that is set far too high to matter. - The script runs with elevated access to read the host. Obtain it from the official repository and read it before you run it as root.
For these reasons, schedule the audit (for example from a weekly job) and compare each result with the last one. A new [WARN] is drift, which means someone changed something.
Monitoring containers at runtime
Auditing tells you how things are configured. Monitoring tells you what is happening. The aim is to detect behavior that a healthy container of this kind should not show, such as a shell appearing inside a web server container.
Docker has several built-in tools. They are limited but cost nothing.
# Stream lifecycle events for containers
docker events --filter 'type=container' --filter 'event=start' --filter 'event=die'
# One-time snapshot of CPU, memory, network, and process counts
docker stats --no-stream
# Processes running inside a container
docker top web
# Files added (A), changed (C), or deleted (D) since the container started
docker diff webdocker diff is useful for incident checks. A container that follows the read-only rule should show almost no changes. Unexpected files under /usr/bin or /etc point to tampering.
A few cautions about these built-in tools:
docker eventsonly reports what the daemon sees. It does not show what a process does inside a container.docker statsvalues that climb toward your--memoryor--pids-limitsettings mean a limit is about to be reached. An unexplained CPU spike can mean abuse such as cryptomining.- Collect container logs outside the host as well. An attacker with host access can delete local logs.
Syscall-level detection with Falco
Falco is an open-source runtime security tool. It watches system calls made by processes, including those inside containers, and matches them against rules. Because it observes the kernel, it sees activity inside a container that Docker itself does not report.
A Falco rule has a condition, an output message, and a priority. This rule alerts when a shell starts in any container:
- rule: Shell started in container
desc: Detects an interactive shell process starting inside a container.
condition: spawned_process and container and proc.name in (sh, bash, dash)
output: >
Shell in container (user=%user.name container=%container.name
image=%container.image.repository command=%proc.cmdline)
priority: WARNING
tags: [container, shell]This rule uses the spawned_process and container macros. Falco defines them in its default rule file, so load the default rules too. Falco ships a similar built-in rule, so check for duplicate alerts before you add your own.
Useful things to alert on include:
- A shell or package manager starting in a production container.
- Writes to sensitive paths such as
/etcor/usr/bin. - A container opening an outbound connection it never made before.
- A process reading the Docker socket.
- Use of
nsenterorchroot, which can signal an escape attempt.
Choosing what to watch
You do not need every tool. Pick by the question you want to answer.
| Question | Tool |
|---|---|
| Is the host configured to the standard? | Docker Bench for Security, scheduled |
| Who changed Docker files or the daemon? | `auditd` watch rules |
| Did a container start, stop, or crash unexpectedly? | `docker events` |
| Is a container near its resource limits? | `docker stats`, or a metrics system |
| Did a container's filesystem change? | `docker diff` |
| Is a process behaving abnormally inside a container? | Falco or a similar syscall monitor |
Send results to one place, such as a log platform, with alerts routed to a person. A warning that nobody reads does not protect anything.
Common mistakes
- Treating a benchmark score as proof of security. The score measures configuration against a list. It does not measure exposure.
- Fixing hosts by hand. The next rebuilt host will be non-compliant again.
- Running the audit once. Drift is the main thing it finds.
- Collecting data without alerts, or alerts without an owner.
- Keeping logs only on the host that may be compromised.
Exercise
Docker Bench for Security reports [WARN] for a check about Swarm on a host that does not use Swarm, and [WARN] for a running container started with --privileged. Which do you fix first, and how do you handle the other?
Show answer
Fix the privileged container first. It gives the container nearly full access to the host and is a direct escape risk. Restart it without --privileged, granting only the specific capabilities it needs, and make the change in the run command or Compose file so it persists. The Swarm warning does not apply because Swarm is not in use. Record that it is accepted as not applicable, with the reason, so a later reviewer does not repeat the analysis.
Exercise
A container named web was started with --read-only. An hour later docker diff web lists a new file, A /usr/bin/.helper. What does this suggest, and what is your first response?
Show answer
A read-only root filesystem should block writes to /usr/bin, so a new file there suggests the container was not actually read-only or that something with elevated access wrote it, which may mean compromise. Preserve evidence first by copying the file and the container logs off the host. Then isolate or stop the container, and check how it was started with docker inspect web (look at HostConfig.ReadonlyRootfs). Finally, redeploy from a clean image and find the entry point.