Skip to content

Latest commit

 

History

History
69 lines (54 loc) · 6.99 KB

06.文件上传漏洞.md

File metadata and controls

69 lines (54 loc) · 6.99 KB

06.文件上传漏洞

文件上传漏洞是指用户上传可执行的脚本文件,并通过该脚本获得执行服务器命令的能力。

6.1 危险因素

  • 该漏洞的影响非常严重,它允许用户上传的脚本文件可以在服务端或客户端运行,检测它的难度也非常高,然而该漏洞也非常普遍。
  • 通过该漏洞可以执行攻击者上传的webshell脚本。通过webshell,攻击者可以执行系统命令,甚至可以控制整个操作系统。
  • 攻击者可以上传钓鱼网页到网站。
  • 攻击者可以将存储XSS放入网站。
  • 这个漏洞可以使网站容易受到XSS等一些其他类型的攻击。
  • 图片上传漏洞甚至可以触发客户端的图片库缺陷从而使浏览网页的客户端遭受攻击,如果图片按原比例显示在客户浏览器。(IE浏览器曾经爆出此漏洞)
  • 内嵌在上传文件的脚本文件或其他脚本可以被执行,当服务器解析存在缺陷或客户端浏览器存在缺陷。
  • 上传病毒,木马文件,引诱用户或管理员进行下载并执行。
  • 上传Flash的策略文件crossdomain.xml以控制Flash在该域下的行为。
  • 上传.htaccess文件来覆盖或重置apache目录下的文件的读写和执行的能力。

6.2 攻击实例

  • 上传jsp、asp、php等文件 -> 让服务器执行从而获得系统权限。
  • 上传超大文件 -> 文件空间拒绝服务。
  • 使用恶意上传文件路径或名称 -> 覆盖关键文件。
  • 上传包含Web脚本语言的标签的文件 -> 包含标签的文件被include执行。
  • 上传exe或sh文件 -> 引诱用户后管理员下载并执行。
  • 上传包含Javascript脚本的文件 -> 引用用户请求执行来实施XSS攻击。

6.3 绕过技巧

  • 如果网站只限制了jsp、php、asp后缀的文件,你可以尝试使用Web语言的拓展后缀,例如jspx、jspf、php2、php3、php4、php5、aspx等。这些后缀文件的效果跟jsp、php、asp后缀的文件别无二致,Web服务器同样也可以执行这些后缀文件的脚本。
  • 有些时候,你甚至可以尝试使用大小写后缀(Jsp、aSP、PhP等)来绕过程序的过滤规则。这些都是由于程序员的疏忽所导致的。
  • 使用在文件结尾添加__空格__、/、或__.__在有些时候可以绕过程序的过滤规则。 因为这些文件在保存的时候,文件名末尾的特殊字符会自定被去除。可以使用本地带来来修改上传的文件后缀(例如“file.asp ... ... . . .. ..”, “file.asp ”, or “file.asp.”)。
  • 有些服务器会使用文件名的第一个点的拓展名作为文件名解析,因此可以上传file.php.jsp这类的文件名进行尝试绕开过滤规则。
  • In case of using IIS6 (or prior versions), it might be possible to bypass this protection by adding a semi-colon after the forbidden extension and before the permitted extension (example: “file.asp;.jpg”).
  • In case of using IIS6 (or prior versions), it might be possible to bypass this protection by putting an executive file such as ASP with another extension in a folder which ends with an executive extension such as “.asp” (example: “folder.asp\file.txt”). Besides, it is possible to create a directory just by using a file uploader and ADS (Alternate Data Stream). In this method, filename should end with “::$Index_Allocation” or “:$I30:$Index_Allocation” to create a directory instead of a file (example: “newfolder.asp::$Index_Allocation” creates “newfolder.asp” as a new directory).
  • This protection can be completely bypassed by using the e.g. control characters like Null (0x00) after the forbidden extension and before the permitted one. In this method, during the saving process all the strings after the Null character will be discarded. Putting a Null character in the filename can be simply done by using a local proxy or by using a script (example: “file.asp%00.jpg”). Besides, it would be perfect if the Null character is inserted directly by using the Hex view option of a local proxy such as Burpsuite or Webscarab in the right place (without using %).
  • It is also possible to create a file with a forbidden extension by using NTFS alternate data stream (ADS). In this case, a “:” sign will be inserted after the forbidden extension and before the permitted one. As a result, an empty file with the forbidden extension will be created on the server (example: “file.asp:.jpg”). Attacker can try to edit this file later to execute his/her malicious codes. However, an empty file is not always good for an attacker. So, there is an invented method by the author of this paper in which an attacker can upload a non-empty shell file by using the ADS. In this method, a forbidden file can be uploaded by using this pattern: “file.asp::$data.”.
  • 在Windows服务器中,可以尝试使用文件的简写进行文件替换。(例如:“web.config”可以使用“web~1.con”进行替换)
  • 结合以上多种方法,会得到意想不到的的效果。

6.4 预防措施

6.4.1 开发人员和网站管理员

  • 建立白名单,白名单以外的文件类型一概拒绝。
  • 判断文件类型时,结合MIME Type,后缀等方式。
  • 删除在文件名中出现的所有__控制字符__和__Unicode字符__;删除在文件名出现的";",":",">","<","/","",".","*","%","$"等特殊字符。如果系统需要接收Unicode字符,强烈建议只接收Alpha-Numeric字符和一个"."作为文件名和文件名的拓展,同时,不需要文件名为空。(正则表达式为__[a-zA-Z0-9]{1,200}\.[a-zA-Z0-9]{1,10}__)
  • 限制文件名的长度。例如在NTFS分区中,文件名+拓展名的长度限制在255个字符以内。
  • 建议使用算法来重命名文件名。例如,使用__【原始文件名+文件数据+随机数】__进行MD5哈希计算,然后把哈希计算的结果作为文件的名称。
  • 对图片文件的上传,可以使用压缩算法或resize函数,可以破坏图片中可能包含的HTML代码。
  • Uploaded directory should not have any “execute” permission.???没有执行权限的目录怎么浏览?
  • 限制文件大小【最大以及最小都需要考虑】以防止拒绝服务攻击。
  • 使用跨域请求伪造保护方法(通常提交同时提交token)。
  • 防止覆盖敏感文件。
  • 同时使用POST方法进行上传操作。
  • 记录用户行为,对用户的操作进行追踪。
  • 在压缩文件中提取内容,应该逐个检查压缩文件中的内容。

6.4.2 系统管理员

  • 单独设置文件服务器的域名,以防止XSS等利用同源策略发起的攻击。
  • 文件上传的目录设置为不可执行。
  • 禁止Web容器对上传文件目录下的文件执行脚本的能力。

6.5 参考资料