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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序