Docker Bench for Securityを使ってみる 〜とりあえず試す編〜

これは何?

CIS Docker BenchmarksというDockerのセキュリティ基準に沿って、自動診断してくれるツール

診断項目はこんな感じ

  • Host Configuration
  • Docker daemon configuration
  • Docker daemon configuration files
  • Container Images and Build File
  • Container Runtime
  • Docker Security Operations
  • Docker Swarm Configuration

公式リポジトリはこちら https://github.com/docker/docker-bench-security

お試し環境の構築

実際使用する際には、本番環境もしくは、本番に近い環境で診断することが望ましいと思われるのですが、今回はVagrant環境を使ってお試し環境を作りました。

https://github.com/takapi86/docker-bench-security-test

実行までの流れ

VMを起動

vagrant up

VMの中に入る

vagrant ssh

dockerを起動

sudo systemctl start docker

docker-bench-securityをpull

docker pull docker/docker-bench-security

診断したいコンテナをあげる

docker run --rm -d nginx

今回は、nginxを例としていますが、診断したいコンテナを起動してください。

診断実行

docker run --rm --net host --pid host --userns host --cap-add audit_control \
    -e DOCKER_CONTENT_TRUST=1 \
    -v /etc:/etc:ro \
    -v /usr/lib/systemd:/usr/lib/systemd:ro \
    -v /var/lib:/var/lib:ro \
    -v /var/run/docker.sock:/var/run/docker.sock:ro \
    --label docker_bench_security \
    docker/docker-bench-security

診断結果を見る

項目が結構あるので、今回は Container Images and Build File を見てみる

[INFO] 4 - Container Images and Build File
[WARN] 4.1  - Ensure a user for the container has been created
[WARN]      * Running as root: serene_yonath
[NOTE] 4.2  - Ensure that containers use trusted base images
[NOTE] 4.3  - Ensure unnecessary packages are not installed in the container
[NOTE] 4.4  - Ensure images are scanned and rebuilt to include security patches
[PASS] 4.5  - Ensure Content trust for Docker is Enabled
[WARN] 4.6  - Ensure HEALTHCHECK instructions have been added to the container image
[WARN]      * No Healthcheck found: [docker.io/nginx:latest]
[PASS] 4.7  - Ensure update instructions are not use alone in the Dockerfile
[NOTE] 4.8  - Ensure setuid and setgid permissions are removed in the images
[INFO] 4.9  - Ensure COPY is used instead of ADD in Dockerfile
[INFO]      * ADD in image history: [docker.io/nginx:latest]
[INFO]      * ADD in image history: [docker.io/docker/docker-bench-security:latest]
[NOTE] 4.10  - Ensure secrets are not stored in Dockerfiles
[NOTE] 4.11  - Ensure verified packages are only Installed

WARNINGとして、コンテナがrootで実行されていることと、HEALTHCHECKがコンテナイメージにないことが指摘されている。

ガイドラインと照らし合わせてチェックしていくと大変なので、便利そう。

参考