本文基于内核版本2.6.23.1
Netfilter 是由内核 2.4.x 和 2.6.x 提供的数据包截获机制,不过要捕获所有局域网中的数据包,首先必须完成两项工作
1.开启网卡的混沌模式
ifconfig eth0 promisc
如果需要设置网卡eth0为非混沌模式ifconfig eth0 -promisc
2.修改linux内核的源代码
1.修改ipv4 接受包的函数
在linux内核源代码树中修改net/ipv4/ip_input.c中函数ip_rcv
int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
{
struct iphdr *iph;
u32 len;
/* When the interface is in promisc. mode, drop all the crap
* that it receives, do not try to analyse it.
*/
if (skb->pkt_type == PACKET_OTHERHOST)
goto drop;
IP_INC_STATS_BH(IPSTATS_MIB_INRECEIVES);
if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
IP_INC_STATS_BH(IPSTATS_MIB_INDISCARDS);
goto out;
…………………………
修改为
/*
* symbol if the catch all packets.
* if wood_promisc is 0 , just catch the packets which dest address is
* local address.
* Else if wood_promisc is null 0 , then catch all packets.
*/
int wood_promisc=0;//wood_promisc标记是否抓取所有经过网卡的包
//可以通过其他内核模块或其他途径来修改此标识
EXPORT_SYMBOL(wood_promisc);
/*
* Main IP Receive routine.
*/
int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
ifconfig eth0 promisc
如果需要设置网卡eth0为非混沌模式ifconfig eth0 -promisc
1.修改ipv4 接受包的函数
在linux内核源代码树中修改net/ipv4/ip_input.c中函数ip_rcv
int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
{
struct iphdr *iph;
u32 len;
/* When the interface is in promisc. mode, drop all the crap
* that it receives, do not try to analyse it.
*/
if (skb->pkt_type == PACKET_OTHERHOST)
goto drop;
IP_INC_STATS_BH(IPSTATS_MIB_INRECEIVES);
if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
IP_INC_STATS_BH(IPSTATS_MIB_INDISCARDS);
goto out;
…………………………
修改为
/*
* symbol if the catch all packets.
* if wood_promisc is 0 , just catch the packets which dest address is
* local address.
* Else if wood_promisc is null 0 , then catch all packets.
*/
int wood_promisc=0;//wood_promisc标记是否抓取所有经过网卡的包
//可以通过其他内核模块或其他途径来修改此标识
EXPORT_SYMBOL(wood_promisc);
/*
* Main IP Receive routine.
*/
int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)