实现功能:抓取分析TCP包

编译方法:

#gcc -c main.c tcp.c ip.c ether.c

#gcc -g -Wall -o mian mian.o ip.o tcp.o ether.o -lpcap

运行方法:

#./main eth2

main.c

  1. #include <sys/types.h>  
  2. #include <arpa/inet.h>  
  3. #include <pcap.h>    
  4. #include <time.h>    
  5. #include <stdlib.h>    
  6. #include <stdio.h>    
  7. #include "lib.h"   
  8.     
  9. int main(int argc,char *argv[])    
  10. {    
  11.   char errBuf[PCAP_ERRBUF_SIZE], * devStr;  
  12.  
  13.   if(argc>1){  
  14.     devStr = argv[1];  
  15.   }  
  16.   else{   
  17.   /* get a device */    
  18.     devStr = pcap_lookupdev(errBuf);    
  19.   }  
  20.  
  21.   if(devStr)    
  22.   {    
  23.     printf("success: device: %s\n", devStr);    
  24.   }    
  25.   else    
  26.   {    
  27.     printf("error: %s\n", errBuf);    
  28.     exit(1);    
  29.   }    
  30.       
  31.   /* open a device, wait until a packet arrives */    
  32.   pcap_t * device = pcap_open_live(devStr, 65535, 1, 0, errBuf);    
  33.       
  34.   if(!device)    
  35.   {    
  36.     printf("error: pcap_open_live(): %s\n", errBuf);    
  37.     exit(1);    
  38.   }    
  39.       
  40.   /* construct a filter */    
  41.   struct bpf_program filter;    
  42.   pcap_compile(device, &filter, "port 80", 1, 0);    
  43.   pcap_setfilter(device, &filter);    
  44.       
  45.   /* wait loop forever */    
  46.   int id = 0;    
  47.   pcap_loop(device, -1, getPacket, (u_char*)&id);    
  48.       
  49.   pcap_close(device);    
  50.     
  51.   return 0;    
  52. }  

ip.c

 

  1. #include <sys/types.h>  
  2. #include <arpa/inet.h>  
  3. #include <pcap.h>    
  4. #include <time.h>    
  5. #include <stdlib.h>    
  6. #include <stdio.h>    
  7.  
  8. struct ip_header{  
  9.   u_int8_t ip_version: 4,ip_header_length: 4;  
  10.   u_int8_t ip_tos;  
  11.   u_int16_t ip_length;  
  12.   u_int16_t ip_id;  
  13.   u_int16_t ip_off;  
  14.   u_int8_t ip_ttl;  
  15.   u_int8_t ip_protocol;  
  16.   u_int16_t ip_checksum;  
  17.   struct in_addr ip_source_address;  
  18.   struct in_addr ip_destination_address;  
  19. };  
  20.  
  21. void ip_protocol_packet_callback(u_char *argument, const struct pcap_pkthdr *packet_header, const u_char *packet_content)  
  22. {  
  23.   struct ip_header *ip_protocol;  
  24.   u_int header_length;  
  25.   u_int offset;  
  26.   u_char tos;  
  27.   u_int16_t checksum;  
  28.   ip_protocol=(struct ip_header*)(packet_content + 14);  
  29.   checksum=ntohs(ip_protocol->ip_checksum);  
  30.   header_length = ip_protocol->ip_header_length * 4;  
  31.   tos=ip_protocol->ip_tos;  
  32.   offset=ntohs(ip_protocol->ip_off);  
  33.   printf("IP Version:%d\n",ip_protocol->ip_version);  
  34.   printf("Header length:%d\n",header_length);  
  35.   printf("TOS:%d\n",tos);  
  36.   printf("Total length:%d\n",ntohs(ip_protocol->ip_length));  
  37.   printf("Identification:%d\n",ntohs(ip_protocol->ip_id));  
  38.   printf("Offset:%d\n",(offset &0x1fff) *8);  
  39.   printf("TTL:%d\n",ip_protocol->ip_ttl);  
  40.   printf("Protocol:%d\n",ip_protocol->ip_protocol);  
  41.   switch (ip_protocol->ip_protocol)  
  42.   {  
  43.     case 6:  
  44.       printf("The Transport Layer Protocol is TCP\n");  
  45.       break;  
  46.     case 17:  
  47.       printf("The Transport Layer Protocol is UDP\n");  
  48.       break;  
  49.     case 1:  
  50.       printf("The Transport Layer Protocol is ICMP\n");  
  51.       break;  
  52.     default:  
  53.       break;  
  54.   }  
  55.   printf("Header checksum:%d\n",checksum);  
  56.   printf("Source address:%s\n",inet_ntoa(ip_protocol->ip_source_address));  
  57.   printf("Destination address:%s\n",inet_ntoa(ip_protocol->ip_destination_address));  
  58.   switch(ip_protocol->ip_protocol){  
  59.     case 6:  
  60.       tcp_protocol_packet_callback(argument,packet_header,packet_content);  
  61.       break;  
  62.     default:  
  63.       break;  
  64.   }  

ether.c

 

  1. #include <sys/types.h>  
  2. #include <arpa/inet.h>  
  3. #include <pcap.h>    
  4. #include <time.h>    
  5. #include <stdlib.h>    
  6. #include <stdio.h>    
  7.  
  8. struct ether_header{  
  9.   u_int8_t ether_dhost[6];  
  10.   u_int8_t ether_shost[6];  
  11.   u_int16_t ether_type;  
  12. };  
  13.  
  14. void getPacket(u_char * arg, const struct pcap_pkthdr * pkthdr, const u_char * packet)    
  15. {   
  16.   struct ip_header *ip_protocol;   
  17.   struct ether_header *ethernet_protocol;  
  18.   int * id = (int *)arg;    
  19.   u_char *mac_string;  
  20.     
  21.   ethernet_protocol=(struct ether_header*)packet;  
  22.   u_short ethernet_type = ntohs(ethernet_protocol->ether_type);  
  23.  
  24.   switch (ethernet_type){  
  25.     case 0x0800:  
  26.       printf("The network layer is IP protocol\n");  
  27.       break;  
  28.     case 0x0806:  
  29.       printf("The network layer is ARP protocol\n");  
  30.       break;  
  31.     case 0x8035:  
  32.       printf("The network layer is RAPR protocol\n");  
  33.       break;  
  34.     default:  
  35.       printf("The network laye protocol unknow\n");  
  36.       break;  
  37.   }  
  38.     
  39.   printf("Mac Source Address is :\n");  
  40.   mac_string = ethernet_protocol->ether_shost;  
  41.   printf("%02x:%02x:%02x:%02x:%02x:%02x\n",*mac_string,*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));  
  42.   printf("id: %d\n", ++(*id));  
  43.   printf("Packet length: %d\n", pkthdr->len);    
  44.   printf("Number of bytes: %d\n", pkthdr->caplen);    
  45.   printf("Recieved time: %s", ctime((const time_t *)&pkthdr->ts.tv_sec));     
  46.   switch(ethernet_type){  
  47.     case 0x0800:  
  48.       ip_protocol_packet_callback(arg,pkthdr,packet);  
  49.       break;  
  50.     default:  
  51.       break;  
  52.   }  
  53.   printf("\n\n");    
  54. }   

