kelleyblackmore/container-hardening-guide

GitHub: kelleyblackmore/container-hardening-guide

基于 DISA 指南的容器镜像与 Kubernetes 部署安全加固参考实现,提供完整的 STIG 合规扫描工作流和可验证的 CI 流水线。

Stars: 0 | Forks: 0

# 容器加固指南 **容器镜像实际上是由什么构成的,以及如何加固它 —— 逐 层、逐项要求地遵循 DISA *容器镜像创建 与部署指南*,V2R0.6。** 这不是一份核对清单。这是一份实用且带注释的参考资料:一个你可以 构建的加固容器,一个你可以扫描的反模式容器,一个包含真正定制文件的完整 OpenSCAP STIG 工作流,加固的 Kubernetes manifests,以及一条用事实 证明每一项声明而非空口断言的 CI 流水线。 ``` git clone https://github.com/kelleyblackmore/container-hardening-guide cd container-hardening-guide make install-tools # hadolint, trivy, grype, syft, trufflehog make build # build the hardened image make inspect # assert each DISA section 2 requirement make stig # OpenSCAP STIG scan: baseline vs tailored ``` ## 从这里开始 | 如果你想… | 请阅读 | |---|---| | 了解镜像*是什么* —— 层、manifest、config,以及为什么删除并没有真正删除 | [1. 容器镜像剖析](docs/01-anatomy-of-a-container-image.md) | | 知道每一层该做什么 | [2. 逐层加固](docs/02-layer-by-layer-hardening.md) | | 查找某一项要求 | [3. 镜像创建 §2](docs/03-image-creation-requirements.md) · [4. 部署 §3](docs/04-deployment-requirements.md) | | 端到端地对容器进行 STIG | [10. 演练](docs/10-stig-a-container-walkthrough.md) | | 使用 OpenSCAP 并编写定制文件 | [5. OpenSCAP 与定制](docs/05-openscap-and-tailoring.md) | | 使用扫描器 | [6. hadolint](docs/06-hadolint.md) · [7. TruffleHog](docs/07-trufflehog.md) · [8. Trivy & Grype](docs/08-trivy-and-grype.md) | | 构建流水线 | [9. DevSecOps 流水线](docs/09-devsecops-pipeline.md) | | 处理你无法修复的问题 | [12. 豁免与 POA&Ms](docs/12-waivers-and-poam.md) | | 一次性查看整个映射关系 | [合规性矩阵](docs/compliance-matrix.md) | ## 这里面有什么 ``` examples/hardened/ the reference image — every line annotated with the Dockerfile requirement it satisfies, in 8 numbered layer steps harden/ STIG remediation scripts, each naming its SSG rules app/ a small Go service designed to BE hardenable README.md line-by-line reading guide examples/insecure/ the same idea built wrong — every line tagged with Dockerfile the requirement it VIOLATES. Lint it, scan it. oscap/ the OpenSCAP workflow run-oscap.sh baseline scan + tailored scan + diff Containerfile.scan throwaway scanner layer (image + oscap + SCAP content) generate-tailoring.sh builds the answer file, with justifications inline not-applicable.rules every deselected rule, with a written reason tailoring/ a hand-written, fully annotated tailoring file k8s/ deployment hardening — §3.1 through §3.11 deployment.yaml PSA restricted, seccomp, caps dropped, read-only root, limits/requests, probes, labels networkpolicy.yaml default-deny, then exactly what the service needs scripts/ scan-vulns.sh, scan-secrets.sh, check-k8s-policy.py .github/workflows/ ci.yml (lint→build→scan→assert), stig.yml (OpenSCAP) docs/ the twelve chapters ``` ## 它的得分 在参考镜像上针对 DISA RHEL 9 STIG profile 运行 `make stig`: ``` BASELINE TAILORED pass 69 69 fail 2 0 notapplicable 412 412 automatic (CPE logic in the content) notchecked 1 1 no automated check - review by hand notselected 1048 1050 +2 = the answer file score 97% 100% ``` answer file 取消选择的那两条规则,恰好就是失败的那两条,而且 两者都是结构性失败:`configure_crypto_policy` 需要内核处于 FIPS 模式,而 `network_configure_name_resolution` 会写入一个在 启动时被 kubelet 覆盖的文件。取消选择任何规则都不是为了粉饰分数。 这个差值才是值得关注的地方。一个如果基线有 40 条规则未通过,而其 answer file 也正好 取消选择了 40 条规则,这样也会得到 100% 的分数,但这毫无意义。 ## 理念 DISA 指南有 20 页的要求,但没有提供实现。这个 仓库就是具体的实现,并且保留了与代码相关的逻辑推理。 它试图在三方面做得与众不同: **1. 每一项声明都经过检验,而非空口断言。** `make inspect` 会读取构建好的 镜像,并证明第 2 节的每一项要求 —— 非 root 用户、没有 `sshd`、没有 setuid 二进制文件、非特权端口、健康检查、层历史记录中没有任何 凭证形态的内容。同样的断言会作为阻塞性 CI job 运行。没有 断言的加固步骤只是一条注释。 **2. 权衡取舍都被记录在案。** 版本固定 RPM 包(可复现)还是 不固定(不会过时)?完整的 UBI(可扫描)还是 distroless(体积更小)?保留 包管理器(SBOM 可用)还是剥离它(减少攻击者的可用工具)?每一个 问题在两个方向上都有合理的答案。这个仓库选择其中之一,明确说明 是哪一个,并解释它的代价 —— 就在做决定的具体文件中。 **3. 工具之间的盲区是核心主题,而不是事后才想起来的补充。** hadolint 看不到 CVE。Trivy 看不到 STIG 发现。OpenSCAP 看不到机密信息。 它们都看不到你是如何部署它的。[工具覆盖 范围表](docs/compliance-matrix.md#tool-coverage) 讲述了每个工具的盲区 所在。 ## 工具及其用途 | 工具 | 它回答的问题 | 文档 | |---|---|---| | **hadolint** | 构建文件格式是否规范? | [docs/06](docs/06-hadolint.md) | | **TruffleHog** | 历史、目录树或层中是否存在有效的凭证? | [docs/07](docs/07-trufflehog.md) | | **Trivy** + **Grype** | 已知 CVE —— **两者都**运行,并对它们进行对比 | [docs/08](docs/08-trivy-and-grype.md) | | **Syft** | 其他所有工具都应依赖的 SBOM | [docs/08](docs/08-trivy-and-grype.md) | | **OpenSCAP** | 镜像中的操作系统是否符合 STIG? | [docs/05](docs/05-openscap-and-tailoring.md) | | **kube-linter** + `check-k8s-policy.py` | manifests 是否符合第 3 节? | [docs/04](docs/04-deployment-requirements.md) | 运行两个 CVE 扫描器并不是多此一举,而是指南自身的 指示(§4.3.3:*"必须使用多种工具进行扫描,因为 结果可能会有所不同"*)。`scripts/scan-vulns.sh` 会对它们进行 diff,并打印出 只有其中一个工具发现的内容 —— 这才是有趣发现 所在的地方。 ## 命令 ``` make build # build the hardened image make rebuild # --no-cache --pull (§2.8, §2.15 — run this on a schedule) make lint # hadolint the hardened Dockerfile (must pass) make lint-insecure # hadolint the anti-pattern (findings expected) make inspect # assert every section 2 requirement against the image make verify # run it read-only, cap-drop=ALL, non-root make scan-secrets # TruffleHog: git history + working tree + image layers make scan-vulns # Trivy + Grype + Syft SBOM, with the scanner diff make stig # OpenSCAP: baseline scan, tailored scan, HTML + ARF make scan-all # all of the above make digests # print base image digests to pin (§2.14) ``` ## 要求覆盖范围 所有 18 项镜像创建要求 (§2) 和所有 11 项部署要求 (§3) 都已实现并映射在[合规性矩阵](docs/compliance-matrix.md)中, 包含 IA 控制、CCI、实现它的文件、验证它的命令, 以及对其进行拦截的 CI job。 第 4 节 (DevSecOps) 已作为两个 workflow 实现,但发布和 发布除外 —— 这个仓库不发布任何内容,因此它们通过你会添加的 cosign/attestation 步骤进行了说明,而不是作为无用的 YAML 摆设。 ## 注意事项 —— 请阅读这些 **这是一份参考资料,而不是一项资质认证。** 它展示了如何实现和 验证指南的要求。它不会使你的镜像自动变得合规,而且它 不能替代你所在组织的流程或你的 AO (授权官员) 的判断。 **要求的优先级高于此仓库。** 如果此处的任何内容与 DISA 指南、STIG、SRG 或你的本地策略相冲突,以那些 要求为准。该指南明确指出它“不能替代 STIG 或 SRG”。 **操作系统 STIG 并不是全部工作。** 将 RHEL 9 STIG 应用于基础镜像 并不能免除其中应用程序的 SRG。Web 服务器仍然需要 遵守 Web Server SRG;数据库仍然需要遵守 Database SRG。 **镜像仓库。** 示例从 `registry.access.redhat.com` 构建,以便没有 Platform One 账户的读者也能运行它们。在 DoD 构建中,`FROM` 行和 `.hadolint.yaml` 中的 `allowed-registries` 列表会变成一个条目: `registry1.dso.mil`。参见 [docs/11](docs/11-approved-sources.md)。 **`examples/insecure/` 是故意设计成有漏洞的。** 它安装了 SSH 服务器 和端口扫描器,并包含伪造的凭证字符串。它存在的目的就是被 lint 和 扫描。`make build-insecure` 会让你等待 5 秒钟并思考 一下。请勿部署它。 ## 来源 DISA,*容器镜像创建与部署指南*,版本 2,发布 0.6, 2020 年 11 月 02 日。分发声明 A —— 批准公开发布。 相关内容:[NIST SP 800-190](https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-190.pdf) · [NIST SP 800-52r2](https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-52r2.pdf) · [DISA STIGs](https://public.cyber.mil/stigs/) · [SCAP Security Guide](https://github.com/ComplianceAsCode/content) · [Iron Bank](https://ironbank.dso.mil) ## 许可证 MIT —— 参见 [LICENSE](LICENSE)。它所遵循的 DISA 指南属于公共领域, 分发声明 A。
标签:DevSecOps, LLM防护, Web截图, 上游代理, 子域名突变, 容器安全, 日志审计, 系统加固, 请求拦截