DVWA File upload 過關秘籍.
LOW
1if( isset( $_POST[ 'Upload' ] ) ) {
2 // Where are we going to be writing to?
3 $target_path = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/";
4 $target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] );
5
6 // Can we move the file to the upload folder?
7 // 完全沒做過濾.
8 // 上傳一個PHP文件也是可以的.
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}
Medium
1if( isset( $_POST[ 'Upload' ] ) ) {
2 // Where are we going to be writing to?
3 $target_path = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/";
4 $target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] );
5
6 // File information
7 $uploaded_name = $_FILES[ 'uploaded' ][ 'name' ];
8 $uploaded_type = $_FILES[ 'uploaded' ][ 'type' ];
9 $uploaded_size = $_FILES[ 'uploaded' ][ 'size' ];
10
11 // Is it an image?
12 // // 開始做了一些過濾
13
14 // 下面是官方對$_FILES 函數的描述
15 // [name] => MyFile.txt (comes from the browser, so treat as tainted)
16 // [type] => text/plain (not sure where it gets this from - assume the browser, so treat as tainted)
17 // [tmp_name] => /tmp/php/php1h4j1o (could be anywhere on your system, depending on your config settings, but the user has no control, so this isn't tainted)
18 // [error] => UPLOAD_ERR_OK (= 0)
19 // [size] => 123 (the size in bytes)
20
21 // 其中對name和type的description的描述都是 `treat as tainted`(被汙染的)
22 // 這意味著它有可能會被修改 unsafe
23
24 // 我們可以嘗試上傳一個PHP文件,使用一些攔截請求工具,修改即將發出的請求.
25 // 來達到修改`name`中的後綴名和`type`中的媒體類型.
26 if( ( $uploaded_type == "image/jpeg" || $uploaded_type == "image/png" ) &&
27 ( $uploaded_size < 100000 ) ) {
28
29 // Can we move the file to the upload folder?
30 if( !move_uploaded_file( $_FILES[ 'uploaded' ][ 'tmp_name' ], $target_path ) ) {
31 // No
32 echo '<pre>Your image was not uploaded.</pre>';
33 }
34 else {
35 // Yes!
36 echo "<pre>{$target_path} succesfully uploaded!</pre>";
37 }
38 }
39 else {
40 // Invalid file
41 echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';
42 }
43}
High
1if( isset( $_POST[ 'Upload' ] ) ) {
2 // Where are we going to be writing to?
3 $target_path = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/";
4 $target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] );
5
6 // File information
7 $uploaded_name = $_FILES[ 'uploaded' ][ 'name' ];
8 // jpg
9 $uploaded_ext = substr( $uploaded_name, strrpos( $uploaded_name, '.' ) + 1);
10
11 // file size
12 $uploaded_size = $_FILES[ 'uploaded' ][ 'size' ];
13
14 // tmp_name 是臨時副本的名字
15 $uploaded_tmp = $_FILES[ 'uploaded' ][ 'tmp_name' ];
16
17 // Is it an image?
18 // 和上面的比起來多個一個文件後綴名的判斷.
19 // strtolower 轉小寫
20 // 擴展名只要滿足jpeg,png或者jpg就行
21 if( ( strtolower( $uploaded_ext ) == "jpg" || strtolower( $uploaded_ext ) == "jpeg" || strtolower( $uploaded_ext ) == "png" ) &&
22 ( $uploaded_size < 100000 ) &&
23 // getimagesize 獲取圖像信息
24 // 這個函數保證你穿的一定得是個圖像
25 // 可以用圖片木馬繞過
26 getimagesize( $uploaded_tmp ) ) {
27
28 // Can we move the file to the upload folder?
29 if( !move_uploaded_file( $uploaded_tmp, $target_path ) ) {
30 // No
31 echo '<pre>Your image was not uploaded.</pre>';
32 }
33 else {
34 // Yes!
35 echo "<pre>{$target_path} succesfully uploaded!</pre>";
36 }
37 }
38 else {
39 // Invalid file
40 echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';
41 }
42}
High
1if( isset( $_POST[ 'Upload' ] ) ) {
2 // Where are we going to be writing to?
3 $target_path = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/";
4 $target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] );
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_tmp = $_FILES[ 'uploaded' ][ 'tmp_name' ];
11
12 // Is it an image?
13 // 對比上面多驗證了文件的後綴名
14 if( ( strtolower( $uploaded_ext ) == "jpg" || strtolower( $uploaded_ext ) == "jpeg" || strtolower( $uploaded_ext ) == "png" ) &&
15 ( $uploaded_size < 100000 ) &&
16
17
18
19 // 函數會通過讀取文件頭,返回圖片的長、寬等信息,如果沒有相關的圖片文件頭,函數會報錯
20 getimagesize( $uploaded_tmp ) ) {
21
22 // 可以看到,High級別的代碼讀取文件名中最後一個”.”後的字符串,期望通過文件名來限制文件類型
23 // 因此要求上傳文件名形式必須是”*.jpg”、”*.jpeg” 、”*.png”之一
24 // 同時,getimagesize函數更是限制了上傳文件的文件頭必須為圖像類型
25
26 // Can we move the file to the upload folder?
27 if( !move_uploaded_file( $uploaded_tmp, $target_path ) ) {
28 // No
29 echo '<pre>Your image was not uploaded.</pre>';
30 }
31 else {
32 // Yes!
33 echo "<pre>{$target_path} succesfully uploaded!</pre>";
34 }
35 }
36 else {
37 // Invalid file
38 echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';
39 }
40}
Impossible
1if( isset( $_POST[ 'Upload' ] ) ) {
2 // Check Anti-CSRF token
3 checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
4
5 // File information
6 $uploaded_name = $_FILES[ 'uploaded' ][ 'name' ];
7 $uploaded_ext = substr( $uploaded_name, strrpos( $uploaded_name, '.' ) + 1);
8 $uploaded_size = $_FILES[ 'uploaded' ][ 'size' ];
9 $uploaded_type = $_FILES[ 'uploaded' ][ 'type' ];
10 $uploaded_tmp = $_FILES[ 'uploaded' ][ 'tmp_name' ];
11
12 // Where are we going to be writing to?
13 $target_path = DVWA_WEB_PAGE_TO_ROOT . 'hackable/uploads/';
14 //$target_file = basename( $uploaded_name, '.' . $uploaded_ext ) . '-';
15
16 // MD5 加密
17 $target_file = md5( uniqid() . $uploaded_name ) . '.' . $uploaded_ext;
18 $temp_file = ( ( ini_get( 'upload_tmp_dir' ) == '' ) ? ( sys_get_temp_dir() ) : ( ini_get( 'upload_tmp_dir' ) ) );
19 $temp_file .= DIRECTORY_SEPARATOR . md5( uniqid() . $uploaded_name ) . '.' . $uploaded_ext;
20
21 // Is it an image?
22 if( ( strtolower( $uploaded_ext ) == 'jpg' || strtolower( $uploaded_ext ) == 'jpeg' || strtolower( $uploaded_ext ) == 'png' ) &&
23 ( $uploaded_size < 100000 ) &&
24 ( $uploaded_type == 'image/jpeg' || $uploaded_type == 'image/png' ) &&
25 getimagesize( $uploaded_tmp ) ) {
26
27 // Strip any metadata, by re-encoding image (Note, using php-Imagick is recommended over php-GD)
28 if( $uploaded_type == 'image/jpeg' ) {
29 $img = imagecreatefromjpeg( $uploaded_tmp );
30 imagejpeg( $img, $temp_file, 100);
31 }
32 else {
33 $img = imagecreatefrompng( $uploaded_tmp );
34 imagepng( $img, $temp_file, 9);
35 }
36 imagedestroy( $img );
37
38 // Can we move the file to the web root from the temp folder?
39 if( rename( $temp_file, ( getcwd() . DIRECTORY_SEPARATOR . $target_path . $target_file ) ) ) {
40 // Yes!
41 echo "<pre><a href='${target_path}${target_file}'>${target_file}</a> succesfully uploaded!</pre>";
42 }
43 else {
44 // No
45 echo '<pre>Your image was not uploaded.</pre>';
46 }
47
48 // Delete any temp files
49 if( file_exists( $temp_file ) )
50 unlink( $temp_file );
51 }
52 else {
53 // Invalid file
54 echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';
55 }
56}
57
58// Generate Anti-CSRF token
59generateSessionToken();
Extension
00%截斷
1$is_upload = false;
2$msg = null;
3if(isset($_POST['submit'])){
4 // 白名單
5 $ext_arr = array('jpg','png','gif');
6 $file_ext = substr($_FILES['upload_file']['name'],strrpos($_FILES['upload_file']['name'],".")+1);
7 if(in_array($file_ext,$ext_arr)){
8 $temp_file = $_FILES['upload_file']['tmp_name'];
9 // 注意這個位置
10 // 最後拼接的儲存路徑,是由用戶提交上的數據來做為路徑
11 $img_path = $_POST['save_path']."/".rand(10, 99).date("YmdHis").".".$file_ext;
12
13 if(move_uploaded_file($temp_file,$img_path)){
14 $is_upload = true;
15 } else {
16 $msg = "上傳失敗";
17 }
18 } else {
19 $msg = "只允許上傳.jpg|.png|.gif類型文件!";
20 }
21}
代碼採用的白名單校驗,只允許上傳圖片格式,理論上這個上傳是不好繞過的。
但是後面採用保存文件的時候,是路徑拼接的形式,而路徑又是從前端獲取,所以我們可以在路徑上做手腳。
如下上傳,顯示文件路徑中有個空格,這並不是真正意義上的空格,而是%00截斷後顯示成的空格。
在url中%00表示ascll碼中的0 ,而ascii中0作為特殊字符保留,表示字符串結束,所以當url中出現%00時就會認為讀取已結束 (php版本要小於5.3.4,5.3.4及以上已經修復該問題)