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 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027
|
#VERSION,2.1.5
# $Id$
###############################################################################
# Copyright (C) 2006 CIRT, Inc.
#
# 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; version 2
# of the License only.
#
# 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, write to
# Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
###############################################################################
# PURPOSE:
# Nikto core functionality
###############################################################################
sub change_variables {
my $line = $_[0]; # $line is the unfiltered variable
my @subtests; # @subtests is the returned array of expanded variables
my $cooked;
my $shname = $mark->{'hostname'} || $mark->{'ip'};
$line =~ s/\@IP/$mark->{'ip'}/g;
$line =~ s/\@HOSTNAME/$shname/g;
$line =~ s/JUNK\(([0-9]+)\)/LW2::utils_randstr($1)/e;
if ($line !~ "\@") {
push(@subtests, $line);
}
else {
foreach my $varname (keys %VARIABLES) {
if ($line =~ /$varname/) {
# We've found the variable; expand it
foreach my $value (split(/ /, $VARIABLES{$varname})) {
$cooked = $line;
$cooked =~ s/$varname/$value/g;
push(@subtests, change_variables($cooked));
}
}
}
}
return @subtests;
}
###############################################################################
sub is_404 {
my ($uri, $content, $rescode, $loc_header) = @_;
$ext = get_ext($uri);
if (($FoF{$ext}{'mode'} eq "STD") && ($rescode =~ /4[0-9][0-9]/)) {
return 1;
}
elsif ($FoF{$ext}{'mode'} eq "REDIR") {
if (get_base_host($loc_header) eq $FoF{$ext}{'location'}) {
return 1;
}
}
elsif (($FoF{$ext}{'type'} eq "BLANK") && ($content eq "")) {
return 1;
}
elsif ($FoF{$ext}{'type'} eq "HASH") {
my $content = rm_active_content($content, $uri);
if (LW2::md4($content) eq $FoF{$ext}{'match'}) {
return 1;
}
}
# Note: ERRCODES are checkined in nikto_tests.plugin as they
# take precedent over ALL other tests
foreach my $string (keys %{ $VARIABLES->{'ERRSTRINGS'} }) {
if ($content =~ /$string/i) {
return 1;
}
}
return 0;
}
###############################################################################
sub nprint {
my $line = shift;
my $mode = shift;
my ($mark) = @_;
chomp($line);
# scrub values
if ($OUTPUT{'scrub'}) {
# name
$line =~ s/$mark->{'hostname'}/example.com/ig unless $mark->{'hostname'} eq '';
# ip
$line =~ s/$mark->{'ip'}/0.0.0.0/ig unless $mark->{'ip'} eq '';
# vhost
$line =~ s/$CLI{'vhost'}/example.com/ig unless $CLI{'vhost'} eq '';
# and in case we got here from set_target
$line =~ s/$mark->{'ident'}/example.com/ig unless $mark->{'ident'} eq '';
}
# don't print debug & verbose to output file...
if ($mode ne '') {
if ($mode eq "d" && $OUTPUT{'debug'}) {
print "D:" . localtime() . " $line\n";
}
if ($mode eq "v" && $OUTPUT{'verbose'}) {
print "V:" . localtime() . " $line\n";
}
if ($mode eq "e" && $OUTPUT{'errors'}) {
print "E:" . localtime() . " $line\n";
}
return;
}
# print errors to STDERR
if ($line =~ /^\t?\+ ERROR:/) { print STDERR "$line\n"; return; }
# don't print to STDOUT if output file is "-"
if ((defined $CLI{'file'}) && ($CLI{'file'} eq "-")) { return; }
$line =~ s/(CVE\-[12][0-9]{4}-[0-9]{4})/http:\/\/cve.mitre.org\/cgi-bin\/cvename.cgi?name\=$1/g;
$line =~ s/(CA\-[12][0-9]{3}-[0-9]{2})/http:\/\/www.cert.org\/advisories\/$1.html/g;
$line =~ s/BID\-([0-9]{4})/http:\/\/www.securityfocus.com\/bid\/$1/g;
$line =~
s/(MS[0-9]{2}\-[0-9]{3})/http:\/\/www.microsoft.com\/technet\/security\/bulletin\/$1.asp/gi;
print $line . "\n";
return;
}
###############################################################################
sub get_ext {
my $uri = $_[0] || return;
if ($uri =~ /\/$/) { return "DIRECTORY"; }
$uri =~ s/^.*\///;
if ($uri =~ /^\.[^.%]/) { return "DOTFILE"; }
$uri =~ s/[?&%].*$//;
if ($uri !~ /\./) { return "NONE"; }
$uri =~ s/\@[A-Z]+//; # remove tokens
$uri =~ s/\".*$//;
$uri =~ s/^.*\.//;
return $uri;
}
###############################################################################
sub status_report {
# without this we could face a div by 0 error
if ($COUNTERS{'totalrequests'} eq 0) {
nprint("- STATUS: Starting up!");
return;
}
my $secleft =
((time() - $COUNTERS{'scan_start'}) / $COUNTERS{'totalrequests'}) *
(($COUNTERS{'total_checks'} * $COUNTERS{'total_targets'}) - $COUNTERS{'totalrequests'});
my $timeleft;
if ($secleft > 60) {
my $minleft = $secleft / 60;
$timeleft = sprintf("%.1f minutes", $minleft);
if ($minleft > 60) {
my $hrsleft = $minleft / 60;
$timeleft = sprintf("%.1f hours", $hrsleft);
}
}
else { $timeleft = sprintf("%.0f seconds", $secleft); }
my $perc_compl =
($COUNTERS{'totalrequests'} / ($COUNTERS{'total_checks'} * $COUNTERS{'total_targets'}) * 100);
my $line;
# This 'if' is because I am a lazy, bad programmer.
# And also because total_checks only takes into account db_tests, not other stuff. I swear.
if (($perc_compl < 100) && ($secleft > 0)) {
$line = "- STATUS: Completed $COUNTERS{'totalrequests'} tests";
if ($COUNTERS{'total_targets'} > 1) {
$line .=
" (target " . ($COUNTERS{'hosts_completed'} + 1) . "/$COUNTERS{'total_targets'})";
}
$line .= sprintf(" (~%.0f%% complete, $timeleft left", $perc_compl);
if ($NIKTO{'current_plugin'} ne '') {
$line .= ": currently in plugin '$NIKTO{'current_plugin'}'";
}
$line .= ")";
}
else { $line = "- STATUS: Finishing up!"; }
nprint($line);
return;
}
###############################################################################
sub date_disp {
my @time = localtime($_[0]);
my $result = sprintf("%d-%02d-%02d %02d:%02d:%02d",
$time[5] + 1900,
$time[4] + 1,
$time[3], $time[2], $time[1], $time[0]);
return $result;
}
###############################################################################
sub get_base_host {
my $uri = $_[0] || return;
# uri, protocol, host, port, params, frag, user, password.
my @hd = LW2::uri_split($uri);
my $base = $hd[1] . "://" . $hd[2];
if (($hd[3] != 80) && ($hd[3] != 443)) { $base .= ":" . $hd[3]; }
$base .= "/";
return $base;
}
###############################################################################
sub map_codes {
my ($mark) = @_;
my %REQS;
my $rs = LW2::utils_randstr(8);
# / for OK response
my ($res, $content, $error, $request, $response, $headers_recv) =
nfetch($mark, "/", "GET", "", "", "", "map_codes");
if ($headers_recv->{'location'} ne '') {
nprint("+ Root page / redirects to: $headers_recv->{'location'}");
if ($headers_recv->{'location'} =~ /^$mark->{'hostname'}/i) # same host
{
my $uri = $headers_recv->{'location'};
($res, $content, $error, $request, $response) =
nfetch($mark, "/", "GET", "", "", "", "map_codes");
}
else # different host... ugh... just guess
{
$FoF{'okay'}{'response'} = 200;
$FoF{'okay'}{'type'} = "STD";
}
}
else {
$FoF{'okay'}{'response'} = $res;
my $cooked = rm_active_content($content);
$FoF{'okay'}{'type'} = "HASH";
$FoF{'okay'}{'match'} = LW2::md4($cooked);
}
# these are some used in mutate that may not be in the db_tests
if (defined $CLI{'mutate'}) {
$db_extensions{'bak'} = 1;
$db_extensions{'data'} = 1;
$db_extensions{'dbc'} = 1;
$db_extensions{'dbf'} = 1;
$db_extensions{'lst'} = 1;
$db_extensions{'htx'} = 1;
}
foreach my $ext (keys %db_extensions) {
if ( $ext ne "DIRECTORY"
&& $ext ne "NONE"
&& $ext ne "DOTFILE") {
$REQS{"/$rs.$ext"} = $ext;
}
}
undef $db_extensions;
# add those generic type holders back as real files
$REQS{"/$rs/"} = "DIRECTORY";
$REQS{"/$rs"} = "NONE";
$REQS{"/.$rs"} = "DOTFILE";
foreach my $file (keys %REQS) {
return if $mark->{'terminate'};
nprint("- Testing error for file: " . $mark->{'root'} . $file . "\n", "v");
%headers_recv = ();
($res, $content, $error, $request, $response, $headers_recv) =
nfetch($mark, $file, "GET", "", "", "", "map_codes");
$ext = $REQS{$file};
$FoF{$ext}{'response'} = $res;
# handle .com to .org redirs or whatnot
if ($headers_recv->{'location'} ne '') {
$FoF{$ext}{'location'} = get_base_host($headers_recv->{'location'});
}
# if it is not specific type, figure out Content or HASH method...
if ($FoF{$ext}{'response'} eq 404) { $FoF{$ext}{'mode'} = "STD"; next; }
elsif ($FoF{$ext}{'response'} eq 200) { $FoF{$ext}{'mode'} = "OK"; }
elsif ($FoF{$ext}{'response'} eq 410) { $FoF{$ext}{'mode'} = "STD"; next; }
elsif ($FoF{$ext}{'response'} eq 401) { $FoF{$ext}{'mode'} = "STD"; next; }
elsif ($FoF{$ext}{'response'} eq 403) { $FoF{$ext}{'mode'} = "STD"; next; }
elsif ($FoF{$ext}{'response'} eq 300) { $FoF{$ext}{'mode'} = "REDIR"; next; }
elsif ($FoF{$ext}{'response'} eq 301) { $FoF{$ext}{'mode'} = "REDIR"; next; }
elsif ($FoF{$ext}{'response'} eq 302) { $FoF{$ext}{'mode'} = "REDIR"; next; }
elsif ($FoF{$ext}{'response'} eq 303) { $FoF{$ext}{'mode'} = "REDIR"; next; }
elsif ($FoF{$ext}{'response'} eq 307) { $FoF{$ext}{'mode'} = "REDIR"; next; }
else { $FoF{$ext}{'mode'} = "OTHER"; }
# if we've got an OK/OTHER response, look at content first
# blank content, or hash...
if (length($content) == 0) {
nprint("- OK/OTHER type settled on: BLANK\n", "v");
$FoF{$ext}{'type'} = "BLANK";
$FoF{$ext}{'match'} = "";
}
else {
nprint("- OK/OTHER type settled on: HASH\n", "v");
my $cooked = rm_active_content($content, $file);
$FoF{$ext}{'type'} = "HASH";
$FoF{$ext}{'match'} = LW2::md4($cooked);
}
}
# lastly, get a hash of index.php so we can cut down on some false positives...
($res, $content, $error, $request, $response) =
nfetch($mark, "/index.php?", "GET", "", "", "", "map_codes");
my $cooked = rm_active_content($content, "/index.php");
$FoF{'index.php'}{'match'} = LW2::md4($cooked);
$FoF{'index.php'}{'type'} = "HASH";
return;
}
###############################################################################
sub rm_active_content {
# Try to remove active content which could mess up the file's signature
my ($cont, $file) = @_;
# Dates/Times
$cont =~ s/[12][0-9]{3}[-.\/][1-3]?[0-9][-.\/][1-3]?[0-9]//g; # 2001-12-12
$cont =~ s/[1-3]?[0-9][-.\/][1-3]?[0-9][-.\/][12][0-9]{3}//g; # 12-12-2002
$cont =~ s/[0-9]{8,14}//g; # timestamp
$cont =~ s/[0-9]{6}//g; # timestamp
$cont =~ s/[0-9]{2}:[0-9]{2}(?::[0-9]{2})?//g; # 12:11:33
$cont =~
s/(?:mon|tue|wed|thu|fri|sat|sun),? [1-3]?[0-9] (?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)//ig;
$cont =~ s/[12][0-9]{3}\s?(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s?[1-3]?[0-9]//gi
; # 2009 jan 29
$cont =~
s/[1-3]?[0-9]\s?(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[, ]?(?:[12][0-9]{3})?//gi
; # 29 Jan 2009
$cont =~ s/[0-9\.]+ second//gi; # page load time
$cont =~ s/[0-9]+ queries//gi; # wordpress
# URI, if provided, plus encoded versions of it
# $_[1] has unescaped file name, and $file has escaped. use appropriate one!
if ($file ne '') {
$file = quotemeta($file);
$cont =~ s/$file//g;
# base 64
my $e = LW2::encode_base64($_[1]);
$cont =~ s/$e//gs;
# hex encoded
$e = LW2::encode_uri_hex($_[1]);
$cont =~ s/$e//gs;
# unicode encoded
$e = LW2::encode_unicode($_[1]);
$cont =~ s/$e//gs;
# url encoding, full url
$e = $_[1];
$e =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
$cont =~ s/$e//gs;
# url encoding, query portion
if ($file =~ /\?(.*$)/) {
my $qs = $1;
# match pages which link to themselves w/diff args
$cont =~ s/$qs//gs;
# url encoded
$qs =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
$cont =~ s/$qs//gs;
}
}
return $cont;
}
###############################################################################
sub dump_target_info {
my ($mark) = @_;
my $sslprint = "";
if ($mark->{ssl}) {
$sslprint = "$VARIABLES{'DIV'}\n";
$sslprint .=
"+ SSL Info: Subject: $mark->{'ssl_cert_subject'}\n"
. " Ciphers: $mark->{'ssl_cipher'}\n"
. " Issuer: $mark->{'ssl_cert_issuer'}";
}
if ($CLI{'plugins'} ne '@@NONE') {
if ($mark->{ip} =~ /[a-z]/i) {
nprint("+ Target IP: (proxied)", "", $mark);
}
else {
nprint("+ Target IP: $mark->{ip}", "", $mark);
}
nprint("+ Target Hostname: $mark->{hostname}", "", $mark);
nprint("+ Target Port: $mark->{port}");
if (defined $CLI{'root'}) {
nprint("+ Target Path: $CLI{'root'}");
}
if ((defined $CLI{'vhost'}) && ($CLI{'vhost'} ne $mark->{hostname})) {
nprint("+ Virtual Host: $CLI{'vhost'}", "", $mark);
}
if ($request{'whisker'}->{'proxy_host'} ne '') {
nprint(
"+ Proxy: $request{'whisker'}->{'proxy_host'}:$request{'whisker'}->{'proxy_port'}"
);
}
if ($mark->{ssl}) {
nprint($sslprint);
}
if (defined $NIKTO{'anti_ids'} && defined $CLI{'evasion'}) {
for (my $i = 1 ; $i <= (keys %{ $NIKTO{'anti_ids'} }) ; $i++) {
if ($CLI{'evasion'} =~ /$i/) {
nprint("+ Using Encoding: $NIKTO{'anti_ids'}{$i}");
}
}
}
if (defined $NIKTO{'mutate_opts'} && defined $CLI{'mutate'}) {
for (my $i = 1 ; $i <= (keys %{ $NIKTO{'mutate_opts'} }) ; $i++) {
if ($CLI{'mutate'} =~ /$i/) {
nprint("+ Using Mutation: $NIKTO{'mutate_opts'}{$i}");
}
}
}
my $time = date_disp($mark->{start_time});
nprint("+ Start Time: $time (GMT$VARIABLES{'GMTOFFSET'})");
nprint($VARIABLES{'DIV'});
}
if ($mark->{banner} ne "") {
nprint("+ Server: $mark->{banner}");
}
else {
nprint("+ Server: No banner retrieved");
}
return;
}
###############################################################################
sub general_config {
## gotta set these first
$| = 1;
# This is used in dump_target_info(), not just help output
$NIKTO{'anti_ids'}{'1'} = "Random URI encoding (non-UTF8)";
$NIKTO{'anti_ids'}{'2'} = "Directory self-reference (/./)";
$NIKTO{'anti_ids'}{'3'} = "Premature URL ending";
$NIKTO{'anti_ids'}{'4'} = "Prepend long random string";
$NIKTO{'anti_ids'}{'5'} = "Fake parameter";
$NIKTO{'anti_ids'}{'6'} = "TAB as request spacer";
$NIKTO{'anti_ids'}{'7'} = "Change the case of the URL";
$NIKTO{'anti_ids'}{'8'} = "Use Windows directory separator (\\)";
$NIKTO{'anti_ids'}{'A'} = "Use a carriage return (0x0d) as a request spacer";
$NIKTO{'anti_ids'}{'B'} = "Use binary value 0x0b as a request spacer";
# This is used in dump_target_info(), not just help output
$NIKTO{'mutate_opts'}{'1'} = "Test all files with all root directories";
$NIKTO{'mutate_opts'}{'2'} = "Guess for password file names";
$NIKTO{'mutate_opts'}{'3'} = "Enumerate user names via Apache (/~user type requests)";
$NIKTO{'mutate_opts'}{'4'} = "Enumerate user names via cgiwrap (/cgi-bin/cgiwrap/~user type requests)";
$NIKTO{'mutate_opts'}{'5'} = "Attempt to brute force sub-domain names, assume that the host name is the parent domain";
$NIKTO{'mutate_opts'}{'6'} = "Attempt to guess directory names from the supplied dictionary file";
### CLI STUFF
$CLI{'pause'} = $CLI{'html'} = $OUTPUT{'verbose'} = $CLI{'skiplookup'} =
$COUNTERS{'totalrequests'} = $OUTPUT{'debug'} = $OUTPUT{'scrub'} = $OUTPUT{'errors'} = 0;
$CLI{'all_options'} = join(" ", @ARGV);
GetOptions("ask=s" => \$CLI{'ask'},
"Cgidirs=s" => \$CLI{'forcecgi'},
"config=s" => \$CLI{'config'},
"dbcheck" => \&check_dbs,
"Display=s" => \$CLI{'display'},
"evasion=s" => \$CLI{'evasion'},
"findonly" => \$CLI{'findonly'},
"Format=s" => \$CLI{'format'},
"Help" => \&usage,
"host=s" => \$CLI{'host'},
"id=s" => \$CLI{'hostauth'},
"key=s" => \$CLI{'key'},
"IgnoreCode=s" => \$CLI{'ignorecode'},
"list-plugins" => \&list_plugins,
"maxtime=s" => \$CLI{'maxtime'},
"mutate-options=s" => \$CLI{'mutate-options'},
"mutate=s" => \$CLI{'mutate'},
"no404" => \$CLI{'nofof'},
"nointeractive" => \$CLI{'nointeractive'},
"nolookup" => \$CLI{'skiplookup'},
"nossl" => \$CLI{'nossl'},
"output=s" => \$CLI{'file'},
"Pause=f" => \$CLI{'pause'},
"Plugins=s" => \$CLI{'plugins'},
"RSAcert=s" => \$CLI{'cert'},
"port=s" => \$CLI{'ports'},
"root=s" => \$CLI{'root'},
"ssl" => \$CLI{'ssl'},
"Save=s" => \$CLI{'saveresults'},
"timeout=i" => \$CLI{'timeout'},
"Tuning=s" => \$CLI{'tuning'},
"update" => \&check_updates,
"Userdbs:s" => \$CLI{'userdbs'},
"useproxy:s" => \$CLI{'useproxy'},
"until:s" => \$CLI{'until'},
"Version" => \&version,
"vhost=s" => \$CLI{'vhost'},
) or usage();
# Set up IgnoreCode
if ($CLI{'ignorecode'} ne '') {
foreach my $code (split(/\s?,\s?/, $CLI{'ignorecode'})) {
if ($code =~ /[^\d]/) { next; }
$VARIABLES->{'ERRCODES'}->{$code} = 1;
}
}
# Maxtime must be seconds
if ($CLI{'maxtime'} ne '') {
$CLI{'maxtime'} = time_to_seconds($CLI{'maxtime'});
if ($CLI{'maxtime'} eq '') {
nprint("+ ERROR: Invalid maxtime value, must be a valid time (e.g., 3600s, 60m, 1h)");
exit;
}
}
# Until
if ($CLI{'until'} ne '') {
# if a number of hours/mins/secs, convert to a time to pause
my $pausetime;
my $now = time();
if ($CLI{'until'} =~ /[hms]$/i) {
my $gotime = time_to_seconds($CLI{'until'});
if ($gotime eq '') {
nprint(
"+ ERROR: Invalid until value, must be a valid time (e.g., 3600s, 60m, 1h) or a time (e.g., 03:00)"
);
exit;
}
# convert to a pausetime
$pausetime = $now + $gotime;
}
elsif ($CLI{'until'} =~ /\d\d:\d\d:?(?:\d\d)?$/) {
# convert until time to epoch time
# format: (mm dd)? hh:mm:ss
$CLI{'until'} =~ /^(?:(\d\d)(?:\s|\/)(\d\d))?(?:\s|\/)?(\d?\d):(\d\d):?(\d\d)?$/;
my ($month, $day, $hh, $mm, $ss) = ($1, $2, $3, $4, $5);
$ss = '00' if $ss eq '';
# get year and complete month/day info
(undef, undef, undef, $cday, $cmon, $year) = localtime(time);
$year += 1900;
$day = $cday if $day eq '';
if ($month eq '') {
$month = $cmon;
}
else {
$month--;
}
$pausetime = timelocal($ss, $mm, $hh, $day, $month, $year);
}
if ($now > $pausetime) {
nprint("+ ERROR: Pause time is in the past.");
exit;
}
$CLI{'until'} = $pausetime;
}
# deprecated -findonly overrides -Plugins
if ($CLI{'findonly'}) {
$CLI{'plugins'} =
'@@NONE;report_csv;report_html;report_text;report_xml;report_nbe;report_msf';
}
# Userdb type: blank is db_tests only, so 'all' is only valid option
if (defined($CLI{'userdbs'})) {
if ($CLI{'userdbs'} =~ /^all$/i) { $CLI{'userdbs'} = 'all'; }
else { $CLI{'userdbs'} = 'tests'; }
}
# CLI proxy overrides nikto.conf
if ((defined($CLI{'useproxy'})) && ($CLI{'useproxy'} ne '')) {
my @prox = LW2::uri_split($CLI{'useproxy'});
$CONFIGFILE{'PROXYHOST'} = $prox[2];
$CONFIGFILE{'PROXYPORT'} = $prox[3];
$CONFIGFILE{'PROXYUSER'} = $prox[6];
$CONFIGFILE{'PROXYPASS'} = $prox[7];
}
elsif (defined($CLI{'useproxy'})) { $CLI{'useproxy'} = 1; }
# Save Results
if (defined($CLI{'saveresults'})) {
if ($CLI{'saveresults'} eq '') {
nprint("+ ERROR: -Save must have a directory name or '.' for auto-generated");
exit;
}
}
# port(s)
if (defined $CLI{'ports'}) {
$CLI{'ports'} =~ s/^\s+//;
$CLI{'ports'} =~ s/\s+$//;
if ($CLI{'ports'} =~ /[^0-9\-\, ]/) {
nprint("+ ERROR: Invalid port option '$CLI{'ports'}'");
exit;
}
}
# auto-generate file name
if (($CLI{'file'} eq '.') && ($CLI{'format'} eq '')) {
nprint("+ ERROR: Output format must be used with auto file naming");
exit;
}
if ($CLI{'file'} eq '.') {
my $hn = $CLI{'host'};
$hn =~ s/[^a-zA-Z0-9\.\-\_]/_/g;
$hn =~ s/_+/_/g;
my $port = $CLI{'ports'};
$port =~ s/,/\-/g;
$port =~ s/[^a-zA-Z0-9\.\-\_]/_/g;
my $now = date_disp(time());
$now =~ s/[^0-9-]/-/g;
$CLI{'file'} = "nikto_" . $hn . "_" . $port . "_" . $now . "." . $CLI{'format'};
$CLI{'file'} =~ s/_+/_/g;
# exists?
if (-e $CLI{'file'}) {
$CLI{'file'} =~ /^(.*)(\.[a-z]{3})/;
my $fn = $1;
my $ext = $2;
my $ctr = 0;
my $exists = 1;
while ($exists) {
$ctr++;
if (!-e $fn . "_" . $ctr . $ext) {
$CLI{'file'} = $fn . "_" . $ctr . $ext;
$exists = 0;
}
}
}
nprint("- Auto-generated save file: $CLI{'file'}", "v");
}
# output file
if (!defined $CLI{'format'}) {
# Check what output has
$CLI{'format'} = "none";
if (defined $CLI{'file'}) {
$CLI{'format'} = lc($CLI{'file'});
$CLI{'format'} =~ s/(^.*\.)([^.]*$)/$2/g;
}
}
if ($CLI{'format'} =~ /te?xt/i) { $CLI{'format'} = "txt"; }
elsif ($CLI{'format'} =~ /html?/i) { $CLI{'format'} = "htm"; }
elsif ($CLI{'format'} =~ /csv/i) { $CLI{'format'} = "csv"; }
elsif ($CLI{'format'} =~ /nbe/i) { $CLI{'format'} = "nbe"; }
elsif ($CLI{'format'} =~ /xml/i) { $CLI{'format'} = "xml"; }
elsif ($CLI{'format'} =~ /msf/i) { $CLI{'format'} = "msf"; }
elsif ($CLI{'format'} eq 'none') { }
else { nprint("+ ERROR: Invalid output format"); exit; }
if ((defined $CLI{'file'}) && ($CLI{'format'} eq "")) {
nprint("+ERROR: Output file specified without a format");
exit;
}
# verify readable dtd
if ($CLI{'format'} eq 'xml' && !-r $CONFIGFILE{'NIKTODTD'}) {
nprint("+ ERROR: reading DTD");
exit;
}
# screen output
if (defined $CLI{'display'}) {
if ($CLI{'display'} =~ /d/i) { $OUTPUT{'debug'} = 1; }
if ($CLI{'display'} =~ /v/i) { $OUTPUT{'verbose'} = 1; }
if ($CLI{'display'} =~ /s/i) { $OUTPUT{'scrub'} = 1; }
if ($CLI{'display'} =~ /e/i) { $OUTPUT{'errors'} = 1; }
if ($CLI{'display'} =~ /p/i) { $OUTPUT{'progress'} = 1; }
if ($CLI{'display'} =~ /1/i) { $OUTPUT{'show_redirects'} = 1; }
if ($CLI{'display'} =~ /2/i) { $OUTPUT{'show_cookies'} = 1; }
if ($CLI{'display'} =~ /3/i) { $OUTPUT{'show_ok'} = 1; }
if ($CLI{'display'} =~ /4/i) { $OUTPUT{'show_auth'} = 1; }
}
# Fixup
if (defined $CLI{'root'}) {
$CLI{'root'} =~ s/\/$//;
if (($CLI{'root'} !~ /^\//) && ($CLI{'root'} ne "")) { $CLI{'root'} = "/$CLI{'root'}"; }
}
if (defined $CLI{'hostauth'}) {
my @x = split(/:/, $CLI{'hostauth'});
if (($#x > 2) || ($x[0] eq "")) {
nprint(
"+ ERROR: \'$CLI{'hostauth'}\' (-i option) syntax is 'user:password' or 'user:password:domain' for host authentication."
);
exit;
}
}
if (defined $CLI{'evasion'}) {
$CLI{'evasion'} =~ s/[^1-8AB]//g;
}
else {
undef $NIKTO{'anti_ids'}; # we don't need this any more
}
if (!defined $CLI{'plugins'} || $CLI{'plugins'} eq "") {
$CLI{'plugins'} = '@@DEFAULT';
}
# Mapping for mutate for plugins
if (defined $CLI{'mutate'}) {
if ($CLI{'mutate'} =~ /1/ || $CLI{'mutate'} =~ /2/) {
my $parameters;
$parameters = "passfiles" if ($CLI{'mutate'} =~ /2/);
$parameters .= ",all" if ($CLI{'mutate'} =~ /1/);
$CLI{'plugins'} .= ';tests(' . $parameters . ')';
}
if ($CLI{'mutate'} =~ /3/ || $CLI{'mutate'} =~ /4/) {
my $parameters;
$parameters = "enumerate";
$parameters .= ",home" if ($CLI{'mutate'} =~ /3/);
$parameters .= ",cgiwrap" if ($CLI{'mutate'} =~ /4/);
$parameters .= ",dictionary:" . $CLI{'mutate-options'}
if (defined $CLI{'mutate-options'});
$CLI{'plugins'} .= ';apacheusers(' . $parameters . ')';
}
if ($CLI{'mutate'} =~ /5/) {
$CLI{'plugins'} .= ";subdomain";
}
if ($CLI{'mutate'} =~ /6/) {
$CLI{'plugins'} .= ';dictionary(dictionary:' . $CLI{'mutate-options'} . ')';
}
nprint(
"- Mutate is deprecated, use -Plugins instead. The following option can be used in future: -Plugin $CLI{'plugins'}"
);
}
else {
undef $NIKTO{'mutate_opts'}; # we don't need this any more
}
# Asking questions?
if ($CLI{'ask'} =~ /^(?:auto|yes|no)$/) {
$CONFIGFILE{'UPDATES'} = $CLI{'ask'}; # override nikto.conf setting
undef($CLI{'ask'});
}
$CLI{'timeout'} = $CLI{'timeout'} || 10;
# Set up User-Agent
$VARIABLES{'useragent'} = $CONFIGFILE{'USERAGENT'};
$VARIABLES{'useragent'} =~ s/\@VERSION/$VARIABLES{'version'}/g;
my $ev = $CLI{'evasion'} || "None";
$VARIABLES{'useragent'} =~ s/\@EVASIONS/$ev/g;
# RFI URL -- push it to VARIABLES
if (defined $CONFIGFILE{'RFIURL'}) {
$VARIABLES{'@RFIURL'} = $CONFIGFILE{'RFIURL'};
}
else {
nprint("- ***** RFIURL is not defined in nikto.conf--no RFI tests will run *****");
}
# SSL Test
if (!LW2::ssl_is_available()) {
nprint("- ***** SSL support not available (see docs for SSL install) *****");
}
# get core version
open(FI, "<$CONFIGFILE{'PLUGINDIR'}/nikto_core.plugin");
my @F = <FI>;
close(FI);
my @VERS = grep(/^#VERSION/, @F);
$VARIABLES{'core_version'} = $VERS[0];
$VARIABLES{'core_version'} =~ s/\#VERSION,//;
chomp($VARIABLES{'core_version'});
$VARIABLES{'TEMPL_HCTR'} = 0;
# POSIX support for status?
$NIKTO{'POSIX'}{'support'} = 0;
if (!$CLI{'nointeractive'}) {
eval "use POSIX qw(:termios_h)";
if (!$@) {
$NIKTO{'SUPPORT'}{'POSIX'} = 1;
eval "use Time::HiRes qw(ualarm)";
if (!$@) {
$NIKTO{'POSIX'}{'support'} = 1;
$NIKTO{'POSIX'}{'fd_stdin'} = fileno(STDIN);
$NIKTO{'POSIX'}{'term'} = POSIX::Termios->new();
$NIKTO{'POSIX'}{'term'}->getattr($fd_stdin);
$NIKTO{'POSIX'}{'oterm'} = $NIKTO{'POSIX'}{'term'}->getlflag();
$NIKTO{'POSIX'}{'echo'} = ECHOE | ECHO | ECHOK | ICANON;
$NIKTO{'POSIX'}{'noecho'} = $oterm & ~$echo;
}
}
}
# see if we have Time::HiRes sleep support
if ($CLI{'pause'} > 0) {
my $notice = "";
eval "use Time::HiRes qw(sleep)";
if ($@ && ($CLI{'pause'} =~ /\./)) {
my $integer = int($CLI{'pause'});
$CLI{'pause'} =~ /\.(\d+)/;
if ($1 >= 5) { $integer++; }
$CLI{'pause'} = $integer;
if ($CLI{'pause'} <= 0) { $CLI{'pause'} = 1; } # don't sleep for 0!
nprint(
"-***** Pausing $CLI{'pause'} second(s) per request (rounded, as Time::HiRes is not available) *****"
);
}
else {
nprint("-***** Pausing $CLI{'pause'} second(s) per request *****");
}
}
# Default values
$COUNTERS{'totalrequests'} = 0;
$COUNTERS{'total_checks'} = 0;
$COUNTERS{'total_targets'} = 0;
return;
}
###############################################################################
sub time_to_seconds {
my $time = $_[0] || return;
if ($time =~ /m$/i) {
$time =~ s/m$//i;
$time = ($time * 60);
}
elsif ($time =~ /h$/i) {
$time =~ s/h$//i;
$time = ($time * 3600);
}
elsif ($time =~ /s$/i) {
$time =~ s/s$//i;
}
return $time;
}
###############################################################################
sub sleeper {
sleep($CLI{'pause'});
}
###############################################################################
sub reset_term {
if (!$NIKTO{'POSIX'}{'support'}) { return; }
$NIKTO{'POSIX'}{'term'}->setlflag($NIKTO{'POSIX'}{'oterm'});
$NIKTO{'POSIX'}{'support'} = 0;
}
###############################################################################
sub safe_quit {
my ($mark) = @_;
$mark->{'end_time'} = time();
$mark->{'elapsed'} = $mark->{'end_time'} - $mark->{'start_time'};
#if ($mark->{'start_time'} ne '') {
#report_host_end($mark);
#}
report_host_end($mark);
report_summary($mark);
report_close($mark);
reset_term();
exit(1);
}
###############################################################################
sub check_input {
my ($mark) = @_;
my $key = readkey();
if ($key eq '') { return; }
lc($key);
if ($key eq ' ') {
status_report();
}
elsif ($key eq 'v') {
if ($OUTPUT{'verbose'}) { $OUTPUT{'verbose'} = 0; }
else { $OUTPUT{'verbose'} = 1; }
}
elsif ($key eq 'd') {
if ($OUTPUT{'debug'}) { $OUTPUT{'debug'} = 0; }
else { $OUTPUT{'debug'} = 1; }
}
elsif ($key eq 'e') {
if ($OUTPUT{'errors'}) { $OUTPUT{'errors'} = 0; }
else { $OUTPUT{'errors'} = 1; }
}
elsif ($key eq 'p') {
if ($OUTPUT{'progress'}) { $OUTPUT{'progress'} = 0; }
else { $OUTPUT{'progress'} = 1; }
}
elsif ($key eq 'r') {
if ($OUTPUT{'show_redirects'}) { $OUTPUT{'show_redirects'} = 0; }
else { $OUTPUT{'show_redirects'} = 1; }
}
elsif ($key eq 'c') {
if ($OUTPUT{'show_cookies'}) { $OUTPUT{'show_cookies'} = 0; }
else { $OUTPUT{'show_cookies'} = 1; }
}
elsif ($key eq 'o') {
if ($OUTPUT{'show_ok'}) { $OUTPUT{'show_ok'} = 0; }
else { $OUTPUT{'show_ok'} = 1; }
}
elsif ($key eq 'a') {
if ($OUTPUT{'show_auth'}) { $OUTPUT{'show_auth'} = 0; }
else { $OUTPUT{'show_auth'} = 1; }
}
elsif (($key eq 'q') || (ord($key) eq 3)) {
safe_quit($mark);
}
elsif ($key eq 'P') {
pause();
}
elsif ($key eq 'N') {
nprint("- Terminating host scan.");
return 'term';
}
return;
}
###############################################################################
sub pause {
if ($_[0] eq 'u') {
nprint("- Pausing due to 'until' flag--press P to resume.");
}
else {
nprint("- Pausing--press P to resume.");
}
while (readkey() ne 'P') { $CLI{'until'} = ''; sleep 1; }
nprint("- Resuming.");
}
###############################################################################
sub readkey {
if (!$NIKTO{'POSIX'}{'support'}) { return; }
my $key;
$NIKTO{'POSIX'}{'term'}->setlflag($NIKTO{'POSIX'}{'noecho'});
$NIKTO{'POSIX'}{'term'}->setattr($NIKTO{'POSIX'}{'fd_stdin'}, TCSANOW);
eval {
local $SIG{ALRM} = sub { die; };
ualarm(1_000);
sysread(STDIN, $key, 1);
ualarm(0);
};
$NIKTO{'POSIX'}{'term'}->setlflag($NIKTO{'POSIX'}{'oterm'});
$NIKTO{'POSIX'}{'term'}->setattr($NIKTO{'POSIX'}{'fd_stdin'}, TCSANOW);
return $key;
}
###############################################################################
sub resolve {
my $ident = $_[0] || return;
my ($name, $ip, $dn) = "";
if (($request{'whisker'}->{'proxy_host'} ne '') && $CLI{'useproxy'}) {
$name = $ident;
$ip = $name;
return $name, $ip, $name;
}
# ident is name, lookup IP
if ($ident =~ /[^0-9\.]/) # not an IP, assume name
{
if ($CLI{'skiplookup'}) {
print("+ ERROR: -skiplookup set, but given name\n");
exit;
}
$ip = gethostbyname($ident);
# can't resolve name to IP
if ($ip eq "") {
nprint("+ ERROR: Cannot resolve hostname '$ident'\n");
return;
}
else {
use IO::Socket;
$ip = inet_ntoa($ip);
if ($ip !~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) {
nprint("+ ERROR: Invalid IP '$ip'\n\n");
return;
}
$name = $ident;
}
}
else # ident is IP, lookup name
{
if ($ident !~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) {
nprint("+ ERROR: Invalid IP '$ident'\n\n");
return;
}
$ip = $ident;
if (!$CLI{'skiplookup'}) {
use IO::Socket;
my $temp_ip = inet_aton($ip);
$name = gethostbyaddr($temp_ip, AF_INET);
# check reverse dns to avoid an inet_aton error
my $rdnsip = gethostbyname($name);
if ($rdnsip ne "") {
$rdnsip = inet_ntoa($rdnsip);
if ($ip ne $rdnsip) { $name = $ip; } # Reverse DNS does not match
}
else { $name = $ip; } # Reverse DNS does not exist
}
if ($name eq "") { $name = $ip; }
}
# set displayname -- name takes precedence
if ($name ne "") { $dn = $name; }
else { $dn = $ip; }
return $name, $ip, $dn;
}
###############################################################################
sub set_targets {
my ($hostlist, $portlist, $ssl, $root) = @_;
my $host_ctr = 1;
my @hosts = split(/,/, $hostlist);
my @tempports = split(/,/, $portlist) if defined $portlist;
my (@ports, @checkhosts, @results, @marks);
my $defaultport = ($ssl) ? 443 : 80;
nprint("- Getting targets", "v");
# Check for portlist and expand
foreach my $port (@tempports) {
if ($port =~ /-/) {
my ($start, $end);
my @temp = split(/-/, $port);
$start = $temp[0];
$end = $temp[1];
if ($start eq "") { $start = 0; }
if ($end eq "") { $end = 65535; }
if ($start > $end) {
nprint("+ ERROR port range $port doesn't make sense - assuming 80/tcp");
next;
}
for (my $i = $start ; $i <= $end ; $i++) {
push(@ports, $i);
}
}
else {
push(@ports, $port);
}
}
# no ports explicitly set, so use default port
if (scalar(@ports) == 0) {
push(@ports, $defaultport);
}
# check whether -h is a file or an entry
foreach my $host (@hosts) {
if (-e $host || $host eq "-") {
@results = parse_hostfile($host);
push(@checkhosts, @results);
}
else {
push(@checkhosts, $host);
}
}
# Now parse the list of checkhosts, store in %targs by host:port
my $targs = {};
foreach my $host (@checkhosts) {
$host =~ s/\s+//g;
if (!defined $host) { next; }
my ($defhost, $defport) = '';
# is it a URL?
if ($host =~ /^https?:\/\//) {
my @hostdata = LW2::uri_split($host);
$defhost = $hostdata[2];
$defport = $hostdata[3];
$targs{ $defhost . ":" . $defport } = ($root ne "") ? $root : '/';
if (($hostdata[0] ne '/') && ($hostdata[0] ne '') && ($root eq '')) {
$hostdata[0] =~ s/\/$//;
$targs{ $defhost . ":" . $defport } = $hostdata[0];
nprint("- Added -root value of '$hostdata[0]' from URI", "v");
}
}
else {
my @h = split(/\:|\,/, $host);
$defhost = $h[0];
$defport = $h[1];
$targs{ $defhost . ":" . $defport } = ($root ne "") ? $root : '/';
}
}
foreach my $host (keys %targs) {
my ($h, $p) = split(/:/, $host);
if ($p eq '') {
foreach my $port (@ports) {
my $markhash = {};
if ($root ne '') {
$markhash->{'root'} = $root;
nprint("- Added -root value of '$root' from CLI", "v");
}
$markhash->{'ident'} = $h;
$markhash->{'port'} = $port;
if ($targs{$host} ne '/') { $markhash->{'root'} = $targs{$host}; }
nprint("- Target:$markhash->{'ident'} port:$markhash->{'port'}", "v", $markhash);
push(@marks, $markhash);
}
}
else {
my $markhash = {};
if ($targs{$host} ne '/') { $markhash->{'root'} = $targs{$host}; }
$markhash->{'ident'} = $h;
$markhash->{'port'} = $p;
push(@marks, $markhash);
}
}
return @marks;
}
###############################################################################
sub load_databases {
my @dbs = qw/db_parked_strings db_404_strings db_outdated db_variables/;
my $prefix = $_[0] || '';
# Only load the right databases if -Userdbs is set
if ((defined($CLI{'userdbs'})) && ($CLI{'userdbs'} eq 'all')) {
if ($prefix eq '') { return; }
else { push(@dbs, 'db_tests'); }
}
if (($prefix eq 'u') || (!defined($CLI{'userdbs'}))) { push(@dbs, 'db_tests'); }
# verify required files
for my $file (@dbs) {
if (!-r "$CONFIGFILE{'DBDIR'}/$file") {
die nprint("+ ERROR: Can't find/read required file \"$CONFIGFILE{'DBDIR'}/$file\"");
}
}
for my $file (@dbs) {
my $filename = $CONFIGFILE{DBDIR} . "/" . $prefix . $file;
if (!-r $filename) { next; }
open(IN, "<$filename") || die nprint("+ ERROR: Can't open \"$filename\":$!\n");
# db_tests
if ($file eq 'db_tests') { push(@DBFILE, <IN>); next; }
# all the other files require per-line processing
else {
my @file;
# Cleanup
while (<IN>) {
chomp;
$_ =~ s/#.*$//;
$_ =~ s/\s+$//;
$_ =~ s/^\s+//;
if ($_ ne "") { push(@file, $_); }
}
# db_variables
if ($file eq 'db_variables') {
foreach my $l (@file) {
if ($l =~ /^@/) {
next if $l eq '';
my @temp = split(/=/, $l);
$VARIABLES{ $temp[0] } .= "$temp[1]";
}
}
}
# db_parked_strings
elsif ($file eq 'db_parked_strings') {
foreach my $l (@file) {
$l = validate_and_fix_regex($l);
$VARIABLES->{'PARKEDSTRINGS'}->{$l} = 1;
}
}
# db_404_strings
elsif ($file eq 'db_404_strings') {
foreach my $l (@file) {
if ($l =~ /^\@CODE=/) {
$l =~ s/^\@CODE=//;
$l = validate_and_fix_regex($l);
$VARIABLES->{'ERRCODES'}->{$l} = 1;
}
else {
$l = validate_and_fix_regex($l);
$VARIABLES->{'ERRSTRINGS'}->{$l} = 1;
}
}
}
# db_outdated
elsif ($file eq 'db_outdated') {
foreach my $l (@file) {
my @T = parse_csv($l);
next if $T[1] eq '';
$T[1] = validate_and_fix_regex($T[1]);
$OVERS{ $T[1] }{ $T[2] } = $T[3];
$OVERS{ $T[1] }{'tid'} = $T[0];
}
}
close(IN);
}
}
return;
}
###############################################################################
sub check_dbs {
my @dbs =
qw/db_headers db_httpoptions db_multiple_index db_server_msgs db_subdomains db_favicon db_embedded db_parked_strings db_404_strings db_outdated db_realms db_tests db_variables db_content_search/;
my $prefix = $_[0];
if (($prefix eq "") || ($prefix eq "dbcheck")) {
$prefix = ""; # for options from Get::Opt
nprint("\n-->\tNikto Databases");
}
elsif ($prefix eq "u") {
nprint("\n-->\tUser Databases");
}
for my $file (@dbs) {
my $filename = $CONFIGFILE{DBDIR} . "/" . $prefix . $file;
if (!-r $filename) {
nprint("+ ERROR: Unable to read \"$filename\"");
next;
}
open(IN, "<$filename") || die nprint("+ ERROR: Can't open \"$filename\":$!\n");
nprint("Syntax Check: $filename");
if ($file eq 'db_outdated') {
foreach $line (<IN>) {
$line =~ s/^\s+//;
if ($line =~ /^\#/) { next; }
chomp($line);
if ($line eq "") { next; }
my @L = parse_csv($line);
if ($#L ne 3) { nprint("\t+ ERROR: Invalid syntax ($#L): $line"); next; }
$ENTRIES{"$L[0]"}++;
}
foreach $entry (keys %ENTRIES) {
if ($ENTRIES{$entry} > 1) {
nprint("\t+ ERROR: Duplicate ($ENTRIES{$entry}): $entry");
}
}
nprint("\t" . keys(%ENTRIES) . " entries");
}
elsif ($file eq 'db_tests') {
my %ENTRIES;
foreach my $line (<IN>) {
if ($line !~ /^\"/) { next; }
my @L = parse_csv($line);
if ( ($L[4] !~ /(GET|POST|TRACE|TRACK|OPTIONS|SEARCH|INDEX)/i)
&& ($L[0] ne '006433')) {
nprint("\t+ ERROR: Possibly invalid method: $L[4] on ($line)");
}
if ($L[5] eq "") { nprint("\t+ ERROR: blank conditional: $line"); next; }
if ($line !~ /^\".*\",\".*\",\".*\",\".*\",\".*\"/) {
nprint("\t+ ERROR: Invalid syntax ($#L): $line");
next;
}
if ($line !~ /^(\".*\",){11}\".*\"/) {
nprint("\t+ ERROR: Invalid syntax ($#L): $line");
next;
}
if (($L[3] =~ /^\@CG/) && ($L[3] !~ /^\@CGIDIRS/)) {
nprint("\t+ ERROR: Possible \@CGIDIRS misspelling: $line");
}
if ($L[1] =~ /[^0-9]/) { nprint("\t+ ERROR: Invalid OSVDB ID: $line"); }
$ENTRIES{"$L[3],$L[4],$L[5],$L[6],$L[7],$L[8],$L[9],$L[11],$L[12]"}++;
if ((count_fields($line, 1) ne 12) && (count_fields($line) ne '')) {
nprint("\t+ ERROR: Invalid syntax: $line");
}
for (my $i = 5 ; $i <= 9 ; $i++) {
my ($result, $bad) = validate_and_fix_regex($L[$i], 1);
if ($bad) { nprint("\t+ ERROR: Invalid regex in field $i: \"$L[$i]\""); }
}
}
foreach $entry (keys %ENTRIES) {
if ($ENTRIES{$entry} > 1) {
nprint("\t+ ERROR: Duplicate ($ENTRIES{$entry}): $entry");
}
}
nprint("\t" . keys(%ENTRIES) . " entries");
}
elsif ($file eq 'db_variables') {
my $ctr = 0;
foreach $line (<IN>) {
if ($line !~ /^\@/) { next; }
if ($line !~ /^\@.+\=.+$/i) { nprint("\t+ ERROR: Invalid syntax: $line"); }
$ctr++;
}
nprint("\t$ctr entries");
}
elsif ($file eq 'db_404_strings') {
my $ctr = 1;
foreach $line (<IN>) {
chomp($line);
$line =~ s/\#.*$//;
next if $line eq '';
my ($result, $bad) = validate_and_fix_regex($line, 1);
if ($bad) { nprint("\t+ ERROR: Invalid regex on line $ctr: \"$line\""); }
$ctr++;
}
$ctr--;
nprint("\t$ctr entries");
}
elsif ($file eq 'db_parked_strings') {
my $ctr = 1;
foreach $line (<IN>) {
chomp($line);
$line =~ s/\#.*$//;
next if $line eq '';
my ($result, $bad) = validate_and_fix_regex($line, 1);
if ($bad) { nprint("\t+ ERROR: Invalid regex on line $ctr: \"$line\""); }
$ctr++;
}
$ctr--;
nprint("\t$ctr entries");
}
elsif ($file eq 'db_headers') {
my $ctr = 0;
foreach $line (<IN>) {
if ((count_fields($line) ne 0) && (count_fields($line) ne '')) {
nprint("\t+ ERROR: Invalid syntax: $line");
}
$ctr++;
}
nprint("\t$ctr entries");
}
elsif ($file eq 'db_multiple_index') {
my $ctr = 0;
foreach $line (<IN>) {
if ((count_fields($line) ne 0) && (count_fields($line) ne '')) {
nprint("\t+ ERROR: Invalid syntax: $line");
}
$ctr++;
}
nprint("\t$ctr entries");
}
else {
# It's a file of standard DB type, we can do this intelligently
my (@headers, @regex_fields);
my $ctr = 0, $fields = 0;
foreach $line (<IN>) {
# first, grab the headers
if ($fields == 0) {
$line =~ s/\#.*//;
next if ($line eq "");
@headers = parse_csv($line);
$fields = $#headers;
# check regex fields for syntax
for (my $i = 0 ; $i <= $#headers ; $i++) {
if ( ($headers[$i] eq 'match')
|| ($headers[$i] eq 'matchstring')
|| ($headers[$i] eq 'server')) {
push(@regex_fields, $i);
}
}
next;
}
if ($regex_fields[0] ne '') {
my @entry = parse_csv($line);
foreach my $f (@regex_fields) {
my ($result, $bad) = validate_and_fix_regex($entry[$f], 1);
if ($bad) {
nprint("\t+ ERROR: Invalid regex in field $f on line $ctr: \"$line\"");
}
}
}
if ( (count_fields($line, 1) != $fields - 1)
&& (count_fields($line) ne '')) {
nprint("\t+ ERROR: Invalid syntax: $line");
}
$ctr++;
}
nprint("\t$ctr entries");
}
close(IN);
}
if ($_[0] eq "") { check_dbs('u'); } # do this once
nprint("\n");
exit;
}
###############################################################################
sub count_fields {
my $line = $_[0] || return;
my $checkid = $_[1] || 0;
if ($line !~ /^\"/) { return; }
chomp($line);
$line =~ s/\s+$//;
if ($line eq '') { return; }
my @L = parse_csv($line);
if ($checkid && ($L[0] ne 'nikto_id') && (($L[0] =~ /[^0-9]/) || ($L[0] eq ''))) { return -1; }
return $#L;
}
###############################################################################
sub port_check {
my ($hostname, $ip, $port, $key, $cert) = @_;
my $headers_recv;
my $m = {};
# Check SKIPPORTS
if ($CONFIGFILE{'SKIPPORTS'} =~ /\b$port\b/) {
nprint("+ ERROR: SKIPPORTS (nikto.conf) contains $port -- not checking");
return 0;
}
$m->{hostname} = $hostname;
$m->{ip} = $ip;
$m->{port} = $port;
$m->{ssl} = 0;
my @checktypes = ('HTTP', 'HTTPS');
if ($CLI{'ssl'}) { shift(@checktypes); }
if ($CLI{'nossl'}) { pop(@checktypes); }
foreach my $method (split(/ /, $CONFIGFILE{'CHECKMETHODS'})) {
$request{'whisker'}->{'method'} = $method;
foreach my $checkssl (@checktypes) {
nprint("- Checking for $checkssl on port "
. ($hostname || $ip)
. ":$port, using $method",
"v",
$m
);
$m->{ssl} = ($checkssl eq "HTTP") ? 0 : 1;
if ($m->{'ssl'}) {
$m->{'key'} = $key;
$m->{'cert'} = $cert;
}
proxy_check($m);
my ($res, $content, $error, $request, $response, $headers_recv) =
nfetch($m, "/", $method, "", "", { noerror => 1, noprefetch => 1, nopostfetch => 1 },
"Port Check");
if ($res) {
# this will fix for some Apaches that are smart enough to answer non ssl reqs on an ssl server
if (defined $content
&& $content =~ /speaking plain HTTP to an SSL/) {
dump_var("Result Hash", \%result);
next;
}
nprint("- $checkssl Server found: "
. ($hostname || $ip)
. ":$port \t$headers_recv->{server}",
"d",
$m
);
return $m->{ssl} + 1;
}
}
}
nprint("+ No web server found on " . ($hostname || $ip) . ":$port");
nprint("---------------------------------------------------------------------------");
return 0;
}
###############################################################################
# Directory listing
sub dirlist {
my $DIR = $_[0] || return;
my $PATTERN = $_[1] || "";
my @FILES_TMP = ();
opendir(DIRECTORY, $DIR) || die print STDERR "+ ERROR: Can't open directory '$DIR': $@";
foreach my $file (readdir(DIRECTORY)) {
if ($file =~ /^\./) { next; } # skip hidden files, '.' and '..'
if ($PATTERN ne "") {
if ($file =~ /$PATTERN/) { push(@FILES_TMP, $file); }
}
else { push(@FILES_TMP, $file); }
}
closedir(DIRECTORY);
return @FILES_TMP;
}
###############################################################################
sub load_plugins {
my @pluginlist = dirlist("$CONFIGFILE{'PLUGINDIR'}", '\.plugin$');
my @all_names;
# populate plugin macros
$CONFIGFILE{'@@NONE'} = "";
# Check if running plugins is NONE - if so, don't bother initalising plugins
if ($CLI{'plugins'} eq '@@NONE') {
return;
}
foreach my $plugin (@pluginlist) {
my $plugin_name = $plugin;
$plugin_name =~ s/\.plugin$//;
my $plugin_init = $plugin_name . "_init";
eval { require "$CONFIGFILE{'PLUGINDIR'}/$plugin"; };
if ($@) {
nprint("- Could not load or parse plugin: $plugin_name\n Error: ");
warn $@;
nprint("- The plugin could not be run.");
}
else {
nprint("- Initialising plugin $plugin_name", "v");
# Call initialisation method
if (defined &$plugin_init) {
my $pluginhash = &$plugin_init;
# Add default weights if not already assigned
while (my ($hook, $hook_params) = each(%{ $pluginhash->{'hooks'} })) {
$hook_params->{$hook}->{'weight'} = 50
unless (defined $hook_params->{$hook}->{'weight'});
}
$pluginhash->{report_weight} = 50 unless (defined $pluginhash->{report_weight});
push(@all_names, $pluginhash->{name});
push(@PLUGINS, $pluginhash);
nprint("- Loaded \"$pluginhash->{full_name}\" plugin.", "v");
}
}
}
$CONFIGFILE{'@@ALL'} = join(';', @all_names);
my @torun = split(/;/, expand_pluginlist($CLI{'plugins'}, 0));
# Second pass to ensure that @@ALL is configured
foreach my $plugin (@PLUGINS) {
# Check that the plugin is to be run
# Perl doesn't allow us to use "in", pity
foreach my $torun_plugin (@torun) {
next if ($torun_plugin eq "");
# split up into parameters
my $name = my $suffix = $torun_plugin;
if ($torun_plugin =~ /\(/) {
$name =~ s/(.*)(\(.*\))/$1/;
$suffix =~ s/(.*)(\(.*\))/$2/;
}
else {
$name = $torun_plugin;
$suffix = "";
}
if ($plugin->{'name'} =~ /$name/i) {
$plugin->{'run'} = 1;
# Create parameters
if ($suffix ne "") {
my $parameters = {};
$suffix =~ s/(\()(.*[^\)])(\)?)/$2/;
foreach my $parameter (split(/,/, $suffix)) {
if ($parameter !~ /:/) {
$parameters->{$parameter} = 1;
}
else {
my $key = my $value = $parameter;
$key =~ s/:.*//;
$value =~ s/.*://;
$parameters->{$key} = $value;
}
}
$plugin->{'parameters'} = $parameters;
}
}
}
}
# For speed in future, create a hash of active plugins ordered by plugin weight, for
# each type of plugin
# first build a temporary hash of all known hooks
my %hooks;
foreach my $plugin (@PLUGINS) {
foreach my $hook (keys(%{ $plugin->{'hooks'} })) {
$hooks{$hook} = ();
}
}
# now we know the types of hooks, look through each plugin for them
foreach my $hook (keys(%hooks)) {
foreach my $plugin (@PLUGINS) {
if ($plugin->{'run'} == 1) {
if (defined $plugin->{'hooks'}->{$hook}->{'method'}) {
push(@{ $hooks{$hook} }, $plugin);
}
}
}
}
# Now sort each array by weight
foreach my $hook (keys(%hooks)) {
my @sorted =
sort { $a->{'hooks'}->{$hook}->{'weight'} <=> $b->{'hooks'}->{$hook}->{'weight'} }
@{ $hooks{$hook} };
$PLUGINORDER{$hook} = \@sorted;
}
}
###############################################################################
sub run_hooks {
my ($mark, $type, $request, $result) = @_;
return if $mark->{'terminate'};
foreach my $plugin (@{ $PLUGINORDER{$type} }) {
return if $mark->{'terminate'};
my ($run) = 1;
# first check for conditionals
my $condition = $plugin->{'hooks'}->{$type}->{'cond'};
if (defined $plugin->{'hooks'}->{$type}->{'cond'}) {
# Evaluate condition
$run = eval($condition);
}
if (!$run) { next; }
my $oldverbose = $OUTPUT{'verbose'};
my $olddebug = $OUTPUT{'debug'};
my $olderrors = $OUTPUT{'errors'};
nprint("- Running $type for \"$plugin->{'full_name'}\" plugin", "v")
unless ($type eq "prefetch" || $type eq "postfetch");
if (defined $plugin->{'parameters'}->{'verbose'}
&& $plugin->{'parameters'}->{'verbose'} == 1) {
$OUTPUT{'verbose'} = 1;
}
if (defined $plugin->{'parameters'}->{'debug'}
&& $plugin->{'parameters'}->{'debug'} == 1) {
$OUTPUT{'debug'} = 1;
}
unless ($type eq "prefetch" || $type eq "postfetch") {
$NIKTO{'current_plugin'} = $plugin->{'full_name'};
}
&{ $plugin->{'hooks'}->{$type}->{'method'} }($mark, $plugin->{'parameters'}, $request,
$result);
$OUTPUT{'verbose'} = $oldverbose;
$OUTPUT{'debug'} = $olddebug;
$OUTPUT{'errors'} = $olderrors;
}
return $request, $result;
}
###############################################################################
sub report_head {
my ($format, $file) = @_;
nprint("- Opening reports ($format, $file)", "v");
# For tuning set up a list of report methods, formats and handles
# This is a frig until I can think of a better way of achieving it
foreach my $i (1 .. 100) {
foreach my $plugin (@PLUGINS) {
if ($plugin->{run} && defined $plugin->{report_item} && $plugin->{report_weight} == $i)
{
my $run = 1;
# first check for conditionals
if (defined $plugin->{report_format}) {
# Evaluate condition
$run = ($format eq $plugin->{report_format});
}
if ($run) {
nprint("- Opening report for \"$plugin->{full_name}\" plugin", "v");
my $handle;
if (defined $plugin->{report_head}) {
$handle = &{ $plugin->{report_head} }($file);
}
# Now store this
my $report_entry = { host_start => $plugin->{report_host_start},
host_end => $plugin->{report_host_end},
item => $plugin->{report_item},
close => $plugin->{report_close},
summary => $plugin->{report_summary},
handle => $handle,
};
push(@REPORTS, $report_entry);
}
}
}
}
return;
}
###############################################################################
sub report_host_start {
my ($mark) = @_;
# Go through all reporting modules
foreach my $reporter (@REPORTS) {
if (defined $reporter->{host_start}) {
&{ $reporter->{host_start} }($reporter->{handle}, $mark);
}
}
}
###############################################################################
sub report_host_end {
my ($mark) = @_;
# Go through all reporting modules
foreach my $reporter (@REPORTS) {
if (defined $reporter->{host_end}) {
&{ $reporter->{host_end} }($reporter->{handle}, $mark);
}
}
}
###############################################################################
sub report_summary {
my ($mark) = @_;
# Go through all reporting modules
foreach my $reporter (@REPORTS) {
if (defined $reporter->{summary}) {
&{ $reporter->{summary} }($reporter->{handle}, $mark);
}
}
}
###############################################################################
sub report_item {
my ($mark, $item) = @_;
# Go through all reporting modules
foreach my $reporter (@REPORTS) {
if (defined $reporter->{item}) {
&{ $reporter->{item} }($reporter->{handle}, $mark, $item);
}
}
}
###############################################################################
sub report_close {
# Go through all reporting modules
foreach my $reporter (@REPORTS) {
if (defined $reporter->{close}) {
&{ $reporter->{close} }($reporter->{handle});
}
}
}
###############################################################################
sub check_updates {
LW2::http_init_request(\%request);
my (%REMOTE, %LOCAL, @DBTOGET) = ();
my ($pluginmsg, $remotemsg) = "";
my $code_updates = 0;
my $serverdir = "/nikto/UPDATES/$VARIABLES{'version'}";
my $server = "CIRT.net";
# set up our mark
my %mark = ('ident' => 'www.cirt.net',
'ssl' => 0,
'port' => 80
);
for (my $i = 0 ; $i <= $#ARGV ; $i++) {
if (($ARGV[$i] eq "-u") || ($ARGV[$i] eq "-useproxy")) {
$CLI{'useproxy'} = 1;
if (($CONFIGFILE{PROXYPORT} ne '') && ($CONFIGFILE{PROXYHOST} ne '')) {
$request{'whisker'}->{'proxy_host'} = $CONFIGFILE{PROXYHOST};
$request{'whisker'}->{'proxy_port'} = $CONFIGFILE{PROXYPORT};
}
proxy_check();
last;
}
}
($mark{'hostname'}, $mark{'ip'}, $mark{'display_name'}) = resolve('www.cirt.net');
# retrieve versions file
my ($code, $content) = nfetch(\%mark, "$serverdir/versions.txt", "GET");
if ($code eq 407) {
if ($CONFIGFILE{'PROXYUSER'} eq "") {
$CONFIGFILE{'PROXYUSER'} = read_data("Proxy ID: ", "");
$CONFIGFILE{'PROXYPASS'} = read_data("Proxy Pass: ", "noecho");
}
# and try again
($code, $content) = nfetch(\%mark, "$serverdir/versions.txt", "GET");
}
if ($code eq "") {
($code, $content) = nfetch(\%mark, "$serverdir/versions.txt", "GET");
}
if ($code ne 200) {
nprint("+ ERROR ($code): Unable to get $mark{'hostname'}$serverdir/versions.txt");
exit;
}
# make hash
for (split(/\n/, $content)) {
my @l = parse_csv($_);
if ($_ =~ /^msg/) {
$remotemsg = "$l[1]";
next;
}
$REMOTE{ $l[0] } = $l[1];
}
# get local versions of plugins/dbs
my %NIKTOFILES;
my @F = dirlist($CONFIGFILE{'PLUGINDIR'}, "");
foreach my $f (@F) { $NIKTOFILES{$f} = $CONFIGFILE{'PLUGINDIR'} . "/" . $f; }
@F = dirlist($CONFIGFILE{'DBDIR'}, "");
foreach my $f (@F) { $NIKTOFILES{$f} = $CONFIGFILE{'DBDIR'} . "/" . $f; }
foreach my $file (keys %NIKTOFILES) {
my $v = "";
open(LOCAL, "<$NIKTOFILES{$file}")
|| print STDERR "+ ERROR: Unable to open '$NIKTOFILES{$file}' for read: $@\n";
my @l = <LOCAL>;
close(LOCAL);
my @VERS = grep(/^#VERSION/, @l);
chomp($VERS[0]);
$LOCAL{$file} = (parse_csv($VERS[0]))[1];
}
# check main nikto versions
foreach my $remotefile (keys %REMOTE) {
my @l = split(/\./, $LOCAL{$remotefile});
my @r = split(/\./, $REMOTE{$remotefile});
my $update = 0;
if ($LOCAL{$remotefile} eq '') { $update = 1; }
elsif ($r[0] > $l[0]) { $update = 1; }
elsif ($r[1] > $l[1]) { $update = 1; }
elsif ($r[2] > $l[2]) { $update = 1; }
if ($update) {
if ($remotefile eq "nikto") {
nprint
"+ Nikto has been updated to $REMOTE{$remotefile}, local copy is $VARIABLES{'version'}\n";
nprint
"+ No update has taken place. Please upgrade Nikto by visiting http://$server/\n";
if ($remotemsg ne "") { nprint("+ $server message: $remotemsg"); }
exit;
}
push(@DBTOGET, $remotefile);
if ($remotefile !~ /^db_/) { $code_updates = 1; }
}
}
# replace local files if updated
foreach my $toget (@DBTOGET) {
nprint("+ Retrieving '$toget'");
my ($code, $content) = nfetch(\%mark, "$serverdir/$toget", "GET");
if ($code ne 200) {
nprint("+ ERROR: Unable to get $server$serverdir/$toget");
exit;
}
if ($content ne "") {
my $dir = '';
if ($toget =~ /^db_/) { $dir = $CONFIGFILE{'DBDIR'}; }
else { $dir = $CONFIGFILE{'PLUGINDIR'}; }
open(OUT, ">$dir/$toget")
|| die print STDERR "+ ERROR: Unable to open '$dir/$toget' for write: $@\n";
print OUT $content;
close(OUT);
}
}
# CHANGES file
if ($code_updates) {
nprint("+ Retrieving 'CHANGES.txt'");
my ($code, $content) = nfetch(\%mark, "$serverdir/CHANGES.txt", "GET");
if (($content ne "") && ($code eq 200)) {
open(OUT, ">$CONFIGFILE{DOCUMENTDIR}/CHANGES.txt")
|| die print STDERR
"+ ERROR: Unable to open '$CONFIGFILE{DOCUMENTDIR}/CHANGES.txt' for write: $@\n";
print OUT $content;
close(OUT);
}
}
if ($#DBTOGET < 0) { nprint("+ No updates required."); }
if ($remotemsg ne "") { nprint("+ $server message: $remotemsg"); }
exit;
}
###############################################################################
# portions of this sub were taken from the Term::ReadPassword module.
# It has been modified to not require Term::ReadLine, but still requires
# POSIX::Termios if it's a POSIX machine
###############################################################################
sub read_data {
if ($CONFIGFILE{PROMPTS} eq 'no') { return; }
my ($prompt, $mode, $POSIX) = @_;
my $input;
my %SPECIAL = ("\x03" => 'INT', # Control-C, Interrupt
"\x08" => 'DEL', # Backspace
"\x7f" => 'DEL', # Delete
"\x0d" => 'ENT', # CR, Enter
"\x0a" => 'ENT', # LF, Enter
);
if ($NIKTO{'POSIX'}{'support'}) {
local (*TTY, *TTYOUT);
open TTY, "<&STDIN" or return;
open TTYOUT, ">>&STDOUT" or return;
# Don't buffer it!
select((select(TTYOUT), $| = 1)[0]);
print TTYOUT $prompt;
# Remember where everything was
my $fd_tty = fileno(TTY);
my $term = POSIX::Termios->new();
$term->getattr($fd_tty);
my $original_flags = $term->getlflag();
if ($mode eq "noecho") {
my $new_flags = $original_flags & ~(ISIG | ECHO | ICANON);
$term->setlflag($new_flags);
}
$term->setattr($fd_tty, TCSAFLUSH);
KEYSTROKE:
while (1) {
my $new_keys = '';
my $count = sysread(TTY, $new_keys, 99);
if ($count) {
for my $new_key (split //, $new_keys) {
if (my $meaning = $SPECIAL{$new_key}) {
if ($meaning eq 'ENT') { last KEYSTROKE; }
elsif ($meaning eq 'DEL') { chop $input; }
elsif ($meaning eq 'INT') { last KEYSTROKE; }
else { $input .= $new_key; }
}
else { $input .= $new_key; }
}
}
else { last KEYSTROKE; }
}
# Done with waiting for input. Let's not leave the cursor sitting
# there, after the prompt.
print TTY "\n";
nprint("\n");
# Let's put everything back where we found it.
$term->setlflag($original_flags);
$term->setattr($fd_tty, TCSAFLUSH);
close(TTY);
close(TTYOUT);
}
else # non-POSIX
{
print $prompt;
$input = <STDIN>;
chomp($input);
}
return $input;
}
###############################################################################
sub proxy_check {
my ($mark) = @_;
if (($request{'whisker'}->{'proxy_host'} ne '') && ($CLI{'useproxy'})) # proxy is set up
{
LW2::http_close(\%request); # force-close any old connections
setup_hash(\%request, $mark, "Proxy Check");
$request{'whisker'}->{'method'} = "GET";
$request{'whisker'}->{'uri'} = "/";
LW2::http_fixup_request(\%request);
sleeper();
LW2::http_do_request_timeout(\%request, \%result);
$COUNTERS{'totalrequests'}++;
dump_var("Request Hash", \%request);
dump_var("Result Hash", \%result);
# First check that we can connect to the proxy
if (exists $result{'whisker'}{'error'}) {
if ($result{'whisker'}{'error'} =~ /Transport endpoint is not connected/) {
nprint("+ ERROR: Could not connect to the defined proxy $CONFIGFILE{PROXYHOST}");
}
nprint("+ ERROR: Proxy error: $result{'whisker'}{'error'}");
exit 1;
}
if ($result{'whisker'}{'code'} eq "407") # proxy requires auth
{
# have id/pw?
if ($CONFIGFILE{PROXYUSER} eq "") {
$CONFIGFILE{PROXYUSER} = read_data("Proxy ID: ", "");
$CONFIGFILE{PROXYPASS} = read_data("Proxy Pass: ", "noecho");
}
if ($result{'proxy-authenticate'} !~ /Basic/i) {
my @x = split(/ /, $result{'proxy-authenticate'});
nprint(
"+ Proxy server uses '$x[0]' rather than 'Basic' authentication. $VARIABLES{'name'} $VARIABLES{'version'} can't do that."
);
exit;
}
# test it...
LW2::http_close(\%request); # force-close any old connections
LW2::auth_set("proxy-basic", \%request, $CONFIGFILE{PROXYUSER}, $CONFIGFILE{PROXYPASS})
; # set auth
LW2::http_fixup_request(\%request);
sleeper();
LW2::http_do_request_timeout(\%request, \%result);
$COUNTERS{'totalrequests'}++;
dump_var("Request Hash", \%request);
dump_var("Result Hash", \%result);
if ($result{'proxy-authenticate'} ne "") {
my @pauthinfo = split(/ /, $result{'proxy-authenticate'});
my @pauthinfo2 = split(/=/, $result{'proxy-authenticate'});
$pauthinfo2[1] =~ s/^\"//;
$pauthinfo2[1] =~ s/\"$//;
nprint(
"+ Proxy requires authentication for '$pauthinfo[0]' realm '$pauthinfo2[1]', unable to authenticate."
);
exit;
}
else { nprint("- Successfully authenticated to proxy.", "v"); }
}
}
return;
}
#######################################################################
sub dump_var {
return if !$OUTPUT{'debug'};
my $msg = $_[0];
my %hash_in = %{ $_[1] };
my $display = LW2::dump('', \%hash_in);
$display =~ s/^\$/'$msg'/;
if ($OUTPUT{'scrub'}) {
$display =~ s/'host' => '.*',/'host' => 'example.com',/g;
$display =~ s/'Host' => '.*'/'host' => 'example.com'/g;
}
nprint($display, "d");
return;
}
######################################################################
sub content_present {
my $result = FALSE;
my $res = $_[0];
# perform an extra check just in case the web server lies about finds
# basically assume that the value for a non-extension is the true
# code for "File not Found".
if ($res ne $FoF{'NONE'}{'response'}) {
foreach $found (split(' ', $VARIABLES{"\@HTTPFOUND"})) {
if ($res eq $found) {
$result = TRUE;
}
}
}
return $result;
}
#######################################################################
sub setup_hash {
my ($reqhash, $mark, $testid) = @_;
# Do the standard set up for the hash
LW2::http_init_request($reqhash);
$reqhash->{'whisker'}->{'ssl_save_info'} = 1;
$reqhash->{'whisker'}->{'keep-alive'} = 1;
$reqhash->{'whisker'}->{'lowercase_incoming_headers'} = 1;
$reqhash->{'whisker'}->{'timeout'} = $CLI{'timeout'};
if (defined $CLI{'evasion'}) {
$reqhash->{'whisker'}->{'anti_ids'} = $CLI{'evasion'};
}
$reqhash->{'User-Agent'} = $VARIABLES{'useragent'};
$reqhash->{'User-Agent'} =~ s/\@TESTID/$testid/;
$reqhash->{'whisker'}->{'retry'} = 0;
$reqhash->{'whisker'}->{'host'} = $mark->{'hostname'} || $mark->{'ip'};
if ($mark->{'vhost'}) {
$reqhash->{'Host'} = $mark->{'vhost'};
}
$reqhash->{'whisker'}->{'port'} = $mark->{'port'};
$reqhash->{'whisker'}->{'ssl'} = $mark->{'ssl'};
$reqhash->{'whisker'}->{'ssl_rsacertfile'} = $mark->{'key'};
$reqhash->{'whisker'}->{'ssl_certfile'} = $mark->{'cert'};
# Proxy stuff
if (($CONFIGFILE{PROXYHOST} ne '') && ($CLI{'useproxy'})) {
$reqhash->{'whisker'}->{'proxy_host'} = $CONFIGFILE{'PROXYHOST'};
$reqhash->{'whisker'}->{'proxy_port'} = $CONFIGFILE{'PROXYPORT'};
if ($CONFIGFILE{'PROXYUSER'} ne '') {
LW2::auth_set("proxy-basic", $reqhash,
$CONFIGFILE{'PROXYUSER'},
$CONFIGFILE{'PROXYPASS'});
}
}
return $reqhash;
}
#######################################################################
sub nfetch {
my ($mark, $uri, $method, $data, $headers_send, $flags, $testid, $httpver) = @_;
my (%request, %result, %headers_recv);
setup_hash(\%request, $mark, $testid);
# check for keyboard input
if (($COUNTERS{'totalrequests'} % 10) == 0) {
if (check_input($mark) eq 'term') { $mark->{'terminate'} = 1; }
if ($CLI{'until'} ne '') {
if ($CLI{'until'} <= time()) {
pause('u');
}
}
}
# check execution time
if ($CLI{'maxtime'} ne '') {
if ((time() - $mark->{'start_time'}) > $CLI{'maxtime'}) {
nprint("+ ERROR: Host maximum execution time of $CLI{'maxtime'} seconds reached");
$mark->{'terminate'} = 1;
}
}
$request{'whisker'}->{'uri'} = $mark->{'root'} . $uri; # prepend -root option's value if set
$request{'whisker'}->{'method'} = $method;
if ($data ne "") {
$data =~ s/\\\"/\"/g;
$request{'whisker'}->{'data'} = $data;
}
# check for extra HTTP headers
if (ref($headers_send) eq "HASH") {
# loop through the hash ref passed and add each header to request
while (my ($key, $value) = each(%$headers_send)) {
$request{$key} = $value;
}
}
# set cookies
if (defined($mark->{'cookiejar'})) {
LW2::cookie_write($mark->{'cookiejar'}, \%request, 1);
}
# over-ride HTTP version
my $httpver_old = $request{'whisker'}->{'version'};
if ($httpver ne '') {
$request{'whisker'}->{'version'} = $httpver;
}
LW2::http_fixup_request(\%request) unless ($flags->{'noclean'});
# Run pre hooks
unless ($flags->{'noprefetch'}) {
(%$request, %$result) = run_hooks($mark, "prefetch", \%request, \%result);
}
# do the request
sleeper();
LW2::http_do_request_timeout(\%request, \%result);
$COUNTERS{'totalrequests'}++;
# If we got an error, do 1 retry. This should be much more intelligent and configurable!
if (defined $result{'whisker'}->{'error'} || $result{'whisker'}{'code'} eq '') {
sleeper();
LW2::http_do_request_timeout(\%request, \%result);
$COUNTERS{'totalrequests'}++;
}
if ($OUTPUT{'debug'}) {
dump_var("Request Hash", \%request);
dump_var("Result Hash", \%result);
}
# Snarf what we can from the whisker hash and put in mark
if (!exists $result{'whisker'}->{'error'}) {
if (!exists $mark->{'banner'}) {
$mark->{'banner'} = $result{'server'};
}
else {
# Check banner hasn't changed
if ( exists $result{'server'}
&& $mark->{'banner'} ne $result{'server'}
&& !exists $mark->{'bannerchanged'}) {
nprint(
"+ Server banner has changed from '$mark->{banner}' to '$result{server}' which may suggest a WAF, load balancer or proxy is in place"
);
$mark->{'bannerchanged'} = 1;
}
}
if (!exists $mark->{'ssl_cipher'} && $mark->{'ssl'}) {
# Grab ssl details
$mark->{'ssl_cipher'} = $result{'whisker'}->{'ssl_cipher'};
$mark->{'ssl_cert_issuer'} = $result{'whisker'}->{'ssl_cert_issuer'};
$mark->{'ssl_cert_subject'} = $result{'whisker'}->{'ssl_cert_subject'};
}
}
nprint("- $result{'whisker'}{'code'} for $method:\t$request{'whisker'}->{'uri'}", "v");
# Check for errors to reduce false positives
if ((defined $result{'whisker'}->{'error'} || $result{'whisker'}{'code'} eq '')
&& !exists $flags->{'noerror'}) {
$mark->{'total_errors'}++;
nprint(
"+ ERROR: $request{'whisker'}->{'uri'} returned an error: $result{'whisker'}{'error'}\n",
"e"
);
if (($result{'whisker'}->{'code'} eq 502) && ($CLI{'useproxy'})) {
nprint("+ ERROR: Revieved 502 'Bad Gateway' from proxy\n");
}
}
if ($OUTPUT{'show_cookies'} && (defined($result{'whisker'}->{'cookies'}))) {
foreach my $c (@{ $result{'whisker'}->{'cookies'} }) {
nprint("+ $request{'whisker'}->{'uri'} sent cookie: $c");
}
}
while (my ($key, $value) = each(%result)) {
if ($key ne "whisker" && $key ne "connection") {
$headers_recv{$key} = $value;
}
}
# Run post hooks
unless ($flags->{'nopostfetch'}) {
($request, %$result) = run_hooks($mark, "postfetch", \%request, \%result);
}
# set version back
$request{'whisker'}->{'version'} = $httpver_old;
return $result{'whisker'}->{'code'}, $result{'whisker'}->{'data'},
$result{'whisker'}->{'error'}, \%request, \%result, \%headers_recv;
}
#######################################################################
sub set_scan_items {
# load the tests
%TESTS = ();
$COUNTERS{total_checks} = 0;
my @SKIPLIST = ();
if (defined $CONFIGFILE{SKIPIDS}) {
@SKIPLIST = split(/ /, $CONFIGFILE{SKIPIDS});
}
# now load checks
foreach my $line (@DBFILE) {
if ($line =~ /^\"/) # check
{
chomp($line);
my @item = parse_csv($line);
my $add = 1;
# check tuning options
if ((defined $CLI{'tuning'}) && (defined $item[2])) {
# Work out the required tuning from the CLI string
my $exclude = 0;
foreach my $tune (split(//, $CLI{'tuning'})) {
if ($tune eq "x") {
$exclude = 1;
next;
}
if ($exclude == 0) {
if ($item[2] !~ /$tune/) { $add = 0; }
next;
}
if ($exclude == 1) {
if ($item[2] =~ /$tune/) { $add = 0; }
}
}
}
# Skip list
foreach my $id (@SKIPLIST) {
if ($id eq $item[0]) { $add = 0; }
}
# RFI URL Defined?
if (($item[2] =~ /c/) && ($VARIABLES{'@RFIURL'} eq '')) {
$add = 0;
}
if ($add) {
my $ext = get_ext($item[3]);
$db_extensions{$ext} = 1;
# validate...
for (my $y = 5 ; $y <= 9 ; $y++) {
next if $item[$y] eq '';
$item[$y] =~ s/\\"/"/g; # quotes are only escaped for csv parsing
$item[$y] = validate_and_fix_regex($item[$y]);
}
$COUNTERS{total_checks}++;
$TESTS{ $item[0] }{'uri'} = $item[3];
$TESTS{ $item[0] }{'osvdb'} = $item[1];
$TESTS{ $item[0] }{'method'} = $item[4];
$TESTS{ $item[0] }{'match_1'} = $item[5];
$TESTS{ $item[0] }{'match_1_or'} = $item[6];
$TESTS{ $item[0] }{'match_1_and'} = $item[7];
$TESTS{ $item[0] }{'fail_1'} = $item[8];
$TESTS{ $item[0] }{'fail_2'} = $item[9];
$TESTS{ $item[0] }{'message'} = $item[10];
$TESTS{ $item[0] }{'data'} = $item[11];
$TESTS{ $item[0] }{'headers'} = $item[12];
}
}
}
undef @DBFILE; # this memory hog is no longer needed!
nprint("- $COUNTERS{'total_checks'} server checks loaded", "v");
if ($COUNTERS{'total_checks'} eq 0 && !defined $CLI{'tuning'}) {
nprint("+ Unable to load valid checks!");
exit;
}
return;
}
#######################################################################
sub max_test_id {
return (sort { $a <=> $b } keys %TESTS)[-1];
}
#######################################################################
# extract IP like strings and return an array
sub get_ips {
my $string = $_[0] || return;
my @ips;
while ($string =~ /(?:\b|[^0-9])([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)(?:\b|[^0-9])/g) {
push(@ips, $1);
}
return @ips;
}
#######################################################################
# Check an IP's validity. Returns booleans for: validity, internal, loopback
sub is_ip {
my $ip = $_[0] || return 0, 0, 0;
my $internal = 0;
my $loopback = 0;
# simple syntax check
# this will fail on some edge cases, but it's 99%...
if ($ip !~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) {
return 0, $internal, $loopback;
}
# now validate octets
my @octets = split(/\./, $ip);
if (scalar(@octets) ne 4) { return 0, $internal, $loopback; }
for (my $o = 0 ; $o <= 3 ; $o++) {
if (($octets[$o] < 0) || ($octets[$o] > 255)) { return 0, $internal, $loopback; }
if ($octet[$o] =~ /^0/) { return 0, $internal, $loopback; }
if (($o eq 0) && ($octets[$o] < 1)) { return 0, $internal, $loopback; }
}
# now check for internal
if ($ip =~ /^(?:10|192\.168|172\.(?:1[6-9]|2\d|3[01]))\./) { $internal = 1; }
# lastly, loopback?
if ($ip eq '127.0.0.1') { $loopback = 1; }
return 1, $internal, $loopback;
}
#######################################################################
sub parse_csv {
my $text = $_[0] || return;
my @new = ();
push(@new, $+) while $text =~ m{
"([^\"\\]*(?:\\.[^\"\\]*)*)",?
| ([^,]+),?
| ,
}gx;
push(@new, undef) if substr($text, -1, 1) eq ',';
return @new;
}
#######################################################################
sub version {
my %NIKTOFILES;
my @F = dirlist($CONFIGFILE{'PLUGINDIR'}, "");
foreach my $f (@F) { $NIKTOFILES{$f} = $CONFIGFILE{'PLUGINDIR'} . "/" . $f; }
@F = dirlist($CONFIGFILE{'DBDIR'}, "");
foreach my $f (@F) { $NIKTOFILES{$f} = $CONFIGFILE{'DBDIR'} . "/" . $f; }
nprint($VARIABLES{'DIV'});
nprint("$VARIABLES{'name'} Versions");
nprint($VARIABLES{'DIV'});
nprint("File Version Last Mod");
nprint("----------------------------- -------- ----------");
nprint("Nikto main $VARIABLES{'version'}");
nprint("LibWhisker $LW2::VERSION");
foreach my $FILE (sort keys %NIKTOFILES) {
next if $FILE eq 'LW2.pm';
open(FI, "<$NIKTOFILES{$FILE}")
|| die print STDERR "+ ERROR: Unable to open '$NIKTOFILES{$FILE}': $!\n";
my @F = <FI>;
close(FI);
my @VERS = grep(/^#VERSION/, @F);
my @MODS = grep(/^# \$Id:/, @F);
chomp($VERS[0]);
chomp($MODS[0]);
my @modification = split(/ /, $MODS[0]);
$VERS[0] =~ s/^#VERSION,//;
my $ws1 = (35 - length($FILE));
my $ws2 = (13 - length($VERS[0]));
nprint("$FILE" . " " x $ws1 . "$VERS[0]" . " " x $ws2 . "$modification[4]");
}
nprint($VARIABLES{'DIV'});
# Check dependencies
eval "require RPC::XML";
if ($@) {
nprint("Module RPC::XML missing. Logging to Metasploit is disabled.");
}
eval "require RPC::XML::Client";
if ($@) {
nprint("Module RPC::XML::Client missing. Logging to Metasploit is disabled.");
}
LW2::init_ssl_engine();
my ($avail, $lib, $ver) = LW2::ssl_is_available();
if ($avail) {
nprint("SSL Module: $lib version $ver");
}
else {
nprint("SSL: support not available.");
}
nprint($VARIABLES{'DIV'});
exit;
}
#######################################################################
sub send_updates {
return if ($CONFIGFILE{'UPDATES'} !~ /yes|auto/i);
my (@MARKS) = @_;
my ($updated_version, $answer, $code);
my $have_updates = 0;
foreach my $mark (@MARKS) {
foreach my $component (keys %{ $mark->{'components'} }) {
if ($mark->{'components'}->{$component} eq 2) {
if ($component !~ /[0-9]/) { next; }
elsif ($component eq "Win32") { next; }
elsif ($component eq "(Win32)") { next; }
elsif ($component eq "Linux-Mandrake") { next; }
elsif ($component eq "") { next; }
$have_updates = 1;
$updated_version .= "$component ";
}
}
}
if ((!$have_updates) || ($updated_version eq "")) { return; }
# make sure the db_outdatedb isn't *too* old
open(OD, "<$CONFIGFILE{'DBDIR'}/db_outdated")
|| die print STDERR "+ ERROR: Unable to open '$CONFIGFILE{'DBDIR'}/db_outdated': $!\n";
@F = <OD>;
close(OD);
my @LASTUPDATED = grep(/^\# \$Id: db_outdated/, @F);
$LASTUPDATED[0] =~ /([0-9]{4}\-[0-9]{2})/;
$lm = $1;
$lm =~ s/\-//g;
my @NOW = localtime(time);
$NOW[5] += 1900;
$NOW[4]++;
if ($NOW[4] < 10) { $NOW[4] = "0$NOW[4]"; }
my $now = "$NOW[5]$NOW[4]";
if (($now - $lm) > 120) { return; } # DB is 4 months old... ignore the updates!
$updated_version =~ s/\s+$//;
$updated_version =~ s/^\s+//;
if ($CONFIGFILE{'UPDATES'} eq "auto") {
$answer = "y";
}
else {
$answer = read_data(
"\n
*********************************************************************
Portions of the server's headers ($updated_version) are not in
the Nikto database or are newer than the known string. Would you like
to submit this information (*no server specific data*) to CIRT.net
for a Nikto update (or you may email to sullo\@cirt.net) (y/n)? ", ""
);
}
if ($answer !~ /y/i) { return; }
# set up our mark
my %mark = ('ident' => 'www.cirt.net',
'ssl' => 0,
'port' => 80
);
for (my $i = 0 ; $i <= $#ARGV ; $i++) {
if (($ARGV[$i] eq "-u") || ($ARGV[$i] eq "-useproxy")) {
$CLI{'useproxy'} = 1;
last;
}
}
($mark{'hostname'}, $mark{'ip'}, $mark{'display_name'}) = resolve('www.cirt.net');
($code, $content) = nfetch(\%mark, "/cgi-bin/versions?DATA=$updated_version", "GET");
if ($code eq 407) {
if ($CONFIGFILE{PROXYUSER} eq "") {
$CONFIGFILE{PROXYUSER} = read_data("Proxy ID: ", "");
$CONFIGFILE{PROXYPASS} = read_data("Proxy Pass: ", "noecho");
}
($code , $content) = nfetch(\%mark, "/cgi-bin/versions?DATA=$updated_version", "GET");
}
if ($code eq "") {
LW2::http_close(\%request); # force-close any old connections
$mark{'ip'} = $CONFIGFILE{CIRT};
($code, $content) = nfetch(\%mark, "/cgi-bin/versions?DATA=$updated_version", "GET");
}
if ($content !~ /SUCCESS/) {
nprint("+ ERROR: ($code, $content): Unable to send update info to CIRT.net");
}
else {
nprint("- Sent updated info to CIRT.net -- Thank you!");
}
return;
}
#######################################################################
sub usage {
if ($_[0] eq "Help") {
print "
Options:
-ask+ Whether to ask about submitting updates
yes Ask about each (default)
no Don't ask, don't send
auto Don't ask, just send
-Cgidirs+ Scan these CGI dirs: \"none\", \"all\", or values like \"/cgi/ /cgi-a/\"
-config+ Use this config file
-Display+ Turn on/off display outputs:
1 Show redirects
2 Show cookies received
3 Show all 200/OK responses
4 Show URLs which require authentication
D Debug output
E Display all HTTP errors
P Print progress to STDOUT
S Scrub output of IPs and hostnames
V Verbose output
-dbcheck Check database and other key files for syntax errors
-evasion+ Encoding technique:\n";
foreach my $k (sort keys %{ $NIKTO{'anti_ids'} }) {
print " $k $NIKTO{'anti_ids'}{$k}\n";
}
print " -Format+ Save file (-o) format:
csv Comma-separated-value
htm HTML Format
msf+ Log to Metasploit
nbe Nessus NBE format
txt Plain text
xml XML Format
(if not specified the format will be taken from the file extension passed to -output)
-Help Extended help information
-host+ Target host
-IgnoreCode Ignore Codes--treat as negative responses
-id+ Host authentication to use, format is id:pass or id:pass:realm
-key+ Client certificate key file
-list-plugins List all available plugins, perform no testing
-maxtime+ Maximum testing time per host
-mutate+ Guess additional file names:\n";
foreach my $k (sort keys %{ $NIKTO{'mutate_opts'} }) {
print " $k $NIKTO{'mutate_opts'}{$k}\n";
}
print " -mutate-options Provide information for mutates
-nointeractive Disables interactive features
-nolookup Disables DNS lookups
-nossl Disables the use of SSL
-no404 Disables nikto attempting to guess a 404 page
-output+ Write output to this file ('.' for auto-name)
-Pause+ Pause between tests (seconds, integer or float)
-Plugins+ List of plugins to run (default: ALL)
-port+ Port to use (default 80)
-RSAcert+ Client certificate file
-root+ Prepend root value to all requests, format is /directory
-Save Save positive responses to this directory ('.' for auto-name)
-ssl Force ssl mode on port
-Tuning+ Scan tuning:
1 Interesting File / Seen in logs
2 Misconfiguration / Default File
3 Information Disclosure
4 Injection (XSS/Script/HTML)
5 Remote File Retrieval - Inside Web Root
6 Denial of Service
7 Remote File Retrieval - Server Wide
8 Command Execution / Remote Shell
9 SQL Injection
0 File Upload
a Authentication Bypass
b Software Identification
c Remote Source Inclusion
x Reverse Tuning Options (i.e., include all except specified)
-timeout+ Timeout for requests (default 10 seconds)
-Userdbs Load only user databases, not the standard databases
all Disable standard dbs and load only user dbs
tests Disable only db_tests and load udb_tests
-until Run until the specified time or duration
-update Update databases and plugins from CIRT.net
-useproxy Use the proxy defined in nikto.conf
-Version Print plugin and database versions
-vhost+ Virtual host (for Host header)
+ requires a value\n\n";
}
else {
print "
-config+ Use this config file
-Display+ Turn on/off display outputs
-dbcheck check database and other key files for syntax errors
-Format+ save file (-o) format
-Help Extended help information
-host+ target host
-id+ Host authentication to use, format is id:pass or id:pass:realm
-list-plugins List all available plugins
-output+ Write output to this file
-nossl Disables using SSL
-no404 Disables 404 checks
-Plugins+ List of plugins to run (default: ALL)
-port+ Port to use (default 80)
-root+ Prepend root value to all requests, format is /directory
-ssl Force ssl mode on port
-Tuning+ Scan tuning
-timeout+ Timeout for requests (default 10 seconds)
-update Update databases and plugins from CIRT.net
-Version Print plugin and database versions
-vhost+ Virtual host (for Host header)
+ requires a value
Note: This is the short help output. Use -H for full help text.\n\n";
}
exit;
}
#######################################################################
sub init_db {
my $dbname = $_[0];
my $filename = "$CONFIGFILE{'DBDIR'}/" . $dbname;
my (@dbarray, @headers);
my $hashref = {};
if ($CLI{'userdbs'} ne 'all') {
# Check that the database exists
unless (open(IN, "<$filename")) {
nprint("+ ERROR: Unable to open database file $dbname: $!.");
return $dbarray;
}
# Now read the header values
while (<IN>) {
chomp;
s/\#.*$//;
if ($_ eq "") { next }
unless (@headers) {
@headers = parse_csv($_);
}
else {
# contents; so split them up and apply to hash
my @contents = parse_csv($_);
my $hashref = {};
for (my $i = 0 ; $i <= $#contents ; $i++) {
$hashref->{ $headers[$i] } = $contents[$i];
}
push(@dbarray, $hashref);
}
}
close(IN);
}
# And the udb_* file
$filename = "$CONFIGFILE{'DBDIR'}/u" . $dbname;
if (open(IN, "<$filename")) {
while (<IN>) {
chomp;
s/\#.*$//;
if ($_ eq "") { next; }
# contents; so split them up and apply to hash
my @contents = parse_csv($_);
my $hashref = {};
for (my $i = 0 ; $i <= $#contents ; $i++) {
$hashref->{ $headers[$i] } = $contents[$i];
}
push(@dbarray, $hashref);
}
}
close(IN);
return \@dbarray;
}
#######################################################################
sub add_vulnerability {
my ($mark, $message, $nikto_id, $osvdb, $method, $uri, $request, $response) = @_;
$uri = "/" unless (defined $uri);
$method = "GET" unless (defined $method);
$osvdb = "0" unless (defined $osvdb);
# check to see if we've alerted already (can be from content search, etc.)
foreach my $r (@RESULTS) {
if ( ($uri eq $r->{'uri'})
&& ($message eq $r->{'message'})
&& ($method eq $r->{'method'})
&& (${ $r->{'mark'} }{'ident'} eq $mark->{'ident'})
&& (${ $r->{'mark'} }{'port'} eq $mark->{'port'})) {
return;
}
}
my $result = "";
if (defined $_[7]) {
$result = $_[7]->{'whisker'}->{'data'};
}
my $resulthash;
%$resulthash = (mark => $mark,
message => $message,
nikto_id => $nikto_id,
osvdb => $osvdb,
method => $method,
uri => $uri,
result => $result,
);
push(@RESULTS, $resulthash);
$mark->{total_vulns}++;
unless ($osvdb eq "0") {
$message = "OSVDB-$osvdb: $message";
}
nprint("+ $message");
# Save it
if ($CLI{'saveresults'} ne '') {
save_item($resulthash, $message, $request, $response);
}
# Now report it
report_item($mark, $resulthash);
}
###############################################################################
sub list_plugins {
# Just do a load_plugins, then loop through the array and print out name,
# description and copyright
load_plugins();
foreach my $plugin (@PLUGINS) {
nprint("Plugin: $plugin->{'name'}");
push(@all_names, $plugin->{'name'});
nprint(" $plugin->{'full_name'} - $plugin->{'description'}");
nprint(" Written by $plugin->{'author'}, Copyright (C) $plugin->{'copyright'}");
if (defined $plugin->{'options'}) {
nprint(" Options:");
while (my ($option, $description) = each(%{ $plugin->{'options'} })) {
nprint(" $option: $description");
}
}
nprint("\n");
}
# Plugin macros
nprint("Defined plugin macros:");
foreach my $macro (keys %CONFIGFILE) {
if ($macro =~ /^@@/) {
nprint(" $macro = \"" . $CONFIGFILE{$macro} . "\"");
if ($CONFIGFILE{$macro} =~ /@@/) {
nprint(" (expanded) = \"" . expand_pluginlist($CONFIGFILE{$macro}, 0) . "\"");
}
}
}
exit(0);
}
###############################################################################
# This is overly complicated and jumps a lot between scalars and arrays. The REs are
# probably dodgy, but it works! W00!
sub expand_pluginlist {
my ($pluginlist, $parent) = @_;
my @macros;
foreach my $config (keys %CONFIGFILE) {
if ($config =~ /^@@/) {
push(@macros, $config);
}
}
# Now loop through each member of the list and expand it
my $count = 0;
my $npluginlist = $pluginlist;
do {
$count++;
my @raw = split(/;/, $npluginlist);
# cooked contains the processed list
my @cooked;
foreach my $entry (@raw) {
# Is it +; if so remap to @@DEFAULT
if ($entry eq "+") {
$entry = '@@DEFAULT';
}
# result contains the processed entry
my $result = $original = $entry;
# Is it a macro
if ($entry =~ /^-?@@/) {
# break up into components
$prefix = ($entry =~ /^-/) ? "-" : "";
$name = $suffix = $entry;
$name =~ s/(^-?)(@@[[:alpha:]]+)(\(?.*\)?$)/$2/;
$suffix =~ s/(.*)(\(.*\))/$2/;
if ($suffix eq $entry) {
$suffix = "";
}
foreach my $macro (@macros) {
if ($entry =~ /-?$macro/) {
# It's a macro, so replace the contents with the macro
# Add prefix and suffix to each member of the macro
my @temp;
foreach my $child (split(/;/, $CONFIGFILE{$macro})) {
push(@temp, "$prefix$child$suffix");
}
$result = join(';', @temp);
# stop an infinite loop
last;
}
}
}
if ($result =~ /^-?@@/ && $result eq $original) {
# macro not found or is itself - ignore
$result = "";
}
if ($count > 100) {
# check for recurstion
nprint("ERROR: Recursion found whilst expanding macros");
$result = "";
last;
}
push(@cooked, $result);
}
$npluginlist = join(';', @cooked);
} while ($npluginlist =~ /@@/ && $count <= 100);
#use re 'debug';
# Now we've expanded out macros, deal with duplicates and -
my @raw = split(/;/, $npluginlist);
# hash so we don't have to mess with duplicates
my %cooked;
foreach my $plugin (@raw) {
# break out components
my $minus;
my $name = my $suffix = $plugin;
$minus = (substr($plugin, 0, 1) eq '-');
$name =~ s/(^-?)([^\(]+)(\(?.*\)?$)/$2/;
$suffix =~ s/(.*)(\(.*\))/$2/;
if ($suffix eq $plugin) {
$suffix = "";
}
#nprint("P:$plugin M:$minus N:$name S:$suffix");
if ($minus) {
# it's a minus - remove any previous entry
if (exists $cooked{$name}) {
delete $cooked{$name};
}
}
else {
# else add it with the parameters as the value of the hash
$cooked{$name} = $suffix;
}
}
# Now rejoin into one happy whole
my $output;
foreach my $plugin (keys %cooked) {
$output .= "$plugin" . $cooked{$plugin} . ";";
}
# remove the last ;
$output =~ s/;$//g;
return $output;
}
###############################################################################
# Check a regex for validation & fix. If mode=1, return a flag which indicates
# whether the regex was changed
sub validate_and_fix_regex {
my $regex = $_[0];
my $mode = $_[1] || 0;
my $fixed = 0;
eval { qr/$regex/ };
if ($@) {
$fixed = 1;
$regex = rquote($regex);
}
if ($mode) { return $regex, $fixed; }
return $regex;
}
###############################################################################
sub rquote {
my $string = $_[0] || return;
$string =~ s/([^A-Za-z_0-9 "'\\])/\\$1/g;
return $string;
}
###############################################################################
sub gmt_offset {
my @t = localtime(time);
return (timegm(@t) - timelocal(@t)) / 3600;
}
###############################################################################
sub nikto_core { return; } # trap for this plugin being called to run. lame.
###############################################################################
1;
|