4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
/* return -1 on error, else 0 */
22
#include "include/nast.h"
24
int flink (u_char *dev)
30
struct libnet_icmpv4_hdr *icmp;
31
struct libnet_ether_addr *mymac;
36
struct libnet_ipv4_hdr * ip;
38
u_char errbuf[LIBNET_ERRBUF_SIZE];
46
u_char testip[20]; /* ipsorci ritornati da map.c*/
47
u_char mac_src[6], mac_dst[6]; /* mac address */
48
u_long ip_src, ip_dst; /* ip da usare dopo */
50
u_short i, k, sd, pcount, n; /* n=num of up hosts */
52
i = n = k = ptag = ip_dst = sd = lg = 0;
56
w_error(1, "Device is null!\n");
58
#ifdef HAVE_LIBNCURSES
65
w_error (0,"Is very useless demonize me in finding link! Omit");
69
n_print ("princ",2,2,lg,"- Searching for possible hosts to use for test : waiting please... ");
72
/* find two hosts for test */
73
if ((uphost = map_lan(dev, 0, &n))==NULL)
75
if(w_error(0, "\nCan't build truly host list! mmhhh!\nReport bug to author please\n\n")==-1)
79
/* there are at least 3 host in lan? */
82
n_print ("princ",4,2,lg,"\nYou have only %d host in lan, test won't be truly...\n", n+1);
83
n_print ("princ",5,2,lg,"Try again with at least 3 hosts up.\n\n");
87
/* find a suitable host that reply to ping request */
88
if ((l = libnet_init (LIBNET_RAW4, NULL, errbuf))==NULL)
90
w_error(1, "\nError : libnet_init: %s\n", errbuf);
93
if (!(mymac = libnet_get_hwaddr(l)))
95
w_error(1, "\nError : can't get hardware address: %s\n", libnet_geterror(l));
98
/* MAC is my MAC ADDRESS*/
100
mac_src[k]=mymac->ether_addr_octet[k];
102
myip = libnet_get_ipaddr4(l);
105
w_error(1, "\nError : autodetect device ip address failed: %s\n", libnet_geterror(l));
108
if (libnet_build_icmpv4_echo(ICMP_ECHO, 0, 0, 1000, 5249, NULL, 0, l,0)==-1)
111
w_error(1, "\nError : can't build ICMP header : %s\n", libnet_geterror(l));
114
for (i = 0; i<n; i++)
116
sprintf(testip, "%d.%d.%d.%d", uphost[i].ip[0], uphost[i].ip[1], uphost[i].ip[2], uphost[i].ip[3]);
117
if ( (ptag = libnet_build_ipv4(LIBNET_ICMPV4_ECHO_H + LIBNET_IPV4_H, 0x00, 1000, 0, 64, IPPROTO_ICMP, 0, myip, inet_addr(testip), NULL, 0, l, ptag)) ==-1)
120
w_error(1, "\nError : can't build TCP header : %s\n", libnet_geterror(l));
123
if (libnet_write (l) == -1)
126
w_error(1, "\nError writing packet on wire : %s\n", libnet_geterror(l));
130
/* open pcap device NOT in promisc mode */
131
if ((p = pcap_open_live (dev, BUFSIZ, NOT_PROMISC, 10, errbuf))==NULL)
134
w_error(1, "\nError : pcap_open_liver() error : %s\n", errbuf);
137
/* retrive socket descriptor for select() funz */
140
/* timeout is 20 packet or timer.. */
143
/* try for an answer ... */
146
if (pcount == 20) break;
148
/* set 2 secondz delay | DONT TOUCH! */
154
if (!select(sd+1, &rfsd, NULL, NULL, &tv))
157
/* capture packet (packet) and pcap_header (hdr) */
158
packet = (u_char *) pcap_next (p, &hdr);
160
if (packet==NULL) continue;
161
if ((ether_type = handle_ethernet (packet)) != ETHERTYPE_IP) continue;
163
if ((offset = (device(dev,p)))==-1) return -1;
164
ip = (struct libnet_ipv4_hdr *) (packet + offset);
165
icmp = (struct libnet_icmpv4_hdr *) (packet + offset + LIBNET_IPV4_H);
167
/* my destination victim hosts reply -> GOOD :-) */
168
if ((ip->ip_src.s_addr == inet_addr(testip)) && icmp->icmp_type==ICMP_ECHOREPLY && icmp->icmp_id == 1000)
170
/* sisitemo ip/mac dst */
171
ip_dst = ip->ip_src.s_addr;
173
mac_dst[k]=uphost[i].mac[k];
176
/* subito il primo host risponde ai ping */
178
sprintf(testip, "%d.%d.%d.%d", uphost[1].ip[0], uphost[1].ip[1], uphost[1].ip[2], uphost[1].ip[3]);
180
sprintf(testip, "%d.%d.%d.%d", uphost[0].ip[0], uphost[0].ip[1], uphost[0].ip[2], uphost[0].ip[3]);
182
if ( (ip_src=inet_addr(testip)) == -1)
184
if(w_error(0, "\nError : uphost[].ip is not a valid ip. Mhh strange, contact developer please\n")==-1)
193
/* altro pacchetto ricevuto */
198
/* l'host non risponde all'icmp request, vado al prossimo */
202
n_print ("winfo",1,1,lg,"\n\nI don't find any host in you LAN which reply to an icmp request!\nI need at last one to resolve test. Try again later and adjust firewall if you can...\n\n");
205
/* --------------------------------------------------------------------- */
208
n_print ("princ",2,68,lg,"OK");
210
if (uphost) free (uphost);
213
n_print ("princ",3,2,lg,"\n- Try to send icmp spoofed request... \n");
215
if ((l = libnet_init (LIBNET_LINK, dev, errbuf))==NULL)
217
w_error(1, "libnet_init: %s\n", errbuf);
220
/* costruisco il pacchetto */
221
if (libnet_build_icmpv4_echo(ICMP_ECHO, 0, 0, 1000, 5249, NULL, 0, l,0)==-1)
224
w_error(1, "Can't build ICMP header : %s\n", libnet_geterror(l));
227
if (libnet_build_ipv4(LIBNET_ICMPV4_ECHO_H + LIBNET_IPV4_H, 0x00, 1000, 0, 64, IPPROTO_ICMP, 0, ip_src, ip_dst, NULL, 0, l, 0)==-1)
230
w_error(1, "Can't build TCP header : %s\n", libnet_geterror(l));
233
if (libnet_build_ethernet(mac_dst, mac_src, ETHERTYPE_IP, NULL, 0, l, 0)==-1)
236
w_error(1, "Can't build ethernet header : %s\n", libnet_geterror(l));
240
if (libnet_write (l) == -1)
243
w_error(1, "Error writing packet on wire : %s\n", libnet_geterror(l));
246
/* open pcap device in promisc mode */
247
if ((p = pcap_open_live (dev, BUFSIZ, PROMISC, 10, errbuf))==NULL)
250
w_error(1, "pcap_open_liver() error : %s\n", errbuf);
253
/* recupero il descrittore per la select() */
256
n_print ("princ",4,2,lg,"- Waiting for a possible reply...\n");
258
/* per il traffico alto metto un timeout di 30 pacchetti */
265
n_print ("princ",6,2,lg,"- No answer -> supposed SWITCH present\n");
269
/* set 2 secondz delay | DON'T TOUCH! */
275
if (!select(sd+1, &rfsd, NULL, NULL, &tv))
277
n_print("princ",6,2,lg,"- No answer within two seconds -> supposed SWITCH present\n");
281
/* capture packet (packet) and pcap_header (hdr) */
282
packet = (u_char *) pcap_next (p, &hdr);
285
//fprintf (stderr, "Null packet!\n");
289
if ((ether_type = handle_ethernet (packet)) != ETHERTYPE_IP) continue;
291
offset = (device(dev,p));
292
ip = (struct libnet_ipv4_hdr *) (packet + offset);
293
icmp = (struct libnet_icmpv4_hdr *) (packet + offset + LIBNET_IPV4_H);
295
if ((ip->ip_src.s_addr == ip_dst) && icmp->icmp_type==ICMP_ECHOREPLY && icmp->icmp_id
298
n_print ("princ",6,2,lg,"- Supposed HUB present\n");
302
/* altro pacchetto ricevuto */
307
n_print("winfo",2,1,0,"Finished\n");