GCP - Token Persistance
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Authenticated User Tokens
To get the current token of a user you can run:
Check in this page how to directly use this token using gcloud:
To get the details to generate a new access token run:
It's also possible to find refresh tokens in $HOME/.config/gcloud/application_default_credentials.json
and in $HOME/.config/gcloud/legacy_credentials/*/adc.json
.
To get a new refreshed access token with the refresh token, client ID, and client secret run:
The refresh tokens validity can be managed in Admin > Security > Google Cloud session control, and by default it's set to 16h although it can be set to never expire:
Auth flow
The authentication flow when using something like gcloud auth login
will open a prompt in the browser and after accepting all the scopes the browser will send a request such as this one to the http port open by the tool:
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.
Note that the communication with localhost is in HTTP, so it it's possible to intercept the data to get a refresh token, however this data is valid just 1 time, so this would be useless, it's easier to just read the refresh token from the file.
OAuth Scopes
You can find all Google scopes in https://developers.google.com/identity/protocols/oauth2/scopes or get them executing:
It's possible to see which scopes the application that gcloud
uses to authenticate can support with this script:
After executing it it was checked that this app supports these scopes:
it's interesting to see how this app supports the drive
scope, which could allow a user to escalate from GCP to Workspace if an attacker manages to force the user to generate a token with this scope.
Check how to abuse this here.
Service Accounts
Just like with authenticated users, if you manage to compromise the private key file of a service account you will be able to access it usually as long as you want. However, if you steal the OAuth token of a service account this can be even more interesting, because, even if by default these tokens are useful just for an hour, if the victim deletes the private api key, the OAuh token will still be valid until it expires.
Metadata
Obviously, as long as you are inside a machine running in the GCP environment you will be able to access the service account attached to that machine contacting the metadata endpoint (note that the Oauth tokens you can access in this endpoint are usually restricted by scopes).
Remediations
Some remediations for these techniques are explained in https://www.netskope.com/blog/gcp-oauth-token-hijacking-in-google-cloud-part-2
References
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Last updated