2012-05-27 2 views
0

지금까지 파일 브라우저를 만들었지 만 꽤 불안합니다. "? dir = .. /"또는 "? dir = ../../.. /"을 삽입하면 예를 들어서는 안되는 파일을 볼 수 있습니다. 내 코드 :PHP에서 파일 브라우저를 제한/보안 설정

<?php 
    if(isset($_GET['dir']) && $_GET['dir'] !== "../") {$dir = $basedir.$_GET['dir']."/";} else {$dir = $basedir;} 
    if ($handle = opendir($dir)) { 
     if(isset($_GET['dir']) && $_GET['dir'] !== "") {echo "<a href='?dir='>back to root</a>";} 
     while (false !== ($file = readdir($handle))) { 
      if ($file != "." && $file != "..") { 
       if(filetype($dir . $file) == "file") {$filetype = what_suffix($file);} else {$filetype = "-";} 
       if(filetype($dir . $file) == "file") {$filesize = round(filesize($dir . $file)/1024/1024 ,2)." MB";} else {$filesize = "-";} 
       if(filetype($dir . $file) == "file") {$filedate = date("d.m.Y H:i:s", filectime($dir.$file));} else {$filedate = "-";} 
       if(filetype($dir . $file) == "file") { 
       echo(
        "<tr> 
         <td><a href='?dir="[email protected]$_GET['dir']."&file=".$file."'>".$file."</td> 
         <td>".$filetype."</td> 
         <td>".$filesize."</td> 
         <td>".$filedate."</td> 
        </tr>" 
       ); } else { 
       echo(
        "<tr> 
         <td><a href='?dir="[email protected]$_GET['dir']."/".$file."'>".$file."</td> 
         <td>-</td> 
         <td>-</td> 
         <td>-</td> 
        </tr>" 
       ); } 


      } 
     } 
     closedir($handle); 
    } 
?> 

제한 할 수있는 방법이 있습니까? (나는 단지 나무에 대한 나무가 보이지 않는 것 같아요)

+3

자신의 파일 저장소 루트에 추가하고 (http://uk3.php.net/realpath) 그 파일의 루트로 시작하는지 확인하십시오. – halfer

+1

이것은 파일 이름/디렉토리 이름을 통해 XSS에 취약합니다. https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet – Cheekysoft

답변

1

다음은 내가 그런 것들에 사용했던 기본적인 보안 검사입니다.

function isSecurityViolation($dir) { 
    if (preg_match('#https?://#i', $dir)) { 
     return true; 
    } 

    // no directory traversal attempts 
    if (strpos($dir, '../') !== false) { 
     return true; 
    } 

    if (strpos($dir, "\x00") !== false) { 
     return true; 
    } 

    if (strpos($dir, '%00') !== false) { 
     return true; 
    } 

    return false; 
} 

그것은 다음과 같은 위반을 검사 :

  • 는 HTTP 또는 HTTPS URL 악성 URL이 특정 기능을 전달할 수 있습니다 ..
  • 널 문자 확인을 사용하여
  • 디렉토리 탐색을 열려고

어딘가에 스크립트의 시작 부분 t, 당신이 호출 할 수 있습니다 [realpath()를]을 실행 한 다음 사용자에서 상대 경로가 있으면,

if (isSecurityViolation($dir)) { 
    die('No soup for you.'); 
} 
+0

thx mate! 잘 작동합니다! – Manticore

+1

최소한 멀티 바이트 문자 공격에 여전히 취약 할 수 있습니다. 가능하면 화이트리스트 접근 방식을 선호하십시오. 이와 같은 블랙리스트는 항상 모든 가능한 공격 벡터의 전체 목록을 포함하지 않을 것입니다. – Cheekysoft