There is an address every cloud instance can reach and almost no one configures on purpose: 169.254.169.254. It is the instance metadata service, and it exists to answer a bootstrapping question, "who am I, and what credentials do I get?" The problem is that a server-side request forgery bug can make your application ask that question on the attacker's behalf, and the answer is a set of working cloud credentials.

The Instance Metadata Service (IMDS) is a link-local HTTP endpoint available to every VM in AWS, Azure, and Google Cloud. It serves instance details and temporary IAM credentials for the role attached to the machine. Those credentials let the workload call cloud APIs without embedded secrets, a good design. The danger is that the metadata endpoint trusts anything that can send it an HTTP request from the instance, and a web application with an SSRF flaw is exactly such a thing.

How the attack works

The Cloud Metadata Service and SSRF: IMDSv1 vs IMDSv2Diagram with these labels, in reading order: INTERNET; APP HOST; METADATA / CLOUD; crafted URL; forged GET; v1: any GET; Attacker; Web app with; an SSRF flaw; IMDSv2 token; Metadata service; 169.254.169.254; Instance role; credentials; IMDSv1 hands these; to any URL-only SSRF.INTERNETAPP HOSTMETADATA / CLOUDcrafted URLforged GETv1: any GETAttackerWeb app withan SSRF flawIMDSv2 tokenMetadata service169.254.169.254Instance rolecredentialsIMDSv1 hands theseto any URL-only SSRF
An SSRF bug turns the metadata endpoint into a credential vending machine.

SSRF lets an attacker coax a server into making requests to a URL the attacker chooses. Point that at the metadata IP and, on a vulnerable instance, the server fetches its own role credentials and returns them in the response. The attacker now holds the instance's identity and can call the cloud API directly, with whatever permissions that role carries. The 2019 Capital One breach is the best-known public example: an attacker used a server-side request forgery weakness in a misconfigured web application firewall to reach the EC2 metadata service, retrieved role credentials and used them to read data in S3 (as reported by Krebs on Security, 2 August 2019).

SSRF reaching the metadata endpoint (AWS IMDSv1)GET /fetch?url=http://169.254.169.254/latest/meta-data/iam/security-credentials/ → role name → GET .../iam/security-credentials/<role> → AccessKeyId, SecretAccessKey, Token

IMDSv1 vs IMDSv2

AWS introduced IMDSv2 in November 2019, a session-oriented scheme that makes the common SSRF path much harder without changing what the metadata contains. The difference is in how you ask:

  • IMDSv1 is a simple request/response: any GET to the metadata IP returns the data. That is precisely what a basic SSRF can forge, because most SSRF primitives only control a URL.
  • IMDSv2 requires a session token first. You PUT to /latest/api/token to obtain a short-lived token, then send it in the X-aws-ec2-metadata-token header on every request. A URL-only SSRF cannot issue a PUT or set a custom header.
  • A hop limit of 1 (the default) stops the token response from being routed off the instance, which blocks relays from containers and proxies. IMDSv2 also refuses to issue tokens to requests that carry an X-Forwarded-For header.
  • Enforcement matters. IMDSv2 only helps when v1 is disabled; leaving both enabled means an attacker just uses the easy one.
Why it matters: The metadata service is the shortest path from a web-app bug to cloud API access. It can turn an SSRF, often rated medium severity in isolation, into credential theft.

The other clouds, and the broader fix

Azure and Google Cloud take a header-based approach: Azure's IMDS requires the request to carry Metadata: true, and GCP's metadata server requires Metadata-Flavor: Google. These header requirements defeat naive SSRF that can only set a URL. As with IMDSv2, this raises the bar but is not absolute: an SSRF that can control headers or a full request-forgery primitive, can still get through.

  • Enforce IMDSv2 and disable v1 across every instance and launch template; require it at the account level so new machines inherit it.
  • Set the metadata hop limit to 1 and block egress to 169.254.169.254 from application code that has no reason to reach it.
  • Fix the SSRF at the source. Validate and allowlist outbound URLs, resolve and pin destinations, and reject link-local and private ranges.
  • Right-size the instance role. If credentials are stolen, least privilege decides whether the blast radius is one bucket or the whole account.

IMDSv2 is one of the rare mitigations that closes much of an attack class with one enforced setting, but only on the instances you know to configure. The workloads you have not inventoried are the ones most likely to still allow v1.

Primary source. Add defense in depth against open firewalls, reverse proxies, and SSRF vulnerabilities with enhancements to the EC2 Instance Metadata Service, AWS Security Blog, by Colm MacCárthaigh, November 19, 2019.

Back to the knowledge baseRequest an assessment