tcp.c

 

  1. #include <sys/types.h>  
  2. #include <arpa/inet.h>  
  3. #include <pcap.h>    
  4. #include <time.h>    
  5. #include <stdlib.h>    
  6. #include <stdio.h>    
  7.  
  8. struct tcp_header{  
  9.   u_int16_t sport;  
  10.   u_int16_t dport;  
  11.   u_int32_t sn;  
  12.   u_int32_t ack;  
  13. //  u_int16_t other;  
  14. #if BYTE_ORDER == LITTLE_ENDIAN  
  15.   u_char th_x2:4,  
  16.     th_off:4;  
  17. #endif  
  18. #if BYTE_ORDER == BIG_ENDIAN  
  19.   u_char th_off:4,  
  20.     th_x2:4;  
  21. #endif  
  22.   u_char th_flags;  
  23. #define TH_FIN 0x01  
  24. #define TH_SYN 0x02  
  25. #define TH_RST 0x04  
  26. #define TH_PUSH 0x08  
  27. #define TH_ACK 0x10  
  28. #define TH_URG 0x20  
  29.  
  30.   u_int16_t win_size;  
  31.   u_int16_t checksum;  
  32.   u_int16_t urg_ptr;  
  33. };  
  34.  
  35. void tcp_protocol_packet_callback(u_char *argument, const struct pcap_pkthdr *packet_header, const u_char *packet_content)  
  36. {  
  37.   struct tcp_header *tcp_protocol;  
  38.   tcp_protocol=(struct tcp_header*)(packet_content + 14 + 20);  
  39.   u_short source_port=ntohs(tcp_protocol->sport);  
  40.   u_short destination_port=ntohs(tcp_protocol->dport);  
  41.   u_int sequence = ntohl(tcp_protocol->sn);  
  42.   u_int acknowledgement = ntohl(tcp_protocol->ack);  
  43.   u_char flags = tcp_protocol->th_flags;  
  44.   u_int16_t win_size = ntohl(tcp_protocol->win_size);  
  45.   u_int16_t checksum = ntohl(tcp_protocol->checksum);  
  46.   printf("Tcp Source Port:%d\n",source_port);  
  47.   printf("Tcp Destination Port:%d\n",destination_port);  
  48.   printf("Sequence Number:%u\n",sequence);  
  49.   printf("Acknowledgement Number:%u\n",acknowledgement);  
  50.   printf("Flags:");  
  51.   if(flags & 0x08)   printf("PSH ");  
  52.   if(flags & 0x10)   printf("ACK ");  
  53.   if(flags & 0x02)   printf("SYN ");  
  54.   if(flags & 0x20)   printf("URG ");  
  55.   if(flags & 0x01)   printf("FIN ");  
  56.   if(flags & 0x04)   printf("RST ");  
  57.   printf("\n");  
  58.  
  59.   int i;  
  60.   for(i=0; i<packet_header->len; ++i)  
  61.   {  
  62.     printf(" %c", packet_content[i]);  
  63.     if( (i + 1) % 16 == 0 )  
  64.     {  
  65.       printf("\n");  
  66.     }  
  67.   }  
  68.  

lib.h

 

  1. struct ether_header;  
  2. struct ip_header;  
  3. struct tcp_header;  
  4.  
  5. void tcp_protocol_packet_callback(u_char *, const struct pcap_pkthdr *, const u_char *);  
  6. void ip_protocol_packet_callback(u_char *, const struct pcap_pkthdr *, const u_char *);  
  7. void getPacket(u_char *, const struct pcap_pkthdr *, const u_char *);