超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<Using Register GlobalsMagic Quotes>
Last updated: Fri, 22 Jun 2007

章 29. User Submitted Data

The greatest weakness in many PHP programs is not inherent in the language itself, but merely an issue of code not being written with security in mind. For this reason, you should always take the time to consider the implications of a given piece of code, to ascertain the possible damage if an unexpected variable is submitted to it.

例子 29-1. Dangerous Variable Usage

<?php
// remove a file from the user's home directory... or maybe
// somebody else's?
unlink ($evil_var);

// Write logging of their access... or maybe an /etc/passwd entry?
fwrite ($fp, $evil_var);

// Execute something trivial.. or rm -rf *?
system ($evil_var);
exec ($evil_var);

?>
You should always carefully examine your code to make sure that any variables being submitted from a web browser are being properly checked, and ask yourself the following questions:

  • Will this script only affect the intended files?

  • Can unusual or undesirable data be acted upon?

  • Can this script be used in unintended ways?

  • Can this be used in conjunction with other scripts in a negative manner?

  • Will any transactions be adequately logged?

By adequately asking these questions while writing the script, rather than later, you prevent an unfortunate re-write when you need to increase your security. By starting out with this mindset, you won't guarantee the security of your system, but you can help improve it.

You may also want to consider turning off register_globals, magic_quotes, or other convenience settings which may confuse you as to the validity, source, or value of a given variable. Working with PHP in error_reporting(E_ALL) mode can also help warn you about variables being used before they are checked or initialized (so you can prevent unusual data from being operated upon).




add a note add a note User Contributed Notes
User Submitted Data
rcphp at littondale dot dyndns dot org
29-May-2003 03:52
As mentioned somewhere else, magic_quotes can be dealt with by:

  # Unmangle input if PHP is running in "Magic Quotes" mode.
  # I need the input for both DB and HTML generation, so use
   # the appopriate translations in the appropriate places.
  if(get_magic_quotes_gpc())
   {
       $content = stripslashes($content);
       $title = stripslashes($title);
  }

Then in HTML I use the htmlspecialchars() function to output, and in SQL I use addslashes(). Because I'm using the same variable in multiple places, I need to be aware of what it is and what to do with it.

Whether fast or not, I use Perl like regular functions for validity checking. The below checks for a number, though PHP has functions to handle this (I am only learning)

  if(!preg_match("/^[0-9]*$/", $alterExistingID))
  {
       # showWarning is one of my functions
        showWarning("Page called with malformed comment ID");
        $alterExistingID = "";
  }

See the is_numeric() function for a better solution to this.
hugo at GO_SPAMASSASSIN dot gewis dot nl
21-Dec-2002 06:14
To prevent the above noted abuse, you can always assure that register_globals is off (or if it isn't, exit).
For example:

if(ini_get('register_globals'))  {
    print '<b>Fatal:</b> This script requires that register_globals in php.ini is disabled.';
    exit;
}

// unlink files here

Hugo.
ps: in general: try to avoid removing files or exec'ing system commands in web-accessible scripts

<Using Register GlobalsMagic Quotes>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22