How the attack works
User data is the text an EC2 instance receives at launch: a shell script or cloud-init directives that configure the machine at boot, with the highest rights on the host. It is an attribute of the instance and can be replaced later. AWS documents the conditions: the instance has to be stopped first, the new user data is visible to the instance after the next start, and by default user data scripts and cloud-init directives run only during the boot cycle of the first launch. AWS also documents that the configuration can be written so that they run at every restart.
That makes user data a way to run commands on an instance with nothing but EC2 API permissions. An attacker who controls a principal that may stop and start instances and call ModifyInstanceAttribute puts a script into the user data that is marked to run at every boot. On the next start it executes as root, where it can read the temporary credentials of the instance role from the instance metadata, add accounts or keys to the host, or install something that stays. No SSH key, no Systems Manager permission and no network path to the instance is needed, and the script stays in the attribute and runs again on later restarts until someone removes it.
EC2 logs all of its API actions as CloudTrail management events under ec2.amazonaws.com. The change is a ModifyInstanceAttribute call that carries the user data, usually between a StopInstances and a StartInstances call for the same instance. The rule reads the attribute from the request field that the API parameter UserData becomes: AWS publishes no example record for this call, and the rule says so. Published lists of AWS privilege escalation and persistence methods describe the technique. No incident report was used for this page.
What an attacker needs
- Credentials for a principal allowed ec2:ModifyInstanceAttribute on the instance, together with ec2:StopInstances and ec2:StartInstances, or the patience to wait for the next planned restart.
- An instance whose operating system processes user data at boot, and whose role or data is worth reaching.
Impact
Code execution as root on the instance at its next start, and again on later starts while the user data stays in place. The attacker reaches the temporary credentials of the instance role, the data on the volumes, and everything the host can reach on the network. Stopping the instance also interrupts the workload and loses data on instance store volumes.
Detection rules
EC2 Instance User Data Modified
Detects ModifyInstanceAttribute calls to Amazon EC2 that set the user data of an instance. User data is processed by the instance at boot with the highest rights on the host, and AWS documents that it can be replaced on a stopped instance and that it can be written to run at every restart. A principal that may change it and restart the instance therefore runs code on the host and reaches the credentials of its instance role. EC2 logs all of its API actions as management events. The attribute is read from requestParameters.userData, which follows the UserData request parameter of the EC2 API reference: AWS publishes no example record for this call, so confirm the field in your own events. The level is one step lower than the impact suggests for that reason. Read the instance ID from the request and look for a stop before and a start after the change. It also matches attempts that failed (errorCode present): check errorCode, because an unauthorized attempt is worth seeing.
title: EC2 Instance User Data Modified
id: b36e0aa2-51df-48b1-b4bd-20e5e4b86bac
status: experimental
description: >-
Detects ModifyInstanceAttribute calls to Amazon EC2 that set the user data
of an instance. User data is processed by the instance at boot with the
highest rights on the host, and AWS documents that it can be replaced on
a stopped instance and that it can be written to run at every restart. A
principal that may change it and restart the instance therefore runs code
on the host and reaches the credentials of its instance role. EC2 logs
all of its API actions as management events. The attribute is read from
requestParameters.userData, which follows the UserData request parameter
of the EC2 API reference: AWS publishes no example record for this call,
so confirm the field in your own events. The level is one step lower than
the impact suggests for that reason. Read the instance ID from the
request and look for a stop before and a start after the change.
It also matches attempts that failed (errorCode present): check errorCode,
because an unauthorized attempt is worth seeing.
references:
- https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
- https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_ModifyInstanceAttribute.html
- https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/monitor-with-cloudtrail.html
- https://attack.mitre.org/techniques/T1037/
author: Forestall
date: 2026-11-18
tags:
- attack.persistence
- attack.privilege-escalation
- attack.t1037
logsource:
product: aws
service: cloudtrail
detection:
selection:
eventSource: ec2.amazonaws.com
eventName: ModifyInstanceAttribute
requestParameters.userData|exists: true
condition: selection
falsepositives:
- Administrators who repair a single instance through its user data, and tools that manage long-lived instances by rewriting their user data. Confirm each event with the owner of the instance, and add such tools to a filter_* selection on userIdentity.arn.
level: medium
False positives
- Administrators who repair a single instance through its user data, and tools that manage long-lived instances by rewriting their user data. Confirm each event with the owner of the instance, and add such tools to a filter_* selection on userIdentity.arn.
Detection logic
Patterns that need more than one event, a baseline or knowledge of your own environment. Each is a hypothesis to build and tune in your SIEM, not a ready rule.
Instance stopped, user data changed and instance started by the same principal
A user data change only takes effect at the next start, and it needs a stopped instance. Someone who wants the script to run now performs all three steps in a row, and that short sequence on one instance is the whole attack.
- A
StopInstancesevent fromec2.amazonaws.com. Note the instance ID and the caller inuserIdentity.arn. - Within 60 minutes a
ModifyInstanceAttributeevent by the same caller that carries user data for the same instance. - Then a
StartInstancesevent for that instance. Alert on the full sequence, and check the activity of the instance role afterwards.
EventsStopInstancesModifyInstanceAttributeStartInstances
LimitsAn administrator who repairs a single instance through user data performs the same steps. An attacker who changes the user data of an instance that is already stopped, and waits for a planned start, produces no close sequence. The 60 minutes are a starting point.
Triage
- Identify the caller (userIdentity.arn), the source IP address and the user agent. Is it a deployment or platform role that manages this instance?
- Read the instance ID of the request, and look for StopInstances before and StartInstances after the change by the same caller.
- Read the current user data of the instance with the EC2 API and check what it does, and whether it is marked to run at every boot.
- Check what the instance role did in CloudTrail after the instance started again, and from which source address.
- Compare the user data with the launch template or the infrastructure code that created the instance.
Response
Once the attack is confirmed:
- Isolate the instance, keep a snapshot of its volumes and a copy of the user data for review, and replace it with a fresh instance from your pipeline.
- Revoke the active sessions of the instance role, and rotate the secrets stored on or readable from the host.
- Contain the principal that changed the user data, and take away its right to modify instance attributes.
- Review CloudTrail for what the instance role did after the restart, and remove what was created.
- Check other instances for user data changes by the same principal.
Mitigation
- Allow ec2:ModifyInstanceAttribute only to deployment and platform roles, and deny it to everyone else with a service control policy.
- Treat permission to stop, modify and start an instance as permission to run code on it, and scope it by instance tags.
- Replace instances through your pipeline when their configuration has to change, so that a user data change on an existing instance is always unexpected.
- Keep instance roles least-privilege, because every script that runs on the host can use the role.
Frequently asked questions
Does changed user data run by itself?
Not by default. AWS documents that user data scripts and cloud-init directives run only during the boot cycle of the first launch, and that after an update the new user data is visible to the instance but scripts are not run. AWS also documents that the configuration can be written so that they run at every restart, and that is what an attacker relies on.
Why does the instance have to be stopped?
AWS documents that the user data of a running instance can be viewed but not modified, so it has to be stopped first. This is why the change usually sits between a `StopInstances` and a `StartInstances` event for the same instance. An unexpected stop of a production instance is a second signal.
Can I read the script from CloudTrail?
Do not rely on it. The event shows who changed the attribute and on which instance. Read the current user data of the instance through the EC2 API instead, and keep a copy before you replace the instance, because it is the evidence of what was run.
Which legitimate activity produces the event?
Very little. Most teams set user data once, in a launch template, and replace instances when it has to change. Administrators sometimes edit the user data of a single instance to repair it, for example to regain access to a host. Each such event can be confirmed with the owner of the instance.