Day 1: What a Compromised Pod Sees
Goal: You see the cluster through the eyes of a hijacked workload – token, API, network. · Duration: approx. 60 min · Prerequisite: Day 0
Briefing
An attacker rarely gets cluster-admin right away. The usual entry point: an application has a vulnerability (SSRF, RCE, a vulnerable image), and suddenly the attacker is sitting inside a pod. From there, only what this pod can do counts. Today you take inventory of this starting point – two questions:
- What is the pod’s token allowed to do against the Kubernetes API?
- Whom can the pod reach on the network?
In a fresh cluster the first answer is reassuring, the second alarming.
Drill 1: Who am I? (Token identity)
In the attacker pod from Day 0. The token lies in the file system; with it you ask the API who you count as:
kubectl -n attacker exec shell -- bash -c '
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
CACERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
curl -s --cacert $CACERT -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-X POST https://kubernetes.default.svc/apis/authentication.k8s.io/v1/selfsubjectreviews \
-d "{\"apiVersion\":\"authentication.k8s.io/v1\",\"kind\":\"SelfSubjectReview\"}"'Expected (shortened):
"status": {
"userInfo": {
"username": "system:serviceaccount:attacker:default",
"groups": [
"system:serviceaccounts",
"system:serviceaccounts:attacker",
"system:authenticated"
]
You are system:serviceaccount:attacker:default – the
default ServiceAccount of the namespace. Remember the groups: a role
that is too broad and is bound to system:authenticated or
system:serviceaccounts hits every pod in the
cluster.
Save your progress? Sign up for free
Drill 2: What am I allowed to do? (RBAC from the inside)
Try to list the pods in your own namespace:
kubectl -n attacker exec shell -- bash -c '
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
CACERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
curl -s -o /dev/null -w "%{http_code}\n" --cacert $CACERT \
-H "Authorization: Bearer $TOKEN" \
https://kubernetes.default.svc/api/v1/namespaces/attacker/pods'Expected:
403
Good. In a fresh cluster the default ServiceAccount has
no RBAC permissions. You can still read something –
everything any authenticated user is allowed to read:
kubectl -n attacker exec shell -- bash -c '
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
CACERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
curl -s --cacert $CACERT -H "Authorization: Bearer $TOKEN" \
https://kubernetes.default.svc/version'Expected (shortened):
{
"major": "1",
"minor": "36",
"gitVersion": "v1.36.6+rke2r1",
Knowing the version is the first step toward the matching CVE (Day 13). You should not hand even this small detail to an anonymous caller – see the Enterprise Note.
Save your progress? Sign up for free
Drill 3: Whom can I reach? (Network from the inside)
Now the second question. Put services in front of the victim application (if they are not there yet from Day 0), so that you can reach it via DNS:
kubectl -n shop expose deployment frontend --port=8080
kubectl -n shop expose deployment api --port=80
kubectl -n shop expose deployment db --port=5432And now from the attacker pod straight across the cluster:
kubectl -n attacker exec shell -- bash -c '
echo -n "frontend: "; curl -s -o /dev/null -w "%{http_code}\n" --max-time 5 \
http://frontend.shop.svc:8080
echo -n "api: "; curl -s -o /dev/null -w "%{http_code}\n" --max-time 5 \
http://api.shop.svc:80
echo -n "db:5432: "; nc -zv -w3 db.shop.svc 5432'Expected:
frontend: 200
api: 200
db:5432: Connection to db.shop.svc (10.43.161.163) 5432 port [tcp/postgresql] succeeded!
That is the shock: a pod in the namespace attacker
reaches the database in the namespace shop – without
asking. In Kubernetes the network is flat by default:
without a NetworkPolicy, every pod may talk to every other pod. That is
exactly what you close on Day 8.
Save your progress? Sign up for free
Drill 4: Fix – turn off the token where none is needed
Most workloads never talk to the Kubernetes API. Then they should not
have a token either. Build the same pod with
automountServiceAccountToken: false:
kubectl -n attacker apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: shell-notoken
spec:
automountServiceAccountToken: false
containers:
- name: shell
image: nicolaka/netshoot
command: ["sleep","infinity"]
EOF
kubectl -n attacker wait --for=condition=Ready pod/shell-notoken --timeout=90sSave your progress? Sign up for free
Drill 5: Proof – no token, no API
kubectl -n attacker exec shell-notoken -- \
ls /var/run/secrets/kubernetes.io/serviceaccount/Expected:
ls: /var/run/secrets/kubernetes.io/serviceaccount/: No such file or directory
command terminated with exit code 1
And without a token, the pod counts as nobody at the API:
kubectl -n attacker exec shell-notoken -- bash -c '
curl -s -o /dev/null -w "%{http_code}\n" --max-time 5 -k \
https://kubernetes.default.svc/api/v1/namespaces/attacker/pods'Expected:
401
401 instead of 403: before, you were a
known account without permissions; now you are not even authenticated.
One less attack surface.
kubectl -n attacker delete pod shell-notokenSave your progress? Sign up for free
Checkpoint
The normal attacker pod has a token and is a named account (403, not 401):
kubectl -n attacker exec shell -- bash -c '
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
curl -s -o /dev/null -w "%{http_code}\n" -k \
-H "Authorization: Bearer $TOKEN" \
https://kubernetes.default.svc/api/v1/namespaces/attacker/pods'Expected: 403
The flat network is real – the DB is reachable from
attacker:
kubectl -n attacker exec shell -- nc -zv -w3 db.shop.svc 5432Expected: … 5432 port [tcp/postgresql] succeeded!
Challenge
Find out whether the default ServiceAccount of the
namespace shop is allowed to do more than the one in
attacker – without going into a pod. Use
kubectl’s ability to make a request on behalf of
another account.
Hint: kubectl auth can-i --list knows
the flag --as.
Solution
kubectl auth can-i --list \
--as=system:serviceaccount:shop:default -n shopExpected (shortened):
Resources Non-Resource URLs Resource Names Verbs
selfsubjectreviews.authentication.k8s.io [] [] [create]
selfsubjectaccessreviews.authorization.k8s.io [] [] [create]
selfsubjectrulesreviews.authorization.k8s.io [] [] [create]
[/api/*] [] [get]
[/healthz] [] [get]
...
In a fresh cluster the list looks sparse – only self-inquiries and a
few non-resource URLs that every authenticated account is allowed. If a
real resource such as pods or secrets shows up
there, a chart or a RoleBinding has granted too much. You hunt that down
on Day 2.
Save your progress? Sign up for free
Enterprise Note
- Set
automountServiceAccountToken: falseas the default per ServiceAccount; only pods that really need the API get a token (set back totrueper pod). - Restrict anonymous access to
/versionand discovery: the kube-apiserver can limit anonymous requests; in hardened setups (CIS),--anonymous-auth=falseor anAnonymousAuthConfigurationis set. - The flat network is the biggest lever: a default-deny NetworkPolicy per namespace (Day 8).
- Turn on audit logging so that such API calls from a workload token become visible at all (Day 3).