linux使用进程抓包,一个适用于windows和linux的抓包程序

};

};

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

};

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

};

};

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

{

vectorcontainer_;

public:

~manager()

{

}

templatevoid 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

}

#ifdef WIN

err = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );

if ( err != 0 ) {

cout

}

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

}

if(bind(sniffer,(SOCKADDR *)&saddr,sizeof(saddr))==SOCKET_ERROR)

{

cout

}

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

}

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));

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值