织梦CMS - 轻松建站从此开始!

罗索

原始套接字的IPv4的抓包程序

落鹤生 发布于 2010-07-07 09:46 点击:次 
一个实现原始套接字的IPv4的抓包程序的源代码。
TAG:

//sniffer.h

 

  1. #ifndef _H_SINIFERR_H_ 
  2. #define _H_SINIFERR_H_ 
  3.  
  4. #include <winsock2.h> 
  5.  
  6. typedef struct IP_HEADER 
  7.     u_char hl:4, /*头部长度,是little-endian,所以hl在前*/ 
  8.             version:4; /*版本*/ 
  9.     u_char tos; /*服务类型*/ 
  10.     u_short length; /*包长*/ 
  11.     u_short id;   /*标识*/ 
  12.     u_short fragment; /*分段*/ 
  13.     u_char TTL; /*生存期*/ 
  14.     u_char protocol; /*下一个头的协议*/ 
  15.     u_short chksum; /*检验和*/ 
  16.     IN_ADDR source; /*源地址*/ 
  17.     IN_ADDR dest; /*目的地址*/ 
  18.  
  19. }IP_HEADER; 
  20.  
  21. char* IPv4_Protocal[103]={ 
  22.    "Reserved",   //0   Reserved                              [JBP] 
  23.    "ICMP",    //1   Internet Control Message       [RFC792,JBP] 
  24.    "IGMP",    //2   Internet Group Management     [RFC1112,JBP] 
  25.    "GGP",    //3   Gateway-to-Gateway              [RFC823,MB] 
  26.    "IP",    //4   IP in IP (encasulation)               [JBP] 
  27.    "ST",    //5   Stream                 [RFC1190,IEN119,JWF] 
  28.    "TCP",    //6   Transmission Control           [RFC793,JBP] 
  29.    "UCL",    //7   UCL                                    [PK] 
  30.    "EGP",    //8   Exterior Gateway Protocol     [RFC888,DLM1] 
  31.    "IGP",    //9   any private interior gateway          [JBP] 
  32.    "BBN-RCC-MON"//10 BBN RCC Monitoring                    [SGC] 
  33.    "NVP-II",   //11 Network Voice Protocol         [RFC741,SC3] 
  34.    "PUP",    //12 PUP                             [PUP,XEROX] 
  35.    "ARGUS",   //13 ARGUS                                [RWS4] 
  36.    "EMCON",   //14 EMCON                                 [BN7] 
  37.    "XNET",    //15 Cross Net Debugger            [IEN158,JFH2] 
  38.    "CHAOS",   //16 Chaos                                 [NC3] 
  39.    "UDP",    //17 User Datagram                  [RFC768,JBP] 
  40.    "MUX",    //18 Multiplexing                    [IEN90,JBP] 
  41.    "DCN-MEAS",   //19 DCN Measurement Subsystems           [DLM1] 
  42.    "HMP",    //20 Host Monitoring                [RFC869,RH6] 
  43.    "PRM",    //21 Packet Radio Measurement              [ZSU] 
  44.    "XNS-IDP",   //22 XEROX NS IDP               [ETHERNET,XEROX] 
  45.    "TRUNK-1",   //23 Trunk-1                              [BWB6] 
  46.    "TRUNK-2",   //24 Trunk-2                              [BWB6] 
  47.    "LEAF-1",   //25 Leaf-1                               [BWB6] 
  48.    "LEAF-2",   //26 Leaf-2                               [BWB6] 
  49.    "RDP",    //27 Reliable Data Protocol         [RFC908,RH6] 
  50.    "IRTP",    //28 Internet Reliable Transaction [RFC938,TXM] 
  51.    "ISO-TP4",   //29 ISO Transport Protocol Class 4 [RFC905,RC77] 
  52.    "NETBLT",   //30 Bulk Data Transfer Protocol    [RFC969,DDC1] 
  53.    "MFE-NSP",   //31 MFE Network Services Protocol [MFENET,BCH2] 
  54.    "MERIT-INP"//32 MERIT Internodal Protocol             [HWB] 
  55.    "SEP",    //33 Sequential Exchange Protocol        [JC120] 
  56.    "3PC",    //34 Third Party Connect Protocol         [SAF3] 
  57.    "IDPR",    //35 Inter-Domain Policy Routing Protocol [MXS1] 
  58.    "XTP",    //36 XTP                                   [GXC] 
  59.    "DDP",    //37 Datagram Delivery Protocol            [WXC] 
  60.    "IDPR-CMTP"//38 IDPR Control Message Transport Proto [MXS1] 
  61.    "TP++",    //39 TP++ Transport Protocol               [DXF] 
  62.    "IL",    //40 IL Transport Protocol                [DXP2] 
  63.    "SIP",    //41 Simple Internet Protocol              [SXD] 
  64.    "SDRP",    //42 Source Demand Routing Protocol       [DXE1] 
  65.    "SIP-SR",   //43 SIP Source Route                      [SXD] 
  66.    "SIP-FRAG",   //44 SIP Fragment                          [SXD] 
  67.    "IDRP",    //45 Inter-Domain Routing Protocol   [Sue Hares] 
  68.    "RSVP",    //46 Reservation Protocol           [Bob Braden] 
  69.    "GRE",    //47 General Routing Encapsulation     [Tony Li] 
  70.    "MHRP",    //48 Mobile Host Routing Protocol[David Johnson] 
  71.    "BNA",    //49 BNA                          [Gary Salamon] 
  72.    "SIPP-ESP",   //50 SIPP Encap Security Payload [Steve Deering] 
  73.    "SIPP-AH",   //51 SIPP Authentication Header [Steve Deering] 
  74.    "I-NLSP",   //52 Integrated Net Layer Security TUBA [GLENN] 
  75.    "SWIPE",   //53 IP with Encryption                    [JI6] 
  76.    "NHRP",    //54 NBMA Next Hop Resolution Protocol 
  77.    "Unassigned",   //55 Unassigned                            [JBP] 
  78.    "Unassigned",   //56 Unassigned 
  79.    "Unassigned",   //57 Unassigned 
  80.    "ICMPv6",       //58 ICMPv6 
  81.    "Unassigned",   //59 Unassigned 
  82.    "Unassigned",   //60 Unassigned 
  83.    "61",           //61 any host internal protocol            [JBP] 
  84.    "CFTP",    //62 CFTP                            [CFTP,HCF2] 
  85.    "63",           //63 any local network                     [JBP] 
  86.    "SAT-EXPAK"//64 SATNET and Backroom EXPAK             [SHB] 
  87.    "KRYPTOLAN"//65 Kryptolan                            [PXL1] 
  88.    "RVD",    //66 MIT Remote Virtual Disk Protocol      [MBG] 
  89.    "IPPC",    //67 Internet Pluribus Packet Core         [SHB] 
  90.    "68",           //68 any distributed file system           [JBP] 
  91.    "SAT-MON",   //69 SATNET Monitoring                     [SHB] 
  92.    "VISA",    //70 VISA Protocol                        [GXT1] 
  93.    "IPCV",    //71 Internet Packet Core Utility          [SHB] 
  94.    "CPNX",    //72 Computer Protocol Network Executive [DXM2] 
  95.    "CPHB",    //73 Computer Protocol Heart Beat         [DXM2] 
  96.    "WSN",    //74 Wang Span Network                     [VXD] 
  97.    "PVP",    //75 Packet Video Protocol                 [SC3] 
  98.    "BR-SAT-MON"//76 Backroom SATNET Monitoring            [SHB] 
  99.    "SUN-ND",   //77 SUN ND PROTOCOL-Temporary             [WM3] 
  100.    "WB-MON",   //78 WIDEBAND Monitoring                   [SHB] 
  101.    "WB-EXPAK",   //79 WIDEBAND EXPAK                        [SHB] 
  102.    "ISO-IP",   //80 ISO Internet Protocol                 [MTR] 
  103.    "VMTP",    //81 VMTP                                 [DRC3] 
  104.    "SECURE-VMTP"//82 SECURE-VMTP                          [DRC3] 
  105.    "VINES",   //83 VINES                                 [BXH] 
  106.    "TTP",    //84 TTP                                   [JXS] 
  107.    "NSFNET-IGP"//85 NSFNET-IGP                            [HWB] 
  108.    "DGP",    //86 Dissimilar Gateway Protocol     [DGP,ML109] 
  109.    "TCF",    //87 TCF                                  [GAL5] 
  110.    "IGRP",    //88 IGRP                            [CISCO,GXS] 
  111.    "OSPFIGP",   //89 OSPFIGP                      [RFC1583,JTM4] 
  112.    "Sprite-RPC"//90 Sprite RPC Protocol            [SPRITE,BXW] 
  113.    "LARP",    //91 Locus Address Resolution Protocol     [BXH] 
  114.    "MTP",    //92 Multicast Transport Protocol          [SXA] 
  115.    "AX.25",   //93 AX.25 Frames                         [BK29] 
  116.    "IPIP",    //94 IP-within-IP Encapsulation Protocol   [JI6] 
  117.    "MICP",    //95 Mobile Internetworking Control Pro.   [JI6] 
  118.    "SCC-SP",   //96 Semaphore Communications Sec. Pro.    [HXH] 
  119.    "ETHERIP",   //97 Ethernet-within-IP Encapsulation     [RXH1] 
  120.    "ENCAP",   //98 Encapsulation Header         [RFC1241,RXB3] 
  121.    "99",    //99 any private encryption scheme         [JBP] 
  122.    "GMTP",    //100 GMTP                                 [RXB5] 
  123.    "Unassigned"//101 Unassigned                            [JBP] 
  124.    "Reserved"}; //102 Reserved                              [JBP]*/ 
  125.  
  126. #endif 

