Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15315559
  • 博文数量: 112
  • 博客积分: 11195
  • 博客等级: 上将
  • 技术积分: 1989
  • 用 户 组: 普通用户
  • 注册时间: 2005-06-20 11:04
文章分类

全部博文(112)

文章存档

2013年(2)

2012年(27)

2011年(6)

2010年(11)

2009年(6)

2007年(7)

2006年(23)

2005年(30)

分类: LINUX

2006-07-31 17:28:13

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

/******************************************************************
windows:g++ -o process process.cpp -lws2_32 -DWIN
linux  :g++  -o process process.cpp
all right reserve
*******************************************************************/
#include
#include
#ifdef WIN
#include
#include
#else
#include
#include
#include
#include
#include
#endif


#include "head_ip.h"
#ifdef WIN
#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
#endif
 
using namespace std;
#ifdef WIN
int ioct(SOCKET sniffer);
#else
int do_promisc(char *nif, int sock ) ;
void die(char *why, int n);
#endif

int print_ip(const char * ip_hdr);
struct iphdr * char_to_ip(const char * pkg);
struct tcphdr * char_to_tcp(const char * pkg);
struct udphdr * char_to_udp(const char * pkg);

class processor
{
public:
     virtual void  print(const char * pkg) const =0;

};


class protocol
{
public:
     virtual bool  judge(const char* pkg) const =0;
     virtual processor* create_processor() const =0;
};


char  processor_buffer[sizeof(processor)];

/********************************tcp***************************************************/
class processor_tcp:public processor
{
public:
     virtual void print(const char * pkg) const
     {
      cout<<"-----------------------------------------------------------------"<      cout<<"this is a tcp packet"<     print_ip(pkg);
     
     
     cout<<"source port:"<source)<     cout<<"dest port:"<dest)<     cout<<"seq:"<seq<     cout<<"ack seq:"<ack_seq<     cout<<"ack:"<ack<     cout<<"syn:"<syn<     cout<<"fin:"<fin<


      
      };

};


class protocol_tcp :public protocol
{
public:    
    virtual bool judge(const  char * pkg) const
    {     

           if( char_to_ip(pkg)->protocol==6)
           return true;
           else
           return false;
    }
   
    virtual processor* create_processor() const
    {
         return new(processor_buffer) processor_tcp;
        
    }
};

 

/*********************************udp*************************************************/
class processor_udp:public processor
{
public:
     virtual void print(const char * pkg) const
     {
      cout<<"-----------------------------------------------------------------"<      cout<<"this is a udp packet"<     print_ip(pkg);
     
     cout<<"source port:"<source)<     cout<<"dest port:"<dest)<     cout<<"length:"<len<      
      };

};


class protocol_udp :public protocol
{
public:    
    virtual bool judge(const  char * pkg) const
    {     
        if( char_to_ip(pkg)->protocol==17)
        return true;
           else
           return false;
    }
   
    virtual processor* create_processor() const
    {
         return new(processor_buffer) processor_udp;
    }
};


/*********************************udp*************************************************/
class processor_icmp:public processor
{
public:
     virtual void print(const char * pkg) const
     {
      cout<<"-----------------------------------------------------------------"<      cout<<"this is a icmp packet"<     print_ip(pkg);
     
      };

};


class protocol_icmp :public protocol
{
public:    
    virtual bool judge(const  char * pkg) const
    {     
        if( char_to_ip(pkg)->protocol==1)
        return true;
           else
           return false;
    }
   
    virtual processor* create_processor() const
    {
         return new(processor_buffer) processor_icmp;
    }
};

 

 

 

 

 

class manager
{
    vector  container_;

public:
    ~manager()
   {
     
   }
    template
    void install_protocol()
    {
         container_.push_back( new _Proctocol());
    }
  
   int create_process(const char * pkg) const
    {
              for(vector::const_iterator it = container_.begin(); it != container_.end(); it++)
               {         
                 if((*it)->judge(pkg) == true)
                   {
                 
                processor * xx=(*it)->create_processor();
                return 1;
                  }
            
       }
        return 0;
    }
};

 

 

