SnailSploit/KubeRoast

GitHub: SnailSploit/KubeRoast

Kubernetes环境红队工具,快速扫描配置错误和攻击路径。

Stars: 6 | Forks: 2

Python 3.9+ MIT License Tests Version

KubeRoast

红队 Kubernetes 错误配置与攻击路径扫描器
快速、主观、只读。专为真实的提权路径而构建。

快速开始检测内容用法CI/CD输出贡献

## 为什么选择 KubeRoast 大多数 Kubernetes 安全扫描器会产生大量噪音。KubeRoast 专注于**真正会导致你被黑客攻击的问题** —— 提权路径、暴露的 kubelet、过度授权的 RBAC、向互联网开放的网络服务,以及明文可见的 secrets。它只进行读取,从不写入。可安全地在生产环境中运行。 ## 快速开始 ``` # 安装 git clone https://github.com/SnailSploit/KubeRoast_v1.git cd KubeRoast_v1 pip install -e . # 扫描您的 cluster kuberoast --report text ``` 就是这样。KubeRoast 会自动获取你当前的 kubeconfig context。 ## 检测内容 KubeRoast 跨 7 个类别运行 **30 多项安全检查**。每项发现都包括严重程度、描述、可操作的修复建议和参考链接。 ### Pod 安全(11 项检查) | ID | 发现 | 严重程度 | |---|---|---| | `POD-PRIV` | 特权容器 | Critical | | `POD-ROOT` | 容器以 root 用户身份运行 (`runAsUser=0`) | High | | `POD-PE` | 未禁用 `allowPrivilegeEscalation` | High/Medium | | `POD-HOSTNS` | Pod 使用 host namespaces (network/PID/IPC) | High | | `POD-CAPS` | 危险的 Linux capabilities (`SYS_ADMIN`, `SYS_PTRACE` 等) | High | | `POD-HOSTPATH` | 挂载了 hostPath volume | High | | `POD-RWFS` | 可写的 root filesystem | Medium | | `POD-NO-SECCOMP` | 未配置 seccomp profile | Medium | | `POD-NO-LIMITS` | 没有 CPU/内存资源限制 | Medium | | `POD-SATOKEN` | 未禁用 Service account token automount | Low | | `POD-NO-APPARMOR` | 未配置 AppArmor profile | Low | ### RBAC(5 项检查) | ID | 发现 | 严重程度 | |---|---|---| | `RBAC-ANON` | 绑定了匿名或通配符用户 | Critical | | `RBAC-CLUSTER-ADMIN` | 通过 binding 授予了 `cluster-admin` | Critical | | `RBAC-ESCALATION-VERB` | 包含提权动词 (`bind`/`escalate`/`impersonate`) | Critical | | `RBAC-WILDCARD` | role rules 中存在通配符 `*` | High | | `RBAC-SENSITIVE-WRITE` | 对敏感资源拥有写入权限 | High | ### 攻击路径建模(1 项复合检查) | ID | 发现 | 严重程度 | |---|---|---| | `AP-RBAC-ESC` | RBAC 权限允许提权 | Critical | 将每个主体(尤其是 ServiceAccounts)映射到具体的提权能力 —— bind、escalate、impersonate、create pods + read secrets、exec/attach、modify nodes —— 并将 SA 关联回运行它们的 pods。 ### 网络暴露(5 项检查) | ID | 发现 | 严重程度 | |---|---|---| | `NET-LB-OPEN` | LoadBalancer 缺少 `loadBalancerSourceRanges` | High | | `NET-EXTERNAL-IP` | Service 带有 `externalIPs` | High | | `NET-INGRESS-NO-TLS` | Ingress 缺少 TLS | High | | `NET-NODEPORT` | Service 通过 NodePort 暴露 | Medium | | `NET-INGRESS-WILDCARD` | Ingress 具有通配符 host | Medium | ### 节点安全(2 项检查) | ID | 发现 | 严重程度 | |---|---|---| | `NODE-KUBELET-RO` | Kubelet 只读端口 10255 可达 | Critical | | `NODE-KUBELET-API` | Kubelet API 端口 10250 可达 | Medium | 节点探测并发运行,可在大规模集群中实现快速扫描。 ### Secrets(3 项检查) | ID | 发现 | 严重程度 | |---|---|---| | `SECRET-SENSITIVE` | Opaque secret 包含类似凭据的键 | Medium | | `SECRET-DOCKER-HUB` | secret 中包含 Docker Hub 凭据 | Medium | | `SECRET-TLS-MANUAL` | TLS secret 未由 cert-manager 管理 | Low | ### 策略与 PSS(2 项检查) | ID | 发现 | 严重程度 | |---|---|---| | `POLICY-NONE` | 未检测到策略引擎 (Kyverno/Gatekeeper) | High | | `PSS-NOT-ENFORCED` | Namespace 缺少 Pod Security Admission 标签 | High/Info | 系统 namespace (`kube-system` 等) 将以 `info` 严重程度进行标记,并提供量身定制的修复建议。 ## 用法 ``` kuberoast [OPTIONS] ``` ### Flags | Flag | 默认值 | 描述 | |---|---|---| | `--report {json,text,html}` | `json` | 输出格式 | | `--out FILE` | — | 将报告写入文件(HTML 必需) | | `--kubeconfig PATH` | — | kubeconfig 的路径(默认为 `~/.kube/config`) | | `-n, --namespace NS` | — | 将扫描限制在单个 namespace 内 | | `--min-severity {info,low,medium,high,critical}` | `info` | 过滤掉低于此严重程度的发现 | | `--fail-on {info,low,medium,high,critical}` | — | 如果任何发现达到此阈值,则退出码为 1 | | `--skip-nodes` | `false` | 跳过 kubelet 端口探测 | | `--skip-secrets` | `false` | 跳过 secret 检查 | | `--skip-attack-paths` | `false` | 跳过 RBAC 攻击路径分析 | | `--provider {generic,eks,aks,gke}` | `generic` | 用于修复建议措辞的云提供商提示 | | `-v, --verbose` | `false` | 将进度日志输出到 stderr | ### 示例 **对默认 namespace 进行快速文本扫描:** ``` kuberoast -n default --report text ``` **全集群扫描,仅显示 high 和 critical:** ``` kuberoast --min-severity high --report text ``` **提供给安全团队的 HTML 报告:** ``` kuberoast --report html --out report.html ``` **CI 门禁 —— 发现 critical 级别的问题则中断 pipeline:** ``` kuberoast --fail-on critical --report json > results.json ``` **详细扫描,跳过节点探测(速度更快):** ``` kuberoast -v --skip-nodes --report text ``` ## CI/CD 集成 KubeRoast 旨在用于部署门禁。使用 `--fail-on` 设置阈值: ``` # GitHub Actions 示例 - name: Security scan run: | pip install -e . kuberoast --fail-on high --report json > kuberoast-results.json ``` ### 退出码 | 代码 | 含义 | |---|---| | `0` | 扫描完成,没有发现达到或超过 `--fail-on` 阈值的结果 | | `1` | 发现的结果达到或超过了 `--fail-on` 阈值 | | `2` | 使用错误或运行时失败 | ## 输出格式 ### JSON(默认) 机器可读的发现结果数组。通过管道传递给 `jq` 进行过滤: ``` kuberoast | jq '[.[] | select(.severity == "critical")]' ``` ### 文本 按严重程度分组,每个发现包含摘要行和修复建议: ``` === kuberoast scan: 12 findings (3 critical, 4 high, 5 medium) === --- CRITICAL (3) --- [CRITICAL] Privileged container Resource: pod/prod/web-0::nginx Description: Container runs in privileged mode, granting broad access to the host kernel. Remediation: Remove privileged=true. Grant narrow capabilities only if needed. ``` ### HTML 带有严重程度徽章、可排序表格和修复指南的深色主题报告。可在任何浏览器中打开: ``` kuberoast --report html --out report.html && open report.html ``` ## 发现结果 Schema 每个发现都遵循结构化的格式: ``` { "id": "POD-PRIV", "title": "Privileged container", "description": "Container runs in privileged mode, granting broad access to the host kernel.", "severity": "critical", "category": "Pod Security", "namespace": "prod", "resource": "pod/web-0::nginx", "metadata": {}, "remediation": "Remove privileged=true. Grant narrow capabilities only if needed.", "references": ["https://kubernetes.io/docs/concepts/security/pod-security-standards/"] } ``` **严重程度级别:** `critical` > `high` > `medium` > `low` > `info` **类别:** Pod Security, RBAC, AttackPath, Network, Node, Secrets, Policy ## Kubernetes RBAC KubeRoast 只需要**读取权限**。应用此最小权限的 ClusterRole: ``` apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: kuberoast-reader rules: - apiGroups: [""] resources: [pods, secrets, nodes, namespaces, services] verbs: [get, list, watch] - apiGroups: [rbac.authorization.k8s.io] resources: [roles, rolebindings, clusterroles, clusterrolebindings] verbs: [get, list, watch] - apiGroups: [networking.k8s.io] resources: [ingresses] verbs: [get, list, watch] - apiGroups: [apiextensions.k8s.io] resources: [customresourcedefinitions] verbs: [get, list, watch] --- apiVersion: v1 kind: ServiceAccount metadata: name: kuberoast namespace: default --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: kuberoast-reader-binding roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: kuberoast-reader subjects: - kind: ServiceAccount name: kuberoast namespace: default ``` Secrets 和 nodes 是可选的 —— 如果这些 API 返回 401/403,KubeRoast 会优雅地继续执行。 ## 架构 ``` kuberoast/ cli.py # CLI entry point, arg parsing, orchestration utils/ findings.py # Pydantic Finding model kube.py # K8s API clients, pagination, error handling scanners/ pods.py # 11 pod-level security checks rbac.py # 5 RBAC hygiene checks network.py # Service + Ingress exposure checks nodes.py # Concurrent kubelet port probes secrets.py # Credential heuristics policy.py # Policy engine (Kyverno/Gatekeeper) detection pss.py # Pod Security Standards label checks shared.py # Container iteration helpers attackpaths/ rbac_escalation.py # RBAC privilege escalation graph reporting/ json.py # JSON output text.py # Severity-grouped text output html.py # Dark-themed HTML report tests/ test_pods.py # Pod scanner unit tests test_rbac.py # RBAC scanner unit tests test_network.py # Network scanner unit tests test_secrets.py # Secret scanner unit tests test_pss.py # PSS scanner unit tests test_reporting.py # Output format tests ``` ## 故障排除 | 问题 | 解决方法 | |---|---| | 在某些 API 上出现 `403/401` | KubeRoast 将带着部分结果继续运行。请添加上述 RBAC 权限。 | | 未找到集群 | 检查 `KUBECONFIG` 或运行 `kubectl config get-contexts` | | HTML 需要指定 `--out` | `kuberoast --report html --out report.html` | | 在大型集群上速度缓慢 | 使用 `--skip-secrets`、`--skip-nodes` 或 `-n ` 来缩小范围 | | 节点探测超时 | Kubelet 端口可能被防火墙拦截。请使用 `--skip-nodes` | ## 路线图 - CIS Kubernetes Benchmark 标签 - 特定于提供商的修复建议 (EKS/AKS/GKE) - 离线 manifest 扫描 (`--manifests`) - Gatekeeper/Kyverno 策略清单与偏差 - 为每个发现添加 MITRE ATT&CK 技术标签 - 用于容器化扫描的 Dockerfile ## 📚 文档与作者 关于本项目的完整文章、方法论和相关研究位于: **[https://snailsploit.com/tools](https://snailsploit.com/tools)** 由 **Kai Aizen** —— 独立的进攻性安全研究员创建。 [snailsploit.com](https://snailsploit.com) · [研究](https://snailsploit.com/research) · [框架](https://snailsploit.com/frameworks) · [GitHub](https://github.com/SnailSploit) · [LinkedIn](https://linkedin.com/in/kaiaizen) · [ResearchGate](https://www.researchgate.net/profile/Kai-Aizen-2) · [X/Twitter](https://x.com/SnailSploit)
标签:Kubernetes, Python, StruQ, 云安全, 安全工具, 无后门, 红队工具