← 返回列表

ECScape: Understanding IAM Privilege Boundaries in Amazon ECS

单站 Naor Haziz 作者 Naor Haziz 发布 2025-07-21T16:00:00+03:00 入库 2026-11-18T05:05:12

ECScape: Understanding IAM Privilege Boundaries in Amazon ECS

This post is Part 2 of our educational series on Amazon ECS security. In Part 1 – Under the Hood of Amazon ECS on EC2, we explored how the ECS agent, IAM roles and the ECS control plane provide credentials to tasks. Here we’ll demonstrate how those mechanisms can lead to a known risk when tasks with different privilege levels share the same EC2 host. This cross-task credential exposure highlights the inherent risks of relying on per-task IAM scoping and task execution boundaries when workloads share the same EC2 instance, and it underscores why Fargate (where each task runs in its own micro‑VM) offers stronger isolation. Our goal is to help you understand the IAM privilege boundaries in Amazon ECS and how to configure your services securely.

Cloud environments offer unparalleled flexibility and scalability, but they also introduce complex security considerations. Subtle interactions between AWS services can lead to unintended privilege exposure if you assume container‑level isolation where none exists. In this post, I’ll walk you through how I discovered this cross‑container IAM credential exposure in Amazon ECS (Elastic Container Service), demonstrate the technique (dubbed “ECScape”), and share lessons learned for securing your own environments. By demystifying the control plane, we hope to equip readers with the knowledge to architect for isolation and to advocate for improvements.

Resources

TL;DR

Quick refresher: on EC2 launch type, the ECS control plane assumes each task role, pushes those credentials over the ACS WebSocket to the agent, and the agent serves them to tasks via 169.254.170.2. ECScape abuses that delivery path.

Discovery

I didn’t set out to research ECS; my original goal was to finish an eBPF-based monitoring tool that could watch ECS workloads in real time. To build accurate per-task dashboards, I needed a quick, local way to map processes → containers → ECS tasks and then tag them with cluster name, task ARN, and service name.

While experimenting, I first tried to scrape the Docker container labels that ECS automatically adds to each task. Those labels include the task ARN, task definition family (with revision), cluster ARN, and container name – but the service name is conspicuously absent.

Alt text

To get the missing service name, I turned to the ECS Task Metadata endpoint (version 4) that the ECS agent exposes inside each container’s network namespace (at 169.254.170.2/v4/…). Querying that endpoint from within a container returned all the details I needed, including the service name, task definition family, revision, and more. Perfect for my monitoring use case.

Alt text

Naturally, I wondered if my eBPF-based sensor could simply mimic what the agent does - query the same metadata endpoint and stitch the data together for any process on the host. Before trying that, I double-checked the IAM permissions of the EC2 instance’s IAM role (the ecsInstanceRole attached to the container host) and noticed something odd: it did not have ECS APIs like ecs:ListServices or anything that would normally reveal a service name. Yet the agent clearly knew the service name for each task - it was able to hand it to me via the metadata endpoint. So the agent was clearly getting it from the ECS control plane, not via public ECS APIs.

Alt text

Curiosity led me down a rabbit hole of packet capture. I set up a small local proxy to watch the traffic between the ECS agent and AWS endpoints. What I observed was startling:

Alt text

At this moment, I realized that the ECS control plane was actively pushing task credentials down to the agent over this WebSocket channel. Normally, this is fine - it’s how the agent gets credentials to give to each task’s container - but it got me thinking: if I could somehow tap into that channel, I might capture credentials that weren’t meant for my container. My thought was: if the control plane hands out all task credentials to the agent, could I pose as the agent and trick AWS into sending methose credentials?

That question was the genesis of ECScape – a scenario that explores how escaping the container’s confines and impersonating the ECS agent could allow access to other tasks’ credentials.

How ECScape Works: Impersonating the ECS Agent to Access Other Credentials

Now we’ll walk through how ECScape actually works in practice, step by step. The goal for the attacker (a malicious process in one container) is to obtain the IAM credentials of all other tasks on the same EC2 host. To do this, the attacker will:

  1. Obtain the host’s IAM role credentials (so it can act as the ECS agent).
  2. Discover the ECS control plane endpoint that the agent talks to.
  3. Gather the necessary identifiers (cluster name/ARN, container instance ARN, etc.) to authenticate as the agent.
  4. Establish a fake agent session (WebSocket) with the ECS control plane and request sendCredentials.
  5. Harvest credentials for all running tasks on that instance, then use them for further exploitation.

