Skip to content
Account takeover Write-up Intermediate

Fall account takeover via Amazon Cognito misconfiguration

The Amazon Cognito custom authentication flow did not verify that the IdToken presented in the RespondToAuthChallenge call belonged to the same user as the session and username. An attacker could obtain a valid session for a victim's account and combine it with their own IdToken to receive the victim's tokens, leading to account takeover.

Hossam Ahmed Follow

Published on medium.com

Hello reader, I hope you are doing well. Today I want to talk about one of my findings.

Hossam Ahmed, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 9 min read.

Context

The target is a public bug bounty program that the author refers to as redirect.com; the real name is withheld because the bug was not fixed at the time of writing. The program uses Amazon Cognito for authentication and authorisation, and offers two ways to register: email and password, and OAuth through Google as an identity provider (IdP).

Cognito has two main components. User pools are user directories that provide sign-up and sign-in, either directly with a username and password or federated through a third-party IdP such as Facebook, Amazon, Google or Apple. Identity pools let the developer grant users access to other AWS services. They can be used separately or together. After a successful user pool sign-in the app receives user pool tokens, which the developer can use to retrieve AWS credentials, to control access to server-side resources, or to control access to Amazon API Gateway. The developer can also create user pool groups to manage permissions and to represent different types of users.

From the original on medium.com

Two authentication flows matter here.

Client-side authentication flow:

  1. The user enters their user name and password into the app.
  2. The app calls InitiateAuth with the user name and Secure Remote Password (SRP) details. The operation returns the authentication parameters.
  3. The app calls RespondToAuthChallenge. If it succeeds, Cognito returns the user's tokens and the flow is complete. If another challenge is required, no tokens are returned; instead the call returns a session.
  4. If RespondToAuthChallenge returns a session, the app calls RespondToAuthChallenge again with the session and the challenge response (for example an MFA code).
Client-side authentication flow From the original on medium.com

Custom authentication flow: user pools allow custom flows that build a challenge/response model using AWS Lambda triggers. The flow starts with InitiateAuth, indicating the type of authentication and any initial parameters. Cognito answers with one of three things: a challenge for the user along with a session and parameters; an error if the user fails to authenticate; or ID, access and refresh tokens if the supplied parameters are sufficient to sign the user in (typically the user or app must first answer a challenge, but the custom code determines this). If a challenge comes back, the app gathers more input and calls RespondToAuthChallenge with the challenge responses and the session. Cognito responds the same way as to InitiateAuth: tokens if signed in, otherwise another challenge or an error. The sequence repeats until the user signs in or an error is returned.

An app starts a custom flow by calling InitiateAuth with CUSTOM_AUTH as the Authflow. Three Lambda triggers control challenges and verification:

  1. DefineAuthChallenge takes a session array of previous challenges and responses, and generates the next challenge name plus Booleans indicating whether the user is authenticated and can be granted tokens. It is a state machine controlling the user's path through the challenges.
  2. CreateAuthChallenge takes a challenge name and generates the challenge and parameters used to evaluate the response. When DefineAuthChallenge returns CUSTOM_CHALLENGE as the next challenge, the flow calls CreateAuthChallenge, which passes the next type of challenge in the challenge metadata parameter.
  3. VerifyAuthChallengeResponse evaluates the response and returns a Boolean indicating whether it was valid.

A custom flow can combine built-in challenges, such as SRP password verification and SMS MFA, with custom challenges such as CAPTCHA or secret questions.

From the original on medium.com

Tokens. The IdToken is a JSON web token (JWT) containing claims about the authenticated user, such as name, email and phone_number. It can be used inside the application and to authenticate users to resource servers or server applications, and outside the application with web API operations, in which case its signature must be verified before any claims inside it are trusted. Its expiration can be set between 5 minutes and 1 day. The AccessToken contains claims about the authenticated user, a list of the user's groups and a list of scopes; its purpose is to authorise API operations in the context of the user, for example to add, change or delete user attributes. Its expiration can also be set between 5 minutes and 1 day. The RefreshToken retrieves new ID and access tokens; by default it expires 30 days after sign-in, and the application's refresh token expiration can be set between 60 minutes and 10 years.

