Cross-Site Scripting (XSS) Introduction

 

Cross-Site Scripting (XSS) Introduction

Note** The following examples utilize Web for Penetration Testers which I do not own any rights to. These examples provide solutions to the live image of Web for Pentester 1 which can be downloaded at https://pentesterlab.com/exercises/web_for_pentester/course. Please attempt these exercises on your own before reviewing my solutions to gain maximum benefit.

 

Introduction

XSS is an often-overlooked vulnerability. The generic “Hello World” example of XSS (e.g. <script>alert(“XSS”);</script>) is an overly benign example. Once an XSS vulnerability is discovered it becomes possible for attackers to modify the webpage to re-direct users to malicious site, steal a user’s session or cookie, or bypass a log in.

Because XSS is such a huge topic, a series will be developed with exploitations to follow. For this blog article, XSS detection will be focused on. Eventually this series will cover JSON, XML, XPath, JQuery, and other types of injections which are like XSS.

XSS Types

XSS can be broadly categorized into client side (e.g. browsers, connecting computers, etc.) and server side (e.g. web server, database, server log files, etc.) attacks. They can be further categorized in Reflected, DOM and Stored XSS though these definitions can become confusing as they overlap each other. OWASP provides an excellent explanation of each type of XSS in the link below:

https://owasp.org/www-community/Types_of_Cross-Site_Scripting

XSS Detection  

Pentester Lab 1 XSS example 6 will be used as an example for how XSS can be detected. Before detection can begin it is a good idea to see if your browser has any XSS prevention features and disable them. Browsers such as IE and Chrome will attempt to prevent XSS; in this example the Firefox browser is used.

Press F12 to bring up the browser console and attempt to enter the following address below in the browser:

http://<ipv4>/xss/example1.php?name=<script>console.log(“XSS”)</script>

You will likely get the following response below:

Looking at the above image you can see there is an unexpected token Syntax Error. This is an excellent clue to the XSS evasion that is being implemented. Pressing “Ctrl+u” on the page reveals the page’s source code. A search could be performed on “XSS” will bring us to the line in the source code:

It looks like user input is being set to variable “a” and this variable is getting wrapped in a script. We can attempt to close this line off and comment out any additional code with the following line:

http://<ipv4>/xss/example6.php?name=</script><script>console.log(“XSS”);</script>//

When we send this GET request to the site, we see the XSS message appear in the browser console. Note the developer console’s Inspector, Console, and Network tabs are useful when detecting XSS vulnerabilities. The Linux terminal command curl can also be useful for detecting and exploiting XSS vulnerabilities and could be added in a python script (article on this later).

Developers will sometimes try to be clever by suppressing certain characters or tags as seen in the Pentesters Lab source code for example 3:

$name = preg_replace(“/<script>/i”,””,$name);

$name = preg_replace(“/</script>/i”,””,$name);

In this example tries to suppress the opening and closing script tags. Fortunately (or unfortunately if you’re the developer) this can be easily circumvented by using other tags such as the BODY tag:

 http://<ipv4>/xss/example3.php?name=<BODY onload=alert(1);

Blacklisting tags such as <script> or events such as “alert” is generally a bad idea for preventing XSS as they can easily be maneuvered around with other tags or events. It’s normally better to instead develop a whitelist of characters that’ll be used in the site and then work with suppressing XSS with those necessary characters. XSS is particularly difficult to prevent though, an article on XSS prevention will come later.

There are many brute force methods for detecting XSS and the following sites can help in developing a script for these brute-force attempts:

https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/XSS%20Injection#xss-in-files

https://owasp.org/www-community/xss-filter-evasion-cheatsheet

BeEF is also a great tool for detecting and exploiting XSS vulnerabilities and will be used in a subsequent article.

Conclusion

You may be asking yourself at this point, “So what? I found a vulnerability in some page source code and was able to reflect text to a console. How would this be beneficial in compromising a site?” This is where the HTML DOM objects comes into play. Payloads such as document.cookie, window.location.href, and other objects could be used to steal session cookies or re-direct users to malicious sites.

 

References

maxrodrigo. (2020). PayloadsAllTheThings. Retrieved from

https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/XSS%20Injection#xss-in-files

OWASP. (2020). Types of XSS. Retrieved from

https://owasp.org/www-community/Types_of_Cross-Site_Scripting

OWASP. (2020). XSS Filter Evasion Cheatsheet. Retrieved from

            https://owasp.org/www-community/xss-filter-evasion-cheatsheet

W3Schools. (2020). HTML DOM. Retrieved from w3schools.com

Comments

Popular posts from this blog

Covering Your Tracks

Covering Your Tracks - Anti-forensics for the Cloud - Introduction