我正在建立一个平面文件cms使用php文件。用户将能够使用输入字段重命名文件,新旧文件路径将通过ajax发送到服务器,我在那里测试安全性。我意识到使用正则表达式甚至OR操作符可以更容易地做到这一点。我去掉了OR操作符,这样对于这篇文章来说字符串就不会太长。至于正则表达式,我希望对发送回客户端的错误有更多的控制。
CMS本身很像一个PAAS,它驻留在每个用户将拥有的所有单独站点文件夹之上的目录中。我的目标是防止用户在上面的父目录中注入可能干扰其他(相邻的)用户文件夹或cms本身的代码。
我还没有解析路径的有效性。我只是想知道恶意用户如何能够利用我到目前为止所写的内容。
<?php
$old_path = $_POST['file'].'.php'; // path/to/file.php
$new_path = $_POST['new_file'].'.php'; // path/to/newfile.php';
if(strstr($new_path,"<?")){
echo "Sorry, path cannot contain script tags";
}elseif(strstr($new_path,"?>")){
echo "Sorry, path cannot contain script tags";
}elseif (strstr($new_path,">")){
echo "Sorry, path cannot contain script tags";
}elseif (strstr($new_path,"<")){
echo "Sorry, path cannot contain script tags";
}elseif($new_path[0]==="." OR $new_path[0]==='/' OR $new_path[0]==='\\'){
echo 'Sorry first character of path cannot be a period or slash';
}else{
//this is set when the user logs in based on details in a database
$users_dedicated_directory = $_SESSION['userfolder'].'/';
// add the users folder when renaming just for more control
$old_path = $users_dedicated_directory.$old_path;
// add the users folder when renaming just for more control
$new_path = $users_dedicated_directory.$old_path;
rename($old_path,$new_path);
//trim the users folder name. Send it back to the client
echo explode($users_dedicated_directory,$new_path);
}
?>发布于 2015-06-09 23:50:44
如果new_path是类似a/../../path/to/one/of/you/cms/core/file.php的东西,如果认为恶意用户可以覆盖您的内容管理系统的一些文件。
你必须移除web服务器对你的CMS文件的写权限,以防止这种情况发生。
https://stackoverflow.com/questions/30736247
复制相似问题