Setup

The author worked as a normal user of the program, with BurpSuite to capture and replay requests. The scope was limited to authentication and authorisation: sign-up, sign-in and OAuth where available. Two accounts are involved in the attack: the attacker's own account, used to obtain a valid IdToken, and a victim account whose username and email are known to the attacker. No special tooling beyond BurpSuite Repeater is shown.

Finding it

The recon was done in three stages: use the program as a normal user, analyse requests and responses to understand what each feature does, then build an attack scenario from that analysis.

Registering with email and password revealed nothing unfamiliar. Registering through OAuth revealed two things:

  1. During registration, after authenticating with the third party and granting access to the application, there were delays in accessing the account that did not stem from the internet connection.
  2. After registration there was no option to change the email or to unlink the Google account from the program account.

That prompted a closer look at the OAuth registration requests, which were highlighted in BurpSuite so they could be analysed individually.

The OAuth registration process works as follows.

  1. Clicking the Google icon transfers the user to Google for authentication, then to the consent step granting access to the client (the target app).
From the original on medium.com
  1. The user pool receives the code from the third-party provider, exchanges it for tokens, and sends them to the program. Decoding the JWT showed that the email had not yet been verified.
From the original on medium.com
  1. The client generates an API call to an endpoint responsible for linking the Google account username with the program account. That endpoint takes 2 parameters and the IdToken as part of the request headers.
From the original on medium.com
  1. The target app does an extra step, fetching the IdToken and AccessToken after the email has been confirmed, using two requests. First it connects to an InitiateAuth endpoint that takes a set of parameters in JSON. This endpoint fetches a valid session belonging to the user account, the challenge name created by the website developers through the AWS Lambda trigger, and the username and email address of the user account.
From the original on medium.com

Second, the app connects to the RespondToAuthChallenge endpoint with the session, the ChallengeName and the username from the previous step, plus additional parameters. If the session, username and the additional parameter Answer (the IdToken) are validated, user-specific tokens are returned in the HTTP response. The session is associated with the username of the user and the IdToken.

From the original on medium.com

The question that drove the attack: what happens if a valid session of another user is used instead of my own session, together with the IdToken of my account and the username of the other user account? Manipulating the parameters of the RespondToAuthChallenge request answered it.

Exploitation

Step 1: obtain the attacker's IdToken

The attacker logs in to their own account to get a valid IdToken belonging to that account.

Step 2: obtain the victim's session with InitiateAuth

The attacker sends a POST request to the AWS InitiateAuth endpoint to retrieve a valid session belonging to the victim account. In BurpSuite Repeater, a new window is opened with this request:

