-
Notifications
You must be signed in to change notification settings - Fork 8
/
synflood-server.c
146 lines (124 loc) · 2.87 KB
/
synflood-server.c
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
#define _POSIX_C_SOURCE 200112L
/**
* TCP SYN Flood Listener
*
* Small program which listens on port 9001 with a small listen backlog to
* help encourage SYN floods. Produce a SYN flood with hping3 or just a big
* heap of netcat in the background.
*
* gcc -Wall -Wextra -Wpedantic -std=c99 -no-pie synflood-server.c -o synflood-server */
#include <errno.h>
#include <getopt.h>
#include <netdb.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
void print_usage_and_exit(char *execname)
{
printf("Usage: %s -4|-6 HOST\n", execname);
printf("SYN flood listener.\n");
printf(" -4 Use IPv4\n");
printf(" -6 Use IPv6\n");
printf(" HOST Listen hostname\n");
printf(" 0.0.0.0 - IPv4 any\n");
printf(" 127.0.0.1 - IPv4 addr\n");
printf(" :: - IPv6 any\n");
printf(" ::1 - IPv6 addr\n");
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
if (argc < 2)
print_usage_and_exit(argv[0]);
bool ipv4_flag = false;
bool ipv6_flag = false;
int c = 0;
while ((c = getopt (argc, argv, "46")) != -1)
switch(c) {
case '4':
ipv4_flag = true;
break;
case '6':
ipv6_flag = true;
break;
case '?':
default:
print_usage_and_exit(argv[0]);
break;
}
if (
((!ipv4_flag) && (!ipv6_flag)) ||
((ipv4_flag) && (ipv6_flag))
) {
printf("ERROR: Specify either -4 or -6\n");
print_usage_and_exit(argv[0]);
}
if (!argv[optind]) {
printf("ERROR: No hostname provided\n");
print_usage_and_exit(argv[0]);
}
int sockfd = 0;
int connfd = 0;
int res = 0;
int my_domain = 0;
if (ipv4_flag)
my_domain = AF_INET;
else
my_domain = AF_INET6;
sockfd = socket(my_domain, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
struct addrinfo hints = { 0 };
struct addrinfo *result = NULL;
hints.ai_family = my_domain;
hints.ai_socktype = SOCK_STREAM;
res = getaddrinfo(argv[optind], "9001", &hints, &result);
if (res != 0) {
printf("ERROR: getaddrinfo: %s\n", gai_strerror(res));
exit(EXIT_FAILURE);
}
res = bind(sockfd, result->ai_addr, result->ai_addrlen);
if (res == -1) {
perror("bind");
exit(EXIT_FAILURE);
}
const int enable = 1;
res = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable));
if (res == -1) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
res = listen(sockfd, 1);
if (res == -1) {
perror("listen");
exit(EXIT_FAILURE);
} else {
printf("listening...\n");
}
char buffer[80];
while (true) {
connfd = accept(sockfd, NULL, NULL);
if (connfd == -1) {
perror("accept");
exit(EXIT_FAILURE);
} else {
printf(".");
fflush(stdout);
}
(void) recv(connfd, buffer, sizeof(buffer), 0);
res = close(connfd);
if (res == -1) {
perror("close");
exit(EXIT_FAILURE);
}
}
close(sockfd);
return 0;
}