GCP - Non-svc Persistance

Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)

Support HackTricks

Są to przydatne techniki, gdy w jakiś sposób przejąłeś jakieś poświadczenia GCP lub maszynę działającą w środowisku GCP.

Token Hijacking

Authenticated User Tokens

Aby uzyskać aktualny token użytkownika, możesz uruchomić:

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

Sprawdź na tej stronie, jak bezpośrednio użyć tego tokena za pomocą gcloud:

Aby uzyskać szczegóły dotyczące wygenerowania nowego tokena dostępu, uruchom:

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

Możliwe jest również znalezienie refresh tokens w $HOME/.config/gcloud/application_default_credentials.json oraz w $HOME/.config/gcloud/legacy_credentials/*/adc.json.

Aby uzyskać nowy odświeżony access token za pomocą refresh token, client ID i client secret, uruchom:

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

Ważność tokenów odświeżania można zarządzać w Admin > Security > Google Cloud session control, a domyślnie jest ustawiona na 16h, chociaż można ją ustawić na nigdy nie wygasa:

Auth flow

Przepływ uwierzytelniania podczas używania czegoś takiego jak gcloud auth login otworzy monit w przeglądarce, a po zaakceptowaniu wszystkich zakresów przeglądarka wyśle żądanie takie jak to do portu http otwartego przez narzędzie:

/?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

Następnie, gcloud użyje stanu i kodu z pewnym zakodowanym na stałe client_id (32555940559.apps.googleusercontent.com) oraz client_secret (ZmssLNjJy2998hD4CTg2ejr2) aby uzyskać ostateczne dane tokena odświeżania.

Należy zauważyć, że komunikacja z localhost odbywa się przez HTTP, więc możliwe jest przechwycenie danych w celu uzyskania tokena odświeżania, jednak te dane są ważne tylko raz, więc byłoby to bezużyteczne, łatwiej jest po prostu odczytać token odświeżania z pliku.

OAuth Scopes

Możesz znaleźć wszystkie zakresy Google na https://developers.google.com/identity/protocols/oauth2/scopes lub uzyskać je wykonując:

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

Możliwe jest sprawdzenie, które zakresy aplikacja używana przez gcloud do uwierzytelniania może obsługiwać za pomocą tego skryptu:

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

Po wykonaniu sprawdzono, że ta aplikacja obsługuje te zakresy:

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

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)

Support HackTricks

Last updated