Title: IIRF: A decent URL rewriter ISAPI filter
Slug: iirf-a-decent-url-rewriter-isapi-filter
Date: 2008-05-27 14:45:00
Author: Kartones
Lang: en
Tags: Security, Windows, Systems-IT
Description: A description of the IIRF URL rewriter ISAPI filter

 <p>IT's been a long time since my last security-related post, but from time to time I'm assigned small tasks related to it. Last one, just finished today, was doing some research and proof of concepts about ISAPI filters for a spanish company, to harden their servers and protect them from <acronym title="Cross Site Scripting">XSS</acronym> and SQL Injection.</p> <p>One of my colleages and I did some research and found some filters, both free and commercial (and some a bit outdated). After trying one commercial, it proved crap. It couldn't even capture right way the parameters (was taking the URI instead of full URL, so querystring was ignored, to give one of multiple examples). We grew desperate and started searching for more filters like <a href="http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html">Mod_rewrite</a> in Apache.</p> <p>That was, until we came across <a href="http://www.codeplex.com/IIRF">Ionic's Isapi Rewrite Filter (IIRF)</a>, an opensource ISAPI filter written in Visual C++, which uses Perl-compatible regular expressions to define the rules and conditions for rewriting (or redirecting) URLs.</p> <p>The installation is simple, so I won't go deep on it (read the included readme.txt, it is just adding a new ISAPI filter to IIS pointing to the filter's DLL file).</p> <p>Interesting stuff comes after that, configuring the IsapiRewrite4.ini, which holds all the regular expressions (rules) for matching and replacing.</p> <p>They are used much like the mentioned before Mod_rewrite, allowing for quite interesting stuff. Here are a few simple examples:</p> <p><font face="Courier New"><font color="#000080">RewriteRule (.*)(&lt;|%3c)(script|%73%63%72%69%70%74)(&gt;|%3e)(.*) /incorrecturl.aspx [I,L]</font><br></font>This rule blocks all <font face="Courier New">&lt;script&gt;</font> tags (either normal or URL-encoded), ignoring case (<font face="Courier New">[I]</font> modifier)</p> <p><font face="Courier New" color="#000080">RewriteRule (.*)%00(.*) $1$2 [I]</font><br>This rule avoids nulls, used for example to avoid detection of tags, for example: <font face="Courier New">javas%00cript:</font><br>Note: Here the [L] modifier is omited to allow multiple replacements (if placed it would only remove <b>the first</b> null found)</p> <p><font face="Courier New" color="#000080">RewriteRule (.*)eval(\(|%28)(.*) /incorrecturl.aspx [I,L]<br></font>Same as the first one, this time with the "<font face="Courier New">eval(</font>" javascript function start.</p> <p> </p> <p>This are just but a few rules, for a (quite large) list of common and not so common XSS attacks, you can check any XSS cheat-sheet.</p>
