代码审计之命令执行
命令执行可以细分成系统命令执行和代码注入。漏洞成因都是因为用户输入进入了关键函数导致的。
#代码注入
关键函数
eval(), assert(), preg_replace(), call_user_func(), call_user_func_array(), array_map() 以及动态函数$a($b),
assert
bool assert ( mixed $assertion [, string $description ] )
assert() 会检查指定的 assertion 并在结果为 FALSE 时采取适当的行动。
如果 assertion 是字符串,它将会被 assert() 当做 PHP 代码来执行。
测试代码:
<?php
$a=$_GET['a'];
#fdsffd
assert($a);
$b='feafdea';
// var_dump(assert($a));
// var_dump(assert('2 < 1'));
?>
当test.php?a=phpinfo()时,phpinfo()会被执行。
eval
mixed eval ( string $code )
把字符串 code 作为PHP代码执行。
eval就更加直接了。
call_user_func
call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数
mixed call_user_func ( callable $callback [, mixed $parameter [, mixed $… ]] )
第一个参数 callback 是被调用的回调函数,其余参数是回调函数的参数。
测试代码:
call_user_func($_REQUEST['func'], $_REQUEST['pass']);
preg_replace
preg_replace — 执行一个正则表达式的搜索和替换
说明
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
搜索subject中匹配pattern的部分, 以replacement进行替换。
测试代码:
<?php
preg_replace("/\[(.*)\]/e",'\\1',$_GET['str']);
?>
典型案例:
系统命令执行
关键函数
system(),exec(),
system
样例代码:
<?php
// include("common.php");
// showMenu();
// echo '<br>';
$status = $_GET['status'];
$ns = $_GET['ns'];
$host = $_GET['host'];
$query_type = $_GET['query_type']; // ANY, MX, A , etc.
$ip = $_SERVER['REMOTE_ADDR'];
$self = $_SERVER['PHP_SELF'];
$host = trim($host);
$host = strtolower($host);
echo("<span class=\"plainBlue\"><b>Executing : <u>dig @$ns $host $query_type</u></b><br>");
echo '<pre>';
//start digging in the namserver
system ("dig @$ns $host $query_type");
echo '</pre>';
?>
利用方式:
使用||
分隔,执行多条命令