用户提交的数据

很多 PHP 程序所存在的重大弱点并不是 PHP 语言本身的问题,而是编程者的安全意识不高而导致的。因此,必须时时注意每一段代码可能存在的问题,去发现非正确数据提交时可能造成的影响。

Example #1 危险的变量用法

<?php
// 从用户目录中删除一个文件,或者......能删除更多的东西?
unlink ($evil_var);

// 记录用户的登陆,或者......能否在 /etc/passwd 添加数据?
fwrite ($fp$evil_var);

// 执行一些普通的命令,或者......可以执行 rm -rf * ?
system ($evil_var);
exec ($evil_var);

?>

必须时常留意你的代码,以确保每一个从客户端提交的变量都经过适当的检查,然后问自己以下一些问题:

  • 此脚本是否只能影响所预期的文件?
  • 非正常的数据被提交后能否产生作用?
  • 此脚本能用于计划外的用途吗?
  • 此脚本能否和其它脚本结合起来做坏事?
  • 是否所有的事务都被充分记录了?
在写代码的时候问自己这些问题,否则以后可能要为了增加安全性而重写代码了。注意了这些问题的话,也许还不完全能保证系统的安全,但是至少可以提高安全性。

还可以考虑关闭 register_globals,magic_quotes 或者其它使编程更方便但会使某个变量的合法性,来源和其值被搞乱的设置。在开发时,可以使用 error_reporting(E_ALL) 模式帮助检查变量使用前是否有被检查或被初始化,这样就可以防止某些非正常的数据的挠乱了。

User Contributed Notes

Livingstone@stonyhills[dot]com 02-Feb-2008 01:51
making sure your form is submitted from your page! Could also be adapted to url, by additing &token to the query string and checking this against session data(or what ever array you like) with $_GET, not that this string is randomly generated and stored. If you like you could build your own array to store the generated string if you dont want to use $_SESSION, say you could make yours like $tokens = array(), and in your easysecure class you store all the stuff in that array!

<?php

class easysecure {
   
    var
$curr_user;
    var
$curr_permission;
    var
$curr_task;
    var
$validpermission;
    var
$error;
   
   
    function &
setVar( $name, $value=null ) {
        if (!
is_null( $value )) {
           
$this->$name = $value;
        }
        return
$this->$name;
    }

    function
maketoken($formname, $id){
       
       
$token = md5(uniqid(rand(), true));
       
       
$_SESSION[$formname.$id] = $token;
       
        return
$token;
    }
   
    function
checktoken($token, $formname, $id){
       
//print_r($_SESSION);
        //echo ($token);
        //if we dont have a valid token, return invalid;
       
if(!$token){
           
$this->setVar('validpermission', 0);
           
$this->setVar('error', 'no token found, security bridgedetected');
            return
false;
        }
       
       
//if we have a valid token check that is is valid
       
$key = $_SESSION[$formname.$id];
        if(
$key !== $token ){
           
$this->setVar('validpermission', 0);
           
$this->setVar('error', 'invalid token');
            return
false;
        }
       
        if(
$this->validpermission !==1){
              echo
'invalid Permissions to run this script';
              return
false;   
        }else{
            return
true;
        }
    }
   
}

?>

<?php $userid = *** //make it what ever id you like ?>
<form name="newform" action="index.php" method="post">
<input type="text" name="potentialeveilfield" value="" size 30 />
<input type="hidden" name="token" value="<?php echo maketoken(newform, $userid); //$userid here could be user profile id ?>" />
<input type="submit" />
</form>

Now when processing the form... check the value of your token

<?php

//well you know the form name
if(!checktoken($_POST['token'], 'newform', $userid))
{
//failed
exit(); //or what ever termination and notification method best suits you.
//you could also design the class your way to get more accurate fail (error messages from the var)
}

//you can now continue with input data clean up (validation)

?>
Uli Kusterer 13-Sep-2005 12:50
One thing I would repeat in the docs here is what information actually comes from the user. Many people think a Cookie, since it's written by PHP, was safe. But the fact is that it's stored on the user's computer, transferred by the user's browser, and thus very easy to manipulate.

So, it'd be handy to mention here again that:

CGI parameters in the URL, HTTP POST data and cookie variables are considered "user data" and thus need to be validated. Session data and SQL database contents only need to be validated if they came from untrustworthy sources (like the ones just mentioned).

Not new, but I would have expected this info under this headline, at least as a short recap plus linlk to the actual docs.