upload labs实战11-20

文件上传漏洞
摘要由CSDN通过智能技术生成

Pass-11 GET00截断绕过

image.png

<?php
include '../config.php';
include '../head.php';
include '../menu.php';

$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'];
		//上传的文件最终要保存的目录 读到%00就结束了
        $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类型文件!";
    }
}
?>

如果上传路径不可控

image.png image.png

Pass-12 POST00截断绕过

![image.png](https://img-blog.csdnimg.cn/img_convert/be843206c8e26f0b211a9c571b8497e2.png#averageHue=#f1f0f0&clientId=u87ac3729-03d2-4&from=paste&height=122&id=ubade5463&name=image.png&originHeight=122&originWidth=472&originalType=binary&ratio=1&rotation=0&showTitle=true&size=10960&status=done&style=none&taskId=u46dcb395-db00-4b12-8f6e-ddf0e756e3a&title=要在Hex 00截断&width=472 “要在Hex 00截断”)

Pass-13 图片马绕过

方法1

image.png
http://www.upload.com/include.php?file=./upload/9620220222032411.gif
文件头验证函数

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

方法2

上传合成图片马

Pass-14 图片马绕过

文件后缀验证

<?php
include '../config.php';
include '../head.php';
include '../menu.php';

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 = "上传出错!";
        }
    }
}
?>

Pass-15 exif_imagetype绕过

函数返回值类型验证

<?php
include '../config.php';
include '../head.php';
include '../menu.php';

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 = "上传出错!";
        }
    }
}
?>

Pass-16 图像二次渲染绕过

<?php
include '../config.php';
include '../head.php';
include '../menu.php';

$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);//basename():显示带有文件扩展名的文件名

    // 获得上传文件的扩展名
	  搜索 "." 在字符串中的位置,并返回从该位置到字符串结尾的所有字符
    $fileext= substr(strrchr($filename,"."),1);
    //判断文件后缀与类型,合法才进行上传操作
    if(($fileext == "jpg") && ($filetype=="image/jpeg")){
      将文件移动到上传目录
        if(move_uploaded _file($tmpname,$target_path)){
            //使用上传的图片生成新的图片
              创建一块画布,并从JPEG文件或者URL地址载入一副图像
            $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 = "上传出错!";
        }

先上传一个gif文件,上传成功后另存到本地,利用WinHex查看二进制文件,在不变的部分插入一句话木马。
image.png

Pass-17 条件竞争绕过

<?php
include '../config.php';
include '../head.php';
include '../menu.php';

$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 = '上传出错!';
    }
}
?>

利用 u p l o a d f i l e 与 r e n a m e ( upload_file与rename( uploadfilerename(upload_file, $img_path)的时间差

Pass-18 条件竞争加解析漏洞绕过

Pass-19 00截断漏洞绕过

<?php
include '../config.php';
include '../common.php';
include '../head.php';
include '../menu.php';

$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 . '文件夹不存在,请手工创建!';
    }
}
?>
  1. 把lw19.php改为lw19.jpg,绕过 if(!in_array( f i l e e x t , file_ext, fileext,deny_ext))
  2. 利用Windows特性,通过截断,读到%00就结束了,

绕过 i m g p a t h = U P L O A D P A T H . ′ / ′ . img_path = UPLOAD_PATH . '/' . imgpath=UPLOADPATH./.file_name;
if (move_uploaded_file($temp_file, $img_path))
image.png
image.png

Pass-20 数组配合windows特性绕过

<?php
include '../config.php';
include '../common.php';
include '../head.php';
include '../menu.php';

if (isset($_POST['submit'])) {
    if (file_exists(UPLOAD_PATH)) {

        $is_upload = false;
        $msg = null;
        if(!empty($_FILES['upload_file'])){
            //mime check
            $allow_type = array('image/jpeg','image/png','image/gif');
            if(!in_array($_FILES['upload_file']['type'],$allow_type)){
                $msg = "禁止上传该类型文件!";
            }else{
                //check filename
                $file = empty($_POST['save_name']) ? $_FILES['upload_file']['name'] : $_POST['save_name'];
                if (!is_array($file)) {
                    $file = explode('.', strtolower($file));
                }//a.php 用点分割开

                $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];
                  a.php
                    $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 = "请选择要上传的文件!";
        }
        
    } else {
        $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
    }
}
?>

array[2]
[0]=>“20.php”
[2]=>“jpg”
image.png

  • 首先修改Content-Type: application/octet-stream为

image/jpeg 绕过if(!in_array( F I L E S [ ′ u p l o a d f i l e ′ ] [ ′ t y p e ′ ] , _FILES['upload_file']['type'], FILES[uploadfile][type],allow_type))

  • e x t = e n d ( ext = end( ext=end(file)时,取jpg,正好绕过了
$allow_suffix = array('jpg','png','gif');
    if (!in_array($ext, $allow_suffix)) {
        $msg = "禁止上传该后缀文件!";
  • f i l e n a m e = r e s e t ( file_name = reset( filename=reset(file) . ‘.’ . f i l e [ c o u n t ( file[count( file[count(file) - 1]时,刚好取lwbb20.php
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值