问题描述
假设已知远程主机的IP地址和端正口号,如何获得它往本机发送的数据包?需要用的那些技术,谢谢
解决方案
解决方案二:
在csdn上原来的帖子里搜索到的winpcap做的#pragmaonce#include<list>#include"pcap.h"#pragmapack(push,1)structip_address{u_charbyte1;u_charbyte2;u_charbyte3;u_charbyte4;};/*IPv4header*/structip_header{u_charver_ihl;//Version(4bits)+Internetheaderlength(4bits)u_chartos;//Typeofserviceu_shorttlen;//Totallengthu_shortidentification;//Identificationu_shortflags_fo;//Flags(3bits)+Fragmentoffset(13bits)u_charttl;//Timetoliveu_charproto;//Protocolu_shortcrc;//Headerchecksumip_addresssaddr;//Sourceaddressip_addressdaddr;//Destinationaddressu_intop_pad;//Option+Padding};structtcp_header{//TransportControlProtocolheaderu_shortth_sport;//sourceportu_shortth_dport;//destinationportintth_seq;//sequencenumber-32bitsintth_ack;//acknumber-32bits//unusedanddataoffset!u_charth_x2:4,th_off:4;//Controlbits,6bits[fromlefttoright]u_charth_flags;#defineTH_FIN0x01#defineTH_SYN0x02#defineTH_RST0x04#defineTH_PUSH0x08#defineTH_ACK0x10#defineTH_URG0x20#defineTH_ECE0x40#defineTH_CWR0x80#defineTH_FLAGS(TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)u_shortth_win;//windowu_shortth_sum;//checksumu_shortth_urp;//urgentpointer};/*UDPheader*/structudp_header{u_shortsport;//Sourceportu_shortdport;//Destinationportu_shortlen;//Datagramlengthu_shortcrc;//Checksum};#pragmapack(pop)classPacketInfo{public:ip_addresssrcaddr;ip_addressdstaddr;unsignedshortsrcport;unsignedshortdstport;std::stringdatas;std::stringtimestr;intlen;};classSnifferThread{public:SnifferThread(){}~SnifferThread(){}voidbegin(){DWORDthreadid=0;_handle=(HANDLE)CreateThread(NULL,0,ThreadFunc,this,0,&threadid);}boolget_one_packet(PacketInfo&clone){_lock.Lock();if(_packets.empty()){_lock.Unlock();returnfalse;}clone=(*_packets.begin());_packets.pop_front();_lock.Unlock();returntrue;}private:voidstart(){is_stop=false;}voidstop(){is_stop=true;_lock.Lock();_packets.clear();_lock.Unlock();}voidterm(){TerminateThread(_handle,1);}voidrun(){pcap_if_t*alldevs;charerrbuf[PCAP_ERRBUF_SIZE];if(pcap_findalldevs(&alldevs,errbuf)==-1){MessageBox(NULL,"打开设备出错","错误",MB_OK);return;}for(pcap_if_t*d=alldevs;d;d=d->next){pcap_t*adhandle=pcap_open_live(d->name,4096,1,1000,errbuf);if(adhandle!=NULL){_cap_handles.push_back(adhandle);structbpf_programfcode;bpf_u_int32NetMask=0xffffff;if(d->addresses!=NULL)/*Retrievethemaskofthefirstaddressoftheinterface*/NetMask=((structsockaddr_in*)(d->addresses->netmask))->sin_addr.S_un.S_addr;char*filter="tcpport25ortcpport110";pcap_compile(adhandle,&fcode,filter,1,NetMask);pcap_setfilter(adhandle,&fcode);}}if(_cap_handles.empty()){MessageBox(NULL,"无设备可用","错误",MB_OK);return;}while(true){if(is_stop){SleepEx(1000,TRUE);continue;}for(std::list<pcap_t*>::iteratorit=_cap_handles.begin();it!=_cap_handles.end();it++){structpcap_pkthdr*header;constu_char*pkt_data;pcap_t*curhandle=*it;intres=pcap_next_ex(curhandle,&header,&pkt_data);//=0是超时if(res>0){structtm*ltime=localtime(&header->ts.tv_sec);;chartimestr[16];strftime(timestr,sizeoftimestr,"%H:%M:%S",ltime);ip_header*ih=(ip_header*)(pkt_data+14);intip_len=(ih->ver_ihl&0xf)*4;tcp_header*th=(tcp_header*)((u_char*)ih+ip_len);PacketInfopacketinfo;packetinfo.timestr=timestr;packetinfo.srcaddr=ih->saddr;packetinfo.dstaddr=ih->daddr;packetinfo.srcport=th->th_sport;packetinfo.dstport=th->th_dport;packetinfo.len=header->len-14-ip_len-th->th_off*4;intdatasize=header->caplen-14-ip_len-th->th_off*4;if(datasize>0){char*data=(char*)th+th->th_off*4;packetinfo.datas=std::string(data,datasize);}_packets.push_back(packetinfo);}}}}staticDWORDWINAPIThreadFunc(void*arg){SnifferThread*pthis=(SnifferThread*)arg;pthis->run();return0;}CComAutoCriticalSection_lock;boolis_stop;HANDLE_handle;std::list<pcap_t*>_cap_handles;std::list<PacketInfo>_packets;};
解决方案三:
这个是C语言的??