递归删除目录,要求不能删除某目录以外的其它目录
请大家帮我看看这个函数是否有安全漏洞:递归删除目录,要求不能删除$CFG['vhost_root'] 以外的其它目录
[php]<?php
// 删除非空目录,即使里面还有下级目录
function rmDirForce($dir) {
global $CFG;
if ( !file_exists($dir) ) {
return false;
}
if ( str_replace('\\','/',dirname($dir)) == $CFG['root'] ) {
// 不能删除 $CFG['root'] 下的一级目录
return false;
}
$dir = str_replace('\\', '/', $dir);
$dir = str_replace('../', '', $dir);
$dir = str_replace('./', '', $dir);
if ( !strstr($CFG['vhost_root'], $dir)) || strlen($dir) <= strlen($CFG['vhost_root'] ) {
// 不能删除 $CFG['vhost_root'] 以外的目录
return false;
}
if ( $handle = opendir($dir) ) {
while (false !== ($item = readdir($handle))) {
if ($item != "." && $item != "..") {
if (is_dir($dir . '/' . $item)) {
rmDirForce($dir . '/' . $item);
} else {
unlink($dir . '/' . $item);
//echo " removing $dir/$item<br>\n";
}
}
}
closedir($handle);
rmdir($dir);
return true;
}
}
?>[/php]
其中: $CFG['vhost_root'] 是虚拟主机的绝对路径,例如 /www/www.hbcms.com/htdocs 或 e:/vhost/www.hbcms.com/web
$dir 是要删除的目录。
[[i] 本帖最后由 hbcms 于 2007-3-28 23:51 编辑 [/i]] if ( !strstr($CFG['vhost_root'], $dir)) || strlen($dir) <= strlen($CFG['vhost_root']) ) {
// 不能删除 $CFG['vhost_root'] 以外的目录
return false;
}
这里,如果是相对目录呢? hbcms也是phper中的名流呀~ if ( str_replace('\\','/',dirname($dir)) == $CFG['root'] ) {
// 不能删除 $CFG['root'] 下的一级目录
return false;
}
这个怎么限制不能删除下级目录的啊
$dir = str_replace('\\', '/', $dir);
$dir = str_replace('../', '', $dir);
$dir = str_replace('./', '', $dir);
如果是.....///呢?
有点糊涂 呵呵.....
直接发现里面有./ ../这样的就die掉不是很安全么? [quote]原帖由 [i]剑心[/i] 于 2007-3-26 11:22 发表
if ( str_replace('\\','/',dirname($dir)) == $CFG ) {
// 不能删除 $CFG 下的一级目录
return false;
}
这个怎么限制不能删除下级目录的啊
$dir = str_replace('\\', '/', $dir);
$dir = str_r ... [/quote]
好像.....///是一个大问题!!! 感谢。。。我测试去。 realname 这个呢?? 能exec的话直接用系统命令也可以。
页:
[1]
