# Portswigger Clickjacking 🙄 <style>body {text-align: justify}</style> Hi, dưới đây là writeup của 5/5 bài lab về [Clickjacking](https://portswigger.net/web-security/clickjacking) mình đã solve trong quá trình ôn tập thi chứng chỉ của Portswigger. ### 1. Basic clickjacking with CSRF token protection ##### Description > 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` ##### Writeup Sau khi đăng nhập bằng `wiener:peter`, tại trang `/my-account` xuất hiện chức năng `Delete account` được bảo vệ bằng csrf token. ![](https://i.imgur.com/anTDhaX.png) Ta sẽ tạo một trang web giả để đánh lừa nạn nhân click vào `Delete account` của họ bằng kĩ thuật Clickjacking. Trang web giả có mã HTML như sau: ```htmlembedded <style> iframe { position:relative; width:1000px; height: 600px; opacity: 0.00001; z-index: 2; } div { position:absolute; top: 512px; left: 65px; z-index: 1; } </style> <div>Click me</div> <iframe src="https://0aa100f703714afac0d7d6e300cb00a6.web-security-academy.net/my-account"></iframe> ``` Cụ thể nó sẽ tạo một iframe của trang `/my-account` và sử dụng CSS làm sao cho dòng chữ `Click me` sẽ đè lên `Delete account`. Tất nhiên iframe sẽ bị làm trong suốt đi bằng thuộc tính `opacity`. Và vì nạn nhân luôn click vào chữ `click` nên nó sẽ vô tình kích hoạt chức năng `Delete account`. Tạo trang web giả trên bằng exploit-server. ![](https://i.imgur.com/eizjr1u.png) Đây là lúc căn chỉnh `Click me` sao cho đè lên `Delete account`. ![](https://i.imgur.com/b9WeNkq.png) Chỉnh `opacity: 0.00001;` để iframe trong suốt gần như hoàn toàn và chứa chữ `Click me` để đánh lừa nạn nhân. ![](https://i.imgur.com/BqhxwjB.png) Lúc này `Deliver exploit to victim`, do nạn nhân đã ấn vào `Click me` tức là nó đã `Delete account` chính nó &rarr; ta solve được challenge. ![](https://i.imgur.com/hfhfv0g.png) ### 2. Clickjacking with form input data prefilled from a URL parameter ##### Description > 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 ##### Writeup Mục tiêu của bài lab này là khiến nạn nhân `Update email` bất kì bằng kĩ thuật Clickjacking. Điểm đặc biệt ở form `Update email` này, email cần thay đổi sẽ được tự động trích xuất từ tham số `email` trên URL &rarr; ta cần iframe với source là trang `/my-account` với tham số `email`. ![](https://i.imgur.com/xkGwcwF.png) Sử dụng thử ClickBandit để tạo payload đối với chức năng `Update email` tại đường dẫn `/my-account?email=hacked@gmail.com` để thay đổi email nạn nhân thành `hacked@gmail.com`. ![](https://i.imgur.com/8FtP5Z5.png) Sau khi hoàn thành, click `Save` thì ClickBandit sẽ trả về mã nguồn trang giả mạo cho mình. Sử dụng mã nguồn đó vào exploit-server. ![](https://i.imgur.com/rxq0man.png) `View exploit` thử thì thấy button `Click`. ![](https://i.imgur.com/ytxHdJT.png) Khi click vào thì ta sẽ clickjacking thành công. ![](https://i.imgur.com/31V0JKw.png) Kiểm tra account lúc này email đã bị thay đổi. ![](https://i.imgur.com/VNkq4IY.png) Lúc này chỉ cần `Deliver exploit to victim`, ta solve thành công challenge. ![](https://i.imgur.com/bCE9NOk.png) ### 3. Clickjacking with a frame buster script ##### Description > 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` ##### Writeup Tương tự bài lab 2, ta sẽ cố gắng Clickjacking đối với chức năng `Update email`. Tham số `email` trên URL được truyền sẽ tự động ghi vào form `Update email` như bài lab 2. ![](https://i.imgur.com/hVDBIyA.png) Tuy nhiên, khi đọc thêm mã nguồn HTML, xuất hiện 1 đoạn script gọi là frame buster script, có chức năng ngăn chặn trang `/my-account` này bị framed bằng cách kiểm tra xem trang `/my-account` hiện tại có phải top window hay không. ![](https://i.imgur.com/ls8hZ7x.png) Tuy nhiên ta sẽ có cách bypass nó đó là sử dụng thuộc tính `sandbox` trong `<iframe>` không chứa giá trị `allow-top-navigation` vì khi đó, iframe không thể check được nó là top window hay không &rarr; frame buster script trên bị vô hiệu hóa. Bên cạnh đó, để submit form `Update email`, ta cần `sandbox` có giá trị `allow-forms`. Ý tưởng payload sẽ giống như 2 lab trên. ```htmlembedded <style> iframe { position:relative; width:1000px; height: 600px; opacity: 0.00001; z-index: 2; } div { position:absolute; top: 466px; left: 69px; z-index: 1; } </style> <div>Click me</div> <iframe src="https://0a3f009103ef1abbc3261402001600ff.web-security-academy.net/my-account?email=hacked@gmail.com" sandbox="allow-forms"></iframe> ``` Nhét payload vào exploit-server và xem thử, ta đã framed trang `my-account` thành công và căn chỉnh cho dòng chữ `Click me` đè lên `Update mail`. ![](https://i.imgur.com/HGjpgLc.png) Bây giờ chỉ cần set `opacity: 0.00001;` để frame được trong suốt và `Deliver exploit to victim`, ta solve được challenge. ![](https://i.imgur.com/U1uM1Ge.png) ### 4. Exploiting clickjacking vulnerability to trigger DOM-based XSS ##### Description > 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. ##### Writeup Ứng dụng web có chức năng feedback tại `/feedback` nhận các tham số trên URL làm giá trị cho các input tương ứng của form. ![](https://i.imgur.com/I3bUxUL.png) Khi submit form thì có thấy một thông báo chứa giá trị của tham số `name`. ![](https://i.imgur.com/DIywfS6.png) Đọc mã nguồn HTML thì có 1 file js xử lí việc này `/resources/js/submitFeedback.js`. Cụ thể, sink `innerHTML` được sử dụng để in ra dòng này với source là `name`. ![](https://i.imgur.com/ikjkfmJ.png) Source `name` đó chính là trường `name` mình input. Có thể thấy không có bất kì cơ chế validate nào &rarr; có thể dính DOM XSS. ![](https://i.imgur.com/TjEwWid.png) Do dùng sink `innerHTML` nên ta sẽ sử dụng payload `<img src=1 onerror=print()>` tại trường `name`. Kết quả sau khi submit thì hàm `print()` được thực thi thành công. ![](https://i.imgur.com/RWFDw6Q.png) Như vậy ta đã DOM XSS thành công, bây giờ sẽ tạo payload Clickjacking tại trang `/feedback` sao cho button `Click me` đè lên button `Submit feedback`. Tất nhiên tham số `name` tại URL sẽ là XSS payload `<img src=1 onerror=print()>`. ```htmlembedded <style> iframe { position:relative; width:1000px; height: 850px; opacity: 0.5; z-index: 2; } button { position:absolute; top: 801px; left: 73px; z-index: 1; } </style> <button>Click me</button> <iframe src="https://0a43006103d61e03c0f8901400af00e1.web-security-academy.net/feedback?name=%3Cimg%20src=1%20onerror=print()%3E&email=hacked@gmail.com&subject=XSS&message=DOM%20XSS%20Clickjacking"></iframe> ``` Căn chỉnh sao cho button `Click me` đè lên `Submit feedback`. ![](https://i.imgur.com/3eCSHQb.png) Chỉnh `opacity: 0.00001;` để làm trong suốt frame rồi click thử ta thấy `print()` thành công. ![](https://i.imgur.com/0WdIinl.png) `Deliver exploit to victim` và ta solve được challenge. ![](https://i.imgur.com/wkd6KlV.png) ### 5. Multistep clickjacking ##### Description > 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` ##### Writeup Bài lab này tương tự bài lab 1 là cần đánh lừa nạn nhân `Delete account`. ![](https://i.imgur.com/kjAVdls.png) Tuy nhiên ở đây khi `Delete account` thì sẽ có thêm 1 bước confirm &rarr; ta cần tạo payload chứa 2 button `Click` và khiến nạn nhân click theo đúng thứ tự `Delete account` &rarr; `Yes`. ![](https://i.imgur.com/iPRei83.png) Payload tương tự các bài trên. ```htmlembedded <style> iframe { position:relative; width:1000px; height: 850px; opacity: 0.000001; z-index: 2; } #first { position:absolute; top: 509px; left: 44px; z-index: 1; } #second { position:absolute; top: 308px; left: 192px; z-index: 1; } </style> <button id="first">Click me first</button> <button id="second">Click me next</button> <iframe src="https://0a3700be03ff9e3cc02e4f1200a90028.web-security-academy.net/my-account"></iframe> ``` Căn chỉnh sao cho `Click me first` đè lên `Delete account` ![](https://i.imgur.com/q2Av3o0.png) và `Click me next` đè lên `Yes`. ![](https://i.imgur.com/n7DhFU7.png) `Deliver exploit to victim` và ta solve được challenge. ![](https://i.imgur.com/tyTzPn8.png) ## *Countermeasures - Using **X-Frame-Options**: `'deny', 'sameorigin', 'allow-from <URL>'` - Using **Content Security Policy (CSP)**: `frame-ancestors` - More flexible than using the X-Frame-Options header because you can specify multiple domains and use wildcards. - CSP also validates each frame in the parent frame hierarchy, whereas X-Frame-Options only validates the top-level frame. ###### tags: `portswigger`, `clickjacking`, `client-side`