Çó½Ì£¬ÔõôÔÚlinuxÏÂʵÏÖÊý¾Ý°üµÄÀ¹½Ø£¿
СµÜµÄ±ÏÒµÉè¼ÆÊÇ×ö¸ö°ü¹ýÂË·À»ðǽ£¬ÒòΪÒÔǰûѧ¹ýlinux£¬ËùÒԺܲˡ£ÇëÎʸ÷λÀÏ´óÔõô²ÅÄܰÑÊý¾Ý°üÀ¹½ØÔڵײ㣿ÊDz»ÊÇ¿ÉÒÔÓù³×Óº¯Êý£¨ÀÏʦ½ÐÎÒÓÃÕâ¸ö£¬²»»áÓã©£¿»òÕß±ðµÄʲô·½·¨£¿°ÝÍи÷λÁË£¬¿ìµã´ð¸´°É¡£ ×Ô¼º¶¥£¬µÈ´ý¸ßÊÖing ûÈË»á×ö£¿²»¿ÉÄܰɣ¿
ÕÙ»½°ßÖñ¡£ Óù³×Óº¯Êý£¬²Î¿¼ netfilter/iptables µÄʵÏÖÔÀí£¬
[url]http://www.netfilter.org/documentation/index.html#documentation-howto[/url] °ßÖñ¹ûÈ»±»ÎÒÕÙ»½³öÀ´ÁË¡£¹»ÅóÓѰ¡£¬¿ÉϧӢÎIJ»ºÃ£¬ÄÜÍÆ¼ö¸öÖÐÎĵIJ»£¿ ÕâÀïµÄһƪ¾Í²»´íŶ£º [url]http://www.linuxeden.com/forum/t142918.html[/url] ллÁË¡£EdenµÄ°ßÖñÕæµÄ²»´í¡£ ʹÓÃnetfilter/iptablesÊDz»ÊÇÒªÉèÖÃʲô¶«Î÷£¿ÎÒ¶Ô×ÅÊé´òµÄ´úÂë¶¼±àÒë²»¹ý¡£ ÊéÉϵĴúÂëÒ»°ã±È½ÏÀÏ,ÔÚÐÂϵͳÏÂÈÝÒ׳ö´í, ÓÃgoogleËÑË÷Ò»ÏÂ,Ò»°ã¿ÉÒÔÕÒµ½´íÎóÔÒò ллÄ㣬ÎÒ»¹ÊÇÒª×Ô¼º¿´Ï¶«Î÷¡£Ê¡µÃÎÊÈõÖÇÎÊÌ⣬ÈÇÈË·³Ôê¡£
²»¹ýÓÐʲôºÃ¶«Î÷£¬ºÃ×ÊÁÏ£¬°ÝÍÐÍÆ¼ö¸øÎÒ¿´¿´¡£±Ï¾¹ÎÒÊÇһС²ËÄñÈö¡£ ÎÒûÓÐÕâ·½ÃæµÄ×ÊÁÏ, ¶àÓÃÒ»ÏÂËÑË÷ÒýÇæ, ÓÃ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] ÎÒ·¢ÊÄÒ»¶¨ÒªÑ§ºÃEÎÄ£¬Ê¡µÄ¿´²»¶®Íâ¹ú×ÊÁÏ¡££¡£¡£¡ ÕâÊÇÒ»¸öÅóÓѸøÎҵĴúÂ룬Ëû˵¿ÉÒÔÔËÐеġ£¿ÉÊÇΪʲôÔÚÎÒ»ú×ÓÒª±àÒ붼ͨ²»¹ýÄØ£¿
Ê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] ²âÊÔ¹ý,ûÓÐÎÊÌâ; ÐèÒªÄÚºËÔ´´úÂë, ×¢Òâ·¾¶:
gcc -O2 -I /usr/src/linux-2.4.20-8/include/ -c filter.c °ßÖñ£¬ÄÚºËÔ´´úÂëÎÒºÃÏóÒѾ¼ÓÁË¡£»¹ÊDz»Ðа¡¡£ ÔÚʲôϵͳϱàÒëµÄ? ϵͳ°æ±¾, ÄÚºË, gccµÄ°æ±¾
ÎÒÔÚredhat9ÏÂͨ¹ýÁË, 2.4.28µÄÄÚºË °ßÖñ£¬Áô¸öQQ¸øÎÒ°É¡£»òÕß¼Ó17514662 ºÜÉÙÓÃQQÁË, ÓÐÎÊÌâÔÚÂÛ̳Ìá³öÀ´°É. #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º¯ÊýÀ´ÉèÖÃÎÒÒªÖ¸¶¨µÄ¶Ë¿ÚºÅ£¬¶¼²»ÖªµÀ·ÅÔÚʲôλÖᣠ°ßÖñÔÙÏÖÏÂÉí¿©
Ò³:
[1]
2