我试图构建一个可以用于任意代码执行的函数列表。其目的不是列出应该列入黑名单或以其他方式禁止的函数。更确切地说,我希望在搜索受感染的服务器以寻找后门时,有一个易于grep-able的红旗关键字列表。

其思想是,如果您想构建一个多用途的恶意PHP脚本——例如c99或r57这样的“web shell”脚本——您将不得不在文件中的某个位置使用一个或多个相对较小的函数集,以便允许用户执行任意代码。搜索这些函数可以帮助您更快地将数万个PHP文件缩小到需要仔细检查的相对较小的脚本集。

显然,例如,以下任何一种代码都被认为是恶意的(或糟糕的编码):

<? eval($_GET['cmd']); ?>

<? system($_GET['cmd']); ?>

<? preg_replace('/.*/e',$_POST['code']); ?>

等等。

前几天在搜索一个受感染的网站时,我没有注意到一段恶意代码,因为我没有意识到preg_replace可以通过使用/e标志而变得危险(真的吗?为什么会出现这种情况?)还有其他我错过的吗?

以下是我目前列出的清单:

Shell执行

系统 执行 popen 撇号操作符 pcntl_exec

PHP执行

eval Preg_replace(带/e修饰符) create_function 包括[_once] /要求[_once](详见mario的回答)

有一个能够修改文件的函数列表可能也很有用,但我想99%的情况下,利用代码将至少包含上述函数之一。但是如果你有一个所有能够编辑或输出文件的函数的列表,把它贴出来,我会把它包括在这里。(我没有计算mysql_execute,因为它是另一类利用的一部分。)


当前回答

还要注意允许任意内存位置被读写的“中断漏洞”类!

这些影响函数,如trim(), rtrim(), ltrim(),爆炸(),strchr(), strstr(), substr(), chunk_split(), strtok(), addcslashes(), str_repeat()等。这主要(但不完全)是由于该语言的调用时引用传递特性,该特性已经被弃用了10年,但并没有被禁用。

欲了解更多信息,请参阅Stefan Esser在BlackHat USA 2009 Slides Paper上关于中断漏洞和其他底层PHP问题的演讲

本文还展示了如何使用dl()来执行任意系统代码。

其他回答

php手册上的倒勾操作符

为了构建这个列表,我使用了两个来源。红字和老鼠的研究。我也加入了一些我自己的想法,在这条线上的人也提供了帮助。

编辑:在发布了这个列表之后,我联系了RIPS的创始人,到目前为止,这个工具会搜索PHP代码来使用这个列表中的每个函数。

这些函数调用大多被分类为sink。当一个受污染的变量(如$_REQUEST)被传递给接收器函数时,就有一个漏洞。RATS和RIPS等程序使用类似grep的功能来识别应用程序中的所有接收器。这意味着程序员在使用这些函数时应该格外小心,但如果它们都被禁止了,那么你就不能完成很多工作。

“权力越大,责任越大。”

——斯坦·李

命令执行

exec           - Returns last line of commands output
passthru       - Passes commands output directly to the browser
system         - Passes commands output directly to the browser and returns last line
shell_exec     - Returns commands output
`` (backticks) - Same as shell_exec()
popen          - Opens read or write pipe to process of a command
proc_open      - Similar to popen() but greater degree of control
pcntl_exec     - Executes a program

PHP代码执行

除了eval,还有其他方法来执行PHP代码:include/require可以以本地文件包含和远程文件包含漏洞的形式用于远程代码执行。

eval()
assert()  - identical to eval()
preg_replace('/.*/e',...) - /e does an eval() on the match
create_function()
include()
include_once()
require()
require_once()
$_GET['func_name']($_GET['argument']);
$func = new ReflectionFunction($_GET['func_name']); $func->invoke(); or $func->invokeArgs(array());

接受回调的函数列表

这些函数接受一个字符串参数,可以用来调用攻击者选择的函数。根据函数的不同,攻击者可能有也可能没有传递参数的能力。在这种情况下,可以使用像phpinfo()这样的信息披露函数。

Function                     => Position of callback arguments
'ob_start'                   =>  0,
'array_diff_uassoc'          => -1,
'array_diff_ukey'            => -1,
'array_filter'               =>  1,
'array_intersect_uassoc'     => -1,
'array_intersect_ukey'       => -1,
'array_map'                  =>  0,
'array_reduce'               =>  1,
'array_udiff_assoc'          => -1,
'array_udiff_uassoc'         => array(-1, -2),
'array_udiff'                => -1,
'array_uintersect_assoc'     => -1,
'array_uintersect_uassoc'    => array(-1, -2),
'array_uintersect'           => -1,
'array_walk_recursive'       =>  1,
'array_walk'                 =>  1,
'assert_options'             =>  1,
'uasort'                     =>  1,
'uksort'                     =>  1,
'usort'                      =>  1,
'preg_replace_callback'      =>  1,
'spl_autoload_register'      =>  0,
'iterator_apply'             =>  1,
'call_user_func'             =>  0,
'call_user_func_array'       =>  0,
'register_shutdown_function' =>  0,
'register_tick_function'     =>  0,
'set_error_handler'          =>  0,
'set_exception_handler'      =>  0,
'session_set_save_handler'   => array(0, 1, 2, 3, 4, 5),
'sqlite_create_aggregate'    => array(2, 3),
'sqlite_create_function'     =>  2,

