平面文件CMS (PAAS)的安全性


Security for Flat File CMS (PAAS)

我正在构建一个使用php文件的平面文件cms。用户将能够使用输入字段重命名文件,旧的和新的文件路径将通过ajax发送到我测试安全性的服务器。我意识到这可以用regex甚至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);    
   }   
?>

如果new_path是类似a/../../path/to/one/of/you/cms/core/file.php的东西,如果认为恶意用户可以覆盖您的CMS的一些文件。

你必须删除web服务器对你的CMS文件的写权限,以防止这种情况。