Skip to content

Commit a92fdc1

Browse files
[New Rule] Adding Coverage for AWS IAM Customer-Managed Policy Attached to Role by Rare User (#4245)
* adding new rule 'AWS IAM Customer-Managed Policy Attached to Role by Rare User' * adding investigation guide tag * adds new hunting query * updated notes * changed name * adjusting pyproject.toml version
1 parent 6a39009 commit a92fdc1

File tree

7 files changed

+211
-1
lines changed

7 files changed

+211
-1
lines changed

detection_rules/etc/non-ecs-schema.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@
151151
"aws.cloudtrail.flattened.request_parameters.cidrIp": "keyword",
152152
"aws.cloudtrail.flattened.request_parameters.fromPort": "keyword",
153153
"aws.cloudtrail.flattened.request_parameters.roleArn": "keyword",
154+
"aws.cloudtrail.flattened.request_parameters.roleName": "keyword",
155+
"aws.cloudtrail.flattened.request_paramters.policyArn": "keyword",
154156
"aws.cloudtrail.flattened.request_parameters.serialNumber": "keyword"
155157
},
156158
"logs-azure.signinlogs-*": {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# AWS IAM Customer-Managed Policy Attachment for Privilege Escalation
2+
3+
---
4+
5+
## Metadata
6+
7+
- **Author:** Elastic
8+
- **Description:** This hunting query identifies instances where customer-managed IAM policies are attached to existing roles, potentially indicating a privilege escalation attempt. By detecting unexpected actors attaching customer-managed policies with elevated permissions to roles, this query helps identify potential abuse or misuse within AWS. Adversaries may attach these policies to gain unauthorized permissions or enable lateral movement and persistence within the environment.
9+
10+
- **UUID:** `418baaf2-9ae1-11ef-be63-f661ea17fbcd`
11+
- **Integration:** [aws.cloudtrail](https://docs.elastic.co/integrations/aws/cloudtrail)
12+
- **Language:** `[ES|QL]`
13+
- **Source File:** [AWS IAM Customer-Managed Policy Attachment for Privilege Escalation](../queries/iam_customer_managed_policies_attached_to_existing_roles.toml)
14+
15+
## Query
16+
17+
```sql
18+
from logs-aws.cloudtrail*
19+
| where
20+
event.dataset == "aws.cloudtrail"
21+
and event.provider == "iam.amazonaws.com"
22+
and event.action == "AttachRolePolicy"
23+
and event.outcome == "success"
24+
| dissect aws.cloudtrail.request_parameters "{%{}::%{owner}:%{?policy_key}/%{attached_policy_name}, %{?role_name_key}=%{target_role_name}}"
25+
| where owner != "aws"
26+
| stats
27+
actor_attaching_role_count = count(*) by aws.cloudtrail.user_identity.arn, attached_policy_name, target_role_name
28+
```
29+
30+
## Notes
31+
32+
- Review the `target_account_id` field to verify the AWS account in which the role is being modified, especially if this account is outside of your organization’s typical accounts.
33+
- Examine `aws.cloudtrail.request_parameters` for details on the role and attached policy. Customer-managed policies granting overly permissive access, such as `AdministratorAccess`, may signal unauthorized privilege escalation.
34+
- Cross-reference `event.action` values where `AttachRolePolicy` appears to further investigate attached policies that could enable lateral movement or persistence.
35+
- Evaluate `aws.cloudtrail.user_identity.arn` to confirm if the actor attaching the policy has legitimate permissions for this action. Anomalous or unauthorized actors may indicate privilege abuse.
36+
- Look for patterns of multiple `AttachRolePolicy` actions across roles by the same user or entity. High frequency of these actions could suggest an attempt to establish persistent control across roles within your AWS environment.
37+
38+
## MITRE ATT&CK Techniques
39+
40+
- [T1548.005](https://attack.mitre.org/techniques/T1548/005)
41+
42+
## License
43+
44+
- `Elastic License v2`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[hunt]
2+
author = "Elastic"
3+
description = """
4+
This hunting query identifies instances where customer-managed IAM policies are attached to existing roles, potentially indicating a privilege escalation attempt. By detecting unexpected actors attaching customer-managed policies with elevated permissions to roles, this query helps identify potential abuse or misuse within AWS. Adversaries may attach these policies to gain unauthorized permissions or enable lateral movement and persistence within the environment.
5+
"""
6+
integration = ["aws.cloudtrail"]
7+
uuid = "418baaf2-9ae1-11ef-be63-f661ea17fbcd"
8+
name = "AWS IAM Customer-Managed Policy Attachment to Existing Roles"
9+
language = ["ES|QL"]
10+
license = "Elastic License v2"
11+
notes = [
12+
"Review the `attached_policy_name` and `target_role_name` fields to identify the customer-managed policy and role involved in the attachment.",
13+
"Review the permissions of the attached policy to determine the potential impact of the privilege escalation attempt.",
14+
"Review all entities that `target_role_name` may be attached to as these entities may have been compromised or misused.",
15+
"Consider reviewing the `aws.cloudtrail.user_identity.arn` field to identify the actor responsible for the privilege escalation attempt.",
16+
"Review the user agent of the actor to determine the source of the privilege escalation attempt, such as an AWS CLI or SDK.",
17+
]
18+
mitre = ['T1548.005']
19+
query = [
20+
'''
21+
from logs-aws.cloudtrail*
22+
| where
23+
event.dataset == "aws.cloudtrail"
24+
and event.provider == "iam.amazonaws.com"
25+
and event.action == "AttachRolePolicy"
26+
and event.outcome == "success"
27+
| dissect aws.cloudtrail.request_parameters "{%{}::%{owner}:%{?policy_key}/%{attached_policy_name}, %{?role_name_key}=%{target_role_name}}"
28+
| where owner != "aws"
29+
| stats
30+
actor_attaching_role_count = count(*) by aws.cloudtrail.user_identity.arn, attached_policy_name, target_role_name
31+
'''
32+
]

hunting/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Here are the queries currently available:
44

55

66
## aws
7+
- [AWS IAM Customer-Managed Policy Attachment for Privilege Escalation](./aws/docs/iam_customer_managed_policies_attached_to_existing_roles.md) (ES|QL)
78
- [EC2 Modify Instance Attribute User Data](./aws/docs/ec2_modify_instance_attribute_user_data.md) (ES|QL)
89
- [EC2 Suspicious Get User Password Request](./aws/docs/ec2_suspicious_get_user_password_request.md) (ES|QL)
910
- [High EC2 Instance Deployment Count Attempts by Single User or Role](./aws/docs/ec2_high_instance_deployment_count_attempts.md) (ES|QL)

hunting/index.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ aws:
364364
path: ./aws/queries/sts_suspicious_federated_temporary_credential_request.toml
365365
mitre:
366366
- T1550.001
367+
418baaf2-9ae1-11ef-be63-f661ea17fbcd:
368+
name: AWS IAM Customer-Managed Policy Attachment for Privilege Escalation
369+
path: ./aws/queries/iam_customer_managed_policies_attached_to_existing_roles.toml
370+
mitre:
371+
- T1548.005
367372
windows:
368373
44e6adc6-e183-4bfa-b06d-db41669641fa:
369374
name: Rundll32 Execution Aggregated by Command Line

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "detection_rules"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
description = "Detection Rules is the home for rules used by Elastic Security. This repository is used for the development, maintenance, testing, validation, and release of rules for Elastic Security’s Detection Engine."
55
readme = "README.md"
66
requires-python = ">=3.12"
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
[metadata]
2+
creation_date = "2024/11/04"
3+
integration = ["aws"]
4+
maturity = "production"
5+
updated_date = "2024/11/04"
6+
7+
[rule]
8+
author = ["Elastic"]
9+
description = """
10+
Detects when an AWS Identity and Access Management (IAM) customer-managed policy is attached to a role by an unusual or
11+
unauthorized user. Customer-managed policies are policies created and controlled within an AWS account, granting
12+
specific permissions to roles or users when attached. This rule identifies potential privilege escalation by flagging
13+
cases where a customer-managed policy is attached to a role by an unexpected actor, which could signal unauthorized
14+
access or misuse. Attackers may attach policies to roles to expand permissions and elevate their privileges within the
15+
AWS environment. This is a [New
16+
Terms](https://www.elastic.co/guide/en/security/current/rules-ui-create.html#create-new-terms-rule) rule that uses the
17+
`aws.cloudtrail.user_identity.arn` and `aws.cloudtrail.flattened.request_parameters.roleName` fields to check if the
18+
combination of the actor ARN and target role name has not been seen in the last 14 days.
19+
"""
20+
false_positives = [
21+
"""
22+
Legitimate IAM administrators may attach customer-managed policies to roles for various reasons, such as granting
23+
temporary permissions or updating existing policies. Ensure that the user attaching the policy is authorized to do
24+
so and that the action is expected.
25+
""",
26+
]
27+
from = "now-9m"
28+
index = ["filebeat-*", "logs-aws.cloudtrail-*"]
29+
language = "kuery"
30+
license = "Elastic License v2"
31+
name = "AWS IAM Customer-Managed Policy Attached to Role by Rare User"
32+
note = """## Triage and Analysis
33+
34+
### Investigating AWS IAM Customer-Managed Policy Attachment to Role by Unusual User
35+
36+
This rule detects when a customer-managed IAM policy is attached to a role by an unusual or unauthorized user. This activity may indicate a potential privilege escalation attempt within the AWS environment. Adversaries could attach policies to roles to expand permissions, thereby increasing their capabilities and achieving elevated access.
37+
38+
#### Possible Investigation Steps
39+
40+
- **Identify the Initiating User and Target Role**:
41+
- **User Identity**: Examine the `aws.cloudtrail.user_identity.arn` field to determine the user who initiated the policy attachment. Confirm if this user typically has permissions to modify IAM roles and if their activity is consistent with their usual responsibilities.
42+
- **Target Role**: Review `aws.cloudtrail.flattened.request_parameters.roleName` to identify the role to which the policy was attached. Assess whether modifying this role is expected for this user or if this action is unusual in your environment.
43+
44+
- **Analyze the Attached Policy**:
45+
- **Policy ARN**: Inspect the `aws.cloudtrail.flattened.request_parameters.policyArn` field to identify the specific customer-managed policy attached to the role. Evaluate if this policy grants sensitive permissions, especially permissions that could enable privileged actions or data access.
46+
- **Policy Permissions**: Examine the policy content to determine the scope of permissions granted. Policies enabling actions like `s3:*`, `ec2:*`, or `iam:*` could be leveraged for broader access, persistence, or lateral movement.
47+
48+
- **Review Source and User Agent Details**:
49+
- **Source IP and Location**: Analyze the `source.address` and `source.geo` fields to confirm the IP address and geographic location where the policy attachment originated. Verify if this matches expected locations for the initiating user.
50+
- **User Agent Analysis**: Examine `user_agent.original` to determine if AWS CLI, SDK, or other tooling was used to perform this action. Tool identifiers like `aws-cli` or `boto3` may indicate automation, while others may suggest interactive sessions.
51+
52+
- **Evaluate Anomalous Behavior Patterns**:
53+
- **User’s Historical Activity**: Check if the initiating user has a history of attaching policies to roles. An unusual pattern in policy attachments could indicate suspicious behavior, especially if the user lacks authorization.
54+
- **Role Modification History**: Investigate if the targeted role is frequently modified by this or other users. Repeated, unauthorized modifications to a role could signal an attempt to maintain elevated access.
55+
56+
- **Correlate with Related CloudTrail Events**:
57+
- **Other IAM or CloudTrail Activities**: Look for recent actions associated with the same user or role by reviewing `event.action` by `event.provider` to identify which AWS services were accessed. This may provide context on the user’s intent or additional actions taken.
58+
- **Broader Suspicious Patterns**: Identify if similar anomalous events have recently occurred, potentially suggesting a coordinated or escalating attack pattern within the AWS account.
59+
60+
### False Positive Analysis
61+
62+
- **Authorized Administrative Actions**: IAM administrators may legitimately attach policies to roles as part of routine role management. Verify if the user is authorized and if the activity aligns with expected administrative tasks.
63+
- **Role-Specific Modifications**: Roles that frequently undergo policy updates may trigger this rule during standard operations. Consider monitoring for patterns or establishing known exceptions for specific users or roles where appropriate.
64+
65+
### Response and Remediation
66+
67+
- **Immediate Access Review**: If the policy attachment is unauthorized, consider detaching the policy and reviewing the permissions granted to the initiating user.
68+
- **Restrict Role Modification Permissions**: Limit which users or roles can attach policies to critical IAM roles. Apply least privilege principles to reduce the risk of unauthorized policy changes.
69+
- **Enhance Monitoring and Alerts**: Enable real-time alerts and monitoring on IAM policy modifications to detect similar actions promptly.
70+
- **Regular Policy Audits**: Conduct periodic audits of IAM policies and role permissions to ensure that unauthorized changes are quickly identified and addressed.
71+
72+
### Additional Information
73+
74+
For more information on managing IAM policies and roles in AWS environments, refer to the [AWS IAM Documentation](https://docs.aws.amazon.com/IAM/latest/APIReference/API_AttachRolePolicy.html) and AWS security best practices.
75+
"""
76+
references = ["https://docs.aws.amazon.com/IAM/latest/APIReference/API_AttachRolePolicy.html"]
77+
risk_score = 21
78+
rule_id = "f6d07a70-9ad0-11ef-954f-f661ea17fbcd"
79+
severity = "low"
80+
tags = [
81+
"Domain: Cloud",
82+
"Data Source: AWS",
83+
"Data Source: Amazon Web Services",
84+
"Data Source: AWS IAM",
85+
"Resources: Investigation Guide",
86+
"Use Case: Identity and Access Audit",
87+
"Tactic: Privilege Escalation",
88+
]
89+
timestamp_override = "event.ingested"
90+
type = "new_terms"
91+
92+
query = '''
93+
event.dataset: "aws.cloudtrail"
94+
and event.provider: "iam.amazonaws.com"
95+
and event.action: "AttachRolePolicy"
96+
and event.outcome: "success"
97+
and not aws.cloudtrail.flattened.request_paramters.policyArn: arn\:aws\:iam\:\:aws\:policy*
98+
'''
99+
100+
101+
[[rule.threat]]
102+
framework = "MITRE ATT&CK"
103+
[[rule.threat.technique]]
104+
id = "T1548"
105+
name = "Abuse Elevation Control Mechanism"
106+
reference = "https://attack.mitre.org/techniques/T1548/"
107+
[[rule.threat.technique.subtechnique]]
108+
id = "T1548.005"
109+
name = "Temporary Elevated Cloud Access"
110+
reference = "https://attack.mitre.org/techniques/T1548/005/"
111+
112+
113+
114+
[rule.threat.tactic]
115+
id = "TA0004"
116+
name = "Privilege Escalation"
117+
reference = "https://attack.mitre.org/tactics/TA0004/"
118+
119+
[rule.new_terms]
120+
field = "new_terms_fields"
121+
value = ["aws.cloudtrail.user_identity.arn", "aws.cloudtrail.flattened.request_parameters.roleName"]
122+
[[rule.new_terms.history_window_start]]
123+
field = "history_window_start"
124+
value = "now-14d"
125+
126+

0 commit comments

Comments
 (0)