LinuxÒÁµéÔ°ÂÛ̳'s Archiver

handy2139 ·¢±íÓÚ 2006-4-21 11:29

Çó½Ì£¬ÔõôÔÚlinuxÏÂʵÏÖÊý¾Ý°üµÄÀ¹½Ø£¿

СµÜµÄ±ÏÒµÉè¼ÆÊÇ×ö¸ö°ü¹ýÂË·À»ðǽ£¬ÒòΪÒÔǰûѧ¹ýlinux£¬ËùÒԺܲˡ£ÇëÎʸ÷λÀÏ´óÔõô²ÅÄܰÑÊý¾Ý°üÀ¹½ØÔڵײ㣿ÊDz»ÊÇ¿ÉÒÔÓù³×Óº¯Êý£¨ÀÏʦ½ÐÎÒÓÃÕâ¸ö£¬²»»áÓã©£¿»òÕß±ðµÄʲô·½·¨£¿
°ÝÍи÷λÁË£¬¿ìµã´ð¸´°É¡£

handy2139 ·¢±íÓÚ 2006-4-21 16:26

×Ô¼º¶¥£¬µÈ´ý¸ßÊÖing

handy2139 ·¢±íÓÚ 2006-4-22 01:22

ûÈË»á×ö£¿²»¿ÉÄܰɣ¿
ÕÙ»½°ßÖñ¡£

Roc.Ken ·¢±íÓÚ 2006-4-22 01:35

Óù³×Óº¯Êý£¬²Î¿¼ netfilter/iptables µÄʵÏÖÔ­Àí£¬
[url]http://www.netfilter.org/documentation/index.html#documentation-howto[/url]

handy2139 ·¢±íÓÚ 2006-4-23 09:09

°ßÖñ¹ûÈ»±»ÎÒÕÙ»½³öÀ´ÁË¡£¹»ÅóÓѰ¡£¬¿ÉϧӢÎIJ»ºÃ£¬ÄÜÍÆ¼ö¸öÖÐÎĵIJ»£¿

Roc.Ken ·¢±íÓÚ 2006-4-23 22:22

ÕâÀïµÄһƪ¾Í²»´íŶ£º [url]http://www.linuxeden.com/forum/t142918.html[/url]

handy2139 ·¢±íÓÚ 2006-4-24 11:54

ллÁË¡£EdenµÄ°ßÖñÕæµÄ²»´í¡£

handy2139 ·¢±íÓÚ 2006-5-5 22:11

ʹÓÃnetfilter/iptablesÊDz»ÊÇÒªÉèÖÃʲô¶«Î÷£¿ÎÒ¶Ô×ÅÊé´òµÄ´úÂë¶¼±àÒë²»¹ý¡£

Roc.Ken ·¢±íÓÚ 2006-5-6 20:08

ÊéÉϵĴúÂëÒ»°ã±È½ÏÀÏ,ÔÚÐÂϵͳÏÂÈÝÒ׳ö´í,  ÓÃgoogleËÑË÷Ò»ÏÂ,Ò»°ã¿ÉÒÔÕÒµ½´íÎóÔ­Òò

handy2139 ·¢±íÓÚ 2006-5-6 22:20

ллÄ㣬ÎÒ»¹ÊÇÒª×Ô¼º¿´Ï¶«Î÷¡£Ê¡µÃÎÊÈõÖÇÎÊÌ⣬ÈÇÈË·³Ôê¡£
²»¹ýÓÐʲôºÃ¶«Î÷£¬ºÃ×ÊÁÏ£¬°ÝÍÐÍÆ¼ö¸øÎÒ¿´¿´¡£±Ï¾¹ÎÒÊÇһС²ËÄñÈö¡£

Roc.Ken ·¢±íÓÚ 2006-5-7 12:57

ÎÒûÓÐÕâ·½ÃæµÄ×ÊÁÏ, ¶àÓÃÒ»ÏÂËÑË÷ÒýÇæ, ÓÃgoogleÄÜÕÒµ½²»´íµÄ×ÊÁÏ:

[url]http://www.google.com/search?hl=zh-CN&newwindow=1&q=packet+catch+hold+netfilter&btnG=%E6%90%9C%E7%B4%A2&lr=[/url]

[url]http://www.google.com/search?q=packet+catch+hold+netfilter+filetype%3Apdf&btnG=%E6%90%9C%E7%B4%A2&hl=zh-CN&newwindow=1[/url]

handy2139 ·¢±íÓÚ 2006-5-8 13:30

ÎÒ·¢ÊÄÒ»¶¨ÒªÑ§ºÃEÎÄ£¬Ê¡µÄ¿´²»¶®Íâ¹ú×ÊÁÏ¡££¡£¡£¡

handy2139 ·¢±íÓÚ 2006-5-9 19:04

ÕâÊÇÒ»¸öÅóÓѸøÎҵĴúÂ룬Ëû˵¿ÉÒÔÔËÐеġ£¿ÉÊÇΪʲôÔÚÎÒ»ú×ÓÒª±àÒ붼ͨ²»¹ýÄØ£¿
ÊDz»ÊÇÔËÐÐ֮ǰ»¹ÒªÓÐʲôԤ´¦ÀíÃüÁ

