diff --git a/hyperdbg/linux/mock/Makefile b/hyperdbg/linux/mock/Makefile new file mode 100644 index 00000000..0680d315 --- /dev/null +++ b/hyperdbg/linux/mock/Makefile @@ -0,0 +1,10 @@ +obj-m += mymodule.o +mymodule-objs := mock.o mem.o + +PWD := $(CURDIR) + +all: + $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules + +clean: + $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean diff --git a/hyperdbg/linux/mock/mem.c b/hyperdbg/linux/mock/mem.c new file mode 100644 index 00000000..8f22ccc4 --- /dev/null +++ b/hyperdbg/linux/mock/mem.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include "module_info.h" +#include "mem.h" + +void* MemAllocKernel(size_t Size) +{ + void* ptr = kzalloc(Size, GFP_KERNEL); + + if (ptr) + printk(KERN_INFO "MemAllocKernel: Allocated %zu bytes at %px\n", Size, ptr); + else + printk(KERN_ERR "MemAllocKernel: failed to allocate %zu bytes\n", Size); + + return ptr; +} + +void MemFree(void* Ptr) +{ + if (Ptr) { + printk(KERN_INFO "MemFree: Freeing memory at %px\n", Ptr); + kfree(Ptr); + } +} + +void MemCopy(void* Destination, const void* Source, size_t Size) +{ + memcpy(Destination, Source, Size); +} + +void MemSet(void* Destination, int Value, size_t Size) +{ + memset(Destination, Value, Size); +} + diff --git a/hyperdbg/linux/mock/mem.h b/hyperdbg/linux/mock/mem.h new file mode 100644 index 00000000..b170ea5c --- /dev/null +++ b/hyperdbg/linux/mock/mem.h @@ -0,0 +1,16 @@ +#ifndef MEM_LINUX_H +#define MEM_LINUX_H + +#if !defined(__linux__) || !defined(__KERNEL__) +#error "This code must be compiled for Linux kernel only" +#endif + +#include + +void* MemAllocKernel(size_t Size); +void MemFree(void* Ptr); +void MemCopy(void* Destination, const void* Source, size_t Size); +void MemSet(void* Destination, int Value, size_t Size); + +#endif + diff --git a/hyperdbg/linux/mock/mock.c b/hyperdbg/linux/mock/mock.c new file mode 100644 index 00000000..1cafa619 --- /dev/null +++ b/hyperdbg/linux/mock/mock.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include "module_info.h" +#include "mem.h" + + +static void* g_AllocatedBuffer; +static size_t g_BufferSize = 4096; + +static int mock_init(void) +{ + char source_data[] = "This is a test string from Linux kernel module"; + + printk(KERN_INFO "Mock module loading\n"); + + g_AllocatedBuffer = MemAllocKernel(g_BufferSize); + if (!g_AllocatedBuffer) + return -ENOMEM; + + MemCopy(g_AllocatedBuffer, source_data, sizeof(source_data)); + printk(KERN_INFO "Copied data: %s\n", (char*)g_AllocatedBuffer); + + MemSet(g_AllocatedBuffer, 0, g_BufferSize); + printk(KERN_INFO "Buffer zeroed\n"); + + return 0; +} + +static void mock_exit(void) +{ + printk(KERN_INFO "Mock module unloading\n"); + MemFree(g_AllocatedBuffer); +} + +module_init(mock_init); +module_exit(mock_exit); + diff --git a/hyperdbg/linux/mock/module_info.h b/hyperdbg/linux/mock/module_info.h new file mode 100644 index 00000000..e5c82f7a --- /dev/null +++ b/hyperdbg/linux/mock/module_info.h @@ -0,0 +1,13 @@ +// module_info.h + +#ifndef MODULE_INFO_H +#define MODULE_INFO_H + +#include + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Alish"); +MODULE_DESCRIPTION("Linux Kernel module Mock"); +MODULE_VERSION("0.1"); + +#endif // _MODULE_INFO_H_