DVWA —— File Upload分析

Low

Low等级的源码如下,对上传文件格式并未做出任何过滤,上传成功后还回显了文件的路径

直接上传php文件,上传成功

到该路径下查看php文件,文件成功解析

 

Medium

Medium等级下的代码对文件类型进行了过滤,限制只能够上传JPEG和png图片,并限制上传文件大小

但仍可以先修改文件扩展名为jpeg,然后抓包看到文件类型为image/jpeg,再将文件扩展名改为php

上传成功

 

 

High

High等级的源码是限制文件扩展名只能为jpeg、jpg、png

此处可使用%00截断来绕过,先将文件后缀改为%00.jpeg,然后上传

抓包找到hex中的%(25)将其改为00(此处将%00都改为了00)

放出数据包,显示上传成功

到对应路径下查看,文件成功解析

 

Impossible

 

 1  <?php
 2 
 3 if( isset( $_POST[ 'Upload' ] ) ) {
 4     // Check Anti-CSRF token
 5     checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
 6 
 7 
 8     // File information
 9     $uploaded_name = $_FILES[ 'uploaded' ][ 'name' ];
10     $uploaded_ext  = substr( $uploaded_name, strrpos( $uploaded_name, '.' ) + 1);
11     $uploaded_size = $_FILES[ 'uploaded' ][ 'size' ];
12     $uploaded_type = $_FILES[ 'uploaded' ][ 'type' ];
13     $uploaded_tmp  = $_FILES[ 'uploaded' ][ 'tmp_name' ];
14 
15     // Where are we going to be writing to?
16     $target_path   = DVWA_WEB_PAGE_TO_ROOT . 'hackable/uploads/';
17     //$target_file   = basename( $uploaded_name, '.' . $uploaded_ext ) . '-';
18     $target_file   =  md5( uniqid() . $uploaded_name ) . '.' . $uploaded_ext;
19     $temp_file     = ( ( ini_get( 'upload_tmp_dir' ) == '' ) ? ( sys_get_temp_dir() ) : ( ini_get( 'upload_tmp_dir' ) ) );
20     $temp_file    .= DIRECTORY_SEPARATOR . md5( uniqid() . $uploaded_name ) . '.' . $uploaded_ext;
21 
22     // Is it an image?
23     if( ( strtolower( $uploaded_ext ) == 'jpg' || strtolower( $uploaded_ext ) == 'jpeg' || strtolower( $uploaded_ext ) == 'png' ) &&
24         ( $uploaded_size < 100000 ) &&
25         ( $uploaded_type == 'image/jpeg' || $uploaded_type == 'image/png' ) &&
26         getimagesize( $uploaded_tmp ) ) {
27 
28         // Strip any metadata, by re-encoding image (Note, using php-Imagick is recommended over php-GD)
29         if( $uploaded_type == 'image/jpeg' ) {
30             $img = imagecreatefromjpeg( $uploaded_tmp );
31             imagejpeg( $img, $temp_file, 100);
32         }
33         else {
34             $img = imagecreatefrompng( $uploaded_tmp );
35             imagepng( $img, $temp_file, 9);
36         }
37         imagedestroy( $img );
38 
39         // Can we move the file to the web root from the temp folder?
40         if( rename( $temp_file, ( getcwd() . DIRECTORY_SEPARATOR . $target_path . $target_file ) ) ) {
41             // Yes!
42             echo "<pre><a href='${target_path}${target_file}'>${target_file}</a> succesfully uploaded!</pre>";
43         }
44         else {
45             // No
46             echo '<pre>Your image was not uploaded.</pre>';
47         }
48 
49         // Delete any temp files
50         if( file_exists( $temp_file ) )
51             unlink( $temp_file );
52     }
53     else {
54         // Invalid file
55         echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';
56     }
57 }
58 
59 // Generate Anti-CSRF token
60 generateSessionToken();
61 
62 ?>

Impossible等级的代码随机生成了文件名并进行了MD5加密,而且还对文件内容进行了检查

 

posted @ 2019-02-17 00:19  Loong716  阅读(338)  评论(0编辑  收藏  举报