免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4637 | 回复: 12
打印 上一主题 下一主题

netfilter中的端口过滤疑问(讨论) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-13 08:31 |只看该作者 |倒序浏览
这两天学了netfilter 做了个关闭web端口的程序,但是程序运行起来还是没关到.于是我一直不断的修改程序,也找了不少资料,但是无功而返.是我做错了,还是netfilter根本就不能过滤端口(很怀疑).欢迎大家来讨论指出
#ifndef __KERNEL__
#define __KERNEL__
#endif

#ifndef MODULE
#define MODULE
#endif
#include <linux/module.h>         
#include <linux/sched.h>  
#include <linux/kernel.h>         
#include <linux/netdevice.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/skbuff.h>
#include <linux/proc_fs.h>
#include <linux/if.h>
#include <linux/in.h>

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/netfilter_ipv4.h>
#include <linux/ip.h>


#define PERMIT_PORT 80

static unsigned int kill_port(unsigned int hooknum ,struct sk_buff **skb,
                                 const struct net_device *in ,
                                 const struct net_device *out,
                                 int(*okfn)(struct sk_buff*))
{  
               struct tcphdr *tcph;
               struct iphdr *iph;
               if(iph->protocol==IPPROTO_TCP){
                   tcph=(*skb)->h.th;
                 if(ntohs(tcph->dest)==80){
                  printk("\nweb service refused");
                return NF_DROP;
                }
                   }

return NF_ACCEPT;
}
static struct nf_hook_ops kill=
{
        {NULL,NULL},
        kill_port,
        PF_INET,
        NF_IP_LOCAL_IN,
        NF_IP_PRI_FILTER -1
};
            

int init_module()
{
            return nf_register_hook(&kill);
}
void cleanup_module()
{
           printk("over");
           nf_unregister_hook(&kill);
}

论坛徽章:
0
2 [报告]
发表于 2007-05-13 08:34 |只看该作者
顶一个,希望大家能让我死个明白.不能不明不白的栽在netfilter手上     谢谢各位,欢迎讨论

论坛徽章:
0
3 [报告]
发表于 2007-05-13 08:36 |只看该作者

修改一下

if(ntohs(tcph->dest)==80)
改为
if(tcph->dest==80)手误
呵呵,不过改了还是没效果

论坛徽章:
0
4 [报告]
发表于 2007-05-13 09:53 |只看该作者
看了LZ的 这段代码  struct iphdr *iph; 未经赋值就直接使用了 问题不少

自己写了个禁止 网页浏览程序 如果是web服务器 把目的端口该为源即可

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>

struct nf_hook_ops nfkiller;
//static unsigned short deny_port = 0x5000;
unsigned char *deny_port = "\x00\x50";

unsigned int lwfw_hookfn(unsigned int hooknum,
                       struct sk_buff **skb,
                       const struct net_device *in,
                       const struct net_device *out,
                       int (*okfn)(struct sk_buff *))
{
   struct sk_buff *sk = *skb;

   if (!sk ) return NF_ACCEPT;
   if (!(sk->nh.iph)) return NF_ACCEPT;

   if (sk->nh.iph->protocol == IPPROTO_TCP) {
      struct tcphdr *thead = (struct tcphdr *)(sk->data + (sk->nh.iph->ihl * 4));
      if ((thead->source) == *(unsigned short *)deny_port)
         return NF_DROP;
   }
   return NF_ACCEPT;
}

int XC_init(void)
{
        /* Now register the network hooks */
        nfkiller.hook = lwfw_hookfn;
        nfkiller.hooknum = NF_IP_PRE_ROUTING;   /* First stage hook */
        nfkiller.pf = PF_INET;                   /* IPV4 protocol hook */
        nfkiller.priority = NF_IP_PRI_FIRST;    /* Hook to come first */
        nf_register_hook(&nfkiller);
        return 0;
}

void XC_exit(void)
{
        nf_unregister_hook(&nfkiller);
}

module_init(XC_init);
module_exit(XC_exit);

论坛徽章:
0
5 [报告]
发表于 2007-05-13 11:06 |只看该作者
楼上正解,看了你的代码,我学到了很多,我会继续努力的.我现在才发现我自己把它想的太简单,谢谢
做个朋友吗
资料在邮箱里哈

论坛徽章:
0
6 [报告]
发表于 2007-05-13 11:23 |只看该作者
LZ初学的话看看这篇文章

