自动化管理 Docker 容器的防火墙规则
Automate management of firewall rules for Docker containers.
Linux with a recent kernel, around 5.10 or newer.
Docker by default creates iptables rules to handle container traffic that override almost all user-set rules. There are two main ways to get around this:
"iptables": false in /etc/docker/daemon.jsonDOCKER-USER iptables chainAdding rules to the DOCKER-USER chain is what whalewall does to avoid managing more firewall rules
than it needs to. You may be wondering if whalewall is necessary, after all it is very easy to add
firewall rules to the DOCKER-USER chain yourself. Well, Docker containers and networks are ephemeral,
meaning every time a container or network is destroyed and recreated, the IP address and subnet
respectively will be randomized. Whalewall takes care of creating or deleting rules when containers
are created or killed, which would be very tedious and error-prone manually. Finally, as well as
managing firewall rules to limit traffic to and from localhost and external interfaces, whalewall
can also enforce container network isolation by limiting traffic between containers.
Whalewall listens for Docker container start and die events and creates or deletes
nftables
rules appropriately. Why is nftables used instead of iptables? A few reasons:
Whalewall stores details of containers it is managing rules for in a SQLite database. If containers are started or stopped while whalewall isn't running, whalewall will compare currently running containers to what was last saved to the database and create/delete firewall rules appropriately.
Whalewall needs the NET_ADMIN capability to manage nftables rules. It also needs to be a member
of the docker group in order to use /var/run/docker/docker.sock to receive events from the
local Docker daemon.
To reduce attack surface, landlock and seccomp are leveraged to ensure only files and syscalls required by whalewall can be accessed and called respectively. This vastly limits what whalewall is able to do in the event an attacker is able to execute code in the context of its process. However, this will not prevent said attacker from taking advantage of the Docker socket whalewall has access to which can trivially lead to privilege escalation.
Download the Docker image:
docker pull ghcr.io/capnspacehook/whalewall:0.2.0Ensure whalewall is given necessary permissions, and that it is using host network mode. This
allows the whalewall container to modify host firewall rules.
Example Docker compose file:
version: "3"
services:
whalewall:
cap_add:
- NET_ADMIN
image: ghcr.io/capnspacehook/whalewall
network_mode: host
volumes:
- whalewall_data:/data
- /var/run/docker.sock:/var/run/docker.sock:ro
volumes:
whalewall_data:If you want to run whalewall natively, download a release binary.
Or if you want to compile from source, assuming you have Go 1.19 installed:
go install github.com/capnspacehook/whalewall/cmd/whalewall@latestAfter installing whalewall, grant it required permissions by running:
# this must be run first, it will erase any set capabilities
chgrp docker whalewall
setcap 'cap_net_admin=+ep' whalewall
Whalewall uses Docker labels for configuration:
whalewall.enabled is used to enable or disable firewall rules for a container. If this label is
not present and set to true for a container, whalewall will not create any firewall rules for it.whalewall.rules specifies the firewall rules for a container. If this label is not specified but
whalewall.enabled=true is, no traffic will be allowed to or from the container (unless another
container has an output rule for this container).The contents of the whalewall.rules label is a yaml config.
Whalewall creates rules with a default drop policy, meaning any traffic not explicitly allowed will be dropped.
Below is an example Docker compose file that configures Miniflux, a feed reader. Miniflux needs to connect to a Postgresql database to store state and make outbound HTTPS connections to fetch articles, so that's only what is allowed.
…Note to make this Docker compose config as concise as possible, best practices were not followed. This is merely intended to be an example of whalewall rules, not how to setup Miniflux securely.
…Port and IP ranges are inclusive. Examples:
4000-5000 will match all ports between and including port 4000 and port 50001.1.1.1-2.2.2.2 will match all IPs between and including 1.1.1.1 and 2.2.2.2Whalewall accepts several environmental variables that can be used to configure how it connects to a Docker server:
DOCKER_HOST to set the URL to the Docker server.DOCKER_API_VERSION to set the version of the Docker API to use, leave empty for latest.DOCKER_CERT_PATH to specify the directory from which to load the TLS certificates (ca.pem, cert.pem, key.pem).DOCKER_TLS_VERIFY to enable or disable TLS verification (off by default)./var/log/kern.log for Debian based distros
and /var/log/messages for RHEL based distrosdocker0 network interface, which is often 172.17.0.1default network when creating container to
container rulesStarting from v0.2.0, all Docker images and binary checksum files are signed. You can verify images or released binaries to ensure they were not tampered with in transit.
Verifying Docker images or binaries both require cosign.
Simply check the signature of the image with cosign:
cosign verify ghcr.io/capnspacehook/whalewall: | jqYou can verify the image was built by Github Actions by inspecting the Issuer and Subject fields of the output.
Download the checksums file, certificate, signature and the archive to the same directory.
Extract the binary from the archive, verify the checksums file and verify the contents of the binary:
tar xfs whalewall__linux_amd64.tar.gz
cosign verify-blob --certificate checksums.txt.crt --signature checksums.txt.sig checksums.txt
sha256sum -c checksums.txtYou can also reproduce the released binaries to verify that they were built from this unmodified
source code. Verifying binaries requires gorepro.
First, download the release archive and extract it. Clone this repro and go into it.
Install gorepro and run it on the extracted release binary. gorepro will tell you if reproducing
the binary was successful. Don't worry about checking out the correct tag or commit, gorepro will
handle that for you.
If you don't trust gorepro you can run it again additionally passing the -d flag. This will
print the commands gorepro generated to reproduce the release binary. You can run the printed commands
and verify for yourself that the reproduced binary is bit for bit identical to the released one.
tar fxs whalewall__linux_amd64.tar.gz
git clone https://github.com/capnspacehook/whalewall whalewall-src
cd whalewall-src
# reproduce binary
go install github.com/capnspacehook/gorepro@latest
gorepro -b="-ldflags=-s -w -X main.version " ../whalewall
# reproduce by manually running command from gorepro
BUILD_CMD="$(gorepro -d -b='-ldflags=-s -w -X main.version ' ../whalewall)"
echo "$BUILD_CMD"
"$BUILD_CMD"
sha256sum whalewall whalewall.repro暂无开放 Issues,或尚未同步最近议题。