gpt4 book ai didi

c - 如何使用 LKM Hook 中断门

转载 作者:行者123 更新时间:2023-11-30 16:35:14 25 4
gpt4 key购买 nike

我正在研究制作 rootkit。

我正在尝试使用 LKM Hook 门中断 4,在 VirtualBox 上工作。

但是当我触发门时,虚拟机就会卡住。 Hook 和脱钩似乎有效,但我不知道为什么它会被卡住。

这可能是与 VirtualBox 有关的问题吗?或者我错过了什么/做错了什么?hook 门的方式是 per-cpu IDT。我在每个 CPU 中创建一个内核线程,然后安装新的门。在 VirtualBox 中,每个 CPU 使用相同的 IDTR,因此我必须添加一些检查才能兼容。

如有任何帮助,我们将不胜感激。

我粘贴下面的代码。

Makefile

obj-m += so.o

so-objs := main.o core-asm.o asmcore.o

EXTRA_CFLAGS := -O0

KERNEL_HEADERS = /lib/modules/$(shell uname -r)/build

all:
make V=1 -C $(KERNEL_HEADERS) M=$(PWD) core.s
gcc -c core.s -o core-asm.o
gcc -c asmcore.s -o asmcore.o
make V=1 -C $(KERNEL_HEADERS) M=$(PWD) modules

clean:
make V=1 -C $(KERNEL_HEADERS) M=$(PWD) clean

ma​​in.c

#include <linux/module.h>

extern int hook_int(void);
extern void unhook_int(void);

int init_module(void) {
return hook_int();
}

void cleanup_module(void) {
unhook_int();
}

MODULE_LICENSE("GPL");

core.c

#include <asm/desc.h>
#include <linux/uaccess.h>
#include <linux/kthread.h>
#include <linux/mman.h>

#define VECTOR 4

extern void my_int_handler(void);
void for_each_idt(void (*cb)(gate_desc *idt));
void install_hook(gate_desc *idt);
void uninstall_hook(gate_desc *idt);
int kthread_fn(void *arg);
gate_desc *last_idt = NULL;
gate_desc gate_backup;
void *real_int_handler = NULL;
extern int my_memcpy(void *dst, void *src, size_t len);

int kthread_fn(void *arg) {
void (*cb)(gate_desc *idt) = arg;
struct desc_ptr idtr;
gate_desc *idt = NULL;

store_idt(&idtr);
idt = (gate_desc *) idtr.address;
if (last_idt != idt) {
cb(idt);
last_idt = idt;
}

return 0;
}

void for_each_idt(void (*cb)(gate_desc *idt)) {
size_t cpus = 0, i = 0;
struct task_struct *thread = NULL;

last_idt = NULL;
cpus = num_present_cpus();
while (i != cpus) {
thread = kthread_create(kthread_fn, cb, "kworker/%d:%d", (int) i, (int) cpus);
kthread_bind(thread, i);
wake_up_process(thread);
i++;
}
}

void install_hook(gate_desc *idt) {
gate_desc gate;

my_memcpy(&gate, &idt[VECTOR], sizeof(gate));
my_memcpy(&gate_backup, &idt[VECTOR], sizeof(gate));
printk("segment = %x\n", gate.segment);
printk("bits.ist = %x\n", gate.bits.ist);
printk("bits.zero = %x\n", gate.bits.zero);
printk("bits.type = %x\n", gate.bits.type);
printk("bits.dpl = %x\n", gate.bits.dpl);
printk("bits.p = %x\n", gate.bits.p);
printk("reserved = %x\n", gate.reserved);
printk("offset_low = %x\n", gate.offset_low);
printk("offset_middle = %x\n", gate.offset_middle);
printk("offset_high = %x\n", gate.offset_high);
gate.offset_low = (u16) my_int_handler;
gate.offset_middle = (u16) ((long) my_int_handler >> 16);
gate.offset_high = (u32) ((long) my_int_handler >> 32);
real_int_handler = idt[VECTOR].offset_low | ((int) idt[VECTOR].offset_middle << 16) | ((long) idt[VECTOR].offset_high << 32);
printk("after\n");
printk("offset_low = %x\n", gate.offset_low);
printk("offset_middle = %x\n", gate.offset_middle);
printk("offset_high = %x\n", gate.offset_high);
printk("my_int_handler = %lx\n", (long)my_int_handler);
printk("real_int_handler = %lx\n", (long)real_int_handler);
asm("cli\n\tmov\t%%cr0, %%rax\n\tand\t$0xfffffffffffeffff, %%rax\n\tmov\t%%rax, %%cr0" ::: "rax");
my_memcpy(&idt[VECTOR], &gate, sizeof(gate));
asm("mov\t%%cr0, %%rax\n\tor\t$0x10000, %%rax\n\tmov\t%%rax, %%cr0\n\tsti" ::: "rax");
}

void uninstall_hook(gate_desc *idt) {
asm("cli\n\tmov\t%%cr0, %%rax\n\tand\t$0xfffffffffffeffff, %%rax\n\tmov\t%%rax, %%cr0" ::: "rax");
my_memcpy(&idt[VECTOR], &gate_backup, sizeof(gate_backup));
asm("mov\t%%cr0, %%rax\n\tor\t$0x10000, %%rax\n\tmov\t%%rax, %%cr0\n\tsti" ::: "rax");
}

int hook_int(void) {
for_each_idt(install_hook);
return 0;
}

void unhook_int(void) {
for_each_idt(uninstall_hook);
}

asmcore.s

        .extern real_int_handler
.text
.globl my_memcpy
.type my_memcpy, @function
my_memcpy:
mov %rdx, %rcx
rep movsb
mov %rcx, %rax
ret
.size my_memcpy, .-my_memcpy

.globl my_int_handler
.type my_int_handler, @function
my_int_handler:
jmp *real_int_handler(%rip)
.size my_int_handler, .-my_int_handler

trigger.s

        .globl main
.type main, @function
main:
int $4
ret

.size main, .-main

加载后的 dmesg

[ 1000.001717] segment = 10
[ 1000.001718] bits.ist = 0
[ 1000.001718] bits.zero = 0
[ 1000.001719] bits.type = e
[ 1000.001719] bits.dpl = 3
[ 1000.001720] bits.p = 1
[ 1000.001720] reserved = 0
[ 1000.001721] offset_low = 1030
[ 1000.001721] offset_middle = 9480
[ 1000.001722] offset_high = ffffffff
[ 1000.001722] after
[ 1000.001723] offset_low = 1513
[ 1000.001723] offset_middle = c052
[ 1000.001724] offset_high = ffffffff
[ 1000.001724] my_int_handler = ffffffffc0521513
[ 1000.001725] real_int_handler = ffffffff94801030

不 Hook 执行./trigger

diwou@diwou-VirtualBox:~/arpso2$ ./trigger
Violación de segmento (`core' generado)

最佳答案

源代码是正确的。问题出在 VirtualBox 上。修改 IDT、GDT 和 MSR 可能是导致其卡住的原因。

我能够添加一个新的 IDT 条目,但问题似乎出在更改已写入的值时。例如更改 MSR_LSTAR 寄存器、在 GDT 中添加调用门或修改 IDT 上的中断处理程序。

关于c - 如何使用 LKM Hook 中断门,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49016149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com