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.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World

Comments

Comments are now closed.

Most Popular Reviews

Follow Us

Best Deals on GoodGearGuide

Shopping.com

Latest News Articles

Resources

GGG Evaluation Team

Kathy Cassidy

STYLISTIC Q702

First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni

STYLISTIC Q572

For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Steph Mundell

LIFEBOOK UH574

The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi

STYLISTIC Q702

The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott

STYLISTIC Q702

My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Latest Jobs

Shopping.com

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?