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
|
/********************************************************************************
NDPMon - Neighbor Discovery Protocol Monitor
Copyright (C) 2006 MADYNES Project, LORIA - INRIA Lorraine (France)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Author Info:
Name: Thibault Cholez
Mail: thibault.cholez@esial.uhp-nancy.fr
Maintainer:
Name: Frederic Beck
Mail: frederic.beck@loria.fr
MADYNES Project, LORIA-INRIA Lorraine, hereby disclaims all copyright interest in
the tool 'NDPMon' (Neighbor Discovery Protocol Monitor) written by Thibault Cholez.
Olivier Festor, Scientific Leader of the MADYNEs Project, 20 August 2006
***********************************************************************************/
#ifdef _COUNTERMEASURES_
#include "./plugins/countermeasures/countermeasures.h"
#endif
#include "membounds.h"
#include "routers.h"
#include "neighbors.h"
#include "parser.h"
#include "ndpmon_defs.h"
#include "ndpmon.h"
int DEBUG = 0;
router_list_t *routers = NULL;
neighbor_list_t *neighbors = NULL;
char admin_mail[128] = "";
char syslog_facility[16] = "";
int ignor_autoconf = 0;
/* Initialize with values defined in ndpmon_defs and generated by ./configure */
char config_path[128] = _CONFIG_PATH_;
char cache_path[128] = _CACHE_PATH_;
char dtd_path[128] = _CACHE_DTD_PATH_;
char dtd_config_path[128] = _CONFIG_DTD_PATH_;
char discovery_history_path[128] = _DISCOVERY_HISTORY_PATH_;
struct action_selector action_low_pri, action_high_pri;
int use_reverse_hostlookups;
#ifdef _MACRESOLUTION_
manufacturer_t *manuf = NULL;
#endif
int learning = 0;
static pcap_t* descr = NULL;
/*Function called each time that a packet pass the filter and is captured*/
void callback(u_char *args,const struct pcap_pkthdr* hdr,const u_char*
packet)
{
static time_t last_save_time=0;/*to periodicaly save the neighbor cache*/
time_t current= time(NULL);
struct ether_header *eptr; /* net/ethernet.h */
struct ip6_hdr* ipptr; /*netinet/ip6.h*/
struct icmp6_hdr* icmpptr=NULL; /*netinet/icmp6.h*/
struct ip6_ext* opt_hdr;
struct nd_router_solicit* rsptr;
struct nd_router_advert* raptr;
struct nd_neighbor_solicit* nsptr;
struct nd_neighbor_advert* naptr;
struct nd_redirect* rdptr;
#ifdef _COUNTERMEASURES_
struct nd_ndpmon_present* npptr;
#endif
int new_eth =0;/*Use to detect dad dos attack, 1 if NA annouces a new station*/
int type_58 = 0;
char message[NOTIFY_BUFFER_SIZE];
const time_t* time = (const time_t*) &(hdr->ts).tv_sec;
if(DEBUG)
{
/* General info on the paquet */
fprintf(stderr,"length of this packet: %d\n", hdr->len);
fprintf(stderr,"Received at: %s", (char*)ctime(time));
}
/* Info from the ethernet layer */
eptr = (struct ether_header *) packet;
if(DEBUG)
{
print_eth(*eptr);
}
/* Info from the IPV6 layer */
if (ntohs (eptr->ether_type) == ETHERTYPE_IPV6)
{
if(DEBUG)
{
fprintf(stderr,"Ethernet type hex:%x dec: it's an IPv6 packet\n", ntohs(eptr->ether_type));
}
ipptr = (struct ip6_hdr*)(packet + ETHERNET_SIZE);
if(DEBUG)
{
print_ip6hdr(*ipptr);
}
/*Jumping optional headers if there are*/
if(ipptr->ip6_nxt != 58)
{
opt_hdr = (struct ip6_ext*)(packet + ETHERNET_SIZE + IPV6_SIZE);
if(DEBUG)
{
fprintf(stderr,"next option header : %d\n",opt_hdr->ip6e_nxt);
}
while((opt_hdr->ip6e_nxt != 58) &&((u_char*)opt_hdr < (packet+hdr->len)))
{
/*cf rfc 2460: opt_hdr->ip6e_len
8-bit unsigned integer. Length of the extension
header in 8-octet units, not including the first
8 octets.
*/
opt_hdr = (struct ip6_ext*)((u_char*)opt_hdr + (1 + opt_hdr->ip6e_len)*8);
if(DEBUG)
{
fprintf(stderr,"next option header : %d\n",opt_hdr->ip6e_nxt);
}
}
if(opt_hdr->ip6e_nxt == 58)
{
icmpptr = (struct icmp6_hdr*)((u_char*)opt_hdr + (1 + opt_hdr->ip6e_len)*8);
/*Info from the ICMPv6 layer*/
if(DEBUG)
{
fprintf(stderr,"IP type after ext headers: %d, it's an ICMPv6 packet\n",opt_hdr->ip6e_nxt);
}
type_58 = 1;
}
}
else
{
icmpptr = (struct icmp6_hdr*)(packet + ETHERNET_SIZE + IPV6_SIZE);
/*Info from the ICMPv6 layer*/
if(DEBUG)
{
fprintf(stderr,"IP type: %d, it's an ICMPv6 packet\n",ipptr->ip6_nxt);
}
type_58 = 1;
}
if(type_58 && icmpptr)
{
struct ether_addr *src_eth;
int general_tests = 0;
#ifdef _COUNTERMEASURES_
if (cm_on_link_remove(packet, hdr->len)!=0) {
fprintf(stderr,"---- ICMP packet ----\n");
fprintf(stderr, "[countermeasures]: Packet dropped as it is a NDPMon counter measure.\n");
fprintf(stderr,"------------------\n\n");
return;
}
#endif
if(DEBUG)
{
fprintf(stderr,"ND type: %d\n", icmpptr->icmp6_type);
}
switch (icmpptr->icmp6_type)
{
case ND_ROUTER_SOLICIT:
fprintf(stderr,"----- ND_ROUTER_SOLICIT -----\n");
rsptr = (struct nd_router_solicit*) (packet + ETHERNET_SIZE + IPV6_SIZE);
if(DEBUG)
{
print_rs(*rsptr);
}
/*RS with node addr Should be used to build cache so:*/
if(!IN6_IS_ADDR_UNSPECIFIED(&ipptr->ip6_src))
{
src_eth = (struct ether_addr *) eptr->ether_shost;
new_station(&neighbors,*src_eth,ipptr->ip6_src, &new_eth);
}
general_tests = 1;
break;
case ND_ROUTER_ADVERT:
fprintf(stderr,"----- ND_ROUTER_ADVERT -----\n");
src_eth = (struct ether_addr *) eptr->ether_shost;
raptr = (struct nd_router_advert*) (packet + ETHERNET_SIZE + IPV6_SIZE);
if(DEBUG)
{
print_ra(*raptr);
}
new_station(&neighbors,*src_eth,ipptr->ip6_src, &new_eth);
watch_ra(message, packet, eptr, ipptr, hdr->len);
general_tests = 1;
break;
case ND_NEIGHBOR_SOLICIT:
fprintf(stderr,"----- ND_NEIGHBOR_SOLICIT -----\n");
nsptr = (struct nd_neighbor_solicit*) (packet + ETHERNET_SIZE + IPV6_SIZE);
if(DEBUG)
{
print_ns(*nsptr);
}
/*NS with node addr Should be used to build cache so:*/
if(!IN6_IS_ADDR_UNSPECIFIED(&ipptr->ip6_src))
{
src_eth = (struct ether_addr *) eptr->ether_shost;
new_station(&neighbors,*src_eth,ipptr->ip6_src, &new_eth);
}
watch_dad(eptr, ipptr, nsptr);
general_tests = 1;
break;
case ND_NEIGHBOR_ADVERT:
fprintf(stderr,"----- ND_NEIGHBOR_ADVERT -----\n");
src_eth = (struct ether_addr *) eptr->ether_shost;
naptr = (struct nd_neighbor_advert*) (packet + ETHERNET_SIZE + IPV6_SIZE );
if(DEBUG)
{
print_na(*naptr);
}
new_station(&neighbors,*src_eth,ipptr->ip6_src, &new_eth);
watch_dad_dos(message, eptr, ipptr, naptr, new_eth);
watch_R_flag(message, eptr, ipptr, naptr);
general_tests = 1;
break;
case ND_REDIRECT:
rdptr = (struct nd_redirect*) (packet + ETHERNET_SIZE + IPV6_SIZE);
print_rd(*rdptr);
watch_rd_src(message, eptr, ipptr);
general_tests = 1;
break;
#ifdef _COUNTERMEASURES_
case ND_NDPMON_PRESENT:
npptr = (struct nd_ndpmon_present*) (packet + ETHERNET_SIZE + IPV6_SIZE);
if (npptr->nd_np_code==ND_NP_CODE) {
fprintf(stderr,"----- ND_NDPMON_PRESENT -----\n");
watch_ndpmon_present(message, packet, eptr, ipptr, npptr, hdr->len);
general_tests = 1;
break;
}
/*else no break*/
#endif
case 128:
printf ("Echo request: %d\n", icmpptr->icmp6_type);
break;
case 129:
printf ("Echo reply: %d\n", icmpptr->icmp6_type);
break;
case 1:
printf ("Address Unreachable: %d\n", icmpptr->icmp6_type);
break;
default:
printf ("Unknown ICMPv6 type: %d\n", icmpptr->icmp6_type);
break;
}/*end switch*/
if(general_tests)
{
/*General verifications*/
watch_eth_mismatch(message, packet, eptr, ipptr, icmpptr, hdr->len);
watch_eth_broadcast(message, eptr, ipptr);
watch_ip_broadcast(message, eptr, ipptr);
watch_bogon(message, eptr, ipptr);
watch_hop_limit(message, eptr, ipptr);
}
}
else
fprintf(stderr,"IP type:%d , is not an ICMPv6 packet\n", ipptr->ip6_nxt);
}
else
fprintf(stderr,"Ethernet type %x is not an IPv6 packet\n", ntohs(eptr->ether_type));
fprintf(stderr,"------------------\n\n");
if(difftime(current, last_save_time)> 2*60)
{
write_cache();
last_save_time = current;
#ifdef _COUNTERMEASURES_
cm_indicate_ndpmon_presence();
#endif
}
}/*end callback*/
void usage()
{
fprintf(stderr,"Usage: ndpmon [ -i interfacename ] [ -f config_file ] [-e config-dtd] [ -F filter ] [ -n number ] [ -L ] [-g neighbor_file] [-d neighbor_dtd] [-v] [-h]\nPlease refer to manpage for more details.\n");
exit(1);
}
/*To write cache before exiting*/
void handler(int n)
{
if(learning)
write_config();
write_cache();
fprintf(stderr,"\nInterrupted ;) \n");
/* cleanup */
pcap_close(descr);
syslog(LOG_NOTICE,"Program Stopped...");
closelog();
/* free data structures */
clean_routers(&routers);
clean_neighbors(&neighbors);
#ifdef _MACRESOLUTION_
clean_manufacturer(&manuf);
#endif
exit(0);
}
/*To display properly the network address and device's mask */
void interface_spec(char* interface, bpf_u_int32 netp, bpf_u_int32 maskp)
{
struct in_addr addr;
char *net; /* network address */
char *mask;/* network mask */
if(DEBUG)
{
fprintf(stderr,"Interface: %s\n", interface);
}
addr.s_addr = netp;
net = inet_ntoa(addr);
if(net == NULL)
{
fprintf(stderr,"Problem with net address");
exit(1);
}
if(DEBUG)
{
fprintf(stderr,"Net: %s\n",net);
}
addr.s_addr = maskp;
mask = inet_ntoa(addr);
if(mask == NULL)
{
fprintf(stderr,"Problem with mask");
exit(1);
}
if(DEBUG)
{
fprintf(stderr,"Mask: %s\n",mask);
fprintf(stderr,"\n");
}
}
int main(int argc,char **argv)
{
char *interface; /* name of the interface/device to use */
char *filter; /* filter to select the parckets to grab */
int nb_packet = 0;
int op = 0;
char errbuf[PCAP_ERRBUF_SIZE];
struct bpf_program fp;/* string which contains the filter expression */
bpf_u_int32 maskp; /* mask */
bpf_u_int32 netp; /* ip */
u_char* args = NULL; /* for the callback function */
FILE *dat = NULL; /* for the discovery stats */
interface = NULL;
filter = "icmp6";
nb_packet=0;/*all packets are captured until kill*/
memset(errbuf,0,PCAP_ERRBUF_SIZE);
fprintf(stderr,"----- Initialization -----\n");
while ((op = getopt(argc, argv, "i:vhF:n:f:e:g:d:L")) != -1) /* former EOF changed to -1*/
switch (op)
{
case 'i':
interface = optarg;
fprintf(stderr,"interface: %s \n", interface);
break;
case 'F':
filter = optarg;
fprintf(stderr,"filter expression: %s \n", filter);
break;
case 'n':
nb_packet = atoi(optarg);
fprintf(stderr,"nb packets: %d \n", nb_packet);
break;
case 'f':
strncpy(config_path,optarg, PATH_SIZE);
fprintf(stderr,"config file path: %s \n", config_path);
break;
case 'e':
strncpy(dtd_config_path, optarg, PATH_SIZE);
fprintf(stderr,"config dtd file path: %s \n", dtd_config_path);
break;
case 'g':
strncpy(cache_path, optarg, PATH_SIZE);
fprintf(stderr,"cache file path: %s \n", cache_path);
break;
case 'd':
strncpy(dtd_path, optarg, PATH_SIZE);
fprintf(stderr,"neighbor dtd file path: %s \n", dtd_path);
break;
case 'L':
learning = 1;
fprintf(stderr,"learning phase\n");
break;
case 'v':
DEBUG = 1;
fprintf(stderr,"Debug mode on\n");
break;
case 'h':
usage();
break;
default:
usage();
break;
}
signal(SIGINT,handler);
signal(SIGQUIT,handler);
signal(SIGTERM,handler);
/* if the device isn't specified */
if ( (interface == NULL) && ((interface = pcap_lookupdev(errbuf)) == NULL))
{
fprintf(stderr,"%s\n",errbuf); exit(1);
}
#ifdef _COUNTERMEASURES_
cm_init(interface);
#endif
/* pcap get information on the interface */
pcap_lookupnet(interface,&netp,&maskp,errbuf);
interface_spec(interface,netp,maskp);
/* open device for reading */
descr = pcap_open_live(interface,BUFSIZ,1,1000,errbuf);
if(descr == NULL)
{
fprintf(stderr,"pcap_open_live(): %s\n",errbuf); exit(1);
}
/* using the filter */
if(pcap_compile(descr,&fp,filter,0,netp) <0)
{
fprintf(stderr,"Error calling pcap_compile %s \n", pcap_geterr(descr));
exit(1);
}
if(pcap_setfilter(descr,&fp) == -1)
{
fprintf(stderr,"Error setting filter\n"); exit(1);
}
#ifdef _MACRESOLUTION_
read_manuf_file(_MANUF_PATH_,&manuf);
#endif
/* opening and parsing the configuration xml files*/
parse_config();
parse_cache();
set_alarm(!learning);
if(DEBUG)
{
fprintf(stderr,"Routers' List Parsed:\n");
print_routers(routers);
fprintf(stderr,"Neighbors' List Parsed:\n");
print_neighbors(neighbors);
}
/* Now we have parsed the cache file, we can init the history stat file */
/* Write in discovery_history.dat the number of neighbors in the cache for statistics */
if( (dat = fopen(discovery_history_path,"w")) != NULL)
{
fprintf(dat,"%d %d\n", (int)time(NULL), nb_neighbor(neighbors) );
fclose(dat);
}
fprintf(stderr,"------------------\n\n");
/* then we can capture the packets */
pcap_loop(descr,nb_packet,callback,args);
write_cache();
/* cleanup */
pcap_freecode(&fp);
pcap_close(descr);
fprintf(stdout,"Finished! \n");
/*printf ( "using libpcap: %s \n", pcap_lib_version());*/
return 0;
}/*end main*/
|