⑴ 怎么用C/C++编写发送tcp/ip协议中的syn包
要先装pcap,用Winpcap构造 SYN的数据包
#include<iostream>
usingnamespacestd;
#include<pcap/pcap.h>
#pragmacomment(lib,"wpcap.lib")//要先装pcap,用Winpcap构造SYN的数据包
#include<winsock2.h>
#pragmacomment(lib,"ws2_32.lib")
#include"Packet_Header.h"
#defineMSG_MAXLEN58
USHORTchecksum(USHORT*buf,intsize)
{
ULONGcksum=0;
while(size>1)
{
cksum+=*buf++;
size-=sizeof(USHORT);
}
if(size)
{
cksum+=*(USHORT*)buf;
}
cksum=(cksum>>16)+(cksum&0xffff);
cksum+=(cksum>>16);
return(USHORT)(~cksum);
}
intmain()
{
pcap_if_t*alldevs;
charerrbuf[PCAP_ERRBUF_SIZE]={};
if(-1==pcap_findalldevs(&alldevs,errbuf))
{
cout<<"Getdevlisterror"<<endl;
return-1;
}
for(pcap_if_t*d=alldevs;NULL!=d;d=d->next)
{
cout<<d->description<<endl;
}
pcap_t*pt=pcap_open_live(alldevs->name,65536,true,20,errbuf);
if(NULL==pt)
{
cout<<"Opendeverror"<<endl;
return-1;
}
UCHARSendMsg[MSG_MAXLEN]={0};
PETHERHDRpethhdr=(PETHERHDR)SendMsg;
pethhdr->ethDst[0]=0x00;//mac
pethhdr->ethDst[1]=0x21;
pethhdr->ethDst[2]=0x85;
pethhdr->ethDst[3]=0x02;
pethhdr->ethDst[4]=0x30;
pethhdr->ethDst[5]=0x4e;
pethhdr->ethSrc[0]=0x00;
pethhdr->ethSrc[1]=0x23;
pethhdr->ethSrc[2]=0x8b;
pethhdr->ethSrc[3]=0xb6;
pethhdr->ethSrc[4]=0xbd;
pethhdr->ethSrc[5]=0xd7;
pethhdr->ethtype=htons(0x0800);
/*IPheader*/
PIPHDRpiphdr=(PIPHDR)(SendMsg+sizeof(ETHERHDR));
piphdr->ipv_l=(4<<4|(sizeof(IPHDR)/sizeof(ULONG)));
piphdr->iptos=0;
piphdr->iplen=htons(sizeof(SendMsg)-sizeof(ETHERHDR));
//piphdr->ipID=htons(0x1234);//0xede6
piphdr->ipTTL=58;
piphdr->ipCheckSum=0;
piphdr->ipProtocol=IPPROTO_TCP;
piphdr->ipSrc=inet_addr("192.168.0.183");
piphdr->ipDst=inet_addr("119.75.217.109");//119.75.217.109
//发送给网络(ip119.75.217.109:80)
piphdr->ipCheckSum=checksum((USHORT*)piphdr,sizeof(IPHDR));
/*TCPheader*/
PTCPHDRptcphdr=(PTCPHDR)(SendMsg+sizeof(ETHERHDR)+sizeof(IPHDR));
ptcphdr->tcpSrcPort=htons(44523);
ptcphdr->tcpDstPort=htons(80);
ptcphdr->tcpSeq=htonl(0x7d2cb526);//0x7d2cb526
ptcphdr->tcpAck=htonl(0);
ptcphdr->tcpl_r=((sizeof(TCPHDR)/sizeof(ULONG))<<4|0);
ptcphdr->tcpFlags=2;
ptcphdr->tcpWnd=htons(2048);
ptcphdr->tcpChkSum=0;
ptcphdr->tcpUrp=0;
UCHAR*tmpbuf=newUCHAR[sizeof(SendMsg)-sizeof(ETHERHDR)-sizeof(IPHDR)+sizeof(PSDHDR)];
memset(tmpbuf,0,sizeof(TCPHDR)+sizeof(PSDHDR));
PPSDHDRppsdhdr=(PPSDHDR)tmpbuf;
ppsdhdr->psdSrcAddr=piphdr->ipSrc;
ppsdhdr->psdDstAddr=piphdr->ipDst;
ppsdhdr->psdzero=0;
ppsdhdr->psdProto=IPPROTO_TCP;
ppsdhdr->psdPackLen=htons(sizeof(SendMsg)-sizeof(ETHERHDR)-sizeof(IPHDR));
memcpy(tmpbuf+sizeof(PSDHDR),SendMsg+sizeof(ETHERHDR)+sizeof(IPHDR),sizeof(tmpbuf+sizeof(PSDHDR)));
ptcphdr->tcpChkSum=checksum((USHORT*)tmpbuf,sizeof(SendMsg)-sizeof(ETHERHDR)-sizeof(IPHDR));
if(0!=pcap_sendpacket(pt,SendMsg,sizeof(SendMsg)))
{
cout<<"Error"<<endl;
}
system("pause");
return0;
}
⑵ 以太网数据帧的监听与分析
楼上说的对,这不是一句两句话能说玩的,问题也不是一分钟两分钟能解决的了的,编写代码不是一天两天能搞定的。。。楼主,我劝你还是不要对这里报希望了