SECURITY ENGINEERING · PART 10 · CLOUD SECURITY · ATTACKS · 143 OF 229

IMDS and SSRF: How Cloud Credentials Get Stolen

The instance metadata service hands every cloud workload its own credentials, and a single SSRF bug can steal them. This episode explains the metadata service, how SSRF reaches it, the Capital One breach, the defences (IMDSv2, SSRF protection, least-privilege roles, egress limits), and why this is the attack that most clearly links application and cloud security.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 10

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • IMDS hands out role credentials
  • SSRF can steal them (Capital One)
  • enforce IMDSv2
  • fix SSRF
  • least-privilege roles

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.

What IMDS is
  • 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.

The attack
  • 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.

The real case
  • 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.

Harden IMDS
  • 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.

SSRF defence in the app
  • 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.

If creds are stolen anyway
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1What is the instance metadata service (IMDS)?

A link-local service (at 169.254.169.254 on AWS, with equivalents on Azure and GCP) that provides a cloud instance with information about itself and the temporary credentials of its attached role, so workloads can call cloud APIs without a stored key.

Q2How does SSRF let an attacker steal cloud credentials?

If an application feature fetches a URL without restriction, an attacker supplies the metadata service address instead. The server fetches it and returns the role credentials in the response, giving the attacker the workload's cloud credentials through a single application bug.

Q3What happened in the Capital One breach?

In 2019 an SSRF flaw reached the metadata service, which returned credentials for an over-permissioned role with broad storage access. The attacker used them to read around 100 million customer records. It required three failures: the SSRF bug, an unprotected IMDS, and an over-broad role.

Q4What is IMDSv2 and how does it help?

A hardened version of the metadata service that requires the caller to first obtain a session token via a PUT request before reading data. Simple GET-based SSRF cannot perform that step, so it fails. Enforcing IMDSv2 and a low hop limit stops most metadata-theft attacks.

Q5How do you prevent SSRF from reaching the metadata service?

Allowlist the hosts and schemes the application may fetch, resolve DNS and block private and link-local addresses including the metadata IP, disable redirects, and never fetch arbitrary user-supplied URLs. This closes the path to the metadata service.

Q6Why does least privilege on instance roles matter for this attack?

Because even if credentials are stolen, a least-privilege role can do little. Capital One was severe because the role had broad storage access. Web server roles should never carry broad storage or admin permissions, limiting the blast radius of stolen credentials.

Q7How can you detect metadata credential theft?

Alert when credentials that were issued to an instance are used from an IP address other than that instance, for example from an external location. This off-instance use of instance role credentials is a strong indicator of this specific attack.

Q8Why does this attack link application and cloud security?

Because it chains an application-layer flaw (SSRF) with a cloud-layer feature (the metadata service) and cloud IAM (role permissions). Defending it requires secure coding, cloud hardening and least-privilege IAM together, illustrating that the two domains are inseparable.