Last reviewed: by Dr. Austin Osuide
Spot temporary EC2 role credentials being used away from the instance that received them, then separate theft from legitimate central egress and administrator testing.
A server-side request forgery flaw, exposed proxy or compromised process can reach the EC2 Instance Metadata Service at 169.254.169.254. If the instance has an IAM role, the metadata path iam/security-credentials/role-name returns temporary access credentials for that role. AWS rotates them, but they remain usable until they expire or policy changes remove their access.
The metadata request itself is usually not where you catch this. IMDS is a link-local service that only software on the instance can reach, and the evidence AWS builds its own detections on comes later: both GuardDuty findings are driven by CloudTrail events recorded when the credentials call AWS services. That turns the question from 'who requested 169.254.169.254?' into 'where were this instance's role credentials used, and what did they do?'. Host or web logs can explain how the credentials were taken, and CloudTrail and GuardDuty show what they did in AWS afterwards.
The role session is a valid credential that may now be in the wrong place, and its API calls authenticate normally even though the credential reached the caller through a web flaw or a compromised workload. The related ATT&CK coverage sits under T1078 Valid Accounts for that reason.
IMDSv1 is plain request and response: a client sends a GET request to the metadata endpoint and needs no session token. IMDSv2 adds a session, which starts with a PUT request to /latest/api/token and requires the returned token on later metadata GET requests. That token can last from one second to six hours and is tied to the instance where it was created, so it cannot be used on another instance.
Which of the two an instance accepts is set by HttpTokens. Set to optional, the instance answers both IMDSv1 and IMDSv2, and set to required it rejects any request without a valid IMDSv2 token. The PUT response hop limit is a separate control on how many network hops the token response can traverse, and container workloads may need a value of 2 rather than 1.
Requiring IMDSv2 blocks many simple SSRF and open-proxy paths because the caller must make the token request and pass its header. Code execution on the instance, an application that forwards the required method and headers, or a compromised process can still retrieve credentials, so migrating does not retire the detection or the least-privilege work.
GuardDuty has two managed findings for this case. UnauthorizedAccess:IAMUser/InstanceCredentialExfiltration.OutsideAWS covers EC2 instance credentials used from an external IP address, and UnauthorizedAccess:IAMUser/InstanceCredentialExfiltration.InsideAWS covers credentials used through an IP address or supported VPC endpoint owned by a different AWS account. Both draw on CloudTrail management events and CloudTrail data events for S3.
Either finding is worth triaging promptly, because AWS has tied the access key back to EC2-delivered credentials. It still takes a person to decide whether it is a compromise, since centralised internet egress, Outposts, VPN routing or an approved operator exporting temporary credentials can all produce activity away from the instance. For the InsideAWS finding, inspect the remote account ID and whether GuardDuty marks that account as affiliated.
The pair should not be the only detector, though. AWS documents that continued activity from the same remote source can stop producing findings after GuardDuty treats the source as expected, so route the findings quickly and keep an independent CloudTrail hunt running for unusual use of EC2 role sessions.
CloudTrail has the identity, action and request origin for each AWS API call. Filter on userIdentity.type set to AssumedRole and keep userIdentity.accessKeyId, userIdentity.arn, userIdentity.sessionContext.sessionIssuer.arn, sourceIPAddress, userAgent, eventSource, eventName, awsRegion and errorCode. The access key ID is the most direct join from a GuardDuty finding to the API calls around it.
EC2 role credentials also carry session context. CloudTrail can record userIdentity.sessionContext.ec2RoleDelivery as 1.0 or 2.0, showing whether the credential came from IMDSv1 or IMDSv2. That describes how the credential was delivered and says nothing about where the later API call came from.
Compare sourceIPAddress and any VPC endpoint context with an inventory of the instance's expected egress paths, because neither kind of source settles the question alone. A public IP outside AWS can be suspicious or can be an on-premises gateway used by normal routing, and an AWS-owned address can be a cross-account VPC endpoint or an attacker-controlled EC2 host, both of which are part of the InsideAWS case.
AWS exposes policy context that can limit where EC2 role credentials work. aws:Ec2InstanceSourceVpc is the VPC where the credentials were delivered, and aws:Ec2InstanceSourcePrivateIPv4 is the primary private address, which must be paired with the VPC key because private addresses are not globally unique. ec2:SourceInstanceArn identifies the instance that created the role session.
The ec2:RoleDelivery key can deny API calls made with credentials retrieved through IMDSv1. Once a workload has moved to IMDSv2, a NumericLessThan condition against 2.0 enforces that. Roll it out role by role against tested services, and check service-mediated calls before any broad deny reaches production.
These controls narrow what a stolen credential can do, and CloudTrail still has to show what was attempted. Keep policy denials visible and retain enough management and data events for the services the role can reach, because a denied call from an unexpected source may be the first sign that the control worked.
The first question is whether anyone expected that source. Check the instance owner, deployment tooling, egress design and any approved use of temporary credentials away from the workload, and if nobody can account for it, treat the instance and its role session as compromised.
Take the evidence before touching the workload, then contain in the order below. One caveat on isolation: AWS notes that changing security groups does not terminate existing tracked connections, so incident handling may also need network ACL action or instance replacement.
Search the full lifetime of the access key ID across every account and Region that central logging covers, reviewing successful and failed calls, the resources touched and any new sessions created from the stolen credential. If the role changed policies, shared snapshots or created persistence, widen the incident beyond the original instance.
A safe test uses a low-privilege instance role in a non-production account. Retrieve its temporary credentials through the supported IMDS path, call a harmless read-only API from the instance, then repeat from an approved test host. Confirm which CloudTrail fields survive ingestion and how long GuardDuty takes to publish a finding in that account.
Run a second test through normal central egress, which the detection should be able to tell apart from an unknown residential or unrelated AWS source. If the two look identical in the SIEM, the rule lacks egress and account context, and more event names will not supply it.
Review the detector after network changes, GuardDuty administrator changes or a move to new VPC endpoints. DCV can record the GuardDuty finding as managed-finding evidence and the CloudTrail rule as SIEM-detection evidence against T1078, and recorded separately, they show whether the SOC has two independent detection paths or one.