为什么在尝试绕过 winapi 时进程会崩溃?

Why is the process crashing when attempting to detour a winapi?

我正在尝试用MS走弯路,不知道是不是我做错了;我似乎找不到问题的答案。

我曾尝试使用注入的 DLL 绕过进程中的多个函数,但每次尝试都会导致进程崩溃。

我尝试挂钩的函数之一是 winapi DirectDrawCreate:

DetourTransactionBegin();
DetourUpdateThread( GetCurrentThread() );
DetourAttach( (PVOID *)DirectDrawCreate, hkDirectDrawCreate );
DetourTransactionCommit();

hkDirectDrawCreate 定义为:

HRESULT __stdcall hkDirectDrawCreate( GUID *p1, LPDIRECTDRAW *p2, IUnknown *p3 )
{
    if( !pDDC )
        return 0x00;

    printf( "A call to hkDirectDrawCreate was made\n" );

    return DirectDrawCreate( p1, p2, p3 );
}

在调用 DetourAttach 时进程崩溃;堆栈跟踪是:

myProj.dll!detour_skip_jmp(unsigned char * pbCode, void * * ppGlobals) Line 135 C++
myProj.dll!DetourCodeFromPointer(void * pPointer, void * * ppGlobals) Line 984  C++
myProj.dll!DetourAttachEx(void * * ppPointer, void * pDetour, _DETOUR_TRAMPOLINE * * ppRealTrampoline, void * * ppRealTarget, void * * ppRealDetour) Line 1456  C++
myProj.dll!DetourAttach(void * * ppPointer, void * pDetour) Line 1395   C++

代码在 'detour_skip_jmp' 处的“0x68B028BD”处中断:

// First, skip over the import vector if there is one.
    if (pbCode[0] == 0xff && pbCode[1] == 0x25) {   // jmp [imm32]
68B028B2  mov         ecx,1  
68B028B7  imul        edx,ecx,0  
68B028BA  mov         eax,dword ptr [pbCode]  
68B028BD  movzx       ecx,byte ptr [eax+edx]  
68B028C1  cmp         ecx,0FFh  
68B028C7  jne         detour_skip_jmp+82h (68B02912h)  
68B028C9  mov         edx,1  
68B028CE  shl         edx,0  
68B028D1  mov         eax,dword ptr [pbCode]  
68B028D4  movzx       ecx,byte ptr [eax+edx]  
68B028D8  cmp         ecx,25h  
68B028DB  jne         detour_skip_jmp+82h (68B02912h) 

编辑:ppGlobals 为 NULL,pbCode 给出错误 'Error reading characters of string'

回到 DetourCodeFromPointer ppGlobals 那里也是 NULL,但我想它应该是;这是电话:

 pDetour = DetourCodeFromPointer(pDetour, NULL);

毫无疑问,导入 table 已作为反挂机技术被移动或清除。直接在hkDirectDrawCreate的DirectDrawCreate开始添加一个跳转,然后在调用原来的跳转回到DirectDrawCreate时,但是一定要在你的跳转之后跳转到你的hook,否则你会陷入无限的递归循环。