Metasploit开发笔记之BrowserExploitServer

一、简介

Metasploit框架提供了不同的mixins供开发浏览器利用模块使用,主要包括:

  • Msf::Exploit::Remote::HttpServer - 最基础的HTTP服务器。
  • Msf::Exploit::Remote::HttpServer::HTML - 提供JavaScript函数,供模块在构建HTML内容时使用。
  • Msf::Exploit::Remote::BrowserExploitServer - 包含HttpServer和HttpServer::HTML的功能,但提供了更多的功能。本文将介绍BrowserExploitServer mixin。

二、自动化利用过程

BrowserExploitServer mixin是专门为浏览器利用设计的。使用此mixin之前,您应该了解它背后自动完成的操作:

  1. 收集浏览器信息
    它会自动收集浏览器的相关信息,包括操作系统名称、版本、浏览器名称、版本、是否使用代理、Java插件版本、Microsoft Office版本等。如果浏览器没有启用JavaScript,则它知道的信息会更少。所有收集到的信息会被存储在由该mixin管理的配置文件中。

  2. 浏览器标记
    该mixin会为浏览器会话打上标签,并使用相同的标签来在需要时检索配置文件。

  3. 判断是否满足利用条件
    在决定是否将利用代码发送到浏览器之前,mixin会先检查模块是否有任何可利用的条件。如果条件不满足,它会向浏览器发送一个404响应,终止操作。

  4. 传递配置信息
    如果满足条件,mixin会将收集的浏览器信息(配置文件)传递给模块,并让模块继续执行后续操作。

提示: 在模块中,你可以检查配置文件中的 :source 键来判断浏览器是否启用了JavaScript。如果 :source 为 “script”,表示启用了JavaScript;如果是 “headers”(即HTTP头部),则表示浏览器禁用了JavaScript。

三、设置可利用条件

能够设置浏览器要求是这个mixin的一个重要功能。它使得你的攻击更加智能和有针对性,避免了误操作。举个例子:假设你有一个针对Internet Explorer的漏洞,仅影响某些MSHTML版本,你可以通过设置 :os_name:ua_name:ua_ver:mshtml_build 来确保不会对其他浏览器执行利用。:mshtml_build 可以在MSHTML的文件属性中的“产品版本”找到。

在模块的元数据中定义可利用的浏览器要求。以下是定义一个受影响的目标运行某个ActiveX控件的例子:

'BrowserRequirements' =>
{
	source: /script/i,
        activex: [
          {
            clsid: '{D27CDB6E-AE6D-11cf-96B8-444553540000}',
            method: 'LoadMovie'
          }
        ],
	os_name: /win/i
}

你还可以定义特定目标的要求。这也是mixin能够自动选择目标的方式,你可以通过“get_target”方法获取目标。以下是如何为IE8在Windows XP和IE9在Windows 7上定义特定目标要求的示例:

'BrowserRequirements' =>
  {
    :source   => /script|headers/i,
    'ua_name' => HttpClients::IE,
  },
'Targets'             =>
  [
    [ 'Automatic', {} ],
    [
      'Windows XP with IE 8',
      {
        :os_name    => 'Windows XP',
        'ua_ver'    => '8.0',
        'Rop'       => true,
        'Offset'    => 0x100
      }
    ],
    [
      'Windows 7 with IE 9',
      {
        'os_name'   => 'Windows 7',
        'ua_ver'    => '9.0',
        'Rop'       => true,
        'Offset'    => 0x200
      }
    ]
  ]

你可以使用以下常量来匹配:os_name

常量目的
OperatingSystems::Match::WINDOWS匹配所有版本的Windows
OperatingSystems::Match::WINDOWS_95匹配Windows 95
OperatingSystems::Match::WINDOWS_98匹配Windows 98
OperatingSystems::Match::WINDOWS_ME匹配Windows ME
OperatingSystems::Match::WINDOWS_NT3匹配Windows NT 3
OperatingSystems::Match::WINDOWS_NT4匹配Windows NT 4
OperatingSystems::Match::WINDOWS_2000匹配Windows 2000
OperatingSystems::Match::WINDOWS_XP匹配Windows XP
OperatingSystems::Match::WINDOWS_2003匹配Windows Server 2003
OperatingSystems::Match::WINDOWS_VISTA匹配Windows Vista
OperatingSystems::Match::WINDOWS_2008匹配Windows Server 2008
OperatingSystems::Match::WINDOWS_7匹配Windows 7
OperatingSystems::Match::WINDOWS_2012匹配Windows 2012
OperatingSystems::Match::WINDOWS_8匹配Windows 8
OperatingSystems::Match::WINDOWS_81匹配Windows 8.1
OperatingSystems::Match::LINUX匹配Linux发行版
OperatingSystems::Match::MAC_OSX匹配Mac OSX
OperatingSystems::Match::FREEBSD匹配FreeBSD
OperatingSystems::Match::NETBSD匹配NetBSD
OperatingSystems::Match::OPENBSD匹配OpenBSD
OperatingSystems::Match::VMWARE匹配VMWare
OperatingSystems::Match::ANDROID匹配Android
OperatingSystems::Match::APPLE_IOS匹配Apple IOS

你可以使用以下常量来匹配:ua_name

常量
HttpClients::IE"MSIE"
HttpClients::FF"Firefox"
HttpClients::SAFARI"Safari"
HttpClients::OPERA"Opera"
HttpClients::CHROME"Chrome"

你可以在以下链接中找到更多常量以及相关信息:

  1. 常量定义:msf/core/constants.rb

  2. 支持的要求集合:browser_exploit_server.rb

