2016-07-26 118 views
-1

嘿,我嘗試驗證,如果用戶試圖所以我使用下面的代碼停止雙擴展uplaod

function ExtensionValide($sFileName) { 

     $fileNameInfo = array(); 
     if (preg_match('/.php.|.js./',$sFileName)) { 
      return false; # Tentative de masquer des extensions dangereuses ! 
     } 
     else { 
      $extension_valide = array('gif','png','jpg','jpeg','txt','pdf','html','mp3','csv'); 
      $extension_upload = strtolower(substr(strrchr($sFileName,'.'),1)); 
      $fileNameInfo[0] = in_array($extension_upload,$extension_valide); 
      $fileNameInfo[1] = ".$extension_upload"; 
      $fileNameInfo[2] = basename($sFileName,$fileNameInfo[1]); 
      return $fileNameInfo; 
     } 
    } 

,但問題仍然是用戶可以uplaod上傳雙擴展名文件以大寫字母,所以我想找到是否有任何正則表達式,可以處理thnig在preg_match('/.php.|.js./',$sFileName)我想說.PHP.png preg_match('/.any_string./',$sFileName)我怎麼能做到這一點

+1

也許你可以在檢查前使用http://php.net/manual/en/function.strtolower.php strtolower?將所有字母轉換爲小寫? – Danielius

+0

@Danielius非常感謝你,你做得對 – PacMan

+0

沒問題,祝你好運:) – Danielius

回答

1

如果修改了正則表達式preg_match('/.php.|.js.|.PHP.|.JS./'),大寫PHP或JS的文件得到匹配。如果你想匹配所有的字符,就像你在preg_match('/.any_string./',$sFileName)中提到的那樣,正則表達式是preg_match('/。[a-zA-Z] *。 /', $sFileName)。 如果要檢查,如果有一個以上週期字符字符串,只需使用下面的代碼片段作爲條件:

$condition = substr_count($sFileName, '.') > 1 

希望它能幫助:)

1

我猜測你正試圖在這裏停止文件上傳漏洞。停止雙重extension upload經常被上傳php文件並執行它們濫用。阻止此類攻擊的正確方法是在上傳目錄中設置適當的權限。添加讀取和寫入權限但不執行權限。

如果你使用亞馬遜S3或其他東西,這將會更簡單。

具體到你的使用情況,這裏是一個示例代碼,您可以使用

<?php 
$file_name = ''; //Unsecure file with double extention 
$file_explode = explode(".",$file_name); 
$extention = $file_explode[sizeof($file_explode)]; // Last extention 
$file_new_name = uniqid()+"."+$extention; 

//Save the file using $file_new_name 
//Add database entry that maps $file_name to $file_new_name 
?> 

這樣就可以防止雙擴展名上傳卻又設置適當的目錄權限是更好的,這種方法。