diff --git a/src/pentesting-cloud/gcp-security/gcp-persistence/gcp-non-svc-persistence.md b/src/pentesting-cloud/gcp-security/gcp-persistence/gcp-non-svc-persistence.md new file mode 100644 index 000000000..b389156b7 --- /dev/null +++ b/src/pentesting-cloud/gcp-security/gcp-persistence/gcp-non-svc-persistence.md @@ -0,0 +1,90 @@ +# GCP - トークン持続性 + +{{#include ../../../banners/hacktricks-training.md}} + +### 認証されたユーザートークン + +ユーザーの**現在のトークン**を取得するには、次のコマンドを実行できます: +```bash +sqlite3 $HOME/.config/gcloud/access_tokens.db "select access_token from access_tokens where account_id='';" +``` +このページで**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='';" +``` +**`$HOME/.config/gcloud/application_default_credentials.json`** および **`$HOME/.config/gcloud/legacy_credentials/*/adc.json`** でリフレッシュトークンを見つけることも可能です。 + +**リフレッシュトークン**、クライアントID、およびクライアントシークレットを使用して新しいアクセストークンを取得するには、次のコマンドを実行します: +```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 +``` +リフレッシュトークンの有効性は **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](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}}