文件上传漏洞代码

Web安全之文件上传漏洞

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

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

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

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

0x01 MIME类型检测

  • 前端界面
    <html>
    <meta charset="UTF-8">
    <body>
    <form action=upload_file.php method="post"enctype="multipart/form-data">
    <label for="file">上传文件:</label>
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="submit" value="上传">
    </form>
    </body>
    </html>
  • 后端代码

    <?php
    if ((($_FILES["file"]["type"] == "image/gif") 
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 20000))
      {
      if ($_FILES["file"]["error"] > 0)  //file_error等于0的话则上传成功
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";//由文件上传导致的错误代码
        }
      else
        {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";//被上传文件的名称
        echo "Type: " . $_FILES["file"]["type"] . "<br />"; //被上传文件的类型
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";//被上传文件的大小,以字节计
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";//存储在服务器的文件的临时副本的名称
    
        if (file_exists("up/" . $_FILES["file"]["name"]))//file_exists() 函数检查文件或目录是否存在
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "up/" . $_FILES["file"]["name"]);//move_uploaded_file() 函数将上传的文件移动到新位置。
          echo "Stored in: " . "up/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?>
  • 绕过方式

将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/" . '文件夹不存在,请手工创建!';
    }
}
?>
  • 绕过方式
    1. 利用windows特性, 在文件名后面加 . 就是 shell.php. 上传到服务器后由于windows特性自动去掉.
    2. 利用 apache特性, 由右向左解析
    3. 利用 不常见的后缀 php2、 php3、 phtml等等
    4. 上传.htaccess文件

    0x03 文件头检测

​ 后端代码:

<?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 "上传出错!";
        }
    }
}
?>
  • 绕过方式

    方法一:

    使用winhex等编辑器 打开图片文件,将webshell直接插入末尾

    方法二:

    直接上传webshell,使用burpsuit 拦截并添加图片的文件头

    方法三:

    使用cmd命令行 copy 生成图片马

    这种图片马,一般和文件包含漏洞在一起搭配


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/" . '文件夹不存在,请手工创建!';
    }
}
?>

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

猜你喜欢

转载自www.cnblogs.com/dxmbrsl/p/12431610.html