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
| Log | Search for |
|---|---|
| GuardDuty | Findings 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 CLI | Findings 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 Lake | Calls 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 |
| EC2 | Instances that still allow IMDSv1: HttpTokens set to optional in aws ec2 describe-instances output |
| VPC Flow Logs | Outbound connections from the instance to unknown hosts around the first finding |
Contain
| # | Item |
|---|---|
| 1 | Snapshot the instance's volumes before changing it. See Evidence |
| 2 | Revoke 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 |
| 3 | Require IMDSv2 on the instance: aws ec2 modify-instance-metadata-options --instance-id <id> --http-tokens required --http-endpoint enabled |
| 4 | Only 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} |
| 5 | Isolate the instance if code runs on it. See Cloud scenario |
Eradicate and recover
| # | Item |
|---|---|
| 1 | Fix the entry point: the SSRF flaw, the exposed service or the malware on the host |
| 2 | Review every action the role took off-host and reverse changes made in the window |
| 3 | Cut the role's permissions to what the workload uses, with IAM Access Analyzer unused access findings as input |
| 4 | Set IMDSv2 as required in launch templates and as the account default: aws ec2 modify-instance-metadata-defaults --http-tokens required --region <region> |
| 5 | Rebuild 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.