HTTP
POST / HTTP/2Host: cognito-idp.us-east-1.amazonaws.comUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0Accept: */*Accept-Language: en-US,en;q=0.5Accept-Encoding: gzip, deflateReferer: https://redirect.com/Content-Type: application/x-amz-json-1.1X-Amz-Target: AWSCognitoIdentityProviderService.InitiateAuthX-Amz-User-Agent: aws-amplify/5.0.4 jsContent-Length: 155Origin: https://redirect.comSec-Fetch-Dest: emptySec-Fetch-Mode: corsSec-Fetch-Site: cross-siteTe: trailers{"AuthFlow":"CUSTOM_AUTH","ClientId":"targetAppClientId","AuthParameters":{"USERNAME":"XXXXXXXXXXXXXXXXXXX"},"ClientMetadata":{}}

The X-Amz-Target header selects the AWSCognitoIdentityProviderService.InitiateAuth operation, the body sets AuthFlow to CUSTOM_AUTH and identifies the app with ClientId. The victim's email is put as the value of the USERNAME parameter. The response body is:

JSON
{"ChallengeName":"CUSTOM_CHALLENGE","ChallengeParameters":{"USERNAME":"VICTIM_USERNAME","email":"Victim's email"},"Session":"Victim's session"}

So the call returns a CUSTOM_CHALLENGE, the victim's username and email in the challenge parameters, and a session belonging to the victim.

Step 3: exchange the victim's session for the victim's tokens

The attacker sends a POST request to the AWS RespondToAuthChallenge endpoint to retrieve valid tokens belonging to the victim account, while using an IdToken for the attacker's account. In BurpSuite Repeater, another window is opened with this request:

HTTP
POST / HTTP/2Host: cognito-idp.us-east-1.amazonaws.comUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0Accept: */*Accept-Language: en-US,en;q=0.5Accept-Encoding: gzip, deflateReferer: https://redirect.com/Content-Type: application/x-amz-json-1.1X-Amz-Target: AWSCognitoIdentityProviderService.RespondToAuthChallengeX-Amz-User-Agent: aws-amplify/5.0.4 jsContent-Length: 2385Origin: https://redirect.comSec-Fetch-Dest: emptySec-Fetch-Mode: corsSec-Fetch-Site: cross-siteTe: trailers{"ChallengeName":"CUSTOM_CHALLENGE","ChallengeResponses":{"USERNAME":"VICTIM-USERNAME","ANSWER":"ATTACKER_IdToken"},"ClientId":"targetAppClientId","Session":"VICTIM'S_SESSION"}

The three values that matter are set as follows:

  1. The victim's username as the value of the USERNAME parameter.
  2. The attacker's IdToken as the value of the ANSWER parameter. The attacker's IdToken must be valid.
  3. The victim's session obtained from the previous request as the value of the SESSION parameter.

The response body is:

JSON
{"AuthenticationResult":{"AccessToken":"VICTIM'S-AccessToken","ExpiresIn":3600,"IdToken":"VICTIM'S_IdToken","RefreshToken":"VICTIM'S_RefreshToken","TokenType":"Bearer"},"ChallengeParameters":{}}

The attacker now holds the victim's access token, ID token and refresh token, with an ExpiresIn of 3600 seconds.

From the original on medium.com

Root cause

By manipulating the parameter values of the RespondToAuthChallenge request, the author found that:

  1. The session is validated and linked to the username.
  2. The validity of the IdToken is verified.
  3. It is not checked whether the IdToken is associated with this session and this username.

The checks are performed piece by piece: the session belongs to the victim, the IdToken is a genuine, valid token, and the username matches the session.: the session belongs to the victim, the IdToken is a genuine, valid token, and the username matches the session. Nothing ties the IdToken to the session or to the username it is presented with, so any valid IdToken from any account satisfies the challenge for any session.

Impact

An attacker who knows a victim's username or email can obtain that victim's access token, ID token and refresh token. The author notes the bug was not fixed at the time of writing and does not describe the program or its bounty.

Fix

The report does not describe a fix. : the identity in the verified token has to match the identity the session was issued for, and the request must be rejected otherwise.

Variants to test

  • Other custom challenge answers. On any Cognito-backed app using CUSTOM_AUTH, replay RespondToAuthChallenge with a session from one account and a valid IdToken from another. A positive result is an AuthenticationResult containing tokens for the session's account.
  • A positive result is a successful link that should have been rejected.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 18 min read.

What happened, in plain words

A security researcher was looking at a website that lets people sign in either with an email address and password, or with their Google account. The website used a service from Amazon, called Amazon Cognito, to handle all of that signing in and checking of permissions. The researcher found a flaw in the way that service was wired up: it was possible to take a valid login session that belonged to one person and pair it with a login token that belonged to a completely different person, and the service would happily hand back the first person's login tokens. With those tokens, an attacker holds the victim's access token, ID token and refresh token.

The researcher calls the target redirect.com because the bug was still unfixed when the write-up was published, so the real name is withheld. Everything below is about how the researcher got there, step by step, in a way you can follow even if you have never done security testing before.

Background: what Amazon Cognito is

