Enhance AWS Account Security: Enforcing IMDSv2 for Launching EC2 Instances Safely

Akesh Patil
5 min readFeb 10, 2024

--

In recent years, the importance of securing AWS accounts has become increasingly evident. As more and more organizations migrate their workloads to the cloud, it is essential to ensure that sensitive data and resources are adequately protected. One critical aspect of this is safeguarding against potential vulnerabilities in the EC2 instance metadata service, particularly version 1. In this blog post, we will explore the significance of protecting AWS accounts and discuss best practices to enforce launching EC2 instances using the metadata service version 2.

Understanding the EC2 Metadata and Metadata Service

Instance metadata is data about your instance that you can use to configure or manage the running instance. Instance metadata is divided into categories, for example, host name, events, and security groups.

You can access instance metadata from a running instance using one of the following methods:

  • Instance Metadata Service Version 1 (IMDSv1) — a request/response method
  • Instance Metadata Service Version 2 (IMDSv2) — a session-oriented method

By default, you can use either IMDSv1 or IMDSv2, or both.

The EC2 metadata service plays a crucial role in enabling EC2 instances to retrieve information about themselves, such as instance type, public IP address and security group settings and the most importantly short term credentials using IAM roles associated . This information is accessible from within the instance, allowing applications and services running on the instance to dynamically adapt to their environment.

The metadata service is available to any code running on the instance and it can be accessed using a simple HTTP request to a well-known endpoint. While this functionality is incredibly useful for legitimate purposes, it also introduces potential security risks if not properly secured.

The Risks of Unprotected Metadata Service Version 1

One of the key vulnerabilities associated with the EC2 metadata service version 1 (IMDSv1) is the potential for unauthorized access. If an attacker gains access to the instance, they can use the metadata service to retrieve sensitive information, such as security credentials or instance metadata. This could lead to unauthorized access to other AWS resources and compromise the security of the entire account.

Furthermore, certain types of attacks, such as Server-Side Request Forgery (SSRF), can be leveraged to exploit the metadata service and exfiltrate sensitive data. This underscores the critical importance of securing the metadata service to prevent such attacks.

Best Practices for Instance Metadata Service

To mitigate the risks associated with the EC2 metadata service version 1 (IMDSv1), there are several best practices that organizations should implement:

1. Use IAM Roles: Instead of storing access keys and secret keys directly on the instance, leverage IAM roles to grant permissions to EC2 instances. IAM roles provide temporary security credentials that are automatically rotated, reducing the risk of credential exposure.

2. Upgrade to Metadata Service Version 2 (IMDSv2): AWS introduced version 2 of the EC2 metadata service, which provides enhanced security features, including the use of session-based tokens for authentication. Migrating to version 2 can significantly improve the security posture of EC2 instances.

3. Monitor Access Patterns: Implement robust logging and monitoring to track access to the metadata service. By analyzing access patterns and detecting anomalous behavior, organizations can identify potential security threats and take proactive measures to address them.

Enforce AWS accounts to launch EC2 instances with only Medata Service Version 2 (IMDSv2)

In today’s cloud computing environment, it is essential for organizations to have control over their AWS accounts to ensure compliance and security. AWS Service Control Policies (SCPs) provide a powerful tool for managing permissions and access across multiple AWS accounts. By using SCPs, organizations can establish guardrails to enforce policies and prevent unauthorized actions within their AWS environments.

SCPs allow administrators to set guardrails at the organization level, ensuring that all accounts and resources within the organization adhere to the defined policies. This centralized approach simplifies the management of permissions and reduces the risk of policy violations. With SCPs, organizations can define which services and actions are allowed or denied across their AWS accounts, providing a consistent and secure environment for their workloads.

Most of the organizations today use multi-account strategy using AWS Landing Zone and AWS Organizations is one of the core component in building landing zone. To enforce all AWS accounts to launch EC2 instances with IMDSv2, following SCP can be enforced at root level. With this SCP, EC2 launch operations will be failed if it is launched with Instance Metadata Service version 1 (IMDSv1)

{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:RunInstances"
],
"Resource": [
"arn:aws:ec2:*:*:instance/*"
],
"Effect": "Deny",
"Condition": {
"StringNotEquals": {
"ec2:MetadataHttpTokens": "required"
}
}
}
]
}

With above SCP, we can enforce new EC2 instances to be launched with IMDSv2 however how about existing instances in the account, which are already using IMDSv1 and you would like to migrate them to use IMDSv2. So here are the steps below

Identifying EC2 instance using IMDSv1

For existing EC2 instances, the first step before transitioning instance to use IMDSv2 is to identity all such instances which are using IMDSv1. We can do this in multiples ways, like using AWS Console or AWS CLI however the efficient way to do this is with AWS Config service or AWS Security Hub

Using AWS Config

The AWS Config rule ec2-imdsv2-check checks whether your Amazon EC2 instance metadata version is configured with IMDSv2. The rule is NON_COMPLIANT if the HttpTokens is set to optional, which means the EC2 instance has IMDSv1 enabled.

Using Security Hub

AWS Security Hub provides detection and alerting capability at the account and organization levels. Security Hub has an Amazon EC2 control ([EC2.8] Amazon EC2 instances should use Instance Metadata Service Version 2 (IMDSv2)) that uses the AWS Config rule ec2-imdsv2-check to check if the instance metadata version is configured with IMDSv2. The rule is NON_COMPLIANT if the HttpTokens is set to optional, which means EC2 instance has IMDSv1 enabled.

Transition to using Instance Metadata Service Version 2

Once you identity all such servers using IMDSv1, you can follow steps provided in the following documentation to transition using IMDSv2.

https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/instance-metadata-transition-to-version-2.html

Conclusion

Protecting AWS accounts from potential vulnerabilities associated with the EC2 metadata service version 1 (IMDSv1) is paramount for ensuring a robust security posture in the cloud.

As organizations continue to embrace cloud technologies, it is imperative to prioritize security measures that safeguard against evolving threats. By taking proactive steps to secure EC2 instances and the associated metadata service, organizations can confidently harness the scalability and flexibility of AWS while maintaining a strong security posture.

In conclusion, in this blog we learned how we can make use of Service Control Policies (SCPs) in AWS Organizations to protect AWS accounts to launch EC2 instances using only metadata service version 2 (IMDSv2) and deny all EC2 launch operations with metadata service version 1 (IMDSv1). Additionally, we also explored how to identity instances using IMDSv1 and transition from using IMDSv1 to IMDSv2.

--

--

Akesh Patil

I am a proud AWS Ambassador working as a Sr. Cloud Architect & expertise in Full Stack Development, Cloud Consulting & Solutioning, DevOps & Infra Automations.