From 7909384155a45378f1f149d4299eca51cce9ac9a Mon Sep 17 00:00:00 2001 From: Translator Date: Mon, 17 Nov 2025 12:18:42 +0000 Subject: [PATCH] Translated ['', 'src/pentesting-cloud/kubernetes-security/kubernetes-har --- .../kubernetes-hardening/README.md | 188 +++++++++++------- 1 file changed, 117 insertions(+), 71 deletions(-) diff --git a/src/pentesting-cloud/kubernetes-security/kubernetes-hardening/README.md b/src/pentesting-cloud/kubernetes-security/kubernetes-hardening/README.md index 41f73c855..5e8bba9b7 100644 --- a/src/pentesting-cloud/kubernetes-security/kubernetes-hardening/README.md +++ b/src/pentesting-cloud/kubernetes-security/kubernetes-hardening/README.md @@ -2,11 +2,11 @@ {{#include ../../../banners/hacktricks-training.md}} -## Tools to analyse a cluster +## 分析集群的工具 -### [**Steampipe - Kubernetes Compliance](https://github.com/turbot/steampipe-mod-kubernetes-compliance) +### [Steampipe - Kubernetes Compliance](https://github.com/turbot/steampipe-mod-kubernetes-compliance) -它将对Kubernetes集群进行**多个合规性检查**。它包括对CIS、国家安全局(NSA)和网络安全与基础设施安全局(CISA)Kubernetes加固的网络安全技术报告的支持。 +它会对 **Kubernetes 集群进行多项合规性检查**。它支持 CIS、National Security Agency (NSA) 和 Cybersecurity and Infrastructure Security Agency (CISA) 关于 Kubernetes 加固的网络安全技术报告。 ```bash # Install Steampipe brew install turbot/tap/powerpipe @@ -27,130 +27,176 @@ powerpipe server ``` ### [**Kubescape**](https://github.com/armosec/kubescape) -[**Kubescape**](https://github.com/armosec/kubescape) 是一个 K8s 开源工具,提供多云 K8s 单一视图,包括风险分析、安全合规、RBAC 可视化和镜像漏洞扫描。Kubescape 扫描 K8s 集群、YAML 文件和 HELM 图表,根据多个框架(如 [NSA-CISA](https://www.armosec.io/blog/kubernetes-hardening-guidance-summary-by-armo) 、[MITRE ATT\&CK®](https://www.microsoft.com/security/blog/2021/03/23/secure-containerized-environments-with-updated-threat-matrix-for-kubernetes/))检测错误配置、软件漏洞和 RBAC(基于角色的访问控制)违规,能够在 CI/CD 流水线的早期阶段即时计算风险分数并显示风险趋势。 +[**Kubescape**](https://github.com/armosec/kubescape) 是一款面向 K8s 的开源工具,提供跨多云的 K8s 单一管理面板,包含风险分析、安全合规、RBAC 可视化以及镜像漏洞扫描。Kubescape 会扫描 K8s 集群、YAML 文件和 HELM charts,依据多个框架(例如 the [NSA-CISA](https://www.armosec.io/blog/kubernetes-hardening-guidance-summary-by-armo) , [MITRE ATT\&CK®](https://www.microsoft.com/security/blog/2021/03/23/secure-containerized-environments-with-updated-threat-matrix-for-kubernetes/))检测配置错误、软件漏洞和 RBAC (role-based-access-control) 违规,能在 CI/CD pipeline 的早期阶段发现问题,实时计算风险评分并展示随时间变化的风险趋势。 ```bash curl -s https://raw.githubusercontent.com/kubescape/kubescape/master/install.sh | /bin/bash kubescape scan --verbose ``` ### [**Popeye**](https://github.com/derailed/popeye) -[**Popeye**](https://github.com/derailed/popeye) 是一个实用工具,用于扫描实时的 Kubernetes 集群并 **报告已部署资源和配置的潜在问题**。它根据已部署的内容而不是磁盘上的内容来清理您的集群。通过扫描您的集群,它可以检测配置错误,并帮助您确保最佳实践到位,从而防止未来的麻烦。它旨在减少在实际操作 Kubernetes 集群时面临的认知负担。此外,如果您的集群使用了 metric-server,它会报告潜在的资源过/不足分配,并在您的集群容量不足时尝试警告您。 +[**Popeye**](https://github.com/derailed/popeye) 是一个实用工具,用于扫描实时 Kubernetes 集群并 **报告已部署资源和配置的潜在问题**。它基于已部署的内容而不是磁盘上的内容来清理你的集群。通过扫描你的集群,它可以检测到错误配置,帮助你确保已采用最佳实践,从而避免将来的麻烦。它旨在减少在实际运行 Kubernetes 集群时操作人员面临的认知过载。进一步来说,如果你的集群使用了 metric-server,它会报告潜在的资源过/少 分配,并在集群可能耗尽容量时尝试发出警告。 ### [**Kube-bench**](https://github.com/aquasecurity/kube-bench) -工具 [**kube-bench**](https://github.com/aquasecurity/kube-bench) 是一个通过运行 [**CIS Kubernetes Benchmark**](https://www.cisecurity.org/benchmark/kubernetes/) 中记录的检查来检查 Kubernetes 是否安全部署的工具。\ -您可以选择: +The tool [**kube-bench**](https://github.com/aquasecurity/kube-bench) is a tool that checks whether Kubernetes is deployed securely by running the checks documented in the [**CIS Kubernetes Benchmark**].\ +You can choose to: -- 从容器内部运行 kube-bench(与主机共享 PID 命名空间) -- 运行一个在主机上安装 kube-bench 的容器,然后直接在主机上运行 kube-bench -- 从 [Releases page](https://github.com/aquasecurity/kube-bench/releases) 安装最新的二进制文件, -- 从源代码编译。 +- 在 container 内运行 kube-bench(与 host 共享 PID namespace) +- 运行一个将 kube-bench 安装到 host 上的 container,然后直接在 host 上运行 kube-bench +- 从 [Releases page](https://github.com/aquasecurity/kube-bench/releases) 安装最新的 binaries, +- 从 source 编译它。 ### [**Kubeaudit**](https://github.com/Shopify/kubeaudit) -**[已弃用]** 工具 [**kubeaudit**](https://github.com/Shopify/kubeaudit) 是一个命令行工具和 Go 包,用于 **审计 Kubernetes 集群** 的各种安全问题。 +**[DEPRECATED]** The tool [**kubeaudit**](https://github.com/Shopify/kubeaudit) is a command line tool and a Go package to **审计 Kubernetes 集群** for various different security concerns. -Kubeaudit 可以检测它是否在集群中的容器内运行。如果是,它将尝试审计该集群中的所有 Kubernetes 资源: +Kubeaudit can detect if it is running within a container in a cluster. If so, it will try to audit all Kubernetes resources in that cluster: ``` kubeaudit all ``` -该工具还具有参数 `autofix` 以 **自动修复检测到的问题。** +该工具还带有参数 `autofix`,用于**自动修复检测到的问题。** ### [**Kube-hunter**](https://github.com/aquasecurity/kube-hunter) -**[已弃用]** 工具 [**kube-hunter**](https://github.com/aquasecurity/kube-hunter) 用于寻找 Kubernetes 集群中的安全弱点。该工具的开发旨在提高对 Kubernetes 环境中安全问题的意识和可见性。 +**[DEPRECATED]** 该工具 [**kube-hunter**](https://github.com/aquasecurity/kube-hunter) 用于在 Kubernetes 集群中发现安全弱点。该工具的开发目的是提高对 Kubernetes 环境中安全问题的认识和可见性。 ```bash kube-hunter --remote some.node.com ``` ### [Trivy](https://github.com/aquasecurity/trivy) -[Trivy](https://github.com/aquasecurity/trivy) 有扫描器可以查找安全问题,以及可以找到这些问题的目标: +[Trivy](https://github.com/aquasecurity/trivy) 具有用于检测安全问题的扫描器,以及它可以检查的目标: -- 容器镜像 -- 文件系统 -- Git 仓库(远程) -- 虚拟机镜像 +- Container Image +- Filesystem +- Git Repository (remote) +- Virtual Machine Image - Kubernetes ### [**Kubei**](https://github.com/Erezf-p/kubei) -**[看起来没有维护]** +**[似乎已不再维护]** -[**Kubei**](https://github.com/Erezf-p/kubei) 是一个漏洞扫描和 CIS Docker 基准工具,允许用户对其 Kubernetes 集群进行准确和即时的风险评估。Kubei 扫描 Kubernetes 集群中使用的所有镜像,包括应用程序 Pod 和系统 Pod 的镜像。 +[**Kubei**](https://github.com/Erezf-p/kubei) 是一个漏洞扫描和 CIS Docker 基准检测工具,能够让用户对其 Kubernetes 集群进行准确且即时的风险评估。Kubei 会扫描 Kubernetes 集群中使用的所有镜像,包括应用 pods 和系统 pods 的镜像。 ### [**KubiScan**](https://github.com/cyberark/KubiScan) -[**KubiScan**](https://github.com/cyberark/KubiScan) 是一个用于扫描 Kubernetes 集群中 Kubernetes 基于角色的访问控制(RBAC)授权模型中风险权限的工具。 +[**KubiScan**](https://github.com/cyberark/KubiScan) 是一个用于扫描 Kubernetes 集群中基于 Role-based access control (RBAC) 授权模型下高风险权限的工具。 ### [Managed Kubernetes Auditing Toolkit](https://github.com/DataDog/managed-kubernetes-auditing-toolkit) -[**Mkat**](https://github.com/DataDog/managed-kubernetes-auditing-toolkit) 是一个构建用于测试其他类型高风险检查的工具,与其他工具相比。它主要有 3 种不同的模式: +[**Mkat**](https://github.com/DataDog/managed-kubernetes-auditing-toolkit) 是一个用于执行与其他工具不同类型高风险检查的工具。它主要有 3 种模式: -- **`find-role-relationships`**: 将查找哪些 AWS 角色在运行哪些 Pods -- **`find-secrets`**: 尝试识别 K8s 资源中的秘密,例如 Pods、ConfigMaps 和 Secrets。 -- **`test-imds-access`**: 尝试运行 Pods 并尝试访问元数据 v1 和 v2。警告:这将在集群中运行一个 Pod,请非常小心,因为您可能不想这样做! +- **`find-role-relationships`**: 用于查找哪些 AWS roles 在哪些 pods 中运行 +- **`find-secrets`**: 尝试识别 Pods、ConfigMaps 和 Secrets 等 K8s 资源中的密钥 +- **`test-imds-access`**: 会尝试运行 pods 并尝试访问 metadata v1 和 v2。警告:这将会在集群中运行一个 pod,请非常小心,可能你并不希望这样做! ## **审计 IaC 代码** ### [**KICS**](https://github.com/Checkmarx/kics) -[**KICS**](https://github.com/Checkmarx/kics) 在以下 **基础设施即代码解决方案** 中发现 **安全漏洞**、合规性问题和基础设施错误配置:Terraform、Kubernetes、Docker、AWS CloudFormation、Ansible、Helm、Microsoft ARM 和 OpenAPI 3.0 规范 +[**KICS**](https://github.com/Checkmarx/kics) 在以下 Infrastructure as Code 解决方案中发现安全漏洞、合规性问题和基础设施配置错误:Terraform、Kubernetes、Docker、AWS CloudFormation、Ansible、Helm、Microsoft ARM 以及 OpenAPI 3.0 规范 ### [**Checkov**](https://github.com/bridgecrewio/checkov) -[**Checkov**](https://github.com/bridgecrewio/checkov) 是一个用于基础设施即代码的静态代码分析工具。 +[**Checkov**](https://github.com/bridgecrewio/checkov) 是一个针对 infrastructure-as-code 的静态代码分析工具。 -它扫描使用 [Terraform](https://terraform.io) 提供的云基础设施、Terraform 计划、[Cloudformation](https://aws.amazon.com/cloudformation/)、[AWS SAM](https://aws.amazon.com/serverless/sam/)、[Kubernetes](https://kubernetes.io)、[Dockerfile](https://www.docker.com)、[Serverless](https://www.serverless.com) 或 [ARM 模板](https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/overview),并使用基于图形的扫描检测安全和合规性错误配置。 +它扫描使用 [Terraform](https://terraform.io)、Terraform plan、[Cloudformation](https://aws.amazon.com/cloudformation/)、[AWS SAM](https://aws.amazon.com/serverless/sam/)、[Kubernetes](https://kubernetes.io)、[Dockerfile](https://www.docker.com)、[Serverless](https://www.serverless.com) 或 [ARM Templates](https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/overview) 所部署的云基础设施,并使用基于图的扫描检测安全和合规性配置错误。 ### [**Kube-score**](https://github.com/zegl/kube-score) -[**kube-score**](https://github.com/zegl/kube-score) 是一个对您的 Kubernetes 对象定义进行静态代码分析的工具。 +[**kube-score**](https://github.com/zegl/kube-score) 是一个对你的 Kubernetes 对象定义执行静态代码分析的工具。 -要安装: +To install: -| 发行版 | 命令 / 链接 | -| ------------------------------------------------- | ----------------------------------------------------------------------------------- | -| macOS、Linux 和 Windows 的预构建二进制文件 | [GitHub releases](https://github.com/zegl/kube-score/releases) | -| Docker | `docker pull zegl/kube-score` ([Docker Hub](https://hub.docker.com/r/zegl/kube-score/)) | -| Homebrew(macOS 和 Linux) | `brew install kube-score` | -| [Krew](https://krew.sigs.k8s.io/)(macOS 和 Linux) | `kubectl krew install score` | +| 平台 | 命令 / 链接 | +| -------------------------------------------------- | ---------------------------------------------------------------------------------------- | +| 适用于 macOS、Linux 和 Windows 的预构建二进制文件 | [GitHub releases](https://github.com/zegl/kube-score/releases) | +| Docker | `docker pull zegl/kube-score` ([Docker Hub)](https://hub.docker.com/r/zegl/kube-score/) | +| Homebrew (macOS and Linux) | `brew install kube-score` | +| [Krew](https://krew.sigs.k8s.io/) (macOS and Linux) | `kubectl krew install score` | -## 提示 +## Tools to analyze YAML files & Helm Charts -### Kubernetes PodSecurityContext 和 SecurityContext +### [**Kube-linter**](https://github.com/stackrox/kube-linter) +```bash +# Install Kube-linter +brew install kube-linter -您可以配置 **Pods 的安全上下文**(使用 _PodSecurityContext_)和将要运行的 **容器** 的安全上下文(使用 _SecurityContext_)。有关更多信息,请阅读: +# Run Kube-linter +## lint ./path/to/yaml/or/chart +``` +### [Checkov](https://github.com/bridgecrewio/checkov) +```bash +# Install Checkov +pip install checkov + +# Run Checkov +checkov -d ./path/to/yaml/or/chart +``` +### [kube‑score](https://github.com/zegl/kube-score) +```bash +# Install kube-score +brew install kube-score + +# Run kube-score +kube-score score ./path/to/yaml +# or +helm template chart /path/to/chart | kube-score score - +# or if the chart needs some values +helm template chart /path/to/chart \ +--set 'config.urls[0]=https://dummy.backend.internal' \ +| kube-score score - +``` +### [Kubesec](https://github.com/controlplaneio/kubesec) +```bash +# Install Kubesec +## Download from https://github.com/controlplaneio/kubesec/releases + +# Run Kubesec in a yaml +kubesec scan ./path/to/yaml +# or +helm template chart /path/to/chart | kubesec scan - +# or if the chart needs some values +helm template chart /path/to/chart \ +--set 'config.urls[0]=https://dummy.backend.internal' \ +| kubesec scan - +``` +## Tips + +### Kubernetes PodSecurityContext and SecurityContext + +你可以配置 **Pods 的安全上下文**(使用 _PodSecurityContext_)以及将要运行的 **容器** 的安全上下文(使用 _SecurityContext_)。更多信息请阅读: {{#ref}} kubernetes-securitycontext-s.md {{#endref}} -### Kubernetes API 加固 +### Kubernetes API Hardening -保护 **Kubernetes Api Server 的访问** 非常重要,因为具有足够权限的恶意行为者可能会滥用它并以多种方式破坏环境。\ -确保 **访问**(**白名单** 允许访问 API Server 的来源并拒绝任何其他连接)和 [**身份验证**](https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet-authentication-authorization/)(遵循 **最小权限** 原则)都很重要。绝对 **永远** **不允许** **匿名** **请求**。 +保护对 Kubernetes Api Server 的访问非常重要,因为拥有足够权限的恶意行为者可能滥用它并以多种方式破坏环境。\ +需要同时保护对 API Server 的 **访问**(**白名单** 来源以访问 API Server 并拒绝任何其他连接)和 [**authentication**](https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet-authentication-authorization/)(遵循 **最小** **权限** 原则)。并且绝对**不要****允许****匿名** **请求**。 -**常见请求流程:**\ -用户或 K8s ServiceAccount –> 身份验证 –> 授权 –> 录取控制。 +**Common Request process:**\ +User or K8s ServiceAccount –> Authentication –> Authorization –> Admission Control. -**提示**: +**Tips**: - 关闭端口。 - 避免匿名访问。 -- NodeRestriction;不允许特定节点访问 API。 +- NodeRestriction;阻止特定节点访问 API。 - [https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#noderestriction](https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#noderestriction) -- 基本上防止 kubelets 添加/删除/更新带有 node-restriction.kubernetes.io/ 前缀的标签。此标签前缀保留给管理员为工作负载隔离目的标记其节点对象,kubelets 将不被允许修改带有该前缀的标签。 -- 还允许 kubelets 添加/删除/更新这些标签和标签前缀。 -- 确保通过标签实现安全的工作负载隔离。 -- 避免特定 Pods 访问 API。 -- 避免 ApiServer 暴露在互联网上。 -- 避免未经授权的访问 RBAC。 -- ApiServer 端口使用防火墙和 IP 白名单。 +- 基本上阻止 kubelets 添加/删除/更新带有 node-restriction.kubernetes.io/ 前缀的标签。该标签前缀保留给管理员用于对 Node objects 进行标签以实现工作负载隔离,kubelets 将不被允许修改具有该前缀的标签。 +- 同时,也允许 kubelets 添加/删除/更新这些标签和标签前缀。 +- 使用标签确保安全的工作负载隔离。 +- 阻止特定 pods 访问 API。 +- 避免将 ApiServer 暴露到互联网。 +- 避免未授权访问,配置 RBAC。 +- 通过防火墙和 IP 白名单保护 ApiServer 端口。 -### SecurityContext 加固 +### SecurityContext Hardening -默认情况下,如果未指定其他用户,则在启动 Pod 时将使用 root 用户。您可以使用类似于以下模板的方式在更安全的上下文中运行您的应用程序: +如果未指定其他用户,默认情况下启动 Pod 时会使用 root 用户。你可以使用类似下面的模板在更安全的上下文中运行你的应用: ```yaml apiVersion: v1 kind: Pod @@ -179,29 +225,29 @@ allowPrivilegeEscalation: true - [https://kubernetes.io/docs/tasks/configure-pod-container/security-context/](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/) - [https://kubernetes.io/docs/concepts/policy/pod-security-policy/](https://kubernetes.io/docs/concepts/policy/pod-security-policy/) -### 一般加固 +### 常规加固 -您应该根据需要频繁更新您的Kubernetes环境,以确保: +你应该根据需要频繁更新你的 Kubernetes 环境,以确保: - 依赖项保持最新。 -- 修复漏洞和安全补丁。 +- 应用 bug 修复和安全补丁。 -[**发布周期**](https://kubernetes.io/docs/setup/release/version-skew-policy/): 每3个月会有一个新的次要版本 -- 1.20.3 = 1(主要).20(次要).3(补丁) +[**Release cycles**](https://kubernetes.io/docs/setup/release/version-skew-policy/): 每隔 3 个月会有一个新的次要版本 —— 例如 1.20.3 = 1(Major).20(Minor).3(patch) -**更新Kubernetes集群的最佳方法是(从** [**这里**](https://kubernetes.io/docs/tasks/administer-cluster/cluster-upgrade/)**):** +**更新 Kubernetes 集群 的最佳方式是(来自** [**here**](https://kubernetes.io/docs/tasks/administer-cluster/cluster-upgrade/)**):** - 按照以下顺序升级主节点组件: -- etcd(所有实例)。 -- kube-apiserver(所有控制平面主机)。 -- kube-controller-manager。 -- kube-scheduler。 -- 如果使用云控制器管理器,则升级云控制器管理器。 -- 升级工作节点组件,如kube-proxy、kubelet。 +- etcd (all instances). +- kube-apiserver (all control plane hosts). +- kube-controller-manager. +- kube-scheduler. +- cloud controller manager, if you use one. +- 升级工作节点组件,例如 kube-proxy、kubelet。 -## Kubernetes监控与安全: +## Kubernetes 监控与安全: -- Kyverno策略引擎 -- Cilium Tetragon - 基于eBPF的安全可观察性和运行时强制 +- Kyverno Policy Engine +- Cilium Tetragon - 基于 eBPF 的安全可观测性与运行时强制 - 网络安全策略 - Falco - 运行时安全监控与检测