web安全——檔案上傳
檔案上傳本身不是漏洞,但如果檔案上傳功能的限制出現紕漏,允許了不合法且影響網站安全的檔案的上傳
可以將不合法且影響網站安全穩定性的檔案等內容上傳的均為“檔案上傳漏洞”
黑方將檔案上傳後可通過手段執行以及上傳的指令碼檔案(通過獲得上傳的地址目錄檢視檔案並達到目的)
一般的,以上所述的內容檔案為通俗的所說的:“一句話木馬”。
而檔案上傳功能是大多web應用均具備的功能(例如圖片、附件、頭像等)正常的將檔案上傳是合法的。
但如果通過修改檔案性質,繞過web應用的限制,將惡意的指令碼檔案上傳到伺服器後臺,並可以執行,意味著獲得了webshell
獲得webshell則意味著伺服器的操作許可權被拿到了下一步的攻擊則是最危險的(違法)
{使用者=是無法直接看見後端程式碼的,後端程式碼在伺服器,當用戶請求伺服器
(靜態下,由伺服器給出響應,瀏覽器直接渲染)
(動態下,瀏覽器和後端的php中介軟體通訊,由中介軟體對程式處理或解釋,最終生成html的結果)}
流程:
成功上傳——獲得指令碼路徑——webshell
成功繞過機制將惡意指令碼上傳到伺服器路徑下後
獲得指令碼存放的路徑
進入指令碼儲存路徑對指令碼執行(中國菜刀)
low等級:
沒有任何審查機制,直接將php指令碼上傳即可上傳成功並獲得指令碼位置後
1 <?php 2 3 if( isset( $_POST[ 'Upload' ] ) ) { 4 // Where are we going to be writing to?php程式碼5 $target_path = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/"; 6 $target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] ); 7 8 // Can we move the file to the upload folder? 9 if( !move_uploaded_file( $_FILES[ 'uploaded' ][ 'tmp_name' ], $target_path ) ) { 10 // No 11 echo '<pre>Your image was not uploaded.</pre>'; 12 } 13 else { 14 // Yes! 15 echo "<pre>{$target_path} succesfully uploaded!</pre>"; 16 } 17 } 18 19 ?>
{ps:實際情況下,使用者是無法直接看見php原始碼和路徑地址的}
Low等級的機制下沒有對上傳的檔案型別進行檢查,所以直接上傳php指令碼即可;會返回路徑(靶機返回,現實中不直接返回)
Medium等級:
添加了對檔案格式、大小的檢查機制
1 if( isset( $_POST[ 'Upload' ] ) ) { 2 // Check Anti-CSRF token 3 checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' ); 4 5 6 // File information 7 $uploaded_name = $_FILES[ 'uploaded' ][ 'name' ]; 8 $uploaded_ext = substr( $uploaded_name, strrpos( $uploaded_name, '.' ) + 1); 9 $uploaded_size = $_FILES[ 'uploaded' ][ 'size' ]; 10 $uploaded_type = $_FILES[ 'uploaded' ][ 'type' ]; 11 $uploaded_tmp = $_FILES[ 'uploaded' ][ 'tmp_name' ]; 12 13 // Where are we going to be writing to? 14 $target_path = DVWA_WEB_PAGE_TO_ROOT . 'hackable/uploads/'; 15 //$target_file = basename( $uploaded_name, '.' . $uploaded_ext ) . '-'; 16 $target_file = md5( uniqid() . $uploaded_name ) . '.' . $uploaded_ext; 17 $temp_file = ( ( ini_get( 'upload_tmp_dir' ) == '' ) ? ( sys_get_temp_dir() ) : ( ini_get( 'upload_tmp_dir' ) ) ); 18 $temp_file .= DIRECTORY_SEPARATOR . md5( uniqid() . $uploaded_name ) . '.' . $uploaded_ext; 19 20 // Is it an image? 21 if( ( strtolower( $uploaded_ext ) == 'jpg' || strtolower( $uploaded_ext ) == 'jpeg' || strtolower( $uploaded_ext ) == 'png' ) && 22 ( $uploaded_size < 100000 ) && 23 ( $uploaded_type == 'image/jpeg' || $uploaded_type == 'image/png' ) && 24 getimagesize( $uploaded_tmp ) ) { 25 26 // Strip any metadata, by re-encoding image (Note, using php-Imagick is recommended over php-GD) 27 if( $uploaded_type == 'image/jpeg' ) { 28 $img = imagecreatefromjpeg( $uploaded_tmp ); 29 imagejpeg( $img, $temp_file, 100); 30 } 31 else { 32 $img = imagecreatefrompng( $uploaded_tmp ); 33 imagepng( $img, $temp_file, 9); 34 } 35 imagedestroy( $img ); 36 37 // Can we move the file to the web root from the temp folder? 38 if( rename( $temp_file, ( getcwd() . DIRECTORY_SEPARATOR . $target_path . $target_file ) ) ) { 39 // Yes! 40 echo "<pre><a href='${target_path}${target_file}'>${target_file}</a> succesfully uploaded!</pre>"; 41 } 42 else { 43 // No 44 echo '<pre>Your image was not uploaded.</pre>'; 45 } 46 47 // Delete any temp files 48 if( file_exists( $temp_file ) ) 49 unlink( $temp_file ); 50 } 51 else { 52 // Invalid file 53 echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>'; 54 } 55 } 56 57 // Generate Anti-CSRF token 58 generateSessionToken(); 59 60 ?>php程式碼
Medium等級下上傳gif/jpg(MIME型別和字尾)且1000b以下的檔案即可上傳成功;除此以外的檔案均被攔截不可上傳。
而在安全領域下有一個名詞:繞過(過狗)
通過Burp代理進行訪問後攔擊資料包並修改後釋放上傳
檔案上傳繞過思路:推薦文章
https://www.cnblogs.com/blacksunny/p/8001201.html
https://www.freebuf.com/articles/web/179954.html