J. *_*Doe 2 windows debugging hook code-injection
我将我的 DLL 注入到一个进程中,然后挂钩这样的函数:(recv)
BOOL HookFunction(LPCWSTR moduleName, LPCSTR funcName, LPVOID funcProxy,
unsigned char* lpBackup)
{
BYTE jmp[6] = { 0xe9,0x00,0x00,0x00,0x00,0xc3 };
DWORD funcAddr = (DWORD)GetProcAddress(GetModuleHandle(moduleName), funcName);
DWORD prev;
VirtualProtect((LPVOID)funcAddr, 6, PAGE_EXECUTE_READWRITE, &prev);
ReadProcessMemory(GetCurrentProcess(), (LPVOID)funcAddr, lpBackup, 6, NULL);
DWORD proxy = ((DWORD)funcProxy - funcAddr) - 5;
memcpy(&jmp[1], &proxy, 4);
memcpy((LPVOID)funcAddr, jmp, 6);
VirtualProtect((LPVOID)funcAddr, 6, prev, &prev);
FlushInstructionCache(GetCurrentProcess(), NULL, NULL);
return funcAddr;
}
// Hook
HookFunction(L"ws2_32.dll", "recv", (LPVOID*)nRecv, hookR);
Run Code Online (Sandbox Code Playgroud)
然后我附加了一个调试器,结果如下:
不过,有几件事我不明白,因为我仍在尝试理解和想象堆栈、堆等如何在调试器中协同工作。
BYTE jmp[6] = { 0xe9,0x00,0x00,0x00,0x00,0xc3 };
Run Code Online (Sandbox Code Playgroud)
我是否在这里替换指令,例如,用 0xe9 替换原始函数的“move, edi, edi”(recv)?然后下一条指令为 0x00... 或者它究竟是如何工作的?
任何详细的解释将不胜感激。
BOOL HookFunction(LPCWSTR moduleName, LPCSTR funcName, LPVOID funcProxy,
unsigned char* lpBackup)
{
BYTE jmp[6] =
{
0xe9,0x00,0x00,0x00,0x00, /*JMP and 4 bytes of offset*/
0xc3 /*RET*/
};
/*
JMP (e9) is relative, its 32-bit signed immediate operand encodes the
number of bytes to jump forward relative to the NEXT instruction.
*/
/* Get the target address of the function to hook */
DWORD funcAddr = (DWORD)GetProcAddress(GetModuleHandle(moduleName), funcName);
/* Code is not necessarily mapped as writable, we remap it */
DWORD prev;
VirtualProtect((LPVOID)funcAddr, 6, PAGE_EXECUTE_READWRITE, &prev);
/* Read the original 6 bytes we are going to overwrite */
ReadProcessMemory(GetCurrentProcess(), (LPVOID)funcAddr, lpBackup, 6, NULL);
/*
Compute the offset: target - source
target = funcProxy
source = funcAddr + 5 (length of JMP)
target - source = funcProxy - funcAddr - 5
*/
DWORD proxy = ((DWORD)funcProxy - funcAddr) - 5;
/*
Create the JMP instruction: set the offset
*/
memcpy(&jmp[1], &proxy, 4);
/* Overwrite the first 6 bytes of the target function */
memcpy((LPVOID)funcAddr, jmp, 6);
/* Reset the memory protection to its original value*/
VirtualProtect((LPVOID)funcAddr, 6, prev, &prev);
/* Since we write to a code section with DS, flush the L1 I cache */
FlushInstructionCache(GetCurrentProcess(), NULL, NULL);
return funcAddr;
}
Run Code Online (Sandbox Code Playgroud)
的HookFunction在存储器中创建小片的x86代码(一个蹦床形式的)
jmp <0> ;e9 00 00 00 00
ret ;c3
Run Code Online (Sandbox Code Playgroud)
where<0>被跳转的编码目标(参见代码中的注释)连续覆盖 - 钩子函数。
蹦床一旦制作完成,就会变成
jmp funcProxy ;e9 .. .. .. ..
ret ;c3
Run Code Online (Sandbox Code Playgroud)
然后直接在挂钩函数的开头编写此代码,从而覆盖其原始代码。
代码是多语言的 - 它适用于 x86 和 x86-64。
将钩子函数的原始代码复制到lpBackup.
这是需要再次调用原函数的,钩子函数不先恢复就不能调用。
由于这很昂贵且不可重入,因此更简洁的方法是修改导入地址表- 然而,此解决方案的有效性取决于您的要求。