-
Notifications
You must be signed in to change notification settings - Fork 201
/
KeyLoggerThread.cpp
327 lines (295 loc) · 7.85 KB
/
KeyLoggerThread.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
Copyright 2015 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "stdafx.h"
#include "KeyLoggerThread.h"
#include "Utility.h"
#include <ETWProviders\etwprof.h>
#include <atomic>
namespace
{
std::atomic<bool> g_LogKeyboardDetails(false);
std::string MetaKeys()
{
std::string metaKeys;
if (GetAsyncKeyState(VK_CONTROL))
metaKeys += "Ctrl+";
if (GetAsyncKeyState(VK_SHIFT))
metaKeys += "Shift+";
if (GetAsyncKeyState(VK_MENU))
metaKeys += "Alt+";
if (GetAsyncKeyState(VK_LWIN) || GetAsyncKeyState(VK_RWIN))
metaKeys += "Win+";
return metaKeys;
}
_Pre_satisfies_(nCode == HC_ACTION)
LRESULT CALLBACK LowLevelKeyboardHook(int nCode, WPARAM wParam, LPARAM lParam)
{
UIETWASSERT(nCode == HC_ACTION);
// wParam is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP
const KBDLLHOOKSTRUCT* const pKbdLLHook = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)
{
// Translate some of the keys to character names to make them easier to read
// in the trace. Note that having the character codes means that this is a
// true key logger. Consider checking g_LogKeyboardDetails if this is a
// concern.
char buffer[20];
const char* pLabel = buffer;
DWORD code = pKbdLLHook->vkCode;
bool isMetaKey = false;
if ((code >= 'A' && code <= 'Z') || (code >= '0' && code <= '9') || code == ' ')
{
if (!g_LogKeyboardDetails)
{
// Make letters and numbers generic, for privacy.
if (code >= 'A' && code <= 'Z')
code = 'A';
else if (code >= '0' && code <= '9')
code = '0';
}
sprintf_s(buffer, "%c", code);
}
else if (code >= VK_NUMPAD0 && code <= VK_NUMPAD9)
{
if (!g_LogKeyboardDetails)
code = '0';
sprintf_s(buffer, "%c", '0' + (code - VK_NUMPAD0));
}
else if (code >= VK_F1 && code <= VK_F12)
{
sprintf_s(buffer, "F%lu", code + 1 - VK_F1);
}
else
{
switch (code)
{
case VK_BACK:
pLabel = "backspace";
break;
case VK_TAB:
pLabel = "tab";
break;
case VK_RETURN:
pLabel = "enter";
break;
case VK_PRIOR:
pLabel = "page up";
break;
case VK_NEXT:
pLabel = "page down";
break;
case VK_END:
pLabel = "end";
break;
case VK_HOME:
pLabel = "home";
break;
case VK_LEFT:
pLabel = "left";
break;
case VK_UP:
pLabel = "up";
break;
case VK_RIGHT:
pLabel = "right";
break;
case VK_DOWN:
pLabel = "down";
break;
case VK_DELETE:
pLabel = "delete";
break;
case VK_INSERT:
pLabel = "insert";
break;
case VK_SHIFT:
case VK_LSHIFT:
case VK_RSHIFT:
pLabel = "shift";
isMetaKey = true;
break;
case VK_CONTROL:
case VK_LCONTROL:
case VK_RCONTROL:
pLabel = "control";
isMetaKey = true;
break;
case VK_MENU:
case VK_LMENU:
case VK_RMENU:
pLabel = "alt";
isMetaKey = true;
break;
case VK_ESCAPE:
pLabel = "esc";
break;
case VK_LWIN:
case VK_RWIN:
pLabel = "Win";
isMetaKey = true;
break;
case VK_OEM_PERIOD:
pLabel = ".";
break;
default:
// Handle miscellaneous keys that are otherwise missed.
if (const UINT translated = MapVirtualKey(code, MAPVK_VK_TO_CHAR))
sprintf_s(buffer, "%c", translated);
else
pLabel = "<unknown key>";
break;
}
}
std::string keyDownDetails = isMetaKey ? pLabel : MetaKeys() + pLabel;
ETWKeyDown(code, keyDownDetails.c_str(), 0, 0);
}
return CallNextHookEx(0, nCode, wParam, lParam);
}
_Pre_satisfies_(nCode == HC_ACTION)
LRESULT CALLBACK LowLevelMouseHook(int nCode, WPARAM wParam, LPARAM lParam) noexcept
{
UIETWASSERT(nCode == HC_ACTION);
const MSLLHOOKSTRUCT* const pMouseLLHook = reinterpret_cast<MSLLHOOKSTRUCT*>(lParam);
int whichButton = -1;
bool pressed = true;
switch (wParam)
{
case WM_LBUTTONDOWN:
whichButton = 0;
break;
case WM_MBUTTONDOWN:
whichButton = 1;
break;
case WM_RBUTTONDOWN:
whichButton = 2;
break;
case WM_LBUTTONUP:
whichButton = 0;
pressed = false;
break;
case WM_MBUTTONUP:
whichButton = 1;
pressed = false;
break;
case WM_RBUTTONUP:
whichButton = 2;
pressed = false;
break;
}
if (whichButton >= 0)
{
if (pressed)
{
ETWMouseDown(whichButton, 0, pMouseLLHook->pt.x, pMouseLLHook->pt.y);
}
else
{
ETWMouseUp(whichButton, 0, pMouseLLHook->pt.x, pMouseLLHook->pt.y);
}
}
else
{
if (wParam == WM_MOUSEWHEEL)
{
const int wheelDelta = GET_WHEEL_DELTA_WPARAM(pMouseLLHook->mouseData);
ETWMouseWheel(0, wheelDelta, pMouseLLHook->pt.x, pMouseLLHook->pt.y);
}
if (wParam == WM_MOUSEMOVE)
{
ETWMouseMove(0, pMouseLLHook->pt.x, pMouseLLHook->pt.y);
}
}
return CallNextHookEx(0, nCode, wParam, lParam);
}
DWORD __stdcall InputThread(LPVOID) noexcept
{
SetCurrentThreadName("Input logger");
// When UIforETW is halted in a debugger the keyboard and mouse hooks cannot respond
// in a timely manner. This means that each bit of user input has to timeout, which
// makes debugging painful - the timeout appears to be about ten seconds.
if (IsDebuggerPresent())
{
#ifdef OUTPUT_DEBUG_STRINGS
OutputDebugString(L"Input logging disabled while debugging.\n");
#endif
return 0;
}
// Install a hook so that this thread will receive all keyboard messages. They must be
// processed in a timely manner or else bad things will happen. Doing this on a
// separate thread is a good idea, but even then bad things will happen to your system
// if you halt in a debugger. Even simple things like calling printf() from the hook
// can easily cause system deadlocks which render the mouse unable to move!
HHOOK keyHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardHook, NULL, 0);
HHOOK mouseHook = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseHook, NULL, 0);
if (!keyHook && !mouseHook)
return 0;
// Run a message pump -- necessary so that the hooks will be processed
BOOL bRet;
MSG msg;
// Keeping pumping messages until WM_QUIT is received. If this is opened
// in a child thread then you can terminate it by using PostThreadMessage
// to send WM_QUIT.
while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// Unhook and exit.
if (keyHook)
UnhookWindowsHookEx(keyHook);
if (mouseHook)
UnhookWindowsHookEx(mouseHook);
return 0;
}
}
void SetKeyloggingState(enum KeyLoggerState state) noexcept
{
static HANDLE s_hThread = 0;
static DWORD s_threadID;
// If key logging should be off then terminate the thead
// if it is running.
if (state == kKeyLoggerOff)
{
if (s_hThread)
{
PostThreadMessage(s_threadID, WM_QUIT, 0, 0);
WaitForSingleObject(s_hThread, INFINITE);
CloseHandle(s_hThread);
s_hThread = 0;
}
return;
}
// If key logging should be on then start the thread if
// it isn't running.
if (!s_hThread)
{
s_hThread = CreateThread(NULL, 0, InputThread, NULL, 0, &s_threadID);
}
switch (state)
{
case kKeyLoggerAnonymized:
g_LogKeyboardDetails = false;
break;
case kKeyLoggerFull:
g_LogKeyboardDetails = true;
break;
}
}