我读到了create_function,它将在PHP7.2上被废弃,这很容易被PHP命令注入。我开始玩它,并创造了一个例子。
下面的代码应该返回数组中每个字符串的小写版本。
我的问题是,为什么在这种情况下使用'array_walk_recursive‘函数来修复代码注入呢?
的使用
Array
(
[0] => hello
)Array (
[0] => phpinfo();
)中
(请注意phpinfo运行。警告来自于php沙箱)
*Warning: phpinfo() has been disabled for security reasons in [...][...](32) : runtime-created function on line 1
Array
(
[0] => anything
)*发布于 2018-08-22 10:38:35
为什么在这种情况下使用'array_walk_recursive‘函数来修复代码注入呢?
解决这个问题的不是array_walk_recursive。问题是根本就没有问题。
您不能为代码注入使用create_function函数参数,因为这些参数被视为变量字符串,并且不能用于脱离当前上下文。
注入的参数需要来自外部。例如:
'b', 'c' => 'D'];
$func = $_GET['x']; /// eg x=strtolower
array_walk_recursive(
$arr,
create_function('&$value, $key', '$value = ' . $func . '($value);')
);
print_r($arr);
?>现在您可以获得代码执行:
x=strtolower($value);phpinfo();Detailed解释
'b', 'c' => 'D'];
$func = $_GET['x']; /// eg x=strtolower
array_walk_recursive( // apply the function created below to all array elements
$arr,
create_function( // creating a new function
'&$value, $key', // arguments for the function
'$value = ' . $func . '($value);' // the function itself.
// function uses $func from outside this string context (this is the essential part).
// $value is the value from the previous parameter. It is just a string here.
)
);
print_r($arr);
?>当用x=strtolower($value);phpinfo();调用它时,create_function从给定的字符串创建的函数基本上是:
function (&$value, $key) {
$value = strtolower($value);phpinfo();
}然后对每个数组条目调用这个函数。我们可以看到这将如何导致代码执行。
如果我们将其与OP中的代码进行比较,所创建的函数将是:
function (&$value, $key) {
$value = strtolower($value);
}此函数中没有代码执行,因为在创建函数的字符串中没有使用来自外部的值。
https://security.stackexchange.com/questions/192056
复制相似问题