# IMDSv2
> AWS's session-token hardening for the EC2 Instance Metadata Service: a defense-in-depth control that kills GET-only SSRF against 169.254.169.254 but not SSRF primitives that control HTTP method and headers.
## How it works
IMDSv2 makes metadata access session-oriented. A client must first **PUT** `/latest/api/token` with header `X-aws-ec2-metadata-token-ttl-seconds` (1–21600), then present the returned token as `X-aws-ec2-metadata-token` on every subsequent GET:
```
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" \
-H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
curl -H "X-aws-ec2-metadata-token: $TOKEN" \
http://169.254.169.254/latest/meta-data/iam/security-credentials/
```
The token is valid for up to 6h and only from the same instance where the session began. A bare GET to the metadata service on an IMDSv2-enforced host returns **401 Unauthorized**. AWS explicitly frames this as defense-in-depth, not a complete SSRF fix.
Two further controls:
- **Hop limit** — the PUT token response has a default IP TTL (hop limit) of **1**, so a token request originating from a container behind the instance's bridge/NAT is dropped before it returns. Platform teams routinely raise it to 2 so ECS/EKS SDKs work, which reopens the container path.
- **X-Forwarded-For rejection** — token requests carrying `X-Forwarded-For` are refused, to stop misconfigured reverse proxies from fetching tokens.
## What SSRF primitives can / can't satisfy it
| SSRF primitive | Completes IMDSv2 flow? |
|---|---|
| Plain GET-only (`?url=`) | **No** — cannot issue the PUT |
| Controls HTTP method + headers | **Yes** |
| CRLF / header injection in URL | **Yes** (smuggle PUT + token headers) |
| Method-forwarding proxy | **Yes** |
| Headless browser running attacker JS (`fetch()`) | **Yes** |
| gopher:// capable client | **Yes** (craft raw PUT then GET) |
| Follows redirects, attacker controls method semantics | **Yes** (303 converts POST→GET; see cases) |
So IMDSv2 raises the cost but does not remove the class: any bug giving method + header control is sufficient.
## IMDSv2 configuration / enforcement
- Require it per-instance: `HttpTokens=required`; enforce fleet-wide with an Org policy / SCP. A launch-template default does **not** retrofit already-running instances — confirm the live setting rather than assuming a rollout date.
- Keep hop limit at **1** unless a container genuinely needs IMDS.
- Strongest control: `HttpEndpoint=disabled` where the workload never needs metadata (then task/pod/file/env credentials must come from elsewhere).
| Mode | Blocks | Does not block |
|---|---|---|
| IMDSv1 enabled | nothing | any GET-capable SSRF |
| IMDSv2 required, hop limit 1 | GET-only SSRF + container-originated token requests | host-level method+header primitives |
| IMDSv2 required, hop limit 2 | GET-only SSRF | header-capable SSRF from containers too |
| IMDS disabled | metadata credential theft on that host | task/pod/file/env credentials |
## Bypass research / notable cases
- **Confluence `makeRequest` (CVE-2019-8451)** — Atlassian gadgets' `gadgets.io.makeRequest()` accepts `httpmethod`, `postData`, `headers` params. Yassine Aboukir & Thái Vũ used these to issue the PUT (`X-aws-ec2-metadata-token-ttl-seconds`) and then the authenticated GET, fully defeating IMDSv2 on a target that initially returned 401. The classic proof that header control = game over.
- **HTTP 303 redirect trick** — a user-controlled `token_uri` (BigQuery service-account cred) + a Python HTTP client that follows redirects by default: attacker server answers a POST to IMDS with **303**, which implicitly rewrites POST→GET, letting a POST-only primitive retrieve IMDS credentials for the K8s worker node.
- **Open-WebUI SafePlaywrightURLLoader (CVE-2026-87996)** — TOCTOU/DNS-rebinding: Python validates the IP, Chromium independently re-resolves at fetch time, rebinding to the metadata address and siphoning AWS/GCP/Azure credentials into the model chat context. Shows that a headless-browser SSRF primitive sets its own headers and thus satisfies IMDSv2.
- **Spring Boot `/proxy?url=`** — a proxy that forwards arbitrary method/headers lets the attacker run the full PUT-then-GET token flow verbatim through the SSRF sink to harvest IAM creds and pivot past a VPC-endpoint-restricted S3 bucket.
- **IPv6 / Craft CMS (bypass of CVE-2025-68437)** — denylists keyed on the IPv4 literal miss `[fd00:ec2::254]`; `gethostbyname()` only resolves A records, so an AAAA-only host returns the hostname string and passes the blocklist, reaching the IMDS IPv6 endpoint (IMDSv2 token rules still apply if enabled).
## Related
- [[Server-Side Request Forgery]]
- [[Cloud Metadata Service Exploitation]]
- [[SSRF Prevention]]
- [[DNS Rebinding]]
- [[IP Address Encoding Bypasses]]
- [[Open Redirect to SSRF]]
- [[Gopher Protocol SSRF]]
- [[SSRF in AI Agents and LLM Tools]]
## Sources
- [[raw/agentoffense.com/ssrf-cloud-metadata-attacks-url-fetcher-to-iam-credentials-15332|SSRF Cloud Metadata Attacks: URL Fetcher to IAM Credentials]]
- [[raw/book.hacktricks.xyz/cloud-ssrf-58|Cloud SSRF - HackTricks]]
- [[raw/medium.com/how-ssrf-exploits-imdsv2-limitations-in-aws-3262|Uncovering Cloud Security Flaws: How SSRF Exploits IMDSv2 Limitations in AWS]]
- [[raw/yassineaboukir.com/exploitation-of-an-ssrf-vulnerability-against-ec2-imdsv2-3260|Exploitation of an SSRF vulnerability against EC2 IMDSv2]]
- [[raw/wiz.io/imds-abused-hunting-rare-behaviors-to-uncover-exploits-2259|IMDS Abused: Hunting Rare Behaviors to Uncover Exploits]]
- [[raw/infosecwriteups.com/the-http-303-ssrf-hack-from-python-http-client-defaults-to-aws-credent-10216|The HTTP 303 SSRF Hack: From Python HTTP Client Defaults to AWS Credential Exfiltration]]
- [[raw/sh3llc0d3.com/open-webui-toctou-dns-rebinding-ssrf-to-cloud-metadata-13399|Open-WebUI TOCTOU DNS Rebinding SSRF (CVE-2026-87996)]]
- [[raw/cheatsheetseries.owasp.org/server-side-request-forgery-prevention-owasp-cheat-sheet-series-107|Server-Side Request Forgery Prevention Cheat Sheet]]
- [[raw/github.com/craft-cms-cloud-metadata-ssrf-protection-bypass-via-ipv6-resolution-4786|Cloud Metadata SSRF Protection Bypass via IPv6 Resolution]]