
NativeMirror 是 PE-sieve 功能的独立 Object Pascal 实现,改编自 hasherezade 的 PE-sieve。它扫描进程内存,检测被修改或注入的代码,并转储 PE 文件。
该实现使用 Delphi 原生 API,并以 NativePe 作为其 PE 引擎。它可以直接作为 Delphi 库使用,也可以通过其 CLI 使用,或通过兼容 PE-sieve 的 DLL 接口使用。三者共享同一个扫描器核心。兼容性目标是 PE-sieve 0.4.1.5 的外部可观察行为;尚未确立完全的行为等价性。
../NativePe,或调整 Delphi 搜索路径。NativeMirrorScan.NativeAsm.dproj 项目启用 USE_NATIVEASM。没有它时,NativeMirror 使用 Delphi/LDE 回退方案。将 NativeASM 搜索路径调整到你的安装位置。py 启动器:DLL 构建后步骤和 Python 测试所需。脚本使用 Python 标准库。某些项目配置包含本地依赖路径。在其他机器上构建前请检查搜索路径。
在 Delphi 中打开 NativeMirror.groupproj,并选择所需的项目、平台和配置。
| 项目 | 输出 |
|---|---|
tools/NativeMirrorScan/NativeMirrorScan.dproj | NativeMirrorScan.exe |
tools/NativeMirrorDll/pe-sieve.dproj | pe-sieve.dll |
tests/ScannerIntegration/NativeMirrorTestTarget.dproj | Win64 测试目标 |
tests/ScannerIntegration/NativeMirrorTestTarget32.dproj | Win32 测试目标 |
tests/ScannerIntegration/NativeMirrorScannerTests.dproj | Win64 扫描器测试运行器 |
tests/HollowsHunterIntegration/NativeMirrorHollowsHunterTests.dproj | Win64 HollowsHunter 测试运行器 |
根据需要为 Win32 和 Win64 构建 CLI 和 DLL。在运行集成测试之前构建两个测试目标。为 Win64 构建测试运行器。
输出到 bin/<Platform>/<Config>。以下示例使用 Debug。编译器单元输出到 dcu/。将可执行文件保持在 tools/ 之外。
从项目根目录运行这些命令。将 1234 替换为要扫描的进程 ID。
.\bin\Win64\Debug\NativeMirrorScan.exe /help
.\bin\Win64\Debug\NativeMirrorScan.exe /pid 1234
.\bin\Win64\Debug\NativeMirrorScan.exe /pid 1234 /json /quiet /ofilter 2
.\bin\Win64\Debug\NativeMirrorScan.exe /pid 1234 /dir output
/ofilter 2 禁用转储输出。/dir output 选择报告和转储的输出目录。需要时使用 bin/Win32/Debug 下的 Win32 可执行文件。对目标进程的访问取决于你的 Windows 权限。
将 src/ 及其单元子目录,连同 NativePe 一起添加到项目的搜索路径中。使用 TNativeMirrorOptions 调用 ScanAndDump。该库会编译进你的应用程序;此用法既不需要 NativeMirrorScan.exe,也不需要 pe-sieve.dll。Python 仅用于单独的 DLL 构建和 Python 测试。
此控制台示例扫描 PID 1234 并将其报告以 JSON 格式打印:
program ScanExample;
{$APPTYPE CONSOLE}
uses
System.JSON,
NativeMirror.Core.Types,
NativeMirror.Core.Options,
NativeMirror.Core.Engine;
var
Options: TNativeMirrorOptions;
Report: TNativeMirrorReportEx;
Json: TJSONObject;
begin
Options := TNativeMirrorOptions.Default;
Options.Pid := 1234;
Options.Quiet := True;
Options.OutputFilter := nmOutNoDir;
Report := ScanAndDump(Options);
try
Json := Report.ToJson(Options.ResultsFilter, Options.JsonLevel);
try
Writeln(Json.ToJSON);
finally
Json.Free;
end;
finally
Report.Free;
end;
end.
将 PID 替换为你的目标进程。nmOutNoDir 会禁用报告文件和转储。检查 Report.ErrorReport 以获取扫描错误,检查 Report.ScanReport 以获取扫描结果。调用方拥有返回的报告,并且必须释放它。
从 PE-sieve releases 下载 pe-sieve32.exe 和 pe-sieve64.exe。现有的比较脚本使用 v0.4.1.1 可执行文件。将两者放入 bin/Win64/Debug;Win64 测试脚本会在那里查找它们的参考扫描器。32 可执行文件是原始的 Win32 参考版本,尽管它与 Win64 测试工具存放在一起。
Debug 配置如下所示:
bin/
Win64/Debug/
NativeMirrorScan.exe
pe-sieve.dll
pe-sieve.dll.exports.json
pe-sieve32.exe
pe-sieve64.exe
NativeMirrorTestTarget.exe
NativeMirrorScannerTests.exe
NativeMirrorHollowsHunterTests.exe
hollows_hunter.exe
Win32/Debug/
NativeMirrorScan.exe
pe-sieve.dll
pe-sieve.dll.exports.json
NativeMirrorTestTarget32.exe
NativeMirror 生成 CLI、DLL、导出记录、目标以及测试运行器。PE-sieve 和 HollowsHunter 可执行文件需单独下载。HollowsHunter 仅在其集成测试或手动使用 DLL 时才需要。
从 HollowsHunter releases 下载 hollows_hunter64_dll.zip, v0.4.1.1。请使用 DLL 包。在主机架构和 API 契约匹配的情况下,NativeMirror 最终生成的 pe-sieve.dll 会替换 HollowsHunter 所使用的原始 PE-sieve DLL。独立的静态可执行文件无法使用替换后的 DLL。所提供的集成测试覆盖 v0.4.1.1 Win64 DLL 宿主。
如需手动运行,请将包中的 hollows_hunter.exe 复制到 bin/Win64/Debug,与 NativeMirror 构建的 pe-sieve.dll 放在一起。不要用包中的原始 DLL 覆盖 NativeMirror DLL。宿主和 DLL 都必须是 Win64。
.\bin\Win64\Debug\hollows_hunter.exe /pid 1234 /quiet /hooks /ofilter 2 /json /report 7
对于自动化比较,请从同一软件包中将未更改的原始 EXE 和 DLL 提取到:
tests/HollowsHunterIntegration/fixtures/Win64/pe-sieve-C/
hollows_hunter.exe
pe-sieve.dll
manifest.json
保留现有的 manifest.json。它记录了预期的版本和 SHA-256 哈希值。运行器会检查这些文件,将同一主机复制到独立的 reference 和 NativeMirror 目录中,并针对受控的测试进程测试每个 DLL。它不会下载文件,也不会覆盖 bin 中的构建。
PE-sieve 将 PESieve_version 导出为 32 位数据值。对于版本 0.4.1.5,该值为 0x00040105。调用方期望导出地址指向这四个字节,而不是指向一个返回该数字的函数。
本项目使用的 Delphi 12 编译器/链接器不会通过 exports 子句直接导出该变量。因此,DLL 中包含标记的版本值,并最初以所需名称导出一个辅助函数。
链接后,项目的生成后事件会运行:
py tools/NativeMirrorDll/finalize_exports.py bin/Win64/Debug/pe-sieve.dll
脚本将 PESieve_version 导出条目指向已存储的编号,并更新 PE 校验和。它保持扫描器代码和其他导出目标不变。此步骤在构建 pe-sieve.dproj 时自动运行;脚本失败会导致构建失败。
该脚本还会创建 pe-sieve.dll.before-version-export 作为备份,以及 pe-sieve.dll.exports.json,其中包含最终确定的导出信息和 DLL 哈希。请保留该 JSON 文件供测试运行器使用。普通的 DLL 客户端只需要最终的 DLL。构建/测试机器上需要安装 Python,使用最终 DLL 时则不需要。
在构建所需项目并安装上述参考文件后,从项目根目录运行命令。
.\bin\Win64\Debug\NativeMirrorScannerTests.exe
.\bin\Win64\Debug\NativeMirrorHollowsHunterTests.exe
py tests/CLIOutput/test_source_contract.py
py tests/CLIOutput/test_cli_arguments.py
py tests/ParityAudit/run_module_cases.py
扫描器运行器会在自身旁边的 bin/Win64/<Config> 中找到 CLI 和 pe-sieve64.exe。Python 比较脚本默认使用 bin/Win64/Debug。在支持的地方可以使用显式覆盖:
$env:NATIVEMIRROR_SCAN_EXE = "$PWD\bin\Win64\Debug\NativeMirrorScan.exe"
$env:PESIEVE_EXE = "$PWD\bin\Win64\Debug\pe-sieve64.exe"
$env:NATIVEMIRROR_TARGET32_EXE = "$PWD\bin\Win32\Debug\NativeMirrorTestTarget32.exe"
源码契约测试需要固定版本的 PE-sieve 0.4.1.5 源码树,位于 pe-sieve-master/pe-sieve-master。C++ DLL 测试使用其来自 include/ 的原始头文件。这些参考源码未包含在已发布的项目中。
要构建并运行 C++ DLL 契约测试:
cmake -S tests/PeSieveDll -B parity-work/dll-contract-build-x64 -G "Visual Studio 17 2022" -A x64
cmake --build parity-work/dll-contract-build-x64 --config Debug
cmake -S tests/PeSieveDll -B parity-work/dll-contract-build-x86 -G "Visual Studio 17 2022" -A Win32
cmake --build parity-work/dll-contract-build-x86 --config Debug
py -m unittest discover -s tests/PeSieveDll -p test_version_export.py -v
py tests/PeSieveDll/run_contract.py --arch both
最后一条命令需要已定稿的 Win32 和 Win64 NativeMirror DLL、两个 CLI 构建以及两个测试目标。测试产物写入 parity-work/ 下;扫描器诊断信息写入扫描器测试运行器旁边。
下载的参考二进制文件为 0.4.1.1。与它们的比较仅覆盖这些测试用例,并不代表与 0.4.1.5 源码参考完全兼容。
| 目录 | 内容 |
|---|---|
src/ | 库核心、扫描器、转储器、报告、CLI 解析和 DLL API |
tools/NativeMirrorScan/ | CLI 项目文件 |
tools/NativeMirrorDll/ | DLL 项目和 Python 导出定稿器 |
tests/ | Delphi 集成测试、Python 比较和 C++ DLL 调用程序 |
bin/ | 本地可执行文件和 DLL |
dcu/ | Delphi 编译器输出 |
parity-work/ | 本地测试运行、契约测试构建和比较数据 |
licenses/ | 第三方许可证文本 |
NativeMirror 是 hasherezade 的 PE-sieve 的 Object Pascal 改编版本,以 0.4.1.5 作为其源码参考。原始扫描器、其算法及其设计的功劳归 hasherezade 和 PE-sieve 贡献者所有。原始作品仍归他们所有。
Copyright (c) 2026, Selahattin Erkoc: NativeMirror 改编。 Copyright (c) 2017-2026, @hasherezade: 原始 PE-sieve 作品。
NativeMirror 使用 BSD 2-Clause License,与 PE-sieve 相同。参见 LICENSE、保留的 PE-sieve 许可证 和 THIRD-PARTY-NOTICES.md。单独的依赖项保留各自的许可证。