AWS EC2 User Data Retrieval for EC2 Instance
Identifies discovery request DescribeInstanceAttribute
with the attribute userData and instanceId in AWS CloudTrail
logs. This may indicate an attempt to retrieve user data from an EC2 instance. Adversaries may use this information to
gather sensitive data from the instance such as hardcoded credentials or to identify potential vulnerabilities. This is
a New Terms rule that
identifies when aws.cloudtrail.user_identity.arn
requests the user data for a specific
aws.cloudtrail.flattened.request_parameters.instanceId
from an EC2 instance in the last 14 days.
Elastic rule (View on GitHub)
1[metadata]
2creation_date = "2024/04/14"
3integration = ["aws"]
4maturity = "production"
5updated_date = "2025/02/03"
6
7[rule]
8author = ["Elastic"]
9description = """
10Identifies discovery request `DescribeInstanceAttribute` with the attribute userData and instanceId in AWS CloudTrail
11logs. This may indicate an attempt to retrieve user data from an EC2 instance. Adversaries may use this information to
12gather sensitive data from the instance such as hardcoded credentials or to identify potential vulnerabilities. This is
13a [New Terms](https://www.elastic.co/guide/en/security/current/rules-ui-create.html#create-new-terms-rule) rule that
14identifies when `aws.cloudtrail.user_identity.arn` requests the user data for a specific
15`aws.cloudtrail.flattened.request_parameters.instanceId` from an EC2 instance in the last 14 days.
16"""
17from = "now-9m"
18index = ["filebeat-*", "logs-aws.cloudtrail-*"]
19language = "kuery"
20license = "Elastic License v2"
21name = "AWS EC2 User Data Retrieval for EC2 Instance"
22note = """
23## Triage and Analysis
24
25### Investigating AWS EC2 User Data Retrieval for EC2 Instance
26
27This rule detects requests to retrieve the `userData` attribute of an EC2 instance using the `DescribeInstanceAttribute` API action. The `userData` field can contain sensitive information, such as hardcoded credentials or configuration scripts, that adversaries may exploit for further attacks.
28
29#### Possible Investigation Steps
30
31- **Identify the Target Instance**:
32 - **Instance ID**: Review the `aws.cloudtrail.flattened.request_parameters.instanceId` field to identify the EC2 instance targeted by the request. Confirm whether this instance should expose its `userData` and whether it is associated with sensitive workloads.
33 - **Analyze userData**: If possible, retrieve and inspect the `userData` field to identify sensitive information like hardcoded credentials or configuration scripts.
34
35- **Review User Context**:
36 - **User Identity**: Inspect the `aws.cloudtrail.user_identity.arn` field to identify the user or role that executed the `DescribeInstanceAttribute` action. Investigate whether this user typically performs such actions.
37 - **Access Patterns**: Validate whether the user or role has the necessary permissions and whether the frequency of this action aligns with expected behavior.
38 - **Access Key ID**: Check the `aws.cloudtrail.user_identity.access_key_id` field to determine the key used to make the request as it may be compromised.
39
40- **Analyze Request Details**:
41 - **Parameters**: Verify that the `attribute=userData` parameter was explicitly requested. This indicates intentional access to user data.
42 - **Source IP and Geolocation**: Check the `source.address` and `source.geo` fields to validate whether the request originated from a trusted location or network. Unexpected geolocations can indicate adversarial activity.
43
44- **Review Source Tool**:
45 - **User Agent**: Inspect the `user_agent.original` field to determine the tool or client used (e.g., Terraform, AWS CLI). Legitimate automation tools may trigger this activity, but custom or unknown user agents may indicate malicious intent.
46
47- **Check for Related Activity**:
48 - **IAM Changes**: Correlate this event with any IAM changes or temporary credential creation to identify potential privilege escalation attempts.
49 - **API Usage**: Look for other unusual API calls (e.g., `RunInstances`, `GetObject`, `AssumeRole`) by the same user or IP to detect lateral movement or data exfiltration attempts.
50
51- **Validate Intent**:
52 - **Permissions and Justification**: Ensure that the user has the least privilege required to perform this action. Investigate whether there is a valid reason for accessing the `userData` field.
53
54### False Positive Analysis
55
56- **Automation**: This event is often triggered by legitimate automation tools, such as Terraform or custom scripts, that require access to `userData` during instance initialization.
57- **Maintenance Activity**: Verify whether this event aligns with expected administrative activities, such as debugging or instance configuration updates.
58
59### Response and Remediation
60
61- **Revoke Excessive Permissions**: If unauthorized, immediately remove `DescribeInstanceAttribute` permissions from the user or role.
62- **Quarantine the Target Instance**: If malicious behavior is confirmed, isolate the affected EC2 instance to limit further exposure.
63- **Secure User Data**:
64 - Avoid storing sensitive information, such as credentials, in `userData`. Use AWS Secrets Manager or Parameter Store instead.
65 - Encrypt user data and ensure only authorized users can decrypt it.
66- **Audit IAM Policies**: Regularly review IAM policies to ensure they adhere to the principle of least privilege.
67- **Monitor and Detect**: Set up additional alerts for unexpected `DescribeInstanceAttribute` calls or other suspicious API activity.
68
69### Additional Information
70
71For more details on managing EC2 user data securely, refer to the [AWS EC2 User Data Documentation](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html).
72"""
73references = [
74 "https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeInstanceAttribute.html",
75 "https://hackingthe.cloud/aws/exploitation/local_ec2_priv_esc_through_user_data",
76]
77risk_score = 47
78rule_id = "c1e79a70-fa6f-11ee-8bc8-f661ea17fbce"
79severity = "medium"
80tags = [
81 "Domain: Cloud",
82 "Data Source: AWS",
83 "Data Source: Amazon Web Services",
84 "Data Source: Amazon EC2",
85 "Resources: Investigation Guide",
86 "Use Case: Log Auditing",
87 "Tactic: Discovery",
88]
89timestamp_override = "event.ingested"
90type = "new_terms"
91
92query = '''
93event.dataset: "aws.cloudtrail"
94 and event.provider: "ec2.amazonaws.com"
95 and event.action: "DescribeInstanceAttribute"
96 and event.outcome: "success"
97 and aws.cloudtrail.request_parameters: (*attribute=userData* and *instanceId*)
98 and not aws.cloudtrail.user_identity.invoked_by: (
99 "AWS Internal" or
100 "cloudformation.amazonaws.com"
101 )
102'''
103
104
105[[rule.threat]]
106framework = "MITRE ATT&CK"
107[[rule.threat.technique]]
108id = "T1580"
109name = "Cloud Infrastructure Discovery"
110reference = "https://attack.mitre.org/techniques/T1580/"
111
112
113[rule.threat.tactic]
114id = "TA0007"
115name = "Discovery"
116reference = "https://attack.mitre.org/tactics/TA0007/"
117[[rule.threat]]
118framework = "MITRE ATT&CK"
119[[rule.threat.technique]]
120id = "T1552"
121name = "Unsecured Credentials"
122reference = "https://attack.mitre.org/techniques/T1552/"
123[[rule.threat.technique.subtechnique]]
124id = "T1552.005"
125name = "Cloud Instance Metadata API"
126reference = "https://attack.mitre.org/techniques/T1552/005/"
127
128
129
130[rule.threat.tactic]
131id = "TA0006"
132name = "Credential Access"
133reference = "https://attack.mitre.org/tactics/TA0006/"
134
135[rule.new_terms]
136field = "new_terms_fields"
137value = ["aws.cloudtrail.user_identity.arn", "aws.cloudtrail.flattened.request_parameters.instanceId"]
138[[rule.new_terms.history_window_start]]
139field = "history_window_start"
140value = "now-14d"
Triage and Analysis
Investigating AWS EC2 User Data Retrieval for EC2 Instance
This rule detects requests to retrieve the userData
attribute of an EC2 instance using the DescribeInstanceAttribute
API action. The userData
field can contain sensitive information, such as hardcoded credentials or configuration scripts, that adversaries may exploit for further attacks.
Possible Investigation Steps
-
Identify the Target Instance:
- Instance ID: Review the
aws.cloudtrail.flattened.request_parameters.instanceId
field to identify the EC2 instance targeted by the request. Confirm whether this instance should expose itsuserData
and whether it is associated with sensitive workloads. - Analyze userData: If possible, retrieve and inspect the
userData
field to identify sensitive information like hardcoded credentials or configuration scripts.
- Instance ID: Review the
-
Review User Context:
- User Identity: Inspect the
aws.cloudtrail.user_identity.arn
field to identify the user or role that executed theDescribeInstanceAttribute
action. Investigate whether this user typically performs such actions. - Access Patterns: Validate whether the user or role has the necessary permissions and whether the frequency of this action aligns with expected behavior.
- Access Key ID: Check the
aws.cloudtrail.user_identity.access_key_id
field to determine the key used to make the request as it may be compromised.
- User Identity: Inspect the
-
Analyze Request Details:
- Parameters: Verify that the
attribute=userData
parameter was explicitly requested. This indicates intentional access to user data. - Source IP and Geolocation: Check the
source.address
andsource.geo
fields to validate whether the request originated from a trusted location or network. Unexpected geolocations can indicate adversarial activity.
- Parameters: Verify that the
-
Review Source Tool:
- User Agent: Inspect the
user_agent.original
field to determine the tool or client used (e.g., Terraform, AWS CLI). Legitimate automation tools may trigger this activity, but custom or unknown user agents may indicate malicious intent.
- User Agent: Inspect the
-
Check for Related Activity:
- IAM Changes: Correlate this event with any IAM changes or temporary credential creation to identify potential privilege escalation attempts.
- API Usage: Look for other unusual API calls (e.g.,
RunInstances
,GetObject
,AssumeRole
) by the same user or IP to detect lateral movement or data exfiltration attempts.
-
Validate Intent:
- Permissions and Justification: Ensure that the user has the least privilege required to perform this action. Investigate whether there is a valid reason for accessing the
userData
field.
- Permissions and Justification: Ensure that the user has the least privilege required to perform this action. Investigate whether there is a valid reason for accessing the
False Positive Analysis
- Automation: This event is often triggered by legitimate automation tools, such as Terraform or custom scripts, that require access to
userData
during instance initialization. - Maintenance Activity: Verify whether this event aligns with expected administrative activities, such as debugging or instance configuration updates.
Response and Remediation
- Revoke Excessive Permissions: If unauthorized, immediately remove
DescribeInstanceAttribute
permissions from the user or role. - Quarantine the Target Instance: If malicious behavior is confirmed, isolate the affected EC2 instance to limit further exposure.
- Secure User Data:
- Avoid storing sensitive information, such as credentials, in
userData
. Use AWS Secrets Manager or Parameter Store instead. - Encrypt user data and ensure only authorized users can decrypt it.
- Avoid storing sensitive information, such as credentials, in
- Audit IAM Policies: Regularly review IAM policies to ensure they adhere to the principle of least privilege.
- Monitor and Detect: Set up additional alerts for unexpected
DescribeInstanceAttribute
calls or other suspicious API activity.
Additional Information
For more details on managing EC2 user data securely, refer to the AWS EC2 User Data Documentation.
References
Related rules
- AWS STS GetCallerIdentity API Called for the First Time
- AWS SSM `SendCommand` Execution by Rare User
- AWS CloudTrail Log Created
- AWS EC2 Admin Credential Fetch via Assumed Role
- AWS KMS Customer Managed Key Disabled or Scheduled for Deletion