Sub topic 15
Reading instance metadata securely with IMDSv2
Every EC2 instance can ask about itself: its ID, its Availability Zone, its network addresses, and the temporary credentials of its IAM role. It does this through the instance metadata service (IMDS). This chapter explains how the service works and why version 2 (IMDSv2) exists. It also shows how to require IMDSv2 on new and running instances.
This chapter assumes you can launch an instance (earlier chapters), you have the AWS CLI configured, and you can use curl over SSH. You should also know that an instance profile delivers an IAM role to an instance.
What the metadata service is
The IMDS is an HTTP endpoint at the link-local address 169.254.169.254. A link-local address is reachable only from the machine itself, so the request never leaves the host. It is served by the Nitro system, not by software you install.
The data is organized as a tree of paths:
| Path | What it returns |
|---|---|
| `/latest/meta-data/instance-id` | The instance ID |
| `/latest/meta-data/placement/availability-zone` | The Availability Zone |
| `/latest/meta-data/local-ipv4` | The private IPv4 address |
| `/latest/meta-data/iam/security-credentials/<role-name>` | Temporary credentials for the instance's role |
| `/latest/user-data` | The user data script you supplied at launch |
| `/latest/dynamic/instance-identity/document` | A JSON document describing the instance |
Applications use this to configure themselves without hard-coded values. The AWS CLI and SDKs also use it to find credentials when you attach an instance profile.
Why version 1 was a problem
IMDSv1 is a plain request and response. Any GET to the address returns data. That is a problem when an application on the instance can be tricked into making HTTP requests for an attacker. This attack is called server-side request forgery (SSRF). If a web app fetches any URL a user supplies, the attacker can submit http://169.254.169.254/latest/meta-data/iam/security-credentials/my-role. The app then returns the role's credentials, which the attacker can use from anywhere until they expire.
IMDSv2 changes the protocol so that this common attack pattern fails.
How IMDSv2 works
IMDSv2 is session-oriented. You first make a PUT request to get a token, then include that token in a header on every metadata request.
TOKEN=$(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/instance-idRun these on the instance. The first call returns an opaque token string. The TTL header sets how long the token is valid, from 1 to 21600 seconds (6 hours). The second call presents the token and returns the instance ID.
Why this blocks typical SSRF
Several properties of the design defend against forwarded requests:
- A PUT is required. Most SSRF bugs only let an attacker control a
GETURL. Without the ability to send aPUTwith a custom header, the attacker cannot obtain a token. - Custom headers are required. A simple URL fetcher or an open redirect cannot add the TTL or token headers.
- Requests with
X-Forwarded-Forare rejected. The service refuses requests that carry this header. This is a sign that a proxy forwarded the call. - The token response has a hop limit. The IP time-to-live (TTL) of the response packet defaults to 1. The packet cannot cross a network hop, so a token cannot be delivered through a router or proxy off the instance.
Comparing the two versions
| Row | IMDSv1 | IMDSv2 |
|---|---|---|
| Request style | Plain `GET` | `PUT` for a token, then `GET` with a header |
| Session token | None | Required, valid for 1 second to 6 hours |
| Resists simple SSRF | No | Yes |
| Setting | `HttpTokens=optional` | `HttpTokens=required` |
With HttpTokens=optional, an instance accepts both versions. Only required turns v1 off.
Requiring IMDSv2
Require IMDSv2 at launch with --metadata-options, as in the launch command from the earlier chapter:
aws ec2 run-instances \
--image-id "$AMI_ID" \
--instance-type t3.micro \
--metadata-options HttpTokens=required \
--subnet-id subnet-0123456789abcdef0You can also change a running instance without a restart:
aws ec2 modify-instance-metadata-options \
--instance-id i-0123456789abcdef0 \
--http-tokens required \
--http-endpoint enabledTo audit which instances in a Region still allow v1:
aws ec2 describe-instances \
--query 'Reservations[].Instances[].[InstanceId,MetadataOptions.HttpTokens]' \
--output tableAny row showing optional accepts IMDSv1. Amazon Linux 2023 AMIs are set up to require IMDSv2 by default. You can also set an account-level default per Region with aws ec2 modify-instance-metadata-defaults, so that new launches start with required.
Containers and the hop limit
The default hop limit of 1 has a side effect. A container using bridge networking on the instance adds a network hop, so the token response never reaches it. Calls from inside the container then time out.
If containers on the instance legitimately need metadata, raise the limit to 2:
aws ec2 modify-instance-metadata-options \
--instance-id i-0123456789abcdef0 \
--http-put-response-hop-limit 2Do this only when needed. A higher limit lets the token travel one more hop, so it weakens one of the protections. Containers that do not need instance credentials should not be able to reach the IMDS at all.
Other settings and limits
- Disable the service.
--http-endpoint disabledturns the IMDS off completely. Do this if nothing on the instance needs metadata or role credentials. - Tags in metadata.
--instance-metadata-tags enabledexposes instance tags under/latest/meta-data/tags/instance. It is off by default. - Role credentials are temporary. The IMDS returns an access key, a secret key, a session token, and an expiry time. AWS rotates them before they expire, so applications should re-read them rather than cache them forever. The SDKs do this for you.
Common errors
- A 401 response. The token is missing, expired, or malformed. Request a new one.
- A timeout from inside a container. The hop limit is 1. Raise it to 2 if the container needs metadata.
- A 403 response on
PUT. The request probably carried anX-Forwarded-Forheader. - An application cannot find credentials after enforcing v2. The SDK or agent is too old. Upgrade it.
- Running the commands off the instance.
169.254.169.254is only reachable from the instance. From your laptop the request hangs.
Exercise
An application on your instance has an SSRF bug that lets attackers make it issue arbitrary GET requests. The instance has HttpTokens=required. Why can the attacker not read the role credentials through this bug, and what would still let them?
Show answer
With IMDSv2 required, a GET without a token returns 401, and the attacker cannot obtain a token because that needs a PUT request with a custom header, which a GET-only bug cannot send. They could still read the credentials if they gained code execution on the instance, because any local process can request a token. The role's permissions should therefore be kept minimal.
Exercise
A containerized app on an instance with HttpTokens=required times out when it calls the metadata service. The same call works from the host shell. What is the likely cause and the fix?
Show answer
The IMDSv2 token response has an IP hop limit of 1 by default, and the container's bridge network adds a hop, so the response is dropped. Set the hop limit to 2 with aws ec2 modify-instance-metadata-options --http-put-response-hop-limit 2, and only do so if the container actually needs metadata.