信息披露

这些函数调用大多数都不是接收器。但是,如果攻击者可以看到返回的任何数据,那么它可能是一个漏洞。如果攻击者可以看到phpinfo(),这肯定是一个漏洞。

phpinfo
posix_mkfifo
posix_getlogin
posix_ttyname
getenv
get_current_user
proc_get_status
get_cfg_var
disk_free_space
disk_total_space
diskfreespace
getcwd
getlastmo
getmygid
getmyinode
getmypid
getmyuid

其他

extract - Opens the door for register_globals attacks (see study in scarlet).
parse_str -  works like extract if only one argument is given.  
putenv
ini_set
mail - has CRLF injection in the 3rd parameter, opens the door for spam. 
header - on old systems CRLF injection could be used for xss or other purposes, now it is still a problem if they do a header("location: ..."); and they do not die();. The script keeps executing after a call to header(), and will still print output normally. This is nasty if you are trying to protect an administrative area. 
proc_nice
proc_terminate
proc_close
pfsockopen
fsockopen
apache_child_terminate
posix_kill
posix_mkfifo
posix_setpgid
posix_setsid
posix_setuid

文件系统函数

According to RATS all filesystem functions in php are nasty. Some of these don't seem very useful to the attacker. Others are more useful than you might think. For instance if allow_url_fopen=On then a url can be used as a file path, so a call to copy($_GET['s'], $_GET['d']); can be used to upload a PHP script anywhere on the system. Also if a site is vulnerable to a request send via GET everyone of those file system functions can be abused to channel and attack to another host through your server.

// open filesystem handler
fopen
tmpfile
bzopen
gzopen
SplFileObject->__construct
// write to filesystem (partially in combination with reading)
chgrp
chmod
chown
copy
file_put_contents
lchgrp
lchown
link
mkdir
move_uploaded_file
rename
rmdir
symlink
tempnam
touch
unlink
imagepng   - 2nd parameter is a path.
imagewbmp  - 2nd parameter is a path. 
image2wbmp - 2nd parameter is a path. 
imagejpeg  - 2nd parameter is a path.
imagexbm   - 2nd parameter is a path.
imagegif   - 2nd parameter is a path.
imagegd    - 2nd parameter is a path.
imagegd2   - 2nd parameter is a path.
iptcembed
ftp_get
ftp_nb_get
// read from filesystem
file_exists
file_get_contents
file
fileatime
filectime
filegroup
fileinode
filemtime
fileowner
fileperms
filesize
filetype
glob
is_dir
is_executable
is_file
is_link
is_readable
is_uploaded_file
is_writable
is_writeable
linkinfo
lstat
parse_ini_file
pathinfo
readfile
readlink
realpath
stat
gzfile
readgzfile
getimagesize
imagecreatefromgif
imagecreatefromjpeg
imagecreatefrompng
imagecreatefromwbmp
imagecreatefromxbm
imagecreatefromxpm
ftp_put
ftp_nb_put
exif_read_data
read_exif_data
exif_thumbnail
exif_imagetype
hash_file
hash_hmac_file
hash_update_file
md5_file
sha1_file
highlight_file
show_source
php_strip_whitespace
get_meta_tags

下面是我的提供商出于安全考虑禁用的函数列表:

执行 戴斯。莱纳姆: 中将 apache_note apache_setenv closelog debugger_off debugger_on define_syslog_variables escapeshellarg escapeshellcmd ini_restore openlog passthru pclose pcntl_exec popen proc_close proc_get_status proc_nice proc_open proc_terminate shell_exec syslog 系统 url_exec

这些函数也会产生一些不好的影响。

函数() unserialize () register_tick_function () register_shutdown_function ()

前两种方法会耗尽所有可用内存,而后者会继续耗尽内存……

让我们将pcntl_signal和pcntl_alarm添加到列表中。

在这些函数的帮助下,您可以绕过php.ini或脚本中创建的任何set_time_limit限制。

例如,该脚本将运行10秒,尽管set_time_limit(1);

(感谢塞巴斯蒂安·伯格曼的推文和要点:

<?php
declare(ticks = 1);

set_time_limit(1);

function foo() {
    for (;;) {}
}

class Invoker_TimeoutException extends RuntimeException {}

class Invoker
{
    public function invoke($callable, $timeout)
    {
        pcntl_signal(SIGALRM, function() { throw new Invoker_TimeoutException; }, TRUE);
        pcntl_alarm($timeout);
        call_user_func($callable);
    }
}

try {
    $invoker = new Invoker;
    $invoker->invoke('foo', 1);
} catch (Exception $e) {
    sleep(10);
    echo "Still running despite of the timelimit";
}