在PHP中使用输入生成的变量的安全漏洞

时间:2014-05-23 11:08:36

标签: php security

鉴于以下有问题的PHP代码的知识,其中变量是根据请求数据生成的(例如,提交的HTML表单字段),可能有哪些漏洞利用?

我知道请求可以构建为包含专门用来覆盖现有变量的数据(尽管我们对存在的变量视而不见),从而打破了应用程序,我只是努力想办法打破收益申请。

<?php
function foo( $data ) {

    // Some unknown code may exist here

    foreach( $data as $key => $value) {
        $$key = $value;
    }

    // Some other unknown code may exist here
}

if ( ! empty($_POST) ) {
    foo( $_POST );
}

3 个答案:

答案 0 :(得分:0)

<?php

$userid = $_SESSION['uid'];

function foo( $data ) {

    // Some unknown code may exist here
    //$_POST contains userid field;

    foreach( $data as $key => $value) {
        $$key = $value;
    }

    // Some other unknown code may exist here
}

if ( ! empty($_POST) ) {
    foo( $_POST );
}

$credit_card_no = $cardDetails->getById($userid);

echo 'your current card on file is: ' . $credit_card_no;

答案 1 :(得分:0)

变量是算法中的占位符,它们代表,它们在运行时填充。在您的算法中,您使用这些值执行某些操作,并且您依赖于这些值在某种程度上处于某种已知状态。通常,算法中的变量,占位符是固定的,并按一定顺序进行操作;这就是算法的定义。

we take A
and assign to it B
and then we assign A to C
and then we pass C to the function X
...

现在,变量变量基本上允许您根据其他值修改这些操作:

we take A
and assign to it B
AND THEN WE ASSIGN B TO ???  <---
and then we assign A to C
and then we pass C to the function X
...

那么,B分配给哪个变量?你的算法的其余部分从现在开始如何表现?你现在能相信你对任何特定变量状态的了解吗?

这些是基于用户输入的变量变量创建的问题。编写足够仔细的代码来避免这是一个问题并非不可能,但你必须非常小心。

答案 2 :(得分:0)

从不信任用户生成的数据。这是非常危险的代码,因为您可以轻松覆盖超级全局。

详细了解风险:

http://www.php.net/manual/en/security.globals.php

http://php.net/manual/en/function.extract.php

最好使用带有您期望的键的白名单:

// no whitelist

$_POST = array('foo' => 'bar', '_GET' => array('foo' => 'bar'), '_SESSION' => array('foo' => 'bar'));

foreach ($_POST as $key => $value) {
    $$key = $value;
}

echo $foo;
print_r($_GET);
print_r($_SESSION);

// whitelist

$whitelist = array('foo');

$_POST = array('foo' => 'bar', '_GET' => array('foo' => 'bar'), '_SESSION' => array('foo' => 'bar'));

foreach ($_POST as $key => $value) {
    if (in_array($key, $whitelist)) {
        $$key = $value;
    }
}

echo $foo;
print_r($_GET);
print_r($_SESSION);