LoadLibraryを使わずにメモリからDLLをロードし、エクスポートを解決し、ホットスワップ可能なIPlugin ABIを介して呼び出しをディスパッチする、最小限のWindows x64リフレクティブPEマッパー。
Windows x64 向けのリフレクティブ PE マッパー、モジュールスタンピングエンジン、そしてクリーンな IPlugin ABI を中心に構築されたマルチプラグインマネージャーです。メモリ上のバイトバッファから DLL をマッピングし、エクスポートを解決し、プラグイン関数を呼び出します。呼び出し元は匿名メモリ(従来のリフレクティブロード)からでも、正規にロードされた身代わり DLL の内部(モジュールスタンピング)からでも可能です。同時実行プラグイン、ホットリロード、回避レイヤー統合用の WinApiTable 抽象化、そして 2 プロセスビーコンアーキテクチャを備えた統合スリープマスクモジュールのための PluginManager を含みます。
モジュールスタンピング経路は回避向けに堅牢化されています。セクションごとの保護(RWX なし)、プライベートマッピングのクリーンアップ、元のバイト列の復元、ヒープのスクラブを実施します。クリーンアップ後に pe-sieve、Moneta、hollows_hunter、BeaconEye、YARA でスキャンしても、スタンピングされたモジュールに対する検出結果はゼロです。
CMake 3.15 以上と、Windows 上の MSVC、Clang、または MinGW のいずれかが必要です。
# Configure (defaults to Release)
cmake -B build -G Ninja
# Build
cmake --build build
# The build produces:
# build/Modules/SleepMask/sleepmask.dll (sleep mask plugin)
# build/Testing/controller.exe (operator console)
# build/Testing/beacon.exe (headless beacon)
# build/Testing/harness.exe (plugin test harness)
デバッグビルドで完全な診断出力を有効にする場合:
cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug
cmake --build build
.\controller.exe ..\Modules\SleepMask\sleepmask.dll .\beacon.exe
スリープマスクDLLをロードするヘッドレスビーコンプロセスを起動します。ビーコンは自身のPEイメージを暗号化対象として自動的に指定します。mask>プロンプトで使用できるコマンド:
info — show technique and target
set ekko — switch to Ekko (timer-queue ROP chain)
set kraken — switch to KrakenMask (APC trampoline)
test <ms> — one-shot self-test (allocate, encrypt, sleep, decrypt, verify)
run <ms> — start callback loop (sleep encrypted → wake → poll → execute → repeat)
echo <msg> — round-trip test
exit — terminate beacon
コールバックループモード(run 10000)では、ビーコンがスリープしている間にコマンドを入力できます。コマンドはキューに蓄積され、次のウェイクサイクルで実行されます。暗号化されたスリープ期間中に、Process Hacker を使用してビーコンのメモリを検査してください。
.\harness.exe
実行時のプラグイン管理のためのインタラクティブメニュー:
[1] Load plugin — pick ID, DLL path, engine (reflective/stomped)
[2] Unload plugin — by ID
[3] Execute plugin — by ID, with a command string
[4] Reload plugin — hot-swap a new DLL into an existing slot
[5] List plugins — shows count, names, versions, engine type
[6] Execute all — runs every loaded plugin with one command
[7] Scan commands — prints pe-sieve/moneta commands for this PID
[q] Quit
# Load multiple plugins and execute them
.\harness.exe --load cmd cmdplugin.dll --load echo echoplugin.dll --exec cmd whoami --exec echo "hello" --list
# Module stomping
.\harness.exe --load cmd cmdplugin.dll --stomp xpsservices.dll --exec cmd whoami
# Hot-reload test (counter resets after reload)
.\harness.exe --load counter counterplugin.dll --exec counter a --exec counter b --reload counter counterplugin.dll --exec counter "after-reload" --list
# Unload
.\harness.exe --load cmd cmdplugin.dll --exec cmd whoami --unload cmd --list --unload-all
1. MapImage(plugin) private reflective mapping
2. LoadLibrary(sacrificial) legitimate load, PEB entry
3. Save original .text bytes for later restoration
4. Copy plugin image -> .text contiguous image preserves RIP-relative offsets
5. Relocate stomped code fix addresses for new base
6. Patch entry point mov eax,1; ret (safe FreeLibrary)
7. Per-section protections RX / R / RW per section (never RWX)
8. Resolve exports from private mapping, return stomped addresses
9. Free private mapping eliminates floating PE artifact
10. Execute plugin code runs inside "sacrificial.dll"
11. Restore original .text bytes sacrificial code back in place
12. FreeLibrary(sacrificial) unloads the module
13. LoadLibrary(sacrificial) fresh load = clean file-backed pages
14. Scrub heap buffers zero saved bytes + raw DLL data
クリーンアップ後、犠牲DLLは通常ロードされたモジュールと区別がつかない。ページはファイルバッキング(プライベートではない)であり、内容はディスク上のファイルと一致し、ヒープ上にPEアーティファクトは残らない。
| IOC | 緩和策 |
|---|---|
| RWXメモリ領域 | セクションごとの保護: .text RX、.rdata R、.data RW |
| プライベート実行可能メモリ(フローティングPE) | エクスポート解決後にプライベートマッピングをVirtualFree |
| 変更されたモジュールコード | 元の.textバイトを復元し、クリーンなファイルバッキングページのためにFreeLibrary + LoadLibraryを実行 |
| ヒープ上のPEヘッダー | 解放前に生のDLLバッファと保存バイトをmemset(0) |
| アンロード時のDllMainクラッシュ | スタンピング前にエントリポイントをmov eax,1; retにパッチ |
ReflectivePluginLoader/
├── CMakeLists.txt
├── Include/
│ ├── IPlugin.h Plugin ABI (TaskApi, IPlugin)
│ ├── Win32.h WinApiTable abstraction
│ ├── ReflectiveLoaderEngine.h PE mapper + export resolver + FreeImage
│ ├── StompedLoaderEngine.h Module stomping engine
│ └── PluginManager.h Multi-plugin lifecycle manager
├── Modules/
│ ├── CmdPlugin/ Command execution plugin
│ ├── EchoPlugin/ Echo plugin (multi-plugin demo)
│ ├── CounterPlugin/ Stateful counter (persistence demo)
│ └── SleepMask/
│ ├── sleepmask.cpp Unified sleep mask (Ekko + KrakenMask)
│ └── CMakeLists.txt
├── Examples/
│ ├── basic_load.cpp Minimal single-plugin load + execute
│ ├── multi_plugin.cpp Multiple plugins with lifecycle
│ ├── hot_reload.cpp Hot-reload with CounterPlugin
│ ├── custom_api_table.cpp Custom WinApiTable with logging wrappers
│ ├── module_stomping.cpp Load via module stomping engine
│ └── Makefile NMAKE build for standalone examples
├── Testing/
│ ├── main.cpp Interactive + CLI plugin harness
│ ├── controller.cpp Operator console (spawns beacon, pipe IPC)
│ ├── beacon.cpp Headless beacon (loads sleep mask, callback loop)
│ └── CMakeLists.txt
└── Tools/
└── file2hex.py DLL to C byte array converter
Modules/ の下にディレクトリを作成し、IPlugin.h をインクルードします。IPlugin インターフェースを実装します:#include "IPlugin.h"
class MyPlugin : public IPlugin {
public:
void init() const override { /* setup */ }
void execute(TaskApi* task) const override { /* work */ }
void cleanup() const override { /* tear */ }
};
5つのエントリポイント(create_plugin、destroy_plugin、plugin_init、plugin_exec、plugin_cleanup)をエクスポートします。CRTセーフな割り当てには HeapAlloc + プレースメント new を使用します。
オプションでメタデータ用に plugin_info をエクスポートします:
struct PluginInfo { const char* name; const char* version; const char* description; };
extern "C" __declspec(dllexport) PluginInfo __stdcall plugin_info() {
return { "myplugin", "2.0.3", "My plugin description" };
}
CMakeLists.txt を追加し、ルートで add_subdirectory() を使って登録します。犠牲 DLL の .text セクションは、プラグインの仮想イメージサイズより大きい必要があります。また、プロセスによって活発に使用されておらず、実行時に整合性チェックされないものであるべきです。
適した候補: xpsservices.dll (1.8 MB .text)、mscms.dll、colorui.dll、npmproxy.dll
不適な候補: ntdll.dll、kernel32.dll (活発に使用)、clr.dll (整合性チェックあり)
.text セクションに収まる必要がありますimage