XSS Attack your database to detect missing Output Encoding
December 11, 2009 .Net, Improve Your Code, Secure your code, Security, XSSAttack No CommentsCross Site Scripting (XSS) Attacks must be one of the most popular type of attacks of websites these days with maybe only SQL Injection attacks getting more attention that that.
XSS Attacks
The concept is quite simple for Persistent Attacks. User A attacks a website and via some vulnerability, it manages to push an XSS script into the storage (some database most of the times) of the website.
User B looks at the attacked website and part of the content delivered by the website the injected XSS from the User A is delivered to User B. Now, depending on what the script was, User A could have a great deal of control on what User A does and sees. From session stealing to running operation on behalf of User B.
These type of attacks can be devastating for a website with one of my preferred examples being the Samy is my hero XSS attack on MySpace back in 2006
Almost every year I talked at Microsoft TechEd I had to say something about XSS and how dangerous it is and I’m always surprised that very few people know about it and try to take measures to avoid XSS attacks on their websites.
Mitigations
Some of the mitigations factors are Input Validation, Input Filtering and Output Encoding. None of this mitigations have any value if used alone or incompletely.
Input Validation and Input Filtering are mandatory but don’t have them as your only protection. There are tools out there that can try to automatically throw hundreds of attacks at your application to see how it responds to input validation.
However I think Output encoding is the most critical one as the other ones are just security gates that sooner or later someone will be able to bypass one way or another. Output encoding is also the hardest one to test as you have to verify that your website properly encodes every bit of data that is/was/will be potentially modifiable by a malicious user.
XSS Attack Tool
This is where the XSS Attack Tool comes into place. The tool will simulate an attack on your database and update up to 5000 rows in every table and replace your strings in your database with random XSS attacks. Just imagine a malicious user had direct access to your database and tried to to his best to XSS attack your site.
After you run the tool go and browse your website. Considering the large number of XSS scripts injected in your database you should expect some of the scripts to surface in the UI as potential attacks. This will allow you to find and encode all the places where data could surface. The tool is not intended to cover every scenario possible so using the tool does not guarantee that your website is not vulnerable, however it’s yet another tool in the arsenal we are required to have to better protect our sites.
Usage
- Download XSS Attack 1.0.
- Unzip
- BACKUP your database.
- Now BACKUP your database again. Are you sure you did that? Do it one more time just to be sure you can get back to it.
- Modify the “Database” connection string in the XSSAttack.exe.config to point to your database
- Enter any Schema, Table or Column name (format Schema, Schema.Table or Schema.Table.Column) in the ignore field in the config file to let the tool know those should be ignored.
- Ignore tables like System Properties tables that contain properties required for your site to run or Reference Data tables that can’t be modified or never surface back to the user
- Run the tool and watch it update your database.
- The tool might fail to update some columns every now and then or fail if you have some custom indexes or referential integrity.
- Go back to your website and browse. If some scripts make it to the surface un-encoded you should see it pretty quickly.
- Note that you might have to do some manual updates to your database to get your site up and running again (e.g. fix your user name so you can login or your password).
- If ALL your website still looks good run the tool few more times and try again.
Note: The tool DOES NOT INSERT any data in your database. It only updates existing rows so make sure your database has a good sample set of your data that you can use for testing.
Demo
My current demo site, a GUID Store (just in case anyone wants to buy a GUID):
Running the tool:
Loading the website:
And few more alerts and errors.
I did a restore of the database and tried a new run of the tool and got different results:
The tool might give you false positives, e.g. modify columns that can never be modified by anyone and the results of which should not be encoded or not manage to create injections so take it’s results with a bit of salt. It’s for you to decide what a good result is and maybe try restore your database to a known state, configure the tool to ignore more columns and re-run the process.
Note: I’m not responsible for any loos, damage, security issues, fame or whatever before, during or after using this tool. This is a tool for developers and security reviewers so treat it as such. Make sure you backup your database before running this tool and make sure you never run this on a production system. The tool injects scripts from other websites that could be malicious to your browser or OS.
If you do find the tool useful then drop me an email at corneliu at acorns dot com dot au to let me know how you used it.
