Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
94900ee
Change global variable used only locally to local variable for simpli…
razvanstoica89 Mar 9, 2021
8e0daa1
Print date and time in ISO 8601 UTC format
razvanstoica89 Mar 9, 2021
c351120
Add support for logging remediation measures in JSON format
razvanstoica89 Mar 9, 2021
519f20b
Append JSON logs when run multiple times
razvanstoica89 Mar 9, 2021
58205d4
Add new programs to the list of required programs
razvanstoica89 Mar 9, 2021
c00ef43
Add details about remediations measure for host configuration tests
razvanstoica89 Mar 9, 2021
2132b03
Usage instructions aligned between the README.md and docker-bench-sec…
razvanstoica89 Mar 10, 2021
9ae0d92
Fix "nohealthlocal: command not found" error
razvanstoica89 Mar 10, 2021
6c586b4
Print remediation measures at the end of the logs
razvanstoica89 Mar 10, 2021
f769a32
Update benchmark log photo
razvanstoica89 Mar 11, 2021
85117ea
Improve wording
razvanstoica89 Mar 11, 2021
1623c45
Set a relative image link
razvanstoica89 Mar 11, 2021
82bbe1d
Update benchmark log photo
razvanstoica89 Mar 11, 2021
b3a36e8
Print Section B only if it contains remediation measures
razvanstoica89 Mar 11, 2021
c623d3a
Print the remediation measure only if the check is not passed
razvanstoica89 Mar 11, 2021
59a63dd
Print more details in help message
razvanstoica89 Mar 11, 2021
ad62371
Move all pictures to a dedicated folder
razvanstoica89 Mar 11, 2021
ed23f2d
Change default log locations
razvanstoica89 Mar 11, 2021
82ecb7e
README file updated with new default logs location
razvanstoica89 Mar 11, 2021
11886d4
Fixed invalid JSON log
razvanstoica89 Mar 11, 2021
bf11d68
Add new check groups
razvanstoica89 Mar 11, 2021
ee5972c
Add new folders to improve code segmentation
razvanstoica89 Mar 16, 2021
2986d61
Ignore all. Whitelist only essential things.
razvanstoica89 Mar 16, 2021
b757aa7
Connection to the apk repositories is HTTPS by default
razvanstoica89 Mar 16, 2021
ca03a37
Remove duplicate Dockerfile
razvanstoica89 Mar 16, 2021
7144b94
Tests update
razvanstoica89 Mar 16, 2021
9722e5d
Move the help scripts to a dedicated folder
razvanstoica89 Mar 16, 2021
091b4b9
Add option to specify trusted users. Add option to disable the printi…
razvanstoica89 Mar 16, 2021
e5efe2b
Add explanations for newly added features
razvanstoica89 Mar 16, 2021
68bcd14
Increase version
razvanstoica89 Mar 17, 2021
7e89fdd
Add remediation stuff on host configuration
razvanstoica89 Mar 18, 2021
c05c586
Add remediation stuff on daemon configuration
razvanstoica89 Mar 18, 2021
25de0bd
Add remediation stuff on security operations
razvanstoica89 Mar 18, 2021
3a7fe3b
Add remediation stuff on swarm configuration
razvanstoica89 Mar 18, 2021
cc8171f
Add remediation stuff on enterprise configuration
razvanstoica89 Mar 18, 2021
68cb493
Log the remediation measure in JSON file only if the -p flag is not set
razvanstoica89 Mar 19, 2021
c8721c9
Displays the correct current time
razvanstoica89 Mar 19, 2021
f31e60c
Add more remediation stuff
razvanstoica89 Mar 22, 2021
4b68c2e
Rename logs without the .sh extension
razvanstoica89 Mar 27, 2021
8a934ae
Remove the Debian family-specific installation command
razvanstoica89 Mar 28, 2021
86985f8
Overwrite the json log at each run. Beautify the json log.
razvanstoica89 Mar 28, 2021
d0443cc
Bug fixing and improving source code readability
razvanstoica89 Mar 29, 2021
81ac358
Remove temporary files
razvanstoica89 Mar 29, 2021
c67469d
Fix systemctl error when running inside a container
razvanstoica89 Mar 29, 2021
38ac7ef
Fix the printing error when using bash
razvanstoica89 Apr 10, 2021
15aa1ee
Update remediation impact message for test 5.31
razvanstoica89 Apr 14, 2021
e4d9bd1
Set remediationImpact for 5.31 test to None.
razvanstoica89 Apr 14, 2021
0a59baa
Change "Impact" to "Remediation Impact" in bash log
razvanstoica89 Apr 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.git
*.md
*.png
distros

