-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
707 lines (557 loc) · 23.1 KB
/
main.c
File metadata and controls
707 lines (557 loc) · 23.1 KB
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
* name: main.c
* this file is part of the "Net-Injector" project. It defines the main() function
* Copyright (C) 2002 by Jean Philippe GUILLEMIN <jp.guillemin@free.fr>
* license: This software is under GPL license
* date: 04 17 2003
* rev: 0.8
*/
#include "neti.h" /* include headers are here !*/
/* See below for usage */
int usage(void) {
fprintf(stdout,"\nNet-Injector version 0.8 (jp.guillemin@free.fr)\n");
fprintf(stdout, "\nUsage : neti [options] [dest IP] [dest port]\n"
"\n\t GLOBAL options :\n"
"\t\t [-C config file] use a config file for parameters\n"
"\t\t [-m MODE] set the protocol as UDP, TCP, ICMP or protocol number\n"
"\t\t [-h] help\n"
"\t\t [-a IP] source IP adress\n"
"\t\t [-i if name] output interface to use\n"
"\t\t [-p port] source port\n"
"\t\t [-d filename] file to read data from\n"
"\t\t [-T TTL] set the IP time to live\n"
"\t\t [-o offset] set the IP offset\n"
"\t\t [-q TOS] set the TOS options\n"
"\t\t\t \"TOS\" is a chain set from : \n"
"\t\t\t [C] : Minimum Cost (nntp)\n"
"\t\t\t [R] : Maximum Reliability (snmp)\n"
"\t\t\t [T] : Maximum Throughput (data transfert)\n"
"\t\t\t [D] : Minimum Delay (telnet)\n"
"\t\t [-O] flOOd mode\n"
"\t\t [-n] numeric mode, no lookup before sending\n"
"\n\t SNIFFER options :\n"
"\t\t [-b] blind mode, don't listen, just send\n"
"\t\t [-r] try a reverse lookup before printing\n"
"\t\t [-l] listen mode, don't stop sniffing\n"
"\t\t [-t TIMEOUT] timeout to wait for response in seconds\n"
"\t\t [-s] set the promiscuous mode\n"
"\t\t [-f FILTERS] filter incoming packets\n"
"\t\t\t \"FILTERS\" is a chain set from : \n"
"\t\t\t [U] : UDP protocol \n"
"\t\t\t [T] : TCP protocol \n"
"\t\t\t [I] : ICMP protocol \n"
"\t\t\t [A] : IP Address of target \n"
"\n\t TCP options :\n"
"\t\t [-F FLAGS] set the TCP flags & options \n"
"\t\t\t \"FLAGS\" option is a chain set from : \n"
"\t\t\t [S] the SYN flag\n"
"\t\t\t [A] the ACK flag\n"
"\t\t\t [P] the PUSH flag\n"
"\t\t\t [R] the RST flag\n"
"\t\t\t [F] the FIN flag\n"
"\t\t\t [U] the URG flag\n"
"\t\t\t [T] add the TCP_TIMESTAMP option\n"
"\t\t [-W WINDOW] set the TCP window\n"
"\n\t ICMP options :\n"
"\t\t [-y ICMP type] set the ICMP type\n"
"\t\t [-c ICMP code] set the ICMP code\n"
"\t\t [-g ICMP redirect gateway] set the IP of the gw\n"
"\t\t [-e ICMP echo ID] set the ICMP echo ID\n"
"\t\t [-H] show ICMP supported types and codes\n"
"\n");
return EXIT_SUCCESS;
} /* end usage */
int icmp_usage(void) {
fprintf(stdout,"\nSupported ICMP types and codes\n");
fprintf(stdout, "\n[-y] option can be followed by any of these types :\n"
"\t\t ECHOREPLY\t\t\t0 Echo Reply \n"
"\t\t DEST_UNREACH\t\t\t3 Destination Unreachable \n"
"\t\t SOURCE_QUENCH\t\t4 Source Quench \n"
"\t\t REDIRECT\t\t\t5 Redirect (change route) \n"
"\t\t ECHO\t\t\t\t8 Echo Request \n"
"\t\t TIME_EXCEEDED\t\t11 Time Exceeded \n"
"\t\t PARAMETERPROB\t\t12 Parameter Problem \n"
"\t\t TIMESTAMP\t\t\t13 Timestamp Request \n"
"\t\t TIMESTAMPREPLY\t\t14 Timestamp Reply \n"
"\t\t INFO_REQUEST\t\t\t15 Information Request \n"
"\t\t INFO_REPLY\t\t\t16 Information Reply \n"
"\t\t ADDRESS\t\t\t17 Address Mask Request \n"
"\t\t ADDRESSREPLY\t\t\t18 Address Mask Reply \n"
"\n[-c] option can be followed by any of these codes:\n"
"\t Codes for UNREACH :\n"
"\t\t NET_UNREACH\t\t\t0 Network Unreachable \n"
"\t\t HOST_UNREACH\t\t\t1 Host Unreachable \n"
"\t\t PROT_UNREACH\t\t\t2 Protocol Unreachable \n"
"\t\t PORT_UNREACH\t\t\t3 Port Unreachable \n"
"\t\t FRAG_NEEDED\t\t\t4 Fragmentation Needed/DF set \n"
"\t\t SR_FAILED\t\t\t5 Source Route failed \n"
"\t\t NET_UNKNOWN\t\t\t6\n"
"\t\t HOST_UNKNOWN\t\t\t7\n"
"\t\t HOST_ISOLATED\t\t8\n"
"\t\t NET_ANO\t\t\t9\n"
"\t\t HOST_ANO\t\t\t10\n"
"\t\t NET_UNR_TOS\t\t\t11\n"
"\t\t HOST_UNR_TOS\t\t\t12\n"
"\t\t PKT_FILTERED\t\t\t13 Packet filtered \n"
"\t\t PREC_VIOLATION\t\t14 Precedence violation \n"
"\t\t PREC_CUTOFF\t\t\t15 Precedence cut off \n"
"\t\t UNREACH\t\t\t15 \n"
"\t Codes for REDIRECT :\n"
"\t\t REDIR_NET\t\t\t0 Redirect Net \n"
"\t\t REDIR_HOST\t\t\t1 Redirect Host \n"
"\t\t REDIR_NETTOS\t\t\t2 Redirect Net for TOS \n"
"\t\t REDIR_HOSTTOS\t\t3 Redirect Host for TOS \n"
"\t Codes for TIME_EXCEEDED :\n"
"\t\t EXC_TTL\t\t\t0 TTL count exceeded \n"
"\t\t EXC_FRAGTIME\t\t\t1 Fragment Reass time exceeded \n"
"\n");
return EXIT_SUCCESS;
} /* end icmp_usage */
/************************************************************/
/* FUNCTIONS INCLUSIONS (to build without make) */
/************************************************************/
/*
#include "chksum.c"
#include "tcp.c"
#include "udp.c"
#include "icmp.c"
#include "lookup.c"
#include "getif_ip.c"
#include "getif_name.c"
#include "getout_ip.c"
#include "promisc.c"
#include "listen.c"
#include "utils.c"
*/
/**********************/
/* MAIN */
/**********************/
char *filename, *datagram, *pack, char_addr_buf[32];
int main(int argc, char *argv[]) {
struct sockaddr_in sin;
struct stat filestat;
FILE *file;
u_int32_t size, transporthdr_size, inet_addr_buf;
int i, pack_size;
char *filename, *datagram, *pack, char_addr_buf[32], *config_file;
struct iphdr *iph;
int one = 1;
const int *val = &one;
config_file = malloc(128);
myhdr = malloc(sizeof(struct optionhdr));
waitfor = malloc(sizeof(struct filter));
signal(SIGINT, catch_sig);
if (getuid()) {
fprintf(stderr, "Error : you should be root to use Net-Injector !\n");
exit (EXIT_FAILURE);
}
if(argc < 2)
{
usage();
exit (EXIT_FAILURE);
}
/* set default_parameters */
strncpy (config_file, "neti.conf", 9);
myhdr->src_port = 1025;
myhdr->inet_src_port = htons(1025);
myhdr->dst_port = 80;
myhdr->inet_dst_port = htons(80);
myhdr->data_size = 0;
strncpy(myhdr->dst_ip, "127.0.0.1", 32);
myhdr->inet_dst_ip = inet_addr(myhdr->dst_ip);
myhdr->syn = 0;
myhdr->ack = 0;
myhdr->psh = 0;
myhdr->rst = 0;
myhdr->fin = 0;
myhdr->urg = 0;
myhdr->ttl = 40;
myhdr->window = 65535;
myhdr->offset = 0;
myhdr->proto = 6;
myhdr->icmp_type = 8;
myhdr->icmp_code = 0;
myhdr->icmp_echo_id = 1;
myhdr->icmp_echo_sequence = 1;
myhdr->iptos = 0;
myhdr->tcp_opt_size = 0;
flood = 0;
keep_sniffing = 0;
no_lookup = 0;
do_reverse_lookup = 0;
use_this_if = 0;
use_this_address = 0;
be_promiscuous = 0;
be_blind = 0;
waitfor->filter_on_icmp = 0;
waitfor->filter_on_udp = 0;
waitfor->filter_on_tcp = 0;
waitfor->filter_on_target_addr = 0;
time_to_wait_for_packet = 36000;
/* parsing cli args & config file */
while ((i = getopt(argc, argv, "HUOlnhsbrC:m:T:W:t:F:o:a:p:d:i:y:c:g:e:f:q:")) != EOF){
switch (i){
case 'C': /* parse the config file */
strncpy(config_file, optarg, 128);
take_args_from_file(config_file);
break;
case 'h': usage(); exit (EXIT_SUCCESS);
case 'H': icmp_usage(); exit (EXIT_SUCCESS);
case 'i':
strncpy(myhdr->interface, optarg, 32);
use_this_if=1;
break;
case 'a':
myhdr->inet_src_ip=inet_addr(optarg);
strncpy(myhdr->src_ip, optarg, 32);
use_this_address = 1;
break;
case 'p':
myhdr->inet_src_port=htons(atoi(optarg));
myhdr->src_port=atoi(optarg);
break;
case 'd': /* payload data will be read from filename */
size = (sizeof (optarg)+2);
filename = malloc(size);
strncpy(filename, optarg, size);
if((file=fopen(filename, "rb")) == NULL) {
fprintf (stderr,"Error opening file %s!\n",filename);
exit(EXIT_SUCCESS);
}
stat(filename,&filestat);
myhdr->data_size = filestat.st_size;
payload = malloc(myhdr->data_size);
fread(payload,sizeof(char),myhdr->data_size/sizeof(char),file);
fclose(file);
break;
case 'm':
if (!strcmp (optarg, "UDP")) {
myhdr->proto = 17;
break;
}
if (!strcmp (optarg, "TCP")) {
myhdr->proto = 6;
break;
}
if (!strcmp (optarg, "ICMP")) {
myhdr->proto = 1;
break;
}
myhdr->proto = atoi(optarg);
break;
case 'F':
if (strstr (optarg, "S")!=NULL) myhdr->syn = 1;
if (strstr (optarg, "P")!=NULL) myhdr->psh = 1;
if (strstr (optarg, "A")!=NULL) myhdr->ack = 1;
if (strstr (optarg, "R")!=NULL) myhdr->rst = 1;
if (strstr (optarg, "F")!=NULL) myhdr->fin = 1;
if (strstr (optarg, "U")!=NULL) myhdr->urg = 1;
if (strstr (optarg, "T")!=NULL) myhdr->tcp_opt_size = 12;
break;
case 'T': myhdr->ttl = atoi(optarg);break;
case 'W': myhdr->window = atoi(optarg);break;
case 'y':
myhdr->icmp_type = atoi(optarg);
if (myhdr->icmp_type == 13) myhdr->data_size = ICMP_TIMESTAMP_SIZE;
break;
case 'c': myhdr->icmp_code = atoi(optarg);break;
case 'g': strncpy(myhdr->icmp_redirect_gateway, optarg, 32);break;
case 'e': myhdr->icmp_echo_id = atoi(optarg);break;
case 'o': myhdr->offset = atoi(optarg);break;
case 'f':
if (strstr (optarg, "U")!=NULL) waitfor->filter_on_udp = 1;
if (strstr (optarg, "T")!=NULL) waitfor->filter_on_tcp = 1;
if (strstr (optarg, "I")!=NULL) waitfor->filter_on_icmp = 1;
if (strstr (optarg, "A")!=NULL) waitfor->filter_on_target_addr = 1;
break;
case 'q':
if (strstr (optarg, "C")!=NULL) myhdr->iptos |= 0x2;
if (strstr (optarg, "R")!=NULL) myhdr->iptos |= 0x4;
if (strstr (optarg, "T")!=NULL) myhdr->iptos |= 0x8;
if (strstr (optarg, "D")!=NULL) myhdr->iptos |= 0x10;
break;
case 'O': flood = 1;break;
case 'l': keep_sniffing = 1;break;
case 't': time_to_wait_for_packet=atoi(optarg);break;
case 'n': no_lookup = 1;break;
case 'r': do_reverse_lookup = 1;break;
case 's': be_promiscuous = 1;break;
case 'b': be_blind = 1;
}
}
/* It's now time to parse the last two arguments (dest IP and port) */
/* printf("\noptind=%d ::: argc=%d\n", optind, argc);*/
if (optind >= (argc)){
fprintf(stdout, "\nNo IP address specified : using default / config_file...\n");
}else{
if (!no_lookup){
lookup(&inet_addr_buf, char_addr_buf, argv[optind]); /* resolving given name into IP */
myhdr->inet_dst_ip = inet_addr_buf;
strncpy(myhdr->dst_ip, char_addr_buf, 32);
}else{
strncpy(myhdr->dst_ip, argv[optind], 32);
myhdr->inet_dst_ip = inet_addr(myhdr->dst_ip);
}
}
if (myhdr->proto != 1){ /* if we send ICMP we do not need dest port number */
optind++;
if (optind >= argc){
fprintf(stdout, "\nNo dest port specified : using default / config_file...\n");
}else{
myhdr->inet_dst_port=htons(atoi(argv[optind])); /* getting dest port number */
myhdr->dst_port=atoi(argv[optind]);
}
}
/* BUILD_SOCKADDR_IN structure */
sin.sin_family = AF_INET;
sin.sin_port = myhdr->inet_dst_port;
sin.sin_addr.s_addr = myhdr->inet_dst_ip;
/* If it's not forced by the command line [-a], we need to find a valid source IP address */
if (use_this_address == 0){
if (use_this_if == 1){
getif_ip_by_name(myhdr->src_ip, &myhdr->inet_src_ip, myhdr->interface); /* find given interface IP address*/
}else{
/* Try to obtain the IP address of the output interface */
struct sockaddr_in out;
out.sin_family = AF_INET;
if (getout_ip(&out, &sin) == 0) {
/*fprintf(stdout, "\n addr=%s\n", inet_ntoa(out.sin_addr)); debug */
myhdr->inet_src_ip = out.sin_addr.s_addr;
strncpy(myhdr->src_ip, inet_ntoa(out.sin_addr), 32);
}else {
fprintf(stderr, "Error : cannot get src address !\n");
exit(EXIT_FAILURE);
}
getif_name_by_ip(myhdr->interface, &myhdr->inet_src_ip); /* Try to obtain the name of the output interface */
}
}
/* Opening RAW SOCKETs */
if (!(so_udp = socket (AF_INET, SOCK_RAW, IPPROTO_UDP))) { /* Open UDP raw socket */
fprintf (stderr, "Error : cannot open socket !\n");
exit (EXIT_FAILURE);
}
if (!(so_tcp = socket (AF_INET, SOCK_RAW, IPPROTO_TCP))) { /* Open TCP raw socket */
fprintf (stderr, "Error : cannot open socket !\n");
exit (EXIT_FAILURE);
}
if (!(so_icmp = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP))) { /* Open ICMP raw socket */
fprintf (stderr, "Error : cannot open socket !\n");
exit (EXIT_FAILURE);
}
/*fcntl(so_udp, F_SETFL, O_NONBLOCK);*/ /* set it to non-blocking mode */
/*fcntl(so_icmp, F_SETFL, O_NONBLOCK);*/ /* set it to non-blocking mode */
/*fcntl(so_tcp, F_SETFL, O_NONBLOCK);*/ /* set it to non-blocking mode */
setsockopt(so_udp, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)); /* enable broadcasts */
setsockopt(so_tcp, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)); /* enable broadcasts */
setsockopt(so_icmp, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)); /* enable broadcasts */
switch (myhdr->proto) {
case 17 :
so_send = &so_udp; /* set *so_send pointer to UDP raw socket */
transporthdr_size = UDPHDR_SIZE;
break;
case 1 :
so_send = &so_icmp; /* set *so_send pointer to ICMP raw socket */
transporthdr_size = ICMPHDR_SIZE;
break;
case 6 :
so_send = &so_tcp; /* set *so_send pointer to TCP raw socket */
transporthdr_size = TCPHDR_SIZE;
break;
default :
if (!(so_raw = socket (AF_INET, SOCK_RAW, myhdr->proto))) { /* Open raw socket */
fprintf (stderr, "Error : cannot open socket !\n");
exit (EXIT_FAILURE);
}
so_send = &so_raw;
transporthdr_size = 0;
break;
}
/* filling IP header */
/*
0 ____________________15-16___________________________31
| 4 | IHL | To S | Total Lenght |
|_____|_______|________|______________________________|
| Identification | Flags | Fragment offset |
|____________________|_______|________________________|
| TTL | Protocol | Header checksum |
|_________|____________|______________________________|
| Source IP address |
|_____________________________________________________|
| Destination IP address |
|_____________________________________________________|
> Options (facultatif) <
|_____________________________________________________|
| |
< Data <
> >
|_____________________________________________________|
*/
datagram = malloc(IPHDR_SIZE + transporthdr_size + myhdr->tcp_opt_size + myhdr->data_size);
data = malloc(myhdr->data_size);
iph = (struct iphdr*) datagram; /* set memory adress -> beginning of IP HEADER */
memset(datagram, 0, IPHDR_SIZE + transporthdr_size + myhdr->tcp_opt_size + myhdr->data_size);
iph->ihl = 5;
iph->version = 4;
iph->tos = myhdr->iptos;
iph->tot_len = IPHDR_SIZE + transporthdr_size + myhdr->tcp_opt_size + myhdr->data_size;
iph->ttl = myhdr->ttl;
iph->protocol = myhdr->proto;
iph->check = 0; /* checksum will be set later */
iph->saddr = myhdr->inet_src_ip;
iph->daddr = myhdr->inet_dst_ip;
if (myhdr->offset != 0){
iph->frag_off |= htons(MF);
iph->frag_off |= htons(myhdr->offset >> 3);
}
/* filling UDP/TCP/ICMP header */
if (myhdr->proto == 6){
pack_size = TCPHDR_SIZE + myhdr->tcp_opt_size + myhdr->data_size;
pack = malloc(PSEUDOHDR_SIZE + pack_size);
if (write_tcp(pack, pack_size, iph->saddr, iph->daddr) != 0) fprintf (stderr, "Error : cannot build TCP header !\n");
iph->check = sum ((u_int16_t *) datagram, iph->tot_len);
memcpy((datagram + IPHDR_SIZE), (pack + PSEUDOHDR_SIZE), transporthdr_size + myhdr->tcp_opt_size + myhdr->data_size);
}
if (myhdr->proto == 17){
pack_size = UDPHDR_SIZE + myhdr->data_size;
pack = malloc(PSEUDOHDR_SIZE + pack_size);
if (write_udp(pack, pack_size, iph->saddr, iph->daddr) != 0) fprintf (stderr, "Error : cannot build UDP header !\n");
iph->check = sum ((u_int16_t *) datagram, iph->tot_len);
memcpy((datagram + IPHDR_SIZE), (pack + PSEUDOHDR_SIZE), transporthdr_size + myhdr->tcp_opt_size + myhdr->data_size);
}
if (myhdr->proto == 1){
pack_size = ICMPHDR_SIZE + myhdr->data_size;
pack = malloc(pack_size);
if (write_icmp(pack, pack_size) != 0) fprintf (stderr, "Error : cannot build ICMP header !\n");
iph->check = sum ((u_int16_t *) datagram, iph->tot_len);
memcpy((datagram + IPHDR_SIZE), pack, transporthdr_size + myhdr->tcp_opt_size + myhdr->data_size);
}
/* Call to IP_HDRINCL, to inform kernel that header is included in the given data */
if (setsockopt (*so_send, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0) {
fprintf (stderr, "Error : cannot set HDRINCL!\n");
exit(EXIT_FAILURE);
}
/* sending packet */
if (flood == 1){
fprintf (stdout, "flooding ...\n");
print(do_reverse_lookup);
while (1){
if (sendto (*so_send, datagram, iph->tot_len, 0, (struct sockaddr *) &sin,
sizeof(struct sockaddr)) < 0) { /* normal send() */
fprintf (stderr, "Error : cannot send packet !\n");
exit(EXIT_FAILURE);
}
}
}else {
print(do_reverse_lookup);
if (sendto (*so_send, datagram, iph->tot_len, 0, (struct sockaddr *) &sin,
sizeof(struct sockaddr)) < 0) { /* normal send() */
fprintf (stderr, "Error : cannot send packet !\n");
exit(EXIT_FAILURE);
}else {
if (!be_blind) {
if (be_promiscuous == 1) set_promisc(*so_send);
sniff(so_udp, so_tcp, so_icmp, keep_sniffing, time_to_wait_for_packet, do_reverse_lookup); /* listen for response */
}
}
}
close_sockets();
return EXIT_SUCCESS;
} /* end main */
/****************************/
/*print out what we've done */
/****************************/
/*
param1 : rlookup = [0,1] tell print() to perform a reverse lookup (1) or no (0)
*/
void print(u_int16_t rlookup){
char src_hostname[1024];
char dst_hostname[1024];
if (rlookup){
reverse_lookup(src_hostname, myhdr->src_ip);
reverse_lookup(dst_hostname, myhdr->dst_ip);
}else{
strncpy(src_hostname, myhdr->src_ip, 1024);
strncpy(dst_hostname, myhdr->dst_ip, 1024);
}
if (myhdr->proto == 6) {
fprintf(stdout, "Sending ");
fprintf(stdout, "<TCP>");
if (myhdr->syn != 0) fprintf(stdout, "-SYN");
if (myhdr->ack != 0) fprintf(stdout, "-ACK");
if (myhdr->psh != 0) fprintf(stdout, "-PSH");
if (myhdr->rst != 0) fprintf(stdout, "-RST");
if (myhdr->fin != 0) fprintf(stdout, "-FIN");
fprintf(stdout, "-WIN(%d)", myhdr->window);
fprintf (stdout, "-%s[%d]", src_hostname, myhdr->src_port);
if (myhdr->offset != 0) fprintf(stdout, "-FRAG(%d)", myhdr->offset);
fprintf(stdout, "-TTL(%d)", myhdr->ttl);
fprintf(stdout, "-TOS(%d)", myhdr->iptos);
fprintf (stdout, " -> %s[%d]\n", dst_hostname, myhdr->dst_port);
}
if (myhdr->proto == 17) {
fprintf(stdout, "Sending ");
fprintf(stdout, "<UDP>");
fprintf (stdout, "-%s[%d]", src_hostname, myhdr->src_port);
if (myhdr->offset != 0) fprintf(stdout, "-FRAG(%d)", myhdr->offset);
fprintf(stdout, "-TTL(%d)", myhdr->ttl);
fprintf(stdout, "-TOS(%d)", myhdr->iptos);
fprintf (stdout, " -> %s[%d]\n", dst_hostname, myhdr->dst_port);
}
if (myhdr->proto == 1) {
fprintf(stdout, "Sending ");
fprintf(stdout, "<ICMP>");
if (myhdr->icmp_type==0) {
fprintf(stdout, "-[ECHO_REPLY]");
fprintf(stdout, "-[ID %d]", myhdr->icmp_echo_id);
}
if (myhdr->icmp_type==3){
fprintf(stdout, "-[DEST_UNREACH]");
if (myhdr->icmp_code==0) fprintf(stdout, "-[NET_UNREACH]");
if (myhdr->icmp_code==1) fprintf(stdout, "-[HOST_UNREACH]");
if (myhdr->icmp_code==2) fprintf(stdout, "-[PROT_UNREACH]");
if (myhdr->icmp_code==3) fprintf(stdout, "-[PORT_UNREACH]");
if (myhdr->icmp_code==4) fprintf(stdout, "-[FRAG_NEEDED]");
if (myhdr->icmp_code==5) fprintf(stdout, "-[SR_FAILED]");
if (myhdr->icmp_code==6) fprintf(stdout, "-[NET_UNKNOWN]");
if (myhdr->icmp_code==7) fprintf(stdout, "-[HOST_UNKNOWN]");
if (myhdr->icmp_code==8) fprintf(stdout, "-[HOST_ISOLATED]");
if (myhdr->icmp_code==9) fprintf(stdout, "-[NET_ANO]");
if (myhdr->icmp_code==10) fprintf(stdout, "-[HOST_ANO]");
if (myhdr->icmp_code==11) fprintf(stdout, "-[NET_UNR_TOS]");
if (myhdr->icmp_code==12) fprintf(stdout, "-[HOST_UNR_TOS]");
if (myhdr->icmp_code==13) fprintf(stdout, "-[PKT_FILTERED]");
if (myhdr->icmp_code==14) fprintf(stdout, "-[PREC_VIOLATION]");
if (myhdr->icmp_code==15) fprintf(stdout, "-[PREC_CUTOFF]");
}
if (myhdr->icmp_type==4) fprintf(stdout, "-[SOURCE_QUENCH]");
if (myhdr->icmp_type==5){
fprintf(stdout, "-[REDIRECT]");
if (myhdr->icmp_code==0) fprintf(stdout, "-[REDIR_NET]");
if (myhdr->icmp_code==1) fprintf(stdout, "-[REDIR_HOST]");
if (myhdr->icmp_code==2) fprintf(stdout, "-[REDIR_NETTOS]");
if (myhdr->icmp_code==3) fprintf(stdout, "-[REDIR_HOSTTOS]");
fprintf(stdout, "-[GW %s]", myhdr->icmp_redirect_gateway);
}
if (myhdr->icmp_type==8) {
fprintf(stdout, "-[ECHO_REQUEST]");
fprintf(stdout, "-[ID %d]", myhdr->icmp_echo_id);
}
if (myhdr->icmp_type==11){
fprintf(stdout, "-[TIME_EXCEEDED]");
if (myhdr->icmp_code==0) fprintf(stdout, "-[EXC_TTL]");
if (myhdr->icmp_code==1) fprintf(stdout, "-[EXC_FRAGTIME]");
}
if (myhdr->icmp_type==12) fprintf(stdout, "-[PARAMETER PROB]");
if (myhdr->icmp_type==13) fprintf(stdout, "-[TIMESTAMP]");
if (myhdr->icmp_type==14) fprintf(stdout, "-[TIMESTAMP REPLY]");
if (myhdr->icmp_type==15) fprintf(stdout, "-[INFO_REQUEST]");
if (myhdr->icmp_type==16) fprintf(stdout, "-[INFO_REPLY]");
if (myhdr->icmp_type==17) fprintf(stdout, "-[ADDRESS]");
if (myhdr->icmp_type==18) fprintf(stdout, "-[ADDRESSREPLY]");
fprintf (stdout, "-%s", src_hostname);
if (myhdr->offset != 0) fprintf(stdout, "-FRAG(%d)", myhdr->offset);
fprintf(stdout, "-TTL(%d)", myhdr->ttl);
fprintf(stdout, "-TOS(%d)", myhdr->iptos);
fprintf (stdout, " -> %s\n", dst_hostname);
}
}