一、漏洞成因
当应用在调用一些能将字符转化为代码的函数(如PHP中的eval,assert)时,没有考虑用户是否能控制这个字符串,这就会造成代码执行漏洞。
二、PHP漏洞环境搭建
2.1 phpstudy环境安装
phpStudy Linux 面板简称小皮面板,小皮面板是phpStudy官方开发团队在2019.10.8号发布的一款Linux服务器管理软件,一键LAMP/LNMP、创建网站及FTP、数据库、文件管理、PHP多版本共存及切换、SSL、计划任务以及服务器网站安全管理。
- Centos安装脚本
`yum install -y wget && wget -O install.sh https://download.xp.cn/install.sh && sh install.sh`
- Ubuntu安装脚本
wget -O install.sh https://download.xp.cn/install.sh && sudo bash install.sh
- Deepin安装脚本
wget -O install.sh https://download.xp.cn/install.sh && sudo bash install.sh
- Debian安装脚本
wget -O install.sh https://download.xp.cn/install.sh && sudo bash install.sh
按照命令行提示
访问首页
2.2 修改php页面
点击“网站”,查看网站目录
cd /www/admin/localhost_80/wwwroot/error
mv index.html index.backup.html
cd ..
vim index.php
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<?php
echo "你好,世界!";
?>
三、不同类型漏洞复现
php中可以执行代码的函数有: eval()、assert()、``、system()、exec()、shell_exec()、passthru()、 pcntl_exec()
这些函数中的参数(部分)可控时,则可能命令注入漏洞。
防御方法:使用escapeshellarg对参数进行处理
3.1 eval()函数
eval函数把字符串作为PHP代码执行
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<?php
echo "你好,世界!";
eval("$_POST[1]")
?>
使用火狐浏览器的插件HackBar提交Post data,获得phpinfo
3.2 assert()函数
assert函数检查一个断言是否为FALSE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<?php
echo "你好,世界!";
assert("$_POST[1]")
?>
漏洞复现过程与eval()函数一样
3.3 call_user_func()函数
call_user_func函数把第一个参数作为回调函数调用,其余参数是回调函数的参数
函数语法:mixed call_user_func_array ( callable $callback , array $param_arr )
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<?php
echo "你好,世界!";
call_user_func($_POST['fun'],$_POST['arg'])
?>
同理,还有 call_user_fun_array函数
3.4 create_function()函数
create_function函数根据传递的参数创建匿名函数,并为该匿名函数返回唯一的名称
函数语法:string create_function(string $arges , string $code)
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<?php
echo "你好,世界!";
$a= $_POST['func'];
$b = create_function('$a',"echo $a");
$b('');
?>