一、介绍
文件上传漏洞是指,用户上传了一个可执行文件脚本,并通过此脚本获取了执行服务器端命令的能力。
Uploaded files represent a significant risk to applications. The first step in many attacks is to get some code to the system to be attacked. Then the attack only needs to find a way to get the code executed. Using a file upload helps the attacker accomplish the first step.
Reference: http://www.owasp.org/index.php/Unrestricted_File_Upload
形成条件
- 上传的文件能够被Web容器解析执行
- 用户可以通过web访问到上传文件
- 上传文件未被格式化或压缩等功能改变内容
二、弱防御与绕过方法:
2.1、浏览器检查
2.1.1 javascript 检查
有些web应用通过前端的javascript代码对文件扩展名进行验证,如果不是白名单中的扩展名,就不会上传给服务器。
检查扩展名的javascript代码
/**
验证文件扩展名是否合法
**/
function checkFiles(str)
{
var strRegex = "(.jpg|.png|.gif|.ps|.jpeg)$"; //用于验证图片扩展名的正则表达式
var re=new RegExp(strRegex);
if (re.test(str.toLowerCase())){
return true;
}
else{
alert("文件名不合法,文件的扩展名必须为jpg,jpeg,gif或ps格式");
return false;
}
}
但攻击者完全可以无视javascript代码,自己构造表单提交,如果这时web服务器相信了浏览器的提交,未对文件进行再次检测,恶意脚本就被成功上传到服务器中。
构建表单代码
<form action="ht