




已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
基于RawSocket的數(shù)據(jù)包過(guò)濾程序,共包含7個(gè)文件:inetheader.h、rawsocsniffer.h、rawsocket.h、rawsocket.c、rawsocsniffer.c、main.c、makefile。編譯:命令行模式下,切換到代碼所在目錄,輸入make即可編譯。編譯后將生成一個(gè)可執(zhí)行文件main,以及一些obj文件。運(yùn)行:命令行下輸入 ./main -s 源IP -d 目的IP -atui 即可運(yùn)行程序,可以通過(guò)設(shè)置不同的參數(shù)可以實(shí)現(xiàn)簡(jiǎn)單的過(guò)濾功能。參數(shù)功能如下: s是根據(jù)源IP地址過(guò)濾數(shù)據(jù)包 d是根據(jù)目的IP地址過(guò)濾數(shù)據(jù)包 a是過(guò)濾ARP包 t是過(guò)濾TCP包 u是過(guò)濾UDP包 i是過(guò)濾ICMP包代碼如下:#include #include #include #include rawsocsniffer.h#include inetheader.husing namespace std;rawsocsniffer:rawsocsniffer(int protocol):rawsocket(protocol),max_packet_len(2048) packet=new charmax_packet_len; memset(&simfilter,0,sizeof(simfilter);rawsocsniffer:rawsocsniffer() delete packet;/set the socket to promiscuous mode which means to capture all packets.bool rawsocsniffer:init() dopromisc(eth0);/set the filtervoid rawsocsniffer:setfilter(filter myfilter) tocol=tocol; simfilter.sip=myfilter.sip; simfilter.dip=myfilter.dip;bool rawsocsniffer:testbit(const unsigned int p,int k) if(p(k-1)&0x01)return true; elsereturn false;void rawsocsniffer:setbit(unsigned int &p,int k) p=(p)|(0x01)0) analyze(); else continue; /analyze packets;void rawsocsniffer:analyze() ether_header_t *etherpacket=(ether_header_t *)packet; if(tocol=0)tocol=0xff; switch (ntohs(etherpacket-frametype) case 0x0800: if(tocol)1) coutnn/*-ip packet-*/endl; ParseIPPacket(); break;case 0x0806: if(testbit(tocol,1) coutnn/*-arp packet-*/endl; ParseARPPacket(); break;case 0x0835: if(testbit(tocol,5) coutnn/*-RARP packet-*/endl;ParseRARPPacket(); break;default: coutnn/*-Unknown packet-*/endl; coutUnknown ethernet frametype!endl; break; /analyze ip packetsvoid rawsocsniffer:ParseIPPacket() ip_packet_t *ippacket=(ip_packet_t *)packet; tocol: tocol)ipheader.src_ip) return; if(simfilter.dip!=0) if(simfilter.dip!=(ippacket-ipheader.des_ip) return; switch (int(tocol) case 1: if(testbit(tocol,4) coutReceived an ICMP packetendl; ParseICMPPacket(); break;case 6: if(testbit(tocol,2) coutReceived an TCP packetendl; ParseTCPPacket(); break;case 17: if(testbit(tocol,3) coutReceived an UDP packetendl; ParseUDPPacket(); break;default: coutUnknown ip protocoltypearpheader.send_hw_addr); print_hw_addr(arppacket-arpheader.des_hw_addr); coutarpheader.send_prot_addr); print_ip_addr(arppacket-arpheader.des_prot_addr); coutendl; coutsetw(15)Hardware type: 0xhexarpheader.hw_type); coutsetw(15) Protocol type: 0t_type); coutsetw(15) Operation code: 0xhexarpheader.flag); coutendl;/analyze udp packets;void rawsocsniffer:ParseUDPPacket() udp_packet_t *udppacket=(udp_packet_t *)packet; coutsetw(20)etherheader.src_hw_addr); coutetherheader.des_hw_addr); coutendlsetw(20)ipheader.src_ip); coutipheader.des_ip); coutendl; coutsetw(10)srcport: udpheader.src_port) desport: udpheader.des_port) length:udpheader.len)endl;/analyze tcp packets;void rawsocsniffer:ParseTCPPacket() tcp_packet_t *tcppacket=(tcp_packet_t *)packet; coutsetw(20)etherheader.src_hw_addr); coutetherheader.des_hw_addr); coutendlsetw(20)ipheader.src_ip); coutipheader.des_ip); coutendl; coutsetw(10)srcport: tcpheader.src_port) desport: tcpheader.des_port)endl; coutseq: tcpheader.seq) ack: tcpheader.ack)endl;/analyze icmp packets;void rawsocsniffer:ParseICMPPacket() icmp_packet_t *icmppacket=(icmp_packet_t *)packet; coutsetw(20)etherheader.src_hw_addr); coutetherheader.des_hw_addr); coutendlsetw(20)ipheader.src_ip); coutipheader.des_ip); coutendl; coutsetw(12)icmp type: icmpheader.type) icmp code: icmpheader.code)endl; coutsetw(12)icmp id: icmpheader.id) icmp seq: icmpheader.seq)endl;void rawsocsniffer:print_hw_addr(const unsigned char *ptr) char hw_addr18; sprintf(hw_addr,%02x:%02x:%02x:%02x:%02x
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年軟件開(kāi)發(fā)關(guān)鍵考點(diǎn)試題及答案
- 計(jì)算機(jī)二級(jí)VB考試的匹配題詳解及試題與答案
- 進(jìn)口替代政策的經(jīng)濟(jì)效果試題及答案
- 2025關(guān)于北京二手房購(gòu)房合同的范本
- 行政管理考試提綱試題與答案
- 行政法學(xué)知識(shí)檢查試題與答案
- 如何通過(guò)分享經(jīng)驗(yàn)改善工作計(jì)劃
- 資產(chǎn)配置的基本方法計(jì)劃
- 戰(zhàn)略部署的執(zhí)行力與風(fēng)險(xiǎn)控制試題及答案
- 行政法學(xué)考試實(shí)踐試題及答案報(bào)告
- 機(jī)駕長(zhǎng)習(xí)題+答案
- 學(xué)生宿舍衛(wèi)生評(píng)比方案
- 2025年中鐵特貨物流股份有限公司招聘(75人)筆試參考題庫(kù)附帶答案詳解
- 利劍護(hù)蕾安全教育
- 煙花爆竹零售店(點(diǎn))安全技術(shù)規(guī)范
- 超星爾雅學(xué)習(xí)通《人工智能與科學(xué)之美(湘潭大學(xué))》2025章節(jié)測(cè)試附答案
- qc崗位面試試題及答案
- 電商家具用戶體驗(yàn)研究-深度研究
- iata第 66版危險(xiǎn)貨物規(guī)則(dgr 66th)
- 高考英語(yǔ)單詞3500(亂序版)
- 兒童化妝品安全知識(shí)科普
評(píng)論
0/150
提交評(píng)論