diff options
author | kartofen <mladenovnasko0@gmail.com> | 2023-04-26 23:29:14 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2023-04-26 23:29:14 +0300 |
commit | 2c0f30c29b4b70a45ba01a0c32ae31ac7f75625b (patch) | |
tree | dd207c5eb32fec346b51af493e386948083fd966 | |
parent | 9bf5719d4c59993c07113c089ebd5a92d692f785 (diff) |
learning about modules
-rw-r--r-- | src/initramfs-init.sh | 4 | ||||
-rw-r--r-- | src/module/module.c | 79 | ||||
-rwxr-xr-x | vm.sh | 6 |
3 files changed, 81 insertions, 8 deletions
diff --git a/src/initramfs-init.sh b/src/initramfs-init.sh index 2144100..7ce8606 100644 --- a/src/initramfs-init.sh +++ b/src/initramfs-init.sh @@ -1,5 +1,9 @@ #!/bin/sh +# mount the proc file system +mkdir /proc +mount -t proc proc /proc + insmod /usr/bin/keylogger.ko dmesg 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"); @@ -2,7 +2,7 @@ function run { - qemu-system-x86_64 \ + st qemu-system-x86_64 \ -kernel "$BIND/$KERNEL_NAME" \ -initrd "$BIND/$INITFS_NAME" \ -append "console=ttyS0" -nographic @@ -12,12 +12,15 @@ function initramfs { IMG="$(pwd)/$1" DIR="${IMG%.*}" + mkdir -p $DIR # get busybox things install -D $(which busybox) $DIR/bin/busybox ln -sf busybox $DIR/bin/sh ln -sf busybox $DIR/bin/dmesg + ln -sf busybox $DIR/bin/mount + # ln -sf busybox $DIR/bin/bash # copy the compiled binaries mkdir -p $DIR/usr/bin @@ -29,7 +32,6 @@ function initramfs # make image and delete folder (cd $DIR; find . | cpio -H newc -o | gzip > $IMG) - rm -rf $DIR } |