#include #include #include #include // #define NAME "keylogger" #define NAME "_test_module" ssize_t proc_read(struct file *file, char __user *user, size_t size, loff_t *off); ssize_t proc_write(struct file *file, const char __user *user, size_t size, loff_t *off); static struct proc_dir_entry *proc = NULL; static const struct proc_ops proc_fops = { .proc_read = proc_read, .proc_write = proc_write, }; static char *msg_buf = NULL; #define COPY_TO_USER(_msg, _len) \ if(copy_to_user(user, _msg, _len)) return -EFAULT; \ *off += _len; \ user += _len; ssize_t proc_read(struct file *file, char __user *user, size_t size, loff_t *off) { printk("FILE READ KEK\n"); if (*off > 0) return 0; // we have already written if(msg_buf == NULL) { COPY_TO_USER("You have no previous messages\n", 30); return 30; } size_t len = strlen(msg_buf); if(size < (len + 24)) return 0; COPY_TO_USER("Your last message was: ", 23); COPY_TO_USER(msg_buf, len); COPY_TO_USER("\n", 1); return len + 24; } ssize_t proc_write(struct file *file, const char __user *user, size_t size, loff_t *off) { printk("FIEL WRITE KEK\n"); kfree(msg_buf); if((msg_buf = kmalloc(size + 1, GFP_KERNEL)) == NULL) { return -ENOMEM; } if(copy_from_user(msg_buf, user, size)) { return -EFAULT; } msg_buf[size] = '\0'; if(msg_buf[size-1] == '\n') msg_buf[size-1] = '\0'; return size; } static int __init init_keylogger(void) { printk("Init Kek\n"); if((proc = proc_create(NAME, 0666, NULL, &proc_fops)) == NULL) return -ENOMEM; return 0; } static void __exit exit_keylogger(void) { printk("Exit Kek\n"); proc_remove(proc); kfree(msg_buf); } module_init(init_keylogger); module_exit(exit_keylogger); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Kartofen"); MODULE_DESCRIPTION("A simple keylogger kernel module"); // Key logging example code // #include // #include // #include // static struct notifier_block nb; // static int keylogger_notify(struct notifier_block *nblock, // unsigned long code, void *_param) // { // struct keyboard_notifier_param *param = _param; // if (code == KBD_KEYCODE && param->value == 1) { // printk(KERN_INFO "Key pressed: %d\n", param->value); // } // return NOTIFY_OK; // } // static int __init keylogger_init(void) // { // nb.notifier_call = keylogger_notify; // register_keyboard_notifier(&nb); // return 0; // } // static void __exit keylogger_exit(void) // { // unregister_keyboard_notifier(&nb); // } // module_init(keylogger_init); // module_exit(keylogger_exit); // MODULE_LICENSE("GPL");