[php]
/*
* filter.c will display all tcp packet information on the screen
*
* gcc -O2 -I /usr/src/linux-2.4.20-8/include/ -c  filter.c
* insmod filter.o
*
* Author interstar
*
*/

#ifndef __KERNEL__
# define __KERNEL__
#endif
#ifndef MODULE
# define MODULE
#endif

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netdevice.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <net/tcp.h>

MODULE_LICENSE("GPL");

char *inet_ntoa(__u32 ina)
{
    static char buf[4 * sizeof "123"];
    unsigned char *ucp = (unsigned char *)&ina;
    sprintf(buf, "%d.%d.%d.%d",
    ucp[0] & 0xff,
    ucp[1] & 0xff,
    ucp[2] & 0xff,
    ucp[3] & 0xff);
    return buf;
}


static unsigned int local_input (unsigned int hooknum, struct sk_buff **pskb,
        const struct net_device *in, const struct net_device *out,
        int (*okfn) (struct sk_buff *))
{
  struct tcphdr *tcph;
  struct iphdr *iph;
  struct sk_buff *skb = *pskb;
  __u32 odaddr;
  __u32 osaddr;
  __u16 odport;
  __u16 osport;
  printk("Local Input Called!\n");
  printk("Input  Device:%s\n",in->name);
  printk("Output Device:%s\n",out->name);
  if (skb->protocol == htons (ETH_P_IP))
    {
      iph = skb->nh.iph;
      if (iph->protocol == IPPROTO_TCP)
      {
      tcph = (struct tcphdr *)((__u32 *)iph+iph->ihl);
      osaddr = iph->saddr;
      odaddr = iph->daddr;
      odport=ntohs(tcph->dest);
      osport=ntohs(tcph->source);
      printk("From:%-16s  Port:%u\n",inet_ntoa(odaddr),osport);
      printk("To:  %-16s  Port:%u\n",inet_ntoa(osaddr),odport);
      printk("fin:%u,syn:%u,ack:%u\n",tcph->fin,tcph->syn,tcph->ack);
      }
    }
  return NF_ACCEPT;
}

static unsigned int local_output (unsigned int hooknum, struct sk_buff **pskb,
          const struct net_device *in, const struct net_device *out,
          int (*okfn) (struct sk_buff *))
{
  struct tcphdr *tcph;
  struct iphdr *iph;
  struct sk_buff *skb = *pskb;
  __u32 odaddr;
  __u32 osaddr;
  __u16 odport;
  __u16 osport;
  printk("Local Output Called!\n");
  printk("Input  Device:%s\n",in->name);
  printk("Output Device:%s\n",out->name);
  if (skb->protocol == htons (ETH_P_IP))
    {
      iph = skb->nh.iph;
      if (iph->protocol == IPPROTO_TCP)
      {
      tcph = (struct tcphdr *)((__u32 *)iph+iph->ihl);
      osaddr = iph->saddr;
      odaddr = iph->daddr;
      odport=ntohs(tcph->dest);
      osport=ntohs(tcph->source);
      printk("From:%-16s  Port:%u\n",inet_ntoa(odaddr),osport);
      printk("To:  %-16s  Port:%u\n",inet_ntoa(osaddr),odport);
      printk("fin:%u,syn:%u,ack:%u\n",tcph->fin,tcph->syn,tcph->ack);
      }
    }
  return NF_ACCEPT;
}


static unsigned int forward_filter (unsigned int hooknum, struct sk_buff **pskb,
          const struct net_device *in, const struct net_device *out,
          int (*okfn) (struct sk_buff *))
{
  struct tcphdr *tcph;
  struct iphdr *iph;
  struct sk_buff *skb = *pskb;
  __u32 odaddr;
  __u32 osaddr;
  __u16 odport;
  __u16 osport;
  printk("Forword Called!\n");
  printk("Input  Device:%s\n",in->name);
  printk("Output Device:%s\n",out->name);
  if (skb->protocol == htons (ETH_P_IP))
    {
      iph = skb->nh.iph;
      if (iph->protocol == IPPROTO_TCP)
      {
      tcph = (struct tcphdr *)((__u32 *)iph+iph->ihl);
      osaddr = iph->saddr;
      odaddr = iph->daddr;
      odport=ntohs(tcph->dest);
      osport=ntohs(tcph->source);
      printk("From:%-16s  Port:%u\n",inet_ntoa(odaddr),osport);
      printk("To:  %-16s  Port:%u\n",inet_ntoa(osaddr),odport);
      printk("fin:%u,syn:%u,ack:%u\n",tcph->fin,tcph->syn,tcph->ack);
      }
    }
  return NF_ACCEPT;
}

static struct nf_hook_ops input_filter =
  {
    {NULL, NULL},
    local_input,
    AF_INET,
    NF_IP_LOCAL_IN,
    NF_IP_PRI_FILTER - 1
  };

static struct nf_hook_ops output_filter =
  {
    {NULL, NULL},
    local_output,
    AF_INET,
    NF_IP_LOCAL_OUT,
    NF_IP_PRI_FILTER - 1
  };


