Skip to content

Commit f081454

Browse files
Fea, 提供自定义DLL加载能力(__pfnYY_Thunks_CustomLoadLibrary)
1 parent 62d0e85 commit f081454

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

src/Build.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ goto:eof
140140

141141
:: FixObj "XXX\YY_Thunks_for_Vista.obj" 1.def+2.def
142142
:FixObj
143-
LibMaker.exe FixObj %1 /WeakExternFix:__security_cookie=%PointType% /WeakExternFix:__acrt_atexit_table=%PointType% /WeakExternFix:__pfnDllMainCRTStartupForYY_Thunks=%PointType% /WeakExternFix:__YY_Thunks_Disable_Rreload_Dlls=4
143+
LibMaker.exe FixObj %1 /WeakExternFix:__security_cookie=%PointType% /WeakExternFix:__acrt_atexit_table=%PointType% /WeakExternFix:__pfnDllMainCRTStartupForYY_Thunks=%PointType% /WeakExternFix:__YY_Thunks_Disable_Rreload_Dlls=4 /WeakExternFix:__pfnYY_Thunks_CustomLoadLibrary=%PointType%
144144
if %ErrorLevel% NEQ 0 exit /b %ErrorLevel%
145145
if "%2"=="" goto:eof
146146
set DEF_FILES=%2

src/Thunks/YY_Thunks.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,19 @@ static HMODULE __fastcall try_get_module(volatile HMODULE* pModule, const wchar_
11591159
// this fails, cache the sentinel handle value INVALID_HANDLE_VALUE so that
11601160
// we don't attempt to load the module again:
11611161
HMODULE new_handle = NULL;
1162-
if (Flags & USING_GET_MODULE_HANDLE)
1162+
1163+
if (__pfnYY_Thunks_CustomLoadLibrary)
1164+
{
1165+
new_handle = __pfnYY_Thunks_CustomLoadLibrary(module_name, Flags);
1166+
}
1167+
1168+
if (new_handle)
1169+
{
1170+
// 使用 CustomLoadLibrary的结果
1171+
if (new_handle == INVALID_HANDLE_VALUE)
1172+
new_handle = nullptr;
1173+
}
1174+
else if (Flags & USING_GET_MODULE_HANDLE)
11631175
{
11641176
new_handle = GetModuleHandleW(module_name);
11651177
}

src/Thunks/YY_Thunks.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ EXTERN_C const BOOL __YY_Thunks_Disable_Rreload_Dlls = TRUE;
115115
*/
116116
EXTERN_C extern BOOL __YY_Thunks_Disable_Rreload_Dlls /* = FALSE*/;
117117

118+
119+
// 直接通过GetModuleHandleW获取,改选项非常危险,如果dll尚未加载会将不会加载!!!
120+
#define USING_GET_MODULE_HANDLE 0x00000001
121+
// 以 LOAD_LIBRARY_AS_DATAFILE 标记作为资源加载。
122+
#define LOAD_AS_DATA_FILE 0x00000002
123+
// 直接使用LoadLibrary,该加载模式存在劫持风险,使用前请确认该DLL处于KnownDll。
124+
#define USING_UNSAFE_LOAD 0x00000004
125+
/// <summary>
126+
/// 如果对YY-Thunks的内置的LoadLibrary加载方式不满意,则通过设置__pfnYY_Thunks_CustomLoadLibrary以实现自定义DLL加载。
127+
/// </summary>
128+
/// <param name="_szModuleName">需要加载的模块名称,比如`ntdll.dll`。</param>
129+
/// <param name="_fFlags">请参考 USING_GET_MODULE_HANDLE 等宏。</param>
130+
/// <returns>
131+
/// 返回 nullptr:继续执行YY_Thunk默认DLL加载流程。
132+
/// 返回 -1 :加载失败,并阻止执行YY_Thunks默认加载流程。
133+
/// 其他:CustomLoadLibrary加载成功,必须返回有效的 HMODULE。
134+
/// </returns>
135+
EXTERN_C extern HMODULE (__fastcall * const __pfnYY_Thunks_CustomLoadLibrary)(const wchar_t* _szModuleName, DWORD _fFlags);
136+
118137
// 从DllMain缓存RtlDllShutdownInProgress状态,规避退出时调用RtlDllShutdownInProgress。
119138
// 0:缓存无效
120139
// 1:模块正常卸载
@@ -315,12 +334,6 @@ static __forceinline T* __fastcall __crt_interlocked_read_pointer(T* const volat
315334
return __crt_interlocked_compare_exchange_pointer(target, nullptr, nullptr);
316335
}
317336

318-
// 改选项非常危险,只调用GetModuleHandleW!!!
319-
#define USING_GET_MODULE_HANDLE 0x00000001
320-
#define LOAD_AS_DATA_FILE 0x00000002
321-
// 该加载模式存在劫持风险,使用前请确认。
322-
#define USING_UNSAFE_LOAD 0x00000004
323-
324337
static HMODULE __fastcall try_get_module(volatile HMODULE* pModule, const wchar_t* module_name, int Flags) noexcept;
325338

326339
#define _APPLY(_MODULE, _NAME, _FLAGS) \

src/YY-Thunks.UnitTest/weak.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ const void* __acrt_atexit_table;
44
const void* __pfnDllMainCRTStartupForYY_Thunks;
55

66
const void* __YY_Thunks_Disable_Rreload_Dlls;
7+
8+
const void* __pfnYY_Thunks_CustomLoadLibrary;

0 commit comments

Comments
 (0)