关于 performing-kubernetes-cis-benchmark-with-kube-bench
这是一份使用 kube-bench 按 CIS Benchmark 检查 Kubernetes 配置的技能指南,适用于审计集群的安全团队。它涵盖不同集群组件,支持定向检查和结构化报告,并提供托管 Kubernetes 的基准配置示例。
主要功能
- 01检查控制平面、etcd、节点和策略
- 02按组件、组或检查项运行
- 03支持 EKS、GKE 和 AKS 基准配置
- 04导出 JSON 和 JUnit 报告
适用场景
- 01依据 CIS 指南审计 Kubernetes 集群
- 02在安全测试中运行指定检查
- 03导出基准结果用于 CI 或合规报告
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill performing-kubernetes-cis-benchmark-with-kube-bench安装后在 Claude Code 中输入/performing-kubernetes-cis-benchmark-with-kube-bench确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENSEassets/template.mdreferences/api-reference.mdstandards.mdworkflows.mdscripts/agent.pyprocess.py详细介绍
使用 kube-bench 执行 Kubernetes CIS Benchmark
概述
kube-bench 是 Aqua Security 推出的开源 Go 工具,可运行 CIS Kubernetes Benchmark 检查。它依据安全最佳实践验证控制平面、etcd、工作节点和策略配置,生成可操作的通过/失败/警告报告。
使用场景
- 当进行涉及使用 kube bench 执行 kubernetes cis benchmark 的安全评估时
- 当遵循相关安全事件的事件响应流程时
- 当执行定期的安全测试或审计活动时
- 当通过实际操作测试验证安全控制措施时
前提条件
- Kubernetes 集群(v1.24+)
- 具有 cluster-admin 访问权限的 kubectl
- 用于直接运行或特权 Pod 访问的节点访问权限
安装
# Binary installation
curl -L https://github.com/aquasecurity/kube-bench/releases/download/v0.7.3/kube-bench_0.7.3_linux_amd64.tar.gz | tar xz
sudo mv kube-bench /usr/local/bin/
# Run as Kubernetes Job
kubectl apply -f https://raw.githubusercontent.com/aquasecurity/kube-bench/main/job.yaml
kubectl logs job/kube-bench
# Run as a pod with host access
kubectl apply -f https://raw.githubusercontent.com/aquasecurity/kube-bench/main/job-master.yaml
kubectl apply -f https://raw.githubusercontent.com/aquasecurity/kube-bench/main/job-node.yaml
运行 Benchmark
完整 Benchmark
# Run all checks (auto-detects node type)
kube-bench run
# Run with JSON output
kube-bench run --json > kube-bench-results.json
# Run with JUnit output for CI
kube-bench run --junit > kube-bench-results.xml
特定组件检查
# Control plane (master) checks
kube-bench run --targets master
# Worker node checks
kube-bench run --targets node
# etcd checks
kube-bench run --targets etcd
# Policies checks
kube-bench run --targets policies
# Control plane + etcd
kube-bench run --targets master,etcd
托管 Kubernetes
# Amazon EKS
kube-bench run --benchmark eks-1.2.0
# Google GKE
kube-bench run --benchmark gke-1.4.0
# Azure AKS
kube-bench run --benchmark aks-1.0
# Red Hat OpenShift
kube-bench run --benchmark rh-1.0
筛选结果
# Show only failures
kube-bench run --targets master | grep "\[FAIL\]"
# Run specific check
kube-bench run --check 1.2.1
# Run check group
kube-bench run --group 1.2
CIS Benchmark 章节
| 章节 | 组件 | 关键检查 |
|---|---|---|
| 1.1 | 控制平面 - API Server | 匿名认证、RBAC、审计日志 |
| 1.2 | 控制平面 - API Server | 准入控制器、加密 |
| 1.3 | 控制平面 - Controller Manager | 服务账号令牌、绑定地址 |
| 1.4 | 控制平面 - Scheduler | 性能分析、绑定地址 |
| 2.1 | etcd | 客户端证书认证、对等加密 |
| 3.1 | 控制平面 - 认证 | OIDC、客户端证书 |
| 4.1 | 工作节点 - kubelet | 匿名认证、授权 |
| 4.2 | 工作节点 - kubelet | TLS、只读端口 |
| 5.1 | 策略 - RBAC | Cluster-admin 使用、服务账号 |
| 5.2 | 策略 - Pod 安全 | 特权、主机命名空间 |
| 5.3 | 策略 - 网络 | 按命名空间的网络策略 |
| 5.7 | 策略 - 通用 | Secret、安全上下文 |
输出示例
[INFO] 1 Control Plane Security Configuration
[INFO] 1.1 Control Plane Node Configuration Files
[PASS] 1.1.1 Ensure that the API server pod specification file permissions are set to 600
[PASS] 1.1.2 Ensure that the API server pod specification file ownership is set to root:root
[FAIL] 1.1.3 Ensure that the controller manager pod specification file permissions are set to 600
[WARN] 1.1.4 Ensure that the scheduler pod specification file permissions are set to 600
== Summary ==
45 checks PASS
12 checks FAIL
8 checks WARN
0 checks INFO
CI/CD 集成
GitHub Actions
name: CIS Benchmark
on:
schedule:
- cron: '0 6 * * 1'
jobs:
kube-bench:
runs-on: ubuntu-latest
steps:
- name: Configure kubectl
uses: azure/setup-kubectl@v3
- name: Run kube-bench
run: |
kubectl apply -f https://raw.githubusercontent.com/aquasecurity/kube-bench/main/job.yaml
kubectl wait --for=condition=complete job/kube-bench --timeout=120s
kubectl logs job/kube-bench > kube-bench-report.txt
- name: Check for failures
run: |
FAILS=$(grep -c "\[FAIL\]" kube-bench-report.txt || true)
echo "Failed checks: $FAILS"
if [ "$FAILS" -gt 0 ]; then
echo "::warning::$FAILS CIS benchmark checks failed"
fi
- name: Upload report
uses: actions/upload-artifact@v4
with:
name: kube-bench-report
path: kube-bench-report.txt
修复示例
1.2.1 - 确保 --anonymous-auth 设置为 false
# /etc/kubernetes/manifests/kube-apiserver.yaml
spec:
containers:
- command:
- kube-apiserver
- --anonymous-auth=false
4.2.1 - 确保 kubelet 上的 --anonymous-auth 设置为 false
# /var/lib/kubelet/config.yaml
authentication:
anonymous:
enabled: false
webhook:
enabled: true
5.2.1 - 尽量减少通配符 RBAC
# Find roles with wildcard permissions
kubectl get clusterroles -o json | jq '.items[] | select(.rules[].resources[] == "*") | .metadata.name'
最佳实践
- 在集群配置前后运行 kube-bench
- 通过 CronJob 安排每周扫描以检测漂移
- 导出 JSON用于 SIEM/合规报告
- 先修复 FAIL 项,再处理 WARN 项
- 使用与你的 Kubernetes 发行版匹配的 benchmark 配置文件
- 跟踪分数随时间的变化以衡量安全态势的改进
- 结合准入控制器以防止漂移
评论
支持 Markdown