拦截自己编写的ARP发包程序发出去的ARP包不成功
本帖最后由 liudeyi545 于 2015-01-05 18:54 编辑我想实现的功能是:防止本机在中ARP病毒后,病毒向网关发送虚假ARP包,所以拦截本机发出去的ARP包,对其原MAC地址进行比较,如果和本机一致则通过,不一致说明是病毒发出去的,就丢弃。但是没有实现!
内核版本号:3.13.0-43
#define arp_hrd ea_hdr.ar_hrd
#include<linux/module.h>
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/netfilter.h>
#include<linux/netfilter_ipv4.h>
#include<linux/ip.h>
#include<net/ip.h>
#include<linux/udp.h>
#include<linux/in.h>
#include<linux/skbuff.h>
#include<linux/netdevice.h>
#include<linux/if_ether.h>
#include<linux/if.h>
#include<linux/if_arp.h>
#include<linux/netfilter_arp.h>
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 *)
)
{
struct arphdr *arp;//定义ARP包结构体
unsigned char *sha,*arp_ptr;
char buf;
arp=arp_hdr(skb);//获取数据包ARP头
arp_ptr=(unsigned char*)(arp+1);
//指针偏移找到数据包源MAC地址
sha=arp_ptr;
sprintf(buf,"%02x:%02x:%02x:%02x:%02x:%02x",sha,sha,sha,sha,sha,sha);
if(strcmp(buf,"dc:0e:a1:e8:94:d6")==0)
{
printk("MATCH!\n");
return NF_ACCEPT;
//若数据包源MAC地址与本机MAC一致则通过
}
else
{
printk("UNMATCH!\n");
//若数据包源MAC地址与本机MAC地址不一致,则丢弃该数据包
return NF_DROP;
}
}
static struct nf_hook_ops nfho={
.hook=hook_func,//注册功能函数
.pf=NFPROTO_ARP,//协议规则
.hooknum=NF_ARP_OUT,//挂载点
.priority=NF_IP_PRI_FIRST,//处理优先级
.owner=THIS_MODULE,//指定模块
};
static int __init hook_init(void)//注册模块
{
if (nf_register_hook(&nfho)) {
printk(KERN_ERR"<0>nf_register_hook() failed\n");
return -1;
}
return 0;
}
static void __exit hook_fini(void)//注销模块
{
nf_unregister_hook(&nfho);
}
module_init(hook_init);
module_exit(hook_fini);
MODULE_LICENSE("GPL");
这个钩子对于自己编写ARP发包程序发出去的ARP包不起作用,不明白为什么?请大家指教! 内核版本号 直接打印skb->data数据看 是否是你想要的arp报文 后者在你的hook函数最前面调用这个函数skb_reset_mac_header(skb);在进行你后面的操作 内核版本号:3.13.0-43
#define arp_hrd ea_hdr.ar_hrd
#include<linux/module.h>
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/netfilter.h>
#include<linux/netfilter_ipv4.h>
#include<linux/ip.h>
#include<net/ip.h>
#include<linux/udp.h>
#include<linux/in.h>
#include<linux/skbuff.h>
#include<linux/netdevice.h>
#include<linux/if_ether.h>
#include<linux/if.h>
#include<linux/if_arp.h>
#include<linux/netfilter_arp.h>
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 *)
)
{
struct arphdr *arp;//定义ARP包结构体
unsigned char *sha,*arp_ptr;
char buf;
arp=arp_hdr(skb);//获取数据包ARP头
arp_ptr=(unsigned char*)(arp+1);
//指针偏移找到数据包源MAC地址
sha=arp_ptr;
sprintf(buf,"%02x:%02x:%02x:%02x:%02x:%02x",sha,sha,sha,sha,sha,sha);
if(strcmp(buf,"dc:0e:a1:e8:94:d6")==0)
{
printk("MATCH!\n");
return NF_ACCEPT;
//若数据包源MAC地址与本机MAC一致则通过
}
else
{
printk("UNMATCH!\n");
//若数据包源MAC地址与本机MAC地址不一致,则丢弃该数据包
return NF_DROP;
}
}
static struct nf_hook_ops nfho={
.hook=hook_func,//注册功能函数
.pf=NFPROTO_ARP,//协议规则
.hooknum=NF_ARP_OUT,//挂载点
.priority=NF_IP_PRI_FIRST,//处理优先级
.owner=THIS_MODULE,//指定模块
};
static int __init hook_init(void)//注册模块
{
if (nf_register_hook(&nfho)) {
printk(KERN_ERR"<0>nf_register_hook() failed\n");
return -1;
}
return 0;
}
static void __exit hook_fini(void)//注销模块
{
nf_unregister_hook(&nfho);
}
module_init(hook_init);
module_exit(hook_fini);
MODULE_LICENSE("GPL");
这个钩子对于自己编写ARP发包程序发出去的ARP包不起作用,不明白为什么?请您指教!
回复 4# tc1989tc
现在的程序可以正确打印原MAC地址,但是拦截效果没有实现。回复 3# tc1989tc
你这是 自己实现的arptables吗?
要实现什么功能。感觉你逻辑上有点儿混乱吧。out里面arp包源mac肯定与本地一致的吧
您好,我想实现的效果是防止本机在中ARP病毒后,本机ARP病毒向网关发送错误的MAC地址,所以拦截本机发出去的ARP包进行分析。回复 7# anyhit
我在内核中嵌入我编写的这个模块之后,用我自己写的ARP发包程序发出去的虚假ARP包并不能被拦截,而本机发出去的ARP请求包可以被捕获,这个问题困扰了我好久。回复 7# anyhit
你自己写的arp发送程序是怎么样的?确定是不是经过arp的out hook函数处理了
页:
[1]
2