2.6.30内核Netfilter的简单例子、一(DropAll)

66 篇文章 0 订阅

 

今天在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` clean

install:
        /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

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值