Day 0: Training Ground
Goal: A real three-node cluster, a victim and an attacker pod are ready. · Duration: approx. 60 min · Prerequisite: three VMs
Briefing
A toy cluster on your laptop is not enough here: many attacks target the node – kubelet, node file system, etcd. For that you need real machines. We use RKE2 on three VMs: one server, two agents.
rke2-server .126 rke2-agent1 .228 rke2-agent2 .83
control-plane + etcd worker worker
|________________________|___________________|
one cluster, three nodes
On top of that, two roles in the cluster: the namespace
shop is the “normal” application (the victim), the
namespace attacker is the location of a compromised
workload. From there you run the attacks – never from your laptop with
cluster-admin, but the way an attacker would sit: in a pod, with its
permissions.
Drill 1: Prepare the VMs
On all three VMs (via SSH). Swap off, firewall off in the lab, hostnames set:
sudo swapoff -a
sudo hostnamectl set-hostname rke2-server # or rke2-agent1 / rke2-agent2
cat /etc/os-release | grep PRETTY_NAMEExpected:
PRETTY_NAME="Ubuntu 24.04.5 LTS"
Save your progress? Sign up for free
Drill 2: Install the RKE2 server
On rke2-server (.126):
curl -sfL https://get.rke2.io | sudo sh -
sudo systemctl enable --now rke2-server.serviceWait until the service is running, and make kubectl
usable:
sudo systemctl is-active rke2-server
export KUBECONFIG=/etc/rancher/rke2/rke2.yaml
export PATH=$PATH:/var/lib/rancher/rke2/bin
sudo -E kubectl get nodesExpected (shortened, may differ depending on the version):
active
NAME STATUS ROLES AGE VERSION
rke2-server Ready control-plane,etcd 90s v1.36.6+rke2r1
You will need the join token in a moment:
sudo cat /var/lib/rancher/rke2/server/node-tokenSave your progress? Sign up for free
Drill 3: Attach the two agents
On rke2-agent1 (.228) and rke2-agent2
(.83). <TOKEN> is the value from Drill 2,
<SERVER-IP> is 172.16.0.126:
curl -sfL https://get.rke2.io | sudo INSTALL_RKE2_TYPE="agent" sh -
sudo mkdir -p /etc/rancher/rke2
cat <<EOF | sudo tee /etc/rancher/rke2/config.yaml
server: https://172.16.0.126:9345
token: <TOKEN>
EOF
sudo systemctl enable --now rke2-agent.serviceBack on the server, check:
sudo -E kubectl get nodesExpected (shortened):
NAME STATUS ROLES AGE VERSION
rke2-agent1 Ready <none> 88s v1.36.6+rke2r1
rke2-agent2 Ready <none> 69s v1.36.6+rke2r1
rke2-server Ready control-plane,etcd 5m v1.36.6+rke2r1
Tip: Put
export KUBECONFIG=/etc/rancher/rke2/rke2.yamland the PATH into the server’s~/.bashrc; that does not save you thesudo -E– but for the node attacks later you need root anyway. In this book, work asubuntuwithsudowhere needed.
Save your progress? Sign up for free
Drill 4: Set up the victim
A plain application in the namespace shop: a web
frontend, an API, a database. Still without any hardening – that is
intentional.
sudo -E kubectl create namespace shop
sudo -E kubectl -n shop create deployment frontend \
--image=nginxinc/nginx-unprivileged:stable
sudo -E kubectl -n shop create deployment api \
--image=ealen/echo-server:latest
sudo -E kubectl -n shop create deployment db \
--image=postgres:16-alpine
sudo -E kubectl -n shop set env deployment/db POSTGRES_PASSWORD=lab-password
sudo -E kubectl -n shop get podsExpected (shortened):
NAME READY STATUS RESTARTS AGE
api-xxxxxxxxxx-xxxxx 1/1 Running 0 20s
db-xxxxxxxxxx-xxxxx 1/1 Running 0 20s
frontend-xxxxxxxxx-xxxxx 1/1 Running 0 20s
Save your progress? Sign up for free
Drill 5: Set up the attacker pod
The compromised workload. A perfectly normal pod with network tools and its default ServiceAccount token – an attacker needs nothing more as a starting point:
sudo -E kubectl create namespace attacker
sudo -E kubectl -n attacker run shell --image=nicolaka/netshoot \
--command -- sleep infinity
sudo -E kubectl -n attacker wait --for=condition=Ready pod/shell --timeout=60sThis is how you get into the attacker pod – from now on you are “in the cluster”, no longer at the control desk:
sudo -E kubectl -n attacker exec -it shell -- bashFirst look from the inside: which token does the pod carry?
ls /var/run/secrets/kubernetes.io/serviceaccount/
cat /var/run/secrets/kubernetes.io/serviceaccount/namespaceExpected:
ca.crt namespace token
attacker
The token in that directory is your key to the Kubernetes API. What you are allowed to do with it is the question of the next days – and how you restrict it.
Save your progress? Sign up for free
Checkpoint
Three nodes ready:
sudo -E kubectl get nodes --no-headers | grep -c ReadyExpected: 3
Victim and attacker are running:
sudo -E kubectl get pod -n attacker shell -o jsonpath='{.status.phase}'; echoExpected: Running
The attacker pod has an API token:
sudo -E kubectl -n attacker exec shell -- \
test -s /var/run/secrets/kubernetes.io/serviceaccount/token && echo hat-tokenExpected: hat-token
Challenge
Find out which ServiceAccount the attacker pod runs as – without
going into the pod, only with kubectl from the server.
Hint: A pod without a
serviceAccountName set gets a particular default.
Solution
sudo -E kubectl -n attacker get pod shell \
-o jsonpath='{.spec.serviceAccountName}'; echoExpected: default
Every namespace has a default ServiceAccount, and every
pod without its own setting uses it. In a well-hardened cluster this
default is allowed to do almost nothing – we check that on
Day 2.
Save your progress? Sign up for free
Enterprise Note
- In production, RKE2 comes with CIS hardening
(
profile: cis) and an admission setup; we deliberately start unhardened to see the gaps. - Keep lab clusters strictly separate from the company network. A deliberately vulnerable cluster is a worthwhile target – for others, too.
- etcd sits on the server node here. In real setups it belongs encrypted and on dedicated nodes with restricted access (Day 10).