Before the attack makes sense, you need to know what the website was using under the bonnet.

Amazon Cognito is a service that Amazon Web Services (AWS) sells. It does the boring but critical job of letting users sign up, sign in, and get permission to do things. Instead of every website building its own login system from scratch, the website hands that job to Cognito. Cognito has two main parts:

  • User pools: a directory of users. It handles sign-up and sign-in, either with a username and password, or through a third party such as Facebook, Amazon, Google or Apple. When someone signs in successfully, the app receives tokens from the user pool.
  • Identity pools: these let the developer give users access to other AWS services. They can be used on their own or together with user pools.

After a successful sign-in, the app gets user pool tokens. The developer can use those tokens to fetch AWS credentials, to control access to server-side resources, or to control access to Amazon API Gateway. The developer can also create user pool groups to manage permissions and to represent different types of users.

From the original on medium.com

A quick everyday comparison: think of a user pool as the reception desk of an office building. You show your ID, the receptionist checks it, and gives you a visitor badge. The badge is a token. Other doors in the building trust the badge, not you personally. If someone else gets hold of your badge, they can walk through the same doors you can.

The two sign-in flows that matter

Cognito supports several ways of signing a user in. Two of them are relevant here.

Client-side authentication flow

This is the ordinary flow most people experience without noticing:

  1. The user enters their user name and password into the app.
  2. The app calls InitiateAuth with the user name and Secure Remote Password (SRP) details. This operation returns the authentication parameters.
  3. The app calls RespondToAuthChallenge. If it succeeds, Cognito returns the user's tokens and the flow is complete. If another challenge is required, no tokens are returned; instead the call returns a session.
  4. If RespondToAuthChallenge returns a session, the app calls RespondToAuthChallenge again with the session and the challenge response (for example an MFA code).
Client-side authentication flow From the original on medium.com

A session here is a short-lived handle that Cognito gives the app so that the next step can be tied back to the same sign-in attempt. Think of it as a numbered ticket you get at a deli counter: you order, you get a ticket, and when your number is called you come back to collect your food. The ticket on its own is not the food, but it lets the counter know which order is yours.

MFA stands for multi-factor authentication: a second proof of identity, such as a code sent by SMS, on top of the password.

Custom authentication flow

User pools also allow custom flows, which build a challenge/response model using AWS Lambda triggers. A Lambda trigger is a small piece of code that AWS runs automatically at a particular moment, like a hook. The flow starts with InitiateAuth, indicating the type of authentication and any initial parameters. Cognito answers with one of three things:

  1. A challenge for the user, along with a session and parameters.
  2. An error if the user fails to authenticate.
  3. ID, access and refresh tokens if the supplied parameters are sufficient to sign the user in. Typically the user or app must first answer a challenge, but the custom code determines this.

If a challenge comes back, the app gathers more input and calls RespondToAuthChallenge with the challenge responses and the session. Cognito responds the same way as to InitiateAuth: tokens if signed in, otherwise another challenge or an error. The sequence repeats until the user signs in or an error is returned.

An app starts a custom flow by calling InitiateAuth with CUSTOM_AUTH as the Authflow. Three Lambda triggers control challenges and verification:

  1. DefineAuthChallenge takes a session array of previous challenges and responses, and generates the next challenge name plus Booleans indicating whether the user is authenticated and can be granted tokens. It is a state machine controlling the user's path through the challenges.
  2. CreateAuthChallenge takes a challenge name and generates the challenge and parameters used to evaluate the response. When DefineAuthChallenge returns CUSTOM_CHALLENGE as the next challenge, the flow calls CreateAuthChallenge, which passes the next type of challenge in the challenge metadata parameter.
  3. VerifyAuthChallengeResponse evaluates the response and returns a Boolean indicating whether it was valid.

A custom flow can combine built-in challenges, such as SRP password verification and SMS MFA, with custom challenges such as CAPTCHA or secret questions.

From the original on medium.com

