问题
在某个客户的电脑上上传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类型伪造测试(文件上传漏洞)
工具:
fiddler
代码:
<?php
$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
参考
阮一峰-mime-type
mime-type
文件上传
浏览器获取mime-type