Background
I am working on a legacy product that can successfully with an injection DLL intercept an arbitrary method call that the injection process is trying to make in an arbitrary dll. In particular, the gdi32.dll library. Unfortunately, it does not work when it is embedded in 64-bit applications. It became a hot button and it was time to update its functionality. Unfortunately, the source is a barren commentary (typical>: - <), and from his views the one who wrote this was quite familiar with the x86 instruction set. I have not worked with the assembly for many years, and when I did, it was a Motorola assembly.
After cleaning the Internet, I came across this article from an Intel employee. If our source code had not preceded this article for about 7 years, I would say that it was here that our Mr. NoComments developer learned how to intercept API methods. This is like a procedure. This article also summarizes in the good pdf format ( API Interceptor API) , which can also be found linked to the aforementioned website.
Problem
I would like to really understand the example provided in the link to the Intel web page so that I can crack the solution for the 64-bit script well. This is well documented and a little easier for me to understand. The following is a snippet using the InterceptAPI () procedure. I added my own comments, marked with "// #" (the original comments are marked with the // standard), where I explain what I think I know and what I don't know
BOOL InterceptAPI(HMODULE hLocalModule, const char* c_szDllName,
const char* c_szApiName, DWORD dwReplaced, DWORD dwTrampoline, int offset)
{
int i;
DWORD dwOldProtect;
DWORD dwAddressToIntercept = (DWORD)GetProcAddress(
GetModuleHandle((char*)c_szDllName), (char*)c_szApiName);
BYTE *pbTargetCode = (BYTE *) dwAddressToIntercept;
BYTE *pbReplaced = (BYTE *) dwReplaced;
BYTE *pbTrampoline = (BYTE *) dwTrampoline;
VirtualProtect((void *) dwTrampoline, 5+offset, PAGE_WRITECOPY, &dwOldProtect);
for (i=0;i<offset;i++)
*pbTrampoline++ = *pbTargetCode++;
pbTargetCode = (BYTE *) dwAddressToIntercept;
*pbTrampoline++ = 0xE9;
*((signed int *)(pbTrampoline)) = (pbTargetCode+offset) - (pbTrampoline + 4);
VirtualProtect((void *) dwTrampoline, 5+offset, PAGE_EXECUTE, &dwOldProtect);
VirtualProtect((void *) dwAddressToIntercept, 5, PAGE_WRITECOPY, &dwOldProtect);
*pbTargetCode++ = 0xE9;
*((signed int *)(pbTargetCode)) = pbReplaced - (pbTargetCode +4);
VirtualProtect((void *) dwAddressToIntercept, 5, PAGE_EXECUTE, &dwOldProtect);
FlushInstructionCache(GetCurrentProcess(), NULL, NULL);
return TRUE;
}
, , . , : 64- ? : ", , 8 , ." , JMP - 32- , op- 32- 64- . , , , 5 . - ?
. , , "Microsoft Detours" "EasyHook". , , . . , . , , . ", , { } ."