Let’s break these down in detail:

Step 1: Steal EC2 Instance Role Credentials via IMDS

The attacker’s starting point is a compromised container (any low-privileged ECS task running on EC2). By default, unless prevented, containers can query the instance metadata service (IMDS). A simple curl request to http://169.254.169.254/latest/meta-data/iam/security-credentials/{InstanceProfileName} will return the Access Key, Secret Key, and Session Token for the EC2 host’s IAM role. These are the same credentials that the ECS agent on the host uses for its API calls. Now our malicious container has obtained the host instance role credentials.

Alt text

At this point, the attacker possesses the EC2 instance’s temporary credentials (an STS session representing the container instance). Importantly, these are not the task role credentials of any application container; having the instance profile’s credentials alone doesn’t automatically let you assume the roles of tasks because of IAM trust boundaries:

Because of these restrictions, if the attacker tried to directly call AWS STS to assume another task’s role using the instance credentials, it would fail due to the task role’s trust policy (and likely missing IAM permissions). In other words, the attacker can’t simply pivot to task roles by normal AssumeRole API calls. They need another way – which is where impersonating the agent comes in.

Security note: Reading IMDS from within the container (an HTTP GET) is not logged by CloudTrail, so this initial credential theft is stealthy. If the attacker uses the stolen instance credentials to call AWS APIs (as they will in subsequent steps), those actions will appear in CloudTrail logs as if performed by the instance role.

Step 2: Discover the Poll Endpoint URL (ecs:DiscoverPollEndpoint)

The ECS agent doesn’t talk to the control plane at a generic public API endpoint. Instead, AWS provides a specific polling endpoint for each cluster and container instance. Using the stolen instance role credentials, the attacker calls the ECS API ecs:DiscoverPollEndpoint. This API returns a URL, something like https://ecs-a-1..amazonaws.com, which is the endpoint the agent should connect to in order to receive ACS messages.

The instance IAM role is allowed to call DiscoverPollEndpoint (it’s required for the agent to function). If for some reason this API call failed, the endpoint might be guessable (it tends to include the region and some cluster-specific identifier), but typically using the API is straightforward and succeeds with default permissions. Now the attacker knows where to initiate the connection for the control plane communications.

Alt text

Step 3: Gather Required Identifiers (Cluster, Container Instance ARN, etc.)

When the real ECS agent connects to ACS, it includes various identifiers so the backend knows which cluster and which specific container instance (EC2 host) is checking in. To convincingly masquerade as the agent, the attacker needs to supply these identifiers. Important values include:

How can the attacker get these? The Task Metadata endpoint we used earlier (169.254.170.2) can provide the Cluster ARN easily (since each task’s metadata includes the cluster). The tricky one is the Container Instance ARN, which isn’t exposed to tasks via the normal metadata endpoint. However, ECS has an introspection API on the agent (normally accessed from the host) that can return the container instance ARN and other info. In my exploit, I found that by querying a specific local endpoint, I could retrieve the container instance ARN from inside the container. In some cases, one could also call ecs:ListContainerInstances via AWS APIs to get it, but that might require additional IAM permissions that the instance role doesn’t have by default. I leveraged the agent’s introspection API to get everything I needed – it returned the container instance ARN and even the agent’s software version.

Alt text

Note: The fact that a container can access the agent’s introspection API is itself a minor isolation gap. Typically, that API might be bound to localhost or a Unix socket on the host. If it’s exposed on a link-local address and iptables rules don’t filter it out, a container could reach it. Small piece of the chain, but shows how one exposure can help an attacker.

Armed with the cluster ARN, container instance ARN, and other identifiers, the attacker is ready to masquerade as the ECS agent.

Step 4: Forge and Sign the ACS WebSocket Request (Impersonating the Agent)

