这个PHP代理脚本在htdocs / public_html之外提供文件的安全性如何?

时间:2013-07-09 21:50:05

标签: php security proxy moodle

好的,做了搜索,找不到任何接近这个,所以这里......

我正在编写一个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 个答案:

答案 0 :(得分:1)

这是超级不安全,请不要在任何服务器上发布此脚本。

不要修改.././或您在脚本中似乎不喜欢的其他模式。此外,只是替换它们不会阻止攻击者将替换的模式插入到您的脚本中。

例如,看一下这个网址:

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

用空字符串替换../后(与您一样),文件的路径为../some/file,您的脚本已经损坏,因为它会使您的下载根目录之外的文件可访问。< / p>

避免这种情况的一个解决方案是使用realpath()。但是,我强烈建议为此目的使用现有的安全脚本。