A state machine is just a set of rules about what can happen next: from state A you can go to B, from B to C, and so on. A vending machine is a state machine: insert coin, choose item, receive item.

Tokens: what they are and why they matter

A token is a string of characters that a server gives to a client as proof that something has already happened, usually a successful login. The client sends the token back on later requests instead of sending the password again. It is like a coat-check ticket: you hand over your coat once, get a ticket, and later the ticket gets your coat back. Whoever holds the ticket gets the coat.

Cognito issues three kinds of token.

IdToken. A JSON web token (JWT) containing claims about the authenticated user, such as name, email and phone_number. It can be used inside the application and to authenticate users to resource servers or server applications, and outside the application with web API operations, in which case its signature must be verified before any claims inside it are trusted. Its expiration can be set between 5 minutes and 1 day.

A JWT (JSON Web Token) is a token with a standard structure: three chunks of text separated by dots. The middle chunk is a JSON object with fields called claims, which are simply statements like "this user's email is X". The third chunk is a cryptographic signature, so the server can check the token was really issued by Cognito and not forged.

AccessToken. Contains claims about the authenticated user, a list of the user's groups and a list of scopes. Its purpose is to authorise API operations in the context of the user, for example to add, change or delete user attributes. Its expiration can also be set between 5 minutes and 1 day.

RefreshToken. Used to retrieve new ID and access tokens. By default it expires 30 days after sign-in, and the application's refresh token expiration can be set between 60 minutes and 10 years.

An API (Application Programming Interface) is the set of URLs and rules a program uses to talk to another program. When your browser loads a page, it is making API calls behind the scenes. An API endpoint is one specific URL in that set, like one counter in a post office: each counter does one job.

Setting up

The researcher worked as a normal user of the program, with a tool called BurpSuite to capture and replay requests. BurpSuite is a program that sits between your browser and the website and lets you see, pause, edit and resend every request. Think of it as a transparent proxy: the browser talks to Burp, Burp talks to the website, and you can read and change everything in the middle.

The scope was limited to authentication and authorisation: sign-up, sign-in and OAuth where available. OAuth is the system that lets you click "Sign in with Google" and have Google vouch for you, without the website ever seeing your Google password.

Two accounts are involved in the attack: the attacker's own account, used to obtain a valid IdToken, and a victim account whose username and email are known to the attacker. No special tooling beyond BurpSuite Repeater is shown. Repeater is the part of BurpSuite that lets you take one captured request, change a value, and send it again to see what comes back.

Finding it

The recon was done in three stages: use the program as a normal user, analyse requests and responses to understand what each feature does, then build an attack scenario from that analysis. Recon (reconnaissance) just means looking around before attacking.

Registering with email and password revealed nothing unfamiliar. Registering through OAuth revealed two things:

  1. During registration, after authenticating with the third party and granting access to the application, there were delays in accessing the account that did not stem from the internet connection.
  2. After registration there was no option to change the email or to unlink the Google account from the program account.

After noticing these, the researcher moved on to analysing the OAuth registration requests, which were highlighted in BurpSuite so they could be analysed individually. That prompted a closer look at the OAuth registration requests, which were highlighted in BurpSuite so they could be analysed individually.

The OAuth registration process works as follows.

  1. Clicking the Google icon transfers the user to Google for authentication, then to the consent step granting access to the client (the target app).
From the original on medium.com
  1. The user pool receives the code from the third-party provider, exchanges it for tokens, and sends them to the program. Decoding the JWT showed that the email had not yet been verified.
From the original on medium.com
  1. The client generates an API call to an endpoint responsible for linking the Google account username with the program account. That endpoint takes 2 parameters and the IdToken as part of the request headers.
From the original on medium.com
  1. The target app does an extra step, fetching the IdToken and AccessToken after the email has been confirmed, using two requests. First it connects to an InitiateAuth endpoint that takes a set of parameters in JSON. This endpoint fetches a valid session belonging to the user account, the challenge name created by the website developers through the AWS Lambda trigger, and the username and email address of the user account.
