免费注册 查看新帖 |

Chinaunix

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

求大神来看看~通过替换目标地址的代码,来劫持系统函数的问题。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-04-19 15:05 |只看该作者 |倒序浏览
本帖最后由 kenshinoor 于 2012-04-19 15:36 编辑

学习Hacking the Linux Kernel Network Stack里边第六部分的代码。劫持Netfilter里边nf_iterate这个函数位置在net\netfilter\core.c。

很短的一段代码。劫持Netfilter里边的nf_iterate函数。

orig_nf_iterate是指向nf_iterate的函数指针。地址在System.map查到。

my_code是用来替换目标地址的代码。

劫持以后。直接返回NF_ACCEPT通过全部的包。

编译的结果。


可是只要insmod就直接死机。键盘等闪啊闪~
  1. //hijack nf_iterate

  2. #include <linux/netfilter.h>
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/netdevice.h>
  6. #include <linux/inetdevice.h>
  7. #include <asm/page.h>
  8. #include <linux/smp_lock.h>

  9. #define CODESIZE 10

  10. static unsigned char my_code[CODESIZE] = "\xb8\x00\x00\x00\x00"
  11.                                       "\x40\x90\x48"
  12.                                       "\xff\xe0";
  13. static unsigned char orig_code[CODESIZE];

  14. //lock define
  15. static spinlock_t hijack_lock = SPIN_LOCK_UNLOCKED;
  16. #define HIJACK_LOCK   spin_lock_irqsave(&hijack_lock, sl_flags)
  17. #define HIJACK_UNLOCK spin_unlock_irqrestore(&hijack_lock, sl_flags)

  18. unsigned int (*orig_nf_iterate)(struct list_head *head,
  19.                         struct sk_buff *skb,
  20.                         unsigned int hook,
  21.                         const struct net_device *indev,
  22.                         const struct net_device *outdev,
  23.                         struct list_head **i,
  24.                         int (*okfn)(struct sk_buff *),
  25.                         int hook_thresh)=0xc07ac3a0;


  26. unsigned int my_nf_iterate(struct list_head *head,
  27.                         struct sk_buff *skb,
  28.                         unsigned int hook,
  29.                         const struct net_device *indev,
  30.                         const struct net_device *outdev,
  31.                         struct list_head **i,
  32.                         int (*okfn)(struct sk_buff *),
  33.                         int hook_thresh){
  34.     return NF_ACCEPT;
  35. }

  36. int init_module(void){
  37.     int sl_flags;

  38.     *(unsigned int *)(my_code + 1) = (unsigned int)my_nf_iterate;
  39.     HIJACK_LOCK;

  40.     memcpy(orig_code, (char *)orig_nf_iterate, CODESIZE);
  41.     memcpy((char *)orig_nf_iterate, my_code, CODESIZE);

  42.     HIJACK_UNLOCK;
  43.     return 0;
  44. }

  45. void cleanup_module(){
  46.     int sl_flags;

  47.     lock_kernel();
  48.     HIJACK_LOCK;

  49.     memcpy((char *)orig_nf_iterate, orig_code, CODESIZE);

  50.     HIJACK_UNLOCK;
  51.     unlock_kernel();
  52. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-04-21 17:42 |只看该作者
  1. 可是32位系统?
  2. code本身好像没有问题
  3. # as --32 aa.s -o aa
  4. # objdump -d aa

  5. aa:     file format elf32-i386


  6. Disassembly of section .text:

  7. 00000000 <.text>:
  8.    0:   b8 00 00 00 00          mov    $0x0,%eax
  9.    5:   40                      inc    %eax
  10.    6:   90                      nop
  11.    7:   48                      dec    %eax
  12.    8:   ff e0                   jmp    *%eax
复制代码
只是 memcpy((char *)orig_nf_iterate, my_code, CODESIZE); 这句话中

orig_nf_iterate所在的位置是是只读?


Linux的内核有代码段ro的功能

论坛徽章:
0
3 [报告]
发表于 2012-04-23 16:26 |只看该作者
回复 2# sanbiangongzi


   

确实。。

很疑惑为什么这个文章里没有提到代码段可读的问题。。 http://www.ibm.com/developerworks/cn/linux/l-knldebug/index.html

请教一下,能强制写入代码段吗?

论坛徽章:
0
4 [报告]
发表于 2012-04-23 16:28 |只看该作者
回复 2# sanbiangongzi


    确实。。很疑惑这篇文章为什么完全没有提到代码段能不能写入的问题http://www.ibm.com/developerworks/cn/linux/l-knldebug/index.html

    请教一下,有什么办法可以写入呢?

论坛徽章:
0
5 [报告]
发表于 2012-04-23 16:41 |只看该作者
他用的是2.4的内核,可能当时的内核中没有对代码段做保护吧

我用的2.6.32中
init_post >>  mark_rodata_ro >> set_memory_ro 实现只读功能

你应该可以调用 set_memory_rw来修改代码段为可写

论坛徽章:
0
6 [报告]
发表于 2012-04-24 10:06 |只看该作者
貌似需要修改一下cr0还是那个寄存器,网上自己找一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP