使用Wincap編制一個簡單的網(wǎng)絡(luò)數(shù)據(jù)包監(jiān)聽與捕獲程序_第1頁
使用Wincap編制一個簡單的網(wǎng)絡(luò)數(shù)據(jù)包監(jiān)聽與捕獲程序_第2頁
使用Wincap編制一個簡單的網(wǎng)絡(luò)數(shù)據(jù)包監(jiān)聽與捕獲程序_第3頁
使用Wincap編制一個簡單的網(wǎng)絡(luò)數(shù)據(jù)包監(jiān)聽與捕獲程序_第4頁
使用Wincap編制一個簡單的網(wǎng)絡(luò)數(shù)據(jù)包監(jiān)聽與捕獲程序_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、一、 實驗?zāi)康膹?fù)習(xí)計算機(jī)網(wǎng)絡(luò)課程相關(guān)知識,采用合適的程序開發(fā)語言完成網(wǎng)絡(luò)程序開發(fā)。二、實驗環(huán)境微機(jī)一臺操作系統(tǒng):WinXP / Linux編程軟件:C+二、實驗內(nèi)容要求使用Wincap編制一個簡單的網(wǎng)絡(luò)數(shù)據(jù)包監(jiān)聽與捕獲程序,同時,將捕獲的數(shù)據(jù)包進(jìn)行分析并將分析結(jié)果顯示在屏幕上。三、 實驗步驟、記錄和結(jié)果代碼如下:#include pcap.htypedef structint number;char name10;Protocol;Protocol protocol10;/* 4字節(jié)的IP地址 */typedef struct ip_addressu_char byte1;u_char byt

2、e2;u_char byte3;u_char byte4;ip_address;/* IPv4 首部 */typedef struct ip_headeru_char ver_ihl; / 版本 (4 bits) + 首部長度 (4 bits)u_char tos; / 服務(wù)類型(Type of service) u_short tlen; / 總長(Total length) u_short identification; / 標(biāo)識(Identification)u_short flags_fo; / 標(biāo)志位(Flags) (3 bits) + 段偏移量(Fragment offset) (

3、13 bits)u_char ttl; / 存活時間(Time to live)u_char proto; / 協(xié)議(Protocol)u_short crc; / 首部校驗和(Header checksum)ip_address saddr; / 源地址(Source address)ip_address daddr; / 目的地址(Destination address)u_int op_pad; / 選項與填充(Option + Padding)ip_header;/* 回調(diào)函數(shù)原型 */void packet_handler(u_char *param, const struct pc

4、ap_pkthdr *header, const u_char *pkt_data);int main()pcap_if_t *alldevs;pcap_if_t *d;int inum;int i=0;pcap_t *adhandle;char errbufPCAP_ERRBUF_SIZE;u_int netmask;/char packet_filter = ip and udp;char packet_filter = ip;struct bpf_program fcode;protocol0.number = 1; strcpy(, ICMP);protoc

5、ol1.number = 2; strcpy(, IGMP);protocol2.number = 4; strcpy(, IP);protocol3.number = 6; strcpy(, TCP);protocol4.number = 8; strcpy(, EGP);protocol5.number = 9; strcpy(, IGP);protocol6.number = 17; strcpy(, UDP);proto

6、col7.number = 41; strcpy(, IPv6);protocol8.number = 50; strcpy(, ESP);protocol9.number = 89; strcpy(, OSPF);/* 獲得設(shè)備列表 */if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) = -1)fprintf(stderr,Error in pcap_findalldevs: %sn, errbuf);exit(1);/*

7、 打印列表 */for(d=alldevs; d; d=d-next)printf(%d. %s, +i, d-name);if (d-description)printf( (%s)n, d-description);elseprintf( (No description available)n);if(i=0)printf(nNo interfaces found! Make sure WinPcap is installed.n);return -1;printf(Enter the interface number (1-%d):,i);scanf(%d, &inum);if(inum

8、 i)printf(nInterface number out of range.n);/* 釋放設(shè)備列表 */pcap_freealldevs(alldevs);return -1;/* 跳轉(zhuǎn)到已選設(shè)備 */for(d=alldevs, i=0; inext, i+);/* 打開適配器 */if ( (adhandle= pcap_open(d-name, / 設(shè)備名65536, / 要捕捉的數(shù)據(jù)包的部分 / 65535保證能捕獲到不同數(shù)據(jù)鏈路層上的每個數(shù)據(jù)包的全部內(nèi)容PCAP_OPENFLAG_PROMISCUOUS, / 混雜模式1000, / 讀取超時時間NULL, / 遠(yuǎn)程機(jī)器驗證e

9、rrbuf / 錯誤緩沖池) ) = NULL)fprintf(stderr,nUnable to open the adapter. %s is not supported by WinPcapn);/* 釋放設(shè)備列表 */pcap_freealldevs(alldevs);return -1;/* 檢查數(shù)據(jù)鏈路層,為了簡單,我們只考慮以太網(wǎng) */if(pcap_datalink(adhandle) != DLT_EN10MB)fprintf(stderr,nThis program works only on Ethernet networks.n);/* 釋放設(shè)備列表 */pcap_fr

