小编典典

可利用的PHP函数

php

我正在尝试建立可用于任意代码执行的功能列表。目的不是列出应列入黑名单或以其他方式不允许的功能。而是,当在受感染的服务器中搜索后门时,我希望有一个grep-able
红旗 关键字列表。

这个想法是,如果您要构建多功能的恶意PHP脚本(例如c99或r57之类的“ Web
Shell”脚本),则必须使用一个或多个相对较小的功能集文件中的某个位置,以便允许用户执行任意代码。搜索这些功能可以帮助您更快地将成千上万的PHP文件的范围缩小到需要仔细检查的相对较小的脚本集。

显然,例如,以下任何一项将被视为恶意(或可怕的编码):

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

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

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

等等。

前几天,我在一个受感染的网站上进行搜索时,没有注意到一段恶意代码,因为我没有意识到preg_replace使用该/e标志可能会造成危险(
这很严重?为什么甚至在那里也是如此 ?)。还有其他我想念的吗?

到目前为止,这是我的清单:

Shell执行

  • system
  • exec
  • popen
  • backtick operator
  • pcntl_exec

PHP执行

  • eval
  • preg_replace(带/e修饰符)
  • create_function
  • include[ _once] / require[ _once](有关漏洞利用程序的详细信息, 请参阅mario的答案

列出能够修改文件的功能可能也很有用,但是我想99%的利用代码中至少会包含上述功能之一。但是,如果您具有能够编辑或输出文件的所有功能的列表,请将其发布,并将在此包括在内。(我不算数mysql_execute,因为那是另一类漏洞利用的一部分。)


阅读 395

收藏
2020-05-26

共1个答案

小编典典

为了建立此列表,我使用了2个来源。猩红与RATS研究。我还添加了一些我自己的东西,这个线程上的人也提供了帮助。

编辑: 发布此列表后,我联系了RIPS的创建者,到目前为止,此工具会搜索PHP代码以使用此列表中的每个函数。

这些函数调用大多数都归为接收器。将受污染的变量(如$_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());

接受回调的函数列表

这些函数接受一个字符串参数,该参数可用于调用攻击者选择的函数。根据功能的不同,攻击者可能会或可能不会传递参数。在那种情况下,可以使用InformationDisclosure类似的功能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

文件系统功能

根据RATS,php中的所有文件系统功能都很讨厌。其中一些对于攻击者似乎不太有用。其他的则比您想象的有用。例如,如果allow_url_fopen=On将URL用作文件路径,copy($_GET['s'],$_GET['d']);则可以使用对的调用将PHP脚本上载到系统中的任何位置。同样,如果站点容易受到通过GET发送的请求的影响,那么这些文件系统功能中的每个功能都可能被滥用来通过服务器引导和攻击另一台主机。

// 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
2020-05-26