免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123
最近访问板块 发新帖
楼主: albcamus
打印 上一主题 下一主题

使用kprobes,截获execve系统调用 [复制链接]

论坛徽章:
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
21 [报告]
发表于 2010-04-20 13:03 |只看该作者
看不懂。。。
   收藏并膜拜
snriyt 发表于 2010-04-20 12:10

这里有个系统调用劫持的总结,其实就是针对albcamus兄的程序的,LZ可以参考一下,帮助你理解:
http://blog.chinaunix.net/u/33048/showart_2109264.html

论坛徽章:
0
22 [报告]
发表于 2010-04-20 15:00 |只看该作者
路过看一下。。。了

论坛徽章:
0
23 [报告]
发表于 2010-04-21 15:55 |只看该作者
syscall table已经是readonly的了, 不能直接赋值, 需要修改其属性为read-write,然后再写。

论坛徽章:
0
24 [报告]
发表于 2012-02-23 19:38 |只看该作者
高手啊,受教了!

论坛徽章:
0
25 [报告]
发表于 2013-04-05 12:46 |只看该作者
您好,我想获得系统运行时所有进程的所有系统调用,怎么实现?麻烦您给相关提示,或者相关资料,我在entry.S中找到系统调用那块,能劫持到系统调用号,但是不知道如何用汇编代码将内核中的信息打印到文件,而且不知道这样可不可以?希望您能给些帮助,不生感激~
回复 1# albcamus


   

论坛徽章:
0
26 [报告]
发表于 2015-01-13 11:18 |只看该作者
我是在kernel 2.6.32上跑的,劫持了read系统调用出现了panic,其他系统调用都没有问题,求大神指点

1.bug重现:
#insmod prov.ko (涉及的模块)
...read some files...
#rmmod prov
#insmod prov.ko
切换到另一个tty终端上,系统就崩溃了。
PS:a.在第一次insmod时,点击另外一个tty终端,是不会出错的。
b. 去掉if(ret ==0){...}, 也是不会出现panic的
c. 不是每次都会出现,但基本都出现,否则就要多一次rmmod,insmod,才出现panic

发了帖也沉了==、http://bbs.chinaunix.net/thread-4166052-1-1.html

求助!!

