今天在Linux kernel 2.6.30.5内核上进行了一个简单的Netfilter例子,记录下来“以 飨 读者 ” ,哈哈!
模块的功能是丢弃所有的进入内核的IP数据包,够邪恶吧?!嘿嘿。
1、首先,保证您在/usr/src/ 目录下有和当前内核版本一致 的内核源码!并建立有符合连接“linux”
ln -s /usr/src/linux-2.6.30.5/ /usr/src/linux
2、源文件:dropAll.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
MODULE_LICENSE("GPL");
/* This is the structure we shall use to register our function */
static struct nf_hook_ops nfho;
/* This is the hook function itself */
unsigned 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 *))
{
return NF_DROP; /* Drop ALL packets */
}
/* Initialisation routine */
int init_module()
{
/* Fill in our hook structure */
nfho.hook = hook_func; /* Handler function */
nfho.hooknum = NF_INET_PRE_ROUTING; /* First hook for IPv4 */
nfho.pf = PF_INET;
nfho.priority = NF_IP_PRI_FIRST; /* Make our function first */
nf_register_hook(&nfho);
pr_info("dropAll install into kernel!/n");
return 0;
}
/* Cleanup routine */
void cleanup_module()
{
nf_unregister_hook(&nfho);
pr_info("dropAll removed from kernel!/n");
}
3、Makfile文件
obj-m +=dropAll.o
all:
make -C /lib/modules/`uname -r`/build M=`pwd`
clean:
make -C /lib/modules/`uname -r`/build M=`pwd` cleaninstall:
/sbin/insmod dropAll.ko
remove:
/sbin/rmmod dropAll
4、编译模块
make
5、安装模块
make install
6、测试
ping 127.0.0.1
可以发现,ping不通,哈哈!
7、卸载模块
make remove
8、清理
make clean
9、注意 :使用SSH登录目标机器的谨慎使用,因为连接会断开的!
10、下载 http://download.csdn.net/source/1651542