If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

This tutorial is aimed to introduce the reader to PHP filters from OWASP. OWASP (Open Web Application Security Project ) released a top ten list for web application security vulnerabilities in 2003 and 2004, you can find the 2004 list here:http://www.owasp.org/documentation/topten.html

Most of the top ten vulnerabilities including (A1) Unvalidated Input, (A2) Broken Access Control, (A4) Cross Site Scripting (XSS) Flaws, and (A6) Injection Flaws, can be avoided using these filters.

Installation
Download the file from the provided link, and extract the contents. We will be using the file sanitize.inc.php.txt. Rename this to sanitize.inc.php and we will use if from here on. Place it into a folder in the www root of your webserver with PHP installed.

Create a .php file with this code and save it into the same folder as sanitize.inc.php:

Visit that file you created in your browser. The output should be "This is a test string". If that is the output, then we are ready to begin using the filters.

The first filter is the PARANOID filter. Comment ("//") the "echo $Test;" line and uncomment the other two. View the page, and you will notice that the output string is now different. This is because we used our sanitize function. The syntax for the function is "sanitize($String, $Flags)". PARANOID was our flag. You can replace this with SQL, SYSTEM, HTML, INT, FLOAT, LDAP, or UTF8, all of which have different sanitization capabilities.

Filters

PARANOID

This will return a string containing only alphanumeric values. This is very strict and will remove anything that isn't a number or letter.

SQL

Returns a string with slashed out quotes. This is to be used for strings being entered in SQL queries, because single quotes can lead to a MySQL injection. (OWASP A1, A6)

SYSTEM

Returns a string without special characters and wrapped in quotes. This is for strings being used for system commands. If you wrote a PHP web frontend for a command line tool such as nmap and used a string from a form for command line arguments, an attacker could use it to specify arguments to compromise your system. (OWASP A1, A5, A6, A9)

HTML

Returns a string with HTML replacements for special characters. This allows HTML to be shown on screen instead of interpreted, and prevents XSS attacks. (OWASP A4)

INT and FLOAT

Returns only an integer/float without any extraneous characters. This prevents bad characters from being used where integers or floats are expected. (OWASP A1)

This will return "&ltscript&gt&#39 or &#39&#39&lt/script&gt", which will not be interpreted but will render as "<script>' or ''</script> ". It is now "safe" to query a database with that variable, and also display it to the screen.

These filters take a large chunk of the sanization work out for you, but there is still the issue of string length, with PHP the substr function will take care of that. These filters are hardly a end-all solution, but it provides a good drop-in solution that will be strengthened with other developers.