PIVTR-D
RAWS playbook · Compute and containers

Stolen EC2 instance role credentials

The attacker reads role credentials from the instance metadata service (IMDS), often through server-side request forgery (SSRF) or code running on the host, and uses them from another network or AWS account. Revoke the role's older sessions first, then require IMDSv2 on the instance.

Look here

LogSearch for
GuardDutyFindings for instance credentials used off the instance: UnauthorizedAccess:IAMUser/InstanceCredentialExfiltration.OutsideAWS (used from an external IP), UnauthorizedAccess:IAMUser/InstanceCredentialExfiltration.InsideAWS (used from another AWS account), UnauthorizedAccess:EC2/MetadataDNSRebind
GuardDuty CLIFindings of one type, here use from outside AWS: aws guardduty list-findings --detector-id <id> --finding-criteria '{"Criterion":{"type":{"Eq":["UnauthorizedAccess:IAMUser/InstanceCredentialExfiltration.OutsideAWS"]}}}'
CloudTrail LakeCalls made with the role. Flag source IPs outside your VPCs and NAT addresses: SELECT eventTime, eventName, awsRegion, sourceIPAddress, userIdentity.accessKeyId FROM <event-data-store-id> WHERE userIdentity.arn LIKE 'arn:aws:sts::<account>:assumed-role/<role>/%' ORDER BY eventTime
EC2Instances that still allow IMDSv1: HttpTokens set to optional in aws ec2 describe-instances output
VPC Flow LogsOutbound connections from the instance to unknown hosts around the first finding

Contain

#Item
1Snapshot the instance's volumes before changing it. See Evidence
2Revoke role sessions issued before now, with Revoke active sessions on the role in the IAM console or: aws iam put-role-policy --role-name <role> --policy-name AWSRevokeOlderSessions --policy-document file://revoke.json
3Require IMDSv2 on the instance: aws ec2 modify-instance-metadata-options --instance-id <id> --http-tokens required --http-endpoint enabled
4Only where the instance reaches every AWS service through VPC endpoints, deny role use outside its VPC. Without VPC endpoints this denies every call the instance makes. The deny uses Null ec2:SourceInstanceARN = false, BoolIfExists aws:ViaAWSService = false and: StringNotEquals aws:SourceVpc = ${aws:Ec2InstanceSourceVpc}
5Isolate the instance if code runs on it. See Cloud scenario

Eradicate and recover

#Item
1Fix the entry point: the SSRF flaw, the exposed service or the malware on the host
2Review every action the role took off-host and reverse changes made in the window
3Cut the role's permissions to what the workload uses, with IAM Access Analyzer unused access findings as input
4Set IMDSv2 as required in launch templates and as the account default: aws ec2 modify-instance-metadata-defaults --http-tokens required --region <region>
5Rebuild the instance from a known-good image if the host was compromised
Watch out
  • The revoke policy also denies the instance's own current credentials. Expect errors until the instance picks up credentials issued after the revoke time. IMDS offers new credentials at least five minutes before the old ones expire.
  • AWS advises against removing a role from an instance profile to contain this. The change can take up to one hour to take effect.
Adapts material from Dynamic Incident Response: A Framework for Security Teams by Joshua Wright, © 2026 The Escal Institute of Advanced Technologies, Inc. d/b/a SANS Institute, licensed under CC BY 4.0. Figures reproduced under that license. Changes were made. Not published by, endorsed by, or affiliated with the SANS Institute.