[C++] DLL Hooking GetAsyncKeyState With MS-Detours

On lunes, 10 de enero de 2011 0 comentarios

n this fast tutorial we will be using MS-Detours (Microsoft Library Header file) to Hook the API GetAsyncKeyState.
Firstly you will need to get MS-Detours, you can download it here.
Once imported into your new empty DLL project add this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include 
#include "detours.h"
 
typedef SHORT (WINAPI *tGetAsyncKeyState)(__in  int vKey);
tGetAsyncKeyState oGetAsyncKeyState;
 
short WINAPI hGetAsyncKeyState(int key)
{
 MessageBoxA(NULL,"GetAsyncKeyState Detected.","Success",MB_OK);
 return oGetAsyncKeyState(key); // Returns the normal key, if you want to change it, you can.
}
 
int WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
    if ( dwReason == DLL_PROCESS_ATTACH )
    {
 DisableThreadLibraryCalls(hInstance); //Disables some callbacks
        oGetAsyncKeyState = (tGetAsyncKeyState)DetourFunction((PBYTE)GetAsyncKeyState, (PBYTE) hGetAsyncKeyState);
    }
 return true;
}
Firstly, we need to import the library’s we need, then typedef the API we want to Hook with the same arguments as the API. Next we can see our function, we can change whatever the API returns, etc.
Once the DLL attaches, it runs the Detour Function. Insert our API: (PBYTE)GetAsyncKeyState and our function: (PBYTE) hGetAsyncKeyState
Compile the DLL, now to inject it, we can use a freeware APP like RemoteDLL which you can download here.
Any questions, post in comments!
Enjoy!

0 comentarios:

Publicar un comentario