In this blog, we are going to discuss an attack vector that utilizes the STS AssumeRole
and GetSessionToken
API calls, and focus on what you have to do differently to detect, mitigate, and prevent abuse vs handling permanent access keys.
Imagine This
An attacker has just compromised one of your AWS credentials and gained access to your production AWS account. What do you do next? Revoke the credentials, of course. But just revoking the compromised credentials is not enough to keep the attacker out of your environment. In this blog post, we’ll explore temporary tokens and how they can provide an attacker continued access to your AWS environments, even after you have revoked compromised credentials.
Temporary tokens are provided by AWS Secure Token Service (STS) and are similar to permanent access keys in functionality and have been used to implement several common AWS features such as:
- Assuming roles, including the passing of roles to services
- Federated identities (e.g., single sign-on and cross-account access)
- Authentication of IoT devices
As with all security design, there are tradeoffs and security concerns from the use of temporary tokens:
- Temporary tokens can cause confusion during incident handling/response versus permanent access keys because their creation and use is logged in CloudTrail using different, somewhat confusing fields and values. As an example, there is no explicit json attribute that distinguishes a temporary token from a permanent API access key–you must infer from the access key id naming convention or other fields.
- The remediation options for Temporary Tokens can have with high impact; e.g., deletion of users
- Temporary tokens are harder to lock down since the creation of some types of temporary tokens cannot be restricted by policy.
- There is no tracking of which tokens have already been created, so auditing temporary token usage and identifying temporary tokens is difficult, as they must be derived from parsing CloudTrail logs. Further, there are no direct management functions (such as deletion) for temporary tokens. We’ll discuss in detail what mitigation steps apply later in the blog. This also makes it more difficult to assess exposure from credential risk because there is no easy way to list all temporary credentials that have been issued or are active/outstanding.
Temporary Tokens
Temporary tokens are implicitly created by AWS in the case of IoT device authentication or from assuming of roles by services. Temporary tokens can also be explicitly created by authenticated users calling STS AssumeRole
or GetSessionToken
, e.g.,
In addition to the access key id and secret, a temporary credential includes an expiration date and a session token, which must be included in any API calls (along with the access key and secret).
Temporary tokens essentially have the same functionality and similar security exposure as permanent access keys, but with a few differences:
- Expiration: Temporary tokens have an expiration, ranging from 15 minutes to 36 hours. This is good from a security viewpoint, as it reduces the time window for abuse in case of lost or stolen temporary tokens.
- API Access: Temporary tokens can call any service that the original credential (that created the temporary token) has privileges for, except that:
- within STS, temporary tokens can only call the
AssumeRole
API call. - within IAM, MFA is required.
These restrictions may not necessarily constrain attackers, since there are a large number of services that support AssumeRole [1], which combined with numerous techniques for escalating privileges [2], creates a large attack surface for lateral movement from temporary tokens to other privileged roles.
Attack Scenario
To highlight the challenges when temporary tokens are used in an attack, let’s look at a simple attack scenario. The environment has support personnel who must manage production S3 buckets, and the organization has created a specific role, MyBucketRole, that is assumed by support personnel whenever the need arises for S3 bucket support.
The Attacker:
- Gains initial access to the environment with stolen credentials (access key A) which was accidentally hard-coded into a script that was uploaded to Git by a support person.
- Generates a temporary token B using the stolen credentials (access key A) with a call to STS
GetSessionToken
, for persistent access i.e., backdoor access. This is not used immediately but is saved for redundant access and will be used in a secondary attack, and we will see shortly why it has different challenges than permanent keys and other temporary tokens generated byAssumeRole
. - Uses access key A to escalate privileges by assuming a role,
MyBucketRole
, that has access to an S3 Bucket. This returns temporary token C, which when used will have the permissions ofMyBucketRole
. - Accesses sensitive data on the S3 Bucket using temporary token C.
- Exfiltrates data from the S3 Bucket (e.g. S3 sync to another S3 bucket)
The initial compromise and attack vector in this scenario is the compromised credential. The use of temporary tokens does not change this risk profile but it will make the detection, mitigation, and prevention steps different or more challenging, which we cover in the next section.
Defender Viewpoint
Let’s look at this attack from the defender’s viewpoint to understand any challenges that stem from the use of temporary tokens by the attacker. We’re in the middle of the attack, we’re alerted to suspicious data access patterns during the exfiltration phase, and we’ve found out that a support user called AssumeRole
and is performing data exfiltration.
Detection and Remediation
1 – Delete Compromised Key A
We immediately delete or make inactive the compromised access key A, regenerate new keys, ensure key rotation is enabled: