rohitrsrohit/day-10-container-security

GitHub: rohitrsrohit/day-10-container-security

一套 Docker 容器安全加固实战工具包,通过对比演示、自动化审计脚本和 Trivy 集成帮助开发者掌握容器安全最佳实践。

Stars: 0 | Forks: 0

# 🔒 Day 10 — 容器安全加固 一套完整的容器安全工具包:包含易受攻击与安全版本的 Dockerfile 逐项对比、包含 10 项检查的自动化安全审计脚本、实时加固演示(只读文件系统、降权 capability、非 root 用户)、严格的 seccomp profile 以及 Trivy CVE 扫描集成。 ## 🎯 本项目涵盖内容 ``` 1. Non-root user — adduser + USER instruction 2. Read-only filesystem — --read-only + --tmpfs /tmp 3. Capability dropping — --cap-drop=ALL + add only what's needed 4. No privilege escalation — --no-new-privileges 5. Hardcoded secrets — detect in ENV vars and Dockerfile 6. Image size — Alpine, multi-stage, layer optimization 7. Health checks — HEALTHCHECK instruction 8. Base image pinning — exact version or @sha256 digest 9. Trivy CVE scanning — HIGH/CRITICAL vulnerability detection 10. seccomp profile — restrict syscall surface ``` ## 📁 项目结构 ``` day-10-container-security/ ├── sample-apps/ │ ├── vulnerable-app/ │ │ └── Dockerfile.vulnerable # ← What NOT to do (annotated) │ └── secure-app/ │ ├── Dockerfile.secure # ← Best practices (annotated) │ ├── .dockerignore # ← Excludes .env, .git, tests │ └── src/ │ ├── index.js │ └── package.json ├── scripts/ │ ├── audit.sh # 10-check security audit │ └── harden_demo.sh # live before/after demos ├── tests/ │ └── test_security.sh # 15 automated tests ├── configs/ │ └── seccomp-profile.json # restrictive syscall allowlist ├── docker-compose.yml # secure service config ├── docs/ │ └── checklist.md # security checklist ├── .gitignore └── README.md ``` ## 🚀 快速开始 ``` git clone https://github.com/YOUR_USERNAME/day-10-container-security.git cd day-10-container-security chmod +x scripts/*.sh tests/*.sh # 运行 pre-flight 测试 bash tests/test_security.sh # 运行 hardening 演示(secure 与 insecure 并排对比) bash scripts/harden_demo.sh alpine:latest # 对任何 image 运行 security audit bash scripts/audit.sh nginx:alpine bash scripts/audit.sh ubuntu:latest bash scripts/audit.sh node:18-alpine # 通过 Dockerfile check 审计您自己的 image bash scripts/audit.sh myimage:latest sample-apps/secure-app/Dockerfile.secure # 构建并审计 secure app docker build -t day10-secure:latest \ -f sample-apps/secure-app/Dockerfile.secure \ sample-apps/secure-app/ bash scripts/audit.sh day10-secure:latest sample-apps/secure-app/Dockerfile.secure ``` ## 🆚 易受攻击与安全的 Dockerfile 对比 | 问题 | 易受攻击 | 安全 | |---|---|---| | 基础镜像 | `ubuntu:latest` | `node:18.19-alpine3.18` | | 基础标签 | `:latest`(未固定版本) | 精确版本 | | 用户 | root | `appuser` (UID 1001) | | 敏感信息 | `ENV DB_PASSWORD=secret` | 运行时注入 | | COPY | `COPY . .`(所有内容) | 仅 `src/` | | CMD 格式 | Shell 格式 | Exec 格式 `["node","..."]` | | HEALTHCHECK | 无 | 已配置 | | .dockerignore | 无 | 排除 .env, .git, tests | | 构建阶段 | 单一阶段 | 多阶段(无 devDeps) | ## 🔒 加固的 `docker run` 命令 ``` docker run \ --read-only \ # read-only root filesystem --tmpfs /tmp:rw,size=64m \ # writable RAM-only /tmp --user 1001:1001 \ # non-root user --cap-drop=ALL \ # drop all Linux capabilities --cap-add=NET_BIND_SERVICE \ # add back only what's needed --no-new-privileges \ # no privilege escalation --security-opt no-new-privileges:true \ --security-opt seccomp=configs/seccomp-profile.json \ --memory=256m \ # max 256MB RAM --cpus=1.0 \ # max 1 CPU core --pids-limit=100 \ # max 100 processes --network my-app-net \ # specific network -e DB_PASSWORD="${DB_PASSWORD}" \ # secret at runtime only -p 8080:3000 \ # unprivileged internal port day10-secure:latest ``` ## 🔍 使用 Trivy 进行 CVE 扫描 ``` # 安装 Trivy brew install trivy # macOS apt install trivy # Debian/Ubuntu # 或通过 Docker(无需安装) docker run aquasec/trivy image nginx:alpine docker run aquasec/trivy image ubuntu:latest # 仅扫描 HIGH + CRITICAL trivy image --severity HIGH,CRITICAL nginx:alpine # 扫描 Dockerfile 以查找 misconfigurations trivy config sample-apps/secure-app/Dockerfile.secure ``` ## 📋 安全检查清单 - [ ] Dockerfile 中配置了非 root 的 USER - [ ] 固定基础镜像版本(不使用 `:latest`) - [ ] 使用 Alpine 或 slim 基础镜像(最小攻击面) - [ ] 多阶段构建(最终镜像中无 devDeps) - [ ] `.dockerignore` 排除了 `.env`, `.git`, `node_modules` - [ ] Dockerfile 中没有 `ENV` 敏感信息 - [ ] 已配置 `HEALTHCHECK` - [ ] CMD 使用 exec 格式(`["node","..."]`) - [ ] 运行时使用 `--read-only` - [ ] 运行时使用 `--cap-drop=ALL` - [ ] 运行时使用 `--no-new-privileges` - [ ] 配置了资源限制(--memory, --cpus) - [ ] Trivy 扫描通过(无 HIGH/CRITICAL) - [ ] 已应用 seccomp profile - [ ] 使用特定的 Docker 网络(非默认 bridge) ## 🤝 30 Days of DevOps 的一部分 | Day | 主题 | |---|---| | Days 6–9 | ✅ Docker 基础 | | Day 10 | ✅ 容器安全加固(本项目) | | Day 11 | GitHub Actions CI Pipeline | ## 📄 License MIT — 可自由使用、修改和分发。
标签:Cutter, Docker, GitHub Advanced Security, Web截图, 安全加固, 安全防御评估, 容器安全, 版权保护, 请求拦截