用Zig编写的反射式PE加载器。直接从内存加载并执行原生和.NET PE文件。
__security_cookie)SetProcessValidCallTargets注册有效调用目标用于从已加载的PE映像中解析导出表的实用函数:
getExportByName(base, ntheaders, name) — 按名称解析getExportByOrdinal(base, ntheaders, ordinal) — 按序号解析通过加载目标DLL自动处理转发导出(例如,"NTDLL.RtlAllocateHeap"解析为ntdll.dll中的实际函数)。
使用PEB ApiSetMap自动将Windows API集(api-ms-win-*、ext-ms-win-*)解析到其实际的主机DLL。这使得能够加载从虚拟API集DLL导入的PE。
utils.detect_platform(buffer) — 检测PE是32位还是64位utils.is_dotnet_assembly(ntheaders) — 检查PE是否为.NET程序集utils.getDotNetVersion(buffer) — 从元数据中提取.NET运行时版本字符串utils.rvaToFileOffset(buffer, rva) — 将RVA转换为原始文件偏移克隆仓库:
git clone https://github.com/Thoxy67/zig-pe.git
cd zig-pe
构建项目:
zig build
zig build -Ddotnet=false # 禁用.NET支持(默认启用)
zig build # 构建所有目标
zig build run-putty64 # 运行64位原生PE示例
zig build run-putty32 # 运行32位原生PE示例
zig build run-dotnet # 运行.NET程序集示例
zig build test # 运行单元测试
const pe = @import("pe");
pub fn main() !void {
// Load and execute an embedded PE file
try pe.RunPE.init(@embedFile("bin/app.exe")).run();
}
const pe = @import("pe");
pub fn main() !void {
var loader = pe.RunPE.init(@embedFile("bin/app.exe"));
try loader.runWithArgs(&.{ "arg1", "arg2", "arg3" });
}
const std = @import("std");
const pe = @import("pe");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const file_content = try std.fs.cwd().readFileAlloc(
allocator,
"path/to/executable.exe",
std.math.maxInt(usize),
);
defer allocator.free(file_content);
try pe.RunPE.init(file_content).run();
}
const pe = @import("pe");
// 将DLL加载到内存后...
const base: [*]const u8 = @ptrCast(loaded_base);
const ntheaders = // ... 获取NT头
// 按名称解析
if (pe.getExportByName(base, ntheaders, "ExportedFunction")) |func_ptr| {
const func: *const fn () void = @ptrCast(@alignCast(func_ptr));
func();
}
// 按序号解析
if (pe.getExportByOrdinal(base, ntheaders, 42)) |func_ptr| {
// 使用函数指针
}
GetCommandLineW()返回提供的参数Main(string[] args)该项目涉及加载和执行任意代码,具有潜在危险性。请仅在受信任的PE文件和受控环境中使用此加载器。作者对因使用本软件造成的任何误用或损坏不承担责任。
欢迎为zig-pe做出贡献!请随时提交拉取请求、创建问题或传播此项目。
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)本项目采用MIT许可证授权 - 详见LICENSE文件。
本项目仅供教育目的。在加载和执行任何PE文件前,请确保你拥有必要的权利和权限。
| 架构 | 机器代码 | 位数 |
|---|
| i386 | 0x014c | 32-bit |
| AMD64 | 0x8664 | 64-bit |
| IA64 | 0x0200 | 64-bit |
| ARM (ARMNT) | 0x01C4 | 32-bit |
| ARM64 | 0xAA64 | 64-bit |