forked from rapid7/metasploit-framework
-
Notifications
You must be signed in to change notification settings - Fork 3
怎么在模块中使用 HttpServer 和 HttpClient
L edited this page May 19, 2022
·
2 revisions
在 Metasploit 模块中使用多个网络 mixin 总是一件棘手的事情,因为您很可能会遇到重叠数据存储选项、变量、方法等问题,super 调用仅适用于一个 mixin 等。这被认为是高级的模块开发,有时自己弄清楚可能会很痛苦。为了改善 Metasploit 的开发体验,我们提供了一些示例来演示需要您使用多个 mixin 来实现利用的常见场景。
假设您想利用 Web 服务器或 Web 应用程序。您可以在盒子上执行代码,但您需要找到一种方法来交付最终的有效负载(可能是可执行文件),而 HTTP 服务器恰好是您的选择。
以下是您可以设置的方法:
##
# This module requires Metasploit: http://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
require 'msf/core'
class MetasploitModule < Msf::Exploit::Remote
Rank = NormalRanking
include Msf::Exploit::Remote::HttpClient
include Msf::Exploit::Remote::HttpServer::HTML
def initialize(info={})
super(update_info(info,
'Name' => "HttpClient and HttpServer Example",
'Description' => %q{
This demonstrates how to use two mixins (HttpClient and HttpServer) at the same time,
but this allows the HttpServer to terminate after a delay.
},
'License' => MSF_LICENSE,
'Author' => [ 'sinn3r' ],
'References' =>
[
['URL', 'http://metasploit.com']
],
'Payload' => { 'BadChars' => "\x00" },
'Platform' => 'win',
'Targets' =>
[
[ 'Automatic', {} ],
],
'Privileged' => false,
'DisclosureDate' => "Dec 09 2013",
'DefaultTarget' => 0))
register_options(
[
OptString.new('TARGETURI', [true, 'The path to some web application', '/']),
OptInt.new('HTTPDELAY', [false, 'Number of seconds the web server will wait before termination', 10])
], self.class)
end
def on_request_uri(cli, req)
print_status("#{peer} - Payload request received: #{req.uri}")
send_response(cli, 'You get this, I own you')
end
def primer
print_status("Sending a malicious request to #{target_uri.path}")
send_request_cgi({'uri'=>normalize_uri(target_uri.path)})
end
def exploit
begin
Timeout.timeout(datastore['HTTPDELAY']) { super }
rescue Timeout::Error
# 当服务器由于我们的超时而停止时,会引发这种情况
end
end
end以下是运行上述示例时发生的情况:
- 包裹在 Timeout 块中的 super 调用将启动 Web 服务器。
- 在 Web 服务器处于无限循环状态之前,将调用 prime() 方法,这是您发送恶意请求以获取代码执行的地方。
- 您的 HttpServer 根据请求提供最终的有效负载。
- 10 秒后,模块引发超时异常。 Web 服务器最终终止。
如果您想知道为什么 web 服务器必须在一段时间后终止,这是因为如果模块无法在目标机器上获得代码执行,显然它永远不会向您的 web 服务器询问恶意负载,因此有没有必要让它永远活着。通常,获取有效负载请求也不会花费很长时间,因此我们将超时保持在较短的时间。
上述示例的输出应如下所示:
msf exploit(test) > run
[*] Exploit running as background job.
[*] Started reverse handler on 10.0.1.76:4444
[*] Using URL: http://0.0.0.0:8080/SUuv1qjZbCibL80
[*] Local IP: http://10.0.1.76:8080/SUuv1qjZbCibL80
[*] Server started.
[*] Sending a malicious request to /
msf exploit(test) >
[*] 10.0.1.76 test - 10.0.1.76:8181 - Payload request received: /SUuv1qjZbCibL80
[*] Server stopped.
msf exploit(test) >