Kubernetes Pentesting
Kubernetes (K8s) orchestrates containers at scale, and its security is a world of its own: the surface includes the API server, secrets, RBAC, network policies, pod service accounts, and the possibility of escaping a container to the node (see Container and Docker Escapes). The attacker’s goal is usually to go from “I have access to a pod” to “I control the cluster” (cluster-admin) or the underlying node.
Threat model
Section titled “Threat model”The attacker starts from one of two positions: from outside (an exposed API server or kubelet, sometimes with anonymous access) or from inside a compromised pod (via a vulnerable webapp). From inside, the asset is the service account token mounted in the pod: with its RBAC permissions it enumerates and, if they’re excessive, escalates to cluster-admin or escapes to the node. Unencrypted etcd and an open kubelet (10250) are direct shortcuts to all secrets and to execution in pods.
flowchart TD
A[Compromised pod] --> B[Mounted SA token]
B --> C[kubectl auth can-i --list]
C --> D{Excessive RBAC?}
D -->|get/list secrets| E[Steal privileged SA tokens]
D -->|create pods| F[Privileged pod / hostPath]
D -->|exec/impersonate| G[Lateral to other pods]
F --> H[Node root]
E --> I[cluster-admin]
H --> I
Theory: architecture (the minimum)
Section titled “Theory: architecture (the minimum)”- API server (port 6443): the brain, every operation goes through it.
- etcd: the cluster database (includes secrets if not encrypted).
- kubelet (port 10250): agent on each node; poorly protected, it allows execution in pods.
- pods: containers; each with a service account whose token is mounted inside.
- RBAC: defines who can do what over which resources.
Red Team
Section titled “Red Team”Recon and access
Section titled “Recon and access”# API exposed? anonymous?curl -k https://<ip>:6443/versionkubectl --insecure-skip-tls-verify --server https://<ip>:6443 get pods # anonymous access?# exposed kubelet (10250) -> sometimes allows execution in podscurl -k https://<ip>:10250/podsFrom inside a pod
Section titled “From inside a pod”The mounted service account usually can talk to the API; with its permissions you enumerate and sometimes escalate:
# pod token and namespaceexport TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)export NS=$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace)curl -k -H "Authorization: Bearer $TOKEN" \ https://kubernetes.default.svc/api/v1/namespaces/$NS/pods# or with kubectl if availablekubectl auth can-i --list # what I can do (KEY)kubectl get secrets -A # can I read secrets? (credentials, tokens)kubectl auth can-i --list is the sudo -l of Kubernetes: it tells you exactly what your service account allows.
Escalation vectors
Section titled “Escalation vectors”Excessive RBAC:
create pods→ launch a privileged pod that mounts the node FS → node root.get/list secrets→ steal tokens of more privileged SAs (including cluster-admin).impersonate→ act as another user/SA.execinto pods → enter other people’s pods (lateral).
Container escape (see Container and Docker Escapes): privileged pod, hostPath, hostPID, hostNetwork, or mounting /var/run/docker.sock → node access. And infrastructure shortcuts: unencrypted etcd exposes all secrets; an open kubelet (10250) allows exec without going through the API.
Classic example: with permission to create pods, you launch one with hostPath mounting the node’s /:
apiVersion: v1kind: Podmetadata: name: pwnspec: containers: - name: pwn image: alpine command: ["sleep", "3600"] volumeMounts: - name: host mountPath: /host volumes: - name: host hostPath: path: /You mount the node’s filesystem at /host → read/write as node root → from there, the cluster.
kubectl— official client (auth can-i,get,exec).kube-hunter— discovers and tests cluster weaknesses.kubeletctl— abuse the kubelet (10250).peirates— K8s escalation and pivoting framework.kdigger— enumeration from inside a pod.
Real CVEs
Section titled “Real CVEs”- CVE-2018-1002105: API server (proxy) escalation allowing unauthorized access to backends.
- CVE-2020-8558: node-local (
localhost) services reachable from other nodes/pods. - CVE-2020-8559: a compromised API server could redirect and execute on nodes via the kubelet.
Blue Team
Section titled “Blue Team”- Minimal RBAC: no cluster-admin except where essential; no broad
create pods/secrets; audit withkubectl auth can-i. - Pod Security Standards / admission controllers (OPA/Gatekeeper, Kyverno): forbid privileged pods,
hostPath,hostPID. - Secrets encrypted in etcd (encryption at rest) and an external manager; network policies to segment pods.
- Non-anonymous API server, authenticated kubelet (no open 10250), don’t mount
docker.sockor the host FS. - API server logging/auditing; image scanning;
automountServiceAccountToken: falsewhere unused.
Checklist
Section titled “Checklist”- API server (6443) or kubelet (10250) exposed/anonymous?
- Pod service account token and
auth can-i --list -
get/list secrets→ steal privileged tokens -
create pods→ privileged pod/hostPath → node -
exec/impersonatefor lateral/escalation - Container escape to the node (Container and Docker Escapes)
- Unencrypted etcd → cluster secrets
- Map up to cluster-admin or node root