| « | November 2025 | » |  | 日 | 一 | 二 | 三 | 四 | 五 | 六 |   |  |  |  |  |  | 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |  |  |  |  |  |  |  |  
  |    公告 |  
戒除浮躁,读好书,交益友  |    
 
 
 
 
 
| Blog信息 |  
| 
 blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9731161 建立时间:2004年12月20日   |   
 
  
 
  |    | 
  
| 
  [linux kernel]一个完整的netfilter的hook的例子 原创空间,  文章收藏,  软件技术,  电脑与网络 
邢红瑞 发表于 2010/2/25 11:42:21   |  
|  这个例子 就是阻断以后的网络数据包//'Hello World' netfilter hooks example//For any packet, we drop it, and log fact to /var/log/messages
#include <linux/kernel.h>#include <linux/module.h>#include <linux/netfilter.h>#include <linux/netfilter_ipv4.h>
static struct nf_hook_ops nfho;         //struct holding set of hook function options
//function to be called by hookunsigned int hook_func(unsigned int hooknum, struct sk_buff **skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *)){  printk(KERN_INFO "packet dropped\n");                                             //log to var/log/messages  return NF_DROP;                                                                   //drops the packet}
//Called when module loaded using 'insmod'int init_module(){  nfho.hook = hook_func;                       //function to call when conditions below met  nfho.hooknum = NF_IP_PRE_ROUTING;            //called right after packet recieved, first hook in Netfilter  nfho.pf = PF_INET;                           //IPV4 packets  nfho.priority = NF_IP_PRI_FIRST;             //set to highest priority over all other hook functions  nf_register_hook(&nfho);                     //register hook
  return 0;                                    //return 0 for success}
//Called when module unloaded using 'rmmod'void cleanup_module(){  nf_unregister_hook(&nfho);                     //cleanup – unregister hook}Makefile
obj-m := hello.oKDIR := /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd)default: $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules编译命令 make -C /usr/src/kernels/2.6.9-67.EL-i686/ SUBDIRS=$PWD modules加载模块 insmod hello.ko此时网络已经断开,到控制台 输入 lsmod |grep hellormmod hello.kotail /var/log/messages |    
 |   
   
| 
 回复:一个完整的netfilter的hook的例子 原创空间,  文章收藏,  软件技术,  电脑与网络 
cjcj发表评论于2010/2/28 1:16:22   |  
| 偶毕业设计就是做的类似这个,底层就是这样做的,上层写了一个JAVA的app,http://c-j.javaeye.com/blog/364552 |    
 |   
 
  »  1 »  
  |