Web開發安全之檔案上傳安全

來源:互聯網
上載者:User

標籤:擷取   java   possible   .com   path   ima   over   黑名單   第一時間   

  很長一段時間像我這種菜雞搞一個網站第一時間反應就是找上傳,找上傳。藉此機會把檔案上傳的安全問題總結一下。

  首先看一下DVWA給出的Impossible層級的完整代碼:

<?php if( isset( $_POST[ ‘Upload‘ ] ) ) {     // Check Anti-CSRF token     checkToken( $_REQUEST[ ‘user_token‘ ], $_SESSION[ ‘session_token‘ ], ‘index.php‘ );     // File information     $uploaded_name = $_FILES[ ‘uploaded‘ ][ ‘name‘ ];     $uploaded_ext  = substr( $uploaded_name, strrpos( $uploaded_name, ‘.‘ ) + 1);     $uploaded_size = $_FILES[ ‘uploaded‘ ][ ‘size‘ ];     $uploaded_type = $_FILES[ ‘uploaded‘ ][ ‘type‘ ];     $uploaded_tmp  = $_FILES[ ‘uploaded‘ ][ ‘tmp_name‘ ];     // Where are we going to be writing to?     $target_path   = DVWA_WEB_PAGE_TO_ROOT . ‘hackable/uploads/‘;     //$target_file   = basename( $uploaded_name, ‘.‘ . $uploaded_ext ) . ‘-‘;     $target_file   =  md5( uniqid() . $uploaded_name ) . ‘.‘ . $uploaded_ext;     $temp_file     = ( ( ini_get( ‘upload_tmp_dir‘ ) == ‘‘ ) ? ( sys_get_temp_dir() ) : ( ini_get( ‘upload_tmp_dir‘ ) ) );     $temp_file    .= DIRECTORY_SEPARATOR . md5( uniqid() . $uploaded_name ) . ‘.‘ . $uploaded_ext;     // Is it an image?     if( ( strtolower( $uploaded_ext ) == ‘jpg‘ || strtolower( $uploaded_ext ) == ‘jpeg‘ || strtolower( $uploaded_ext ) == ‘png‘ ) &&         ( $uploaded_size < 100000 ) &&         ( $uploaded_type == ‘image/jpeg‘ || $uploaded_type == ‘image/png‘ ) &&         getimagesize( $uploaded_tmp ) ) {         // Strip any metadata, by re-encoding image (Note, using php-Imagick is recommended over php-GD)         if( $uploaded_type == ‘image/jpeg‘ ) {             $img = imagecreatefromjpeg( $uploaded_tmp );             imagejpeg( $img, $temp_file, 100);         }         else {             $img = imagecreatefrompng( $uploaded_tmp );             imagepng( $img, $temp_file, 9);         }         imagedestroy( $img );         // Can we move the file to the web root from the temp folder?         if( rename( $temp_file, ( getcwd() . DIRECTORY_SEPARATOR . $target_path . $target_file ) ) ) {             // Yes!             echo "<pre><a href=‘${target_path}${target_file}‘>${target_file}</a> succesfully uploaded!</pre>";         }         else {             // No             echo ‘<pre>Your image was not uploaded.</pre>‘;         }         // Delete any temp files         if( file_exists( $temp_file ) )             unlink( $temp_file );     }     else {         // Invalid file         echo ‘<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>‘;     } } // Generate Anti-CSRF token generateSessionToken(); ?> 

  我們來分析一下檔案安全上傳的流程:

  1. 取檔案最後的副檔名。
    $uploaded_ext  = substr( $uploaded_name, strrpos( $uploaded_name, ‘.‘ ) + 1);   
  2. 對上傳檔案的檔案名稱做隨機數重新命名操作,DVWA用的是MD5,rand()函數也可以。
     $target_file   =  md5( uniqid() . $uploaded_name ) . ‘.‘ . $uploaded_ext;
  3. 採取白名單方式驗證檔案的尾碼名,MIME-TYPE類型,以及檔案大小。
        if( ( strtolower( $uploaded_ext ) == ‘jpg‘ || strtolower( $uploaded_ext ) == ‘jpeg‘ || strtolower( $uploaded_ext ) == ‘png‘ ) &&         ( $uploaded_size < 100000 ) &&         ( $uploaded_type == ‘image/jpeg‘ || $uploaded_type == ‘image/png‘ 
  4. 至關重要的一點,檢查是否為真正圖片。
    getimagesize( $uploaded_tmp )\\ 若非圖片,則返回一條Flase訊息。
  5. GD庫或image-magick進行二次渲染,洗掉圖片中的惡意代碼。
    $img = imagecreatefromjpeg( $uploaded_tmp ); 
  6. 採用相對路徑回顯到前端頁面。
     if( rename( $temp_file, ( getcwd() . DIRECTORY_SEPARATOR . $target_path . $target_file ) ) )
  • 那些年程式員跟我一起踩過的雷(應用開發常見的錯誤,對照上文開發流程)
  1. JavaScript前端驗證檔案類型

    不吹不黑,除了一些自己做過的政企站,還是一些臨時頁面。互連網行業還真沒有這麼寫的。簡而言之,就是把檔案類型通過JavaScript代碼驗證檔案類型。正確通過,錯誤跳一個alert彈窗。至於怎麼繞不多贅述了,F12、burp大法好。小學生錯誤,不多贅述。

  2. 上傳檔案黑名單,不驗證MIME-TYPE類型。

    保證安全的檔案上傳一定要用白名單,同時要驗證MIME-TYPE類型。普通的黑名單bypass不過多贅述,大家都比較瞭解。印象比較深的就是某第三方開發軟體,通過黑名單驗證的上傳檔案類型而非白名單。結果jspx這個檔案沒有被黑名單包含,加之Tomcat6.0預設設定檔能正常解析jspx,直接伺服器許可權就被拿掉了,剩下做的說多了都是淚。

  3. 不驗證是否為真正的圖片檔案。

    僅僅驗證尾碼名和MIME-TYPE類型是無法判斷是否為真正的檔案。這時候PHP中主要通過getimagesize()來分辨圖片。首先要說一下檔案幻數:

      開啟winhex我們可以看到,不同圖片格式的二進位流是一致的。

      例如GIF檔案就是GIF89a,建立了一個.gif檔案,通過Notepad++編輯如下:

GIF89a(...some binary data...)<?php phpinfo(); ?>(... skipping the rest of binary data ...)

    我們用winhex開啟相關檔案可以看出:

    

    我們再使用getimagesize()函數擷取並echo一下相關的變數值。

      

    如果不使用,檔案幻數頭:

    

         重複上述實驗,返回false。也就是說在驗證了尾碼名白名單,MIME-TYPE以及圖片幻數後,我們能確保上傳的檔案一定是一個圖片。然而,還有種傳說中的東西沒法防禦。圖片馬+解析漏洞,或者圖片馬+包含漏洞。

   4. 圖片二次渲染

   通過GD庫的imagecreatefromjpeg()函數,我們可以洗掉檔案中的一句話木馬,或者惡意代碼。保證檔案二進位流中,不包含惡意代碼。這對解析漏洞或者包含漏洞有著非常不錯的防禦作用。

   5. 不限制上傳覆蓋.htacess檔案

   如果不限制上傳覆蓋.htaccess檔案,我們上述的所有努力都可能白費。

  • 總結:

   本篇僅僅從代碼設計層面去考慮檔案上傳的安全性,未涉及相關的營運安全問題。例如Nginx與Apache的解析漏洞也應該在防禦考慮當中。以及PHP所產生的00截斷問題。這裡不詳加贅述。文章如有錯誤,歡迎大家指正。

   

 

    

 

 

    

Web開發安全之檔案上傳安全

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.