duanqi5114 2018-07-14 20:19
浏览 76
已采纳

wamp服务器如何通过点点斜杠防止访问根文件夹上方

How can I prevent access above root? (by dot dot slash)

I can see all files in the partition.

To test access i wrote this script & added a Go UP link:

<?php
$PartialPath = @$_GET['p']; if(empty($PartialPath)){ $PartialPath = ''; }else{ $PartialPath = "\\".$PartialPath; }
$PartialPath_Root = dirname(__FILE__);

$ScanPath = $PartialPath_Root . $PartialPath;
echo 'Scan: ',$ScanPath,'<br><br>';

    $Files_arr = scandir($ScanPath);
    foreach ($Files_arr as $file) {
        if ('.' === $file){}
        else if ('..' === $file){  echo '<a href="?p=',$PartialPath,'../" target="_self">.. GO UP </a><br><br>'; }
        else{ echo $file,'<br>'; }
    }
?>

unsing @Hamidreza Kalantari answer

I created a filter to detect if path is outside of root:

if(Func_AllowOnlyRootPath($PartialPath) == "1"){
    // continue...
}else{
    echo '<br>unsecure path - outside root<br>'; 
    //die('Directory Traversal Prevented');
}

echo '<br>PartialPath: ',$PartialPath, '<br>';
function Func_AllowOnlyRootPath($VerifyPath){ if(empty($VerifyPath)){ return "1"; }  $real_path=realpath($VerifyPath); if(strpos($real_path, ($_SERVER['DOCUMENT_ROOT']))!==0){ return "0"; } return "1"; }
function Func_AllowOnlyPhpScriptPath($VerifyPath){ if(empty($VerifyPath)){ return "1"; }  $real_path=realpath($VerifyPath); if(strpos($real_path, (dirname(__FILE__)))!==0){ return "0"; } return "1"; }
  • 写回答

1条回答 默认 最新

  • dpfln86244 2018-07-14 20:37
    关注

    use realpath function to get the actual path(which does not contain any ..), then check if it begins with root or not:

    $real_path=realpath($PartialPath);
    if(strpos($real_path, $PartialPath_Root)!==0) die('Directory Traversal Prevented');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable