Skip to content

Commit 29001ee

Browse files
committed
im
1 parent 88dc555 commit 29001ee

File tree

1 file changed

+21
-45
lines changed
  • src/pentesting-cloud/kubernetes-security/abusing-roles-clusterroles-in-kubernetes

1 file changed

+21
-45
lines changed

src/pentesting-cloud/kubernetes-security/abusing-roles-clusterroles-in-kubernetes/README.md

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,16 @@ Therfore, it's possible to **get inside a pod and steal the token of the SA**, o
213213
kubectl exec -it <POD_NAME> -n <NAMESPACE> -- sh
214214
```
215215

216+
> [!NOTE]
217+
> By default the command is executed in the first container of the pod. Get **all the pods in a container** with `kubectl get pods <pod_name> -o jsonpath='{.spec.containers[*].name}'` and then **indicate the container** where you want to execute it with `kubectl exec -it <pod_name> -c <container_name> -- sh`
218+
219+
If it's a distroless container you could try using **shell builtins** to get info of the containers or uplading your own tools like a **busybox** using: **`kubectl cp </path/local/file> <podname>:</path/in/container>`**.
220+
216221
### port-forward
217222

218223
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:
219224

220-
```
225+
```bash
221226
kubectl port-forward pod/mypod 5000:5000
222227
```
223228

@@ -599,7 +604,7 @@ For a [`mutatingwebhookconfigurations` example check this section of this post](
599604

600605
### Escalate
601606

602-
As you can read in the next section: [**Built-in Privileged Escalation Prevention**](#built-in-privileged-escalation-prevention), a principal cannot update neither create roles or clusterroles without having himself those new permissions. Except if he has the **verb `escalate`** over **`roles`** or **`clusterroles`.**\
607+
As you can read in the next section: [**Built-in Privileged Escalation Prevention**](#built-in-privileged-escalation-prevention), a principal cannot update neither create roles or clusterroles without having himself those new permissions. Except if he has the **verb `escalate` or `*`** over **`roles`** or **`clusterroles`** and the respective binding options.\
603608
Then he can update/create new roles, clusterroles with better permissions than the ones he has.
604609

605610
### Nodes proxy
@@ -659,61 +664,31 @@ The privilege to create Rolebindings allows a user to **bind roles to a service
659664

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

662-
#### Create a sidecar proxy app <a href="#create-a-sidecar-proxy-app" id="create-a-sidecar-proxy-app"></a>
663-
664-
Create your .yaml
667+
#### Create a sidecar proxy app
665668

666-
```bash
667-
kubectl run app --image=bash --command -oyaml --dry-run=client > <appName.yaml> -- sh -c 'ping google.com'
668-
```
669+
A sidecar container consists just on adding a **second (or more) container inside a pod**.
669670

670-
Edit your .yaml and add the uncomment lines:
671+
For example, the following is part of the configuration of a pod with 2 containers:
671672

672673
```yaml
673-
#apiVersion: v1
674-
#kind: Pod
675-
#metadata:
676-
# name: security-context-demo
677-
#spec:
678-
# securityContext:
679-
# runAsUser: 1000
680-
# runAsGroup: 3000
681-
# fsGroup: 2000
682-
# volumes:
683-
# - name: sec-ctx-vol
684-
# emptyDir: {}
685-
# containers:
686-
# - name: sec-ctx-demo
687-
# image: busybox
688-
command:
689-
[
690-
"sh",
691-
"-c",
692-
"apt update && apt install iptables -y && iptables -L && sleep 1h",
693-
]
694-
securityContext:
695-
capabilities:
696-
add: ["NET_ADMIN"]
697-
# volumeMounts:
698-
# - name: sec-ctx-vol
699-
# mountPath: /data/demo
700-
# securityContext:
701-
# allowPrivilegeEscalation: true
674+
spec:
675+
containers:
676+
- name: main-application
677+
image: nginx
678+
- name: sidecar-container
679+
image: busybox
680+
command: ["sh","-c","<execute something in the same pod but different container>"]
702681
```
703682

704-
See the logs of the proxy:
705-
706-
```bash
707-
kubectl logs app -C proxy
708-
```
683+
For example, to backdoor an existing pod with a new container you could just add a new container in the specification. Note that you could **give more permissions** to the second container that the first won't have.
709684

710685
More info at: [https://kubernetes.io/docs/tasks/configure-pod-container/security-context/](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/)
711686

712687
### Malicious Admission Controller
713688

714689
An admission controller **intercepts requests to the Kubernetes API server** before the persistence of the object, but **after the request is authenticated** **and authorized**.
715690

716-
If an attacker somehow manages to **inject a Mutationg Admission Controller**, he will be able to **modify already authenticated requests**. Being able to potentially privesc, and more usually persist in the cluster.
691+
If an attacker somehow manages to **inject a Mutation Admission Controller**, he will be able to **modify already authenticated requests**. Being able to potentially privesc, and more usually persist in the cluster.
717692

718693
**Example from** [**https://blog.rewanthtammana.com/creating-malicious-admission-controllers**](https://blog.rewanthtammana.com/creating-malicious-admission-controllers):
719694

@@ -751,7 +726,7 @@ kubectl describe po nginx | grep "Image: "
751726

752727
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!!?
753728

754-
#### Technicalities <a href="#heading-technicalities" id="heading-technicalities"></a>
729+
#### Technicalities
755730

756731
The `./deploy.sh` script establishes a mutating webhook admission controller, which modifies requests to the Kubernetes API as specified in its configuration lines, influencing the outcomes observed:
757732

@@ -806,6 +781,7 @@ https://github.com/aquasecurity/kube-bench
806781
- [**https://www.cyberark.com/resources/threat-research-blog/kubernetes-pentest-methodology-part-1**](https://www.cyberark.com/resources/threat-research-blog/kubernetes-pentest-methodology-part-1)
807782
- [**https://blog.rewanthtammana.com/creating-malicious-admission-controllers**](https://blog.rewanthtammana.com/creating-malicious-admission-controllers)
808783
- [**https://kubenomicon.com/Lateral_movement/CoreDNS_poisoning.html**](https://kubenomicon.com/Lateral_movement/CoreDNS_poisoning.html)
784+
- [**https://kubenomicon.com/**](https://kubenomicon.com/)
809785

810786
{{#include ../../../banners/hacktricks-training.md}}
811787

0 commit comments

Comments
 (0)