A two-minutes post on fixing Cross site scripting issues

Filed under:Cross site scripting — posted by Consultant on October 11, 2007 @ 3:54 pm

Hey programmers, this is simply a short and small recommendation, generic and not language-dependent. Please take two minutes or less to go through this post if you are new to the world of cross site scripting and you’re in the process of learning how to code securely.
It is critical that you don’t use a single encoding function in a find and replace manner. Some websites will blindly recommend programmers to use HTML encoding functions (i.e.: Server.HtmlEncode in ASP) for encoding on output, however, HTML encoding can only do some good on specific cases. It is extremely important that you understand the context where the vulnerability is taking place in order to understand what characters you need to encode and the way the characters need to be encoded.

For example, HTML encoding will usually just encode a small set of 4 characters being < > &amp; ” into their HTML entities (&lt; &gt; &amp; “) and using it on the following example will do absolutely nothing to fix the issue that exists in this PHP line:

&lt;input value=’&lt;?=dynamic_dangerous_variable?&gt;’ type=”text” id=name&gt;

If you are wondering why, then look at the characters that are enclosing the contents of the value attribute and refer back to the 4 characters the HTML encoding function encoded. Right, the function does not take care of encoding single quotes, therefore the issue still exists. An attacker could easily use single quotes to escape from the value attribute and either continue writing attributes for the input tag (such as onclick, onmouseover, …) or, if the encoding function allows it (which is not the case here) close the input tag and continue writing the malicious payload.

Strong encoding libraries such as Reform, the multi-language encoding library originally developed by Michael Eddington and now adopted by the OWASP project provide several sets of encoding functions to be used in different contexts.

The Reform library is available at the OWASP encoding project (http://www.owasp.org/index.php/Category:OWASP_Encoding_Project)

Another thing to have in mind is NOT to opt for generic filtering layers instead. Take some more time and perform encoding wherever needed, that is the right thing to do. Filtering layers could turn out strong but strong filtering layers mean several headaches. Why? If you look around, you will find that Cross site scripting attacks have evolved incredibly ever since and so have the techniques used to disguise Cross site scripting attacks.

A full listing of the different techniques used to disguise XSS attacks can be found at the Cross Site Scripting Cheat Sheet available at http://ha.ckers.org/xss.html (it  goes offline every now and then, so be patient).

Hence if what you want to do is ‘filter all malicious characters’ you will end up filtering a really wide set of characters that you will most likely need for your application to run.

Should I have time and the will, you will soon find another post with regards to different techniques that exist in order to bypass filtering layers. You may want to test them on your own layers and see what happens :)

Finally, you may want to consider avoiding any encoding on situations where the dynamic data being displayed to the browser is a URL. It is usually very easy to break functionality (break the url for instance) and that is something you definitely do not want to happen. You might fix the XSS issue but you will end up having a problem in QA. Therefore you can either validate the Url using a regex or if the dynamic portion of the Url is the querystring, encoding the querystring values using some kind of a UrlEncode method.

.-

Java programmers can also defend against XSS

Filed under:Cross site scripting — posted by Consultant on @ 3:44 pm

Cross site scripting issues are everywhere - I’ll try to provide as many resources as possible for programmers in different programming languages and show them how to take the basic steps in the way of protection.

Remember the basics: Always perform encoding at the time of displaying dynamic data to browsers. If you browse around the blog you should find more information on XSS and how to educate yourself as a programmer or individual (well programmers are individuals too, yeah.)

If your programming language is Java, then you should take a look at the StringEscapeUtils java class which belongs to the Apache commons namespace. It provides a nice set of encoding methods/functions that you need to have handy at the time of programming web based interfaces.

The Url for such class can be found here:

http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/StringEscapeUtils.html

In addition to providing encoding methods, the StringEscapeUtils class also provides decoding functionality - both encoding and decoding available in their Html, Javascript, Java, Xml and SQL versions (this last one not meant for XSS, but worth mentioning.)

Here’s a table of the different methods and references:

static String escapeHtml(String str)
Escapes the characters in a String using HTML entities.

I will also cover in future posts any other encoding libraries available out there. For the moment, a nice alternative could be Michael Eddington’s encoding library named ‘Reform’ and which has recently (?) been adopted by the OWASP Encoding project. The library and the project can be found at:

http://www.owasp.org/index.php/Category:OWASP_Encoding_Project

Hope that’s enough information for now.

Perl programmers, protect against XSS

Filed under:Cross site scripting — posted by Consultant on @ 3:34 pm

This post is meant for any Perl programmers out there, in order to give them a hint at the time of developing code which is meant to be safe (can’t guarantee!) from Cross site scripting problems.  The main message, as always, is make sure that you are encoding your data before it gets displayed to browsers, especially when this information comes from untrusted containers (i.e.: user input, databases, etc.) You can either go for HTML-Encoding or URL-encoding, or even any home-grown methods of your choice, depending on the  context behind what’s being encoded and where it is being placed.

www-perl 

An alternative using www-perl would be:

use CGI::Escape;
print “Information”, HTML::Entities::encode($text);
print “It is located in (URL)”, HTML::Entities::encode($text);

Apache utils

A different alternative now using Apache utils can be:

use Apache::Util;


$e->print(Apache::Util::escape_html($myText), ”
“);
$e->print(”<a href=”/”>link</a>”);

I will keep working on this information in future posts - this is just a first step forward.



image: detail of installation by Bronwyn Lace