How the attack works
An EC2 instance gets AWS permissions through an instance profile, a container for one IAM role. Software on the instance reads temporary credentials for that role from the instance metadata. The profile is chosen when the instance is launched (RunInstances), attached to an instance that has none (AssociateIamInstanceProfile), or swapped on a running instance (ReplaceIamInstanceProfileAssociation). All three need iam:PassRole on the role, and AWS warns that iam:PassRole on resource * lets a user pass any role of the account to an instance. A principal with that permission and a way to run commands on an instance can therefore pick a role that is more privileged than itself and use it. The same result is reached one level down: AddRoleToInstanceProfile puts a different role into an existing profile, and every instance that uses the profile receives the new role.
iam:PassRole is a permission, not an API call, and AWS states that no CloudTrail record is generated for it: the role shows up only in the call that gave it to a resource. EC2 logs all of its API actions as management events, and IAM logs AddRoleToInstanceProfile. The rules on this page match the attach, the swap and the profile change. They do not match RunInstances: most launches in an account carry an instance profile, so a rule on it fires on every normal deployment. That path is described under detection logic. Launching an instance with a passed role is one of the best-known published AWS privilege escalation paths. No incident report was used for this page.
What an attacker needs
- Credentials for a principal allowed iam:PassRole on a privileged role that trusts ec2.amazonaws.com, together with ec2:AssociateIamInstanceProfile, ec2:ReplaceIamInstanceProfileAssociation or ec2:RunInstances.
- For the profile variant, iam:AddRoleToInstanceProfile on an instance profile that instances already use.
- A way to run commands on the instance, such as an existing session, a remote management service or user data at launch.
Impact
The attacker acts with the permissions of the passed role. If the role is an administrator or can modify IAM, that is full control of the account. The credentials are temporary role credentials, and those that were already read from the instance stay valid until they expire, even after the profile is detached.
Detection rules
IAM Instance Profile Associated with or Replaced on an EC2 Instance
Detects AssociateIamInstanceProfile and ReplaceIamInstanceProfileAssociation calls to Amazon EC2. The first attaches an instance profile to an instance that has none, the second swaps the profile of a running instance without detaching the old one first. Either way the instance receives the role inside the profile, and whoever can run commands on the instance can use that role. AWS lists both actions, together with iam:PassRole, as the permissions needed to attach or replace the role of an instance. The rule matches every such call: check which role the profile holds and whether it is more privileged than the caller. AWS documents that all EC2 API actions are logged as management events and publishes no example record for these two, so the rule does not read the profile from the request. It also matches attempts that failed (errorCode present): check errorCode, because an unauthorized attempt is worth seeing.
title: IAM Instance Profile Associated with or Replaced on an EC2 Instance
id: 39cd80d0-0a4d-4f7e-89d0-a948b62f8bbd
status: experimental
description: >-
Detects AssociateIamInstanceProfile and
ReplaceIamInstanceProfileAssociation calls to Amazon EC2. The first
attaches an instance profile to an instance that has none, the second
swaps the profile of a running instance without detaching the old one
first. Either way the instance receives the role inside the profile, and
whoever can run commands on the instance can use that role. AWS lists both
actions, together with iam:PassRole, as the permissions needed to attach
or replace the role of an instance. The rule matches every such call:
check which role the profile holds and whether it is more privileged than
the caller. AWS documents that all EC2 API actions are logged as
management events and publishes no example record for these two, so the
rule does not read the profile from the request.
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/APIReference/API_AssociateIamInstanceProfile.html
- https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_ReplaceIamInstanceProfileAssociation.html
- https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/permission-to-pass-iam-roles.html
- https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_passrole.html
- https://attack.mitre.org/techniques/T1548/005/
author: Forestall
date: 2026-11-18
tags:
- attack.privilege-escalation
- attack.t1548.005
logsource:
product: aws
service: cloudtrail
detection:
selection:
eventSource: ec2.amazonaws.com
eventName:
- AssociateIamInstanceProfile
- ReplaceIamInstanceProfileAssociation
condition: selection
falsepositives:
- Platform teams and automation that attach a standard profile to instances that were launched without one, for example to enroll them in a management service. They use the same roles and the same profiles every time, so add those roles to a filter_* selection on userIdentity.arn and keep alerting on calls by people or unknown roles.
level: medium
False positives
- Platform teams and automation that attach a standard profile to instances that were launched without one, for example to enroll them in a management service. They use the same roles and the same profiles every time, so add those roles to a filter_* selection on userIdentity.arn and keep alerting on calls by people or unknown roles.
IAM Role Added to an Instance Profile
Detects AddRoleToInstanceProfile calls to IAM. An instance profile contains one role, and this call puts a role into it. The IAM API reference notes that the caller must hold the PassRole permission on that role. When the profile is already used by instances, they receive the new role, after a delay or once the profile is associated again, so the call changes what those instances may do without any EC2 event. The role and the profile are in requestParameters.roleName and requestParameters.instanceProfileName, which follow the RoleName and InstanceProfileName parameters of the IAM API reference. The rule matches every such call, including the creation of a new profile for a new workload: check what the role can do and which instances use the profile. It also matches attempts that failed (errorCode present): check errorCode, because an unauthorized attempt is worth seeing.
title: IAM Role Added to an Instance Profile
id: 738ae524-36dc-47e3-a28c-dbc864589274
status: experimental
description: >-
Detects AddRoleToInstanceProfile calls to IAM. An instance profile
contains one role, and this call puts a role into it. The IAM API
reference notes that the caller must hold the PassRole permission on that
role. When the profile is already used by instances, they receive the new
role, after a delay or once the profile is associated again, so the call
changes what those instances may do without any EC2 event. The role and
the profile are in requestParameters.roleName and
requestParameters.instanceProfileName, which follow the RoleName and
InstanceProfileName parameters of the IAM API reference. The rule matches
every such call, including the creation of a new profile for a new
workload: check what the role can do and which instances use the profile.
It also matches attempts that failed (errorCode present): check errorCode,
because an unauthorized attempt is worth seeing.
references:
- https://docs.aws.amazon.com/IAM/latest/APIReference/API_AddRoleToInstanceProfile.html
- https://docs.aws.amazon.com/IAM/latest/UserGuide/cloudtrail-integration.html
- https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_passrole.html
- https://attack.mitre.org/techniques/T1548/005/
author: Forestall
date: 2026-11-18
tags:
- attack.privilege-escalation
- attack.t1548.005
logsource:
product: aws
service: cloudtrail
detection:
selection:
eventSource: iam.amazonaws.com
eventName: AddRoleToInstanceProfile
condition: selection
falsepositives:
- Infrastructure-as-code pipelines and the console, which create an instance profile and add its role whenever a new instance role is set up. Add the deployment roles to a filter_* selection on userIdentity.arn and review calls made by anyone else.
level: medium
False positives
- Infrastructure-as-code pipelines and the console, which create an instance profile and add its role whenever a new instance role is set up. Add the deployment roles to a filter_* selection on userIdentity.arn and review calls made by anyone else.
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 launched with an instance profile the principal has not used before
Deployment roles launch instances with the same few profiles every day. A principal that launches with a profile it has never used, or that has never launched instances at all, may be passing a role it found to a machine it controls.
- Collect
RunInstancesevents fromec2.amazonaws.comwhose request carries an instance profile (the IamInstanceProfile parameter, a name or an ARN). Note the caller inuserIdentity.arn. - Keep a record, over at least 30 days, of which callers launched with which instance profiles.
- Alert when a caller and profile pair appears for the first time, and check what the role in that profile can do compared with the caller.
EventsRunInstances
LimitsNew workloads and new pipelines produce first-time pairs too. The 30 days are a starting point. The instance profile is a request parameter of the EC2 API reference, and AWS publishes no example record that shows it, so confirm the field in your own events first.
Role placed in an instance profile is on your list of privileged roles
Most profile changes involve ordinary workload roles. The change that matters is the one that puts an administrator or IAM-capable role onto instances, and only your own list of privileged roles can tell the two apart.
- Collect
AddRoleToInstanceProfileevents fromiam.amazonaws.comand read the role inrequestParameters.roleName. - Compare the role name with your list of privileged roles, such as roles with administrator access or with IAM write permissions.
- Alert on a match, and list the instances that use the profile named in
requestParameters.instanceProfileName.
EventsAddRoleToInstanceProfile
LimitsIt needs a maintained list of privileged roles, and a role that became privileged after it was put into a profile is not seen. Attaching a profile that already holds a privileged role is covered by the rule on associations.
Instance role credentials used from outside the instance they were issued to
The credentials of an instance role are delivered to one instance by the instance metadata service and are meant to be used there. An attacker who passed a privileged role to an instance usually copies them off it and calls AWS from a machine of their own. GuardDuty raises its InstanceCredentialExfiltration findings for this pattern, and CloudTrail holds what is needed to look for it directly.
- Select events whose
userIdentity.typeisAssumedRoleand whoseuserIdentity.sessionContext.ec2RoleDeliveryis 1.0 or 2.0: credentials provided by the instance metadata service. In AWS's example records the session name at the end ofuserIdentity.arnis the instance ID. - From your EC2 inventory, list the addresses each instance reaches AWS from at the time of the event: its public IP addresses, the NAT gateway addresses of its subnets and the VPC endpoints of its VPC.
- Compare
sourceIPAddress, andvpcEndpointIdwhere the event carries it, with the addresses of the instance named in the session. Alert when they belong to none of them, first for addresses outside AWS and endpoints of other accounts. - Raise the priority for roles that the alerts of this page attached, and for IAM and STS calls made with the credentials.
LimitsInstances that reach AWS through an on-premises gateway, a VPN or a shared egress VPC call from addresses that are not their own, which GuardDuty names as the usual benign cause. Addresses change, so the inventory has to cover the time of the event. Not every service logs vpcEndpointId, and calls that AWS services make show a service name instead of an address.
Triage
- Identify the caller (userIdentity.arn), the source IP address and the user agent. Is it a deployment or platform role, and does it usually manage this instance?
- Read the instance and the instance profile named in the request, find the role inside the profile, and check what that role can do compared with the caller.
- For AddRoleToInstanceProfile, read requestParameters.roleName and requestParameters.instanceProfileName, and list the instances that use the profile.
- Look for a way onto the instance by the same caller around the same time, and for activity of the role's sessions from an address that is not the instance.
- Check whether the previous profile was put back shortly after, which is what a short use of a borrowed role looks like.
Response
Once the attack is confirmed:
- Put the intended instance profile back on the instance, or restore the previous role in the profile.
- Revoke the active sessions of the passed role. Credentials already read from the instance metadata stay valid until they expire unless the sessions are revoked, and revoking affects every workload that uses the role.
- Review CloudTrail for what the sessions of the role did, and remove what they created, such as users, access keys, roles and policy changes.
- Contain the principal that made the change, and take away its iam:PassRole on the privileged role.
- Treat the instance as accessed by the attacker. Review how they ran commands on it and what else is stored there.
Mitigation
- Scope iam:PassRole to specific role ARNs and add the condition key iam:PassedToService with the value ec2.amazonaws.com. Never allow it on Resource
"*". - Do not let administrator roles trust ec2.amazonaws.com, and keep instance roles least-privilege.
- Allow ec2:AssociateIamInstanceProfile, ec2:ReplaceIamInstanceProfileAssociation and iam:AddRoleToInstanceProfile only to deployment and platform roles.
- Limit who can run commands on instances that carry privileged roles, because whoever can do that can use the role.
Frequently asked questions
Why can I not alert on iam:PassRole directly?
`iam:PassRole` is a permission, not an API call, and AWS states that no CloudTrail logs are generated for it. To see which role was passed, you have to read the event that created or modified the resource that received the role. For EC2 these are the launch, the association of an instance profile and its replacement.
Why is there no rule on RunInstances?
Most instances are launched with an instance profile, so a rule on every launch that carries one fires on every normal deployment. The launch path is real, and it is the best known PassRole escalation. This page handles it as a hypothesis under detection logic, which compares the launch with what a principal has done before.
Why does a change to an instance profile matter when no instance was touched?
An instance profile contains one role, and `AddRoleToInstanceProfile` puts a role into it. AWS documents that the caller needs the PassRole permission on that role. Every instance that uses the profile receives the new role, after a delay or after the profile is associated again, so one call can change the permissions of many instances without an EC2 event.
Do the credentials stop working when the instance profile is detached?
No. The role credentials that software on the instance obtained are temporary session credentials, and they stay valid until they expire. Detaching the profile stops the instance from getting new ones. To cut off credentials that were already taken, revoke the active sessions of the role.
References
- MITRE ATT&CK T1548.005: Abuse Elevation Control Mechanism: Temporary Elevated Cloud Access
- Grant a user permissions to pass a role to an AWS service (PassRole in CloudTrail)
- Grant permissions to attach an IAM role to an instance
- Log Amazon EC2 API calls using AWS CloudTrail
- Amazon EC2 API Reference: AssociateIamInstanceProfile
- Amazon EC2 API Reference: ReplaceIamInstanceProfileAssociation
- IAM API Reference: AddRoleToInstanceProfile
- Logging IAM and AWS STS API calls with AWS CloudTrail
- Revoke IAM role temporary security credentials
- CloudTrail userIdentity element (ec2RoleDelivery)
- CloudTrail record contents for management, data, and network activity events
- GuardDuty IAM finding types (InstanceCredentialExfiltration.OutsideAWS and .InsideAWS)
- Example CloudTrail record of an instance role session with ec2RoleDelivery and vpcEndpointId