Now the attacker has to initiate a fake agent session with the ECS control plane. Using the Poll endpoint URL from Step 2 (e.g. something like https://ecs-a-1.region.amazonaws.com) and the identifiers from Step 3, the attacker constructs a WebSocket connection request that looks just like the one the real ECS agent would send. Crucially, this request must be SigV4-signed using the stolen instance profile credentials, so that AWS will authenticate it as coming from the legitimate container instance (the same signing process the real agent uses for API calls).

The WebSocket URL includes query parameters for all those identifiers and settings, for example:

1
2
3
4
5
6
7
8
9

wss://ecs-a-1..amazonaws.com/ws?
agentHash=&
agentVersion=&
clusterArn=arn:aws:ecs:::cluster/&
containerInstanceArn=arn:aws:ecs:::container-instance/&
dockerVersion=&
protocolVersion=&
seqNum=1&
sendCredentials=true

A few important notes on this step:

If our signing and parameters are all correct, AWS will accept the WebSocket connection as if it were the real ECS agent on that container instance. From the perspective of the ECS control plane, our malicious process is now just another authorized agent session for that instance.

It turned out that AWS did not strictly enforce a one-agent-per-instance rule at the time. In my tests, I ended up with two concurrent connections for the same container instance – one from the real ECS agent, and one from my impersonating session. The control plane happily sent messages to both. (If AWS had limited it to one connection at a time, my fake agent session might have knocked the real agent offline or been refused, which could have raised flags. But as it stood, I could stay completely under the radar while eavesdropping on the stream.)

Step 5: Harvest All Task Role Credentials from ACS

With the forged WebSocket session open, the attacker is now riding the same multiplexed ACS message stream as the real ECS agent. Over this channel, the ECS control plane continuously pushes structured messages: heartbeats (keep-alives with sequence numbers), task lifecycle directives (start/stop/update commands), telemetry data, and – most importantly – IamRoleCredentials payloads.

It’s important to clarify how ECS normally handles task credentials: the ECS service (principal ecs-tasks.amazonaws.com in AWS) performs the STS:AssumeRole for each task’s role (and the task execution role, if one is used). The ECS agent never calls STS itself. Instead, the control plane assumes the role on your behalf, obtains a set of temporary credentials, and then delivers those credentials down to the agent via ACS. The agent caches them in memory and serves them to the container when the container asks for its credentials. (Execution role credentials are similarly delivered but used internally by the agent for pulling images, writing logs, etc., and are not exposed through the metadata endpoint.)

Because our forged agent session included sendCredentials=true and authenticated as the correct container instance, the ECS control plane proceeds to push an IamRoleCredentials message for every currently running task on that instance that has an IAM role. Each credentials message includes the task’s ARN (so you know which task it belongs to), the IAM role ARN, a credentials ID (used in the metadata URI), the AccessKeyId, SecretAccessKey, SessionToken, an expiration time, and a flag for the role type (whether it’s the task’s application role or the task’s execution role). If there are, say, five tasks with IAM roles running on that host, as soon as we connect we’ll receive five distinct credential sets. One of those will be for our own compromised task (which we already have and don’t need), but the others belong to other tasks – representing immediate lateral escalation opportunities.

Alt text

The forged agent channel also remains stealthy. Our malicious session mimics the agent’s expected behavior – acknowledging messages, incrementing sequence numbers, sending heartbeats – so nothing seems amiss. ECS actually allows concurrent authenticated sessions for the same container instance, so the real agent continues to operate normally and also receives the same credential messages. From the control plane’s point of view, there’s no anomaly; it’s just delivering credentials and updates to an agent session as usual.

CloudTrail visibility: The AWS API calls we made to set up this exploit (e.g. the ecs:DiscoverPollEndpoint call, and the long-poll ecs:Poll connection which underlies the WebSocket) will show up in CloudTrail as actions by the container instance’s IAM role. Any subsequent AWS actions we perform with stolen task credentials will show up as those task roles performing the actions (with the task’s ARN in the session context). That is one of the few potential detection points: if Task A’s role is suddenly used to perform actions it normally never does (especially outside the expected context or time window for Task A), that’s a red flag. Importantly, merely receiving the credential payloads over ACS does not produce any CloudTrail events – it’s an internal push from AWS to the agent.

A few limitations/nuances to note: If a task on the instance has no task role defined, naturally there are no credentials to steal from it. Tasks that start after our session is established will also have their credentials sent over ACS (and we’ll capture those too, as long as our rogue WebSocket remains connected). We also receive any task execution role credentials in the ACS stream (for tasks that use a separate execution role for pulling images, etc.). This is especially impactful if the execution role is overly privileged — for example, if it allows access to secrets in AWS Secrets Manager or SSM Parameter Store, which can now be exfiltrated by our rogue agent.

In summary, ECS centralizes the AssumeRole process in the control plane and then “moves” those ephemeral credentials to the on-host agent, relying on the secrecy of that ACS channel and per-task metadata scoping for isolation. By impersonating the agent’s upstream connection, ECScape completely collapses that trust model: one compromised container can passively collect every other task’s IAM role credentials on the same EC2 instance and immediately act with those privileges.

Alt text

Impact: Why ECScape Is So Severe

The implications of ECScape are far-reaching for anyone running ECS tasks on shared EC2 hosts:

Alt text

Alt text

In the live demo of ECScape, I deploy three ECS tasks on the same EC2 instance to demonstrate how a low-privileged container can escalate its privileges and compromise sensitive resources:

During the demo, I show how the ecscape-task:

The key takeaway: even a container with no effective permissions can compromise high-value resources if it shares a host with more privileged tasks. This breaks the isolation assumptions many teams rely.

Alt text

Mitigation and Best Practices for ECS

AWS’s official response to this issue was that ECS was operating within its intended design: containers sharing an EC2 host are implicitly in the same trust domain unless you isolate them. In other words, from AWS’s perspective this was “working as designed” (more on their response later), and they did not issue a patch or CVE since they did not view it as a vulnerability in the service. This means it’s on AWS users to harden their ECS-on-EC2 environments. Here are important mitigation steps and best practices to protect against ECScape-like scenarios:

By implementing the above – especially locking down IMDS for tasks – you can significantly reduce the risk of an ECScape‑style scenario. In fact, AWS’s documentation was updated after this research to explicitly warn that “tasks running on the same EC2 instance may potentially access credentials belonging to other tasks on that instance,” and it strongly encourages using Fargate for stronger isolation and monitoring cross‑task role usage via CloudTrail.

Responsible Disclosure and AWS Response

Alt text

When we reported ECScape to AWS through their coordinated disclosure program, they reviewed our findings and confirmed that the behaviour we observed is a design consideration of ECS on EC2 rather than a vulnerability. In AWS’s eyes, containers sharing an EC2 instance are implicitly part of the same trust domain unless the user enforces isolation. They did not issue a CVE or a security bulletin because this does not break AWS’s security boundaries; instead, they emphasised that customers should architect accordingly.

AWS has also reiterated this stance publicly in their security best practices blog for ECS, which encourages users to adopt stronger isolation models such as Fargate where needed.

However, AWS did take two noteworthy actions in response:

Alt text

Alt text

From a researcher’s perspective, ECScape was a deep dive into how ECS stitches together control-plane role assumption, on-host credential delivery, and container isolation. The core lesson is that you should treat each container as potentially compromiseable and rigorously constrain its blast radius. AWS’s convenient abstractions (task roles, metadata service, etc.) make life easier for developers, but when multiple tasks with different privilege levels share an underlying host, their security is only as strong as the mechanisms isolating them – mechanisms which can have subtle weaknesses.

As the industry moves toward stronger isolation models (e.g. AWS Fargate’s per-task microVM, Firecracker, gVisor, and other sandboxing or even hardware-based isolation like Nitro enclaves), this particular class of cross-container credential theft becomes much harder. But today, many environments still run heterogeneous workloads on shared EC2 instances for cost or management reasons. ECScape blurs the line between misconfiguration, design trade-off, and exploit – and it underscores the importance of defense in depth. Things like blocking unnecessary IMDS access, enforcing least privilege, isolating critical tasks, and monitoring for anomalies aren’t just box-checking best practices; they can each break a link in the kill chain we described.

Ultimately, improving security in scenarios like this is a shared responsibility. Cloud providers need to continually refine isolation primitives and credential delivery mechanisms, and cloud users need to architect with the assumption that any one container might become malicious. By understanding exactly how ECS credential delivery works (as we’ve done here) and where the trust boundaries lie, you can make more informed decisions about your cloud architecture and monitoring.

Thanks for reading! Got similar stories or ideas about ECS security? Feel free to reach out :)

Alt text

CC BY 4.0by the author.

原文链接:https://naorhaziz.com/posts/ecscape-iam-privilege-boundaries-in-ecs/