使用netfilter框架处理ARP报文
生活随笔
收集整理的這篇文章主要介紹了
使用netfilter框架处理ARP报文
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
內(nèi)核開(kāi)發(fā)交流群 745510310 歡迎加入學(xué)習(xí)
利用netfilter的框架實(shí)現(xiàn)對(duì)arp報(bào)文的處理,這里只是打印arp報(bào)文信息,更多的處理可以在此基礎(chǔ)上實(shí)現(xiàn)。
arp 首部封裝格式:
內(nèi)核版本 :,
不同版本內(nèi)核頭文件可能不一樣帶來(lái)編譯出錯(cuò)問(wèn)題,可以參考這篇博客https://blog.csdn.net/fuyuande/article/details/79429441 更新一下內(nèi)核。
源碼如下:
/** Description : print arp packet info* Date : 20180331* Author : fuyuande* Note : Kernel version 3.4.39*/#include <linux/init.h> #include <linux/module.h> #include <linux/if_ether.h> #include <linux/in.h> #include <linux/ip.h> #include <linux/if_arp.h> #include <linux/skbuff.h> #include <linux/netfilter.h> #include <linux/netfilter_ipv4.h> #include <linux/netfilter_arp.h>#define LOG(fmt,arg...) printk("[%s %d] "fmt,__FUNCTION__,__LINE__,##arg)/* arp內(nèi)容 */ #pragma pack(push,1) /* 字節(jié)對(duì)齊 */ struct arp_info {unsigned char src[ETH_ALEN];__be32 srcip;unsigned char dst[ETH_ALEN];__be32 dstip; }; #pragma pack(pop)#define IP1(addr) ((unsigned char *)&addr)[0] #define IP2(addr) ((unsigned char *)&addr)[1] #define IP3(addr) ((unsigned char *)&addr)[2] #define IP4(addr) ((unsigned char *)&addr)[3]static unsigned int arp_input_hook(unsigned int hooknum,struct sk_buff *skb,const struct net_device *in,const struct net_device *out,int (*okfn)(struct sk_buff *)) {struct ethhdr * ethh = NULL;/* 獲取L2層首部 */ethh = eth_hdr(skb);if(ethh == NULL){return NF_ACCEPT; } /* 打印網(wǎng)絡(luò)層協(xié)議類型 */LOG(" L3 type :%x \r\n",ethh->h_proto);return NF_ACCEPT; } static unsigned int arp_output_hook(unsigned int hooknum,struct sk_buff *skb,const struct net_device *in,const struct net_device *out,int (*okfn)(struct sk_buff *)) {struct arphdr *arph = NULL;????/* arp首部 */struct arp_info *arpinfo = NULL;arph = arp_hdr(skb);?????????? /* 獲取arp首部 */?if(arph == NULL){LOG("Weird! arp header null \r\n");return NF_ACCEPT;}/* 打印arp首部信息 */LOG(" arp info :\r\n""-------------\r\n" "arp hw type :%x \r\n""arp pro type :%x \r\n""arp hln :%d\r\n""arp plen:%d\r\n""arp ops :%d\r\n""-------------\r\n", ntohs(arph->ar_hrd),ntohs(arph->ar_pro),arph->ar_hln,arph->ar_pln,ntohs(arph->ar_op));/* 打印mac地址信息 */??? arpinfo = (unsigned char *)(arph + 1);LOG("\n-------------\r\n""mac : %x:%x:%x:%x:%x:%x \r\n""sip : %d:%d:%d:%d \r\n""dmac : %x:%x:%x:%x:%x:%x \r\n""dip : %d:%d:%d:%d \r\n""-------------\r\n",arpinfo->src[0],arpinfo->src[1],arpinfo->src[2],arpinfo->src[3],arpinfo->src[4],arpinfo->src[5],IP1(arpinfo->srcip),IP2(arpinfo->srcip),IP3(arpinfo->srcip),IP4(arpinfo->srcip),arpinfo->dst[0],arpinfo->dst[1],arpinfo->dst[2],arpinfo->dst[3],arpinfo->dst[4],arpinfo->dst[5],IP1(arpinfo->dstip),IP2(arpinfo->dstip),IP3(arpinfo->dstip),IP4(arpinfo->dstip)); return NF_ACCEPT; }struct nf_hook_ops arp_hook_ops[] ={{.hook = arp_input_hook,????/* 輸入arp鉤子函數(shù)*/.pf = NFPROTO_ARP,???????? /* 協(xié)議類型 */???.hooknum = NF_ARP_IN,????? /* arp input 鏈*/ ?.priority = 0,???????????? /* 優(yōu)先級(jí) */ ??},{.hook = arp_output_hook, /* 輸出arp鉤子函數(shù) */.pf = NFPROTO_ARP, /* 協(xié)議類型 */.hooknum = NF_ARP_OUT,???? /* arp output 鏈 */???.priority = 0,???????????? /* 優(yōu)先級(jí) */},{} };static int __init arp_hook_init(void) {/* 注冊(cè)netfilter鉤子 */nf_register_hooks(arp_hook_ops,ARRAY_SIZE(arp_hook_ops));return 0; }static void __exit arp_hook_exit(void) {/* 注銷netfilter鉤子 */??? nf_unregister_hooks(arp_hook_ops,ARRAY_SIZE(arp_hook_ops));return ; }module_init(arp_hook_init) module_exit(arp_hook_exit) MODULE_LICENSE("GPL");Makefile:
obj-m := arphook.o PWD :=$(shell pwd) KERNEL_DIR :="/usr/src/linux-headers-3.4.39-030439-generic/"modules:$(MAKE) -C $(KERNEL_DIR) M=${PWD} modules clean:@rm *.ko *.mod.c *.o Modu* modu*在內(nèi)核3.4.39版本上可以直接執(zhí)行如下命令編譯、加載
#編譯模塊 sudo make #加載模塊 sudo insmod arphook.ko #卸載模塊 sudo rmmod arphook.ko效果如下:
總結(jié)
以上是生活随笔為你收集整理的使用netfilter框架处理ARP报文的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: WARNING: at net/core
- 下一篇: 年利率5%怎么算利息