top of page

Securing AWS EC2 Instances with IMDSv2

Writer's picture: KaterynaKateryna
AWS ec2 IMDSv2
AWS ec2 IMDSv2

AWS EC2 Instances are a backbone of modern cloud computing, providing the flexibility to deploy scalable applications and services. However, with this flexibility comes the responsibility to ensure security, especially when accessing sensitive instance metadata. One of the most critical upgrades you can make is disabling Instance Metadata Service version 1 (IMDSv1) in favor of IMDSv2. This blog will walk you through what IMDSv1 and IMDSv2 are, why disabling IMDSv1 is essential, and how to implement IMDSv2.


What is the Instance Metadata Service (IMDS)?


The Instance Metadata Service provides information about the EC2 instance, such as:

  • IAM role credentials.

  • Instance ID and type.

  • Public and private IP addresses.

  • Security group details.

This service is accessed via a local endpoint: http://169.254.169.254/latest/meta-data/.


IMDSv1


IMDSv1 is a simple, unauthenticated service that uses HTTP requests to fetch metadata. While convenient, it has notable security vulnerabilities, particularly in mitigating Server-Side Request Forgery (SSRF) attacks.


IMDSv2

IMDSv2 introduces session-oriented access to the metadata service. A client must first obtain a session token, which is then used to make authenticated requests. This mechanism enhances security and reduces the risk of unauthorized access.


Why Disable IMDSv1?


1. Protection Against SSRF Attacks

In an SSRF attack, an attacker tricks an application into making unauthorized requests to internal services, such as IMDS. IMDSv1 is particularly vulnerable because it lacks authentication. IMDSv2 mitigates this by requiring session tokens.


2. Prevention of Credential Leakage

IAM role credentials accessed via IMDSv1 can be exposed if a malicious actor gains access to an unprotected application or service. IMDSv2’s token-based system ensures only authorized requests are processed.


3. Enforcement of Modern Security Standards

Disabling IMDSv1 and adopting IMDSv2 aligns with AWS’s security best practices, ensuring that your infrastructure complies with modern security requirements.


How IMDSv2 Works


IMDSv2 requires two steps to fetch metadata:

  1. Obtain a Session Token: A PUT request is made to the metadata service to generate a session token. The client specifies a time-to-live (TTL) for the session.

  2. Use the Token for Metadata Requests: All subsequent requests must include the session token in the header.


Example Workflow

# Step 1: Obtain a session token
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" \
       -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")

# Step 2: Fetch metadata using the token
curl -H "X-aws-ec2-metadata-token: $TOKEN" \
     http://169.254.169.254/latest/meta-data/

Steps to Disable IMDSv1


1. Test IMDSv2 Compatibility

Before disabling IMDSv1, ensure your applications and scripts are compatible with IMDSv2. Use the example workflow above to validate access.


2. Modify Metadata Options for Existing Instances

You can disable IMDSv1 and enforce IMDSv2 using the AWS CLI:

aws ec2 modify-instance-metadata-options \
    --instance-id <instance-id> \
    --http-endpoint enabled \
    --http-tokens required
  • --http-endpoint enabled: Keeps the metadata service accessible.

  • --http-tokens required: Ensures all metadata requests use IMDSv2.


3. Enforce IMDSv2 for New Instances

When launching new EC2 instances, set metadata options to enforce IMDSv2:

aws ec2 run-instances \
    --instance-type <instance-type> \
    --image-id <ami-id> \
    --metadata-options "HttpTokens=required"

4. Update Applications and Scripts

Ensure any application or script using the metadata service includes support for session tokens. For example, in Python, you can use the requests library to include the token in your HTTP headers.


Troubleshooting Common Issues


Error: Metadata Service Unreachable

  • Verify that the metadata endpoint is enabled (--http-endpoint enabled).

  • Ensure network access to 169.254.169.254 is not blocked.


Session Token Expiration

  • If a session token expires, you must request a new one. Consider automating this process in your application.


Key Benefits of IMDSv2


  1. Enhanced Security: Session tokens protect against unauthorized access.

  2. Reduced Attack Surface: Limits the exposure of sensitive metadata.

  3. Compliance-Friendly: Meets modern security and compliance requirements.


Final Thoughts

Disabling IMDSv1 and adopting IMDSv2 is a crucial step in securing your EC2 instances. By enforcing token-based metadata access, you can protect sensitive credentials and align your infrastructure with AWS’s security best practices. Take the time to audit your current EC2 instances and applications, test IMDSv2 compatibility, and update your configurations to enhance your cloud security posture.

For more details, consult the AWS IMDSv2 documentation.

9 views0 comments

Comments


Contact Us

bottom of page