遵循CIS Docker Benchmarks规范的开源巡检脚本,


 

1CIS是什么?

CIS[1]即Center for Internet Security (CIS) 为安全基准计划提供了定义明确、公正、基于一致性的行业最佳实践来帮助组织评估和增强其安全性

2Docker Bench Security

Docker Bench for Security[2]是一个开源的脚本。它是基于CIS Docker Benchmark v1.3.1[3]规范的,用于自动化巡检在生产环境中运行Docker容器的数十种常见的最佳实践的脚本。

通过调用tests目录的以下脚本进行巡检, 具体的巡检的内容可以参考的脚本的内容。

  1. 1_host_configuration.sh 
  2. 2_docker_daemon_configuration.sh 
  3. 3_docker_daemon_configuration_files.sh 
  4. 4_container_images.sh 
  5. 5_container_runtime.sh 
  6. 6_docker_security_operations.sh 
  7. 7_docker_swarm_configuration.sh 
  8. 8_docker_enterprise_configuration.sh 
  9. 99_community_checks.sh 

在docker-bench-security中可以通过修改functions中的脚本选择检查的项,也可以通过命令行选项排除不需要检查的项,下面看下支持的命令行选项.

3命令选项

docker-bench-security

  1. -b       可选   Do not print colors 不打印颜色 
  2. -h     可选   Print this help message 打印帮助信息 
  3. -l FILE  可选   Log output in FILE, inside container if run using docker 日志输出文件,如果使用docker运行,在容器内部 
  4. -u USERS 可选   Comma delimited list of trusted docker user(s) 以逗号分隔的受信任docker用户列表 
  5. -c CHECK 可选   Comma delimited list of specific check(s) id 以逗号分隔的指定检查id列表 
  6. -e CHECK 可选   Comma delimited list of specific check(s) id to exclude 要排除的以逗号分隔的检查id列表 
  7. -i INCLUDE 可选 Comma delimited list of patterns within a container or image name to check 以逗号分隔的容器或图像名称中的模式列表 
  8. -x EXCLUDE 可选 Comma delimited list of patterns within a container or image name to exclude from check 容器或图像名称中要排除的以逗号分隔的模式列表 
  9. -n LIMIT   可选 In JSON output, when reporting lists of items (containers, images, etc.), limit the number of reported items to LIMIT. Default 0 (no limit). 
  10.  在JSON输出中,当报告项目列表(容器,图像等)时,限制报告项目的数量为LIMIT。默认0(无限制)。 
  11. -p PRINT   可选 Disable the printing of remediation measures. Default: print remediation measures. 禁用打印补救措施。默认值:打印补救措施。 

每个CIS巡检项在脚本中是以check_<section>_<number>格式命名的. 如果要排除某项检查可以使用

  1. sh docker-bench-security.sh -e check_2_2 # 表示检查所有,除了check_2_2(2.2 Ensure the logging level is set to 'info') 

也可以只检查某项

  1. sh docker-bench-security.sh -c check_2_2 

使用很简单。

在使用的时候可以通过--help查看支持的命令行选项,我在使用的时候发现docker镜像中支持的与git仓库中写的不一致,当然你可以自己打一个镜像。

构建镜像

  1. git clone https://github.com/docker/docker-bench-security.git 
  2. cd docker-bench-security 
  3. docker build --no-cache -t docker-bench-security . 

下面是在Mac上执行一次巡检的Demo.

Docker Desktop for Mac

  1. docker run --rm --net host --pid host --userns host --cap-add audit_control \ 
  2.     -e DOCKER_CONTENT_TRUST=$DOCKER_CONTENT_TRUST \ 
  3.     -v /var/lib:/var/lib:ro \ 
  4.     -v /var/run/docker.sock:/var/run/docker.sock:ro \ 
  5.     -v `pwd`:/usr/local/bin/log/ \ 
  6.     --label docker_bench_security \ 
  7.     docker/docker-bench-security -t devops.v1 
  8. # ------------------------------------------------------------------------------ 
  9. # Docker Bench for Security v1.3.4 
  10. # Docker, Inc. (c) 2015- 
  11. # Checks for dozens of common best-practices around deploying Docker containers in production. 
  12. # Inspired by the CIS Docker Community Edition Benchmark v1.1.0. 
  13. # ------------------------------------------------------------------------------ 
  14.  
  15. Initializing Thu Sep  2 04:55:59 UTC 2021 
  16.  
  17. Looking for image devops.v1 
  18.  
  19. [INFO] 1 - Host Configuration 
  20. [WARN] 1.1  - Ensure a separate partition for containers has been created 
  21. [NOTE] 1.2  - Ensure the container host has been Hardened 
  22. ....[省略省略过多内容] 
  23. [PASS] 7.10  - Ensure management plane traffic has been separated from data plane traffic (Swarm mode not enabled) 
  24.  
  25. [INFO] Checks: 105 
  26. [INFO] Score: 7 

参考资料

[1]Center for Internet Security (CIS): https://www.cisecurity.org/

[2]Docker bench security: https://github.com/docker/docker-bench-security

[3]CIS Docker Benchmark: https://www.cisecurity.org/benchmark/docker/

相关内容