当前位置: 首页 > 后端技术 > PHP

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

时间:2023-03-30 00:09:12 PHP

问题在客户电脑上上传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.sheet2.我们在后台校验mime-type合法性的时候,通常是这样校验的:'application/vnd.ms-excel','application/vnd.ms-excel','application/vnd.openxmlformats-officedocument.spreadsheetml。表',3。thinkphp5在获取上传文件的时候,检测到上传文件的类型是从浏览器传来的,会导致一些问题,所以验证无法通过,也可能是比较严重的文件上传漏洞。参考链接:MIME上传文件mime类型伪造测试(文件上传漏洞)工具:fiddler代码: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'=>int0'size'=>int17300文件类型:application/octet-stream上传成功excel文件假mime类型: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'=>int0'size'=>int17300文件类型:application/octet-stream非法文件类型:application/octet-streamForgephp文件的mime类型,新建一个php文件,将后缀改成xlsxD:\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'=>int0'size'=>int17文件类型:text/x-php上传成功总结利用该漏洞,我们可以跳过一些文件上传没有严格检测,上传一些坏代码到服务器并执行解决方案。在php中有函数finfo和mime_content_type来获取文件的mime-type。问题及扩展1.文件类型的二进制约定方式(搜索RFC)2.浏览器如何获取文件mimet-type参考阮一峰-mime-typemime-type文件上传浏览器获取mime-type