Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18669
  • 博文数量: 4
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 67
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-24 10:32
个人简介

喜欢自己做的事情,不喜欢强迫!

文章分类

全部博文(4)

文章存档

2013年(4)

我的朋友

分类: LINUX

2013-06-24 10:59:58

程序的精髓不在于抓包,而在于对数据包的处理,可以很方便的扩展。

程序如下:


点击(此处)折叠或打开

  1. /******************************************************************
  2. windows:g++ -o process process.cpp -lws2_32 -DWIN
  3. linux :g++ -o process process.cpp
  4. all right reserve
  5. *******************************************************************/
  6. #include <iostream>
  7. #include <vector>
  8. #ifdef WIN
  9. #include <windows.h>
  10. #include <winsock2.h>
  11. #else
  12. #include <netinet/in.h>
  13. #include <sys/socket.h>
  14. #include <sys/ioctl.h>
  15. #include <linux/if_ether.h>
  16. #include <net/if.h>
  17. #endif

  18. #include "head_ip.h"
  19. #ifdef WIN
  20. #define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
  21. #endif
  22.  
  23. using namespace std;
  24. #ifdef WIN
  25. int ioct(SOCKET sniffer);
  26. #else
  27. int do_promisc(char *nif, int sock ) ;
  28. void die(char *why, int n);
  29. #endif
  30. int print_ip(const char * ip_hdr);
  31. struct iphdr * char_to_ip(const char * pkg);
  32. struct tcphdr * char_to_tcp(const char * pkg);
  33. struct udphdr * char_to_udp(const char * pkg);
  34. class processor
  35. {
  36. public:
  37.      virtual void print(const char * pkg) const =0;
  38. };

  39. class protocol
  40. {
  41. public:
  42.      virtual bool judge(const char* pkg) const =0;
  43.      virtual processor* create_processor() const =0;
  44. };

  45. char processor_buffer[sizeof(processor)];
  46. /********************************tcp***************************************************/
  47. class processor_tcp:public processor
  48. {
  49. public:
  50.      virtual void print(const char * pkg) const
  51.      {
  52.       cout<<"-----------------------------------------------------------------"<<endl;
  53.       cout<<"this is a tcp packet"<<endl;
  54.      print_ip(pkg);
  55.       
  56.       
  57.      cout<<"source port:"<<ntohs(char_to_tcp(pkg+20)->source)<<endl;
  58.      cout<<"dest port:"<<ntohs(char_to_tcp(pkg+20)->dest)<<endl;
  59.      cout<<"seq:"<<char_to_tcp(pkg+20)->seq<<endl;
  60.      cout<<"ack seq:"<<char_to_tcp(pkg+20)->ack_seq<<endl;
  61.      cout<<"ack:"<<char_to_tcp(pkg+20)->ack<<endl;
  62.      cout<<"syn:"<<char_to_tcp(pkg+20)->syn<<endl;
  63.      cout<<"fin:"<<char_to_tcp(pkg+20)->fin<<endl;

  64.       
  65.       };
  66. };

  67. class protocol_tcp :public protocol
  68. {
  69. public:
  70.     virtual bool judge(const char * pkg) const
  71.     {
  72.            if( char_to_ip(pkg)->protocol==6)
  73.            return true;
  74.            else
  75.            return false;
  76.     }
  77.     
  78.     virtual processor* create_processor() const
  79.     {
  80.          return new(processor_buffer) processor_tcp;
  81.          
  82.     }
  83. };
  84.  
  85. /*********************************udp*************************************************/
  86. class processor_udp:public processor
  87. {
  88. public:
  89.      virtual void print(const char * pkg) const
  90.      {
  91.       cout<<"-----------------------------------------------------------------"<<endl;
  92.       cout<<"this is a udp packet"<<endl;
  93.      print_ip(pkg);
  94.       
  95.      cout<<"source port:"<<ntohs(char_to_udp(pkg+20)->source)<<endl;
  96.      cout<<"dest port:"<<ntohs(char_to_udp(pkg+20)->dest)<<endl;
  97.      cout<<"length:"<<char_to_udp(pkg+20)->len<<endl;
  98.       
  99.       };
  100. };

  101. class protocol_udp :public protocol
  102. {
  103. public:
  104.     virtual bool judge(const char * pkg) const
  105.     {
  106.         if( char_to_ip(pkg)->protocol==17)
  107.         return true;
  108.            else
  109.            return false;
  110.     }
  111.     
  112.     virtual processor* create_processor() const
  113.     {
  114.          return new(processor_buffer) processor_udp;
  115.     }
  116. };

  117. /*********************************udp*************************************************/
  118. class processor_icmp:public processor
  119. {
  120. public:
  121.      virtual void print(const char * pkg) const
  122.      {
  123.       cout<<"-----------------------------------------------------------------"<<endl;
  124.       cout<<"this is a icmp packet"<<endl;
  125.      print_ip(pkg);
  126.      
  127.       };
  128. };

  129. class protocol_icmp :public protocol
  130. {
  131. public:
  132.     virtual bool judge(const char * pkg) const
  133.     {
  134.         if( char_to_ip(pkg)->protocol==1)
  135.         return true;
  136.            else
  137.            return false;
  138.     }
  139.     
  140.     virtual processor* create_processor() const
  141.     {
  142.          return new(processor_buffer) processor_icmp;
  143.     }
  144. };
  145.  
  146.  
  147.  
  148.  
  149.  
  150. class manager
  151. {
  152.     vector<protocol*> container_;
  153. public:
  154.     ~manager()
  155.    {
  156.       
  157.    }
  158.     template<typename _Proctocol>
  159.     void install_protocol()
  160.     {
  161.          container_.push_back( new _Proctocol());
  162.     }
  163.    
  164.    int create_process(const char * pkg) const
  165.     {
  166.               for(vector<protocol*>::const_iterator it = container_.begin(); it != container_.end(); it++)
  167.                {
  168.                  if((*it)->judge(pkg) == true)
  169.                    {
  170.                   
  171.                 processor * xx=(*it)->create_processor();
  172.                 return 1;
  173.                   }
  174.              
  175.        }
  176.         return 0;
  177.     }
  178. };
  179.  
  180.  
  181. int main(int argc,char *argv[])
  182. {
  183.     manager mgr;
  184. #ifdef WIN
  185.     WSADATA wsaData;
  186.     SOCKADDR_IN saddr;
  187.     SOCKET sniffer;
  188.     int len;
  189.     struct sockaddr_in addr;
  190. #else
  191. int sniffer;
  192. #endif
  193.     int err;
  194.     char *buf1=(char *)malloc(1518);
  195.     char *buf=buf1+14;
  196.     int num;
  197.     
  198.  if(argc!=2)
  199.  {
  200.   cout<<"Input error,such as:\n"<<argv[0]<<" 192.168.0.1"<<endl;;
  201.   return -1;
  202.  }
  203. #ifdef WIN
  204.     err = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
  205.     if ( err != 0 ) {
  206.         cout<<"error!WSAStartup failed!"<<endl;;
  207.         return -1;
  208.     }
  209.     saddr.sin_family = AF_INET;
  210.     saddr.sin_addr.s_addr = inet_addr(argv[1]);
  211.     saddr.sin_port = htons(555);
  212.     
  213.     if((sniffer=socket(AF_INET,SOCK_RAW,IPPROTO_IP))==SOCKET_ERROR)
  214.     {
  215.         cout<<"socket failed!"<<endl;
  216.         return -1;
  217.     }
  218.     if(bind(sniffer,(SOCKADDR *)&saddr,sizeof(saddr))==SOCKET_ERROR)
  219.     {
  220.         cout<<"bind failed!"<<endl;
  221.         return -1;
  222.     }
  223.     ioct(sniffer);
  224.     len = sizeof(addr);
  225. #else
  226. if((sniffer=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL)))==-1)

  227. {
  228.         die("socket", 1);
  229. }
  230. do_promisc("eth0", sniffer);
  231. #endif
  232. mgr.install_protocol<protocol_tcp>();
  233. mgr.install_protocol<protocol_udp>();
  234. mgr.install_protocol<protocol_icmp>();
  235. while(1)
  236.    {
  237. #ifndef WIN
  238. num = recv(sniffer,buf1,1518,0);
  239. #else
  240. num = recvfrom(sniffer,buf,1500, 0, (struct sockaddr *)&addr,&len);
  241. #endif
  242.         if(num>0)
  243.         {
  244.         if(mgr.create_process(buf)==1)
  245.         {
  246.        
  247.     ( reinterpret_cast<processor*>(processor_buffer))->print(buf);
  248.      }
  249.         }
  250.     
  251.    }
  252. #ifdef WIN
  253.     closesocket(sniffer);
  254.     WSACleanup();
  255. #endif
  256.     return 0;
  257. }
  258. #ifdef WIN
  259. int ioct(SOCKET sniffer)
  260. {
  261. DWORD dwBufferLen[10] ;
  262. DWORD dwBufferInLen = 1 ;
  263. DWORD dwBytesReturned = 0 ;
  264. WSAIoctl(sniffer, SIO_RCVALL,&dwBufferInLen, sizeof(dwBufferInLen),&dwBufferLen, sizeof(dwBufferLen),&dwBytesReturned , NULL , NULL );
  265. }
  266. #else
  267. int do_promisc(char *nif, int sock )
  268. {
  269. struct ifreq ifr;
  270.                 
  271. strncpy(ifr.ifr_name, nif,strlen(nif)+1);
  272.    if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) //..flag
  273.    {
  274.      die("ioctl", 2);
  275.    }
  276.    
  277.    ifr.ifr_flags |= IFF_PROMISC; //..flag..
  278.   
  279.    if(ioctl(sock, SIOCSIFFLAGS, &ifr) == -1 ) //....
  280.    {
  281.      die("ioctl", 3);
  282.    }
  283. }
  284. void die(char *why, int n)
  285. {
  286.   perror(why);
  287.   exit(n);
  288. }
  289. #endif

  290. int print_ip(const char * ip_hdr)
  291. {
  292. cout<<"version:"<<char_to_ip(ip_hdr)->version<<endl;
  293. cout<<"TTL:"<<char_to_ip(ip_hdr)->ttl<<endl;
  294. cout<<"tot_len:"<<char_to_ip(ip_hdr)->tot_len<<endl;
  295. cout<<"ID:"<<char_to_ip(ip_hdr)->id<<endl;
  296. cout<<"source ip:"<<char_to_ip(ip_hdr)->saddr_u.ip_str_saddr.ip1<<"."<<char_to_ip(ip_hdr)->saddr_u.ip_str_saddr.ip2<<"."<<char_to_ip(ip_hdr)->saddr_u.ip_str_saddr.ip3<<"."<<char_to_ip(ip_hdr)->saddr_u.ip_str_saddr.ip4<<endl;
  297. cout<<"dest ip:"<<char_to_ip(ip_hdr)->daddr_u.ip_str_daddr.ip1<<"."<<char_to_ip(ip_hdr)->daddr_u.ip_str_daddr.ip2<<"."<<char_to_ip(ip_hdr)->daddr_u.ip_str_daddr.ip3<<"."<<char_to_ip(ip_hdr)->daddr_u.ip_str_daddr.ip4<<endl;
  298. return 0;
  299. }
  300. inline struct iphdr * char_to_ip(const char * pkg)
  301. {
  302.  
  303. return reinterpret_cast<struct iphdr *>(const_cast<char *>(pkg));
  304. }
  305. inline struct udphdr * char_to_udp(const char * pkg)
  306. {
  307.  
  308. return reinterpret_cast<struct udphdr *>(const_cast<char *>(pkg));
  309. }
  310. inline struct tcphdr * char_to_tcp(const char * pkg)
  311. {
  312.  
  313. return reinterpret_cast<struct tcphdr *>(const_cast<char *>(pkg));
  314. }


程序里面有一个很奇怪的问题,如果linux下面如果用recvfrom()这种形式,会把mgr的结构破坏,被迫使用recv()函数了,那位大虾找到原因了可以给我

阅读(955) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~