Description
Check whether the PEM data in EC2 user data is a private key, public key or certificate. Anyone who can read a Base64-encoded private key can decode it.
Potential impact
An exposed private key can be used for authentication or signing wherever it is still trusted.
Remediation
Remove private keys from user data. If the application needs a secret, let its instance role read just that secret from AWS Secrets Manager. Replace an exposed key, remove trust in the old key and check existing instances, launch configurations and template versions for copies.
Examples
amazon.aws.ec2_launch_template accepts Base64-encoded user_data. Supply ami_id, security_group_ids and an existing key pair named default.
Before
- name: launch template with illustrative encoded key material
amazon.aws.ec2_launch_template:
name: special
image_id: "{{ ami_id }}"
key_name: default
security_group_ids: "{{ security_group_ids }}"
instance_type: t3.micro
user_data: LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpzb21lS2V5
The first value illustrates a private-key header.
After
- name: launch template without embedded private key
amazon.aws.ec2_launch_template:
name: special
image_id: "{{ ami_id }}"
key_name: default
security_group_ids: "{{ security_group_ids }}"
instance_type: t3.micro
user_data: IyEvYmluL3NoCmV4aXQgMAo=
The second value is a startup script that exits with exit 0. Configure the application to retrieve any secrets it needs.
References
- CWE-326
- Ansible community.aws.ec2_lc documentation
- community.aws 1.5.0 ec2_lc implementation
- botocore 1.20.106 user-data encoding
- CreateLaunchConfiguration API
- Current autoscaling_launch_config module
- EC2 metadata and user-data access
- Launch configuration limits and migration to launch templates
- Secrets Manager best practices
- RFC 7468: Textual encodings of certificates and keys
- Ansible EC2 launch template module