mcpField
I
implementing-rbac-hardening-for-kubernetes

implementing-rbac-hardening-for-kubernetes

github.com/mukul975安全35.2k61 次安装8 周前更新
代码仓库

关于 implementing-rbac-hardening-for-kubernetes

面向 Kubernetes 管理员和安全团队的 RBAC 加固指南,涵盖最小权限、绑定审计、服务账户隔离及 OIDC 身份验证配置。

主要功能

  1. 01审计 ClusterRole 绑定
  2. 02优先使用命名空间级角色
  3. 03为工作负载配置专用服务账户
  4. 04限制可导致权限提升的权限
  5. 05配置 OIDC 用户身份验证

适用场景

  1. 01检查并清理多余的 cluster-admin 绑定
  2. 02发现权限过大的服务账户
  3. 03为 Kubernetes 工作负载实施最小权限
  4. 04评估集群 RBAC 安全配置

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mukul975/anthropic-cybersecurity-skills --skill implementing-rbac-hardening-for-kubernetes

安装后在 Claude Code 中输入/implementing-rbac-hardening-for-kubernetes确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

LICENSE
assets/
template.md
references/
api-reference.md
standards.md
workflows.md
scripts/
agent.py
process.py

详细介绍

为 Kubernetes 实施 RBAC 加固

概述

Kubernetes RBAC 根据分配给用户、组和服务账户的角色来规范对集群资源的访问。默认配置通常会授予过度权限,如果不进行主动加固,RBAC 会成为权限提升、横向移动和数据窃取的主要攻击途径。加固需要实施最小权限原则、消除不必要的 ClusterRole 绑定、分离服务账户、集成外部身份提供者以及持续审计。

何时使用

  • 在环境中部署或配置 Kubernetes 的 RBAC 加固能力时
  • 在建立符合合规要求的安全控制措施时
  • 在为此领域构建或改进安全架构时
  • 在进行需要此实施的安全评估时

前置条件

  • Kubernetes 集群 v1.24+,并启用 RBAC(自 v1.6 起为默认)
  • 具有 cluster-admin 权限的 kubectl 访问权限,用于初始审计
  • 用于用户身份验证的外部身份提供者(OIDC)
  • 在 API 服务器上启用审计日志

核心加固原则

1. 消除 cluster-admin 泛滥

审计并移除不必要的 cluster-admin 绑定:

# List all cluster-admin bindings
kubectl get clusterrolebindings -o json | jq -r '
  .items[] |
  select(.roleRef.name == "cluster-admin") |
  "\(.metadata.name) -> \(.subjects[]? | "\(.kind)/\(.name) (\(.namespace // "cluster"))")"
'
2. 优先使用命名空间范围的 Role 而非 ClusterRole

使用 Role 和 RoleBinding 而非 ClusterRole 和 ClusterRoleBinding:

# Good: Namespace-scoped role
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: application
  name: app-developer
rules:
  - apiGroups: ["apps"]
    resources: ["deployments"]
    verbs: ["get", "list", "watch", "create", "update", "patch"]
  - apiGroups: [""]
    resources: ["pods", "pods/log"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["configmaps"]
    verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  namespace: application
  name: app-developer-binding
subjects:
  - kind: Group
    name: dev-team
    apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: app-developer
  apiGroup: rbac.authorization.k8s.io
3. 为每个工作负载使用专用服务账户
apiVersion: v1
kind: ServiceAccount
metadata:
  name: payment-processor
  namespace: payments
automountServiceAccountToken: false  # Disable auto-mount
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: payment-processor
  namespace: payments
spec:
  template:
    spec:
      serviceAccountName: payment-processor
      automountServiceAccountToken: true  # Only mount when explicitly needed
      containers:
        - name: processor
          image: payments/processor:v2.1@sha256:abc...
4. 限制危险权限

阻止能够实现权限提升的权限:

# Dangerous verbs/resources to restrict:
# - secrets: get, list, watch (exposes all secrets in namespace)
# - pods/exec: create (enables command execution in pods)
# - pods: create with privileged securityContext
# - serviceaccounts/token: create (generates new tokens)
# - clusterroles/clusterrolebindings: create, update (self-escalation)
# - nodes/proxy: create (bypasses API server authorization)

# Safe read-only role example
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: security-viewer
rules:
  - apiGroups: [""]
    resources: ["pods", "services", "namespaces", "nodes"]
    verbs: ["get", "list", "watch"]
  - apiGroups: ["apps"]
    resources: ["deployments", "daemonsets", "statefulsets"]
    verbs: ["get", "list", "watch"]
  - apiGroups: ["networking.k8s.io"]
    resources: ["networkpolicies"]
    verbs: ["get", "list", "watch"]
5. 用于用户身份验证的 OIDC 集成
# API server flags for OIDC integration
apiVersion: v1
kind: Pod
metadata:
  name: kube-apiserver
spec:
  containers:
    - name: kube-apiserver
      command:
        - kube-apiserver
        - --oidc-issuer-url=https://idp.company.com
        - --oidc-client-id=kubernetes
        - --oidc-username-claim=email
        - --oidc-groups-claim=groups
        - --oidc-ca-file=/etc/kubernetes/pki/oidc-ca.crt

RBAC 审计流程

步骤 1:枚举所有绑定
# All ClusterRoleBindings with subjects
kubectl get clusterrolebindings -o json | jq -r '
  .items[] | select(.subjects != null) |
  .subjects[] as $s |
  "\(.metadata.name) | \(.roleRef.name) | \($s.kind)/\($s.name)"
' | sort | column -t -s '|'

# All RoleBindings across namespaces
kubectl get rolebindings --all-namespaces -o json | jq -r '
  .items[] | select(.subjects != null) |
  .subjects[] as $s |
  "\(.metadata.namespace) | \(.metadata.name) | \(.roleRef.name) | \($s.kind)/\($s.name)"
' | sort | column -t -s '|'
步骤 2:识别权限过高的服务账户
# Find service accounts with cluster-admin or admin roles
kubectl get clusterrolebindings -o json | jq -r '
  .items[] |
  select(.roleRef.name == "cluster-admin" or .roleRef.name == "admin") |
  select(.subjects[]?.kind == "ServiceAccount") |
  "\(.subjects[] | select(.kind == "ServiceAccount") | "\(.namespace)/\(.name)")"
'
步骤 3:检查默认服务账户的使用情况
# Find pods using the default service account
kubectl get pods --all-namespaces -o json | jq -r '
  .items[] |
  select(.spec.serviceAccountName == "default" or .spec.serviceAccountName == null) |
  "\(.metadata.namespace)/\(.metadata.name)"
'
步骤 4:验证令牌自动挂载
# Find pods with auto-mounted service account tokens
kubectl get pods --all-namespaces -o json | jq -r '
  .items[] |
  select(.spec.automountServiceAccountToken != false) |
  "\(.metadata.namespace)/\(.metadata.name) sa=\(.spec.serviceAccountName // "default")"
'

工具

rbac-lookup
# Install rbac-lookup
kubectl krew install rbac-lookup

# View RBAC for a specific user
kubectl rbac-lookup [email protected]

# View all RBAC bindings wide format
kubectl rbac-lookup --kind user -o wide
rakkess(审查访问权限)
# Install rakkess
kubectl krew install access-matrix

# Show access matrix for current user
kubectl access-matrix

# Show access for a specific service account
kubectl access-matrix --sa payments:payment-processor

参考资料

评论

支持 Markdown