这些资源中包含了更多有关常量和混合器支持的需求集的详细信息。

四、设置监听器

在检测阶段和需求检查之后,BrowserExploitServer混合器会触发on_request_exploit回调方法,这时你可以处理HTTP请求,构建HTML,并返回利用响应。以下是如何设置on_request_exploit的示例:

#
# 监听HTTP请求
# cli是套接字
# request是Rex::Proto::Http::Request对象
# target_info是包含所有浏览器信息(即浏览器配置文件)的哈希
#
def on_request_exploit(cli, request, target_info)
  print_status("这是我关于目标的了解:#{target_info.inspect}")
end

1、使用BrowserExploitServer构建HTML

BrowserExploitServer混合器支持两种编写方式:经典的HTML或ERB模板。第一种方式很简单,直接用HTML写即可:

def on_request_exploit(cli, request, target_info)
  html = %Q|
  <html>
    Hello, world!
  </html>
  |
  send_exploit_html(cli, html)
end

ERB是编写Metasploit浏览器漏洞利用的另一种方式。如果你曾经编写过一些Web应用程序,你会对这种方式不陌生。在使用BrowserExploitServer混合器编写漏洞利用时,实际上你是在编写一个Rails模板。以下是使用ERB模板的示例:

def on_request_exploit(cli, request, target_info)
  html = %Q|
  <html>
    你感觉幸运吗,伙计?<br>
    <% if [true, false].sample %>
      幸运!<br>
    <% else %>
      不幸,兄弟!<br>
    <% end %>
  </html>
  |
  send_exploit_html(cli, html)
end

如果你想访问局部变量或参数,确保将binding对象传递给send_exploit_html

def exploit_template1(target_info, txt)
  txt2 = "我可以使用局部变量!"

  template = %Q|
  <% msg = "这是一个由漏洞生成的页面" %>
  <%=msg%><br>
  <%=txt%><br>
  <%=txt2%><br>
  <p></p>
  数据来自来源:#{target_info[:source]}<br>
  操作系统名称:#{target_info[:os_name]}<br>
  用户代理名称:#{target_info[:ua_name]}<br>
  用户代理版本:#{target_info[:ua_ver]}<br>
  Java版本:#{target_info[:java]}<br>
  Office版本:#{target_info[:office]}
  |

  return template, binding()
end

def on_request_exploit(cli, request, target_info)
  send_exploit_html(cli, exploit_template(target_info, txt))
end

2、BrowserExploitServer混合器的其他功能

BrowserExploitServer混合器还提供了许多在构建漏洞利用时有用的功能。例如:你可以调用get_payload方法生成特定目标的有效载荷。它还为你提供了RopDb混合器,包含了一些ROP链,用于绕过DEP(数据执行保护)。确保查看API文档以获取更多信息。

为了开始,这里有一个代码示例,你可以用它来开始开发你的浏览器漏洞利用:

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

class MetasploitModule < Msf::Exploit::Remote
  Rank = NormalRanking

  include Msf::Exploit::Remote::BrowserExploitServer

  def initialize(info = {})
    super(
      update_info(
        info,
        'Name' => 'BrowserExploitServer Example',
        'Description' => %q{
          This is an example of building a browser exploit using the BrowserExploitServer mixin
        },
        'License' => MSF_LICENSE,
        'Author' => [ 'sinn3r' ],
        'References' => [
          [ 'URL', 'http://metasploit.com' ]
        ],
        'Platform' => 'win',
        'BrowserRequirements' => {
          source: /script|headers/i
        },
        'Targets' => [
          [ 'Automatic', {} ],
          [
            'Windows XP with IE 8',
            {
              'os_name' => 'Windows XP',
              'ua_name' => 'MSIE',
              'ua_ver' => '8.0'
            }
          ],
          [
            'Windows 7 with IE 9',
            {
              'os_name' => 'Windows 7',
              'ua_name' => 'MSIE',
              'ua_ver' => '9.0'
            }
          ]
        ],
        'Payload' => { 'BadChars' => "\x00" },
        'DisclosureDate' => '2013-04-01',
        'DefaultTarget' => 0
      )
    )
  end

  def exploit_template(target_info)
    template = %(
    Data source: <%=target_info[:source]%><br>
    OS name: <%=target_info[:os_name]%><br>
    UA name: <%=target_info[:ua_name]%><br>
    UA version: <%=target_info[:ua_ver]%><br>
    Java version: <%=target_info[:java]%><br>
    Office version: <%=target_info[:office]%>
    )

    return template, binding
  end

  def on_request_exploit(cli, _request, target_info)
    send_exploit_html(cli, exploit_template(target_info))
  end

end

JavaScript 混淆
BrowserExploitServer 依赖于 JSObfu mixin 来支持 JavaScript 混淆。当你编写 JavaScript 时,应该像这样编写:

js = js_obfuscate(your_code)

#js_obfuscate 会返回一个 Rex::Exploitation::JSObfu 对象。要获取混淆后的 JavaScript,调用 #to_s 方法:

js.to_s

如果你需要访问一个混淆后的符号名称,可以使用 #sym 方法:

# 获取 test() 函数的混淆版本
var_name = js.sym('test')

请注意,默认情况下,即使你的模块调用了 #js_obfuscate 方法,混淆也不会生效,除非用户设置了 JsObfuscate datastore 选项。这个选项是一个 OptInt 类型,它允许你设置混淆的次数(默认值为 0)。

如果你的基于 BES 的漏洞利用不需要混淆,确保调用 #deregister_options 并移除 JsObfuscate 选项,如下所示:

deregister_options('JsObfuscate')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人间酒中仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值