From the original on medium.com

Second, the app connects to the RespondToAuthChallenge endpoint with the session, the ChallengeName and the username from the previous step, plus additional parameters. If the session, username and the additional parameter Answer (the IdToken) are validated, user-specific tokens are returned in the HTTP response. The session is associated with the username of the user and the IdToken.

From the original on medium.com

A header is a small piece of metadata attached to a request, like the label on a parcel. The body is the contents. JSON is a text format for structured data, using curly braces and quotes, that both humans and programs can read.

The question that drove the attack: what happens if a valid session of another user is used instead of my own session, together with the IdToken of my account and the username of the other user account? Manipulating the parameters of the RespondToAuthChallenge request answered it.

Exploitation

Step 1: obtain the attacker's IdToken

The attacker logs in to their own account to get a valid IdToken belonging to that account.

Step 2: obtain the victim's session with InitiateAuth

The attacker sends a POST request to the AWS InitiateAuth endpoint to retrieve a valid session belonging to the victim account. In BurpSuite Repeater, a new window is opened with this request:

HTTP
POST / HTTP/2Host: cognito-idp.us-east-1.amazonaws.comUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0Accept: */*Accept-Language: en-US,en;q=0.5Accept-Encoding: gzip, deflateReferer: https://redirect.com/Content-Type: application/x-amz-json-1.1X-Amz-Target: AWSCognitoIdentityProviderService.InitiateAuthX-Amz-User-Agent: aws-amplify/5.0.4 jsContent-Length: 155Origin: https://redirect.comSec-Fetch-Dest: emptySec-Fetch-Mode: corsSec-Fetch-Site: cross-siteTe: trailers{"AuthFlow":"CUSTOM_AUTH","ClientId":"targetAppClientId","AuthParameters":{"USERNAME":"XXXXXXXXXXXXXXXXXXX"},"ClientMetadata":{}}

Line by line, in plain words:

  • POST / HTTP/2 says this is a POST request (a request that sends data) to the root path, over HTTP version 2.
  • Host: cognito-idp.us-east-1.amazonaws.com says which server the request is going to: Amazon's Cognito service in the us-east-1 region.
  • User-Agent, Accept, Accept-Language, Accept-Encoding are standard headers that describe the browser and what it will accept back. They are copied from a real browser so the request looks normal.
  • Referer: https://redirect.com/ tells the server which page the request came from.
  • Content-Type: application/x-amz-json-1.1 says the body is JSON in Amazon's particular flavour.
  • X-Amz-Target: AWSCognitoIdentityProviderService.InitiateAuth is the important one: it tells Cognito which operation to run. Here it is InitiateAuth, the operation that starts a sign-in.
  • X-Amz-User-Agent: aws-amplify/5.0.4 js identifies the client library the app uses.
  • Content-Length: 155 is the size of the body in bytes.
  • Origin: https://redirect.com says which site the request is being made on behalf of.
  • Sec-Fetch-* and Te are browser security and transport headers.
  • The body is JSON: AuthFlow is set to CUSTOM_AUTH, which selects the custom authentication flow; ClientId identifies the app; AuthParameters contains the USERNAME; ClientMetadata is an empty object.

The X-Amz-Target header selects the AWSCognitoIdentityProviderService.InitiateAuth operation, the body sets AuthFlow to CUSTOM_AUTH and identifies the app with ClientId. The victim's email is put as the value of the USERNAME parameter. The response body is:

JSON
{"ChallengeName":"CUSTOM_CHALLENGE","ChallengeParameters":{"USERNAME":"VICTIM_USERNAME","email":"Victim's email"},"Session":"Victim's session"}

So the call returns a CUSTOM_CHALLENGE, the victim's username and email in the challenge parameters, and a session belonging to the victim. In other words, just by naming the victim's email, the attacker gets a ticket that the system considers to belong to the victim. The response echoes back the victim's username and email in the challenge parameters.

Step 3: exchange the victim's session for the victim's tokens

The attacker sends a POST request to the AWS RespondToAuthChallenge endpoint to retrieve valid tokens belonging to the victim account, while using an IdToken for the attacker's account. In BurpSuite Repeater, another window is opened with this request:

HTTP
POST / HTTP/2Host: cognito-idp.us-east-1.amazonaws.comUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0Accept: */*Accept-Language: en-US,en;q=0.5Accept-Encoding: gzip, deflateReferer: https://redirect.com/Content-Type: application/x-amz-json-1.1X-Amz-Target: AWSCognitoIdentityProviderService.RespondToAuthChallengeX-Amz-User-Agent: aws-amplify/5.0.4 jsContent-Length: 2385Origin: https://redirect.comSec-Fetch-Dest: emptySec-Fetch-Mode: corsSec-Fetch-Site: cross-siteTe: trailers{"ChallengeName":"CUSTOM_CHALLENGE","ChallengeResponses":{"USERNAME":"VICTIM-USERNAME","ANSWER":"ATTACKER_IdToken"},"ClientId":"targetAppClientId","Session":"VICTIM'S_SESSION"}

