抓包程序(C++版本)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includeWINSOCK2.H#includeiostream#includestdio.h#includewinsock2.h#pragmacomment(lib,WS2_32)#pragmacomment(lib,Mpr)usingnamespacestd;//IPÊý¾Ý±¨Êײ¿¸ñʽtypedefstruct_IPHEADER{unsignedcharheader_len:4;unsignedcharversion:4;unsignedchartos;//typeofserviceunsignedshorttotal_len;//lengthofthepacketunsignedshortident;//uniqueidentifierunsignedshortflags;unsignedcharttl;unsignedcharproto;//protocol(IP,TCP,UDPetc)unsignedshortchecksum;unsignedintsourceIP;unsignedintdestIP;//unsignedchartext[500];//stringdata;}IPHEADER;//TCPÊײ¿¸ñʽstructTCPPacketHead{WORDSourPort;WORDDestPort;DWORDSeqNo;DWORDAckNo;BYTEHLen;BYTEFlag;WORDWndSize;WORDChkSum;WORDUrgPtr;};//ICMPÊײ¿¸ñʽstructICMPPacketHead{BYTEType;BYTECode;WORDChkSum;};//UDPÊײ¿¸ñʽstructUDPPacketHead{WORDSourPort;WORDDestPort;WORDLen;WORDChkSum;};//×¥°ü³ÌÐò²¿·Öintmain(){WORDwVersionRequested;WSADATAwsaData;interr;CHARszHostName[128]={0};HOSTENT*pHost=NULL;CHAR*pszIp=NULL;intiNum=0;SOCKADDR_INsa;wVersionRequested=MAKEWORD(2,0);err=WSAStartup(wVersionRequested,&wsaData);//³õʼ»¯winsock2SOCKETssocket;ssocket=socket(AF_INET,SOCK_RAW,IPPROTO_IP);intrcvtimeo=5000;//5secinsteadof45asdefault//ÉèÖÃÌ×½Ó¿Úsetsockopt(ssocket,SOL_SOCKET,SO_RCVTIMEO,(constchar*)&rcvtimeo,sizeof(rcvtimeo));sa.sin_family=AF_INET;sa.sin_port=htons(7000);pHost=gethostbyname(szHostName);//»ñÈ¡±¾µØIPµØÖ·pszIp=inet_ntoa(*(in_addr*)pHost-h_addr_list[1]);sa.sin_addr.s_addr=inet_addr(pszIp);//Óë±¾µØÖ÷»ú½øÐаó¶¨bind(ssocket,(PSOCKADDR)&sa,sizeof(sa));DWORDdwBufferLen[10];DWORDdwBufferInLen=1;DWORDdwBytesReturned=0;//¿ØÖÆÒ»¸öÌ×½Ó¿ÚµÄģʽWSAIoctl(ssocket,_WSAIOW(IOC_VENDOR,1),&dwBufferInLen,sizeof(dwBufferInLen),&dwBufferLen,sizeof(dwBufferLen),&dwBytesReturned,NULL,NULL);charbuf[1000],*bufwork;char*pSource,*pDest;charszSource[16],szDest[16],szErr[50];//chartext[500];//char*itext1;//ÉèÖüàÌýÊý¾Ý°üµÄÊýÄ¿for(inti=0;i20;i++){memset(buf,0,sizeof(buf));intlen=recv(ssocket,buf,sizeof(buf),0);bufwork=buf;IPHEADER*pIpHeader=(IPHEADER*)bufwork;in_addrina;ina.S_un.S_addr=pIpHeader-sourceIP;pSource=inet_ntoa(ina);strcpy(szSource,pSource);ina.S_un.S_addr=pIpHeader-destIP;pDest=inet_ntoa(ina);strcpy(szDest,pDest);intlength=pIpHeader-header_len;//itext1=pIpHeader-text;//strcpy(itext,itext1);//printf(datalen%dlen%dIP%sip%s,len,pIpHeader-header_len,szSource,szDest);coutdatalen:len;len:length;Ô´µØÖ·:szSource;Ä¿µÄµØÖ·szDest;//printf(\n);coutendl;//strings=pIpHeader-data;//chars1[100];//strcpy(text,s.c_str());//coutbufendl;intHdrLen,totallen;WORDsourport,destport;structTCPPacketHead*pTCPHead;structICMPPacketHead*pICMPHead;structUDPPacketHead*pUDPHead;HdrLen=pIpHeader-header_len&0xf;HdrLen*=4;switch(pIpHeader-proto){caseIPPROTO_ICMP:{pICMPHead=(structICMPPacketHead*)(buf+HdrLen);//½âÎöICMPЭÒébreak;}caseIPPROTO_TCP:{pTCPHead=(structTCPPacketHead*)(buf+HdrLen);sourport=ntohs(pTCPHead-SourPort);destport=ntohs(pTCPHead-DestPort);printf(tcpsourport%ddestport%d,sourport,destport);//½âÎötcpЭÒébreak;}caseIPPROTO_UDP:{pUDPHead=(structUDPPacketHead*)(buf+HdrLen);//½âÎöUDPЭÒébreak;}}printf(\n);printf(\n);}return0;}

1 / 4
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功