Notes
The metadata service
The instance metadata service, or IMDS, lives at a fixed link-local address, reachable only from on the instance itself. It returns information about the instance and, crucially, the temporary credentials of the role attached to it. This is how workloads get credentials without a stored key, the good workload-identity pattern. The catch is that anything running on the instance, including your own application making requests, can reach it, which is what an attacker exploits.
- a link-local service at 169.254.169.254
- returns instance info and, crucially, the role's temporary credentials
- so code can call cloud APIs with no stored key
- reachable only from on the instance itself
- convenient, and a juicy target
SSRF reaches it
The attack combines an application flaw with the metadata service. Many apps fetch a URL: a webhook, an image loader, a PDF renderer, an import. If that feature does not restrict where it can go, an attacker supplies the metadata address instead of a normal URL. The server fetches it and returns the role credentials in the response. Now the attacker holds your workload's cloud credentials, through a single application bug. This is how an app vulnerability becomes a cloud breach.
- app feature: fetch a URL (webhook, image, PDF, import)
- attacker supplies the metadata URL instead
- the server fetches it and returns the credentials
- now the attacker has the workload's cloud credentials
- one app bug -> cloud access
Capital One
The Capital One breach in twenty nineteen is the textbook case. An SSRF flaw let the attacker reach the metadata service, which returned the credentials of the role attached to the server. That role had broad storage permissions, so the attacker read around a hundred million customer records from buckets. It took three failures lined up: the SSRF bug, an unprotected metadata service, and an over-permissioned role. Fixing any one would have stopped it, which is how the defences are layered.
- 2019: an SSRF flaw reached the metadata service
- it returned the role's credentials
- the role was over-permissioned for storage
- attacker read ~100M customer records from buckets
- three failures: SSRF, IMDS, over-broad role
Fix the metadata service
The first defence is to harden the metadata service itself. IMDSv2 requires the caller to first request a session token with a special PUT request before it can read anything. A simple GET-based SSRF, the common kind, cannot do that, so it fails. You enforce IMDSv2 across the fleet, set a low hop limit so containers cannot reach the host's metadata, and disable the service where a workload does not need it. The other clouds have equivalent protections. This alone stops most of these attacks.
- require IMDSv2: a session token must be requested with a PUT first
- simple GET-based SSRF cannot do that, so it fails
- set a low hop limit so containers cannot reach it
- disable IMDS entirely where not needed
- GCP/Azure have similar protections
Fix the SSRF
The second defence is in your code, from the secure-coding part. Where the app fetches a URL, allowlist the hosts and schemes you will actually fetch, resolve the DNS and block private and link-local addresses, including the metadata address specifically, and disable redirects, because an attacker can redirect an allowed URL to the metadata service. The rule is to never fetch an arbitrary user-supplied URL. Fixing the SSRF closes the door that lets the attack reach the metadata service in the first place.
- allowlist the hosts and schemes you will fetch
- resolve the DNS and block private/link-local IPs (including 169.254.169.254)
- disable redirects
- never fetch an arbitrary user-supplied URL
- as in the secure-coding part
Limit the blast radius
The third defence assumes the credentials are stolen anyway and limits the damage. If the role has least privilege, the stolen credentials can do very little, which is why Capital One's over-permissioned role made it so bad. Never attach broad storage or admin permissions to a web server role. Egress filtering helps, since attackers often use the credentials from their own machine. And detection: instance credentials used from an external IP is a strong signal of exactly this attack.
- least-privilege role: stolen creds can do little
- no broad storage or admin on web server roles
- egress filtering: creds are often exfiltrated out; limit outbound
- detect: metadata-derived creds used from an external IP is a strong alert
Common mistakes
Mistakes. Leaving the old IMDSv1 enabled, so simple SSRF still works. An application that fetches arbitrary user-supplied URLs with no allowlist. Over-permissioned instance roles, so stolen credentials unlock everything. Allowing redirects in the app's URL fetcher, which bypasses a naive allowlist. No egress filtering, so exfiltration is easy. And no alert when instance role credentials are used from somewhere other than the instance, which is the clearest sign of this exact attack.
- IMDSv1 still allowed
- the app fetching arbitrary user URLs (SSRF)
- over-permissioned instance roles
- redirects allowed in the app fetcher
- no egress filtering
- no alert on instance creds used off the instance
Say it like this
In the interview: the metadata service hands each instance its role credentials at a fixed local URL. An SSRF bug in the app makes the server fetch that URL and return the credentials, which is how Capital One lost a hundred million records. I defend in layers. I enforce IMDSv2 so simple SSRF cannot get a token. I fix SSRF by allowlisting destinations and blocking link-local addresses. I keep instance roles least-privilege, add egress filtering, and alert when instance credentials are used from an external IP.
- "An SSRF bug makes the server
- fetch the metadata URL, which returns the role credentials.
- I defend in layers: enforce
- IMDSv2 so naive SSRF fails, fix SSRF with allowlists that block link-local IPs, least- privilege instance roles so stolen creds do little, egress
- limits, and alert on misuse."