2013-07-09 44 views
0

好吧,做了搜索,找不到任何接近此的東西,所以這裏...這個PHP代理腳本在htdocs/public_html之外提供文件的安全性如何?

我正在寫一個PHP 5.3 +代理腳本來提供www,htdocs, public_html等,例如/home/sites/example.com/data

這是一個Moodle插件模塊,所以如果你熟悉Moodle代碼,很好,如果不是的話,我已盡我所能地註釋了它。到目前爲止,這一切都按預期工作,但我還沒有對其進行更多的測試。

問題:這有多安全?我主要關心的是用戶獲得指定目錄之外的訪問權限。如果有什麼可以看到這是一個明顯的安全漏洞,請讓我知道。

腳本:提前

require_once('../../config.php'); // conatains $CFG object 
require_once('../../lib/filelib.php'); // contains mimeinfo() and send_file() definitions 
// Don't use Moodle required_param() to avoid sending any HTML messages to Flash apps 

require_login(); // Users must be logged in to access files 

global $CFG; 

$swf_relative_path = get_file_argument(); // gets the appended URL e.g. /dir/subdir/file.jpg 
$swf_ok = false; 
if(strrpos($swf_relative_path,'.') > strlen($swf_relative_path) - 6) { 
    // Strip out special characters, extra slashes, and parent directory stuff 
    $swf_disallowed = array('../','\'','\"',':','{','}','*','&','=','!','?','\\','//','///'); 
    $swf_replace = array('','','','','','','','','','','','','/','/'); 
    $swf_relative_path = str_replace($swf_disallowed,$swf_replace,$swf_relative_path); 
    $swf_full_path = $CFG->dataroot.$CFG->swf_content_dir.$swf_relative_path; 
    if(file_exists($swf_full_path) && is_readable($swf_full_path)) { 
     $swf_path_info = pathinfo($swf_full_path); 
     $swf_mime_type = mimeinfo('type', $swf_path_info['basename']); 
     send_file($swf_full_path,$swf_path_info['basename'],'default',0,false,false,$swf_mime_type,false); 
     exit; 
    } 
} 
header('HTTP/1.0 404 Not Found'); // Send back a 404 so that apps don't wait for a timeout 
exit('404 Error: File not found'); // Pure text output - Flash app friendly 

謝謝! :)

回答

1

這是超級不安全,請不要在任何服務器上發佈此腳本。

不要修改.././或您在腳本中似乎不喜歡的其他模式。而且,只要替換它們也不會阻止攻擊者將替換的模式插入到腳本中。

例如,看看這個網址:

download.php?file=..././some/file 

用一個空字符串替換../(像你這樣)後,該文件的路徑爲../some/file和你的腳本已經破裂,因爲它會使您的下載根目錄之外的文件可以訪問。

避免這種情況的一個解決方案是使用realpath()。不過,我強烈建議爲此使用現有的安全腳本。

+0

Thanks str。最後,我使用了現有的Moodle功能。腳本在這裏:https://github.com/matbury/SWF-Activity-Module2.5/blob/master/swf/content.php – Matt