http://linux.chinaunix.net/bbs/v ... p;extra=&page=1

我也是最近才开始学的 以后可以交流。。。

论坛徽章:
0
7 [报告]
发表于 2007-05-13 13:31 |只看该作者
楼上学了好久了?你发的链接文章我看了.不错很有帮助.网上这种资料很少了 ```(以前一直没找到什么资料)

论坛徽章:
0
8 [报告]
发表于 2007-05-13 13:38 |只看该作者
struct tcphdr *thead = (struct tcphdr *)(sk->data + (sk->nh.iph->ihl * 4));
这句关键的地方没看懂!可以解释一下啊吗?

论坛徽章:
0
9 [报告]
发表于 2007-05-13 17:02 |只看该作者
那篇文章上 没写吗 我记的有写过的

因为这个钩子函数是在 ip_rcv 调用
return NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, dev, NULL,ip_rcv_finish);
此时 h和nh都是指向IP头

论坛徽章:
0
10 [报告]
发表于 2007-05-13 20:46 |只看该作者
原帖由 loveskyer 于 2007-5-13 13:38 发表于 8楼  
struct tcphdr *thead = (struct tcphdr *)(sk->data + (sk->nh.iph->ihl * 4));
这句关键的地方没看懂!可以解释一下啊吗?

这句就是用来跳过ip头得到tcp头吧。
正好这两天做的东西里有个HTTP重定向的部分,是直接写在ip_nat_fn函数里的,也现个丑贴下代码好了:

  1. static unsigned int
  2. ip_nat_fn(unsigned int hooknum,
  3.           struct sk_buff **pskb,
  4.           const struct net_device *in,
  5.           const struct net_device *out,
  6.           int (*okfn)(struct sk_buff *))
  7. {
  8. .............
  9.                 IP_NF_ASSERT(info);
  10.                 if(((*pskb)->nh.iph->protocol ==IPPROTO_TCP))
  11.                 {                       
  12.                         struct tcphdr *thead=(struct tcphdr *)((*pskb)->data +((*pskb)->nh.iph->ihl * 4));
  13.                         __u16 desport=ntohs(thead->dest);
  14.                         struct ip_nat_multi_range mr;
  15.                         switch (ctinfo) {
  16.                                 case IP_CT_NEW:
  17.                                 case IP_CT_ESTABLISHED:
  18.                                        
  19.                                         if (desport == 80)
  20.                                         {
  21.                                                 __u32 lanip=0;
  22.                                                 struct in_device *in_dev;
  23.                                                 rcu_read_lock();
  24.                                                 in_dev = __in_dev_get(in);
  25.                                                 if (in_dev){
  26. //                                                        __u8 ip[4]={NIPQUAD((*pskb)->nh.iph->saddr)};//get src address number
  27.                                                         struct in_ifaddr *ifa;
  28.                                                        //获取网关地址,这段是从inet_select_addr函数里抄的一段,从入口设备获取网关地址
  29.                                                         for (ifa = (in_dev)->ifa_list; ifa && !(ifa->ifa_flags&IFA_F_SECONDARY); ifa = ifa->ifa_next)
  30.                                                         {
  31.                                                                 if (ifa->ifa_scope > RT_SCOPE_UNIVERSE)
  32.                                                                         continue;
  33.                                                                 lanip=ifa->ifa_local;
  34.                                                                 break;
  35.                                                         }
  36.                                                         //比较目的地址是否为网关,如果不是重定向回网关
  37.                                                         if(!inet_ifa_match((*pskb)->nh.iph->daddr,ifa))
  38.                                                         {
  39.                                                                 unsigned int ret;
  40.                                                                 mr.rangesize = 1;
  41.                                                                 mr.range[0].flags = IP_NAT_RANGE_MAP_IPS;
  42.                                                                 mr.range[0].min_ip = mr.range[0].max_ip = lanip;

  43.                                                                 if((ret=ip_nat_setup_info(ct, &mr, hooknum))!=NF_ACCEPT)
  44.                                                                         return ret;
  45.                                                                 DEBUGP("http redirect change to  %u,%u,%u,%u \n",NIPQUAD(lanip));
  46.                                                         }
  47.                                                 }
  48.                                                 rcu_read_unlock();
  49.                                         }       
  50.                                        
  51.                                         break;       
  52.                         }
  53.                 }
  54.         return do_bindings(ct, ctinfo, info, hooknum, pskb);
  55. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP