RAWS playbook · Identity and access
Leaked IAM user access keys used to change permissions
The attacker calls AWS with an IAM user's long-term access key found in code, a continuous integration (CI) log or a laptop. A typical run checks the key with GetCallerIdentity, then creates new users, keys or console passwords and grants itself more policy. Deactivate the key and block the temporary credentials made from it first, and export CloudTrail before the 90-day event history runs out.
Look here
| Log | Search for |
|---|---|
| CloudTrail event history | Management calls made with the key in the last 90 days. Event history has no data events, so use the trail logs or CloudTrail Lake for S3 object access and older activity. IAM events are in us-east-1. Repeat for each Region: aws cloudtrail lookup-events --region <region> --lookup-attributes AttributeKey=AccessKeyId,AttributeValue=<access-key-id> --start-time <utc-start> --end-time <utc-end> |
| CloudTrail, IAM and STS events | Key checks, new principals and policy changes: GetCallerIdentity, CreateUser, CreateAccessKey, CreateLoginProfile, UpdateLoginProfile, AttachUserPolicy, PutUserPolicy, AddUserToGroup, AttachRolePolicy, PutRolePolicy, UpdateAssumeRolePolicy, CreatePolicyVersion, SetDefaultPolicyVersion, GetSessionToken, GetFederationToken, AssumeRole |
| CloudTrail Lake | Every call made with the key, in time order: SELECT eventTime, eventSource, eventName, awsRegion, sourceIPAddress, userAgent, errorCode FROM <event-data-store-id> WHERE userIdentity.accessKeyId = '<access-key-id>' ORDER BY eventTime |
| GuardDuty | Findings for leaked or misused credentials: CredentialAccess:IAMUser/CompromisedCredentials, PrivilegeEscalation:IAMUser/AnomalousBehavior, Persistence:IAMUser/AnomalousBehavior, Discovery:IAMUser/AnomalousBehavior, AttackSequence:IAM/CompromisedCredentials |
| IAM | An AWS quarantine policy on the user, AWSCompromisedKeyQuarantineV2 or AWSCompromisedKeyQuarantineV3: aws iam list-attached-user-policies --user-name <user> |
Contain
| # | Item |
|---|---|
| 1 | Deactivate the key and keep it for the investigation: aws iam update-access-key --user-name <user> --access-key-id <access-key-id> --status Inactive |
| 2 | Leave any AWSCompromisedKeyQuarantine policy attached, and follow the instructions in the AWS Support case |
| 3 | Block temporary credentials issued from the key. The inline policy denies * when aws:TokenIssueTime is before now: aws iam put-user-policy --user-name <user> --policy-name ir-deny-old-tokens --policy-document file://deny-old-tokens.json |
| 4 | List the keys of each user named in the CreateAccessKey events, then deactivate the attacker's keys with the item 1 command: aws iam list-access-keys --user-name <user> |
| 5 | Revoke sessions on every role the key assumed, with the same aws:TokenIssueTime deny: aws iam put-role-policy --role-name <role> --policy-name AWSRevokeOlderSessions --policy-document file://revoke.json |
| 6 | Remove console passwords the attacker created destructive: aws iam delete-login-profile --user-name <user> |
Eradicate and recover
| # | Item |
|---|---|
| 1 | List the versions of each changed managed policy and find the known-good one: aws iam list-policy-versions --policy-arn <arn> |
| 2 | Set the known-good version as the default: aws iam set-default-policy-version --policy-arn <arn> --version-id <known-good> |
| 3 | Restore each changed trust policy: aws iam update-assume-role-policy --role-name <role> --policy-document file://known-good-trust.json |
| 4 | Remove users, keys, inline policies and group memberships created in the window, after the evidence is exported |
| 5 | Rotate every secret and key the user's policies could read, working from the scope in Scope |
| 6 | Delete the exposed key once no workload depends on it destructive: aws iam delete-access-key --user-name <user> --access-key-id <access-key-id> |
| 7 | Move the workload that used the key to an IAM role, and remove the key from the repository or log where it leaked |
Watch out
- Deactivating or rotating the key leaves temporary credentials from
GetSessionTokenandGetFederationTokenvalid for up to 36 hours. Theaws:TokenIssueTimedeny closes that gap for IAM users. It cannot restrict temporary credentials the root user created. For a root key, delete the key and open an AWS Support case. - Those temporary credentials carry their own
ASIAkey IDs. Search for each one in CloudTrail as well.