mcpField
D
detecting-privilege-escalation-in-kubernetes-pods

detecting-privilege-escalation-in-kubernetes-pods

github.com/mukul975セキュリティ35.2k83 回インストール8 週間前に更新
リポジトリ

detecting-privilege-escalation-in-kubernetes-pods について

KubernetesのPodやコンテナにおける権限昇格を、アドミッションポリシー、ランタイム監視、監査ログで検出するセキュリティスキルです。危険なセキュリティコンテキスト、Falcoルール、ポリシー制御、調査手順を扱います。

主な機能

  1. 01リスクの高いPodセキュリティ設定の特定
  2. 02Pod Security Admissionポリシーの適用
  3. 03Gatekeeperアドミッション制御の作成
  4. 04Falcoによるランタイム活動の検出
  5. 05Kubernetes監査ログのイベント調査

活用シーン

  1. 01権限昇格リスクのあるPod設定の確認
  2. 02アドミッション時の危険なワークロードの阻止
  3. 03ランタイム権限昇格の試行の監視
  4. 04不審なRBACやPod変更の調査

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/mukul975/anthropic-cybersecurity-skills --skill detecting-privilege-escalation-in-kubernetes-pods

インストール後、Claude Code で/detecting-privilege-escalation-in-kubernetes-podsを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

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

詳細

Kubernetes ポッドでの権限昇格の検出

概要

Kubernetes での権限昇格は、ポッドまたはコンテナが意図された範囲を超える権限を取得したときに発生します。これには、root としての実行、特権モードの使用、ホストファイルシステムのマウント、危険な Linux ケイパビリティの有効化、カーネルの脆弱性の悪用が含まれます。検出には、アドミッション制御(防止)、ランタイム監視(検出)、監査ログ(調査)を組み合わせます。

使用する場面

  • Kubernetes ポッドでの権限昇格を検出する必要があるセキュリティインシデントを調査するとき
  • この領域の検出ルールや脅威ハンティングクエリを作成するとき
  • SOC アナリストがこの種の分析に関する体系的な手順を必要とするとき
  • 関連する攻撃手法に対するセキュリティ監視のカバレッジを検証するとき

前提条件

  • Kubernetes クラスター v1.25 以降(Pod Security Admission のサポート)
  • クラスター管理者アクセス権を持つ kubectl
  • Falco または同様のランタイムセキュリティツール
  • アドミッションポリシー用の OPA Gatekeeper または Kyverno

Kubernetes における権限昇格のベクトル

ベクトルリスク検出方法
privileged: trueホストへの完全なアクセスアドミッション制御 + 監査
hostPID: trueホストプロセスへのアクセスアドミッション制御
hostNetwork: trueホストのネットワークスタックへのアクセスアドミッション制御
hostPath volumesホストファイルシステムの読み書きアドミッション制御
SYS_ADMIN capability特権に近いアクセスアドミッション制御 + ランタイム
allowPrivilegeEscalation: truesetuid/setgid の悪用アドミッション制御
runAsUser: 0コンテナの root 実行アドミッション制御
automountServiceAccountTokenAPI アクセス用トークンの窃取アドミッション制御
書き込み可能な /proc または /sysカーネルパラメーターの操作ランタイム監視

アドミッション制御による検出

Pod Security Admission(組み込み)
# Enforce restricted policy on namespace
apiVersion: v1
kind: Namespace
metadata:
  name: production
  labels:
    pod-security.kubernetes.io/enforce: restricted
    pod-security.kubernetes.io/enforce-version: latest
    pod-security.kubernetes.io/audit: restricted
    pod-security.kubernetes.io/warn: restricted
OPA Gatekeeper ポリシー
# Block dangerous capabilities
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
  name: k8sdangerouspriv
spec:
  crd:
    spec:
      names:
        kind: K8sDangerousPriv
  targets:
    - target: admission.k8s.gatekeeper.sh
      rego: |
        package k8sdangerouspriv

        dangerous_caps := {"SYS_ADMIN", "SYS_PTRACE", "SYS_MODULE", "DAC_OVERRIDE", "NET_ADMIN", "NET_RAW"}

        violation[{"msg": msg}] {
          container := input.review.object.spec.containers[_]
          cap := container.securityContext.capabilities.add[_]
          dangerous_caps[cap]
          msg := sprintf("Container %v adds dangerous capability: %v", [container.name, cap])
        }

        violation[{"msg": msg}] {
          container := input.review.object.spec.containers[_]
          container.securityContext.privileged == true
          msg := sprintf("Container %v runs in privileged mode", [container.name])
        }

        violation[{"msg": msg}] {
          container := input.review.object.spec.containers[_]
          container.securityContext.allowPrivilegeEscalation == true
          msg := sprintf("Container %v allows privilege escalation", [container.name])
        }

        violation[{"msg": msg}] {
          input.review.object.spec.hostPID == true
          msg := "Pod uses host PID namespace"
        }

        violation[{"msg": msg}] {
          input.review.object.spec.hostNetwork == true
          msg := "Pod uses host network"
        }