*
!docker-bench-security.sh
!functions/
!tests/
!log/
log/*
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.log*
log/*
*.swp*
14 changes: 4 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@ LABEL \
org.label-schema.url="https://dockerbench.com" \
org.label-schema.vcs-url="https://github.com/docker/docker-bench-security.git"

# Switch to the HTTPS endpoint for the apk repositories
# https://github.com/gliderlabs/docker-alpine/issues/184
RUN set -eux; \
sed -i 's!http://dl-cdn.alpinelinux.org/!https://alpine.global.ssl.fastly.net/!g' /etc/apk/repositories && \
apk add --no-cache \
iproute2 \
docker-cli \
dumb-init
RUN apk add --no-cache iproute2 \
docker-cli \
dumb-init

COPY ./*.sh /usr/local/bin/
COPY ./tests/*.sh /usr/local/bin/tests/
COPY . /usr/local/bin/

HEALTHCHECK CMD exit 0

Expand Down
127 changes: 56 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
# Docker Bench for Security

![Docker Bench for Security running](https://raw.githubusercontent.com/docker/docker-bench-security/master/benchmark_log.png)
![Docker Bench for Security running](img/benchmark_log.png)

The Docker Bench for Security is a script that checks for dozens of common
best-practices around deploying Docker containers in production. The tests are
all automated, and are inspired by the [CIS Docker Benchmark v1.2.0](https://www.cisecurity.org/benchmark/docker/).
The Docker Bench for Security is a script that checks for dozens of common best-practices around deploying Docker containers in production. The tests are all automated, and are inspired by the [CIS Docker Benchmark v1.2.0](https://www.cisecurity.org/benchmark/docker/).

We are making this available as an open-source utility so the Docker community
can have an easy way to self-assess their hosts and docker containers against
this benchmark.
The list with all tests is available [here](tests/TESTS.md).

We are making this available as an open-source utility so the Docker community can have an easy way to self-assess their hosts and docker containers against this benchmark.

## Running Docker Bench for Security

We packaged docker bench as a small container for your convenience. Note that
this container is being run with a *lot* of privilege -- sharing the host's
filesystem, pid and network namespaces, due to portions of the benchmark
applying to the running host.
### Run from your base host

You can simply run this script from your base host by running:

```sh
git clone https://github.com/docker/docker-bench-security.git
cd docker-bench-security
sudo sh docker-bench-security.sh
```

### Run with Docker

We packaged docker bench as a small container for your convenience. Note that this container is being run with a *lot* of privilege -- sharing the host's filesystem, pid and network namespaces, due to portions of the benchmark applying to the running host.

The easiest way to run your hosts against the Docker Bench for Security is by
running our pre-built container:
The easiest way to run your hosts against the Docker Bench for Security is by running our pre-built container:

```sh
docker run --rm --net host --pid host --userns host --cap-add audit_control \
Expand All @@ -36,118 +42,97 @@ docker run --rm --net host --pid host --userns host --cap-add audit_control \
Don't forget to adjust the shared volumes according to your operating system.
Some examples are:

1. `Docker Desktop` on macOS doesn't have `/usr/lib/systemd` or the above Docker
binaries.
1. On Ubuntu the `docker.service` and `docker.secret` files are located in
`/lib/systemd/system` folder by default.

```sh
docker run --rm --net host --pid host --userns host --cap-add audit_control \
-e DOCKER_CONTENT_TRUST=$DOCKER_CONTENT_TRUST \
-v /etc:/etc \
-v /etc:/etc:ro \
-v /lib/systemd/system:/lib/systemd/system:ro \
-v /usr/bin/containerd:/usr/bin/containerd:ro \
-v /usr/bin/runc:/usr/bin/runc: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
```

2. On Ubuntu the `docker.service` and `docker.secret` files are located in
`/lib/systemd/system` folder by default.
2. `Docker Desktop` on macOS doesn't have `/usr/lib/systemd` or the above Docker
binaries.

```sh
docker run --rm --net host --pid host --userns host --cap-add audit_control \
-e DOCKER_CONTENT_TRUST=$DOCKER_CONTENT_TRUST \
-v /etc:/etc:ro \
-v /lib/systemd/system:/lib/systemd/system:ro \
-v /usr/bin/containerd:/usr/bin/containerd:ro \
-v /usr/bin/runc:/usr/bin/runc:ro \
-v /usr/lib/systemd:/usr/lib/systemd:ro \
-v /etc:/etc \
-v /var/lib:/var/lib:ro \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
--label docker_bench_security \
docker/docker-bench-security
```

Docker bench requires Docker 1.13.0 or later in order to run.
### Note

Note that when distributions don't contain `auditctl`, the audit tests will
check `/etc/audit/audit.rules` to see if a rule is present instead.

Distribution specific Dockerfiles that fix these issues are available in the
[distros directory](https://github.com/docker/docker-bench-security/tree/master/distros).
Docker bench requires Docker 1.13.0 or later in order to run.

The [distribution specific Dockerfiles](https://github.com/docker/docker-bench-security/tree/master/distros)
may also help if the distribution you're using hasn't yet shipped Docker
version 1.13.0 or later.
Note that when distributions don't contain `auditctl`, the audit tests will check `/etc/audit/audit.rules` to see if a rule is present instead.

### Docker Bench for Security options

```sh
-b optional Do not print colors
-h optional Print this help message
-l FILE optional Log output in FILE, inside container if run using docker
-c CHECK optional Comma delimited list of specific check(s)
-e CHECK optional Comma delimited list of specific check(s) to exclude
-u USERS optional Comma delimited list of trusted docker user(s)
-c CHECK optional Comma delimited list of specific check(s) id
-e CHECK optional Comma delimited list of specific check(s) id to exclude
-i INCLUDE optional Comma delimited list of patterns within a container or image name to check
-x EXCLUDE optional Comma delimited list of patterns within a container or image name to exclude from check
-n LIMIT optional In JSON output, when reporting lists of items (containers, images, etc.), limit the number of reported items to LIMIT. Default 0 (no limit).
-p PRINT optional Disable the printing of remediation measures. Default: print remediation measures.
```

By default the Docker Bench for Security script will run all available CIS tests
and produce logs in the current directory, named `docker-bench-security.sh.log.json`
and `docker-bench-security.sh.log`.
By default the Docker Bench for Security script will run all available CIS tests and produce
logs in the log folder from current directory, named `docker-bench-security.sh.log.json` and
`docker-bench-security.sh.log`.

If the docker container is used then the log files will be created inside the container. If you wish to access them from the host after the container has been run you will need to mount a volume for storing them in.
If the docker container is used then the log files will be created inside the container in location `/usr/local/bin/log/`. If you wish to access them from the host after the container has been run you will need to mount a volume for storing them in.

The CIS based checks are named `check_<section>_<number>`, e.g. `check_2_6`
and community contributed checks are named `check_c_<number>`.
A complete list of checks is present in [functions_lib.sh](functions_lib.sh).
The CIS based checks are named `check_<section>_<number>`, e.g. `check_2_6` and community contributed checks are named `check_c_<number>`. A complete list of checks is present in [TESTS.md](tests/TESTS.md).

`sh docker-bench-security.sh -l /tmp/docker-bench-security.sh.log -c check_2_2`
will only run check `2.2 Ensure the logging level is set to 'info'`.
`sh docker-bench-security.sh -c check_2_2` will only run check `2.2 Ensure the logging level is set to 'info'`.

`sh docker-bench-security.sh -l /tmp/docker-bench-security.sh.log -e check_2_2`
will run all available checks except `2.2 Ensure the logging level is set to 'info'`.
`sh docker-bench-security.sh -e check_2_2` will run all available checks except `2.2 Ensure the logging level is set to 'info'`.

`sh docker-bench-security.sh -l /tmp/docker-bench-security.sh.log -e docker_enterprise_configuration`
will run all available checks except the docker_enterprise_configuration group
`sh docker-bench-security.sh -e docker_enterprise_configuration` will run all available checks except the docker_enterprise_configuration group

`sh docker-bench-security.sh -l /tmp/docker-bench-security.sh.log -e docker_enterprise_configuration,check_2_2`
will run all available checks except the docker_enterprise_configuration group
and `2.2 Ensure the logging level is set to 'info'`
`sh docker-bench-security.sh -e docker_enterprise_configuration,check_2_2` will run allavailable checks except the docker_enterprise_configuration group and `2.2 Ensure the logging level is set to 'info'`

`sh docker-bench-security.sh -l /tmp/docker-bench-security.sh.log -c container_images -e check_4_5`
will run just the container_images checks except
`4.5 Ensure Content trust for Docker is Enabled`
`sh docker-bench-security.sh -c container_images -e check_4_5` will run just the container_images checks except `4.5 Ensure Content trust for Docker is Enabled`

Note that when submitting checks, provide information why it is a
reasonable test to add and please include some kind of official documentation
verifying that information.
Note that when submitting checks, provide information why it is a reasonable test to add and please include some kind of official documentation verifying that information.

## Building Docker Bench for Security
## Building Docker image

If you wish to build and run this container yourself, you can follow the
following steps:
You have two options if you wish to build and run this container yourself:

1. Use Docker Build:

```sh
git clone https://github.com/docker/docker-bench-security.git
cd docker-bench-security
docker build --no-cache -t docker-bench-security .
```

followed by an appropriate `docker run` command as stated above
or use [Docker Compose](https://docs.docker.com/compose/):
Followed by an appropriate `docker run` command as stated above.

```sh
git clone https://github.com/docker/docker-bench-security.git
cd docker-bench-security
docker-compose run --rm docker-bench-security
```

Also, this script can also be simply run from your base host by running:
2. Use Docker Compose:

```sh
git clone https://github.com/docker/docker-bench-security.git
cd docker-bench-security
sudo sh docker-bench-security.sh
docker-compose run --rm docker-bench-security
```

This script was built to be POSIX 2004 compliant, so it should be portable
across any Unix platform.
This script was built to be POSIX 2004 compliant, so it should be portable across any Unix platform.
Binary file removed benchmark_log.png
Binary file not shown.
16 changes: 5 additions & 11 deletions distros/Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
FROM alpine:3.12
FROM alpine:3.13

LABEL \
org.label-schema.name="docker-bench-security" \
org.label-schema.url="https://dockerbench.com" \
org.label-schema.vcs-url="https://github.com/docker/docker-bench-security.git"

# Switch to the HTTPS endpoint for the apk repositories
# https://github.com/gliderlabs/docker-alpine/issues/184
RUN set -eux; \
sed -i 's!http://dl-cdn.alpinelinux.org/!https://alpine.global.ssl.fastly.net/!g' /etc/apk/repositories && \
apk add --no-cache \
iproute2 \
docker-cli \
dumb-init
RUN apk add --no-cache iproute2 \
docker-cli \
dumb-init

COPY ./*.sh /usr/local/bin/
COPY ./tests/*.sh /usr/local/bin/tests/
COPY . /usr/local/bin/

HEALTHCHECK CMD exit 0

Expand Down
Loading