something about Pikachu - Unsafe Fileupload

pikachu

pikachu => 备份地址

Unsafe Fileupload

概述

在web应用系统中文件上传功能很常见,比如上传头像,上传附件等。
当后台程序对上传的文件进行的安全判断条件不够严谨时,攻击者可能会上传一些恶意文件,导致造成损失。

在设计文件上传功能时,要对以下方面进行安全考虑。

  • 验证文件类型,后缀名,大小

  • 验证文件上传方式

  • 对文件进行一定复杂程度的重命名

  • 不要暴露文件上传后的路径

client check

是客户端javascript限制,因此将对应触发语句去除即可。

1
2
<!-- 去除onchange函数 -->
<input class="uploadfile" type="file" name="uploadfile" onchange="checkFileExt(this.value)">

MIME type

  • 更改文件后缀名

    是服务端进行MIME判定,因此将恶意文件重命名为bad.png上传,此时MIME图片。 使用burpsuite截断后将文件名再更改为bad.php

  • 更改HTTP Content-Type

    直接上传bad.php,使用burpsuite截断后将HTTP请求的Content-Type更改为image/png

getimagesize

getimagesize()函数能够获取文件头类型。

文件头类型(# 文件头)
  • 添加伪造文件头

    在恶意文件中添加对应的文件头信息,即可伪装为对应的文件。

    1
    2
    3
    4
    5
    // 伪造image文件头
    GIF98
    <?php
    phpinfo();
    ?>
  • 附加恶意内容

    上传正常图片文件,使用burpsuite截断,在请求内容中追加恶意代码。然后利用文件包含漏洞将此文件包含,即可执行隐藏的代码。

    1
    2
    3
    4
    5
    6
    7
    Content-Disposition: form-data; name="uploadfile"; filename="badimg.png"
    Content-Type: image/png

    PNG

    XXXXX....YYYYYY...ZZZ
    <?php phpinfo(); ?>

something about DVWA - File Upload

DVWA

DVWA => 备份地址

File Upload(文件上传)

Low

  • 手动测试

    • 手动上传恶意文件

      上传bad.php

    • 访问恶意文件

      1
      http://train.com/dvwa/vulnerabilities/upload/../../hackable/uploads/bad.php
  • 源码分析

    • request中获取上传的文件

      1
      2
      3
      // Where are we going to be writing to?
      $target_path = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/";
      $target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] );
    • 将文件移动至指定目录

      1
      2
      3
      4
      5
      6
      // Can we move the file to the upload folder?
      if( !move_uploaded_file( $_FILES[ 'uploaded' ][ 'tmp_name' ], $target_path ) ) {
      // failure
      } else {
      // success
      }

Medium

  • 手动测试

    • 将恶意文件扩展名更改为.png,上传

    • 使用burpsuite拦截上传请求,将文件扩展名更改回.php

      这样,上传请求的Content-Typeimage/png,文件却为php

  • 源码分析

    • request中获取上传的文件

      Low级别相同。

    • 获取文件上传信息

      1
      2
      3
      4
      // File information
      $uploaded_name = $_FILES[ 'uploaded' ][ 'name' ];
      $uploaded_type = $_FILES[ 'uploaded' ][ 'type' ];
      $uploaded_size = $_FILES[ 'uploaded' ][ 'size' ];
    • 判断HTTPContent-Type属性

      1
      2
      3
      4
      5
      // Is it an image?
      if( ( $uploaded_type == "image/jpeg" ||$uploaded_type == "image/png" ) &&
      ( $uploaded_size < 100000 ) ) {
      // yes
      }
    • 将文件移动至指定目录

      Low级别相同。

High

  • 手动测试

    • 将恶意文件扩展名更改为.png,并在文件头处添加文件头信息

      1
      2
      3
      4
      5
      6
      7
      GIF98
      <?php
      // 将文件头设置为`GIF98`

      phpinfo();

      ?>
    • 上传后使用File Inclusion漏洞包含此文件

      1
      2
      # File Inclusion High
      http://train.com/dvwa/vulnerabilities/fi/?page=file://C:\xampp\htdocs\DVWA\hackable\uploads/bad.png
  • 源码分析

    • request中获取上传的文件

      Medium级别相同。

    • 获取HTTPContent-Type;文件信息

      1
      2
      3
      4
      5
      6
      // File information
      $uploaded_name = $_FILES[ 'uploaded' ][ 'name' ];
      $uploaded_ext = substr( $uploaded_name, strrpos( $uploaded_name, '.' ) + 1);
      $uploaded_size = $_FILES[ 'uploaded' ][ 'size' ];
      $uploaded_type = $_FILES[ 'uploaded' ][ 'type' ];
      $uploaded_tmp = $_FILES[ 'uploaded' ][ 'tmp_name' ];
    • 判断HTTPContent-Type;文件类型为image;文件头检测

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      // 判断文件扩展名
      // Is it an image?
      if( ( strtolower( $uploaded_ext ) == 'jpg' || strtolower( $uploaded_ext ) == 'jpeg' || strtolower( $uploaded_ext ) == 'png' ) &&
      ( $uploaded_size < 100000 ) &&
      ( $uploaded_type == 'image/jpeg' || $uploaded_type == 'image/png' ) &&
      getimagesize( $uploaded_tmp ) ) {

      // getimagesize(file) 限制文件大小,并通过文件头判断文件类型

      }
    • 将文件移动至指定目录

      Medium级别相同。

Impossible

  • 源码分析

    • 使用token验证身份

      1
      2
      // Check Anti-CSRF token
      checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
    • 获取HTTPContent-Type与文件信息

      High级别相同。

    • 将文件 重命名 后移动至指定目录

      1
      2
      3
      // 使用md5加密文件名
      //$target_file = basename( $uploaded_name, '.' . $uploaded_ext ) . '-';
      $target_file = md5( uniqid() . $uploaded_name ) . '.' . $uploaded_ext;
      1
      2
      3
      4
      // Can we move the file to the web root from the temp folder?
      if( rename( $temp_file, ( getcwd() . DIRECTORY_SEPARATOR . $target_path . $target_file ) ) ) {
      // yes
      }

文件头

  • 常见文件头

    type header
    bmp 424D
    gif GIF98
    jpeg FF D8 FF
    jpg FF D8 FF
    png 89 50 4E 47 0D 0A
    zip 504B0304