Falco によるランタイム検出

# /etc/falco/rules.d/privesc-detection.yaml
- rule: Setuid Binary Execution in Container
  desc: Detect execution of setuid/setgid binaries in a container
  condition: >
    spawned_process and container and
    (proc.name in (su, sudo, newgrp, chsh, passwd) or
     proc.is_exe_upper_layer=true)
  output: >
    Setuid/setgid binary executed in container
    (user=%user.name container=%container.name image=%container.image.repository
     command=%proc.cmdline parent=%proc.pname)
  priority: WARNING
  tags: [container, privilege-escalation, T1548]

- rule: Capability Gained in Container
  desc: Detect when a process gains elevated capabilities
  condition: >
    evt.type = capset and container and
    evt.arg.cap != ""
  output: >
    Process gained capabilities in container
    (container=%container.name image=%container.image.repository
     capabilities=%evt.arg.cap command=%proc.cmdline)
  priority: WARNING
  tags: [container, privilege-escalation, T1548.001]

- rule: Container with Dangerous Capabilities Started
  desc: Detect container launched with dangerous capabilities
  condition: >
    container_started and container and
    (container.image.repository != "registry.k8s.io/pause") and
    (container.cap_effective contains SYS_ADMIN or
     container.cap_effective contains SYS_PTRACE or
     container.cap_effective contains SYS_MODULE)
  output: >
    Container with dangerous capabilities
    (container=%container.name image=%container.image.repository
     caps=%container.cap_effective)
  priority: CRITICAL
  tags: [container, privilege-escalation, T1068]

- rule: Write to /etc/passwd in Container
  desc: Detect writes to /etc/passwd inside container
  condition: >
    open_write and container and fd.name = /etc/passwd
  output: >
    Write to /etc/passwd in container
    (container=%container.name image=%container.image.repository
     command=%proc.cmdline user=%user.name)
  priority: CRITICAL
  tags: [container, privilege-escalation, T1136]

Kubernetes 監査ログによる検出

# audit-policy.yaml - Capture privilege escalation events
apiVersion: audit.k8s.io/v1
kind: Policy
rules:
  # Log pod creation with security context details
  - level: RequestResponse
    resources:
      - group: ""
        resources: ["pods"]
    verbs: ["create", "update", "patch"]

  # Log privilege escalation attempts
  - level: RequestResponse
    resources:
      - group: "rbac.authorization.k8s.io"
        resources: ["clusterroles", "clusterrolebindings", "roles", "rolebindings"]
    verbs: ["create", "update", "patch", "bind", "escalate"]

  # Log service account token requests
  - level: Metadata
    resources:
      - group: ""
        resources: ["serviceaccounts/token"]
    verbs: ["create"]
権限昇格に関する監査ログのクエリ
# Find pods created with privileged security context
kubectl logs -n kube-system kube-apiserver-* | \
  jq 'select(.verb == "create" and .objectRef.resource == "pods") |
  select(.requestObject.spec.containers[].securityContext.privileged == true)'

# Find RBAC escalation attempts
kubectl logs -n kube-system kube-apiserver-* | \
  jq 'select(.objectRef.resource == "clusterrolebindings" and .verb == "create")'

調査プレイブック

# Check pod security context
kubectl get pod <pod-name> -n <ns> -o jsonpath='{.spec.containers[*].securityContext}'

# Check effective capabilities
kubectl exec <pod-name> -n <ns> -- cat /proc/1/status | grep -i cap

# List pods running as root
kubectl get pods --all-namespaces -o json | \
  jq '.items[] | select(.spec.containers[].securityContext.runAsUser == 0 or .spec.containers[].securityContext.privileged == true) | {name: .metadata.name, ns: .metadata.namespace}'

# Check for hostPath volumes
kubectl get pods --all-namespaces -o json | \
  jq '.items[] | select(.spec.volumes[]?.hostPath != null) | {name: .metadata.name, ns: .metadata.namespace, paths: [.spec.volumes[].hostPath.path]}'

ベストプラクティス

  1. 本番環境の名前空間では、Pod Security Admission を restricted レベルで有効にします
  2. すべてのケイパビリティを削除し、必要なものだけを追加します
  3. すべてのコンテナで allowPrivilegeEscalation: false を設定します
  4. root 以外のユーザーで実行します(runAsNonRoot: true、runAsUser > 0)
  5. API アクセスが必要な場合を除き、automountServiceAccountToken を無効にします
  6. Falco でランタイムの権限昇格の試行を監視します
  7. Kubernetes の監査ログで RBAC の変更を監査します
  8. システムコールを制限するために seccomp プロファイルを使用します

コメント