int main(int argc,char *argv[])
{
    manager mgr;
#ifdef WIN
    WSADATA wsaData;
    SOCKADDR_IN saddr;
    SOCKET sniffer;
    int len;
    struct sockaddr_in addr;
#else

int sniffer;

#endif
    int err;
    char *buf1=(char *)malloc(1518);
    char *buf=buf1+14;
    int num;
   
 if(argc!=2)
 {
  cout<<"Input error,such as:\n"<  return -1;
 }

#ifdef WIN   
    err = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
    if ( err != 0 ) {
        cout<<"error!WSAStartup failed!"<        return -1;
    }

    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = inet_addr(argv[1]);    
    saddr.sin_port = htons(555);
   
    if((sniffer=socket(AF_INET,SOCK_RAW,IPPROTO_IP))==SOCKET_ERROR)
    {
        cout<<"socket failed!"<        return -1;
    }
    if(bind(sniffer,(SOCKADDR *)&saddr,sizeof(saddr))==SOCKET_ERROR)
    {
        cout<<"bind failed!"<        return -1;
    }
    ioct(sniffer);
    len = sizeof(addr); 
#else
if((sniffer=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL)))==-1)


{
        die("socket", 1); 
}
do_promisc("eth0", sniffer); 

#endif

mgr.install_protocol();
mgr.install_protocol();
mgr.install_protocol();
while(1)
   {
#ifndef WIN
num = recv(sniffer,buf1,1518,0);
#else

num = recvfrom(sniffer,buf,1500, 0, (struct sockaddr *)&addr,&len);

#endif
        if(num>0)
        {

        if(mgr.create_process(buf)==1)
        {
      
    ( reinterpret_cast(processor_buffer))->print(buf);
     }
        }
   
   }
#ifdef WIN
    closesocket(sniffer);
    WSACleanup();
#endif
    return 0;

}

#ifdef WIN
int ioct(SOCKET sniffer)
{
DWORD dwBufferLen[10] ;
DWORD dwBufferInLen = 1 ;
DWORD dwBytesReturned = 0 ;
WSAIoctl(sniffer, SIO_RCVALL,&dwBufferInLen, sizeof(dwBufferInLen),&dwBufferLen, sizeof(dwBufferLen),&dwBytesReturned , NULL , NULL );
}
#else
int do_promisc(char *nif, int sock ) 

struct ifreq ifr; 
               
strncpy(ifr.ifr_name, nif,strlen(nif)+1); 
   if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1))  //..flag
   {        
     die("ioctl", 2); 
   } 
  
   ifr.ifr_flags |= IFF_PROMISC;  //..flag..
 
   if(ioctl(sock, SIOCSIFFLAGS, &ifr) == -1 )  //....
   {
     die("ioctl", 3); 
   } 
}

void die(char *why, int n) 

  perror(why); 
  exit(n); 
}  
#endif


int print_ip(const char * ip_hdr)
{
cout<<"version:"<version<cout<<"TTL:"<ttl<cout<<"tot_len:"<tot_len<cout<<"ID:"<id<cout<<"source ip:"<saddr_u.ip_str_saddr.ip1<<"."<saddr_u.ip_str_saddr.ip2<<"."<saddr_u.ip_str_saddr.ip3<<"."<saddr_u.ip_str_saddr.ip4<cout<<"dest ip:"<daddr_u.ip_str_daddr.ip1<<"."<daddr_u.ip_str_daddr.ip2<<"."<daddr_u.ip_str_daddr.ip3<<"."<daddr_u.ip_str_daddr.ip4<

return 0;
}

inline struct iphdr * char_to_ip(const char * pkg)

{
 
return reinterpret_cast(const_cast(pkg));

}
inline struct udphdr * char_to_udp(const char * pkg)

{
 
return reinterpret_cast(const_cast(pkg));

}
inline struct tcphdr * char_to_tcp(const char * pkg)

{
 
return reinterpret_cast(const_cast(pkg));

}


 

程序里面有一个很奇怪的问题,如果linux下面如果用recvfrom()这种形式

会吧mgr的结构破坏,被迫使用recv()函数了,那位大虾找到原因了可以给我

发送E-mail:playmud@hotmail.com

 

 

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

网络安全服务2011-05-16 16:21:23

818````问好```嘿嘿```

daschina2011-03-31 12:57:38

马克!好文!

SpringfieldKing2011-03-16 09:49:59

怎么加不了关注呀,那就加好友八