0

我正在尝试使用 MS Detours 挂钩 win32 API 函数“CreateFile”,但是当我通过使用 MS Word 打开 *.doc 文件对其进行测试时,MS Word 加载的 DLL 和字体文件和目录的 CreateFile 调用被重定向到我的 detour函数,但不适用于该 *.doc 文件,但是当我使用记事本打开 *.txt 文件时,该 *.txt 文件的 CreateFile 调用出现在我的迂回函数中。

我正在使用以下代码来挂钩 CreateFile:

static HANDLE (WINAPI *Real_CreateFile)(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) = CreateFile;

HANDLE WINAPI Routed_CreateFile(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
{
OutputDebugString(lpFileName);
return Real_CreateFile(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
}

BOOL APIENTRY DllMain( HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved )
{
LONG Error;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:

    OutputDebugString(L"Attaching MyDLL.dll");
    OutputDebugString(strInfo);
    DetourRestoreAfterWith();
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourAttach(&(PVOID&)Real_CreateFile, Routed_CreateFile);
    Error = DetourTransactionCommit();

    if (Error == NO_ERROR)
        OutputDebugString(L"Hooked Success");
    else
        OutputDebugString(L"Hook Error");

    break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
    OutputDebugString(L"De-Attaching MyDLL.dll");
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourDetach(&(PVOID&)Real_CreateFile, Routed_CreateFile);
    Error = DetourTransactionCommit();

    if (Error == NO_ERROR)
        OutputDebugString(L"Un-Hooked Success");
    else
        OutputDebugString(L"Un-Hook Error");

    break;
}
return TRUE;
}

提前致谢。

4

2 回答 2

3

我认为您break在此之后缺少一个:

case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
    break;  // Not interested in thread messages
case DLL_PROCESS_DETACH:

你只是在它被调用之前分离弯路吗?也许打开 a.doc创建一个新线程但 a.txt没有,触发此代码路径。

于 2013-01-09T13:47:35.413 回答
1

看起来您没有正确初始化 Real_CreateFile 函数指针。我猜您正在将其设置为模块的 CreateFile 导入表条目。

相反,将其初始化为GetProcAddress(GetModuleHandle("kernel32"),"CreateFileW");

于 2013-01-10T16:06:45.447 回答