附全部代码:
  1. #include <linux/kernel.h>
  2. #include <linux/init.h>
  3. #include <linux/module.h>
  4. #include <linux/kprobes.h>
  5. #include <linux/kallsyms.h>
  6. #include <linux/sched.h>
  7. #include <linux/ptrace.h>
  8. #include <linux/mm.h>
  9. #include <linux/fs.h>
  10. #include <linux/file.h>
  11. #include <linux/smp.h>
  12. #include <linux/user.h>
  13. #include <linux/errno.h>
  14. #include <linux/cpu.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/fcntl.h>
  17. #include <asm/unistd.h>

  18. MODULE_DESCRIPTION("Intercept the system call table in Linux");
  19. MODULE_LICENSE("GPL");


  20. /* comment the following line to shut me up */
  21. #define INTERCEPT_DEBUG

  22. #ifdef INTERCEPT_DEBUG
  23.     #define dbgprint(format,args...) \
  24.         printk("intercept: function:%s-L%d: "format, __FUNCTION__, __LINE__, ##args);
  25. #else
  26.     #define dbgprint(format,args...)  do {} while(0);
  27. #endif


  28. /**
  29. * * the system call table
  30. * */
  31. void **my_table;

  32. unsigned int orig_cr0;

  33. /**
  34. * * the original syscall functions
  35. * */
  36. asmlinkage long (*old_read) (unsigned int fd, char __user *buf, size_t count);


  37. struct idtr {
  38.     unsigned short limit;
  39.     unsigned int base;
  40. } __attribute__ ((packed));


  41. struct idt {
  42.     unsigned short off1;
  43.     unsigned short sel;
  44.     unsigned char none, flags;
  45.     unsigned short off2;
  46. } __attribute__ ((packed));



  47. /**
  48. * * clear WP bit of CR0, and return the original value
  49. * */
  50. unsigned int clear_and_return_cr0(void)
  51. {
  52.     unsigned int cr0 = 0;
  53.     unsigned int ret;

  54.     asm volatile ("movl %%cr0, %%eax"
  55.               : "=a"(cr0)
  56.               );
  57.     ret = cr0;

  58.     /* clear the 20 bit of CR0, a.k.a WP bit */
  59.     cr0 &= 0xfffeffff;

  60.     asm volatile ("movl %%eax, %%cr0"
  61.               :
  62.               : "a"(cr0)
  63.               );
  64.     return ret;
  65. }

  66. /** set CR0 with new value
  67. * *
  68. * * @val : new value to set in cr0
  69. * */
  70. void setback_cr0(unsigned int val)
  71. {
  72.     asm volatile ("movl %%eax, %%cr0"
  73.               :
  74.               : "a"(val)
  75.               );
  76. }


  77. /**
  78. * * Return the first appearence of NEEDLE in HAYSTACK.  
  79. * * */
  80. static void *memmem(const void *haystack, size_t haystack_len,
  81.             const void *needle, size_t needle_len)
  82. {/*{{{*/
  83.     const char *begin;
  84.     const char *const last_possible
  85.         = (const char *) haystack + haystack_len - needle_len;

  86.     if (needle_len == 0)
  87.         /* The first occurrence of the empty string is deemed to occur at
  88. *            the beginning of the string.  */
  89.         return (void *) haystack;

  90.     /* Sanity check, otherwise the loop might search through the whole
  91. *        memory.  */
  92.     if (__builtin_expect(haystack_len < needle_len, 0))
  93.         return NULL;

  94.     for (begin = (const char *) haystack; begin <= last_possible;
  95.          ++begin)
  96.         if (begin[0] == ((const char *) needle)[0]
  97.             && !memcmp((const void *) &begin[1],
  98.                    (const void *) ((const char *) needle + 1),
  99.                    needle_len - 1))
  100.             return (void *) begin;

  101.     return NULL;
  102. }/*}}}*/


  103. /**
  104. * * Find the location of sys_call_table
  105. * */
  106. static unsigned long get_sys_call_table(void)
  107. {/*{{{*/
  108. /* we'll read first 100 bytes of int $0x80 */
  109. #define OFFSET_SYSCALL 100        

  110.     struct idtr idtr;
  111.     struct idt idt;
  112.     unsigned sys_call_off;
  113.     unsigned retval;
  114.     char sc_asm[OFFSET_SYSCALL], *p;

  115.     /* well, let's read IDTR */
  116.     asm("sidt %0":"=m"(idtr)
  117.              :
  118.              :"memory" );

  119.     dbgprint("idtr base at 0x%X\n", (unsigned int)idtr.base);

  120.     /* Read in IDT for vector 0x80 (syscall) */
  121.     memcpy(&idt, (char *) idtr.base + 8 * 0x80, sizeof(idt));

  122.     sys_call_off = (idt.off2 << 16) | idt.off1;

  123.     dbgprint("idt80: flags=%X sel=%X off=%X\n",
  124.                  (unsigned) idt.flags, (unsigned) idt.sel, sys_call_off);

  125.     /* we have syscall routine address now, look for syscall table
  126. *        dispatch (indirect call) */
  127.     memcpy(sc_asm, (void *)sys_call_off, OFFSET_SYSCALL);

  128.     /**
  129. *      * Search opcode of `call sys_call_table(,eax,4)'
  130. *           */
  131.     p = (char *) memmem(sc_asm, OFFSET_SYSCALL, "\xff\x14\x85", 3);
  132.     if (p == NULL)
  133.         return 0;

  134.     retval = *(unsigned *) (p + 3);
  135.     if (p) {
  136.         dbgprint("sys_call_table at 0x%x, call dispatch at 0x%x\n",
  137.              retval, (unsigned int) p);
  138.     }
  139.     return retval;
  140. #undef OFFSET_SYSCALL
  141. }/*}}}*/

  142. asmlinkage long new_read(unsigned int fd, char __user *buf, size_t count)
  143. {
  144.         long ret;
  145.         struct file *file;
  146.         ret = old_read(fd, buf, count);
  147.         /*0 represent EOF
  148.         * We just collect a 'read' when finished
  149.         */
  150.         if(ret == 0){
  151.                 file = fget(fd);
  152.                 if(file){
  153.                         fput(file);
  154.                 }
  155.         }
  156.         return ret;
  157. }

  158. static int intercept_init(void)
  159. {
  160.     my_table = (void **)get_sys_call_table();
  161.     if (my_table == NULL)
  162.         return -1;

  163.     dbgprint("sys call table address %p\n", (void *) my_table);

  164. #define REPLACE(x) old_##x = my_table[__NR_##x];\
  165.     my_table[__NR_##x] = new_##x

  166.    
  167.         REPLACE(read);

  168. #undef REPLACE
  169.     return 0;
  170. }


  171. static int __init this_init(void)
  172. {
  173.     int ret;
  174.     printk("syscall intercept: Hi, poor linux!\n");

  175.     orig_cr0 = clear_and_return_cr0();   
  176.     ret = intercept_init();
  177.     setback_cr0(orig_cr0);

  178.     return ret;
  179. }

  180. static void __exit this_fini(void)
  181. {
  182.     printk("syscall intercept: bye, poor linux!\n");

  183. #define RESTORE(x) my_table[__NR_##x] = old_##x

  184.     orig_cr0 = clear_and_return_cr0();   
  185.         RESTORE(read);
  186.     setback_cr0(orig_cr0);

  187. #undef RESTORE
  188. }

  189. module_init(this_init);
  190. module_exit(this_fini);
复制代码

论坛徽章:
0
27 [报告]
发表于 2015-09-05 14:59 |只看该作者
REPLACE的宏定义和调用有点小坑啊。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP