2012-01-08 2 views
4

내 스크립트를체크 파일 폴더에 존재하는 경우

$secret = check_input($_GET['secret']); 
if(isset($_POST['register'])) { 
    if (isset($secret) || !empty($secret)) { 
     if (file_exists(ROOT . '/intl/codes/' . $secret)) { 
      unlink(ROOT . '/intl/codes/' . $secret); 
      $trusted = 'yes'; 
     } else { 
      $trusted = 'no'; 
     } 
    } 
//$_POST['register'] register details... 
} 
  1. 그것을 할 수있는 또 다른 방법 (더 간단는 등)이 있습니까?
  2. /codes/ 폴더에 $secret이없는 경우 을 어떻게 제거합니까?
  3. $trusted은 파일이 없어도 항상 yes을 제공합니까?

답변

6

디렉터리를 삭제하려면 unlink() 대신 rmdir()을 사용해야합니다.

$secret = check_input($_GET['secret']); 
if(isset($_POST['register'])) { 
    if (!empty($secret)) { 
     if(file_exists(ROOT . '/intl/codes/' . $secret)) { 
      rmdir(ROOT . '/intl/codes/' . $secret); 
      $trusted = 'yes'; 
     } else { 
      $trusted = 'no'; 
     } 
    } 
    //$_POST['register'] register details... 
} 

심각한 보안 위험이 있습니다. check_input()$secret을 제대로 살균하지 못하면 rmdir('/intl/codes/../')은/intl/삭제와 동일합니다. 이런 식으로 뭔가를 시도 :

$allowed = ROOT. '/intl/codes/'; 
$path = realpath($allowed . check_input($_GET['secret'])); 

if(strpos($path, $allowed) === 0) { //Check that $path is within allowed directory 
    if(is_dir($path)) { 
     rmdir($path); 
    } else if(file_exists($path)) { 
     unlink($path); 
    } else { 
     echo "File/folder not found"; 
    } 
} else { 
    echo "Untrusted user tried to delete outside of allowed directory"; 
} 
1
  1. 당신은 if (!empty($secret)) 사용할 수 있습니다 -뿐만 아니라 NULL 값을 empty() 반환 TRUE합니다.

  2. if (file_exists(ROOT . '/intl/codes/' . $secret) && !is_dir(ROOT . '/intl/codes/' . $secret))을 사용하여 파일이 디렉토리가 아니며 해당 경고를 제거하십시오. 그래도 디렉토리를 제거하려면 rmdir() 기능을 사용하십시오.

  3. file_exists()은 디렉토리의 경우에도 TRUE을 반환합니다. 따라서 앞서 말했듯이 인수가 is_dir() 인 디렉토리인지 확인해야합니다.

1
if (file_exists(ROOT . '/intl/codes/' . $secret)) { 
     unlink(ROOT . '/intl/codes/' . $secret); 
     $trusted = 'yes'; 
    } else { 
     $trusted = 'no'; 
    } 

그것을 할 수있는 또 다른 방법이 있나요 (보다 더 간단이, 등)? 링크 해제가의 제거 방법 디렉토리 :

아니, 유일한 방법은이 경고를 생성 $의 비밀에/코드/폴더에 존재하지 않는 경우 file_exists

을 사용하는 것입니다?

$secret 디렉토리로 연결됩니다. if 부분이 true를 반환하기 때문에 실행 경로는 unlink에 도달합니다. 그래서 존재합니다. 파일이 존재하지 않는 경우에도 디렉토리는 $ 항상 신뢰할 왜 rmdir()

네의를 제공합니다 사용 삭제하려면?

unlink은이를 삭제하고 $trustedyes으로 설정하기 때문에.당신이 삭제 한 후 검색 할 때 당신이 존재하지 않는 볼 수 있지만 $trusted은 분명히 yes

1

포함 된 $secret은 빈 문자열입니다, 그러나 당신의 isset() 시험을 통과합니다. 디렉토리ROOT . '/intl/codes/'가 존재하므로 (따라서 file_exists() 수표를 전달 함) 디렉토리를 unlink() 수 없습니다 (사용자의 의도는 아닙니다).

$_GET['secret']에 비어 있지 않은 것이 있는지 확인하고 check_input() 기능을 확인하십시오.

P. 조건의 일부인 isset($secret)을 제거해야합니다. !empty($secret)이면 충분하며 스크립트가 수정됩니다. PHP는 설명서에 명시된 바와 같이

1

file_exists() : 질문 # 3에 대한

Checks whether a file or directory exists 

내 유일한 생각은 : 파일이 존재하고 않는 경우 확인합니다. 단, 파일이 아니라 디렉토리입니다.

$ file_to_check = ROOT : 오류 메시지에 명시된 바와 같이 # 2로

은 또한, 당신은 이런 식으로 뭔가를 할 수 있습니다. '/ intl/codes /'. $ secret;

  1. 내가 check_input()가 무슨 모르겠어요 : 설명의

    $secret = input_get($_GET['secret']); 
    if(isset($_POST['register']) && !empty($secret)) { 
    
        $file_to_check = ROOT . '/intl/codes/' . $secret; 
        if (file_exists($file_to_check)) { 
         if(!is_dir($file_to_check)) 
          unlink($file_to_check); 
         else 
          rmdir($file_to_check); 
         $trusted = 'yes'; 
        } else { 
         $trusted = 'no'; 
        } 
    } 
    
    function input_get($key, $default = ""){ 
    
        if(!isset($_GET[$key])){ 
         return $default; 
        } else { 
         //do input cleanup first, if you want 
         return $_GET[$key]; 
        } 
    } 
    

    조금 :

    if (file_exists($file_to_check)) { 
        if(!is_dir($file_to_check)) 
         unlink($file_to_check); 
        else 
         rmdir($file_to_check); 
        $trusted = 'yes'; 
    } 
    

    하고 # 1 질문에 대한

    , 당신은 하나 이런 식으로 뭔가를 할 수 있습니다 그래서 $_GET[]input_get()에 대한 래퍼 함수를 ​​만들었습니다. isset()을 수행 할 필요가 없으며 기본값도 채 웁니다.
  2. 을 변수 $file_to_check에 넣었 으면 다시 입력 할 필요가 없습니다.
-1

<?php 
    echo file_exists("test.txt"); 
?> 
시도