# GCP Pentesting {{#include ../../banners/hacktricks-training.md}} ## 基本情報 **GCP** 環境の **ペンテスト** を開始する前に、どのように機能するかについて知っておくべき **基本的なこと** がいくつかあります。これにより、何をすべきか、誤設定を見つける方法、そしてそれをどのように悪用するかを理解するのに役立ちます。 **組織** 階層、**権限**、およびその他の基本的な概念については、以下で説明されています: {{#ref}} gcp-basic-information/ {{#endref}} ## 学習用ラボ - [https://gcpgoat.joshuajebaraj.com/](https://gcpgoat.joshuajebaraj.com/) - [https://github.com/ine-labs/GCPGoat](https://github.com/ine-labs/GCPGoat) - [https://github.com/lacioffi/GCP-pentest-lab/](https://github.com/lacioffi/GCP-pentest-lab/) - [https://github.com/carlospolop/gcp_privesc_scripts](https://github.com/carlospolop/gcp_privesc_scripts) ## GCP ペンテスター/レッドチームの方法論 GCP 環境を監査するためには、どの **サービスが使用されているか**、何が **公開されているか**、誰が **何にアクセスできるか**、および内部 GCP サービスと **外部サービス** がどのように接続されているかを知ることが非常に重要です。 レッドチームの観点から、GCP 環境を侵害するための **最初のステップ** は、いくつかの **資格情報** を取得することです。以下はその方法のいくつかのアイデアです: - github (または類似の) での **漏洩** - OSINT - **ソーシャル** エンジニアリング (ページ [**Workspace Security**](../workspace-security/index.html) を確認) - **パスワード** 再利用 (パスワード漏洩) - GCP ホストアプリケーションの脆弱性 - [**サーバーサイドリクエストフォージェリ**](https://book.hacktricks.wiki/en/pentesting-web/ssrf-server-side-request-forgery/cloud-ssrf.html) メタデータエンドポイントへのアクセス - **ローカルファイル読み取り** - `/home/USERNAME/.config/gcloud/*` - `C:\Users\USERNAME\.config\gcloud\*` - 第三者の **侵害** - **内部** 従業員 または、**認証されていないサービス** を侵害することによって: {{#ref}} gcp-unauthenticated-enum-and-access/ {{#endref}} または、**レビュー** を行っている場合は、これらの役割で **資格情報を要求** することができます: {{#ref}} gcp-permissions-for-a-pentest.md {{#endref}} > [!NOTE] > 資格情報を取得した後は、それが **誰のものであるか**、および **何にアクセスできるか** を知る必要があります。したがって、いくつかの基本的な列挙を実行する必要があります: ## 基本的な列挙 ### **SSRF** GCP メタデータを **列挙する** 方法についての詳細は、以下の hacktricks ページを確認してください: {{#ref}} https://book.hacktricks.wiki/en/pentesting-web/ssrf-server-side-request-forgery/cloud-ssrf.html {{#endref}} ### Whoami GCP では、自分が誰であるかを推測するためにいくつかのオプションを試すことができます: ```bash #If you are inside a compromise machine gcloud auth list curl -H "Content-Type: application/x-www-form-urlencoded" -d "access_token=$(gcloud auth print-access-token)" https://www.googleapis.com/oauth2/v1/tokeninfo gcloud auth print-identity-token #Get info from the token #If you compromised a metadata token or somehow found an OAuth token curl -H "Content-Type: application/x-www-form-urlencoded" -d "access_token=" https://www.googleapis.com/oauth2/v1/tokeninfo ``` APIエンドポイント `/userinfo` を使用して、ユーザーに関する詳細情報を取得することもできます: ```bash curl -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: OAuth $(gcloud auth print-access-token)" https://www.googleapis.com/oauth2/v1/userinfo curl -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: OAuth " https://www.googleapis.com/oauth2/v1/userinfo ``` ### 組織の列挙 ```bash # Get organizations gcloud organizations list #The DIRECTORY_CUSTOMER_ID is the Workspace ID gcloud resource-manager folders list --organization # Get folders gcloud projects list # Get projects ``` ### Principals & IAM Enumeration 十分な権限がある場合、**GCPアカウント内の各エンティティの権限を確認すること**は、あなたや他のアイデンティティが何をできるか、また**権限を昇格させる方法**を理解するのに役立ちます。 IAMを列挙するための十分な権限がない場合、**ブルートフォースで盗む**ことでそれを把握できます。\ **列挙とブルートフォースの方法**については、以下を確認してください: {{#ref}} gcp-services/gcp-iam-and-org-policies-enum.md {{#endref}} > [!NOTE] > 現在、**あなたの資格情報に関する情報を持っている**(そして、もしあなたがレッドチームであれば、**検出されていないことを願っています**)。環境で使用されているサービスを特定する時が来ました。\ > 次のセクションでは、**一般的なサービスを列挙する方法**をいくつか確認できます。 ## Services Enumeration GCPには驚くべき数のサービスがあります。次のページでは、**基本情報、列挙**のチートシート、**検出を回避する方法**、**持続性**を得る方法、その他の**ポストエクスプロイト**のトリックについての情報を見つけることができます: {{#ref}} gcp-services/ {{#endref}} すべての作業を**手動で**行う必要はないことに注意してください。この投稿の下部には、[**自動ツール**](#automatic-tools)に関する**セクション**があります。 さらに、この段階で**認証されていないユーザーに公開されているサービスを**発見するかもしれません。それらを悪用できるかもしれません: {{#ref}} gcp-unauthenticated-enum-and-access/ {{#endref}} ## Privilege Escalation, Post Exploitation & Persistence クラウドの資格情報を取得したり、クラウド内で実行されているサービスを侵害した後の最も一般的な方法は、**侵害されたアカウントが持つ誤設定された権限を悪用すること**です。したがって、最初に行うべきことは、自分の権限を列挙することです。 さらに、この列挙中に、**権限は「組織」の最高レベルで設定できる**ことを忘れないでください。 {{#ref}} gcp-privilege-escalation/ {{#endref}} {{#ref}} gcp-post-exploitation/ {{#endref}} {{#ref}} gcp-persistence/ {{#endref}} ### Publicly Exposed Services GCPサービスを列挙しているときに、いくつかのサービスが**インターネットに要素を公開している**のを見つけたかもしれません(VM/コンテナのポート、データベースやキューサービス、スナップショットやバケットなど)。\ ペンテスター/レッドチームとして、**機密情報や脆弱性**を見つけられるかどうかを常に確認すべきです。これにより、**AWSアカウントへのさらなるアクセス**を得ることができるかもしれません。 この本では、**公開されたGCPサービスを見つける方法とそれを確認する方法**に関する**情報**を見つけることができるはずです。**公開されたネットワークサービスの脆弱性を見つける方法**については、特定の**サービス**を以下で**検索**することをお勧めします: {{#ref}} https://book.hacktricks.wiki/ {{#endref}} ## GCP <--> Workspace Pivoting **一つの**プラットフォームでのプリンシパルの**侵害**は、攻撃者が**他のプラットフォームを侵害する**ことを可能にするかもしれません。以下を確認してください: {{#ref}} gcp-to-workspace-pivoting/ {{#endref}} ## Automatic Tools - **GCloudコンソール**で、[https://console.cloud.google.com/iam-admin/asset-inventory/dashboard](https://console.cloud.google.com/iam-admin/asset-inventory/dashboard)にアクセスすると、プロジェクトで使用されているリソースとIAMを確認できます。 - このAPIでサポートされている資産を確認できます:[https://cloud.google.com/asset-inventory/docs/supported-asset-types](https://cloud.google.com/asset-inventory/docs/supported-asset-types) - [**複数のクラウドで使用できるツール**](../pentesting-cloud-methodology.md)を確認してください。 - [**gcp_scanner**](https://github.com/google/gcp_scanner):これはGCPリソーススキャナーで、特定の資格情報がGCP上で**どのレベルのアクセスを持っているか**を判断するのに役立ちます。 ```bash # Install git clone https://github.com/google/gcp_scanner.git cd gcp_scanner virtualenv -p python3 venv source venv/bin/activate pip install -r requirements.txt # Execute with gcloud creds python3 __main__.py -o /tmp/output/ -g "$HOME/.config/gcloud" ``` - [**gcp_enum**](https://gitlab.com/gitlab-com/gl-security/threatmanagement/redteam/redteam-public/gcp_enum): gcloud cliを使用してGCP環境を列挙し、結果をファイルに保存するBashスクリプト。 - [**GCP-IAM-Privilege-Escalation**](https://github.com/RhinoSecurityLabs/GCP-IAM-Privilege-Escalation): 高いIAM権限を列挙し、それを悪用してGCPで権限を昇格させるスクリプト(列挙スクリプトを実行できませんでした)。 - [**BF My GCP Permissions**](https://github.com/carlospolop/bf_my_gcp_permissions): あなたの権限をブルートフォースするスクリプト。 ## gcloud config & debug ```bash # Login so gcloud can use your credentials gcloud auth login gcloud config set project security-devbox gcloud auth print-access-token # Login so SDKs can use your user credentials gcloud auth application-default login gcloud auth application-default set-quota-project security-devbox gcloud auth application-default print-access-token # Update gcloud gcloud components update ``` ### Capture gcloud, gsutil... network **`gcloud`** CLIで**リクエスト**を**表示**するために、**パラメータ** **`--log-http`**を使用できることを忘れないでください。ログがトークン値を隠さないようにするには、`gcloud config set log_http_redact_token false`を使用します。 さらに、通信を傍受するには: ```bash gcloud config set proxy/address 127.0.0.1 gcloud config set proxy/port 8080 gcloud config set proxy/type http gcloud config set auth/disable_ssl_validation True # If you don't want to completely disable ssl_validation use: gcloud config set core/custom_ca_certs_file cert.pem # Back to normal gcloud config unset proxy/address gcloud config unset proxy/port gcloud config unset proxy/type gcloud config unset auth/disable_ssl_validation gcloud config unset core/custom_ca_certs_file ``` ### OAuthトークンのgcloudでの設定 **メタデータエンドポイントから抽出されたサービスアカウントOAuthトークンを使用するためには**、次のようにするだけです: ```bash # Via env vars export CLOUDSDK_AUTH_ACCESS_TOKEN= gcloud projects list # Via setup echo "" > /some/path/to/token gcloud config set auth/access_token_file /some/path/to/token gcloud projects list gcloud config unset auth/access_token_file ``` ## 参考文献 - [https://about.gitlab.com/blog/2020/02/12/plundering-gcp-escalating-privileges-in-google-cloud-platform/](https://about.gitlab.com/blog/2020/02/12/plundering-gcp-escalating-privileges-in-google-cloud-platform/) {{#include ../../banners/hacktricks-training.md}}