Line by line:

  • The headers are the same as before, except X-Amz-Target now says AWSCognitoIdentityProviderService.RespondToAuthChallenge, which is the operation that answers a challenge.
  • Content-Length: 2385 is larger because the body now contains a full IdToken, which is a long string.
  • In the body, ChallengeName is CUSTOM_CHALLENGE, matching what the previous response said.
  • ChallengeResponses holds the answer: USERNAME is the victim's username, and ANSWER is the attacker's IdToken.
  • ClientId is the same app identifier as before.
  • Session is the victim's session from the previous response.

The three values that matter are set as follows:

  1. The victim's username as the value of the USERNAME parameter.
  2. The attacker's IdToken as the value of the ANSWER parameter. The attacker's IdToken must be valid.
  3. The victim's session obtained from the previous request as the value of the SESSION parameter.

The response body is:

JSON
{"AuthenticationResult":{"AccessToken":"VICTIM'S-AccessToken","ExpiresIn":3600,"IdToken":"VICTIM'S_IdToken","RefreshToken":"VICTIM'S_RefreshToken","TokenType":"Bearer"},"ChallengeParameters":{}}

The attacker now holds the victim's access token, ID token and refresh token, with an ExpiresIn of 3600 seconds. ExpiresIn is how long, in seconds, the access token stays valid: 3600 seconds is one hour. TokenType: Bearer means "whoever bears (carries) this token is treated as the user", which is exactly why stealing it is so damaging.

From the original on medium.com

Root cause

By manipulating the parameter values of the RespondToAuthChallenge request, the author found that:

  1. The session is validated and linked to the username.
  2. The validity of the IdToken is verified.
  3. It is not checked whether the IdToken is associated with this session and this username.

The checks are performed piece by piece: the session belongs to the victim, the IdToken is a genuine, valid token, and the username matches the session.: the session belongs to the victim, the IdToken is a genuine, valid token, and the username matches the session. Nothing ties the IdToken to the session or to the username it is presented with, so any valid IdToken from any account satisfies the challenge for any session.

A comparison: imagine a nightclub where the bouncer checks that your ticket is genuine, and checks that the name on the guest list matches the name you gave, but never checks that the ticket has your name on it. Anyone with any genuine ticket gets in under any name on the list.

Impact

An attacker who knows a victim's username or email can obtain that victim's access token, ID token and refresh token. The author notes the bug was not fixed at the time of writing and does not describe the program or its bounty.

Fix

The report does not describe a fix. : the identity in the verified token has to match the identity the session was issued for, and the request must be rejected otherwise.

Variants to test

  • Other custom challenge answers. On any Cognito-backed app using CUSTOM_AUTH, replay RespondToAuthChallenge with a session from one account and a valid IdToken from another. A positive result is an AuthenticationResult containing tokens for the session's account.
  • A positive result is a successful link that should have been rejected.

