diff options
Diffstat (limited to 'src/module/module.c')
-rw-r--r-- | src/module/module.c | 79 |
1 files changed, 73 insertions, 6 deletions
diff --git a/src/module/module.c b/src/module/module.c index 43edb8c..b3b92af 100644 --- a/src/module/module.c +++ b/src/module/module.c @@ -2,19 +2,86 @@ #include <linux/module.h> #include <linux/printk.h> -static int __init init(void) +#include <linux/proc_fs.h> + +// #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) { - pr_info("Hello World!\n"); + 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(void) +static void __exit exit_keylogger(void) { - pr_info("Goodbye World!\n"); + printk("Exit Kek\n"); + + proc_remove(proc); + kfree(msg_buf); } -module_init(init); -module_exit(exit); +module_init(init_keylogger); +module_exit(exit_keylogger); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Kartofen"); |