版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
中南大学CENTRALSOUTHUNIVERSITY《SEEDPROJECT》实验报告学生姓名孙毅学号0906140106指导教师王伟平学院信息科学与工程专业班级信息安全1401成时间2016.12目录TOC\o"1-3"\h\u一、实验原理 1二、实验器材 1三、实验步骤及运行结果 1Task1.编写嗅探程序 1Task2.包欺骗 3Task3:综合使用 4四、附件 4Task1 5Task2 13Task3 17Sniffing_Spoofing实验原理Sniffing就是一种能将本地网卡状态设成‘混杂’状态的模式,当网卡处于这种“混杂”方式时,该网卡具备“广播地址”,它对遇到的每一个帧都产生一个硬件中断以便提醒操作系统处理流经该物理媒体上的每一个报文包。(绝大多数的网卡具备置成混杂模式的能力)一般来说,sniffing和poofing会联合起来使用。当攻击者嗅探到关键信息时,通常会使用poofing技术来构造数据包来劫持会话或者去获取更多信息,通常会造成很大的危害。Poofing技术就是攻击者自己构造数据包的ip/tcp数据包帧头部数据来达到自己的目的。本次实验就是基于以上原理,在linux下模拟整个过程。实验器材1.Ubuntu12.04。2.Wireshark等常用捕包工具。实验步骤及运行结果Task1.编写嗅探程序嗅探程序可以很容易地使用pcap库。利用PCAP,嗅探器的任务变得在pcap库调用一系列简单的程序。在序列结束时,数据包将被放置在缓冲区中,以进一步处理,只要它们被捕获。所有的数据包捕获的细节由pcap库处理。TimCarstens写了一个教程如何使用pcap库写的嗅探程序。1:深入理解并可以编写嗅探程序。2:编写过滤器。请为您的嗅探程序捕捉每个写过滤表达式如下。在你的实验报告,你需要包括screendumps显示应用这些过滤器的结果。•捕获ICMP数据包。•捕获TCP数据包有一个目的端口范围从端口10-100。运行结果如下:在程序中预设捕获10个数据包,当捕获数据包之后会将数据包进行处理,会下显示数据包的类型,还有数据包的源ip和目的ip,源端口和目的端口,当有数据时还会显示数据。对于任务一的2,主要是修改filter中的过滤条件,要实现只捕获ICMP类型的数据包,只需要将charfilter_exp[]="ip"中的ip改为ICMP,然后要捕获端口在10-100之间的tcp数据包,同理,将这条语句中的条件改为‘tcpanddstportrange10-100’即可。Task2.包欺骗在正常的情况下,当一个用户发送一个数据包时,操作系统通常不允许用户设置所有的在协议头字段(如TCP,UDP,和IP报头)。操作系统将大部分的领域,而只允许用户设置几个字段,如目标IP地址、目标端口号等。但是当用户有有root权限,他们可以在数据包标头设置为任意字段。这就是所谓的包欺骗,它可以通过原始套接字完成。原始套接字给程序员的数据包结构的绝对控制,允许程序员构建任何任意的数据包,包括设置头字段和有效载荷。使用原始套接字是相当简单的,它包括四个步骤:(1)创建一个原始套接字,(2)设置套接字选项,(3)构建数据包,和(4)通过原始套接字发送数据包。有许多在线教程,可以教你如何使用原始套接字在C编程。我们已经把一些教程与实验室的网页联系起来了。请阅读它们,并学习如何写一个spoonfing程序包。我们展示了一个简单的的程序。运行结果如下:可以看到成功向1的80端口发送了伪造的的源IP为且端口的234的数据包,这就实现包欺骗的过程。Task3:综合使用在这个任务中,你将嗅探和欺骗技术实现连接,并实现程序。你需要在同一局域网两虚拟机。从VMAping另一个VM的IP,这将产生一个ICMP回送请求报文。如果X是活着的,ping程序将收到一个回音答复,并打印出响应。你嗅探到数据包然后伪造程序运行在虚拟机B、监控网络数据包嗅探。每当它看到ICMP回送请求,不管目标IP地址是什么,你的程序应该立即发出回声应答数据包欺骗技术的使用。因此,考虑到机器X是否是活的,这个程序将总是收到一个回复,这表明X是活的。你要写这样一个程序,包括在你显示你的程序的工作报告screendumps。请在你的报告中附上代码。附件Task1#defineAPP_NAME "sniffex"
#defineAPP_DESC "Snifferexampleusinglibpcap"
#defineAPP_COPYRIGHT "Copyright(c)2005TheTcpdumpGroup"
#defineAPP_DISCLAIMER "THEREISABSOLUTELYNOWARRANTYFORTHISPROGRAM."
#include<pcap.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
/*defaultsnaplength(maximumbytesperpackettocapture)*/
#defineSNAP_LEN1518
/*ethernetheadersarealwaysexactly14bytes[1]*/
#defineSIZE_ETHERNET14
/*Ethernetaddressesare6bytes*/
#defineETHER_ADDR_LEN 6
/*Ethernetheader*/
structsniff_ethernet{
u_charether_dhost[ETHER_ADDR_LEN];/*destinationhostaddress*/
u_charether_shost[ETHER_ADDR_LEN];/*sourcehostaddress*/
u_shortether_type;/*IP?ARP?RARP?etc*/
};
/*IPheader*/
structsniff_ip{
u_charip_vhl;/*version<<4|headerlength>>2*/
u_charip_tos;/*typeofservice*/
u_shortip_len;/*totallength*/
u_shortip_id;/*identification*/
u_shortip_off;/*fragmentoffsetfield*/
#defineIP_RF0x8000/*reservedfragmentflag*/
#defineIP_DF0x4000/*dontfragmentflag*/
#defineIP_MF0x2000/*morefragmentsflag*/
#defineIP_OFFMASK0x1fff/*maskforfragmentingbits*/
u_charip_ttl;/*timetolive*/
u_charip_p;/*protocol*/
u_shortip_sum;/*checksum*/
structin_addrip_src,ip_dst;/*sourceanddestaddress*/
};
#defineIP_HL(ip)(((ip)->ip_vhl)&0x0f)/*与15与运算*/
#defineIP_V(ip)(((ip)->ip_vhl)>>4)/*ip_vhl的各二进位全部右移4位*/
/*TCPheader*/
typedefu_inttcp_seq;
structsniff_tcp{
u_shortth_sport;/*sourceport*/
u_shortth_dport;/*destinationport*/
tcp_seqth_seq;/*sequencenumber*/
tcp_seqth_ack;/*acknowledgementnumber*/
u_charth_offx2;/*dataoffset,rsvd*/
#defineTH_OFF(th)(((th)->th_offx2&0xf0)>>4)
u_charth_flags;
#defineTH_FIN0x01
#defineTH_SYN0x02
#defineTH_RST0x04
#defineTH_PUSH0x08
#defineTH_ACK0x10
#defineTH_URG0x20
#defineTH_ECE0x40
#defineTH_CWR0x80
#defineTH_FLAGS(TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
u_shortth_win;/*window*/
u_shortth_sum;/*checksum*/
u_shortth_urp;/*urgentpointer*/
};
void
got_packet(u_char*args,conststructpcap_pkthdr*header,constu_char*packet);
void
print_payload(constu_char*payload,intlen);
void
print_hex_ascii_line(constu_char*payload,intlen,intoffset);
void
print_app_banner(void);
void
print_app_usage(void);
void/*输出相关信息*/
print_app_banner(void)
{
printf("%s-%s\n",APP_NAME,APP_DESC);
printf("%s\n",APP_COPYRIGHT);
printf("%s\n",APP_DISCLAIMER);
printf("\n");
return;
}
void
print_app_usage(void)
{
printf("Usage:%s[interface]\n",APP_NAME);
printf("\n");
printf("Options:\n");
printf("interfaceListenon<interface>forpackets.\n");
printf("\n");
return;
}
void
print_hex_ascii_line(constu_char*payload,intlen,intoffset)
{
inti;
intgap;
constu_char*ch;
printf("%05d",offset);
ch=payload;
for(i=0;i<len;i++){
printf("%02x",*ch);
ch++;
/*printextraspaceafter8thbyteforvisualaid*/
if(i==7)
printf("");
}
/*printspacetohandlelinelessthan8bytes*/
if(len<8)
printf("");
if(len<16){
gap=16-len;
for(i=0;i<gap;i++){
printf("");
}
}
printf("");
ch=payload;
for(i=0;i<len;i++){
if(isprint(*ch))
printf("%c",*ch);
else
printf(".");
ch++;
}
printf("\n");
return;
}
void
print_payload(constu_char*payload,intlen)
{
intlen_rem=len;
intline_width=16; /*numberofbytesperline*/
intline_len;
intoffset=0; /*zero-basedoffsetcounter*/
constu_char*ch=payload;
if(len<=0)
return;
if(len<=line_width){
print_hex_ascii_line(ch,len,offset);
return;
}
for(;;){
/*computecurrentlinelength*/
line_len=line_width%len_rem;
/*printline*/
print_hex_ascii_line(ch,line_len,offset);
/*computetotalremaining*/
len_rem=len_rem-line_len;
/*shiftpointertoremainingbytestoprint*/
ch=ch+line_len;
/*addoffset*/
offset=offset+line_width;
/*checkifwehavelinewidthcharsorless*/
if(len_rem<=line_width){
/*printlastlineandgetout*/
print_hex_ascii_line(ch,len_rem,offset);
break;
}
}
return;
}
void
got_packet(u_char*args,conststructpcap_pkthdr*header,constu_char*packet)
{
staticintcount=1;/*packetcounter*/
/*declarepointerstopacketheaders*/
conststructsniff_ethernet*ethernet;/*Theethernetheader[1]*/
conststructsniff_ip*ip;/*TheIPheader*/
conststructsniff_tcp*tcp;/*TheTCPheader*/
constchar*payload;/*Packetpayload*/
intsize_ip;
intsize_tcp;
intsize_payload;
printf("\nPacketnumber%d:\n",count);
count++;
/*defineethernetheader*/
ethernet=(structsniff_ethernet*)(packet);
/*define/computeipheaderoffset*/
ip=(structsniff_ip*)(packet+SIZE_ETHERNET);
size_ip=IP_HL(ip)*4;
if(size_ip<20){
printf("*InvalidIPheaderlength:%ubytes\n",size_ip);
return;
}
/*printsourceanddestinationIPaddresses*/
printf("From:%s\n",inet_ntoa(ip->ip_src));
printf("To:%s\n",inet_ntoa(ip->ip_dst));
/*determineprotocol*/
switch(ip->ip_p){
caseIPPROTO_TCP:
printf("Protocol:TCP\n");
break;
caseIPPROTO_UDP:
printf("Protocol:UDP\n");
return;
caseIPPROTO_ICMP:
printf("Protocol:ICMP\n");
return;
caseIPPROTO_IP:
printf("Protocol:IP\n");
return;
default:
printf("Protocol:unknown\n");
return;
}
tcp=(structsniff_tcp*)(packet+SIZE_ETHERNET+size_ip);
size_tcp=TH_OFF(tcp)*4;
if(size_tcp<20){
printf("*InvalidTCPheaderlength:%ubytes\n",size_tcp);
return;
}
printf("Srcport:%d\n",ntohs(tcp->th_sport));
printf("Dstport:%d\n",ntohs(tcp->th_dport));
/*define/computetcppayload(segment)offset*/
payload=(u_char*)(packet+SIZE_ETHERNET+size_ip+size_tcp);
/*computetcppayload(segment)size*/
size_payload=ntohs(ip->ip_len)-(size_ip+size_tcp);
/*
*Printpayloaddata;itmightbebinary,sodon'tjust
*treatitasastring.
*/
if(size_payload>0){
printf("Payload(%dbytes):\n",size_payload);
print_payload(payload,size_payload);
}return;
}
intmain(intargc,char**argv)
{
char*dev=NULL; /*capturedevicename*/
charerrbuf[PCAP_ERRBUF_SIZE]; /*errorbuffer*/
pcap_t*handle; /*packetcapturehandle*/
charfilter_exp[]="ip"; /*filterexpression[3]*/
structbpf_programfp; /*compiledfilterprogram(expression)*/
bpf_u_int32mask; /*子网掩码*/
bpf_u_int32net; /*IP地址*/
intnum_packets=10; /*numberofpacketstocapture*/
print_app_banner();
/*checkforcapturedevicenameoncommand-line*/
if(argc==2){
dev=argv[1];
}
elseif(argc>2){
fprintf(stderr,"error:unrecognizedcommand-lineoptions\n\n");
print_app_usage();
exit(EXIT_FAILURE);
}
else{
/*findacapturedeviceifnotspecifiedoncommand-line*/
dev=pcap_lookupdev(errbuf);
if(dev==NULL){
fprintf(stderr,"Couldn'tfinddefaultdevice:%s\n",
errbuf);
exit(EXIT_FAILURE);
}
}
/*getnetworknumberandmaskassociatedwithcapturedevice*/
if(pcap_lookupnet(dev,&net,&mask,errbuf)==-1){
fprintf(stderr,"Couldn'tgetnetmaskfordevice%s:%s\n",
dev,errbuf);
net=0;
mask=0;
}
/*printcaptureinfo*/
printf("Device:%s\n",dev);
printf("Numberofpackets:%d\n",num_packets);
printf("Filterexpression:%s\n",filter_exp);
/*opencapturedevice*/
handle=pcap_open_live(dev,SNAP_LEN,1,1000,errbuf);
if(handle==NULL){
fprintf(stderr,"Couldn'topendevice%s:%s\n",dev,errbuf);
exit(EXIT_FAILURE);
}
/*makesurewe'recapturingonanEthernetdevice[2]*/
if(pcap_datalink(handle)!=DLT_EN10MB){
fprintf(stderr,"%sisnotanEthernet\n",dev);
exit(EXIT_FAILURE);
}
if(pcap_compile(handle,&fp,filter_exp,0,net)==-1){/*过滤表达式*/
fprintf(stderr,"Couldn'tparsefilter%s:%s\n",
filter_exp,pcap_geterr(handle));
exit(EXIT_FAILURE);
}
if(pcap_setfilter(handle,&fp)==-1){
fprintf(stderr,"Couldn'tinstallfilter%s:%s\n",
filter_exp,pcap_geterr(handle));
exit(EXIT_FAILURE);
}
pcap_loop(handle,num_packets,got_packet,NULL);
pcap_freecode(&fp);
pcap_close(handle);
printf("\nCapturecomplete.\n");
return0;
}
Task2#include<unistd.h>#include<stdio.h>#include<sys/socket.h>#include<netinet/ip.h>#include<netinet/udp.h>#include<stdlib.h>#definePCKT_LEN8192structipheader{unsignedchariph_ihl:5,iph_ver:4;unsignedchariph_tos;unsignedshortintiph_len;unsignedshortintiph_ident;unsignedchariph_flag;unsignedshortintiph_offset;unsignedchariph_ttl;unsignedchariph_protocol;unsignedshortintiph_chksum;unsignedintiph_sourceip;unsignedintiph_destip;};//UDPheader'sstructurestructudpheader{unsignedshortintudph_srcport;unsignedshortintudph_destport;unsignedshortintudph_len;unsignedshortintudph_chksum;};//totaludpheaderlength:8bytes(=64bits)//Functionforchecksumcalculation.FromtheRFC,//thechecksumalgorithmis://"Thechecksumfieldisthe16bitone'scomplementoftheone's//complementsumofall16bitwordsintheheader.Forpurposesof//computingthechecksum,thevalueofthechecksumfieldiszero."unsignedshortcsum(unsignedshort*buf,intnwords){//unsignedlongsum;for(sum=0;nwords>0;nwords--)sum+=*buf++;sum=(sum>>16)+(sum&0xffff);sum+=(sum>>16);return(unsignedshort)(~sum);}//SourceIP,sourceport,targetIP,targetportfromthecommandlineargumentsintmain(intargc,char*argv[]){intsd;//Nodata/payloadjustdatagramcharbuffer[PCKT_LEN];//Ourownheaders'structuresstructipheader*ip=(structipheader*)buffer;structudpheader*udp=(structudpheader*)(buffer+sizeof(structipheader));//Sourceanddestinationaddresses:IPandportstructsockaddr_insin,din;intone=1;constint*val=&one;memset(buffer,0,PCKT_LEN);if(argc!=5){printf("-Invalidparameters!!!\n");printf("-Usage%s<sourcehostname/IP><sourceport><targethostname/IP><targetport>\n",argv[0]);exit(-1);}//CreatearawsocketwithUDPprotocolsd=socket(PF_INET,SOCK_RAW,IPPROTO_UDP);if(sd<0){perror("socket()error");//Ifsomethingwrongjustexitexit(-1);}elseprintf("socket()-UsingSOCK_RAWsocketandUDPprotocolisOK.\n");//Thesourceisredundant,maybeusedlaterifneeded//Theaddressfamilysin.sin_family=AF_INET;din.sin_family=AF_INET;//Portnumberssin.sin_port=htons(atoi(argv[2]));din.sin_port=htons(atoi(argv[4]));//IPaddressessin.sin_addr.s_addr=inet_addr(argv[1]);din.sin_addr.s_addr=inet_addr(argv[3]);//FabricatetheIPheaderorwecanusethe//standardheaderstructuresbutassignourownvalues.ip->iph_ihl=5;ip->iph_ver=4;ip->iph_tos=16;//Lowdelayip->iph_len=sizeof(structipheader)+sizeof(structudpheader);ip->iph_ident=htons(54321);ip->iph_ttl=64;//hopsip->iph_protocol=17;//UDP//SourceIPaddress,canusespoofedaddresshere!!!ip->iph_sourceip=inet_addr(argv[1]);//ThedestinationIPaddressip->iph_destip=inet_addr(argv[3]);//FabricatetheUDPheader.Sourceportnumber,redundantudp->udph_srcport=htons(atoi(argv[2]));//Destinationportnumberudp->udph_destport=htons(atoi(argv[4]));udp->udph_len=htons(sizeof(structudpheader));//Calculatethechecksumforintegrityip->iph_chksum=csum((unsignedshort*)buffer,sizeof(structipheader)+sizeof(structudpheader));//Informthekerneldonotfillupthepacketstructure.wewillbuildourown...if(setsockopt(sd,IPPROTO_IP,IP_HDRINCL,val,sizeof(one))<0){perror("setsockopt()error");exit(-1);}elseprintf("setsockopt()isOK.\n");//Sendloop,sendforevery2secondfor100countprintf("Trying...\n");printf("UsingrawsocketandUDPprotocol\n");printf("UsingSourceIP:%sport:%u,TargetIP:%sport:%u.\n",argv[1],atoi(argv[2]),argv[3],atoi(argv[4]));intcount;for(count=1;count<=20;count++){if(sendto(sd,buffer,ip->iph_len,0,(structsockaddr*)&sin,sizeof(sin))<0)//Verify{perror("sendto()error");exit(-1);}else{printf("Count#%u-sendto()isOK.\n",count);sleep(2);}}close(sd);return0;}Task3#include<pcap.h>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<ctype.h>#include<errno.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>/*defaultsnaplength(maximumbytesperpackettocapture)*/#include<unistd.h>#include<netinet/ip.h>#include<netinet/udp.h>#include<netdb.h>#include<netinet/in_systm.h>#include<netinet/ip.h>#include<netinet/ip_icmp.h>#include<arpa/inet.h>#defineAPP_NAME"sniffex"#defineAPP_DESC "Snifferexampleusinglibpcap"#defineAPP_COPYRIGHT "Copyright(c)2005TheTcpdumpGroup"#defineAPP_DISCLAIMER "THEREISABSOLUTELYNOWARRANTYFORTHISPROGRAM."#defineSNAP_LEN1518/*ethernetheadersarealwaysexactly14bytes[1]*/#defineSIZE_ETHERNET14/*Ethernetaddressesare6bytes*/#defineETHER_ADDR_LEN 6/*Ethernetheader*/char*dstip;char*srcip;structsniff_ethernet{u_charether_dhost[ETHER_ADDR_LEN];/*destinationhostaddress*/u_charether_shost[ETHER_ADDR_LEN];/*sourcehostaddress*/u_shortether_type;/*IP?ARP?RARP?etc*/};/*IPheader*/structsniff_ip{u_charip_vhl;/*version<<4|headerlength>>2*/u_charip_tos;/*typeofservice*/u_shortip_len;/*totallength*/u_shortip_id;/*identification*/u_shortip_off;/*fragmentoffsetfield*/#defineIP_RF0x8000/*reservedfragmentflag*/#defineIP_DF0x4000/*dontfragmentflag*/#defineIP_MF0x2000/*morefragmentsflag*/#defineIP_OFFMASK0x1fff/*maskforfragmentingbits*/u_charip_ttl;/*timetolive*/u_charip_p;/*protocol*/u_shortip_sum;/*checksum*/structin_addrip_src,ip_dst;/*sourceanddestaddress*/};#defineIP_HL(ip)(((ip)->ip_vhl)&0x0f)#defineIP_V(ip)(((ip)->ip_vhl)>>4)/*TCPheader*/typedefu_inttcp_seq;structsniff_tcp{u_shortth_sport;/*sourceport*/u_shortth_dport;/*destinationport*/tcp_seqth_seq;/*sequencenumber*/tcp_seqth_ack;/*acknowledgementnumber*/u_charth_offx2;/*dataoffset,rsvd*/#defineTH_OFF(th)(((th)->th_offx2&0xf0)>>4)u_charth_flags;#defineTH_FIN0x01#defineTH_SYN0x02#defineTH_RST0x04#defineTH_PUSH0x08#defineTH_ACK0x10#defineTH_URG0x20#defineTH_ECE0x40#defineTH_CWR0x80#defineTH_FLAGS(TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)u_shortth_win;/*window*/u_shortth_sum;/*checksum*/u_shortth_urp;/*urgentpointer*/};voidgot_packet(u_char*args,conststructpcap_pkthdr*header,constu_char*packet);voidprint_payload(constu_char*payload,intlen);voidprint_hex_ascii_line(constu_char*payload,intlen,intoffset);voidprint_app_banner(void);voidprint_app_usage(void);/**appname/banner*/voidprint_app_banner(void){ printf("%s-%s\n",APP_NAME,APP_DESC); printf("%s\n",APP_COPYRIGHT); printf("%s\n",APP_DISCLAIMER); printf("\n");return;}/**printhelptext*/voidprint_app_usage(void){ printf("Usage:%s[interface]\n",APP_NAME); printf("\n"); printf("Options:\n"); printf("interfaceListenon<interface>forpackets.\n"); printf("\n");return;}/**printdatainrowsof16bytes:offsethexascii**00000474554202f20485454502f312e310d0aGET/HTTP/1.1..*/voidprint_hex_ascii_line(constu_char*payload,intlen,intoffset){ inti; intgap; constu_char*ch; /*offset*/ printf("%05d",offset); /*hex*/ ch=payload; for(i=0;i<len;i++){ printf("%02x",*ch); ch++; /*printextraspaceafter8thbyteforvisualaid*/ if(i==7) printf(""); } /*printspacetohandlelinelessthan8bytes*/ if(len<8) printf(""); /*fillhexgapwithspacesifnotfullline*/if(len<16){ gap=16-len; for(i=0;i<gap;i++){ printf(""); } } printf(""); /*ascii(ifprintable)*/ ch=payload; for(i=0;i<len;i++){ if(isprint(*ch)) printf("%c",*ch); else printf("."); ch++; } printf("\n");return;}/**printpacketpayloaddata(avoidprintingbinarydata)*/voidprint_payload(constu_char*payload,intlen){ intlen_rem=len; intline_width=16; /*numberofbytesperline*/ intline_len; intoffset=0; /*zero-basedoffsetcounter*/ constu_char*ch=payload; if(len<=0) return; /*datafitsononeline*/ if(len<=line_width){ print_hex_ascii_line(ch,len,offset); return; } /*dataspansmultiplelines*/ for(;;){ /*computecurrentlinelength*/ line_len=line_width%len_rem; /*printline*/ print_hex_ascii_line(ch,line_len,offset); /*computetotalremaining*/ len_rem=len_rem-line_len; /*shiftpointertoremainingbytestoprint*/ ch=ch+line_len; /*addoffset*/ offset=offset+line_width; /*checkifwehavelinewidthcharsorless*/ if(len_rem<=line_width){ /*printlastlineandgetout*/ print_hex_ascii_line(ch,len_rem,offset); break; } }return;}/**dissect/printpacket*/voidgot_packet(u_char*args,conststructpcap_pkthdr*header,constu_char*packet){ staticintcount=1;/*packetcounter*/ /*declarepointerstopacketheaders*/ conststructsniff_ethernet*ethernet;/*Theethernetheader[1]*/ conststructsniff_ip*ip;/*TheIPheader*/ conststructsniff_tcp*tcp;/*TheTCPheader*/ constchar*payload;/*Packetpayload*/ intsize_ip; intsize_tcp; intsize_payload; printf("\nPacketnumber%d:\n",count); count++; /*defineethernetheader*/ ethernet=(structsniff_ethernet*)(packet); /*define/computeipheaderoffset*/ ip=(structsniff_ip*)(packet+14); size_ip=IP_HL(ip)*4; if(size_ip<20){ printf("*InvalidIPheaderlength:%ubytes\n",size_ip); return; } /*printsourceanddestinationIPaddresses*/ printf("From:%s\n",inet_ntoa(ip->ip_src));dstip=inet_ntoa(ip->ip_src);//printf("desip%s",dstip); printf("To:%s\n",inet_ntoa(ip->ip_dst));srcip=inet_ntoa(ip->ip_dst); /*determineprotocol*/ switch(ip->ip_p){ caseIPPROTO_TCP: printf("Protocol:TCP\n"); break; caseIPPROTO_UDP: printf("Protocol:UDP\n"); return; caseIPPROTO_ICMP: printf("Protocol:ICMP\n"); return; caseIPPROTO_IP: printf("Protocol:IP\n"); return; default: printf("Protocol:unknown\n"); return; } /* *OK,thispacketisTCP. */ /*define/computetcpheaderoffset*/ tcp=(structsniff_tcp*)(packet+14+size_ip); size_tcp=TH_OFF(tcp)*4; if(size_tcp<20){ printf("*InvalidTCPheaderlength:%ubytes\n",size_tcp); return; } printf("Srcport:%d\n",ntohs(tcp->th_sport)); printf("Dstport:%d\n",ntohs(tcp->th_dport)); /*define/computetcppayload(segment)offset*/ payload=(u_char*)(packet+14+size_ip+size_tcp); /*computetcppayload(segment)size*/ size_payload=ntohs(ip->ip_len)-(size_ip+size_tcp); /* *Printpayloaddata;itmightbebinary,sodon'tjust *treatitasastring. */ if(size_payload>0){ printf("Payload(%dbytes):\n",size_payload); print_payload(payload,size_payload); }return;}intmain(intargc,char**argv){ char*dev=NULL; /*capturedevicename*/ charerrbuf[PCAP_ERRBUF_SIZE]; /*errorbuffer*/ pcap_t*handle; /*packetcapturehandle*/ charfilter_exp[]="icmp"; /*filterexpression*/ structbpf_programfp; /*compiledfilterprogram(expression)*/ bpf_u_int32mask; /*subnetmask*/ bpf_u_int32net; /*ip*/ intnum_packets=1; /*numberofpacketstocapture*/ print_app_banner(); /*checkforcapturedevicenameoncommand-line*/ if(argc==2){ dev=argv[1]; } elseif(argc>2){ fprintf(stderr,"error:unrecognizedcommand-lineoptions\n\n"); print_app_usage(); exit(EXIT_FAILURE); } else{ /*findacapturedeviceifnotspecifiedoncommand-line*/ dev=pcap_lookupdev(errbuf); if(dev==NULL){ fprintf(stderr,"Couldn'tfinddefaultdevice:%s\n",errbuf); exit(EXIT_FAILURE); } } /*getnetworknumberandmaskassociatedwithcapturedevice*/ if(pcap_lookupnet(dev,&net,&mask,errbuf)==-1){ fprintf(stderr,"Couldn'tgetnetmaskfordevice%s:%s\n", dev,errbuf); net=0; mask=0; } /*printcaptureinfo*/ printf("Device:%s\n",dev); printf("Numberofpackets:%d\n",num_packets); printf("Filterexpression:%s\n",filter_exp); handle=pcap_open_live(dev,1518,1,1000,errbuf); if(handle==NULL){ fprintf(stderr,"Couldn'topendevice%s:%s\n",dev,errbuf); exit(EXIT_FAILURE); } /*makesurewe'recapturingonanEthernetdevice[2]*/ if(pcap_datalink(handle)!=DLT_EN10MB){ fprintf(stderr,"%sisnotanEthernet\n",dev); exit(EXIT_FAILURE); } /*compilethefilterexpression*/ if(pcap_compile(handle,&fp,filter_exp,0,net)==-1){ fprintf(stderr,"Couldn'tparsefilter%s:%s\n",filter_exp,pcap_geterr(handle)); exit(EXIT_FAILURE); } /*applythecompiledfilter*/ if(pcap_setfilter(handle,&fp)==-1){
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024学校实验室设备更新及维修服务合同3篇
- 2024店铺转让协议书
- 2024模具智能制造技术研发合同
- 2024标准版两居室房车短期租赁合同版
- 2024服装工装定制合同
- 2024青岛运动会官方用车租赁服务协议3篇
- 2024年行车设备安装与维护合同3篇
- 2024年版城市供水项目特许经营权协议
- 2024运营总监国际业务拓展与跨国合作合同3篇
- 2025年度网络安全技术股权合作与转让合同3篇
- 《低压电工技术》课程标准
- 22G101系列图集常用点全解读
- (国家基本公共卫生服务项目第三版)7高血压患者健康管理服务规范
- 12 富起来到强起来 精神文明新风尚(说课稿)-部编版道德与法治五年级下册
- (43)-7.2羊肚菌高效栽培
- 中级消防维保理论考试试题题库及答案
- 读书会熵减华为活力之源
- 竣工图绘制规范及标准
- 二年级上学期数学
- GB/T 37433-2019低功率燃油燃烧器通用技术要求
- GB/T 3098.5-2000紧固件机械性能自攻螺钉
评论
0/150
提交评论