static struct nf_hook_ops forward =
  {
    {NULL, NULL},
    forward_filter,
    AF_INET,
    NF_IP_FORWARD,
    NF_IP_PRI_FILTER - 1
  };

//packet flow diagram:
//
//-NF_IP_PRE_ROUTING---| ROUTING PLOCY |---NF_IP_FORWARD---NF_IP_POST_ROUTING
//                            |                                |
//                            |                                |      
//                            |                                |
//                      NF_IP_LOCAL_IN                  NF_IP_LOCAL_OUT
//                            |                                |
//                            |__________|PROCESSING|___________|
//
//ip_build_and_send_pkt,ip_queue_xmit ip_build_xmit_slow,ip_build_xmit


int init_module (void)
{
  printk ("Load Netfilter Module \n");
  if (nf_register_hook (&input_filter) || nf_register_hook (&output_filter) || nf_register_hook (&forward))
    return 1;
  else
    return 0;
}

void cleanup_module (void)
{
  nf_unregister_hook (&input_filter);
  nf_unregister_hook (&output_filter);
  nf_unregister_hook (&forward);
  printk ("UnLoad Netfilter Module\n");
  return;
}
[/php]

Roc.Ken ·¢±íÓÚ 2006-5-9 23:36

²âÊÔ¹ý,ûÓÐÎÊÌâ; ÐèÒªÄÚºËÔ´´úÂë, ×¢Òâ·¾¶:

gcc -O2 -I /usr/src/linux-2.4.20-8/include/ -c  filter.c

handy2139 ·¢±íÓÚ 2006-5-10 19:53

°ßÖñ£¬ÄÚºËÔ´´úÂëÎÒºÃÏóÒѾ­¼ÓÁË¡£»¹ÊDz»Ðа¡¡£

Roc.Ken ·¢±íÓÚ 2006-5-11 09:03

ÔÚʲôϵͳϱàÒëµÄ? ϵͳ°æ±¾, ÄÚºË, gccµÄ°æ±¾
ÎÒÔÚredhat9ÏÂͨ¹ýÁË, 2.4.28µÄÄÚºË

handy2139 ·¢±íÓÚ 2006-5-12 20:03

°ßÖñ£¬Áô¸öQQ¸øÎÒ°É¡£»òÕß¼Ó17514662

Roc.Ken ·¢±íÓÚ 2006-5-13 14:38

ºÜÉÙÓÃQQÁË, ÓÐÎÊÌâÔÚÂÛ̳Ìá³öÀ´°É.

handy2139 ·¢±íÓÚ 2006-5-13 22:44

#define __KERNEL__
#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
/* ÓÃÓÚ×¢²áÎÒÃǵĺ¯ÊýµÄÊý¾Ý½á¹¹ */
static struct nf_hook_ops nfho;

/* ÎÒÃǶªÆúµÄÊý¾Ý°üÀ´×ԵĽӿڵÄÃû×Ö */
static char *drop_if = "lo";

/* ×¢²áµÄhookº¯ÊýµÄʵÏÖ */
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 *))
{
    if (strcmp(in->name, drop_if) == 0) {
        printk("Dropped packet on %s...\n", drop_if);
        return NF_DROP;
    } else {
        return NF_ACCEPT;
    }
}

/* ³õʼ»¯³ÌÐò */
int init_module()
{
    /* Ìî³äÎÒÃǵÄhookÊý¾Ý½á¹¹ */
    nfho.hook     = hook_func;         /* ´¦Àíº¯Êý */
    nfho.hooknum  = NF_IP_PRE_ROUTING; /* ʹÓÃIPv4µÄµÚÒ»¸öhook */
    nfho.pf       = PF_INET;
    nfho.priority = NF_IP_PRI_FIRST;   /* ÈÃÎÒÃǵĺ¯ÊýÊ×ÏÈÖ´ÐÐ */

    nf_register_hook(&nfho);
   
    return 0;
}
   
/* Çå³ý³ÌÐò */
void cleanup_module()
{
    nf_unregister_hook(&nfho);
}
Äã°ïÎÒ¿´¿´£¬ÎÒ×Ô¼ºÑо¿Á˺þã¬Õâ¶Î´úÂëÊÇûÓдíµÄ£¬¿ÉÊÇÓ²ÊDZàÒëͨ²»¹ý£¡
ÒÔǰÓÃC±à³Ì£¬×ÜÊÇÓиömain£¨£©º¯Êý×öÈë¿Ú£¬ÏÖÔÚÕâÑùµÄ³ÌÐò¶¼²»ÖªµÀÔõôÈëÊÖ£¬Ïë±àдһ¸öprintkº¯ÊýÀ´ÉèÖÃÎÒÒªÖ¸¶¨µÄ¶Ë¿ÚºÅ£¬¶¼²»ÖªµÀ·ÅÔÚʲôλÖá£

handy2139 ·¢±íÓÚ 2006-5-19 20:50

°ßÖñÔÙÏÖÏÂÉí¿©

Ò³: [1] 2

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.