【问题标题】:PHP security: 'Nonce' or 'unique form key' problemPHP 安全性:“Nonce”或“唯一表单密钥”问题
【发布时间】:2010-07-06 01:48:49
【问题描述】:

我使用这个类(取自博客教程)来生成唯一键来验证表单:

class formKey {
    //Here we store the generated form key
    private $formKey;

    //Here we store the old form key
    private $old_formKey;

    //The constructor stores the form key (if one excists) in our class variable
    function __construct() {
        //We need the previous key so we store it
        if(isset($_SESSION['form_key'])) {
            $this->old_formKey = $_SESSION['form_key'];
        }
    }

    //Function to generate the form key
    private function generateKey() {
        //Get the IP-address of the user
        $ip = $_SERVER['REMOTE_ADDR'];

        //We use mt_rand() instead of rand() because it is better for generating random numbers.
        //We use 'true' to get a longer string.
        $uniqid = uniqid(mt_rand(), true);

        //Return the hash
        return md5($ip . $uniqid);
    }

    //Function to output the form key
    public function outputKey() {
        //Generate the key and store it inside the class
        $this->formKey = $this->generateKey();
        //Store the form key in the session
        $_SESSION['form_key'] = $this->formKey;

        //Output the form key
        // echo "<input type='hidden' name='form_key' id='form_key' value='".$this->formKey."' />";
        return $this->formKey;
    }

    //Function that validated the form key POST data
    public function validate() {
        //We use the old formKey and not the new generated version
        if($_POST['form_key'] == $this->old_formKey) {
            //The key is valid, return true.
            return true;
        }
        else {
            //The key is invalid, return false.
            return false;
        }
    }
}

我网站上的所有东西都先通过 index.php,所以我把这个放在 index.php 中:$formKey = new formKey();

然后,在每一种形式中,我都会这样写:&lt;?php $formKey-&gt;outputKey(); ?&gt;

生成这个:&lt;input type="hidden" name="form_key" id="form_key" value="7bd8496ea1518e1850c24cf2de8ded23" /&gt;

然后我可以简单地检查if(!isset($_POST['form_key']) || !$formKey-&gt;validate())

我有两个问题。第一:我每页不能使用多个表单,因为只有最后一个生成的密钥才会生效。

第二:因为一切都先通过 index.php,如果我使用 ajax 验证表单,第一次会验证但第二次不会,因为 index.php 生成一个新键,但包含表单的页面会' t 刷新所以表单键不更新..

我已经尝试了几件事,但我无法让它工作。也许你可以更新/修改代码/类来让它工作??谢谢!!!

【问题讨论】:

  • 为什么需要这个验证?您对垃圾邮件提交有疑问吗?
  • 针对 XSS(跨站点脚本)和表单上的跨站点请求伪造提供一些安全性。
  • 我在这段代码中看不到任何针对 xss 的保护措施。 xsrf 和 xss 是非常不同的攻击。

标签: php security forms


【解决方案1】:

你可以把它放到一个类中,但这是不必要的复杂性。简单的安全系统是最好的,因为它们更容易审计。

//Put this in a header file
session_start();
if(!$_SESSION['xsrf_token']){
     //Not the best but this should be enough entropy
     $_SESSION['xsrf_token']=uniqid(mt_rand(),true);
}    
//$_REQUEST is used because you might need it for a GET or POST request. 
function validate_xsrf(){
   return $_SESSION['xsrf_token']==$_REQUEST['xsrf_token'] && $_SESSION['xsrf_token'];
}
//End of header file. 

额外的&amp;&amp; $_SESSION['xsrf_token'] 确保填充此变量。它在那里确保实施安全失败。 (就像你忘记了头文件一样!;)

下面的 html/php 进入任何你想保护 XSRF 的文件,确保你在头文件中有上面的代码。

if(validate_xsrf()){
   //do somthing with $_POST
}

这就是您打印表格所需的全部内容,再次确保您在执行任何操作之前调用session_start();,多次调用也没关系。

<input type="hidden" name="xsrf_token" id="form_key" value="<?=$_SESSION['xsrf_token']?>" />

【讨论】:

  • 谢谢!为什么说它不是最好的解决方案,但它应该足够熵?
  • 有一些类型错误:value="=$_SESSOIN['xsrf_token'?>" 而不是:value="=$_SESSION['xsrf_token]'?>" 和 $ _SESSOIN 而不是 $_SESSION..
  • 但是,当我再次使用 ajax 发布的表单时,令牌会发生变化,并且在第二次尝试中它不会验证......为什么如果 $_SESSION['xsrf_token'] 已经创建一个新令牌设置?
  • @Jonathan 我没有运行这段代码,我只是从内存中输入的。尽管该会话的所有请求都会维护 $_SESSION。它只需要设置一次。未设置的变量也等于 false。 uniqid() 计算将是一个非常大的数字,它严重依赖于时间,并且存在弱加密随机数,但它足够大以至于在 csrf 攻击中强制它是不现实的。
  • @Jonathan 还需要注意的是,XSS 可用于绕过基于令牌或基于引用的 XSRF 保护。确保你修复你的 xss!
【解决方案2】:

未经测试,但应该可以工作。

class formKey {
    //Here we store the generated form key
    private $formKey;

    //Here we store the old form key
    private $old_formKey;

    //The constructor stores the form key (if one excists) in our class variable
    function __construct() {
        //We need the previous key so we store it
        if(isset($_SESSION['form_key'])) {
            $this->old_formKey = $_SESSION['form_key'];
            $this->formKey = $this->generateKey();
            $_SESSION['form_key'] = $this->formKey;
        }
    }

    //Function to generate the form key
    private function generateKey() {
        //Get the IP-address of the user
        $ip = $_SERVER['REMOTE_ADDR'];

        //We use mt_rand() instead of rand() because it is better for generating random numbers.
        //We use 'true' to get a longer string.
        $uniqid = uniqid(mt_rand(), true);

        //Return the hash
        return md5($ip . $uniqid);
    }

    //Function to output the form key
    public function outputKey() {
        return $this->formKey;
    }

    //Function that validated the form key POST data
    public function validate() {
        //We use the old formKey and not the new generated version
        if($_POST['form_key'] == $this->old_formKey) {
            //The key is valid, return true.
            return true;
        }
        else {
            //The key is invalid, return false.
            return false;
        }
    }
}

编辑:改回单键。只需在需要时调用 outputkey() 即可。不要创建多个此类的实例。

【讨论】:

  • 一切都通过 index.php 并且在那里我创建了一个新的类实例。我怎样才能只创建一次呢?
  • @Jonah Bron 不需要多个密钥,只要攻击者不知道这些值,就无法伪造请求。
  • @The Rook:我看了你的一些帖子,我发现你对安全性很了解,你能用更新的代码发布答案并告诉我如何正确使用它吗?
  • @The Rook,哦,是的,只需要一把钥匙。更改了代码。 @Jonathan,只要做$my_key = new formKey();,当你想输出一个带有哈希值的隐藏表单时,将$my_key-&gt;outputKey()插入到value属性中。
  • 它仍然无法正常工作,因为我想我每次都创建一个新的类实例,因为该实例是在 index.php 中创建的,所有页面请求、表单帖子和一切都通过 index.php首先...如何只创建一次实例???
猜你喜欢
  • 2020-07-28
  • 2019-08-15
  • 1970-01-01
  • 2021-02-10
  • 2017-07-09
  • 2017-11-12
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多