⑴ 怎麼用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;
}
⑵ 乙太網數據幀的監聽與分析
樓上說的對,這不是一句兩句話能說玩的,問題也不是一分鍾兩分鍾能解決的了的,編寫代碼不是一天兩天能搞定的。。。樓主,我勸你還是不要對這里報希望了