////main.c

 

  1. #include "sniffer.h" 
  2. #include <stdio.h> 
  3. #include <string.h> 
  4. #include <Mstcpip.h>//#define   SIO_RCVALL _WSAIOW(IOC_VENDOR,1) 
  5.  
  6. int main() 
  7.     char        hostname[100];//主机名 
  8.     HOSTENT     *local;//本地主机信息 
  9.  
  10.     //创建socket的相关数据 
  11.     WSADATA     WsaData; 
  12.     WORD        wVersionRequested; 
  13.     SOCKET      Sock; 
  14.     SOCKADDR_IN SockAddr; 
  15.  
  16.     //设置套接字模式的相关数据 
  17.     DWORD       dwInBuffer=1; 
  18.     DWORD       dwBytesReturned=0; 
  19.  
  20.     char        Buffer[65536];//接受数据缓冲区 
  21.     IP_HEADER   *IpHdr;//IP包头 
  22.     int         Ret;//返回值 
  23.     int         Cnt=0;//计数器 
  24.  
  25.  
  26.     u_short     off; 
  27.     u_char      Version;//版本 
  28.     u_char      IHL;//报头长度 
  29.     u_char      TypeOfSev;//服务类型 
  30.     u_short     TotalLen;//总长度 
  31.     u_short     Identification;//标识 
  32.     u_short     Flag; 
  33.     u_short     FragmentOffset;//偏移 
  34.     u_char      TimeToLive;//生存期 
  35.     u_char      Protocal;//协议 
  36.     u_short     HeaderChk;//检验和 
  37.     char        SrcAddr[16];//源地址 
  38.     char        DesAddr[16];//目的地址 
  39.     FILE        *File=NULL; 
  40.    
  41.     //初始化socket库 
  42.     wVersionRequested=MAKEWORD(2,2); 
  43.     Ret=WSAStartup(wVersionRequested,&WsaData); 
  44.     if(Ret!=0) 
  45.     { 
  46. printf("初始化socket库失败:%d\n",WSAGetLastError()); 
  47. goto over; 
  48.     } 
  49.     else 
  50. printf("初始化socket库成功!\n"); 
  51.  
  52.     printf("\n====================================\n"); 
  53.     //获取主机名 
  54.     if (gethostname(hostname, sizeof(hostname)) == SOCKET_ERROR) 
  55.     { 
  56. printf("获取主机名出错 : %d",WSAGetLastError()); 
  57. return -1; 
  58.     } 
  59.     else 
  60. printf("主机名:%s \n",hostname); 
  61.  
  62.     //获取本地主机IP 
  63.     local=gethostbyname(hostname); 
  64.     if(local==NULL) 
  65.     { 
  66. printf("获取主机IP出错 : %d\n",WSAGetLastError()); 
  67. return -1; 
  68.     } 
  69.     else 
  70.     { 
  71. printf("获取主机IP有: \n"); 
  72. for(Ret=0;local->h_addr_list[Ret]!=0;Ret++) 
  73.      printf("IP%d:%s\n",Ret,inet_ntoa( *(IN_ADDR*)local->h_addr_list[Ret]));     
  74.     } 
  75.     printf("====================================\n\n"); 
  76.  
  77.     Sock=socket(AF_INET,SOCK_RAW,IPPROTO_IP);//创建原始套接字 
  78.     if(Sock==INVALID_SOCKET) 
  79.     { 
  80. printf("创建RAW socket失败:%d\n",WSAGetLastError()); 
  81. goto over; 
  82.     } 
  83.     else 
  84. printf("创建RAW socket成功!\n"); 
  85.  
  86.     //SockAddr.sin_addr.S_un.S_addr=inet_addr("192.168.7.190");
  87. //htonl(INADDR_ANY);//设置接受数据的IP 
  88.     //设置要监听的IP 
  89.     memcpy(&SockAddr.sin_addr.S_un.S_addr,local->h_addr_list[0],
  90. sizeof(SockAddr.sin_addr.S_un.S_addr)); 
  91.     SockAddr.sin_family=AF_INET; 
  92.     SockAddr.sin_port=htons(10000); 
  93.  
  94.     Ret=bind(Sock,(PSOCKADDR_IN)&SockAddr,sizeof(SockAddr)); 
  95.     if(Ret!=0) 
  96.     { 
  97. printf("绑定socket失败:%d\n",WSAGetLastError()); 
  98. goto over; 
  99.     } 
  100.     else 
  101. printf("绑定socket成功!\n"); 
  102.  
  103.     Ret=WSAIoctl(Sock,SIO_RCVALL,&dwInBuffer,sizeof(dwInBuffer)
  104. ,NULL,0,&dwBytesReturned,NULL,NULL); 
  105.     if(Ret!=0) 
  106.     { 
  107. printf("设置socket失败!:%d\n",WSAGetLastError()); 
  108. goto over; 
  109.     } 
  110.     else 
  111. printf("设置socket成功!\n"); 
  112.     
  113.  
  114.     File=fopen("Output.txt","a+"); 
  115.     if(File==NULL) 
  116. goto over; 
  117.     
  118.     while(1) 
  119.     { 
  120. Ret=recv(Sock,Buffer,65535,0); 
  121. if(Ret>0) 
  122.      Cnt++; 
  123.      IpHdr=(IP_HEADER*)Buffer; 
  124.  
  125.      Version=IpHdr->version; 
  126.  
  127.      IHL=IpHdr->hl; 
  128.  
  129.      TypeOfSev=IpHdr->tos; 
  130.  
  131.      TotalLen=ntohs(IpHdr->length); 
  132.  
  133.      Identification=ntohs(IpHdr->id); 
  134.  
  135.      off=ntohs(IpHdr->fragment); 
  136.      off&=0XE000; 
  137.      Flag=off>>13; 
  138.  
  139.      off=ntohs(IpHdr->fragment); 
  140.      off&=0X1FFF; 
  141.      FragmentOffset=off; 
  142.  
  143.      TimeToLive=IpHdr->TTL; 
  144.  
  145.      Protocal=IpHdr->protocol; 
  146.      if(Protocal>102) 
  147.    Protocal=102; 
  148.  
  149.      HeaderChk=ntohs(IpHdr->chksum); 
  150.  
  151.      memset(SrcAddr,0,16); 
  152.      memset(DesAddr,0,16); 
  153.      strcpy(SrcAddr,inet_ntoa(IpHdr->source)); 
  154.      strcpy(DesAddr,inet_ntoa(IpHdr->dest)); 
  155.     
  156.      printf("\n=================%d================\n",Cnt); 
  157.      printf("          版本:IPv%d\n",Version); 
  158.      printf("      头部长度:%d\n",IHL); 
  159.      printf("      服务类型:%d\n",TypeOfSev); 
  160.      printf("          包长:%d\n",TotalLen); 
  161.      printf("          标识:%d\n",Identification); 
  162.      printf("          标志:%d\n",Flag); 
  163.      printf("          分段:%d\n",FragmentOffset); 
  164.      printf("        生存期:%d\n",TimeToLive); 
  165.      printf("下一个头的协议:%s\n",IPv4_Protocal[Protocal]); 
  166.      printf("        检验和:%d\n",HeaderChk); 
  167.      printf("        源地址:%s\n",SrcAddr); 
  168.      printf("      目的地址:%s\n",DesAddr); 
  169.      printf("====================================\n"); 
  170.      if(Protocal==6||Protocal==17) 
  171.      { 
  172.    fprintf(File,"\n=================%d================\n",Cnt); 
  173.    fprintf(File,"          版本:IPv%d\n",Version); 
  174.    fprintf(File,"      头部长度:%d\n",IHL); 
  175.    fprintf(File,"      服务类型:%d\n",TypeOfSev); 
  176.    fprintf(File,"          包长:%d\n",TotalLen); 
  177.    fprintf(File,"          标识:%d\n",Identification); 
  178.    fprintf(File,"          标志:%d\n",Flag); 
  179.    fprintf(File,"          分段:%d\n",FragmentOffset); 
  180.    fprintf(File,"        生存期:%d\n",TimeToLive); 
  181.    fprintf(File,"下一个头的协议:%s\n",IPv4_Protocal[Protocal]); 
  182.    fprintf(File,"        检验和:%d\n",HeaderChk); 
  183.    fprintf(File,"        源地址:%s\n",SrcAddr); 
  184.    fprintf(File,"      目的地址:%s\n",DesAddr); 
  185.    fprintf(File,"====================================\n"); 
  186.      } 
  187.     } 
  188.  
  189. over: 
  190.     if(Sock) 
  191.        closesocket(Sock); 
  192.     if(File) 
  193. fclose(File); 
  194.     WSACleanup(); 
  195.  
  196.     return 0; 

 

(chenjily)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201007/9792.html]
本文出处:百度博客 作者:chenjily
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容