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 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467
|
---
-- MSSQL Library supporting a very limited subset of operations.
--
-- The library was designed and tested against Microsoft SQL Server 2005.
-- However, it should work with versions 7.0, 2000, 2005, 2008 and 2012.
-- Only a minimal amount of parsers have been added for tokens, column types
-- and column data in order to support the first scripts.
--
-- The code has been implemented based on traffic analysis and the following
-- documentation:
-- * SSRP Protocol Specification: http://msdn.microsoft.com/en-us/library/cc219703.aspx
-- * TDS Protocol Specification: http://msdn.microsoft.com/en-us/library/dd304523.aspx
-- * TDS Protocol Documentation: http://www.freetds.org/tds.html.
-- * The JTDS source code: http://jtds.sourceforge.net/index.html.
--
-- * SSRP: Class that handles communication over the SQL Server Resolution Protocol, used for identifying instances on a host.
-- * ColumnInfo: Class containing parsers for column types which are present before the row data in all query response packets. The column information contains information relevant to the data type used to hold the data eg. precision, character sets, size etc.
-- * ColumnData: Class containing parsers for the actual column information.
-- * Token: Class containing parsers for tokens returned in all TDS responses. A server response may hold one or more tokens with information from the server. Each token has a type which has a number of type specific fields.
-- * QueryPacket: Class used to hold a query and convert it to a string suitable for transmission over a socket.
-- * LoginPacket: Class used to hold login specific data which can easily be converted to a string suitable for transmission over a socket.
-- * PreLoginPacket: Class used to (partially) implement the TDS PreLogin packet
-- * TDSStream: Class that handles communication over the Tabular Data Stream protocol used by SQL serve. It is used to transmit the the Query- and Login-packets to the server.
-- * Helper: Class which facilitates the use of the library by through action oriented functions with descriptive names.
-- * Util: A "static" class containing mostly character and type conversion functions.
--
-- The following sample code illustrates how scripts can use the Helper class
-- to interface the library:
--
-- <code>
-- local helper = mssql.Helper:new()
-- status, result = helper:Connect( host, port )
-- status, result = helper:Login( username, password, "temdpb", host.ip )
-- status, result = helper:Query( "SELECT name FROM master..syslogins" )
-- helper:Disconnect()
-- </code>
--
-- The following sample code illustrates how scripts can use the Helper class
-- with pre-discovered instances (e.g. by <code>ms-sql-discover</code> or <code>broadcast-ms-sql-discover</code>):
--
-- <code>
-- local instance = mssql.Helper.GetDiscoveredInstances( host, port )
-- if ( instance ) then
-- local helper = mssql.Helper:new()
-- status, result = helper:ConnectEx( instance )
-- status, result = helper:LoginEx( instance )
-- status, result = helper:Query( "SELECT name FROM master..syslogins" )
-- helper:Disconnect()
-- end
-- </code>
--
-- Known limitations:
-- * The library does not support SSL. The foremost reason being the awkward choice of implementation where the SSL handshake is performed within the TDS data block. By default, servers support connections over non SSL connections though.
-- * Version 7 and ONLY version 7 of the protocol is supported. This should cover Microsoft SQL Server 7.0 and later.
-- * TDS Responses contain one or more response tokens which are parsed based on their type. The supported tokens are listed in the <code>TokenType</code> table and their respective parsers can be found in the <code>Token</code> class. Note that some token parsers are not fully implemented and simply move the offset the right number of bytes to continue processing of the response.
-- * The library only supports a limited subsets of datatypes and will abort execution and return an error if it detects an unsupported type. The supported data types are listed in the <code>DataTypes</code> table. In order to add additional data types a parser function has to be added to both the <code>ColumnInfo</code> and <code>ColumnData</code> class.
-- * No functionality for languages, localization or character codepages has been considered or implemented.
-- * The library does database authentication only. No OS authentication or use of the integrated security model is supported.
-- * Queries using SELECT, INSERT, DELETE and EXEC of procedures have been tested while developing scripts.
--
-- @copyright Same as Nmap--See https://nmap.org/book/man-legal.html
--
-- @author Patrik Karlsson <patrik@cqure.net>
-- @author Chris Woodbury
--
-- @args mssql.username The username to use to connect to SQL Server instances.
-- This username is used by scripts taking actions that require
-- authentication (e.g. <code>ms-sql-query</code>) This username (and its
-- associated password) takes precedence over any credentials discovered
-- by the <code>ms-sql-brute</code> and <code>ms-sql-empty-password</code>
-- scripts.
--
-- @args mssql.password The password for <code>mssql.username</code>. If this
-- argument is not given but <code>mssql.username</code>, a blank password
-- is used.
--
-- @args mssql.instance-name In addition to instances discovered via port
-- scanning and version detection, run scripts on
-- these named instances (string or list of strings)
--
-- @args mssql.instance-port In addition to instances discovered via port
-- scanning and version detection, run scripts on
-- the instances running on these ports (number or list of numbers)
--
-- @args mssql.instance-all In addition to instances discovered via port
-- scanning and version detection, run scripts on all
-- discovered instances. These include named-pipe
-- instances via SMB and those discovered via the
-- browser service.
--
-- @args mssql.domain The domain against which to perform integrated
-- authentication. When set, the scripts assume integrated authentication
-- should be performed, rather than the default sql login.
--
-- @args mssql.protocol The protocol to use to connect to the instance. The
-- protocol may be either <code>NP</code>,<code>Named Pipes</code> or
-- <code>TCP</code>.
--
-- @args mssql.timeout How long to wait for SQL responses. This is a number
-- followed by <code>ms</code> for milliseconds, <code>s</code> for
-- seconds, <code>m</code> for minutes, or <code>h</code> for hours.
-- Default: <code>30s</code>.
--
-- @args mssql.scanned-ports-only If set, the script will only connect
-- to ports that were included in the Nmap scan. This may result in
-- instances not being discovered, particularly if UDP port 1434 is not
-- included. Additionally, instances that are found to be running on
-- ports that were not scanned (e.g. if 1434/udp is in the scan and the
-- SQL Server Browser service on that port reports an instance
-- listening on 43210/tcp, which was not scanned) will be reported but
-- will not be stored for use by other ms-sql-* scripts.
local math = require "math"
local match = require "match"
local nmap = require "nmap"
local datetime = require "datetime"
local outlib = require "outlib"
local smb = require "smb"
local smbauth = require "smbauth"
local stdnse = require "stdnse"
local strbuf = require "strbuf"
local string = require "string"
local table = require "table"
local tableaux = require "tableaux"
local unicode = require "unicode"
_ENV = stdnse.module("mssql", stdnse.seeall)
-- Created 01/17/2010 - v0.1 - created by Patrik Karlsson <patrik@cqure.net>
-- Revised 03/28/2010 - v0.2 - fixed incorrect token types. added 30 seconds timeout
-- Revised 01/23/2011 - v0.3 - fixed parsing error in discovery code with patch
-- from Chris Woodbury
-- Revised 02/01/2011 - v0.4 - numerous changes and additions to support new
-- functionality in ms-sql- scripts and to be more
-- robust in parsing and handling data. (Chris Woodbury)
-- Revised 02/19/2011 - v0.5 - numerous changes in script, library behaviour
-- * huge improvements in version detection
-- * added support for named pipes
-- * added support for integrated NTLMv1 authentication
--
-- (Patrik Karlsson, Chris Woodbury)
-- Revised 08/19/2012 - v0.6 - added multiple data types
-- * added detection and handling of null values when processing query responses from the server
-- * added DoneProc response token support
--
-- (Tom Sellers)
-- Updated 10/01/2012 - v0.7 - added support for 2012 and later service packs for 2005, 2008 and 2008 R2 (Rob Nicholls)
-- Updated 02/06/2015 - v0.8 - added support for 2014 and later service packs for older versions (Rob Nicholls)
local HAVE_SSL, openssl = pcall(require, "openssl")
do
namedpipes = smb.namedpipes
local arg = stdnse.get_script_args( "mssql.timeout" ) or "30s"
local timeout, err = stdnse.parse_timespec(arg)
if not timeout then
error(err)
end
MSSQL_TIMEOUT = timeout
end
-- Make args either a list or nil
local function list_of (input, transform)
if not input then return nil end
if type(input) ~= "table" then
return {transform(input)}
end
for i, v in ipairs(input) do
input[i] = transform(v)
end
return input
end
local SCANNED_PORTS_ONLY = not not stdnse.get_script_args("mssql.scanned-ports-only")
local targetInstanceNames = list_of(stdnse.get_script_args("mssql.instance-name"), string.upper)
local targetInstancePorts = list_of(stdnse.get_script_args("mssql.instance-port"), tonumber)
local targetAllInstances = not not stdnse.get_script_args("mssql.instance-all")
-- This constant is number of seconds from 1900-01-01 to 1970-01-01
local tds_offset_seconds = -2208988800 - datetime.utc_offset()
-- *************************************
-- Informational Classes
-- *************************************
--- SqlServerInstanceInfo class
SqlServerInstanceInfo =
{
instanceName = nil,
version = nil,
serverName = nil,
isClustered = nil,
host = nil,
port = nil,
pipeName = nil,
new = function(self,o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end,
-- Compares two SqlServerInstanceInfo objects and determines whether they
-- refer to the same SQL Server instance, judging by a combination of host,
-- port, named pipe information and instance name.
__eq = function( self, other )
local areEqual
if ( not (self.host and other.host) ) then
-- if they don't both have host information, we certainly can't say
-- whether they're the same
areEqual = false
else
areEqual = (self.host.ip == other.host.ip)
end
if (self.port and other.port) then
areEqual = areEqual and ( other.port.number == self.port.number and
other.port.protocol == self.port.protocol )
elseif (self.pipeName and other.pipeName) then
areEqual = areEqual and (self.pipeName == other.pipeName)
elseif (self.instanceName and other.instanceName) then
areEqual = areEqual and (self.instanceName == other.instanceName)
else
-- if we have neither port nor named pipe info nor instance names,
-- we can't say whether they're the same
areEqual = false
end
return areEqual
end,
--- Merges the data from one SqlServerInstanceInfo object into another.
--
-- Each field in the first object is populated with the data from that field
-- in second object if the first object's field is nil OR if
-- <code>overwrite</code> is set to true. A special case is made for the
-- <code>version</code> field, which is only overwritten in the second object
-- has more reliable version information. The second object is not modified.
Merge = function( self, other, overwrite )
local mergeFields = { "host", "port", "instanceName", "version", "isClustered", "pipeName" }
for _, fieldname in ipairs( mergeFields ) do
-- Add values from other only if self doesn't have a value, or if overwrite is true
if ( other[ fieldname ] ~= nil and (overwrite or self[ fieldname ] == nil) ) then
self[ fieldname ] = other[ fieldname ]
end
end
if (self.version and self.version.source == "SSRP" and
other.version and other.version.Source == "SSNetLib") then
self.version = other.version
end
end,
--- Returns a name for the instance, based on the available information.
--
-- This may take one of the following forms:
-- * HOST\INSTANCENAME
-- * PIPENAME
-- * HOST:PORT
GetName = function( self )
if (self.instanceName) then
return string.format( "%s\\%s", self.host.ip or self.serverName or "[nil]", self.instanceName or "[nil]" )
elseif (self.pipeName) then
return string.format( "%s", self.pipeName )
else
return string.format( "%s:%s", self.host.ip or self.serverName or "[nil]", (self.port and self.port.number) or "[nil]" )
end
end,
--- Sets whether the instance is in a cluster
--
-- @param self
-- @param isClustered Boolean true or the string "Yes" are interpreted as true;
-- all other values are interpreted as false.
SetIsClustered = function( self, isClustered )
self.isClustered = (isClustered == true) or (isClustered == "Yes")
end,
--- Indicates whether this instance has networking protocols enabled, such
-- that scripts could attempt to connect to it.
HasNetworkProtocols = function( self )
return (self.pipeName ~= nil) or (self.port and self.port.number)
end,
}
--- SqlServerVersionInfo class
SqlServerVersionInfo =
{
versionNumber = "", -- The full version string (e.g. "9.00.2047.00")
major = nil, -- The major version (e.g. 9)
minor = nil, -- The minor version (e.g. 0)
build = nil, -- The build number (e.g. 2047)
subBuild = nil, -- The sub-build number (e.g. 0)
productName = nil, -- The product name (e.g. "SQL Server 2005")
brandedVersion = nil, -- The branded version of the product (e.g. "2005")
servicePackLevel = nil, -- The service pack level (e.g. "SP1")
patched = nil, -- Whether patches have been applied since SP installation (true/false/nil)
source = nil, -- The source of the version info (e.g. "SSRP", "SSNetLib")
new = function(self,o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end,
--- Sets the version using a version number string.
--
-- @param versionNumber a version number string (e.g. "9.00.1399.00")
-- @param source a string indicating the source of the version info (e.g. "SSRP", "SSNetLib")
SetVersionNumber = function(self, versionNumber, source)
local parts = {versionNumber:match("^(%d+)%.(%d+)%.(%d+)")}
if not parts[1] then
stdnse.debug1("%s: SetVersionNumber: versionNumber is not in correct format: %s", "MSSQL", versionNumber or "nil" )
return
end
-- If it doesn't match, subBuild will be nil
parts[4] = versionNumber:match( "^%d+%.%d+%.%d+%.(%d+)" )
parts[5] = source
self:SetVersion( table.unpack(parts) )
end,
--- Sets the version using the individual numeric components of the version
-- number.
--
-- @param source a string indicating the source of the version info (e.g. "SSRP", "SSNetLib")
SetVersion = function(self, major, minor, build, subBuild, source)
self.source = source
-- make sure our version numbers all end up as valid numbers
self.major, self.minor, self.build, self.subBuild =
tonumber( major or 0 ), tonumber( minor or 0 ), tonumber( build or 0 ), tonumber( subBuild or 0 )
self.versionNumber = string.format( "%u.%02u.%u.%02u", self.major, self.minor, self.build, self.subBuild )
self:_ParseVersionInfo()
end,
--- Using the version number, determines the product version
_InferProductVersion = function(self)
local VERSION_LOOKUP_TABLE = {
["^6%.0"] = "6.0", ["^6%.5"] = "6.5", ["^7%.0"] = "7.0",
["^8%.0"] = "2000", ["^9%.0"] = "2005", ["^10%.0"] = "2008",
["^10%.50"] = "2008 R2", ["^11%.0"] = "2012", ["^12%.0"] = "2014",
["^13%.0"] = "2016", ["^14%.0"] = "2017", ["^15%.0"] = "2019",
["^16%.0"] = "2022",
}
local product = ""
for m, v in pairs(VERSION_LOOKUP_TABLE) do
if ( self.versionNumber:match(m) ) then
product = v
self.brandedVersion = product
break
end
end
self.productName = ("Microsoft SQL Server %s"):format(product)
end,
--- Returns a lookup table that maps revision numbers to service pack and
-- cumulative update levels for the applicable SQL Server version,
-- e.g., {{1913, "RC1"}, {2100, "RTM"}, {2316, "RTMCU1"}, ...,
-- {3000, "SP1"}, {3321, "SP1CU1"}, ..., {3368, "SP1CU4"}, ...}
_GetSpLookupTable = function(self)
-- Service pack lookup tables:
-- For instances where a revised service pack was released, e.g. 2000 SP3a,
-- we will include the build number for the original SP and the build number
-- for the revision. However, leaving it like this would make it appear that
-- subsequent builds were a patched version of the revision, e.g., a patch
-- applied to 2000 SP3 that increased the build number to 780 would get
-- displayed as "SP3a+", when it was actually SP3+. To avoid this, we will
-- include an additional fake build number that combines the two.
-- Source: https://sqlserverbuilds.blogspot.com/
local SP_LOOKUP_TABLE = {
["6.5"] = {
{201, "RTM"},
{213, "SP1"},
{240, "SP2"},
{258, "SP3"},
{281, "SP4"},
{415, "SP5"},
{416, "SP5a"},
{417, "SP5/SP5a"},
},
["7.0"] = {
{623, "RTM"},
{699, "SP1"},
{842, "SP2"},
{961, "SP3"},
{1063, "SP4"},
},
["2000"] = {
{194, "RTM"},
{384, "SP1"},
{532, "SP2"},
{534, "SP2"},
{760, "SP3"},
{766, "SP3a"},
{767, "SP3/SP3a"},
{2039, "SP4"},
},
["2005"] = {
{1399, "RTM"},
{2047, "SP1"},
{3042, "SP2"},
{4035, "SP3"},
{5000, "SP4"},
},
["2008"] = {
{1600, "RTM"},
{2531, "SP1"},
{4000, "SP2"},
{5500, "SP3"},
{6000, "SP4"},
},
["2008 R2"] = {
{1600, "RTM"},
{2500, "SP1"},
{4000, "SP2"},
{6000, "SP3"},
},
["2012"] = {
{1103, "CTP1"},
{1440, "CTP3"},
{1750, "RC0"},
{1913, "RC1"},
{2100, "RTM"},
{2316, "RTMCU1"},
{2325, "RTMCU2"},
{2332, "RTMCU3"},
{2383, "RTMCU4"},
{2395, "RTMCU5"},
{2401, "RTMCU6"},
{2405, "RTMCU7"},
{2410, "RTMCU8"},
{2419, "RTMCU9"},
{2420, "RTMCU10"},
{2424, "RTMCU11"},
{3000, "SP1"},
{3321, "SP1CU1"},
{3339, "SP1CU2"},
{3349, "SP1CU3"},
{3368, "SP1CU4"},
{3373, "SP1CU5"},
{3381, "SP1CU6"},
{3393, "SP1CU7"},
{3401, "SP1CU8"},
{3412, "SP1CU9"},
{3431, "SP1CU10"},
{3449, "SP1CU11"},
{3470, "SP1CU12"},
{3482, "SP1CU13"},
{3486, "SP1CU14"},
{3487, "SP1CU15"},
{3492, "SP1CU16"},
{5058, "SP2"},
{5532, "SP2CU1"},
{5548, "SP2CU2"},
{5556, "SP2CU3"},
{5569, "SP2CU4"},
{5582, "SP2CU5"},
{5592, "SP2CU6"},
{5623, "SP2CU7"},
{5634, "SP2CU8"},
{5641, "SP2CU9"},
{5644, "SP2CU10"},
{5646, "SP2CU11"},
{5649, "SP2CU12"},
{5655, "SP2CU13"},
{5657, "SP2CU14"},
{5676, "SP2CU15"},
{5678, "SP2CU16"},
{6020, "SP3"},
{6518, "SP3CU1"},
{6523, "SP3CU2"},
{6537, "SP3CU3"},
{6540, "SP3CU4"},
{6544, "SP3CU5"},
{6567, "SP3CU6"},
{6579, "SP3CU7"},
{6594, "SP3CU8"},
{6598, "SP3CU9"},
{6607, "SP3CU10"},
{7001, "SP4"},
},
["2014"] = {
{1524, "CTP2"},
{2000, "RTM"},
{2342, "RTMCU1"},
{2370, "RTMCU2"},
{2402, "RTMCU3"},
{2430, "RTMCU4"},
{2456, "RTMCU5"},
{2480, "RTMCU6"},
{2495, "RTMCU7"},
{2546, "RTMCU8"},
{2553, "RTMCU9"},
{2556, "RTMCU10"},
{2560, "RTMCU11"},
{2564, "RTMCU12"},
{2568, "RTMCU13"},
{2569, "RTMCU14"},
{4100, "SP1"},
{4416, "SP1CU1"},
{4422, "SP1CU2"},
{4427, "SP1CU3"},
{4436, "SP1CU4"},
{4439, "SP1CU5"},
{4449, "SP1CU6"},
{4459, "SP1CU7"},
{4468, "SP1CU8"},
{4474, "SP1CU9"},
{4491, "SP1CU10"},
{4502, "SP1CU11"},
{4511, "SP1CU12"},
{4522, "SP1CU13"},
{5000, "SP2"},
{5511, "SP2CU1"},
{5522, "SP2CU2"},
{5538, "SP2CU3"},
{5540, "SP2CU4"},
{5546, "SP2CU5"},
{5553, "SP2CU6"},
{5556, "SP2CU7"},
{5557, "SP2CU8"},
{5563, "SP2CU9"},
{5571, "SP2CU10"},
{5579, "SP2CU11"},
{5589, "SP2CU12"},
{5590, "SP2CU13"},
{5600, "SP2CU14"},
{5605, "SP2CU15"},
{5626, "SP2CU16"},
{5632, "SP2CU17"},
{5687, "SP2CU18"},
{6024, "SP3"},
{6205, "SP3CU1"},
{6214, "SP3CU2"},
{6259, "SP3CU3"},
{6329, "SP3CU4"},
},
["2016"] = {
{ 200, "CTP2"},
{ 300, "CTP2.1"},
{ 407, "CTP2.2"},
{ 500, "CTP2.3"},
{ 600, "CTP2.4"},
{ 700, "CTP3.0"},
{ 800, "CTP3.1"},
{ 900, "CTP3.2"},
{1000, "CTP3.3"},
{1100, "RC0"},
{1200, "RC1"},
{1300, "RC2"},
{1400, "RC3"},
{1601, "RTM"},
{2149, "RTMCU1"},
{2164, "RTMCU2"},
{2186, "RTMCU3"},
{2193, "RTMCU4"},
{2197, "RTMCU5"},
{2204, "RTMCU6"},
{2210, "RTMCU7"},
{2213, "RTMCU8"},
{2216, "RTMCU9"},
{4001, "SP1"},
{4411, "SP1CU1"},
{4422, "SP1CU2"},
{4435, "SP1CU3"},
{4446, "SP1CU4"},
{4451, "SP1CU5"},
{4457, "SP1CU6"},
{4466, "SP1CU7"},
{4474, "SP1CU8"},
{4502, "SP1CU9"},
{4514, "SP1CU10"},
{4528, "SP1CU11"},
{4541, "SP1CU12"},
{4550, "SP1CU13"},
{4560, "SP1CU14"},
{4574, "SP1CU15"},
{5026, "SP2"},
{5149, "SP2CU1"},
{5153, "SP2CU2"},
{5216, "SP2CU3"},
{5233, "SP2CU4"},
{5264, "SP2CU5"},
{5292, "SP2CU6"},
{5337, "SP2CU7"},
{5426, "SP2CU8"},
{5479, "SP2CU9"},
{5492, "SP2CU10"},
{5598, "SP2CU11"},
{5698, "SP2CU12"},
{5820, "SP2CU13"},
{5830, "SP2CU14"},
{5850, "SP2CU15"},
{5882, "SP2CU16"},
{5888, "SP2CU17"},
{6300, "SP3"},
},
["2017"] = {
{ 1, "CTP1"},
{ 100, "CTP1.1"},
{ 200, "CTP1.2"},
{ 304, "CTP1.3"},
{ 405, "CTP1.4"},
{ 500, "CTP2.0"},
{ 600, "CTP2.1"},
{ 800, "RC1"},
{ 900, "RC2"},
{1000, "RTM"},
{3006, "CU1"},
{3008, "CU2"},
{3015, "CU3"},
{3022, "CU4"},
{3023, "CU5"},
{3025, "CU6"},
{3026, "CU7"},
{3029, "CU8"},
{3030, "CU9"},
{3037, "CU10"},
{3038, "CU11"},
{3045, "CU12"},
{3048, "CU13"},
{3076, "CU14"},
{3162, "CU15"},
{3223, "CU16"},
{3238, "CU17"},
{3257, "CU18"},
{3281, "CU19"},
{3294, "CU20"},
{3335, "CU21"},
{3356, "CU22"},
{3381, "CU23"},
{3391, "CU24"},
{3401, "CU25"},
{3411, "CU26"},
{3421, "CU27"},
{3430, "CU28"},
{3436, "CU29"},
{3451, "CU30"},
},
["2019"] = {
{1000, "CTP2.0"},
{1100, "CTP2.1"},
{1200, "CTP2.2"},
{1300, "CTP2.3"},
{1400, "CTP2.4"},
{1500, "CTP2.5"},
{1600, "CTP3.0"},
{1700, "CTP3.1"},
{1800, "CTP3.2"},
{1900, "RC1"},
{2000, "RTM"},
{2070, "GDR1"},
{4003, "CU1"},
{4013, "CU2"},
{4023, "CU3"},
{4033, "CU4"},
{4043, "CU5"},
{4053, "CU6"},
{4063, "CU7"},
{4073, "CU8"},
{4102, "CU9"},
{4123, "CU10"},
{4138, "CU11"},
{4153, "CU12"},
{4178, "CU13"},
{4188, "CU14"},
{4198, "CU15"},
{4223, "CU16"},
{4249, "CU17"},
},
["2022"] = {
{100, "CTP1.0"},
{101, "CTP1.1"},
{200, "CTP1.2"},
{300, "CTP1.3"},
{400, "CTP1.4"},
{500, "CTP1.5"},
{600, "CTP2.0"},
{700, "CTP2.1"},
{900, "RC0"},
},
}
if ( not self.brandedVersion ) then
self:_InferProductVersion()
end
local spLookupTable = SP_LOOKUP_TABLE[self.brandedVersion]
stdnse.debug1("brandedVersion: %s, #lookup: %d", self.brandedVersion, spLookupTable and #spLookupTable or 0)
return spLookupTable
end,
--- Processes version data to determine (if possible) the product version,
-- service pack level and patch status.
_ParseVersionInfo = function(self)
local spLookupTable = self:_GetSpLookupTable()
if spLookupTable then
local spLookupItr = 0
-- Loop through the service pack levels until we find one whose revision
-- number is the same as or lower than our revision number.
while spLookupItr < #spLookupTable do
spLookupItr = spLookupItr + 1
if (spLookupTable[ spLookupItr ][1] == self.build ) then
spLookupItr = spLookupItr
break
elseif (spLookupTable[ spLookupItr ][1] > self.build ) then
-- The target revision number is lower than the first release
if spLookupItr == 1 then
self.servicePackLevel = "Pre-RTM"
else
-- we went too far - it's the previous SP, but with patches applied
spLookupItr = spLookupItr - 1
end
break
end
end
-- Now that we've identified the proper service pack level:
if self.servicePackLevel ~= "Pre-RTM" then
self.servicePackLevel = spLookupTable[ spLookupItr ][2]
if ( spLookupTable[ spLookupItr ][1] == self.build ) then
self.patched = false
else
self.patched = true
end
end
-- Clean up some of our inferences. If the source of our revision number
-- was the SSRP (SQL Server Browser) response, we need to recognize its
-- limitations:
-- * Versions of SQL Server prior to 2005 are reported with the RTM build
-- number, regardless of the actual version (e.g. SQL Server 2000 is
-- always 8.00.194).
-- * Versions of SQL Server starting with 2005 (and going through at least
-- 2008) do better but are still only reported with the build number as
-- of the last service pack (e.g. SQL Server 2005 SP3 with patches is
-- still reported as 9.00.4035.00).
if ( self.source == "SSRP" ) then
self.patched = nil
if ( self.major <= 8 ) then
self.servicePackLevel = nil
end
end
end
return true
end,
---
ToString = function(self)
local friendlyVersion = strbuf.new()
if self.productName then
friendlyVersion:concatbuf( self.productName )
if self.servicePackLevel then
friendlyVersion:concatbuf( " " )
friendlyVersion:concatbuf( self.servicePackLevel )
end
if self.patched then
friendlyVersion:concatbuf( "+" )
end
end
return friendlyVersion:dump()
end,
--- Uses the information in this SqlServerVersionInformation object to
-- populate the version information in an Nmap port table for a SQL Server
-- TCP listener.
--
-- @param self A SqlServerVersionInformation object
-- @param port An Nmap port table corresponding to the instance
PopulateNmapPortVersion = function(self, port)
port.service = "ms-sql-s"
port.version = port.version or {}
port.version.name = "ms-sql-s"
port.version.product = self.productName
local versionString = strbuf.new()
if self.source ~= "SSRP" then
versionString:concatbuf( self.versionNumber )
if self.servicePackLevel then
versionString:concatbuf( "; " )
versionString:concatbuf( self.servicePackLevel )
end
if self.patched then
versionString:concatbuf( "+" )
end
port.version.version = versionString:dump()
end
return port
end,
}
-- *************************************
-- SSRP (SQL Server Resolution Protocol)
-- *************************************
SSRP =
{
PORT = { number = 1434, protocol = "udp" },
DEBUG_ID = "MSSQL-SSRP",
MESSAGE_TYPE =
{
ClientBroadcast = 0x02,
ClientUnicast = 0x03,
ClientUnicastInstance = 0x04,
ClientUnicastDAC = 0x0F,
ServerResponse = 0x05,
},
--- Parses an SSRP string and returns a table containing one or more
-- SqlServerInstanceInfo objects created from the parsed string.
_ParseSsrpString = function( host, ssrpString )
-- It would seem easier to just capture (.-;;) repeatedly, since
-- each instance ends with ";;", but ";;" can also occur within the
-- data, signifying an empty field (e.g. "...bv;;@COMPNAME;;tcp;1433;;...").
-- So, instead, we'll split up the string ahead of time.
-- See the SSRP specification for more details.
local instanceStrings = {}
local firstInstanceEnd, instanceString
repeat
firstInstanceEnd = ssrpString:find( ";ServerName;(.-);InstanceName;(.-);IsClustered;(.-);" )
if firstInstanceEnd then
instanceString = ssrpString:sub( 1, firstInstanceEnd )
ssrpString = ssrpString:sub( firstInstanceEnd + 1 )
else
instanceString = ssrpString
end
table.insert( instanceStrings, instanceString )
until (not firstInstanceEnd)
stdnse.debug2("%s: SSRP Substrings:\n %s", SSRP.DEBUG_ID, table.concat(instanceStrings , "\n ") )
local instances = {}
for _, instanceString in ipairs( instanceStrings ) do
local instance = SqlServerInstanceInfo:new()
local version = SqlServerVersionInfo:new()
instance.version = version
instance.host = host
instance.serverName = instanceString:match( "ServerName;(.-);")
instance.instanceName = instanceString:match( "InstanceName;(.-);")
instance:SetIsClustered( instanceString:match( "IsClustered;(.-);") )
version:SetVersionNumber( instanceString:match( "Version;(.-);"), "SSRP" )
local tcpPort = tonumber( instanceString:match( ";tcp;(.-);") )
if tcpPort then instance.port = {number = tcpPort, protocol = "tcp"} end
local pipeName = instanceString:match( ";np;(.-);")
local status, pipeSubPath = namedpipes.get_pipe_subpath( pipeName )
if status then
pipeName = namedpipes.make_pipe_name( host.ip, pipeSubPath )
elseif pipeName ~= nil then
stdnse.debug1("%s: Invalid pipe name:\n%s", SSRP.DEBUG_ID, pipeName )
end
instance.pipeName = pipeName
table.insert( instances, instance )
end
return instances
end,
---
_ProcessResponse = function( host, responseData )
local instances
local pos, messageType, dataLength = 1, nil, nil
messageType, dataLength, pos = string.unpack("<BI2", responseData, 1)
-- extract the response data (i.e. everything after the 3-byte header)
responseData = responseData:sub(4)
stdnse.debug2("%s: SSRP Data: %s", SSRP.DEBUG_ID, responseData )
if ( messageType ~= SSRP.MESSAGE_TYPE.ServerResponse or
dataLength ~= responseData:len() ) then
stdnse.debug2("%s: Invalid SSRP response. Type: 0x%02x, Length: %d, Actual length: %d",
SSRP.DEBUG_ID, messageType, dataLength, responseData:len() )
else
instances = SSRP._ParseSsrpString( host, responseData )
end
return instances
end,
--- Attempts to retrieve information about SQL Server instances by querying
-- the SQL Server Browser service on a host.
--
-- @param host A host table for the target host
-- @param port (Optional) A port table for the target SQL Server Browser service
-- @return (status, result) If status is true, result is a table of
-- SqlServerInstanceInfo objects. If status is false, result is an
-- error message.
DiscoverInstances = function( host, port )
port = port or SSRP.PORT
if ( SCANNED_PORTS_ONLY and nmap.get_port_state( host, port ) == nil ) then
stdnse.debug2("%s: Discovery disallowed: scanned-ports-only is set and port %d was not scanned", SSRP.DEBUG_ID, port.number )
return false, "Discovery disallowed: scanned-ports-only"
end
local socket = nmap.new_socket("udp")
socket:set_timeout(5000)
if ( port.number ~= SSRP.PORT.number ) then
stdnse.debug1("%s: DiscoverInstances() called with non-standard port (%d)", SSRP.DEBUG_ID, port.number )
end
local status, err = socket:connect( host, port )
if ( not(status) ) then return false, err end
status, err = socket:send( string.pack( "B", SSRP.MESSAGE_TYPE.ClientUnicast ) )
if ( not(status) ) then return false, err end
local responseData, instances_host
status, responseData = socket:receive()
if ( not(status) ) then return false, responseData
else
instances_host = SSRP._ProcessResponse( host, responseData )
end
socket:close()
return status, instances_host
end,
--- Attempts to retrieve information about SQL Server instances by querying
-- the SQL Server Browser service on a broadcast domain.
--
-- @param host A host table for the broadcast specification
-- @param port (Optional) A port table for the target SQL Server Browser service
-- @return (status, result) If status is true, result is a table of
-- tables containing SqlServerInstanceInfo objects. The top-level table
-- is indexed by IP address. If status is false, result is an
-- error message.
DiscoverInstances_Broadcast = function( host, port )
port = port or SSRP.PORT
local socket = nmap.new_socket("udp")
socket:set_timeout(5000)
local instances_all = {}
if ( port.number ~= SSRP.PORT.number ) then
stdnse.debug1("%S: DiscoverInstances_Broadcast() called with non-standard port (%d)", SSRP.DEBUG_ID, port.number )
end
local status, err = socket:sendto(host, port, string.pack( "B", SSRP.MESSAGE_TYPE.ClientBroadcast ))
if ( not(status) ) then return false, err end
while ( status ) do
local responseData
status, responseData = socket:receive()
if ( status ) then
local remoteIp, _
status, _, _, remoteIp, _ = socket:get_info()
local instances_host = SSRP._ProcessResponse( {ip = remoteIp, name = ""}, responseData )
instances_all[ remoteIp ] = instances_host
end
end
socket:close()
return true, instances_all
end,
}
-- *************************
-- TDS (Tabular Data Stream)
-- *************************
-- TDS packet types
PacketType =
{
Query = 0x01,
Response = 0x04,
Login = 0x10,
NTAuthentication = 0x11,
PreLogin = 0x12,
}
-- TDS response token types
TokenType =
{
ReturnStatus = 0x79,
TDS7Results = 0x81,
ErrorMessage = 0xAA,
InformationMessage = 0xAB,
LoginAcknowledgement = 0xAD,
Row = 0xD1,
OrderBy = 0xA9,
EnvironmentChange = 0xE3,
NTLMSSP_CHALLENGE = 0xed,
Done = 0xFD,
DoneProc = 0xFE,
DoneInProc = 0xFF,
}
-- SQL Server/Sybase data types
DataTypes =
{
SQLTEXT = 0x23,
GUIDTYPE = 0x24,
SYBINTN = 0x26,
SYBINT2 = 0x34,
SYBINT4 = 0x38,
SYBDATETIME = 0x3D,
NTEXTTYPE = 0x63,
BITNTYPE = 0x68,
DECIMALNTYPE = 0x6A,
NUMERICNTYPE = 0x6C,
FLTNTYPE = 0x6D,
MONEYNTYPE = 0x6E,
SYBDATETIMN = 0x6F,
XSYBVARBINARY = 0xA5,
XSYBVARCHAR = 0xA7,
BIGBINARYTYPE = 0xAD,
BIGCHARTYPE = 0xAF,
XSYBNVARCHAR = 0xE7,
SQLNCHAR = 0xEF,
}
-- SQL Server login error codes
-- See http://msdn.microsoft.com/en-us/library/ms131024.aspx
LoginErrorType =
{
AccountLockedOut = 15113,
NotAssociatedWithTrustedConnection = 18452, -- This probably means that the server is set for Windows authentication only
InvalidUsernameOrPassword = 18456,
PasswordChangeFailed_PasswordNotAllowed = 18463,
PasswordChangeFailed_PasswordTooShort = 18464,
PasswordChangeFailed_PasswordTooLong = 18465,
PasswordChangeFailed_PasswordNotComplex = 18466,
PasswordChangeFailed_PasswordFilter = 18467,
PasswordChangeFailed_UnexpectedError = 18468,
PasswordExpired = 18487,
PasswordMustChange = 18488,
}
LoginErrorMessage = {}
for i, v in pairs(LoginErrorType) do
LoginErrorMessage[v] = i
end
-- "static" ColumnInfo parser class
ColumnInfo =
{
Parse =
{
[DataTypes.SQLTEXT] = function( data, pos )
local colinfo = {}
local tmp
colinfo.unknown, colinfo.codepage, colinfo.flags, colinfo.charset, pos = string.unpack("<I4I2I2B", data, pos )
colinfo.tablenamelen, pos = string.unpack("<i2", data, pos )
colinfo.tablename, pos = string.unpack("c" .. (colinfo.tablenamelen * 2), data, pos)
colinfo.msglen, pos = string.unpack("<B", data, pos )
tmp, pos = string.unpack("c" .. (colinfo.msglen * 2), data, pos)
colinfo.text = unicode.utf16to8(tmp)
return pos, colinfo
end,
[DataTypes.GUIDTYPE] = function( data, pos )
return ColumnInfo.Parse[DataTypes.SYBINTN](data, pos)
end,
[DataTypes.SYBINTN] = function( data, pos )
local colinfo = {}
local tmp
colinfo.unknown, colinfo.msglen, pos = string.unpack("<BB", data, pos)
tmp, pos = string.unpack("c" .. (colinfo.msglen * 2), data, pos )
colinfo.text = unicode.utf16to8(tmp)
return pos, colinfo
end,
[DataTypes.SYBINT2] = function( data, pos )
return ColumnInfo.Parse[DataTypes.SYBDATETIME](data, pos)
end,
[DataTypes.SYBINT4] = function( data, pos )
return ColumnInfo.Parse[DataTypes.SYBDATETIME](data, pos)
end,
[DataTypes.SYBDATETIME] = function( data, pos )
local colinfo = {}
local tmp
colinfo.msglen, pos = string.unpack("B", data, pos)
tmp, pos = string.unpack("c" .. (colinfo.msglen * 2), data, pos )
colinfo.text = unicode.utf16to8(tmp)
return pos, colinfo
end,
[DataTypes.NTEXTTYPE] = function( data, pos )
return ColumnInfo.Parse[DataTypes.SQLTEXT](data, pos)
end,
[DataTypes.BITNTYPE] = function( data, pos )
return ColumnInfo.Parse[DataTypes.SYBINTN](data, pos)
end,
[DataTypes.DECIMALNTYPE] = function( data, pos )
local colinfo = {}
local tmp
colinfo.unknown, colinfo.precision, colinfo.scale, pos = string.unpack("<BBB", data, pos)
colinfo.msglen, pos = string.unpack("<B",data,pos)
tmp, pos = string.unpack("c" .. (colinfo.msglen * 2), data, pos )
colinfo.text = unicode.utf16to8(tmp)
return pos, colinfo
end,
[DataTypes.NUMERICNTYPE] = function( data, pos )
return ColumnInfo.Parse[DataTypes.DECIMALNTYPE](data, pos)
end,
[DataTypes.FLTNTYPE] = function( data, pos )
return ColumnInfo.Parse[DataTypes.SYBINTN](data, pos)
end,
[DataTypes.MONEYNTYPE] = function( data, pos )
return ColumnInfo.Parse[DataTypes.SYBINTN](data, pos)
end,
[DataTypes.SYBDATETIMN] = function( data, pos )
return ColumnInfo.Parse[DataTypes.SYBINTN](data, pos)
end,
[DataTypes.XSYBVARBINARY] = function( data, pos )
local colinfo = {}
local tmp
colinfo.lts, colinfo.msglen, pos = string.unpack("<I2B", data, pos)
tmp, pos = string.unpack("c" .. (colinfo.msglen * 2), data, pos )
colinfo.text = unicode.utf16to8(tmp)
return pos, colinfo
end,
[DataTypes.XSYBVARCHAR] = function( data, pos )
return ColumnInfo.Parse[DataTypes.XSYBNVARCHAR](data, pos)
end,
[DataTypes.BIGBINARYTYPE] = function( data, pos )
return ColumnInfo.Parse[DataTypes.XSYBVARBINARY](data, pos)
end,
[DataTypes.BIGCHARTYPE] = function( data, pos )
return ColumnInfo.Parse[DataTypes.XSYBNVARCHAR](data, pos)
end,
[DataTypes.XSYBNVARCHAR] = function( data, pos )
local colinfo = {}
local tmp
colinfo.lts, colinfo.codepage, colinfo.flags, colinfo.charset,
colinfo.msglen, pos = string.unpack("<I2I2I2BB", data, pos )
tmp, pos = string.unpack("c" .. (colinfo.msglen * 2), data, pos)
colinfo.text = unicode.utf16to8(tmp)
return pos, colinfo
end,
[DataTypes.SQLNCHAR] = function( data, pos )
return ColumnInfo.Parse[DataTypes.XSYBNVARCHAR](data, pos)
end,
}
}
-- "static" ColumnData parser class
ColumnData =
{
Parse = {
[DataTypes.SQLTEXT] = function( data, pos )
local len, coldata
-- The first len value is the size of the meta data block
-- for non-null values this seems to be 0x10 / 16 bytes
len, pos = string.unpack( "<B", data, pos )
if ( len == 0 ) then
return pos, 'Null'
end
-- Skip over the text update time and date values, we don't need them
-- We may come back add parsing for this information.
pos = pos + len
-- skip a label, should be 'dummyTS'
pos = pos + 8
-- extract the actual data
coldata, pos = string.unpack( "<s4", data, pos )
return pos, coldata
end,
[DataTypes.GUIDTYPE] = function( data, pos )
local len, coldata, index, nextdata
local hex = {}
len, pos = string.unpack("B", data, pos)
if ( len == 0 ) then
return pos, 'Null'
elseif ( len == 16 ) then
-- Mixed-endian; first 3 parts are little-endian, next 2 are big-endian
local A, B, C, D, E, pos = string.unpack("<I4I2I2>c2c6", data, pos)
coldata = ("%08x-%04x-%04x-%s-%s"):format(A, B, C, stdnse.tohex(D), stdnse.tohex(E))
else
stdnse.debug1("Unhandled length (%d) for GUIDTYPE", len)
return pos + len, 'Unsupported Data'
end
return pos, coldata
end,
[DataTypes.SYBINTN] = function( data, pos )
local len, num
len, pos = string.unpack("B", data, pos)
if ( len == 0 ) then
return pos, 'Null'
elseif ( len <= 16 ) then
local v, pos = string.unpack("<i" .. len, data, pos)
return pos, v
else
return -1, ("Unhandled length (%d) for SYBINTN"):format(len)
end
return -1, "Error"
end,
[DataTypes.SYBINT2] = function( data, pos )
local num
num, pos = string.unpack("<I2", data, pos)
return pos, num
end,
[DataTypes.SYBINT4] = function( data, pos )
local num
num, pos = string.unpack("<I4", data, pos)
return pos, num
end,
[DataTypes.SYBDATETIME] = function( data, pos )
local hi, lo
hi, lo, pos = string.unpack("<i4I4", data, pos)
local result_seconds = (hi*24*60*60) + (lo/300)
local result = datetime.format_timestamp(tds_offset_seconds + result_seconds)
return pos, result
end,
[DataTypes.NTEXTTYPE] = function( data, pos )
local len, coldata
-- The first len value is the size of the meta data block
len, pos = string.unpack( "<B", data, pos )
if ( len == 0 ) then
return pos, 'Null'
end
-- Skip over the text update time and date values, we don't need them
-- We may come back add parsing for this information.
pos = pos + len
-- skip a label, should be 'dummyTS'
pos = pos + 8
-- extract the actual data
coldata, pos = string.unpack( "<s4", data, pos )
return pos, unicode.utf16to8(coldata)
end,
[DataTypes.BITNTYPE] = function( data, pos )
return ColumnData.Parse[DataTypes.SYBINTN](data, pos)
end,
[DataTypes.DECIMALNTYPE] = function( precision, scale, data, pos )
local len, sign, format_string, coldata
len, pos = string.unpack("<B", data, pos)
if ( len == 0 ) then
return pos, 'Null'
end
sign, pos = string.unpack("<B", data, pos)
-- subtract 1 from data len to account for sign byte
len = len - 1
if ( len > 0 and len <= 16 ) then
coldata, pos = string.unpack("<I" .. len, data, pos)
else
stdnse.debug1("Unhandled length (%d) for DECIMALNTYPE", len)
return pos + len, 'Unsupported Data'
end
if ( sign == 0 ) then
coldata = coldata * -1
end
coldata = coldata * (10^-scale)
-- format the return information to reduce truncation by lua
format_string = string.format("%%.%if", scale)
coldata = string.format(format_string,coldata)
return pos, coldata
end,
[DataTypes.NUMERICNTYPE] = function( precision, scale, data, pos )
return ColumnData.Parse[DataTypes.DECIMALNTYPE]( precision, scale, data, pos )
end,
[DataTypes.BITNTYPE] = function( data, pos )
return ColumnData.Parse[DataTypes.SYBINTN](data, pos)
end,
[DataTypes.NTEXTTYPE] = function( data, pos )
local len, coldata
-- The first len value is the size of the meta data block
len, pos = string.unpack( "<B", data, pos )
if ( len == 0 ) then
return pos, 'Null'
end
-- Skip over the text update time and date values, we don't need them
-- We may come back add parsing for this information.
pos = pos + len
-- skip a label, should be 'dummyTS'
pos = pos + 8
-- extract the actual data
coldata, pos = string.unpack( "<s4", data, pos )
return pos, unicode.utf16to8(coldata)
end,
[DataTypes.FLTNTYPE] = function( data, pos )
local len, coldata
len, pos = string.unpack("<B", data, pos)
if ( len == 0 ) then
return pos, 'Null'
elseif ( len == 4 ) then
coldata, pos = string.unpack("<f", data, pos)
elseif ( len == 8 ) then
coldata, pos = string.unpack("<d", data, pos)
end
return pos, coldata
end,
[DataTypes.MONEYNTYPE] = function( data, pos )
local len, value, coldata, hi, lo
len, pos = string.unpack("B", data, pos)
if ( len == 0 ) then
return pos, 'Null'
elseif ( len == 4 ) then
--type smallmoney
value, pos = string.unpack("<i4", data, pos)
elseif ( len == 8 ) then
-- type money
hi, lo, pos = string.unpack("<I4I4", data, pos)
value = ( hi * 0x100000000 ) + lo
else
return -1, ("Unhandled length (%d) for MONEYNTYPE"):format(len)
end
-- the datatype allows for 4 decimal places after the period to support various currency types.
-- forcing to string to avoid truncation
coldata = string.format("%.4f",value/10000)
return pos, coldata
end,
[DataTypes.SYBDATETIMN] = function( data, pos )
local len, coldata
len, pos = string.unpack( "<B", data, pos )
if ( len == 0 ) then
return pos, 'Null'
elseif ( len == 4 ) then
-- format is smalldatetime
local days, mins
days, mins, pos = string.unpack("<I2I2", data, pos)
local result_seconds = (days*24*60*60) + (mins*60)
coldata = datetime.format_timestamp(tds_offset_seconds + result_seconds)
return pos,coldata
elseif ( len == 8 ) then
-- format is datetime
return ColumnData.Parse[DataTypes.SYBDATETIME](data, pos)
else
return -1, ("Unhandled length (%d) for SYBDATETIMN"):format(len)
end
end,
[DataTypes.XSYBVARBINARY] = function( data, pos )
local len, coldata
len, pos = string.unpack( "<I2", data, pos )
if ( len == 65535 ) then
return pos, 'Null'
else
coldata, pos = string.unpack( "c"..len, data, pos )
return pos, "0x" .. stdnse.tohex(coldata)
end
return -1, "Error"
end,
[DataTypes.XSYBVARCHAR] = function( data, pos )
local len, coldata
len, pos = string.unpack( "<I2", data, pos )
if ( len == 65535 ) then
return pos, 'Null'
end
coldata, pos = string.unpack( "c"..len, data, pos )
return pos, coldata
end,
[DataTypes.BIGBINARYTYPE] = function( data, pos )
return ColumnData.Parse[DataTypes.XSYBVARBINARY](data, pos)
end,
[DataTypes.BIGCHARTYPE] = function( data, pos )
return ColumnData.Parse[DataTypes.XSYBVARCHAR](data, pos)
end,
[DataTypes.XSYBNVARCHAR] = function( data, pos )
local len, coldata
len, pos = string.unpack( "<I2", data, pos )
if ( len == 65535 ) then
return pos, 'Null'
end
coldata, pos = string.unpack( "c"..len, data, pos )
return pos, unicode.utf16to8(coldata)
end,
[DataTypes.SQLNCHAR] = function( data, pos )
return ColumnData.Parse[DataTypes.XSYBNVARCHAR](data, pos)
end,
}
}
-- "static" Token parser class
Token =
{
Parse = {
--- Parse error message tokens
--
-- @param data string containing "raw" data
-- @param pos number containing offset into data
-- @return pos number containing new offset after parse
-- @return token table containing token specific fields
[TokenType.ErrorMessage] = function( data, pos )
local token = {}
local tmp
token.type = TokenType.ErrorMessage
token.size, token.errno, token.state, token.severity, token.errlen, pos = string.unpack( "<I2I4BBI2", data, pos )
tmp, pos = string.unpack("c" .. (token.errlen * 2), data, pos )
token.error = unicode.utf16to8(tmp)
token.srvlen, pos = string.unpack("B", data, pos)
tmp, pos = string.unpack("c" .. (token.srvlen * 2), data, pos )
token.server = unicode.utf16to8(tmp)
token.proclen, pos = string.unpack("B", data, pos)
tmp, pos = string.unpack("c" .. (token.proclen * 2), data, pos )
token.proc = unicode.utf16to8(tmp)
token.lineno, pos = string.unpack("<I2", data, pos)
return pos, token
end,
--- Parse environment change tokens
-- (This function is not implemented and simply moves the pos offset)
--
-- @param data string containing "raw" data
-- @param pos number containing offset into data
-- @return pos number containing new offset after parse
-- @return token table containing token specific fields
[TokenType.EnvironmentChange] = function( data, pos )
local token = {}
local tmp
token.type = TokenType.EnvironmentChange
token.size, pos = string.unpack("<I2", data, pos)
return pos + token.size, token
end,
--- Parse information message tokens
--
-- @param data string containing "raw" data
-- @param pos number containing offset into data
-- @return pos number containing new offset after parse
-- @return token table containing token specific fields
[TokenType.InformationMessage] = function( data, pos )
local pos, token = Token.Parse[TokenType.ErrorMessage]( data, pos )
token.type = TokenType.InformationMessage
return pos, token
end,
--- Parse login acknowledgment tokens
--
-- @param data string containing "raw" data
-- @param pos number containing offset into data
-- @return pos number containing new offset after parse
-- @return token table containing token specific fields
[TokenType.LoginAcknowledgement] = function( data, pos )
local token = {}
local _
token.type = TokenType.LoginAcknowledgement
token.size, _, _, _, _, token.textlen, pos = string.unpack( "<I2BBBI2B", data, pos )
token.text, pos = string.unpack("c" .. token.textlen * 2, data, pos)
token.version, pos = string.unpack("<I4", data, pos )
return pos, token
end,
--- Parse done tokens
--
-- @param data string containing "raw" data
-- @param pos number containing offset into data
-- @return pos number containing new offset after parse
-- @return token table containing token specific fields
[TokenType.Done] = function( data, pos )
local token = {}
token.type = TokenType.Done
token.flags, token.operation, token.rowcount, pos = string.unpack( "<I2I2I4", data, pos )
return pos, token
end,
--- Parses a DoneProc token received after executing a SP
--
-- @param data string containing "raw" data
-- @param pos number containing offset into data
-- @return pos number containing new offset after parse
-- @return token table containing token specific fields
[TokenType.DoneProc] = function( data, pos )
local token
pos, token = Token.Parse[TokenType.Done]( data, pos )
token.type = TokenType.DoneProc
return pos, token
end,
--- Parses a DoneInProc token received after executing a SP
--
-- @param data string containing "raw" data
-- @param pos number containing offset into data
-- @return pos number containing new offset after parse
-- @return token table containing token specific fields
[TokenType.DoneInProc] = function( data, pos )
local token
pos, token = Token.Parse[TokenType.Done]( data, pos )
token.type = TokenType.DoneInProc
return pos, token
end,
--- Parses a ReturnStatus token
--
-- @param data string containing "raw" data
-- @param pos number containing offset into data
-- @return pos number containing new offset after parse
-- @return token table containing token specific fields
[TokenType.ReturnStatus] = function( data, pos )
local token = {}
token.value, pos = string.unpack("<i4", data, pos)
token.type = TokenType.ReturnStatus
return pos, token
end,
--- Parses a OrderBy token
--
-- @param data string containing "raw" data
-- @param pos number containing offset into data
-- @return pos number containing new offset after parse
-- @return token table containing token specific fields
[TokenType.OrderBy] = function( data, pos )
local token = {}
token.size, pos = string.unpack("<I2", data, pos)
token.type = TokenType.OrderBy
return pos + token.size, token
end,
--- Parse TDS result tokens
--
-- @param data string containing "raw" data
-- @param pos number containing offset into data
-- @return pos number containing new offset after parse
-- @return token table containing token specific fields
[TokenType.TDS7Results] = function( data, pos )
local token = {}
local _
token.type = TokenType.TDS7Results
token.count, pos = string.unpack( "<I2", data, pos )
token.colinfo = {}
for i=1, token.count do
local colinfo = {}
local usertype, flags, ttype
usertype, flags, ttype, pos = string.unpack("<I2I2B", data, pos )
if ( not(ColumnInfo.Parse[ttype]) ) then
return -1, ("Unhandled data type: 0x%X"):format(ttype)
end
pos, colinfo = ColumnInfo.Parse[ttype]( data, pos )
colinfo.usertype = usertype
colinfo.flags = flags
colinfo.type = ttype
table.insert( token.colinfo, colinfo )
end
return pos, token
end,
[TokenType.NTLMSSP_CHALLENGE] = function(data, pos)
local len, ntlmssp, msgtype, pos = string.unpack("<I2c8I4", data, pos)
local NTLMSSP_CHALLENGE = 2
if ( ntlmssp ~= "NTLMSSP\0" or msgtype ~= NTLMSSP_CHALLENGE ) then
return -1, "Failed to process NTLMSSP Challenge"
end
local ntlm_challenge = {nonce=data:sub( 28, 35 ), type=TokenType.NTLMSSP_CHALLENGE}
pos = pos + len - 13
return pos, ntlm_challenge
end,
},
--- Parses the first token at positions pos
--
-- @param data string containing "raw" data
-- @param pos number containing offset into data
-- @return pos number containing new offset after parse or -1 on error
-- @return token table containing token specific fields or error message on error
ParseToken = function( data, pos )
local ttype
ttype, pos = string.unpack("B", data, pos)
if ( not(Token.Parse[ttype]) ) then
stdnse.debug1("%s: No parser for token type 0x%X", "MSSQL", ttype )
return -1, ("No parser for token type: 0x%X"):format( ttype )
end
return Token.Parse[ttype](data, pos)
end,
}
--- QueryPacket class
QueryPacket =
{
new = function(self,o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end,
SetQuery = function( self, query )
self.query = query
end,
--- Returns the query packet as string
--
-- @return string containing the authentication packet
ToString = function( self )
return PacketType.Query, unicode.utf8to16( self.query )
end,
}
--- PreLoginPacket class
PreLoginPacket =
{
-- TDS pre-login option types
OPTION_TYPE = {
Version = 0x00,
Encryption = 0x01,
InstOpt = 0x02,
ThreadId = 0x03,
MARS = 0x04,
Terminator = 0xFF,
},
versionInfo = nil,
_requestEncryption = 0,
_instanceName = "",
_threadId = 0, -- Dummy value; will be filled in later
_requestMars = nil,
new = function(self,o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end,
--- Sets the client version (default = 9.00.1399.00)
--
-- @param versionInfo A SqlServerVersionInfo object with the client version information
SetVersion = function(self, versionInfo)
self._versionInfo = versionInfo
end,
--- Sets whether to request encryption (default = false)
--
-- @param requestEncryption A boolean indicating whether encryption will be requested
SetRequestEncryption = function(self, requestEncryption)
if requestEncryption then
self._requestEncryption = 1
else
self._requestEncryption = 0
end
end,
--- Sets whether to request MARS support (default = undefined)
--
-- @param requestMars A boolean indicating whether MARS support will be requested
SetRequestMars = function(self, requestMars)
if requestMars then
self._requestMars = 1
else
self._requestMars = 0
end
end,
--- Sets the instance name of the target
--
-- @param instanceName A string containing the name of the instance
SetInstanceName = function(self, instanceName)
self._instanceName = instanceName or ""
end,
--- Returns the pre-login packet as a byte string
--
-- @return byte string containing the pre-login packet
ToBytes = function(self)
-- Lengths for the values of TDS pre-login option fields
local OPTION_LENGTH_CLIENT = {
[PreLoginPacket.OPTION_TYPE.Version] = 6,
[PreLoginPacket.OPTION_TYPE.Encryption] = 1,
[PreLoginPacket.OPTION_TYPE.InstOpt] = -1,
[PreLoginPacket.OPTION_TYPE.ThreadId] = 4,
[PreLoginPacket.OPTION_TYPE.MARS] = 1,
[PreLoginPacket.OPTION_TYPE.Terminator] = 0,
}
local optionLength, optionType = 0, 0
local offset = 1 -- Terminator
offset = offset + 5 -- Version
offset = offset + 5 -- Encryption
offset = offset + 5 -- InstOpt
offset = offset + 5 -- ThreadId
if self._requestMars then offset = offset + 3 end -- MARS
if not self.versionInfo then
self.versionInfo = SqlServerVersionInfo:new()
self.versionInfo:SetVersionNumber( "9.00.1399.00" )
end
optionType = PreLoginPacket.OPTION_TYPE.Version
optionLength = OPTION_LENGTH_CLIENT[ optionType ]
local data = { string.pack( ">BI2I2", optionType, offset, optionLength ) }
offset = offset + optionLength
optionType = PreLoginPacket.OPTION_TYPE.Encryption
optionLength = OPTION_LENGTH_CLIENT[ optionType ]
data[#data+1] = string.pack( ">BI2I2", optionType, offset, optionLength )
offset = offset + optionLength
optionType = PreLoginPacket.OPTION_TYPE.InstOpt
optionLength = #self._instanceName + 1 --(string length + null-terminator)
data[#data+1] = string.pack( ">BI2I2", optionType, offset, optionLength )
offset = offset + optionLength
optionType = PreLoginPacket.OPTION_TYPE.ThreadId
optionLength = OPTION_LENGTH_CLIENT[ optionType ]
data[#data+1] = string.pack( ">BI2I2", optionType, offset, optionLength )
offset = offset + optionLength
if self.requestMars then
optionType = PreLoginPacket.OPTION_TYPE.MARS
optionLength = OPTION_LENGTH_CLIENT[ optionType ]
data[#data+1] = string.pack( ">BI2I2", optionType, offset, optionLength )
offset = offset + optionLength
end
data[#data+1] = string.pack( "B", PreLoginPacket.OPTION_TYPE.Terminator )
-- Now that the pre-login headers are done, write the data
data[#data+1] = string.pack( ">BBI2I2", self.versionInfo.major, self.versionInfo.minor,
self.versionInfo.build, self.versionInfo.subBuild )
data[#data+1] = string.pack( "<BzI4", self._requestEncryption, self._instanceName, self._threadId )
if self.requestMars then
data[#data+1] = string.pack( "B", self._requestMars )
end
return PacketType.PreLogin, table.concat(data)
end,
--- Reads a byte-string and creates a PreLoginPacket object from it. This is
-- intended to handle the server's response to a pre-login request.
FromBytes = function( bytes )
local OPTION_LENGTH_SERVER = {
[PreLoginPacket.OPTION_TYPE.Version] = 6,
[PreLoginPacket.OPTION_TYPE.Encryption] = 1,
[PreLoginPacket.OPTION_TYPE.InstOpt] = -1,
[PreLoginPacket.OPTION_TYPE.ThreadId] = 0, -- According to the TDS spec, this value should be empty from the server
[PreLoginPacket.OPTION_TYPE.MARS] = 1,
[PreLoginPacket.OPTION_TYPE.Terminator] = 0,
}
local status, pos = false, 1
local preLoginPacket = PreLoginPacket:new()
while true do
local optionType, optionPos, optionLength, optionData, expectedOptionLength, _
if pos > #bytes then
stdnse.debug2("%s: Could not extract optionType.", "MSSQL" )
return false, "Invalid pre-login response"
end
optionType, pos = ("B"):unpack(bytes, pos)
if ( optionType == PreLoginPacket.OPTION_TYPE.Terminator ) then
status = true
break
end
expectedOptionLength = OPTION_LENGTH_SERVER[ optionType ]
if ( not expectedOptionLength ) then
stdnse.debug2("%s: Unrecognized pre-login option type: %s", "MSSQL", optionType )
expectedOptionLength = -1
end
if pos + 4 > #bytes + 1 then
stdnse.debug2("%s: Could not unpack optionPos and optionLength.", "MSSQL" )
return false, "Invalid pre-login response"
end
optionPos, optionLength, pos = (">I2I2"):unpack(bytes, pos)
optionPos = optionPos + 1 -- convert from 0-based index to 1-based index
if ( optionLength ~= expectedOptionLength and expectedOptionLength ~= -1 ) then
stdnse.debug2("%s: Option data is incorrect size in pre-login response. ", "MSSQL" )
stdnse.debug2("%s: (optionType: %s) (optionLength: %s)", "MSSQL", optionType, optionLength )
return false, "Invalid pre-login response"
end
optionData = bytes:sub( optionPos, optionPos + optionLength - 1 )
if #optionData ~= optionLength then
stdnse.debug2("%s: Could not read sufficient bytes from version data.", "MSSQL" )
return false, "Invalid pre-login response"
end
if ( optionType == PreLoginPacket.OPTION_TYPE.Version ) then
local major, minor, build, subBuild = (">BBI2I2"):unpack(optionData)
local version = SqlServerVersionInfo:new()
version:SetVersion( major, minor, build, subBuild, "SSNetLib" )
preLoginPacket.versionInfo = version
elseif ( optionType == PreLoginPacket.OPTION_TYPE.Encryption ) then
preLoginPacket:SetRequestEncryption( ("B"):unpack(optionData) )
elseif ( optionType == PreLoginPacket.OPTION_TYPE.InstOpt ) then
preLoginPacket:SetInstanceName( ("z"):unpack(optionData) )
elseif ( optionType == PreLoginPacket.OPTION_TYPE.ThreadId ) then
-- Do nothing. According to the TDS spec, this option is empty when sent from the server
elseif ( optionType == PreLoginPacket.OPTION_TYPE.MARS ) then
preLoginPacket:SetRequestMars( ("B"):unpack(optionData) )
end
end
return status, preLoginPacket
end,
}
--- LoginPacket class
LoginPacket =
{
-- options_1 possible values
-- 0x80 enable warning messages if SET LANGUAGE issued
-- 0x40 change to initial database must succeed
-- 0x20 enable warning messages if USE <database> issued
-- 0x10 enable BCP
-- options_2 possible values
-- 0x80 enable domain login security
-- 0x40 "USER_SERVER - reserved"
-- 0x20 user type is "DQ login"
-- 0x10 user type is "replication login"
-- 0x08 "fCacheConnect"
-- 0x04 "fTranBoundary"
-- 0x02 client is an ODBC driver
-- 0x01 change to initial language must succeed
length = 0,
version = 0x71000001, -- Version 7.1
size = 0,
cli_version = 7, -- From jTDS JDBC driver
cli_pid = 0, -- Dummy value
conn_id = 0,
options_1 = 0xa0,
options_2 = 0x03,
sqltype_flag = 0,
reserved_flag= 0,
time_zone = 0,
collation = 0,
-- Strings
client = "Nmap",
username = nil,
password = nil,
app = "Nmap NSE",
server = nil,
library = "mssql.lua",
locale = "",
database = "master", --nil,
MAC = "\x00\x00\x00\x00\x00\x00", -- should contain client MAC, jTDS uses all zeroes
new = function(self,o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end,
--- Sets the username used for authentication
--
-- @param username string containing the username to user for authentication
SetUsername = function(self, username)
self.username = username
end,
--- Sets the password used for authentication
--
-- @param password string containing the password to user for authentication
SetPassword = function(self, password)
self.password = password
end,
--- Sets the database used in authentication
--
-- @param database string containing the database name
SetDatabase = function(self, database)
self.database = database
end,
--- Sets the server's name used in authentication
--
-- @param server string containing the name or ip of the server
SetServer = function(self, server)
self.server = server
end,
SetDomain = function(self, domain)
self.domain = domain
end,
--- Returns the authentication packet as string
--
-- @return string containing the authentication packet
ToString = function(self)
local data
local offset = 86
local ntlmAuth = not(not(self.domain))
local authLen = 0
self.cli_pid = math.random(100000)
local u_client = unicode.utf8to16(self.client)
local u_app = unicode.utf8to16(self.app)
local u_server = unicode.utf8to16(self.server)
local u_library = unicode.utf8to16(self.library)
local u_locale = unicode.utf8to16(self.locale)
local u_database = unicode.utf8to16(self.database)
local u_username, uc_password
self.length = offset + #u_client + #u_app + #u_server + #u_library + #u_database
if ( ntlmAuth ) then
authLen = 32 + #self.domain
self.length = self.length + authLen
self.options_2 = self.options_2 + 0x80
else
u_username = unicode.utf8to16(self.username)
uc_password = Auth.TDS7CryptPass(self.password, unicode.utf8_dec)
self.length = self.length + #u_username + #uc_password
end
data = {
string.pack("<I4I4I4I4I4I4", self.length, self.version, self.size, self.cli_version, self.cli_pid, self.conn_id ),
string.pack("BBBB", self.options_1, self.options_2, self.sqltype_flag, self.reserved_flag ),
string.pack("<I4I4", self.time_zone, self.collation ),
-- offsets begin
string.pack("<I2I2", offset, #u_client/2 ),
}
offset = offset + #u_client
if ( not(ntlmAuth) ) then
data[#data+1] = string.pack("<I2I2", offset, #u_username/2 )
offset = offset + #u_username
data[#data+1] = string.pack("<I2I2", offset, #uc_password/2 )
offset = offset + #uc_password
else
data[#data+1] = string.pack("<I2I2", offset, 0 )
data[#data+1] = string.pack("<I2I2", offset, 0 )
end
data[#data+1] = string.pack("<I2I2", offset, #u_app/2 )
offset = offset + #u_app
data[#data+1] = string.pack("<I2I2", offset, #u_server/2 )
offset = offset + #u_server
-- Offset to unused placeholder (reserved for future use in TDS spec)
data[#data+1] = string.pack("<I2I2", 0, 0 )
data[#data+1] = string.pack("<I2I2", offset, #u_library/2 )
offset = offset + #u_library
data[#data+1] = string.pack("<I2I2", offset, #u_locale/2 )
offset = offset + #u_locale
data[#data+1] = string.pack("<I2I2", offset, #u_database/2 )
offset = offset + #u_database
-- client MAC address, hardcoded to 00:00:00:00:00:00
data[#data+1] = self.MAC
-- offset to auth info
data[#data+1] = string.pack("<I2", offset)
-- length of nt auth (should be 0 for sql auth)
data[#data+1] = string.pack("<I2", authLen)
-- next position (same as total packet length)
data[#data+1] = string.pack("<I2", self.length)
-- zero pad
data[#data+1] = string.pack("<I2", 0)
-- Auth info wide strings
data[#data+1] = u_client
if ( not(ntlmAuth) ) then
data[#data+1] = u_username
data[#data+1] = uc_password
end
data[#data+1] = u_app
data[#data+1] = u_server
data[#data+1] = u_library
data[#data+1] = u_locale
data[#data+1] = u_database
if ( ntlmAuth ) then
local NTLMSSP_NEGOTIATE = 1
local flags = 0x0000b201
local workstation = ""
data[#data+1] = "NTLMSSP\0"
data[#data+1] = string.pack("<I4I4", NTLMSSP_NEGOTIATE, flags)
data[#data+1] = string.pack("<I2I2I4", #self.domain, #self.domain, 32)
data[#data+1] = string.pack("<I2I2I4", #workstation, #workstation, 32)
data[#data+1] = self.domain:upper()
end
return PacketType.Login, table.concat(data)
end,
}
NTAuthenticationPacket = {
new = function(self, username, password, domain, nonce)
local o = {}
setmetatable(o, self)
o.username = username
o.domain = domain
o.nonce = nonce
o.password = password
self.__index = self
return o
end,
ToString = function(self)
local ntlmssp = "NTLMSSP\0"
local NTLMSSP_AUTH = 3
local domain = unicode.utf8to16(self.domain:upper())
local user = unicode.utf8to16(self.username)
local hostname, sessionkey = "", ""
local flags = 0x00008201
local ntlm_response = Auth.NtlmResponse(self.password, self.nonce)
local lm_response = Auth.LmResponse(self.password, self.nonce)
local domain_offset = 64
local username_offset = domain_offset + #domain
local lm_response_offset = username_offset + #user
local ntlm_response_offset = lm_response_offset + #lm_response
local hostname_offset = ntlm_response_offset + #ntlm_response
local sessionkey_offset = hostname_offset + #hostname
local data = ntlmssp .. string.pack("<I4I2I2I4", NTLMSSP_AUTH, #lm_response, #lm_response, lm_response_offset)
.. string.pack("<I2I2I4", #ntlm_response, #ntlm_response, ntlm_response_offset)
.. string.pack("<I2I2I4", #domain, #domain, domain_offset)
.. string.pack("<I2I2I4", #user, #user, username_offset)
.. string.pack("<I2I2I4", #hostname, #hostname, hostname_offset)
.. string.pack("<I2I2I4", #sessionkey, #sessionkey, sessionkey_offset)
.. string.pack("<I4", flags)
.. domain
.. user
.. lm_response .. ntlm_response
return PacketType.NTAuthentication, data
end,
}
-- Handles communication with SQL Server
TDSStream = {
-- Status flag constants
MESSAGE_STATUS_FLAGS = {
Normal = 0x0,
EndOfMessage = 0x1,
IgnoreThisEvent = 0x2,
ResetConnection = 0x4,
ResetConnectionSkipTran = 0x8,
},
_packetId = 0,
_pipe = nil,
_socket = nil,
_name = nil,
new = function(self,o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end,
--- Establishes a connection to the SQL server.
--
-- @param self A mssql.Helper object
-- @param instanceInfo A SqlServerInstanceInfo object for the instance to
-- connect to.
-- @param connectionPreference (Optional) A list containing one or both of
-- the strings "TCP" and "Named Pipes", indicating which transport
-- methods to try and in what order.
-- @param smbOverrides (Optional) An overrides table for calls to the <code>smb</code>
-- library (for use with named pipes).
ConnectEx = function( self, instanceInfo, connectionPreference, smbOverrides )
if ( self._socket ) then return false, "Already connected via TCP" end
if ( self._pipe ) then return false, "Already connected via named pipes" end
connectionPreference = connectionPreference or stdnse.get_script_args('mssql.protocol') or { "TCP", "Named Pipes" }
if ( connectionPreference and 'string' == type(connectionPreference) ) then
connectionPreference = { connectionPreference }
end
local status, result, connectionType, errorMessage
stdnse.debug3("%s: Connection preferences for %s: %s",
"MSSQL", instanceInfo:GetName(), table.concat(connectionPreference, ", ") )
for _, connectionType in ipairs( connectionPreference ) do
if connectionType == "TCP" then
if not ( instanceInfo.port ) then
stdnse.debug3("%s: Cannot connect to %s via TCP because port table is not set.",
"MSSQL", instanceInfo:GetName() )
result = "No TCP port for this instance"
else
status, result = self:Connect( instanceInfo.host, instanceInfo.port )
if status then return true end
end
elseif connectionType == "Named Pipes" or connectionType == "NP" then
if not ( instanceInfo.pipeName ) then
stdnse.debug3("%s: Cannot connect to %s via named pipes because pipe name is not set.",
"MSSQL", instanceInfo:GetName() )
result = "No named pipe for this instance"
else
status, result = self:ConnectToNamedPipe( instanceInfo.host, instanceInfo.pipeName, smbOverrides )
if status then return true end
end
else
stdnse.debug1("%s: Unknown connection preference: %s", "MSSQL", connectionType )
return false, ("ERROR: Unknown connection preference: %s"):format(connectionType)
end
-- Handle any error messages
if not status then
if errorMessage then
errorMessage = string.format( "%s, %s: %s", errorMessage, connectionType, result or "nil" )
else
errorMessage = string.format( "%s: %s", connectionType, result or "nil" )
end
end
end
if not errorMessage then
errorMessage = string.format( "%s: None of the preferred connection types are available for %s\\%s",
"MSSQL", instanceInfo:GetName() )
end
return false, errorMessage
end,
--- Establishes a connection to the SQL server
--
-- @param host A host table for the target host
-- @param pipePath The path to the named pipe of the target SQL Server
-- (e.g. "\MSSQL$SQLEXPRESS\sql\query"). If nil, "\sql\query\" is used.
-- @param smbOverrides (Optional) An overrides table for calls to the <code>smb</code>
-- library (for use with named pipes).
-- @return status: true on success, false on failure
-- @return error_message: an error message, or nil
ConnectToNamedPipe = function( self, host, pipePath, overrides )
if ( self._socket ) then return false, "Already connected via TCP" end
if ( SCANNED_PORTS_ONLY and smb.get_port( host ) == nil ) then
stdnse.debug2("%s: Connection disallowed: scanned-ports-only is set and no SMB port is available", "MSSQL" )
return false, "Connection disallowed: scanned-ports-only"
end
pipePath = pipePath or "\\sql\\query"
self._pipe = namedpipes.named_pipe:new()
local status, result = self._pipe:connect( host, pipePath, overrides )
if ( status ) then
self._name = self._pipe.pipe
else
self._pipe = nil
end
return status, result
end,
--- Establishes a connection to the SQL server
--
-- @param host table containing host information
-- @param port table containing port information
-- @return status true on success, false on failure
-- @return result containing error message on failure
Connect = function( self, host, port )
if ( self._pipe ) then return false, "Already connected via named pipes" end
if ( SCANNED_PORTS_ONLY and nmap.get_port_state( host, port ) == nil ) then
stdnse.debug2("%s: Connection disallowed: scanned-ports-only is set and port %d was not scanned", "MSSQL", port.number )
return false, "Connection disallowed: scanned-ports-only"
end
local status, result, lport, _
self._socket = nmap.new_socket()
-- Set the timeout to something realistic for connects
self._socket:set_timeout( 5000 )
status, result = self._socket:connect(host, port)
if ( status ) then
-- Sometimes a Query can take a long time to respond, so we set
-- the timeout to 30 seconds. This shouldn't be a problem as the
-- library attempt to decode the protocol and avoid reading past
-- the end of the input buffer. So the only time the timeout is
-- triggered is when waiting for a response to a query.
self._socket:set_timeout( MSSQL_TIMEOUT * 1000 )
status, _, lport, _, _ = self._socket:get_info()
end
if ( not(status) ) then
self._socket = nil
stdnse.debug2("%s: Socket connection failed on %s:%s", "MSSQL", host.ip, port.number )
return false, "Socket connection failed"
end
self._name = string.format( "%s:%s", host.ip, port.number )
return status, result
end,
--- Disconnects from the SQL Server
--
-- @return status true on success, false on failure
-- @return result containing error message on failure
Disconnect = function( self )
if ( self._socket ) then
local status, result = self._socket:close()
self._socket = nil
return status, result
elseif ( self._pipe ) then
local status, result = self._pipe:disconnect()
self._pipe = nil
return status, result
else
return false, "Not connected"
end
end,
--- Sets the timeout for communication over the socket
--
-- @param timeout number containing the new socket timeout in ms
SetTimeout = function( self, timeout )
if ( self._socket ) then
self._socket:set_timeout(timeout)
else
return false, "Not connected"
end
end,
--- Gets the name of the name pipe, or nil
GetNamedPipeName = function( self )
if ( self._pipe ) then
return self._pipe.name
else
return nil
end
end,
--- Send a TDS request to the server
--
-- @param packetType A <code>PacketType</code>, indicating the type of TDS
-- packet being sent.
-- @param packetData A string containing the raw data to send to the server
-- @return status true on success, false on failure
-- @return result containing error message on failure
Send = function( self, packetType, packetData )
local packetLength = packetData:len() + 8 -- +8 for TDS header
local messageStatus, spid, window = 1, 0, 0
if ( packetType ~= PacketType.NTAuthentication ) then self._packetId = self._packetId + 1 end
local assembledPacket = string.pack(">BBI2I2BB", packetType, messageStatus, packetLength, spid, self._packetId, window) .. packetData
if ( self._socket ) then
return self._socket:send( assembledPacket )
elseif ( self._pipe ) then
return self._pipe:send( assembledPacket )
else
return false, "Not connected"
end
end,
--- Receives responses from SQL Server
--
-- The function continues to read and assemble a response until the server
-- responds with the last response flag set
--
-- @return status true on success, false on failure
-- @return result containing raw data contents or error message on failure
-- @return errorDetail nil, or additional information about an error. In
-- the case of named pipes, this will be an SMB error name (e.g. NT_STATUS_PIPE_DISCONNECTED)
Receive = function( self )
local status, result, errorDetail
local combinedData, readBuffer = "", "" -- the buffer is solely for the benefit of TCP connections
local tdsPacketAvailable = true
if not ( self._socket or self._pipe ) then
return false, "Not connected"
end
-- Large messages (e.g. result sets) can be split across multiple TDS
-- packets from the server (which could themselves each be split across
-- multiple TCP packets or SMB messages).
while ( tdsPacketAvailable ) do
local packetType, messageStatus, packetLength, spid, window
local pos = 1
if ( self._socket ) then
-- If there is existing data in the readBuffer, see if there's
-- enough to read the TDS headers for the next packet. If not,
-- do another read so we have something to work with.
if ( readBuffer:len() < 8 ) then
status, result = self._socket:receive_bytes(8 - readBuffer:len())
readBuffer = readBuffer .. result
end
elseif ( self._pipe ) then
-- The named pipe takes care of all of its reassembly. We don't
-- have to mess with buffers and repeatedly reading until we get
-- the whole packet. We'll still write to readBuffer, though, so
-- that the common logic can be reused.
status, result, errorDetail = self._pipe:receive()
readBuffer = result
end
if not ( status and readBuffer ) then return false, result, errorDetail end
-- TDS packet validity check: packet at least as long as the TDS header
if ( readBuffer:len() < 8 ) then
stdnse.debug2("%s: Receiving (%s): packet is invalid length", "MSSQL", self._name )
return false, "Server returned invalid packet"
end
-- read in the TDS headers
packetType, messageStatus, packetLength, pos = string.unpack(">BBI2", readBuffer, pos )
spid, self._packetId, window, pos = string.unpack(">I2BB", readBuffer, pos )
-- TDS packet validity check: packet type is Response (0x4)
if ( packetType ~= PacketType.Response ) then
stdnse.debug2("%s: Receiving (%s): Expected type 0x4 (response), but received type 0x%x",
"MSSQL", self._name, packetType )
return false, "Server returned invalid packet"
end
if ( self._socket ) then
-- If we didn't previously read in enough data to complete this
-- TDS packet, let's do so.
while ( packetLength - readBuffer:len() > 0 ) do
status, result = self._socket:receive()
if not ( status and result ) then return false, result end
readBuffer = readBuffer .. result
end
end
-- We've read in an apparently valid TDS packet
local thisPacketData = readBuffer:sub( pos, packetLength )
-- Append its data to that of any previous TDS packets
combinedData = combinedData .. thisPacketData
if ( self._socket ) then
-- If we read in data beyond the end of this TDS packet, save it
-- so that we can use it in the next loop.
readBuffer = readBuffer:sub( packetLength + 1 )
end
-- TDS packet validity check: packet length matches length from header
if ( packetLength ~= (thisPacketData:len() + 8) ) then
stdnse.debug2("%s: Receiving (%s): Header reports length %d, actual length is %d",
"MSSQL", self._name, packetLength, thisPacketData:len() )
return false, "Server returned invalid packet"
end
-- Check the status flags in the TDS packet to see if the message is
-- continued in another TDS packet.
tdsPacketAvailable = (( messageStatus & TDSStream.MESSAGE_STATUS_FLAGS.EndOfMessage) ~=
TDSStream.MESSAGE_STATUS_FLAGS.EndOfMessage)
end
-- return only the data section ie. without the headers
return status, combinedData
end,
}
--- Helper class
Helper =
{
new = function(self,o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end,
--- Establishes a connection to the SQL server
--
-- @param host table containing host information
-- @param port table containing port information
-- @return status true on success, false on failure
-- @return result containing error message on failure
ConnectEx = function( self, instanceInfo )
local status, result
self.stream = TDSStream:new()
status, result = self.stream:ConnectEx( instanceInfo )
if ( not(status) ) then
return false, result
end
return true
end,
--- Establishes a connection to the SQL server
--
-- @param host table containing host information
-- @param port table containing port information
-- @return status true on success, false on failure
-- @return result containing error message on failure
Connect = function( self, host, port )
local status, result
self.stream = TDSStream:new()
status, result = self.stream:Connect(host, port)
if ( not(status) ) then
return false, result
end
return true
end,
--- Returns true if discovery has been performed to detect
-- SQL Server instances on the given host
WasDiscoveryPerformed = function( host )
local mutex = nmap.mutex( "discovery_performed for " .. host.ip )
mutex( "lock" )
nmap.registry.mssql = nmap.registry.mssql or {}
nmap.registry.mssql.discovery_performed = nmap.registry.mssql.discovery_performed or {}
local wasPerformed = nmap.registry.mssql.discovery_performed[ host.ip ] or false
mutex( "done" )
return wasPerformed
end,
--- Adds an instance to the list of instances kept in the Nmap registry for
-- shared use by SQL Server scripts.
--
-- If the registry already contains the instance, any new information is
-- merged into the existing instance info. This may happen, for example,
-- when an instance is discovered via named pipes, but the same instance has
-- already been discovered via SSRP; this will prevent duplicates, where
-- possible.
AddOrMergeInstance = function( newInstance )
local instanceExists
nmap.registry.mssql = nmap.registry.mssql or {}
nmap.registry.mssql.instances = nmap.registry.mssql.instances or {}
nmap.registry.mssql.instances[ newInstance.host.ip ] = nmap.registry.mssql.instances[ newInstance.host.ip ] or {}
for _, existingInstance in ipairs( nmap.registry.mssql.instances[ newInstance.host.ip ] ) do
if existingInstance == newInstance then
existingInstance:Merge( newInstance )
instanceExists = true
break
end
end
if not instanceExists then
table.insert( nmap.registry.mssql.instances[ newInstance.host.ip ], newInstance )
end
end,
--- Gets a table containing SqlServerInstanceInfo objects discovered on
-- the specified host (and port, if specified).
--
-- This table is the NSE registry table itself, not a copy, so do not alter
-- it unintentionally.
--
-- @param host A host table for the target host
-- @param port (Optional) If omitted, all of the instances for the host
-- will be returned.
-- @return A table containing SqlServerInstanceInfo objects, or nil
GetDiscoveredInstances = function( host, port )
nmap.registry.mssql = nmap.registry.mssql or {}
nmap.registry.mssql.instances = nmap.registry.mssql.instances or {}
nmap.registry.mssql.instances[ host.ip ] = nmap.registry.mssql.instances[ host.ip ] or {}
local instances = nmap.registry.mssql.instances[ host.ip ]
if ( not port ) then
if ( instances and #instances == 0 ) then instances = nil end
return instances
else
for _, instance in ipairs(instances) do
if ( instance.port and instance.port.number == port.number and
instance.port.protocol == port.protocol ) then
return { instance }
end
end
return nil
end
end,
--- Attempts to discover SQL Server instances using SSRP to query one or
-- more (if <code>broadcast</code> is used) SQL Server Browser services.
--
-- Any discovered instances are returned, as well as being stored for use
-- by other scripts (see <code>mssql.Helper.GetDiscoveredInstances()</code>).
--
-- @param host A host table for the target.
-- @param port (Optional) A port table for the target port. If this is nil,
-- the default SSRP port (UDP 1434) is used.
-- @param broadcast If true, this will be done with an SSRP broadcast, and
-- <code>host</code> should contain the broadcast specification (e.g.
-- ip = "255.255.255.255").
-- @return (status, result) If status is true, result is a table of
-- tables containing SqlServerInstanceInfo objects. The top-level table
-- is indexed by IP address. If status is false, result is an
-- error message.
DiscoverBySsrp = function( host, port, broadcast )
if broadcast then
local status, result = SSRP.DiscoverInstances_Broadcast( host, port )
if not status then
return status, result
else
for ipAddress, host in pairs( result ) do
for _, instance in ipairs( host ) do
Helper.AddOrMergeInstance( instance )
-- Give some version info back to Nmap
if ( instance.port and instance.version ) then
instance.version:PopulateNmapPortVersion( instance.port )
--nmap.set_port_version( instance.host, instance.port)
end
end
end
return true, result
end
else
local status, result = SSRP.DiscoverInstances( host, port )
if not status then
return status, result
else
for _, instance in ipairs( result ) do
Helper.AddOrMergeInstance( instance )
-- Give some version info back to Nmap
if ( instance.port and instance.version ) then
instance.version:PopulateNmapPortVersion( instance.port )
nmap.set_port_version( host, instance.port)
end
end
local instances_all = {}
instances_all[ host.ip ] = result
return true, instances_all
end
end
end,
--- Attempts to discover a SQL Server instance listening on the specified
-- port.
--
-- If an instance is discovered, it is returned, as well as being stored for
-- use by other scripts (see
-- <code>mssql.Helper.GetDiscoveredInstances()</code>).
--
-- @param host A host table for the target.
-- @param port A port table for the target port.
-- @return (status, result) If status is true, result is a table of
-- SqlServerInstanceInfo objects. If status is false, result is an
-- error message or nil.
DiscoverByTcp = function( host, port )
local version, instance, status
-- Check to see if we've already discovered an instance on this port
local instance = Helper.GetDiscoveredInstances(host, port)
if instance then
return true, {instance}
end
instance = SqlServerInstanceInfo:new()
instance.host = host
instance.port = port
-- -sV may have gotten a version, but for now, it doesn't extract subBuild.
status, version = Helper.GetInstanceVersion( instance )
if not status then
return false, version
end
Helper.AddOrMergeInstance( instance )
-- The point of this wasn't to get the version, just to use the
-- pre-login packet to determine whether there was a SQL Server on
-- the port. However, since we have the version now, we'll store it.
instance.version = version
-- Give some version info back to Nmap
if ( instance.port and instance.version ) then
instance.version:PopulateNmapPortVersion( instance.port )
nmap.set_port_version( host, instance.port)
end
return true, { instance }
end,
--- Attempts to discover SQL Server instances listening on default named
-- pipes.
--
-- Any discovered instances are returned, as well as being stored for use by
-- other scripts (see <code>mssql.Helper.GetDiscoveredInstances()</code>).
--
-- @param host A host table for the target.
-- @param port A port table for the port to connect on for SMB
-- @return (status, result) If status is true, result is a table of
-- SqlServerInstanceInfo objects. If status is false, result is an
-- error message or nil.
DiscoverBySmb = function( host, port )
local defaultPipes = {
"\\sql\\query",
"\\MSSQL$SQLEXPRESS\\sql\\query",
"\\MSSQL$SQLSERVER\\sql\\query",
}
local tdsStream = TDSStream:new()
local status, result, instances_host
for _, pipeSubPath in ipairs( defaultPipes ) do
status, result = tdsStream:ConnectToNamedPipe( host, pipeSubPath, nil )
if status then
instances_host = {}
local instance = SqlServerInstanceInfo:new()
instance.pipeName = tdsStream:GetNamedPipeName()
tdsStream:Disconnect()
instance.host = host
Helper.AddOrMergeInstance( instance )
table.insert( instances_host, instance )
else
stdnse.debug3("DiscoverBySmb \n pipe: %s\n result: %s", pipeSubPath, tostring( result ) )
end
end
return (instances_host ~= nil), instances_host
end,
--- Attempts to discover SQL Server instances by a variety of means.
--
-- This function calls the three DiscoverBy functions, which perform the
-- actual discovery. Any discovered instances can be retrieved using
-- <code>mssql.Helper.GetDiscoveredInstances()</code>.
--
-- @param host Host table as received by the script action function
Discover = function( host )
local mutex = nmap.mutex( "discovery_performed for " .. host.ip )
mutex( "lock" )
nmap.registry.mssql = nmap.registry.mssql or {}
nmap.registry.mssql.discovery_performed = nmap.registry.mssql.discovery_performed or {}
if nmap.registry.mssql.discovery_performed[ host.ip ] then
mutex "done"
return
end
nmap.registry.mssql.discovery_performed[ host.ip ] = false
-- First, do SSRP discovery. Check any open (got response) ports first:
local port = nmap.get_ports(host, nil, "udp", "open")
while port do
if port.version and port.version.name == "ms-sql-m" then
Helper.DiscoverBySsrp(host, port)
end
port = nmap.get_ports(host, port, "udp", "open")
end
-- Then check if default SSRP port hasn't been done yet.
port = nmap.get_port_state(host, SSRP.PORT)
if not port or port.state == "open|filtered" then
-- Either it wasn't scanned or it wasn't strictly "open" so we missed it above
Helper.DiscoverBySsrp(host, port)
end
-- Next, do TCP discovery. Check any ports with an appropriate service name
port = nmap.get_ports(host, nil, "tcp", "open")
while port do
if port.version and port.version.name == "ms-sql-s" then
Helper.DiscoverByTcp(host, port)
end
port = nmap.get_ports(host, port, "tcp", "open")
end
-- smb.get_port() will return nil if no SMB port was scanned OR if SMB ports were scanned but none was open
if smb.get_port(host) then
Helper.DiscoverBySmb( host )
end
-- if the user has specified ports, we'll check those too
if ( targetInstancePorts ) then
for _, portNumber in ipairs( targetInstancePorts ) do
portNumber = tonumber( portNumber )
Helper.DiscoverByTcp( host, {number = portNumber, protocol = "tcp"} )
end
end
nmap.registry.mssql.discovery_performed[ host.ip ] = true
mutex( "done" )
end,
--- Returns all of the credentials available for the target instance,
-- including any set by the <code>mssql.username</code> and <code>mssql.password</code>
-- script arguments.
--
-- @param instanceInfo A SqlServerInstanceInfo object for the target instance
-- @return A table of usernames mapped to passwords (i.e. <code>creds[ username ] = password</code>)
GetLoginCredentials_All = function( instanceInfo )
local credentials = instanceInfo.credentials or {}
local credsExist = false
for _, _ in pairs( credentials ) do
credsExist = true
break
end
if ( not credsExist ) then credentials = nil end
if ( stdnse.get_script_args( "mssql.username" ) ) then
credentials = credentials or {}
local usernameArg = stdnse.get_script_args( "mssql.username" )
local passwordArg = stdnse.get_script_args( "mssql.password" ) or ""
credentials[ usernameArg ] = passwordArg
end
return credentials
end,
--- Returns a username-password set according to the following rules of
-- precedence:
--
-- * If the <code>mssql.username</code> and <code>mssql.password</code>
-- script arguments were set, their values are used. (If the username
-- argument was specified without the password argument, a blank
-- password is used.)
-- * If the password for the "sa" account has been discovered (e.g. by the
-- <code>ms-sql-empty-password</code> or <code>ms-sql-brute</code>
-- scripts), these credentials are used.
-- * If other credentials have been discovered, the first of these in the
-- table are used.
-- * Otherwise, nil is returned.
--
-- @param instanceInfo A SqlServerInstanceInfo object for the target instance
-- @return (username, password)
GetLoginCredentials = function( instanceInfo )
-- First preference goes to any user-specified credentials
local username = stdnse.get_script_args( "mssql.username" )
local password = stdnse.get_script_args( "mssql.password" ) or ""
-- Otherwise, use any valid credentials that have been discovered (e.g. by ms-sql-brute)
if ( not(username) and instanceInfo.credentials ) then
-- Second preference goes to the "sa" account
if ( instanceInfo.credentials.sa ) then
username = "sa"
password = instanceInfo.credentials.sa
else
-- ok were stuck with some n00b account, just get the first one
for user, pass in pairs( instanceInfo.credentials ) do
username = user
password = pass
break
end
end
end
return username, password
end,
--- Disconnects from the SQL Server
--
-- @return status true on success, false on failure
-- @return result containing error message on failure
Disconnect = function( self )
if ( not(self.stream) ) then
return false, "Not connected to server"
end
self.stream:Disconnect()
self.stream = nil
return true
end,
--- Authenticates to SQL Server.
--
-- If login fails, one of the following error messages will be returned:
-- * "Password is expired"
-- * "Must change password at next logon"
-- * "Account is locked out"
-- * "Login Failed"
--
-- @param username string containing the username for authentication
-- @param password string containing the password for authentication
-- @param database string containing the database to access
-- @param servername string containing the name or ip of the remote server
-- @return status true on success, false on failure
-- @return result containing error message on failure
-- @return errorDetail nil or a <code>LoginErrorType</code> value, if available
Login = function( self, username, password, database, servername )
local loginPacket = LoginPacket:new()
local status, result, data, errorDetail, token
local servername = servername or "DUMMY"
local pos = 1
local ntlmAuth = false
if ( not self.stream ) then
return false, "Not connected to server"
end
loginPacket:SetUsername(username)
loginPacket:SetPassword(password)
loginPacket:SetDatabase(database)
loginPacket:SetServer(servername)
local domain = stdnse.get_script_args("mssql.domain")
if (domain) then
if ( not(HAVE_SSL) ) then return false, "mssql: OpenSSL not present" end
ntlmAuth = true
-- if the domain was specified without an argument, set a default domain of "."
if (domain == 1 or domain == true ) then
domain = "."
end
loginPacket:SetDomain(domain)
end
status, result = self.stream:Send( loginPacket:ToString() )
if ( not(status) ) then
return false, result
end
status, data, errorDetail = self.stream:Receive()
if ( not(status) ) then
-- When logging in via named pipes, SQL Server will sometimes
-- disconnect the pipe if the login attempt failed (this only seems
-- to happen with non-"sa") accounts. At this point, having
-- successfully connected and sent a message, we can be reasonably
-- comfortable that a disconnected pipe indicates a failed login.
if ( errorDetail == "NT_STATUS_PIPE_DISCONNECTED" ) then
return false, "Bad username or password", LoginErrorType.InvalidUsernameOrPassword
end
return false, data
end
local doNTLM = ntlmAuth
while( pos < data:len() ) do
pos, token = Token.ParseToken( data, pos )
if ( -1 == pos ) then
return false, token
end
if ( token.type == TokenType.ErrorMessage ) then
local errorMessageLookup = {
[LoginErrorType.AccountLockedOut] = "Account is locked out",
[LoginErrorType.NotAssociatedWithTrustedConnection] = "User is not associated with a trusted connection (instance may allow Windows authentication only)",
[LoginErrorType.InvalidUsernameOrPassword] = "Bad username or password",
[LoginErrorType.PasswordExpired] = "Password is expired",
[LoginErrorType.PasswordMustChange] = "Must change password at next logon",
}
local errorMessage = errorMessageLookup[ token.errno ] or string.format( "Login Failed (%s)", tostring(token.errno) )
return false, errorMessage, token.errno
elseif ( token.type == TokenType.LoginAcknowledgement ) then
return true, "Login Success"
elseif doNTLM and token.type == TokenType.NTLMSSP_CHALLENGE then
local authpacket = NTAuthenticationPacket:new( username, password, domain, token.nonce )
status, result = self.stream:Send( authpacket:ToString() )
status, data = self.stream:Receive()
if not status then
return false, data
end
doNTLM = false -- don't try again.
else
local found, ttype = tableaux.contains(TokenType, token.type)
if found then
stdnse.debug2("Unexpected token type: %s", ttype)
else
stdnse.debug2("Unknown token type: 0x%02x", token.type)
end
end
end
return false, "Failed to process login response"
end,
--- Authenticates to SQL Server, using the credentials returned by
-- Helper.GetLoginCredentials().
--
-- If the login is rejected by the server, the error code will be returned,
-- as a number in the form of a <code>mssql.LoginErrorType</code> (for which
-- error messages can be looked up in <code>mssql.LoginErrorMessage</code>).
--
-- @param instanceInfo a SqlServerInstanceInfo object for the instance to log into
-- @param database string containing the database to access
-- @param servername string containing the name or ip of the remote server
-- @return status true on success, false on failure
-- @return result containing error code or error message
LoginEx = function( self, instanceInfo, database, servername )
local servername = servername or instanceInfo.host.ip
local username, password = Helper.GetLoginCredentials( instanceInfo )
if ( not username ) then
return false, "No login credentials"
end
return self:Login( username, password, database, servername )
end,
--- Performs a SQL query and parses the response
--
-- @param query string containing the SQL query
-- @return status true on success, false on failure
-- @return table containing a table of columns for each row
-- or error message on failure
Query = function( self, query )
local queryPacket = QueryPacket:new()
local status, result, data, token, colinfo, rows
local pos = 1
if ( nil == self.stream ) then
return false, "Not connected to server"
end
queryPacket:SetQuery( query )
status, result = self.stream:Send( queryPacket:ToString() )
if ( not(status) ) then
return false, result
end
status, data = self.stream:Receive()
if ( not(status) ) then
return false, data
end
-- Iterate over tokens until we get to a rowtag
while( pos < data:len() ) do
local rowtag = string.unpack("B", data, pos)
if ( rowtag == TokenType.Row ) then
break
end
pos, token = Token.ParseToken( data, pos )
if ( -1 == pos ) then
return false, token
end
if ( token.type == TokenType.ErrorMessage ) then
return false, token.error
elseif ( token.type == TokenType.TDS7Results ) then
colinfo = token.colinfo
end
end
rows = {}
while(true) do
local rowtag
rowtag, pos = string.unpack("B", data, pos )
if ( rowtag ~= TokenType.Row ) then
break
end
if ( rowtag == TokenType.Row and colinfo and #colinfo > 0 ) then
local columns = {}
for i=1, #colinfo do
local val
if ( ColumnData.Parse[colinfo[i].type] ) then
if not ( colinfo[i].type == 106 or colinfo[i].type == 108) then
pos, val = ColumnData.Parse[colinfo[i].type](data, pos)
else
-- decimal / numeric types need precision and scale passed.
pos, val = ColumnData.Parse[colinfo[i].type]( colinfo[i].precision, colinfo[i].scale, data, pos)
end
if ( -1 == pos ) then
return false, val
end
table.insert(columns, val)
else
return false, ("unknown datatype=0x%X"):format(colinfo[i].type)
end
end
table.insert(rows, columns)
end
end
result = {}
result.rows = rows
result.colinfo = colinfo
return true, result
end,
--- Attempts to connect to a SQL Server instance listening on a TCP port in
-- order to determine the version of the SSNetLib DLL, which is an
-- authoritative version number for the SQL Server instance itself.
--
-- @param instanceInfo An instance of SqlServerInstanceInfo
-- @return status true on success, false on failure
-- @return versionInfo an instance of mssql.SqlServerVersionInfo, or nil
GetInstanceVersion = function( instanceInfo )
if ( not instanceInfo.host or not (instanceInfo:HasNetworkProtocols()) ) then return false, nil end
local status, response, version
local tdsStream = TDSStream:new()
status, response = tdsStream:ConnectEx( instanceInfo )
if ( not status ) then
stdnse.debug2("%s: Connection to %s failed: %s", "MSSQL", instanceInfo:GetName(), response or "" )
return false, "Connect failed"
end
local preLoginRequest = PreLoginPacket:new()
preLoginRequest:SetInstanceName( instanceInfo.instanceName )
tdsStream:SetTimeout( 5000 )
tdsStream:Send( preLoginRequest:ToBytes() )
-- read in any response we might get
status, response = tdsStream:Receive()
tdsStream:Disconnect()
if status then
local preLoginResponse
status, preLoginResponse = PreLoginPacket.FromBytes( response )
if status then
version = preLoginResponse.versionInfo
else
stdnse.debug2("%s: Parsing of pre-login packet from %s failed: %s",
"MSSQL", instanceInfo:GetName(), preLoginResponse or "" )
return false, "Parsing failed"
end
else
stdnse.debug2("%s: Receive for %s failed: %s", "MSSQL", instanceInfo:GetName(), response or "" )
return false, "Receive failed"
end
return status, version
end,
--- Gets a table containing SqlServerInstanceInfo objects for the instances
-- that should be run against, based on the script-args (e.g. <code>mssql.instance</code>)
--
-- @param host Host table as received by the script action function
-- @param port (Optional) Port table as received by the script action function
-- @return status True on success, false on failure
-- @return instances If status is true, this will be a table with one or
-- more SqlServerInstanceInfo objects. If status is false, this will be
-- an error message.
GetTargetInstances = function( host, port )
-- Perform discovery. This won't do anything if it's already been done.
-- It's important because otherwise we might miss some ports when not using -sV
Helper.Discover( host )
if ( port ) then
local status, instances = Helper.GetDiscoveredInstances(host, port)
if status then
return true, instances
else
return false, "No SQL Server instance detected on this port"
end
else
if ( targetAllInstances and ( targetInstanceNames or targetInstancePorts ) ) then
return false, "All instances cannot be specified together with an instance name or port."
end
if ( not (targetInstanceNames or targetInstancePorts or targetAllInstances) ) then
return false, "No instance(s) specified."
end
local instanceList = Helper.GetDiscoveredInstances( host )
if ( not instanceList ) then
return false, "No instances found on target host"
end
local targetInstances = {}
for _, instance in ipairs( instanceList ) do
repeat -- just so we can use break
if instance.port then
local scanport = nmap.get_port_state(host, instance.port)
-- If scanned-ports-only and it's on a non-scanned port
if (SCANNED_PORTS_ONLY and not scanport)
-- or if a portrule script will run on it
or (scanport and scanport.state == "open") then
break -- not interested
end
-- If they want everything
if targetAllInstances or
-- or if it's in the instance-port arg
(targetInstancePorts and
tableaux.contains(targetInstancePorts, instance.port.number)) then
-- keep it and move on
targetInstances[#targetInstances+1] = instance
break
end
end
-- If they want everything
if targetAllInstances or
-- or if it's in the instance-name arg
(instance.instanceName and targetInstanceNames and
tableaux.contains(targetInstanceNames, string.upper(instance.instanceName))) then
--keep it and move on
targetInstances[#targetInstances+1] = instance
break
end
until false
end
if ( #targetInstances > 0 ) then
return true, targetInstances
else
return false, "Specified instance(s) not found on target host"
end
end
end,
--- Queries the SQL Browser service for the DAC port of the specified instance
--
-- The DAC (Dedicated Admin Connection) port allows DBA's to connect to
-- the database when normal connection attempts fail, for example, when
-- the server is hanging, out of memory or other bad states.
--
-- @param instance the <code>SqlServerInstanceInfo</code> object to probe for a DAC port
-- @return number containing the DAC port on success or nil on failure
DiscoverDACPort = function(instance)
local instanceName = instance.instanceName or instance.pipeName
if not instanceName then
return nil
end
local socket = nmap.new_socket("udp")
socket:set_timeout(5000)
if ( not(socket:connect(instance.host, 1434, "udp")) ) then
return false, "Failed to connect to sqlbrowser service"
end
if ( not(socket:send(string.pack("c2z", "\x0F\x01", instanceName))) ) then
socket:close()
return false, "Failed to send request to sqlbrowser service"
end
local status, data = socket:receive_buf(match.numbytes(6), true)
socket:close()
if ( not(status) ) then
return nil
end
if ( #data < 6 ) then
return nil
end
return string.unpack("<I2", data, 5)
end,
--- Returns an action, portrule, and hostrule for standard SQL Server scripts
--
-- The action function performs discovery if necessary and dispatches the
-- process_instance function on all discovered instances.
--
-- The portrule returns true if the port has been identified as "ms-sql-s" or
-- discovery has found an instance on that port.
--
-- The hostrule returns true if any of the <code>mssql.instance-*</code>
-- script-args has been set and either a matching instance exists or
-- discovery has not yet been done.
-- @usage action, portrule, hostrule = mssql.Helper.InitScript(do_something)
--
-- @param process_instance A function that takes a single parameter, a
-- <code>SqlServerInstanceInfo</code> object, and
-- returns output suitable for an action function to
-- return.
--
-- @return An action function
-- @return A portrule function
-- @return A hostrule function
InitScript = function(process_instance)
local action = function(host, port)
local status, instances = Helper.GetTargetInstances(host, port)
if not status then
stdnse.debug1("GetTargetInstances: %s", instances)
return nil
end
local output = {}
for _, instance in ipairs(instances) do
output[instance:GetName()] = process_instance(instance)
end
if #output > 0 then
return outlib.sorted_by_key(output)
end
return nil
end
-- GetTargetInstances does the right thing depending on whether port is
-- provided, which corresponds to portrule vs hostrule.
return action, Helper.GetTargetInstances, Helper.GetTargetInstances
end,
}
local TDS7Crypt_enc = function (cp)
local c = cp ~ 0x5a5a
local m1= ( c >> 4 ) & 0x0F0F
local m2= ( c << 4 ) & 0xF0F0
return string.pack("<I2", m1 | m2 )
end
Auth = {
--- Encrypts a password using the TDS7 *ultra secure* XOR encryption
--
-- @param password string containing the password to encrypt
-- @param decoder a unicode.lua decoder function to convert password to code points
-- @return string containing the encrypted password
TDS7CryptPass = function(password, decoder)
return unicode.transcode(password, decoder, TDS7Crypt_enc)
end,
LmResponse = function( password, nonce )
if ( not(HAVE_SSL) ) then
stdnse.debug1("ERROR: Nmap is missing OpenSSL")
return
end
password = password .. string.rep('\0', 14 - #password)
password = password:upper()
-- Take the first and second half of the password (note that if it's longer than 14 characters, it's truncated)
local str1 = string.sub(password, 1, 7)
local str2 = string.sub(password, 8, 14)
-- Generate the keys
local key1 = openssl.DES_string_to_key(str1)
local key2 = openssl.DES_string_to_key(str2)
local result = openssl.encrypt("DES", key1, nil, nonce) .. openssl.encrypt("DES", key2, nil, nonce)
result = result .. string.rep('\0', 21 - #result)
str1 = string.sub(result, 1, 7)
str2 = string.sub(result, 8, 14)
local str3 = string.sub(result, 15, 21)
key1 = openssl.DES_string_to_key(str1)
key2 = openssl.DES_string_to_key(str2)
local key3 = openssl.DES_string_to_key(str3)
result = openssl.encrypt("DES", key1, nil, nonce) .. openssl.encrypt("DES", key2, nil, nonce) .. openssl.encrypt("DES", key3, nil, nonce)
return result
end,
NtlmResponse = function( password, nonce )
local lm_response, ntlm_response, mac_key = smbauth.get_password_response(nil,
nil,
nil,
password,
nil,
"v1",
nonce,
false
)
return ntlm_response
end,
}
--- "static" Utility class containing mostly conversion functions
Util =
{
--- Takes a table as returned by Query and does some fancy formatting
-- better suitable for <code>stdnse.format_output</code>
--
-- @param tbl as received by <code>Helper.Query</code>
-- @param with_headers boolean true if output should contain column headers
-- @return table suitable for <code>stdnse.format_output</code>
FormatOutputTable = function ( tbl, with_headers )
local new_tbl = {}
local col_names = {}
if ( not(tbl) ) then
return
end
if ( with_headers and tbl.rows and #tbl.rows > 0 ) then
local headers
for k, v in pairs( tbl.colinfo ) do
table.insert( col_names, v.text)
end
headers = table.concat(col_names, "\t")
table.insert( new_tbl, headers)
headers = headers:gsub("[^%s]", "=")
table.insert( new_tbl, headers )
end
for _, v in ipairs( tbl.rows ) do
table.insert( new_tbl, table.concat(v, "\t") )
end
return new_tbl
end,
}
local unittest = require "unittest"
if not unittest.testing() then
return _ENV
end
local tests = {
{"host", "\x23\xa5\x53\xa5\x92\xa5\xe2\xa5", unicode.utf8_dec},
{"p@ssword12-", "\xa2\xa5\xa1\xa5\x92\xa5\x92\xa5\xd2\xa5\x53\xa5\x82\xa5\xe3\xa5\xb6\xa5\x86\xa5\x77\xa5", unicode.utf8_dec},
}
test_suite = unittest.TestSuite:new()
for _, test in ipairs(tests) do
test_suite:add_test(unittest.equal(Auth.TDS7CryptPass(test[1], test[3]), test[2]), ("TDS7 crypt %s"):format(test[1]))
end
return _ENV
|