10、eealldevs(alldevs);return -1;if(d-addresses != NULL)/* 獲得接口第一個地址的掩碼 */netmask=(struct sockaddr_in *)(d-addresses-netmask)-sin_addr.S_un.S_addr;else/* 如果接口沒有地址,那么我們假設(shè)一個C類的掩碼 */netmask=0xffffff; /編譯過濾器if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) 0 )fprintf(stderr,nUnable to compile th

11、e packet filter. Check the syntax.n);/* 釋放設(shè)備列表 */pcap_freealldevs(alldevs);return -1;/設(shè)置過濾器if (pcap_setfilter(adhandle, &fcode)description);/* 釋放設(shè)備列表 */pcap_freealldevs(alldevs);/* 開始捕捉 */pcap_loop(adhandle, 0, packet_handler, NULL);return 0;/* 回調(diào)函數(shù),當(dāng)收到每一個數(shù)據(jù)包時會被libpcap所調(diào)用 */void packet_handler(u_cha

12、r *param, const struct pcap_pkthdr *header, const u_char *pkt_data)struct tm *ltime;char timestr16;ip_header *ih;u_int ip_len;u_short sport,dport;time_t local_tv_sec;/* 將時間戳轉(zhuǎn)換成可識別的格式 */local_tv_sec = header-ts.tv_sec;ltime=localtime(&local_tv_sec);strftime( timestr, sizeof timestr, %H:%M:%S, ltime);

13、/* 打印數(shù)據(jù)包的時間戳和長度 */printf(%s.%.6d len:%4d , timestr, header-ts.tv_usec, header-len);/* 獲得IP數(shù)據(jù)包頭部的位置 */ih = (ip_header *) (pkt_data +14); /以太網(wǎng)頭部長度/*打印協(xié)議類型*/int i;for(i = 0; i proto)break;if(i %d.%d.%d.%dn,ih-saddr.byte1,ih-saddr.byte2,ih-saddr.byte3,ih-saddr.byte4,ih-daddr.byte1,ih-daddr.byte2,ih-dadd

14、r.byte3,ih-daddr.byte4);程序執(zhí)行結(jié)果如下:上面三張圖是程序運(yùn)行的截圖,第一列是數(shù)據(jù)包的時間戳,第二列是數(shù)據(jù)包的長度,第三列是數(shù)據(jù)包的協(xié)議類型,第四列是數(shù)據(jù)包的源IP地址和目的IP地址。四、 實驗分析 程序執(zhí)行過程:使用pcap_findalldevs_ex函數(shù)獲取已連接的網(wǎng)絡(luò)適配器列表。這個函數(shù)返回一個 pcap_if 結(jié)構(gòu)的鏈表, 每個這樣的結(jié)構(gòu)都包含了一個適配器的詳細(xì)信息。其中數(shù)據(jù)域name表示適配器名稱,但難以理解,如rpcap:/DeviceNPF_7CD080C7-2B67-4839-B6F6-1D90273D960A,另一個數(shù)據(jù)域description表示一

15、個可以讓人們理解的描述,如(Network adapter Realtek PCIe FE Family Controller on local host)。使用pcap_open()函數(shù)打開適配器,將這個函數(shù)的snaplen參數(shù)設(shè)為65535,這比能遇到的最大的MTU還要大,以保證能收到完整的數(shù)據(jù)包。另一個參數(shù)flag設(shè)為PCAP_OPENFLAG_PROMISCUOUS,意為混雜模式,不管這個數(shù)據(jù)包是不是發(fā)給本機(jī)的,都會去捕獲。參數(shù)to_ms 指定讀取數(shù)據(jù)的超時時間,這里設(shè)置為1000ms,在適配器上進(jìn)行讀取操作都會在 to_ms 毫秒時間內(nèi)響應(yīng)。使用pcap_compile編譯過濾器,我們可以創(chuàng)建一個char數(shù)組packet_filter,值為ip賦給

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論