Glossary

  • Account takeover: an attack in which someone else gains control of a victim's account, usually by stealing their login credentials or tokens.
  • API (Application Programming Interface): the set of URLs and rules one program uses to talk to another.
  • API endpoint: one specific URL in an API, each doing one job.
  • AccessToken: a Cognito token that authorises API operations in the context of a user, for example changing user attributes.
  • Amazon Cognito: an AWS service that handles sign-up, sign-in and permissions for web and mobile apps.
  • AWS (Amazon Web Services): Amazon's cloud platform, which hosts many services including Cognito.
  • AWS Lambda trigger: a small piece of code that AWS runs automatically at a particular moment in a flow.
  • Boolean: a value that is either true or false.
  • BurpSuite: a tool that sits between a browser and a website so requests can be seen, paused, edited and resent.
  • Claim: a statement inside a token, such as the user's email address.
  • Client: the program (browser or app) that talks to a server.
  • ClientId: an identifier that tells Cognito which application is making the request.
  • Content-Type: a header that says what format the body of a request is in.
  • Cookie: a small piece of data a website stores in your browser and that your browser sends back on later requests.
  • CUSTOM_AUTH: the value of AuthFlow that selects Cognito's custom authentication flow.
  • CUSTOM_CHALLENGE: the challenge name returned when the custom flow wants the app to answer a challenge.
  • DefineAuthChallenge: the Lambda trigger that decides what the next challenge should be.
  • CreateAuthChallenge: the Lambda trigger that builds the challenge and its parameters.
  • VerifyAuthChallengeResponse: the Lambda trigger that checks whether the answer to a challenge is valid.
  • Header: metadata attached to a request or response, like a label on a parcel.
  • HTTP: the protocol browsers and servers use to exchange requests and responses.
  • HTTP/2: a newer version of HTTP.
  • IdP (Identity Provider): a service that vouches for a user's identity, such as Google or Facebook.
  • IdToken: a Cognito token containing claims about the authenticated user, such as name, email and phone number.
  • Identity pool: the Cognito component that grants users access to other AWS services.
  • IDOR (Insecure Direct Object Reference): a bug where the server accepts an identifier from the client and acts on it without checking the client is allowed to use it.
  • InitiateAuth: the Cognito operation that starts a sign-in and may return a challenge and a session.
  • JSON: a text format for structured data using curly braces, quotes and colons.
  • JWT (JSON Web Token): a token with three dot-separated parts, the middle one holding claims and the last one a signature.
  • Lambda: see AWS Lambda trigger.
  • MFA (Multi-Factor Authentication): a second proof of identity on top of a password, such as an SMS code.
  • OAuth: the system that lets a site sign you in via another provider such as Google, without seeing your password there.
  • Origin: a header that says which site a request is being made on behalf of.
  • POST: an HTTP request method used to send data to a server.
  • Recon (reconnaissance): looking around a target before attacking, to understand how it works.
  • Repeater: the part of BurpSuite that lets you edit and resend a single request.
  • RefreshToken: a Cognito token used to obtain new ID and access tokens.
  • RespondToAuthChallenge: the Cognito operation that answers a challenge and, if accepted, returns tokens.
  • Scope: a permission listed inside an access token.
  • Session: a short-lived handle Cognito gives the app so the next step of a sign-in can be tied back to the same attempt.
  • SRP (Secure Remote Password): a way of proving a password without sending it over the network.
  • State machine: a set of rules about which state can follow which.
  • Token: a string a server gives a client as proof that something, usually a login, has already happened.
  • User pool: the Cognito component that stores users and handles sign-up and sign-in.
  • X-Amz-Target: the header that tells Cognito which operation to run.
  • Referer: a header that says which page a request came from.
  • User-Agent: a header that describes the browser or client making the request.

More by Hossam Ahmed

View all

1 more disclosed report, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Account takeover

The lesson behind this report.

Getting into someone else's account through a flaw in sign-in, password reset, second factor, single sign-on or session handling.