php octet stream,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->不通过,返回错误给前端
猜测原因
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类型伪造测试(文件上传漏洞)
$allowExcelMimeType = [
'application/vnd.ms-excel',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
];
$allowExcelExt = [
'xls', 'xlsx',
];
$fileInfo = $_FILES['fieldNameHere'];
$pos = mb_strripos($fileInfo['name'], '.', 0, 'utf8');
$ext = mb_substr($fileInfo['name'], $pos + 1);
$destination = md5(time()) . '.' . $ext;
move_uploaded_file($fileInfo['tmp_name'], $destination);
var_dump($fileInfo);
$mimeType = mime_content_type($destination);
echo '文件类型:'.$mimeType.PHP_EOL;
if (!in_array($ext, $allowExcelExt, true) || !in_array($fileInfo['type'], $allowExcelMimeType, true)) {
echo '非法的文件类型:'.$mimeType.PHP_EOL;
exit();
}
echo '上传成功';
excel正常上传返回数据:
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:
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
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的函数 finfo 和 mime_content_type
问题及拓展
1.文件的类型的二进制约定方式(查找RFC)
2.浏览器怎么获取文件的mimet-type
参考
php octet stream,php 上传excel时,excel mime-type类型为application/octet-stream,无法通过验证...相关推荐
- php上传图片限制类型,php,_使用php的图片上传类进行图片上传,总是提示:上传文件时出错 : 未允许类型 。都是默认的配置,php - phpStudy...
使用php的图片上传类进行图片上传,总是提示:上传文件时出错 : 未允许类型 .都是默认的配置 使用php的图片上传类进行图片上传,总是提示:上传文件时出错 : 未允许类型 .都是默认的配置 $upl ...
- 上传file时accept限制文件类型pdf、doc、docx、 jpg、 png、xls 、xlsx等格式
<input type="file" accept="image/*" /> accept表示可以上传文件类型,image表示图片,*表示所有支持的 ...
- python 判断文件类型_python接口自动化(三十) 上传文件时自动判断文件类型(filetype)...
前言 如何判断一个文件的类型呢,判断这个文件是png还是jpg,还是MP3文件?filetype包是python用来判断文件类型的依赖包,github地址:https://github.com/h2n ...
- python接口自动化(三十) 上传文件时自动判断文件类型(filetype)
前言 如何判断一个文件的类型呢,判断这个文件是png还是jpg,还是MP3文件?filetype包是python用来判断文件类型的依赖包,github地址:https://github.com/h2n ...
- 【github系列】github上传文件时忽略部分文件类型
参考网址:https://blog.csdn.net/Vansal/article/details/88208848
- transporter上传卡正在交付_Xcode11使用Transporter上传AppStore时卡住解决方式,亲测可用...
Xcode11使用Transporter上传AppStore时卡住解决方式,亲测可用 在Authenticating with the iTunes store(即AppStore认证)卡住 解决办法 ...
- 使用 JavaScript 上传 PDF 和 Excel 等二进制文件到 ABAP 服务器并进行解析
这是 Jerry 2021 年的第 71 篇文章,也是汪子熙公众号总共第 348 篇原创文章. Jerry 之前发布过一篇文章 不使用任何框架,手写纯 JavaScript 实现上传本地文件到 ABA ...
- 解决php上传文件过程中Warning move_uploaded_file, failed to open stream的解决方案
报错信息: Warning: move_uploaded_file(upload/test.png) [function.move-uploaded-file]: failed to open str ...
- 化境上传的时出现 错误 '800a0bbc' 的解决方案
化境上传的时出现 错误 '800a0bbc' 的解决方案 我使用化境上传的时候出现 ADODB.Stream 错误 '800a0bbc' 写入文件失败. /upload_5xsoft.inc,行 18 ...
- smartupload 上传文件时 把页面编码改成gbk 解决乱码
快来java1234 吧 smartupload 上传文件时,经常会发生因为把表单设置为 enctype="multipart/form-data"而出现的中文乱码问题,本人头疼好 ...
最新文章
- 确定浏览器是否支持某些DOM模块
- Metasploit设置VERBOSE参数技巧
- 读书笔记 effective c++ Item 50 了解何时替换new和delete 是有意义的
- 文件操作2-Day3
- Spring AOP 代理模式
- 论文浅尝 | WWW2020 - 知识图谱中的实体摘要:算法、评价和应用 (PPT)
- JAVA设计模式之抽象工厂模式和代理模式
- 90后美少女如何过五关斩六将入职支付宝技术男团?
- Redis集群搭建~Redis-x64-3.2.100版本
- 前端与移动开发之vue-day1(1)
- 如何从源码启动和编译IoTSharp
- hightcharts 3d 堆积图下钻
- ios系统框架一: Core Animation
- 学习 trajectory.txt
- GoLand添加一个已有项目的相关配置
- 如果有人问你爬虫抓取技术的门道,请叫他来看这篇文章
- 3.22	进口物料的报关手册核销
- 王德顺《追求自由的亡命徒》
- python微博评论情感分析_用python对鹿晗、关晓彤微博进行情感分析
- ios中Date.prase()兼容问题