/
endhost.c
484 lines (410 loc) · 13.2 KB
/
endhost.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include "endhost.h"
#include "log.h"
void parse_command_line(int argc, char **argv, struct command_line_args *ob)
{
int opt = 0, longIndex = 0;
memset(ob,'\0',sizeof(struct command_line_args));
opt = getopt_long_only(argc, argv, ":r:t:u:s:", longOpts, &longIndex);
if(opt == -1)
LOG(stderr, ERROR, "usage: endhost [-r filename] [-t port] [-u por] [-s stopthresh] \n");
while (opt != -1)
{
switch(opt)
{
case 'r':
if(*optarg == '-')
LOG(stderr, ERROR, "usage: endhost [-r filename] [-t port] [-u por] [-s stopthresh] \n");
ob->routerfile = optarg;
break;
case 't':
if(*optarg == '-')
LOG(stderr, ERROR, "usage: endhost [-r filename] [-t port] [-u por] [-s stopthresh] \n");
ob->tcpport = atoi(optarg);
break;
case 'u':
if(*optarg == '-')
LOG(stderr, ERROR, "usage: endhost [-r filename] [-t port] [-u por] [-s stopthresh] \n");
ob->udpport = atoi(optarg);
break;
case 's':
if(*optarg == '-')
LOG(stderr, ERROR, "usage: endhost [-r filename] [-t port] [-u por] [-s stopthresh] \n");
ob->stopthresh = atoi(optarg);
break;
case '?':
case ':':
default :
LOG(stderr, ERROR, "usage: endhost [-r filename] [-t port] [-u por] [-s stopthresh] \n");
}
opt = getopt_long_only(argc, argv, ":r:t:u:s:", longOpts, &longIndex);
}
if(ob->tcpport == 0 || ob->udpport == 0 || ob->stopthresh == 0)
LOG(stderr, ERROR, "\nError: Command line arguments for endhost tool not specified correctly\n");
}
void send_start_marking_msg(char *router_ip, int tcpport)
{
int fd;
struct sockaddr_in router_addr;
struct hostent *router_ip_addr;
struct in_addr * address;
struct timeval curtime;
socklen_t alen = sizeof(struct sockaddr_in);
char my_message[BUFSIZE];
memset(my_message, 0, BUFSIZE);
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
LOG(stderr, ERROR, "cannot create socket");
}
memset((char*)&router_addr, 0, sizeof(struct sockaddr_in));
router_addr.sin_family = AF_INET;
router_addr.sin_port = htons(tcpport);
router_ip_addr = gethostbyname(router_ip);
address = (struct in_addr *)router_ip_addr->h_addr;
router_addr.sin_addr.s_addr = inet_addr(inet_ntoa(*address));
if (connect(fd, (struct sockaddr *)&router_addr, alen) == -1) {
close(fd);
perror("TCP connection error");
exit(0);
}
snprintf(my_message,BUFSIZE,"startMarking %s",router_ip);
if(write(fd, my_message, BUFSIZE) < 0)
{
close(fd);
perror("Endhost: Error in write() for victim client");
exit(0);
}
LOG(stdout, LOGL, "EndHost: StartMarking message is sent to Router(IP: %s, TCP: %d)",router_ip, tcpport);
gettimeofday(&curtime, NULL);
LOG(fp_log, LOGL, "%f startMarking %s \'%s\'", SEC(TIME_IN_USEC(curtime)), router_ip, my_message);
close(fd);
}
void send_stop_marking_msg(char *router_ip, int tcpport)
{
int fd;
struct sockaddr_in router_addr;
struct hostent *router_ip_addr;
struct in_addr * address;
socklen_t alen = sizeof(struct sockaddr_in);
char my_message[BUFSIZE];
memset(my_message, 0, BUFSIZE);
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
LOG(stderr, ERROR, "cannot create socket");
}
memset((char*)&router_addr, 0, sizeof(struct sockaddr_in));
router_addr.sin_family = AF_INET;
router_addr.sin_port = htons(tcpport+1);
router_ip_addr = gethostbyname(router_ip);
address = (struct in_addr *)router_ip_addr->h_addr;
router_addr.sin_addr.s_addr = inet_addr(inet_ntoa(*address));
if (connect(fd, (struct sockaddr *)&router_addr, alen) == -1) {
close(fd);
perror("TCP connection error");
exit(0);
}
snprintf(my_message,BUFSIZE,"stopMarking");
if(write(fd, my_message, BUFSIZE) < 0)
{
close(fd);
perror("Endhost: Error in write() for victim client");
exit(0);
}
LOG(stdout, LOGL, "EndHost: StopMarking message is sent to Router(IP: %s, TCP: %d)",router_ip, tcpport);
close(fd);
}
struct attacker_list *create_attackerlist_node(char *router_ip_addr, char *attacker_ip_addr, char *victim_ip_addr)
{
struct attacker_list *newNode = (struct attacker_list *)malloc(sizeof(struct attacker_list));
memset(newNode->attacker_ip_addr,'\0',MAXIPADDRLEN);
memset(newNode->victim_ip_addr,'\0',MAXIPADDRLEN);
strncpy(newNode->attacker_ip_addr, attacker_ip_addr, strlen(attacker_ip_addr));
strncpy(newNode->victim_ip_addr, victim_ip_addr, strlen(victim_ip_addr));
newNode->routerhead = (struct router_list *)malloc(sizeof(struct router_list));
memset(newNode->routerhead,'\0',sizeof(struct router_list));
strncpy(newNode->routerhead->router_ip, router_ip_addr, strlen(router_ip_addr));
struct timeval curtime;
gettimeofday(&curtime, NULL);
LOG(fp_log, LOGL, "%f %s distance", SEC(TIME_IN_USEC(curtime)), router_ip_addr);
newNode->routerhead->frequency = 1;
newNode->routerhead->next = NULL;
newNode->no_of_routers = 1;
newNode->next = NULL;
return newNode;
}
void Create_Attacker_List(char *router_ip, char *attacker_ip, char * victim_ip)
{
struct attacker_list *p;
struct router_list *p_routerlist, *new_routerlist;
if(routerAttackerHead == NULL)
{
routerAttackerHead = create_attackerlist_node(router_ip, attacker_ip, victim_ip);
}
else
{
p = routerAttackerHead;
while(p != NULL)
{
if(strncmp(p->attacker_ip_addr, attacker_ip, MAXIPADDRLEN) == 0)
{
p_routerlist = p->routerhead;
while(p_routerlist != NULL)
{
if(strncmp(p_routerlist->router_ip, router_ip, MAXIPADDRLEN) == 0)
{
p_routerlist->frequency++;
goto end;
}
p_routerlist = p_routerlist->next;
}
if(p_routerlist == NULL)
{
new_routerlist = (struct router_list *)malloc(sizeof(struct router_list));
memset(new_routerlist, '\0', sizeof(struct router_list));
strncpy(new_routerlist->router_ip, router_ip, strlen(router_ip));
new_routerlist->frequency = 1;
new_routerlist->next = p->routerhead;
p->routerhead = new_routerlist;
p->no_of_routers++;
struct timeval curtime;
gettimeofday(&curtime, NULL);
LOG(fp_log, LOGL, "%f %s distance", SEC(TIME_IN_USEC(curtime)), router_ip);
goto end;
}
}
p = p->next;
}
end:
if(p == NULL)
{
p = create_attackerlist_node(router_ip, attacker_ip, victim_ip);
p->next = routerAttackerHead;
routerAttackerHead = p;
}
}
}
struct router_list * source_with_max_freq(struct router_list *routerhead)
{
struct router_list *max_freq_router = routerhead, *p = routerhead;
int max_freq = routerhead->frequency;
while(p != NULL)
{
if(p->frequency > max_freq)
{
max_freq_router = p;
max_freq = p->frequency;
}
p = p->next;
}
return max_freq_router;
}
struct router_list * delete_from_source_list(struct router_list *router_to_delete, struct router_list *routerhead)
{
struct router_list *p;
if(router_to_delete == routerhead)
{
routerhead = routerhead->next;
router_to_delete->next = NULL;
free(router_to_delete);
return routerhead;
}
else
{
p = routerhead;
while(p->next != router_to_delete)
p = p->next;
p->next = router_to_delete->next;
router_to_delete->next = NULL;
free(router_to_delete);
return routerhead;
}
}
void Path_Reconstruction()
{
struct attacker_list *p = routerAttackerHead;
struct router_list *q;
struct hostent *router_ip_address;
while( p != NULL)
{
// LOG(stdout, LOGL, "Victim:%s",p->victim_ip_addr);
// LOG(stdout, LOGL, "Router:");
fprintf(stdout, "\n%s,", p->victim_ip_addr);
while( p->routerhead != NULL )
{
q = source_with_max_freq(p->routerhead);
// LOG(stdout, LOGL, "%s,", q->router_ip);
router_ip_address = gethostbyname(q->router_ip);
fprintf(stdout, "%s,", router_ip_address->h_name);
// fprintf(stdout, "%s", gethostnameq->routerhead);
p->routerhead = delete_from_source_list(q, p->routerhead);
}
// LOG(stdout, LOGL, "Attacker:%s", p->attacker_ip_addr);
fprintf(stdout, "%s", p->attacker_ip_addr);
fflush(stdout);
p = p->next;
}
}
void receive_udp_traceback(struct command_line_args *object)
{
int fd, len;
struct sockaddr_in myAddr, srcAddr;
struct hostent *localhost_ip_addr;
struct in_addr *address;
char traceback_msg[BUFSIZE];
char sign[BUFSIZE], router_ip[BUFSIZE], attacker_ip[BUFSIZE], victim_ip[BUFSIZE];
int i = 0;
socklen_t addrlen = sizeof(struct sockaddr_in);
memset((char *)&myAddr, 0, sizeof(struct sockaddr_in));
myAddr.sin_family = AF_INET;
localhost_ip_addr = gethostbyname("0.0.0.0");
address = (struct in_addr *)localhost_ip_addr->h_addr;
myAddr.sin_addr.s_addr = inet_addr(inet_ntoa(*address));
// myAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
myAddr.sin_port = htons(object->udpport);
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("Endhost: Cannot create udp socket ");
}
if (bind(fd, (struct sockaddr *)&myAddr, sizeof(struct sockaddr_in)) < 0)
{
perror("Endhost: Victim client bind failed");
}
printf("\nPhase 1: EndHost has UDP port number %d and IP address %s\n",htons(myAddr.sin_port), inet_ntoa(*address));
while( i++ != object->stopthresh )
{
memset(traceback_msg,'\0',BUFSIZE);
memset(router_ip,'\0',BUFSIZE);
memset(sign, '\0', BUFSIZE);
memset(attacker_ip,'\0',BUFSIZE);
memset(victim_ip,'\0',BUFSIZE);
memset((char *)&srcAddr, '\0', sizeof(struct sockaddr_in));
while((len = recvfrom(fd, traceback_msg, BUFSIZE, 0, (struct sockaddr *)&srcAddr, &addrlen))<0)
{
printf("Endhost: Error in receiving traceback data");
continue;
}
if(attack_indication == 0)
i--;
LOG(stdout, LOGL,"Endhost has received traceback from Router, %s",traceback_msg);
sscanf(traceback_msg, "%s %s %s %s", sign, router_ip, attacker_ip, victim_ip);
Create_Attacker_List(router_ip, attacker_ip, victim_ip);
}
send_stopMark_message_routers(object);
Path_Reconstruction();
}
void check_for_attack(struct command_line_args *object)
{
int fd,len;
struct sockaddr_in myAddr, srcAddr;
struct hostent *my_ip_addr;
struct in_addr *address;
char buffer[BUFSIZE];
// extern int file_server_exists;
char hostname[HOSTNAME];
socklen_t addrlen = sizeof(struct sockaddr_in);
memset(buffer,'\0',BUFSIZE);
memset(hostname, '\0', HOSTNAME);
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
LOG(stderr, ERROR, "Error: Cannot create socket of directory server");
}
memset((char *)&myAddr, 0, sizeof(struct sockaddr_in));
myAddr.sin_family = AF_INET;
gethostname(hostname, HOSTNAME);
my_ip_addr = gethostbyname(hostname);
address = (struct in_addr *)my_ip_addr->h_addr;
myAddr.sin_addr.s_addr = inet_addr(inet_ntoa(*address));
myAddr.sin_port = htons(61089);
if (bind(fd, (struct sockaddr *)&myAddr, sizeof(struct sockaddr_in)) < 0)
{
LOG(stderr, ERROR, "Error: EndHost tool bind failed");
}
while((len = recvfrom(fd, buffer, BUFSIZE, 0, (struct sockaddr *)&srcAddr, &addrlen))<0)
{
LOG(stderr, ERROR, "Error in receiving registration data");
continue;
}
if(strncmp(buffer, "Attack_Detected", strlen(buffer)) == 0)
{
attack_indication = 1;
LOG(stdout, LOGL, "Endhost: Received attack detected packet from Traffana");
send_startMark_message_routers(object);
}
LOG(stdout, LOGL, "Check for attack ended");
}
void read_packets(u_char *object, const struct pcap_pkthdr* pkthdr, const u_char* packet)
{
const struct sniff_ip *ip;
int version = 0;
int size_ip = 0;
char ip_addr[MAXIPADDRLEN], ip_src_addr[MAXIPADDRLEN];
memset(ip_addr, '\0', MAXIPADDRLEN);
memset(ip_src_addr, '\0', MAXIPADDRLEN);
ip = (struct sniff_ip *)(packet + SIZE_ETHERNET);
version = ip->ip_vhl >> 4;
size_ip = IP_HL(ip)*4;
if(version != 4)
return;
if(size_ip < 20)
return;
sprintf(ip_src_addr, "%s",inet_ntoa(ip->ip_src));
LOG(stdout, LOGL, "%s",ip_src_addr);
}
/*void determine_attacker_with_no_routers(struct command_line_args *object)
{
char *dev, errbuf[PCAP_ERRBUF_SIZE];
pcap_t *handle;
handle = pcap_open_live(dev, BUFSIZ, 1, 0, errbuf);
if (handle == NULL) {
LOG(stderr, ERROR, "Couldn't open device %s: %s", dev, errbuf);
}
pcap_loop(handle, -1, read_packets, NULL);
}
*/
void send_startMark_message_routers(struct command_line_args *object)
{
/* if(object->routerfile == NULL && attack_indication == 1)
determine_attacker_with_no_routers();
*/
FILE *fp = fopen(object->routerfile, "r");
char router_ip[MAXIPADDRLEN];
int len = 0;
memset(router_ip, '\0', MAXIPADDRLEN);
while(fgets(router_ip, MAXIPADDRLEN, fp) != NULL)
{
len = strlen(router_ip);
router_ip[len - 1] = '\0';
LOG(stdout, LOGL, "Router Ip : %s, TCP Port: %d",router_ip, object->tcpport);
send_start_marking_msg(router_ip, object->tcpport);
}
fclose(fp);
}
void send_stopMark_message_routers(struct command_line_args *object)
{
FILE *fp = fopen(object->routerfile, "r");
char router_ip[MAXIPADDRLEN];
int len = 0;
memset(router_ip, '\0', MAXIPADDRLEN);
while(fgets(router_ip, MAXIPADDRLEN, fp) != NULL)
{
len = strlen(router_ip);
router_ip[len - 1] = '\0';
LOG(stdout, LOGL, "Router Ip : %s, TCP Port: %d",router_ip, object->tcpport);
send_stop_marking_msg(router_ip, object->tcpport);
}
fclose(fp);
}
int main(int argc, char **argv)
{
struct command_line_args object;
char hostname[HOSTNAME];
memset(hostname, 0, HOSTNAME);
gethostname(hostname, HOSTNAME);
strcat(hostname, ".endhost.log");
fp_log = fopen(hostname, "w");
parse_command_line(argc, argv, &object);
pthread_create(&udp_traceback, 0, (void *)receive_udp_traceback, &object);
pthread_create(&attack_checker, 0, (void *)check_for_attack, &object);
pthread_join(attack_checker, 0);
pthread_join(udp_traceback,0);
return 0;
}