一个基于LINUX的简单防火墙


 

  1. // firewall.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #define _KERNEL_
  4. #define MODULE
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/version.h>
  8. #include <linux/netfilter.h>
  9. #include <linux/netfilter_ipv4.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/if_packet.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/ip.h>
  14. #include <linux/icmp.h>
  15. #include <linux/in.h>
  16. #include <linux/tcp.h>
  17. #include <linux/netdevice.h>
  18. static struct nf_hook_ops nfho;
  19. static unsigned char *drop_ip="/x7f/x00/x00/x01";
  20. 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 *))
  21. {
  22.     struct sk_buff *sb=*skb;
  23.     struct icmphdr *icmp;
  24.     if(sb->nh.iph->protocol!=IPPROTO_ICMP)
  25.         return NF_ACCEPT;
  26.     icmp=(struct icmphdr *)(sb->data + sb->nh.iph->ihl*4);
  27.  //       if(icmp->code !=MAGIC_CODE || icmp->type!=ICMP_ECHO||ICMP_PAYLOAD_SIZE<REPLY_SIZE){
  28. //      return NF_ACCEPT;
  29. //  }
  30.     printk("trying!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/n");
  31.         if(icmp->type == ICMP_ECHO)
  32.         {
  33.             unsigned char *saddr = &(sb->nh.iph->saddr);
  34.             printk("<1>---ping from:%d.%d.%d.%d ---/n",*saddr,*(saddr+1),*(saddr+2),*(saddr+3));
  35.               sb->h.icmph->checksum+=1;
  36.             return NF_DROP;
  37.         }
  38.              if(icmp->type == ICMP_ECHOREPLY){
  39.             unsigned char *saddr = &(sb->nh.iph->saddr);
  40.             printk("<1>---ping reply from:%d.%d.%d.%d ---/n",*saddr,*(saddr+1),*(saddr+2),*(saddr+3));
  41.             sb->h.icmph->checksum+=1;
  42.             return NF_ACCEPT;
  43.         }
  44. return NF_DROP;
  45. }
  46. int init_module()
  47. {
  48. printk("simple firewall by zsh/n");
  49. nfho.hook    = hook_func;
  50. nfho.hooknum = NF_IP_PRE_ROUTING;
  51. nfho.pf      = PF_INET;
  52. nfho.priority= NF_IP_PRI_FIRST;
  53. nf_register_hook(&nfho);
  54. return 0;
  55. }
  56. void cleanup_module()
  57. {
  58.     nf_unregister_hook(&nfho);
  59. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值