不安全的文件下载与上传 二次渲染绕过

二次渲染绕过
Pass-16
源代码:php

1 $is_upload = false;
 2 $msg = null;
 3 if (isset($_POST[‘submit‘])){
 4     // 得到上传文件的基本信息,文件名,类型,大小,临时文件路径
 5     $filename = $_FILES[‘upload_file‘][‘name‘];
 6     $filetype = $_FILES[‘upload_file‘][‘type‘];
 7     $tmpname = $_FILES[‘upload_file‘][‘tmp_name‘];
 8 
 9     $target_path=$UPLOAD_ADDR.basename($filename);
10 
11     // 得到上传文件的扩展名
12     $fileext= substr(strrchr($filename,"."),1);
13 
14     //判断文件后缀与类型,合法才进行上传操做
15     if(($fileext == "jpg") && ($filetype=="image/jpeg")){
16         if(move_uploaded_file($tmpname,$target_path))
17         {
18             //使用上传的图片生成新的图片
19             $im = imagecreatefromjpeg($target_path);
20 
21             if($im == false){
22                 $msg = "该文件不是jpg格式的图片!";
23             }else{
24                 //给新图片指定文件名
25                 srand(time());
26                 $newfilename = strval(rand()).".jpg";
27                 $newimagepath = $UPLOAD_ADDR.$newfilename;
28                 imagejpeg($im,$newimagepath);
29                 //显示二次渲染后的图片(使用用户上传图片生成的新图片)
30                 $img_path = $UPLOAD_ADDR.$newfilename;
31                 unlink($target_path);
32                 $is_upload = true;
33             }
34         }
35         else
36         {
37             $msg = "上传失败!";
38         }
39 
40     }else if(($fileext == "png") && ($filetype=="image/png")){
41         if(move_uploaded_file($tmpname,$target_path))
42         {
43             //使用上传的图片生成新的图片
44             $im = imagecreatefrompng($target_path);
45 
46             if($im == false){
47                 $msg = "该文件不是png格式的图片!";
48             }else{
49                  //给新图片指定文件名
50                 srand(time());
51                 $newfilename = strval(rand()).".png";
52                 $newimagepath = $UPLOAD_ADDR.$newfilename;
53                 imagepng($im,$newimagepath);
54                 //显示二次渲染后的图片(使用用户上传图片生成的新图片)
55                 $img_path = $UPLOAD_ADDR.$newfilename;
56                 unlink($target_path);
57                 $is_upload = true;               
58             }
59         }
60         else
61         {
62             $msg = "上传失败!";
63         }
64 
65     }else if(($fileext == "gif") && ($filetype=="image/gif")){
66         if(move_uploaded_file($tmpname,$target_path))
67         {
68             //使用上传的图片生成新的图片
69             $im = imagecreatefromgif($target_path);
70             if($im == false){
71                 $msg = "该文件不是gif格式的图片!";
72             }else{
73                 //给新图片指定文件名
74                 srand(time());
75                 $newfilename = strval(rand()).".gif";
76                 $newimagepath = $UPLOAD_ADDR.$newfilename;
77                 imagegif($im,$newimagepath);
78                 //显示二次渲染后的图片(使用用户上传图片生成的新图片)
79                 $img_path = $UPLOAD_ADDR.$newfilename;
80                 unlink($target_path);
81                 $is_upload = true;
82             }
83         }
84         else
85         {
86             $msg = "上传失败!";
87         }
88     }else{
89         $msg = "只容许上传后缀为.jpg|.png|.gif的图片文件!";
90     }
91 }

原理:将一个正常显示的图片,上传到服务器。寻找图片被渲染后与原始图片部分对比仍然相同的数据块部分,将Webshell代码插在该部分,而后上传。具体实现须要本身编写Python程序,人工尝试基本是不可能构造出能绕过渲染函数的图片webshell的。
这里提供一个包含一句话webshell代码并能够绕过PHP的imagecreatefromgif函数的GIF图片示例。
php图像二次渲染:
https://blog.csdn.net/hitwangpeng/article/details/48661433
https://blog.csdn.net/hitwangpeng/article/details/46548849    
https://xz.aliyun.com/t/2657
这两个讲的还能够
不安全的文件下载与上传  二次渲染绕过
打开被渲染后的图片,Webshell代码仍然存在
不安全的文件下载与上传  二次渲染绕过
提供一个jpg格式图片绕过imagecreatefromjpeg函数渲染的一个示例文件。 直接上传示例文件会触发Warning警告,并提示文件不是jpg格式的图片。可是实际上已经上传成功,并且示例文件名没有改变。
不安全的文件下载与上传  二次渲染绕过web

从上面上传jpg图片能够看到咱们想复杂了,程序没有对渲染异常进行处理,直接在正常png图片内插入webshell代码,而后上传示例文件便可,并不须要图片是正常的图片。shell

程序依然没有对文件重命名,携带webshell的无效损坏png图片直接被上传成功。安全

相关文章
相关标签/搜索