# WEB16: OAuth 2.0 authentication vulnerabilities [Lí thuyết](https://infosecwriteups.com/oauth-2-0-hacking-simplified-part-1-understanding-basics-ad323cb4a05c) ## Lab: Authentication bypass via OAuth implicit flow ```! This lab uses an OAuth service to allow users to log in with their social media account. Flawed validation by the client application makes it possible for an attacker to log in to other users' accounts without knowing their password. To solve the lab, log in to Carlos's account. His email address is carlos@carlos-montoya.net. You can log in with your own social media account using the following credentials: wiener:peter. ``` OAuth flow sẽ như sau (từ 46 -> 59) ![](https://i.imgur.com/XzLgXJU.png) ta đặc biệt chú ý đến request 58, sau khi đã có được token từ oauth server, POST request được gửi đến /authenticate để xác thực người dùng. Tuy nhiên nếu thử thay đổi email address thành carlos@carlos-montoya.net ![](https://i.imgur.com/p5so7Iy.png) => Thành công login vào account Carlos ![](https://i.imgur.com/G2ZVxuV.png) ## Lab: Forced OAuth profile linking ```! This lab gives you the option to attach a social media profile to your account so that you can log in via OAuth instead of using the normal username and password. Due to the insecure implementation of the OAuth flow by the client application, an attacker can manipulate this functionality to obtain access to other users' accounts. To solve the lab, use a CSRF attack to attach your own social media profile to the admin user's account on the blog website, then access the admin panel and delete Carlos. The admin user will open anything you send from the exploit server and they always have an active session on the blog website. You can log in to your own accounts using the following credentials: Blog website account: wiener:peter Social media profile: peter.wiener:hotdog ``` Trang hỗ trợ hai cách để login đó là dùng username/password hoặc login với social media account: ![](https://i.imgur.com/Q2oRJpZ.png) Thử login với cách thứ nhất, ta thấy có chức năng link profile với social account: ![](https://i.imgur.com/AiaOm1M.png) Mục tiêu sẽ là lợi dụng tính năng này để link account admin của trang web với social media account của ta. Ấn "Attack a social profile", các request sau được gửi ![](https://i.imgur.com/0TEmHvE.png) ![](https://i.imgur.com/zyv6KTb.png) ![](https://i.imgur.com/v4irArh.png) Ở đây, ta để ý request đến `/oauth-linking` không bao gồm param `state` -> có thể csrf để khiến admin link profile social media account của user wiener với account admin của trang web Turn on Intercept và bắt ngay tại request đã nói ở trên, lưu lại trường `code` sau đó drop request, setup exploit server ![](https://i.imgur.com/gefosz7.png) Delivery to admin, sau đó logout và ngay tại my-account chọn Login với social media ![](https://i.imgur.com/aNEwYcJ.png) => Thành công link đc với admin social media account ![](https://i.imgur.com/f6gGgj3.png) ## Lab: OAuth account hijacking via redirect_uri ```! This lab uses an OAuth service to allow users to log in with their social media account. A misconfiguration by the OAuth provider makes it possible for an attacker to steal authorization codes associated with other users' accounts. To solve the lab, steal an authorization code associated with the admin user, then use it to access their account and delete Carlos. 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 with your own social media account using the following credentials: wiener:peter. ``` Click vào my-account, sẽ được redirect đến trang login với social media ![](https://i.imgur.com/rZGi0tn.png) ![](https://i.imgur.com/KkWvOve.png) Dựa vào đề bài, ta thử thay đổi giá trị của redirect_uri đến burp collaborator: ![](https://i.imgur.com/GslHrIS.png) ![](https://i.imgur.com/w5XvawJ.png) Kết quả: ![](https://i.imgur.com/KhXfdby.png) Vậy ý tưởng sẽ là csrf để lấy giá trị `code` của user admin sau đó access vào đường dẫn `https://0a0000f7034aed788007124600f20012.web-security-academy.net/oauth-callback?code=<admin_code>` Setup exploit server ![](https://i.imgur.com/gQnujw3.png) Admin code: ![](https://i.imgur.com/rWlrA4g.png) Access vào url ![](https://i.imgur.com/nSYgnpn.png) Kết quả ![](https://i.imgur.com/2qv18OD.png) Delete user carlos và solve bài lab ![](https://i.imgur.com/X8bAZyu.png) ## Lab: Stealing OAuth access tokens via an open redirect ```! 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 an open redirect on the blog website and use this 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. *You cannot access the admin's API key by simply logging in to their account on the client application.* 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 ``` Thử thay đổi giá trị của redirect_uri như trong bài lab trước, server trả về phản hồi như sau ![](https://i.imgur.com/q0JK7YZ.png) Vậy ta cần tìm một bug khác trong chính trang web này để bypass. Sau một hồi fuzzing, phát hiện ra bug client side open redirect tại tính năng Next post: ![](https://i.imgur.com/zMFoIgn.png) ![](https://i.imgur.com/wNWzGzs.png) ![](https://i.imgur.com/satkOtA.png) ![](https://i.imgur.com/YQRC8JV.png) Setup exploit server, vì ta sẽ steal access_token của implicit grant type nên cần thêm một đoạn script để lấy access_token từ fragment: ![](https://i.imgur.com/V2sovyq.png) Check access log: ![](https://i.imgur.com/3qf42aS.png) Cóp access_token, sau đó bỏ vào header `Authorization` trong request gửi đến `/me` ![](https://i.imgur.com/BEUXJYX.png) Solve bài lab ![](https://i.imgur.com/1z6qT2O.png) ## Lab: Stealing OAuth access tokens via a proxy page ```! 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. ``` Ở request để fetch về content của postid 1, ta thấy trang web sử dụng iframe để load `/post/comment/comment-form` ![](https://i.imgur.com/D9jFex4.png) Check response khi `GET /post/comment/comment-form`, xuất hiện các đoạn mã js sử dụng hàm postMessage để gửi data - `window.location.href` đến parent của chúng. Và ta có thể lợi dụng điểm này để gửi access_token đến exploit server ![](https://i.imgur.com/EfbFCCG.png) Setup exploit server như sau: ![](https://i.imgur.com/BR18PJG.png) Check access log: ![](https://i.imgur.com/ph3Q2k2.png) Gửi access_token của admin đến server để lấy api key ![](https://i.imgur.com/hL4FyCm.png) Solve bài lab ![](https://i.imgur.com/F5U7ihq.png) ## Lab: SSRF via OpenID dynamic client registration ```! This lab allows client applications to dynamically register themselves with the OAuth service via a dedicated registration endpoint. Some client-specific data is used in an unsafe way by the OAuth service, which exposes a potential vector for SSRF. To solve the lab, craft an SSRF attack to access http://169.254.169.254/latest/meta-data/iam/security-credentials/admin/ and steal the secret access key for the OAuth provider's cloud environment. You can log in to your own account using the following credentials: wiener:peter ``` ![](https://i.imgur.com/vYT1dNb.png) ta tìm được registration endpoint: `https://oauth-0a4400fd04a3be9381340a9302b8006c.oauth-server.net/reg` Thử register một client application với redirect_uri là `https://blabla.com` ![](https://i.imgur.com/I8TnKX4.png) -> server trả về các thông số khác cũng như client_id. Trong flow Oauth login của ứng dụng, ta thấy có một request đến `/client/<client_id>/logo` để lấy về logo của client application. ![](https://i.imgur.com/Jhwhzao.png) và ta có thể register nó thông qua trường `logo_uri` Register lại, bổ sung thêm `logo_uri` ![](https://i.imgur.com/UTyfHZH.png) gửi `GET /client/TK8BhwGGrqEYx-ypyAFl8/logo` ![](https://i.imgur.com/LBAEslz.png) submit secret accecss key và solve bài lab ![](https://i.imgur.com/Hqbm2vA.png) ###### tags: `portswigger`