linux 基于netfilter的网内监听

本文介绍了如何在Linux 2.6.23.1内核中使用Netfilter框架来捕获局域网中的所有数据包。主要内容包括:开启网卡混乱模式,修改`ip_input.c`中的`ip_rcv`函数以允许捕获所有包,重新编译内核,编写内核模块以基于Netfilter进行报文采集,并展示了相关代码示例。
  本文基于内核版本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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值