导航:首页 > 编程语言 > 发送tcpsyn代码

发送tcpsyn代码

发布时间:2023-02-03 11:32:30

⑴ 怎么用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;
}

⑵ 以太网数据帧的监听与分析

楼上说的对,这不是一句两句话能说玩的,问题也不是一分钟两分钟能解决的了的,编写代码不是一天两天能搞定的。。。楼主,我劝你还是不要对这里报希望了

阅读全文

与发送tcpsyn代码相关的资料

热点内容
电脑网卡怎么连接网络连接不上网吗 浏览:838
刷子公司网站怎么做 浏览:272
86版本艾尔文测试 浏览:714
深宫曲文件夹是哪个 浏览:618
苹果u盘修复工具哪个好用 浏览:124
微信动态表情包搞笑 浏览:436
可以去哪里找编程老师问问题 浏览:608
win10lol全屏 浏览:25
qq图片动态动漫少女 浏览:122
sai绘图教程视频 浏览:519
如何分析加载减速法数据 浏览:672
手机怎么免费转换pdf文件格式 浏览:668
在哪个网站可以驾照年检 浏览:89
iphone可以播放ape吗 浏览:991
matlabp文件能破解吗 浏览:817
四川省高三大数据考试是什么 浏览:457
导出打开java文件 浏览:671
win10蓝屏是硬盘坏了么 浏览:46
沈阳哪里适合学编程 浏览:811
django19常用版本 浏览:521

友情链接