Cross-site Scripting

Geeky much!
Secure You!!
6 min readMay 26, 2021

--

Cross-site scripting (XSS) is a security vulnerability typically found in web applications. It's a type of injection that can allow an attacker to execute malicious scripts and have them execute on a victim's machine.

A web application is vulnerable to XSS if it uses unsanitized user input. XSS is possible in Javascript, VBScript, Flash, and CSS.

The extent to the severity of this vulnerability depends on the type of XSS, which is normally split into two categories: persistent/stored and reflected. Depending on which, the following attacks are possible:

  • Cookie Stealing — Stealing your cookie from an authenticated session, allowing an attacker to log in as you without themselves having to provide authentication.
  • Keylogging — An attacker can register a keyboard event listener and send all of your keystrokes to their own server.
  • Webcam snapshot — Using HTML5 capabilities it's possible to even take snapshots from a compromised computer webcam.
  • Phishing — An attacker could either insert fake login forms into the page or have you redirected to a clone of a site tricking you into revealing your sensitive data.
  • Port Scanning — You read that correctly. You can use stored XSS to scan an internal network and identify other hosts on their network.
  • Other browser-based exploits — There are millions of possibilities with XSS.

Who knew this was all possible by just visiting a web page. There are measures put in place to prevent this from happening by your browser and anti-virus.

Stored XSS

Stored cross-site scripting is the most dangerous type of XSS. This is where a malicious string originates from the website's database. This often happens when a website allows user input that is not sanitized (remove the “bad parts” of a user's input) when inserted into the database.

An example

An attacker creates a payload in a field when signing up to a website that is stored in the website's database. If the website doesn’t properly sanitize that field, when the site displays that field on the page, it will execute the payload to everyone who visits it.

The payload could be as simple as <script>alert(1)</script>

However, this payload won't just execute in your browser but any other browsers that display the malicious data inserted into the database.

You can make an alert popup box appear on the page with your document cookies.

<script>alert(document.cookie);</script>

Reflected XSS

In a reflected cross-site scripting attack, the malicious payload is part of the victims request to the website. The website includes this payload in response back to the user. To summarise, an attacker needs to trick a victim into clicking a URL to execute their malicious payload.

This might seem harmless as it requires the victim to send a request containing an attackers payload, and a user wouldn’t attack themselves. However, attackers could trick the user into clicking their crafted link that contains their payload via social-engineering them via email..

Reflected XSS is the most common type of XSS attack.

An example

An attacker crafts a URL containing a malicious payload and sends it to the victim. The victim is tricked by the attacker into clicking the URL. The request could be http://example.com/search?keyword=<script>...</script>

The website then includes this malicious payload from the request in the response to the user. The victims browser will execute the payload inside the response. The data the script gathered is then sent back to the attacker (it might not necessarily be sent from the victim, but to another website where the attacker then gathers this data — this protects the attacker from directly receiving the victims data).

DOM-Based XSS

What is the DOM

In a DOM-based XSS attack, a malicious payload is not actually parsed by the victim’s browser until the website’s legitimate JavaScript is executed. So what does this mean?

With reflective xss, an attackers payload will be injected directly on the website and will not matter when other Javascript on the site gets loaded.

<html>
You searched for <em><script>...</script></em>
</html

With DOM-Based xss, an attackers payload will only be executed when the vulnerable Javascript code is either loaded or interacted with. It goes through a Javascript function like so:

var keyword = document.querySelector('#search')
keyword.innerHTML = <script>...</script>

Using XSS for IP and Port Scanning

Cross-site scripting can be used for all sorts of mischief, one being the ability to scan a victims internal network and look for open ports. If an attacker is interested in what other devices are connected on the network, they can use Javascript to make requests to a range of IP addresses and determine which one responds.

<script>
for (let i = 0; i < 256; i++) { // This is looping from 0 to 255
let ip = ‘192.168.0.’ + i // Creates variable for forming IP
// Creating an image element, if the resource can load, it logs to the /logs page.
let code = ‘<img src=”http://' + ip + ‘/favicon.ico” onload=”this.onerror=null; this.src=/log/’ + ip + ‘“>’
document.body.innerHTML += code // This is adding the image element to the webpage
}
</script>

XSS Keylogger

Javascript can be used for many things, including creating an event to listen for key-presses.

<script type=”text/javascript”>
let l = “”; // Variable to store key-strokes in
2003document.onkeypress = function (e) { // Event to listen for key presses
l += e.key; // If user types, log it to the l variable
console.log(l); // update this line to post to your own server
}
</script>

Filter Evasion

There are many techniques used to filter malicious payloads that are used with cross-site scripting.

Cross-site scripting are extremely common. Below are a few reports of XSS found in massive applications; you can get paid very well for finding and reporting these vulnerabilities.

Protection Methods

There are many ways to prevent XSS, here are the 3 ways to keep cross-site scripting our of your application.

  1. Escaping — Escape all user input. This means any data your application has received is secure before rendering it for your end users. By escaping user input, key characters in the data received bu the web age will be prevented from being interpreter in any malicious way. For example, you could disallow the < and > characters from being rendered.
  2. Validating Input — This is the process of ensuring your application is rendering the correct data and preventing malicious data from doing harm to your site, database and users. Input validation is disallowing certain characters from being submit in the first place.
  3. Sanitising — Lastly, sanitizing data is a strong defence but should not be used to battle XSS attacks alone. Sanitizing user input is especially helpful on sites that allow HTML markup, changing the unacceptable user input into an acceptable format. For example you could sanitise the < character into the HTML entity &#60;

Other Exploits

XSS is often overlooked but can have just as much impact as other big impact vulnerabilities. More often than not, its about stringing several vulnerabilities together to produce a bigger/better exploit. Below are some other interesting XSS related tools and websites.

BeEF is a penetration testing tool that focuses on the web browser. The concept here is that you “hook” a browser (using XSS), then you are able to launch and control a range of different attacks.

BeEF allows the professional penetration tester to assess the actual security posture of a target environment by using client-side attack vectors.

--

--