» 网友学堂 » LINUX教程 » Linux系统可卸载内核模块完全指南
Linux系统可卸载内核模块完全指南
作者:问天 发表时间:2007-2-15 03:38 阅读:530次 在百度搜索相关内容

简介
  将Linux操作系统用于服务器在现在是越来越普遍了。因此,入侵Linux在今天也变得越来越有趣.目前最好的攻击Linux的技术就是修改内核代码.由于一种叫做可卸载内核(Loadable
  KernelModules(LKMs))的机制,我们有可能编写在内核级别运行的代码,而这种代码可以允许我们接触到操作系统中非常敏感的部分.在过去有一些很好的关于LKM知识的文本或者文件,他们介绍一些新的想法,方法以及一名Hacker所梦寐以求的完整的LKMs.而且也有一些很有趣的公开的讨论(在新闻组,邮件列表).
  然而为什么我再重新写这些关于LKMs的东西呢?下面是我的一些理由:
[color=#FFFFFF'][/color]

  在过去的教材中常常没有为那些初学者提供很好的解释.而这个教材中有很大一部分的基础章节.这是为了帮助那些初学者理解概念的.我见过很多人使用系统的缺陷或者监听器然而却丝毫不了解他们是如何工作的.在这篇文章中我包含了很多带有注释的源代码,只是为了帮助那些认为入侵仅仅是一些工具游戏的初学者!
  每一个发布的教材不过把话题集中在某个特别的地方.没有一个完整的指导给那些关注LKMs的Hacker.这篇文章会覆盖几乎所有的关于LKMs的资料(甚至是病毒方面的).
  这篇文章是从Hacker或者病毒的角度进行讨论的,但是系统管理员或者内核的开发者也可以参考并从中学到很多东西.
  以前的文章介绍一些利用LKMs进行入侵的优点或者方法,但是总是还有一些东西是我们过去从来没有听说过的.这篇文章会介绍一些新的想法给大家.(不是所有的新的资料,只是一些对我们有帮助的)
  这篇文章会介绍一些简单的防止LKM攻击的方法,同时也会介绍如何通过使用一些像运行时内核补丁(Runtime Kernel Patching)这样的方法来对付这些防御措施.
  要记住这些新的想法仅仅是通过利用一些特殊的模块来实现的.要在现实中真正使用他们还需要对他们进行改进.这篇文章的主要目的是给大家在整个LKM上一个大方向上的指导.在附录A中,我会给大家一些实用的LKMs,并附上一些简短的注释(这是为那些新手的),以及如何使用他们.
[color=#FFFFFF'][/color]

  整篇文章(除了第五部分)是基于 Linux 2.0.x的80x86机器的.我测试了所有的程序和代码段.为了能够正常使用这里提供的绝大部分代码,你的Linux系统必须有LKM支持.只有在第四部分会给大家一些不需要LKM支持的源代码.本文的绝大多数想法一样可以在Linux2.2.x上实现(也许你会需要一些小小的改动).
  这篇文章会有一个特别的章节来帮助系统管理员进行系统安全防护.你(作为一名Hacker)也必须仔细阅读这些章节.你必须要知道所有系统管理员知道的,甚至更多.你也会从中发现很多优秀的想法.这也会对你开发高级的入侵系统的LKMs有所帮助.
 第一部分. 基础知识

  1.1 什么是LKMs
  LKMs就是可卸载的内核模块(Loadable Kernel
  Modules)。这些模块本来是Linux系统用于扩展他的功能的。使用LKMs的优点有:他们可以被动态的加载,而且不需要重新编译内核。由于这些优点,他们常常被特殊的设备(或者文件系统),例如声卡等使用。
  每个LKM至少由两个基本的函数组成:
[color=#FFFFFF'][/color]

  int init_module(void) /*用于初始化所有的数据*/
  {
[color=#FFFFFF'][/color]

  ...
  }
[color=#FFFFFF'][/color]

  void cleanup_module(void) /*用于清除数据从而能有一个安全的退出*/
  {
[color=#FFFFFF'][/color]

  ...
  }
  加载一个模块(常常只限于root能够使用)的命令是:
  # insmod module.o
  这个命令让系统进行了如下工作:
[color=#FFFFFF'][/color]

  加载可执行的目标文件(在这儿是module.o)
  调用 create_module这个系统调用(至于什么叫系统调用,见1.2)来分配内存.
  不能解决的引用由系统调用get_kernel_syms进行查找引用.
  在此之后系统调用init_module将会被调用用来初始化LKM->执行 int inti_module(void) 等等
  (内核符号将会在1.3节中内核符号表中解释)
[color=#FFFFFF'][/color]

  OK,到目前为止,我想我们可以写出我们第一个小的LKM来演示一下这些基本的功能是如何工作的了.
[color=#FFFFFF'][/color]

  #define MODULE
  #include
[color=#FFFFFF'][/color]

  int init_module(void)
  {
  printk("<1>Hello World\n");
[color=#FFFFFF'][/color]

  return 0;
[color=#FFFFFF'][/color]

  }
[color=#FFFFFF'][/color]

  void cleanup_module(void)
  {
  printk("<1>Bye, Bye");
  }
  你可能会奇怪为什么在这里我用printk(....)而不是printf(.....).在这里你要明白内核编程是完全不同于普通的用户环境下的编程的.你只能使用很有限的一些函数(见1.6)仅使用这些函数你是干不了什么的.因此,你将会学会如何使用你在用户级别中用的那么多函数来帮助你入侵内核.耐心一些,在此之前我们必须做一点其他的.....
  上面的那个例子可以很容易的被编译:
[color=#FFFFFF'][/color]

  # gcc -c -O3 helloworld.c
  # insmod helloworld.o
  OK,现在我们的模块已经被加载了并且给我们打印出了那句很经典的话.现在你可以通过下面这个命令来确认你的LKM确实运行在内核级别中:
  # lsmod
[color=#FFFFFF'][/color]

  Module     Pages  Used by
  helloworld     1    0
[color=#FFFFFF'][/color]

  这个命令读取在 /proc/modules 的信息来告诉你当前那个模块正被加载.'Pages'
[color=#FFFFFF'][/color]

  显示的是内存的信息(这个模块占了多少内存页面).'Used by'显示了这个模块被系统
  使用的次数(引用计数).这个模块只有当这个计数为0时才可以被除去.在检查过这个以后,你可以用下面的命令卸载这个模块
  # rmmod helloworld
[color=#FFFFFF'][/color]

  OK,这不过是我们朝LKMs迈出的很小的一步.我常常把这些LKMs于老的DOS TSR程序做比较,(是的,我知道他们之间有很多地方不一样),那些TSR能够常驻在内存并且截获到我们想要的中断.Microsoft's Win9x有一些类似的东西叫做VxD.关于这些程序的最有意思的一点在于他们都能够挂在一些系统的功能上,在Linux中我们称这些功能为系统调用.
  1.2什么是系统调用

  我希望你能够懂,每个操作系统在内核中都有一些最为基本的函数给系统的其他操作调用.在Linux系统中这些函数就被称为系统调用(System Call).他们代表了一个从用户级别到内核级别的转换.在用户级别中打开一个文件在内核级别中是通过sys_open这个系统调用实现的.在/usr/include/sys/syscall.h中有一个完整的系统调用列表.下面的列表是我的syscall.h
  #ifndef _SYS_SYSCALL_H
  #define _SYS_SYSCALL_H
  #define SYS_setup 0
  /* 只被init使用,用来启动系统的*/
[color=#FFFFFF'][/color]

  #define SYS_exit 1
  #define SYS_fork 2
  #define SYS_read 3
[color=#FFFFFF'][/color]

  #define SYS_write 4
  #define SYS_open 5
  #define SYS_close 6
[color=#FFFFFF'][/color]

  #define SYS_waitpid 7
[color=#FFFFFF'][/color]

  #define SYS_creat 8
[color=#FFFFFF'][/color]

  #define SYS_link 9
  #define SYS_unlink 10
  #define SYS_execve 11
[color=#FFFFFF'][/color]

  #define SYS_chdir 12
[color=#FFFFFF'][/color]

  #define SYS_time 13
  #define SYS_prev_mknod 14
[color=#FFFFFF'][/color]

  #define SYS_chmod 15
[color=#FFFFFF'][/color]

  #define SYS_chown 16
  #define SYS_break 17
  #define SYS_oldstat 18
  #define SYS_lseek 19
  #define SYS_getpid 20
  #define SYS_mount 21
  #define SYS_umount 22
  #define SYS_setuid 23
  #define SYS_getuid 24
[color=#FFFFFF'][/color]

  #define SYS_stime 25
  #define SYS_ptrace 26
  #define SYS_alarm 27
  #define SYS_oldfstat 28
  #define SYS_pause 29
  #define SYS_utime 30
[color=#FFFFFF'][/color]

  #define SYS_stty 31
[color=#FFFFFF'][/color]

  #define SYS_gtty 32
[color=#FFFFFF'][/color]

  #define SYS_access 33
[color=#FFFFFF'][/color]

  #define SYS_nice 34
  #define SYS_ftime 35
  #define SYS_sync 36
  #define SYS_kill 37
  #define SYS_rename 38
  #define SYS_mkdir 39
  #define SYS_rmdir 40
  #define SYS_dup 41
[color=#FFFFFF'][/color]

  #define SYS_pipe 42
[color=#FFFFFF'][/color]

  #define SYS_times 43
[color=#FFFFFF'][/color]

  #define SYS_prof 44
[color=#FFFFFF'][/color]

  #define SYS_brk 45
  #define SYS_setgid 46
  #define SYS_getgid 47
  #define SYS_signal 48
  #define SYS_geteuid 49
  #define SYS_getegid 50
  #define SYS_acct 51
  #define SYS_phys 52
  #define SYS_lock 53
[color=#FFFFFF'][/color]

  #define SYS_ioctl 54
  #define SYS_fcntl 55
  #define SYS_mpx 56
  #define SYS_setpgid 57
[color=#FFFFFF'][/color]

  #define SYS_ulimit 58
  #define SYS_oldolduname 59
  #define SYS_umask 60
[color=#FFFFFF'][/color]

  #define SYS_chroot 61
  #define SYS_prev_ustat 62
  #define SYS_dup2 63
[color=#FFFFFF'][/color]

  #define SYS_getppid 64
  #define SYS_getpgrp 65
  #define SYS_setsid 66
[color=#FFFFFF'][/color]

  #define SYS_sigaction 67
[color=#FFFFFF'][/color]

  #define SYS_siggetmask 68
  #define SYS_sigsetmask 69
  #define SYS_setreuid 70
  #define SYS_setregid 71
  #define SYS_sigsuspend 72
  #define SYS_sigpending 73
  #define SYS_sethostname 74
[color=#FFFFFF'][/color]

  #define SYS_setrlimit 75
[color=#FFFFFF'][/color]

  #define SYS_getrlimit 76
  #define SYS_getrusage 77
[color=#FFFFFF'][/color]

  #define SYS_gettimeofday 78
  #define SYS_settimeofday 79
  #define SYS_getgroups 80
  #define SYS_setgroups 81
  #define SYS_select 82
  #define SYS_symlink 83
  #define SYS_oldlstat 84
  #define SYS_readlink 85
[color=#FFFFFF'][/color]

  #define SYS_uselib 86
  #define SYS_swapon 87
[color=#FFFFFF'][/color]

  #define SYS_reboot 88
  #define SYS_readdir 89
  #define SYS_mmap 90
  #define SYS_munmap 91
[color=#FFFFFF'][/color]

  #define SYS_truncate 92
  #define SYS_ftruncate 93
[color=#FFFFFF'][/color]

  #define SYS_fchmod 94
  #define SYS_fchown 95
  #define SYS_getpriority 96
  #define SYS_setpriority 97
  #define SYS_profil 98
[color=#FFFFFF'][/color]

  #define SYS_statfs 99
  #define SYS_fstatfs 100
  #define SYS_ioperm 101
  #define SYS_socketcall 102
  #define SYS_klog 103
[color=#FFFFFF'][/color]

  #define SYS_setitimer 104
  #define SYS_getitimer 105
  #define SYS_prev_stat 106
  #define SYS_prev_lstat 107
  #define SYS_prev_fstat 108
  #define SYS_olduname 109
[color=#FFFFFF'][/color]

  #define SYS_iopl 110
  #define SYS_vhangup 111
  #define SYS_idle 112
  #define SYS_vm86old 113
  #define SYS_wait4 114
[color=#FFFFFF'][/color]

  #define SYS_swapoff 115
  #define SYS_sysinfo 116
[color=#FFFFFF'][/color]

  #define SYS_ipc 117
  #define SYS_fsync 118
  #define SYS_sigreturn 119
  #define SYS_clone 120
  #define SYS_setdomainname 121
  #define SYS_uname 122
[color=#FFFFFF'][/color]

  #define SYS_modify_ldt 123
  #define SYS_adjtimex 124
[color=#FFFFFF'][/color]

  #define SYS_mprotect 125
[color=#FFFFFF'][/color]

  #define SYS_sigprocmask 126
  #define SYS_create_module 127
[color=#FFFFFF'][/color]

  #define SYS_init_module 128
  #define SYS_delete_module 129
[color=#FFFFFF'][/color]

  #define SYS_get_kernel_syms 130
[color=#FFFFFF'][/color]

  #define SYS_quotactl 131
[color=#FFFFFF'][/color]

  #define SYS_getpgid 132
  #define SYS_fchdir 133
[color=#FFFFFF'][/color]

  #define SYS_bdflush 134
[color=#FFFFFF'][/color]

  #define SYS_sysfs 135
  #define SYS_personality 136
[color=#FFFFFF'][/color]

  #define SYS_afs_syscall 137
[color=#FFFFFF'][/color]

  #define SYS_setfsuid 138
  #define SYS_setfsgid 139
  #define SYS__llseek 140
  #define SYS_getdents 141
[color=#FFFFFF'][/color]

  #define SYS__newselect 142
[color=#FFFFFF'][/color]

  #define SYS_flock 143
  #define SYS_syscall_flock SYS_flock
  #define SYS_msync 144
[color=#FFFFFF'][/color]

  #define SYS_readv 145
  #define SYS_syscall_readv SYS_readv
  #define SYS_writev 146
[color=#FFFFFF'][/color]

  #define SYS_syscall_writev SYS_writev
  #define SYS_getsid 147
[color=#FFFFFF'][/color]

  #define SYS_fdatasync 148
[color=#FFFFFF'][/color]

  #define SYS__sysctl 149
  #define SYS_mlock 150
  #define SYS_munlock 151
[color=#FFFFFF'][/color]

  #define SYS_mlockall 152
  #define SYS_munlockall 153
  #define SYS_sched_setparam 154
  #define SYS_sched_getparam 155
  #define SYS_sched_setscheduler 156
  #define SYS_sched_getscheduler 157
[color=#FFFFFF'][/color]

  #define SYS_sched_yield 158
  #define SYS_sched_get_priority_max 159
  #define SYS_sched_get_priority_min 160
  #define SYS_sched_rr_get_interval 161
  #define SYS_nanosleep 162
  #define SYS_mremap 163
  #define SYS_setresuid 164
  #define SYS_getresuid 165
[color=#FFFFFF'][/color]

  #define SYS_vm86 166
  #define SYS_query_module 167
  #define SYS_poll 168
  #define SYS_syscall_poll SYS_poll
  #endif /* */
  每个系统调用都有一个预定义的数字(见上表),那实际上是用来进行这些调用的.内核通过中断0x80来控制每一个系统调用.这些系统调用的数字以及任何参数都将被放入某些寄存器(eax用来放那些代表系统调用的数字,比如说)
 那些系统调用的数字是一个被称之为sys_call_table[]的内核中的数组结构的索引值.这个结构把系统调用的数字映射到实际使用的函数.

[color=#FFFFFF'][/color]

  OK,这些是继续阅读所必须的足够知识了.下面的表列出了那些最有意思的系统调用以及一些简短的注释.相信我,为了你能够真正的写出有用的LKM你必须确实懂得那些系统调
[color=#FFFFFF'][/color]

  用是如何工作的.
  系统调用列表:
[color=#FFFFFF'][/color]

  int sys_brk(unsigned long new_brk);
  改变DS(数据段)的大小->这个系统调用会在1.4中讨论
  int sys_fork(struct pt_regs regs);
[color=#FFFFFF'][/color]

  著名的fork()所用的系统调用
  int sys_getuid ()
  int sys_setuid (uid_t uid)
[color=#FFFFFF'][/color]

  用于管理UID等等的系统调用
  int sys_get_kernel_sysms(struct kernel_sym *table)
  用于存取系统函数表的系统调用(->1.3)
  int sys_sethostname (char *name, int len);
  int sys_gethostname (char *name, int len);
[color=#FFFFFF'][/color]

  sys_sethostname是用来设置主机名(hostname)的,sys_gethostname是用来取的
  int sys_chdir (const char *path);
[color=#FFFFFF'][/color]

  int sys_fchdir (unsigned int fd);
  两个函数都是用于设置当前的目录的(cd ...)
  int sys_chmod (const char *filename, mode_t mode);
  int sys_chown (const char *filename, mode_t mode);
  int sys_fchmod (unsigned int fildes, mode_t mode);
  int sys_fchown (unsigned int fildes, mode_t mode);
  用于管理权限的函数
  int sys_chroot (const char *filename);
  用于设置运行进程的根目录的
  int sys_execve (struct pt_regs regs);
  非常重要的系统调用->用于执行一个可执行文件的(pt_regs是堆栈寄存器)
[color=#FFFFFF'][/color]

  long sys_fcntl (unsigned int fd, unsigned int cmd, unsigned long arg);
  改变fd(打开文件描述符)的属性的
  int sym_link (const char *oldname, const char *newname);
  int sys_unlink (const char *name);
[color=#FFFFFF'][/color]

  用于管理硬/软链接的函数
[color=#FFFFFF'][/color]

  int sys_rename (const char *oldname, const char *newname);
  用于改变文件名
  int sys_rmdir (const char* name);
  int sys_mkdir (const *char filename, int mode);
[color=#FFFFFF'][/color]

  用于新建已经删除目录
  int sys_open (const char *filename, int mode);
  int sys_close (unsigned int fd);
  所有和打开文件(包括新建)有关的操作,还有关闭文件的.
  int sys_read (unsigned int fd, char *buf, unsigned int count);
  int sys_write (unsigned int fd, char *buf, unsigned int count);
  读写文件的系统调用
  int sys_getdents (unsigned int fd, struct dirent *dirent, unsigned int count);
  用于取得文件列表的系统调用(ls...命令)
  int sys_readlink (const char *path, char *buf, int bufsize);
  读符号链接的系统调用
  int sys_selectt (int n, fd_set *inp, fd_set *outp, fd_set *exp, struct timeval *tvp);
  多路复用I/O操作
  sys_socketcall (int call, unsigned long args);
  socket 函数
  unsigned long sys_create_module (char *name, unsigned long size);
  int sys_delete_module (char *name);
  int sys_query_module (const char *name, int which, void *buf, size_t bufsize,
  size_t *ret);
  用于模块的加载/卸载和查询.
  以上就是我认为入侵者会感兴趣的系统调用.当然如果要获得系统的root权你有可能需要一些特殊的系统调用,但是作为一个hacker他很可能会拥有一个上面列出的最基本的列表.在第二部分中你会知道如何利用这些系统调用来实现你自己的目的.

#Advertisement