detecting-privilege-escalation-in-kubernetes-pods について
KubernetesのPodやコンテナにおける権限昇格を、アドミッションポリシー、ランタイム監視、監査ログで検出するセキュリティスキルです。危険なセキュリティコンテキスト、Falcoルール、ポリシー制御、調査手順を扱います。
主な機能
- 01リスクの高いPodセキュリティ設定の特定
- 02Pod Security Admissionポリシーの適用
- 03Gatekeeperアドミッション制御の作成
- 04Falcoによるランタイム活動の検出
- 05Kubernetes監査ログのイベント調査
活用シーン
- 01権限昇格リスクのあるPod設定の確認
- 02アドミッション時の危険なワークロードの阻止
- 03ランタイム権限昇格の試行の監視
- 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 と一緒にインストールされる参考資料やスクリプト。
LICENSEassets/template.mdreferences/api-reference.mdstandards.mdworkflows.mdscripts/agent.pyprocess.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: true | setuid/setgid の悪用 | アドミッション制御 |
| runAsUser: 0 | コンテナの root 実行 | アドミッション制御 |
| automountServiceAccountToken | API アクセス用トークンの窃取 | アドミッション制御 |
| 書き込み可能な /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]}'
ベストプラクティス
- 本番環境の名前空間では、Pod Security Admission を
restrictedレベルで有効にします - すべてのケイパビリティを削除し、必要なものだけを追加します
- すべてのコンテナで allowPrivilegeEscalation: false を設定します
- root 以外のユーザーで実行します(runAsNonRoot: true、runAsUser > 0)
- API アクセスが必要な場合を除き、automountServiceAccountToken を無効にします
- Falco でランタイムの権限昇格の試行を監視します
- Kubernetes の監査ログで RBAC の変更を監査します
- システムコールを制限するために seccomp プロファイルを使用します
コメント
Markdown 対応