鱼C论坛

 找回密码
 立即注册
查看: 1312|回复: 0

原始套接字抓包源码求 大神修改 VS2010

[复制链接]
发表于 2015-3-17 08:46:29 | 显示全部楼层 |阅读模式
50鱼币

  1. <p><div class="blockcode"><blockquote>#include<iostream>
  2. #include<stdio.h>
  3. #include<winsock2.h>
  4. #include<ws2tcpip.h>
  5. #pragma comment(lib,"ws2_32")

  6. #define IO_RCVALL _WSAIOW(IOC_VENDOR,1)

  7. typedef struct tcp_hdr
  8. {
  9.     unsigned short    th_sport;
  10.     unsigned short    th_dport;
  11.     unsigned int    th_seq;
  12.     unsigned int    th_ack;
  13.     unsigned char    th_lenres;
  14.     unsigned char    th_flag;
  15.     unsigned short    th_win;
  16.     unsigned short    th_sum;
  17.     unsigned short    th_urp;
  18. }TCP_HEADER;

  19. typedef struct IP_HEAD
  20. {
  21. union
  22. {
  23.   unsigned char Version;
  24.   unsigned char HeadLen;
  25. };
  26. unsigned char ServiceType;
  27. unsigned short TotalLen;
  28. unsigned short Identifier;
  29. union
  30. {
  31.   unsigned short Flags;
  32.   unsigned short FragOffset;
  33. };
  34. unsigned char TimeToLive;
  35. unsigned char Protocol;
  36. unsigned short HeadChecksum;
  37. unsigned int SourceAddr;
  38. unsigned int DestinAddr;
  39. unsigned char Options;
  40. }ip_head;

  41. void main(int argc,char *argv[])
  42. {
  43. using namespace std;
  44. argc=2;
  45. if(argc!=2)
  46. {
  47.   cout<<endl<<"请以下格式输入命令行:PackParse packet_sum"<<endl;
  48.   return;
  49. }
  50. WSADATA WSAData;
  51. if(WSAStartup(MAKEWORD(2,2), &WSAData)!=0)
  52. {
  53.   cout<<endl<<"WSASTartup初始化失敗"<<endl;
  54.   return;
  55. }
  56. SOCKET sock=socket(AF_INET,SOCK_RAW,IPPROTO_IP);//創建原始套接字
  57. if(sock==INVALID_SOCKET)
  58. {
  59.   cout<<endl<<"創建Socket失敗!"<<endl;
  60.   goto exit_clean;
  61. }
  62. BOOL flag=TRUE;
  63. if(setsockopt(sock,IPPROTO_IP,IP_HDRINCL,(char*) &flag,sizeof(flag))==SOCKET_ERROR)//設置端口
  64. {
  65.   cout<<endl<<"setsockopt操作失敗:"<<WSAGetLastError()<<endl;
  66.   goto exit_clean;
  67. }
  68. char hostName[128];
  69. if(gethostname(hostName,100)==SOCKET_ERROR)//獲取主機名稱
  70. {
  71.   cout<<endl<<"gethostname操作失敗:"<<WSAGetLastError()<<endl;
  72.   goto exit_clean;
  73. }
  74. hostent *pHostIP;
  75. if((pHostIP=gethostbyname(hostName))==NULL)//獲取主機對應IP地址
  76. {
  77.   cout<<endl<<"gethostbyname操作失敗:"<<WSAGetLastError()<<endl;
  78.   goto exit_clean;
  79. }
  80. sockaddr_in host_addr;
  81. host_addr.sin_family=AF_INET;
  82. host_addr.sin_port=htons(8000);
  83. host_addr.sin_addr=*(in_addr *)pHostIP->h_addr_list[0];
  84. if(bind(sock,(SOCKADDR*)&host_addr,sizeof(host_addr))==SOCKET_ERROR)
  85. {
  86.   cout<<endl<<"bind操作失敗:"<<WSAGetLastError()<<endl;
  87.   goto exit_clean;
  88. }
  89. // char buffer1[65535];
  90. // recv(sock,buffer1,65535,0);
  91. DWORD dwBufferLen[10];
  92. DWORD dwBufferInLen=1;
  93. DWORD dwBytesReturned=0;
  94. if(WSAIoctl(sock,IO_RCVALL ,&dwBufferInLen ,sizeof(dwBufferInLen) ,&dwBufferLen,sizeof(dwBufferLen),&dwBytesReturned,NULL,NULL)==SOCKET_ERROR)
  95. {
  96.   cout<<endl<<"WSAIoctl操作失敗:"<<WSAGetLastError()<<endl;
  97.   goto exit_clean;
  98. }
  99. cout<<endl<<"開始解析IP包:"<<endl;
  100. char buffer[65535];
  101. int packsum=atoi("1231");//

  102. for(int i=0;i<packsum;i++)
  103. {
  104.          p:
  105.   if(recv(sock,buffer,65535,0)>0)
  106.   {
  107.    ip_head ip=*(ip_head *)buffer;
  108.    if((int)ip.Protocol!=IPPROTO_TCP)
  109.    {
  110.            printf("no Smtp\n");
  111.            goto p;
  112.    }
  113.    printf("is Smet");
  114.    cout<<"-----------------------"<<endl;
  115.    cout<<"版本:"<<(ip.Version>>4)<<endl;
  116.    cout<<"頭部長度:"<<((ip.HeadLen &0x0f)*4)<<endl;
  117.    cout<<"服務類型:Priority"<<(ip.ServiceType>>5)<<", Service"<<((ip.ServiceType>>1)&0x0f)<<endl;
  118.    cout<<"總長度:"<<ip.TotalLen<<endl;
  119.    cout<<"標示符:"<<ip.Identifier<<endl;
  120.    cout<<"標志位:"<<((ip.Flags>>15)&0x01)<<",DF= "<<((ip.Flags>>14)&0x01)<<",Mf="<<((ip.Flags>>13)&0x01)<<endl;
  121.    cout<<"偏移位:"<<(ip.FragOffset&0x1fff)<<endl;
  122.    cout<<"生存周期:"<<(int)ip.TimeToLive<<endl;
  123.    cout<<"協議:Protocol: "<<(int)ip.Protocol<<endl;
  124.    switch((int)ip.Protocol)
  125.    {
  126.    case IPPROTO_TCP:
  127.            {
  128.                    printf("----------------TCP----------------- \n");
  129.                                 /*获取 TCP 相关数据 ??????*/
  130.                    goto to;  
  131.            }
  132.         case IPPROTO_ICMP:
  133.            {
  134.                    printf("---------------------ICMP--------------- \n");
  135.                    goto to;  
  136.            }
  137.                    case IPPROTO_CBT:
  138.            {
  139.                    printf("------------------CBT---------------- \n");
  140.                    goto to;  
  141.            }
  142.                    case IPPORT_FTP:
  143.            {
  144.                    printf("-----------------FTP------------- \n");
  145.                    goto to;  
  146.            }
  147.                    case IPPORT_SMTP:
  148.            {
  149.                    printf("------------------SMTP------------------ \n");
  150.                    goto to;  
  151.            }   
  152.    }
  153.   to:
  154.    cout<<"頭部校驗和:"<<ip.HeadChecksum<<endl;
  155.    cout<<"原地址:"<<inet_ntoa(*(in_addr *)&ip.SourceAddr)<<endl;
  156.    cout<<"目的IP地址:"<<inet_ntoa(*(in_addr *)&ip.DestinAddr)<<endl;
  157.    printf("buffer=%d\n",ip.Protocol);
  158.   }
  159. }
  160. Sleep(10000);
  161. exit_clean:
  162. closesocket(sock);
  163. WSACleanup();
  164. }
复制代码




小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-19 11:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表