php 上传excel时,excel mime-type类型为application/octet-stream,无法通过验证

问题

在某个客户的电脑上上传excel时,mime-type类型为application/octet-stream 无法通过后台验证,但在我们的电脑上可以

交互流程

用户使用wps创建文件保存格式,每个不同的文件都拥有不同的mime-type,而此时mime-type为 application/octet-stream ->用户上传excel->浏览器获取文件mime-type 并在请求体中传递Content-Type: 浏览器获取的mime-type->传递到服务器并验证文件的mime-type->不通过,返回错误给前端
clipboard.png

猜测原因

1.可能wps的某个版本将excel的mime-type类型保存为 application/octet-stream 或者其他类型,当浏览器遇到无法识别的类型时,也默认为 application/octet-stream,而在我们的电脑上,wps版本较新,保存的文件mime-type为 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
2.我们在后台验证mime-type的合法性时,一般都会这样验证:
        'application/vnd.ms-excel',
        'application/vnd.ms-excel',
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
3.而thinkphp5在获取上传文件时,检测上传文件的type 是根据浏览器传递过来的,这样就会造成一些问题,所以验证无法通过,也可能是更严重的 文件上传漏洞
参考链接: MIME

上传文件mime类型伪造测试(文件上传漏洞)

工具: fiddler
代码:
excel正常上传返回数据:

clipboard.png

D:\3_tools\wamp\www\test-some\16.upload.php:22:
array (size=5)
  'name' => string 'test.xlsx' (length=9)
  'type' => string 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' (length=65)
  'tmp_name' => string 'D:\3_tools\wamp\tmp\php6F55.tmp' (length=31)
  'error' => int 0
  'size' => int 17300

文件类型:application/octet-stream 上传成功
excel 文件伪造mime-type:
clipboard.png
D:\3_tools\wamp\www\test-some\16.upload.php:22:
array (size=5)
  'name' => string 'test.xlsx' (length=9)
  'type' => string 'application/so-cool' (length=19)
  'tmp_name' => string 'D:\3_tools\wamp\tmp\php2008.tmp' (length=31)
  'error' => int 0
  'size' => int 17300

文件类型:application/octet-stream 非法的文件类型:application/octet-stream 
伪造php文件mime-type,创建一个php文件,将后缀改为xlsx
clipboard.png
D:\3_tools\wamp\www\test-some\16.upload.php:22:
array (size=5)
  'name' => string 'test.php.xlsx' (length=13)
  'type' => string 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' (length=65)
  'tmp_name' => string 'D:\3_tools\wamp\tmp\php133E.tmp' (length=31)
  'error' => int 0
  'size' => int 17

文件类型:text/x-php 上传成功

总结

利用这个漏洞,我们可以跳过一些检测不严格的文件上传,上传一些不好的代码带服务器并执行

解决方案

在php中有获取文件mime-type的函数 finfomime_content_type

问题及拓展

1.文件的类型的二进制约定方式(查找RFC)
2.浏览器怎么获取文件的mimet-type

参考

阮一峰-mime-type
mime-type
文件上传
浏览器获取mime-type

你可能感兴趣的:(wps,thinkphp5,mimetype,mime,php)