用于加载和操作 PE 文件的库。
libPEConv 的目标是创建一个用于自定义加载 PE 文件的“瑞士军刀”。它汇集了各种辅助函数,您可以快速将它们集成到自己的加载器中。例如:重新映射节区、应用重定位、加载导入、解析资源。
它不仅允许加载 PE 文件,还允许自定义某些步骤,例如 IAT 挂钩(通过提供自定义 IAT 解析器)和函数重定向。但是,它并不专注于内联挂钩,不应与 MS Detours 或 MinHook 等库混淆。
LibPeConv 可用于创建 PE 绑定器,因为它允许直接从资源中加载 PE,并将其当作本地代码一样集成。
此外,它还可以帮助您从内存中转储 PE,并重建其 IAT。
警告:使用 MUI 的应用程序不受支持。
最简单的用例:使用 libPeConv 手动加载并运行您选择的 EXE。
#include <Windows.h>
#include <iostream>
#include <peconv.h> // include libPeConv header
int main(int argc, char *argv[])
{
if (argc < 2) {
std::cout << "Args: <path to the exe>" << std::endl;
return 0;
}
LPCSTR pe_path = argv[1];
// manually load the PE file using libPeConv:
size_t v_size = 0;
#ifdef LOAD_FROM_PATH
//if the PE is dropped on the disk, you can load it from the file:
BYTE* my_pe = peconv::load_pe_executable(pe_path, v_size);
#else
size_t bufsize = 0;
BYTE *buffer = peconv::load_file(pe_path, bufsize);
// if the file is NOT dropped on the disk, you can load it directly from a memory buffer:
BYTE* my_pe = peconv::load_pe_executable(buffer, bufsize, v_size);
#endif
if (!my_pe) {
return -1;
}
// if the loaded PE needs to access resources, you may need to connect it to the PEB:
peconv::set_main_module_in_peb((HMODULE)my_pe);
// load delayed imports (if present):
const ULONGLONG load_base = (ULONGLONG)my_pe;
peconv::load_delayed_imports(my_pe, load_base);
// if needed, you can run TLS callbacks before the Entry Point:
peconv::run_tls_callbacks(my_pe, v_size);
//calculate the Entry Point of the manually loaded module
DWORD ep_rva = peconv::get_entry_point_rva(my_pe);
if (!ep_rva) {
return -2;
}
ULONG_PTR ep_va = ep_rva + (ULONG_PTR) my_pe;
//assuming that the payload is an EXE file (not DLL) this will be the simplest prototype of the main:
int (*new_main)() = (int(*)())ep_va;
//call the Entry Point of the manually loaded PE:
return new_main();
}
另请参阅:https://github.com/hasherezade/libpeconv_tpl/blob/master/project_template/main.cpp