Security of PHP scripts

Security of PHP scripts: Part II

On page 1 we looked at secure programming techniques in PHP, focusing on user supplied data. Here, we will look at some less obvious security issues.

Cross site scripting

Over the last year or so the security community has focused on cross site scripting (XSS) - mostly due to the significant shift in the software development industry toward Web-based applications. XSS refers to a situation in which one user can take over another user's session by manipulating a Web application in a particular way.

A popular target has been Web-based e-mail systems. When a user logs in to read their e-mail, all URLs include a unique and hard-to-guess string. For example, a link to the user A's inbox might be http://my.mail.hoster/inbox.php?usrid=123&ses=sjdhfoas983jds8. The variable 'ses' is the session string and it is very hard to guess.

However, say that user B sends user A an e-mail containing HTML - something that happens frequently. The HTML might link to an image at http://malicious.host/img.jpg. Web browsers are designed to identify to the Web servers to which they connect the page from which they were referred. When user A reads the e-mail from user B, the referrer URL will indicate to user B the unique session string identifying user A's session. Using this, user B can take over user A's e-mail account.

User B could also have included JavaScript code to take control of user A's session. Such code could rewrite the inbox page to resemble the login page of the e-mail hoster, with one difference: the form would be submitted to a script hosted on user B's Web server. Thinking that they have been logged out, the user may well resubmit their user name and password - exposing them to user B as a result.

Defensive measures

There are several ways to counter these possible security holes in PHP. All untrusted user-supplied data should be checked for HTML tags. Tags can be escaped with the htmlspecialchars() and htmlentities() functions. They can be removed by using strip_tags.

If your application required users to submit some HTML, but you wanted to disallow tags such as <A>, <IMG> and <SCRIPT>, you would need to write your own script. An example - xss_strip.php. It is a complicated little script, so we will go over it.

The script starts by defining an array of disallowed tags: $badcode. The code then loops to parse the input string, $input. Inside the loop, we attempt to locate a less-than symbol < - the beginning of a tag. If the function strpos() cannot find this character, there is no HTML left in $input so we break from the loop. If it does find the beginning of a tag, we copy all the text up to the < to $output, using substr().

The script then iterates through the HTML tags in $badcode, testing if a match is found. An extract follows:

for($i=0;$i<count($badcode);$i++) {
$tmp = substr($input,$s + 1,strlen($badcode[$i]));
if(strcasecmp($tmp,$badcode[$i]) == 0) {
/* matched */
$found = 1;
/* escaped start */
$output .= "&lt;";
$e = strpos($input,">",$s);
if($e === false) {
/* no closing tag, copy the rest */
$output .= substr($input,$s + 1);
$end = 1;
break;
} else {
$output .= substr($input,$s + 1,$e - $s - 1)."&gt;";
$input = substr($input,$e + 1);
}
}
} 

The second line of the extract copies the initial segment of the input string into a temporary variable $tmp. The length of this segment corresponds to the current item from $badcode that we are testing. The function strcasecmp() then tests the current $badcode item against the segment from the input string. If they match, we have found a tag that is disallowed.The script replaces the less-than symbol with an HTML entity representing it: &lt;. It then attempts to locate the closing greater-than tag >. If it cannot be found ($e === false), then the remainder of the string is concatenated to $output and we break from the loop. If we do find a closing > we copy all the characters after the < and before the > and append the HTML entity for the great-than symbol: &gt;.

Developers looking to improve their PHP skills should modify xss_strip.php to escape closing tags, such as '</a>'.

If you are interested in reading more about cross site scripting, visit the page at http://www.cert.org/advisories/CA-2000-02.html.

Join the newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

George Khoury

Sharp PN-40TC1 Huddle Board

The biggest perks for me would be that it comes with easy to use and comprehensive programs that make the collaboration process a whole lot more intuitive and organic

David Coyle

Brother PocketJet PJ-773 A4 Portable Thermal Printer

I rate the printer as a 5 out of 5 stars as it has been able to fit seamlessly into my busy and mobile lifestyle.

Kurt Hegetschweiler

Brother PocketJet PJ-773 A4 Portable Thermal Printer

It’s perfect for mobile workers. Just take it out — it’s small enough to sit anywhere — turn it on, load a sheet of paper, and start printing.

Matthew Stivala

HP OfficeJet 250 Mobile Printer

The HP OfficeJet 250 Mobile Printer is a great device that fits perfectly into my fast paced and mobile lifestyle. My first impression of the printer itself was how incredibly compact and sleek the device was.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?