文件上传漏洞代码

时间:2020-03-06 23:50:34   收藏:0   阅读:72

Web安全之文件上传漏洞

1、编写一个上传图片的功能页面

2、针对上传的文件进行验证(后缀验证、文件头验证、文件名验证等)

3、文件上传通常会与文件解析漏洞相结合,可以收集整理存在解析漏洞的组件和相关版本,无法部署相关环境,可以学习相关技术,不用实际操作

扩展学习:学习如何绕过黑名单验证、文件头验证,如何杜绝上传图片的功能无法利用获取 shell ?

0x01 MIME类型检测

将content-type修改为 image/jpeg image/png image/gif 其中一个即可


0x02 后缀名检测

后端代码
<?php
header("Content-Type: text/html;charset=utf-8");
if (isset($_POST['submit'])) {
    if (file_exists("up/")) {
        $deny_ext = array('.asp','.aspx','.php','.jsp');
        $file_name = trim($_FILES["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["file"]["tmp_name"];
            $img_path = "up/" . $file_name;            
            if (move_uploaded_file($temp_file,$img_path)) {
                 echo "Stored in: " . "up/" . $file_name;
            } else {
                $msg = '上传出错!';
            }
        } else {
            echo '不允许上传.asp,.aspx,.php,.jsp后缀文件!';
        }
    } else {
        $msg = "up/" . '文件夹不存在,请手工创建!';
    }
}
?>

? 后端代码:

<?php
header("Content-Type: text/html;charset=utf-8");
function getReailFileType($filename){
    $file = fopen($filename, "rb");//fopen() 函数打开一个文件或 URL。
    $bin = fread($file, 2); //只读2字节 fread() 函数读取打开的文件。
    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;
}
if(isset($_POST['submit'])){
    $temp_file = $_FILES['file']['tmp_name'];
    $file_type = getReailFileType($temp_file);

    if($file_type == 'unknown'){
        echo "文件未知,上传失败!";
    }else{
        $img_path = "up/".rand(10, 99).date("YmdHis").".".$file_type;
        if(move_uploaded_file($temp_file,$img_path)){
            echo "Stored in: " .$img_path;
        } else {
            echo "上传出错!";
        }
    }
}
?>

0x04 白名单验证

后端代码:

<?php
header("Content-Type: text/html;charset=utf-8");
if (isset($_POST['submit'])) {
    if (file_exists("up/")) {
          $ext_arr = array('.jpg','.png','.gif');
        //$file_name = trim($_FILES["file"]["name"]);
          $file_name = trim($_FILES["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, $ext_arr)) {
            $temp_file = $_FILES["file"]["tmp_name"];
            $img_path = "up/" . $file_name;            
            if (move_uploaded_file($temp_file,$img_path)) {
                 echo "Stored in: " . "up/" . $file_name;
            } else {
                echo '上传出错!';
            }
        } else {
            echo '不允许上传.asp,.aspx,.php,.jsp后缀文件!';
        }
    } else {
        echo "up/" . '文件夹不存在,请手工创建!';
    }
}
?>

这个白名单控制的上传,目前还没有找到可以绕过的方法, 只能上传图片马,配合文件包含

原文:https://www.cnblogs.com/dxmbrsl/p/12431610.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!