GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — git/Invoke-Portscan.ps1 at master · sinmygit/git Expand file tree/ Copy pathInvoke-Portscan.ps1More file actions 1109 lines (894 loc) · 41.8 KB / Copy pathInvoke-Portscan.ps1File metadata and controls1109 lines (894 loc) · 41.8 KB 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 function Invoke-Portscan { <# .SYNOPSIS Simple portscan module PowerSploit Function: Invoke-Portscan Author: Rich Lundeen (http://webstersProdigy.net) License: BSD 3-Clause Required Dependencies: None Optional Dependencies: None .DESCRIPTION Does a simple port scan using regular sockets, based (pretty) loosely on nmap .NOTES version .13 .PARAMETER Hosts Include these comma seperated hosts (supports IPv4 CIDR notation) or pipe them in .PARAMETER HostFile Input hosts from file rather than commandline .PARAMETER ExcludeHosts Exclude these comma seperated hosts .PARAMETER Ports Include these comma seperated ports (can also be a range like 80-90) .PARAMETER PortFile Input ports from a file .PARAMETER TopPorts Include the x top ports - only goes to 1000, default is top 50 .PARAMETER ExcludedPorts Exclude these comma seperated ports .PARAMETER Open Only show hosts with open ports. .PARAMETER SkipDiscovery Treat all hosts as online, skip host discovery .PARAMETER PingOnly Ping scan only (disable port scan) .PARAMETER DiscoveryPorts Comma separated ports used for host discovery. -1 is a ping .PARAMETER Threads number of max threads for the thread pool (per host) .PARAMETER nHosts number of hosts to concurrently scan .PARAMETER Timeout Timeout time on a connection in miliseconds before port is declared filtered .PARAMETER SleepTimer Wait before thread checking, in miliseconds .PARAMETER SyncFreq How often (in terms of hosts) to sync threads and flush output .PARAMETER T [0-5] shortcut performance options. Default is 3. higher is more aggressive. Sets (nhosts, threads,timeout) 5 {$nHosts=30; $Threads = 1000; $Timeout = 750 } 4 {$nHosts=25; $Threads = 1000; $Timeout = 1200 } 3 {$nHosts=20; $Threads = 100; $Timeout = 2500 } 2 {$nHosts=15; $Threads = 32; $Timeout = 3000 } 1 {$nHosts=10; $Threads = 32; $Timeout = 5000 } .PARAMETER GrepOut Greppable output file .PARAMETER XmlOut output XML file .PARAMETER ReadableOut output file in 'readable' format .PARAMETER AllformatsOut output in readable (.nmap), xml (.xml), and greppable (.gnmap) formats .PARAMETER noProgressMeter Suppresses the progress meter .PARAMETER quiet supresses returned output and don't store hosts in memory - useful for very large scans .PARAMETER ForceOverwrite Force Overwrite if output Files exist. Otherwise it throws exception .EXAMPLE C:\PS> Invoke-Portscan -Hosts "webstersprodigy.net,google.com,microsoft.com" -TopPorts 50 Description ----------- Scans the top 50 ports for hosts found for webstersprodigy.net,google.com, and microsoft.com .EXAMPLE C:\PS> echo webstersprodigy.net | Invoke-Portscan -oG test.gnmap -f -ports "80,443,8080" Description ----------- Does a portscan of "webstersprodigy.net", and writes a greppable output file .EXAMPLE C:\PS> Invoke-Portscan -Hosts 192.168.1.1/24 -T 4 -TopPorts 25 -oA localnet Description ----------- Scans the top 20 ports for hosts found in the 192.168.1.1/24 range, outputs all file formats .LINK http://webstersprodigy.net #> [CmdletBinding()]Param ( #Host, Ports [Parameter(ParameterSetName="cmdHosts", ValueFromPipeline=$True, Mandatory = $True)] [String[]] $Hosts, [Parameter(ParameterSetName="fHosts", Mandatory = $True)] [Alias("iL")] [String] $HostFile, [Parameter(Mandatory = $False)] [Alias("exclude")] [String] $ExcludeHosts, [Parameter(Mandatory = $False)] [Alias("p")] [String] $Ports, [Parameter(Mandatory = $False)] [Alias("iP")] [String] $PortFile, [Parameter(Mandatory = $False)] [String] $TopPorts, [Parameter(Mandatory = $False)] [Alias("xPorts")] [String] $ExcludedPorts, [Parameter(Mandatory = $False)] [Switch] $Open, #Host Discovery [Parameter(Mandatory = $False)] [Alias("Pn")] [Switch] $SkipDiscovery, [Parameter(Mandatory = $False)] [Alias("sn")] [Switch] $PingOnly, [Parameter(Mandatory = $False)] [Alias("PS")] [string] $DiscoveryPorts = "-1,445,80,443", #Timing and Performance [Parameter(Mandatory = $False)] [int] $Threads = 100, [Parameter(Mandatory = $False)] [int] $nHosts = 25, [Parameter(Mandatory = $False)] [int] $Timeout = 2000, [Parameter(Mandatory = $False)] [int] $SleepTimer = 500, [Parameter(Mandatory = $False)] [int] $SyncFreq = 1024, [Parameter(Mandatory = $False)] [int] $T, #Output [Parameter(Mandatory = $False)] [Alias("oG")] [String] $GrepOut, [Parameter(Mandatory = $False)] [Alias("oX")] [String] $XmlOut, [Parameter(Mandatory = $False)] [Alias("oN")] [String] $ReadableOut, [Parameter(Mandatory = $False)] [Alias("oA")] [String] $AllformatsOut, [Parameter(Mandatory = $False)] [Switch] $noProgressMeter, [Parameter(Mandatory = $False)] [Alias("q")] [Switch] $quiet, [Parameter(Mandatory = $False)] [Alias("F")] [Switch] $ForceOverwrite #TODO add script parameter #TODO add resume parameter ) PROCESS { Set-StrictMode -Version 2.0 $version = .13 $hostList = New-Object System.Collections.ArrayList $portList = New-Object System.Collections.ArrayList $hostPortList = New-Object System.Collections.ArrayList $scannedHostList = @() function Parse-Hosts { Param ( [Parameter(Mandatory = $True)] [String] $Hosts ) [String[]] $iHosts = $Hosts.Split(",") foreach($iHost in $iHosts) { $iHost = $iHost.Replace(" ", "") if(!$iHost) { continue } if($iHost.contains("/")) { $netPart = $iHost.split("/")[0] [uint32]$maskPart = $iHost.split("/")[1] $address = [System.Net.IPAddress]::Parse($netPart) if ($maskPart -ge $address.GetAddressBytes().Length * 8) { throw "Bad host mask" } $numhosts = [System.math]::Pow(2,(($address.GetAddressBytes().Length *8) - $maskPart)) $startaddress = $address.GetAddressBytes() [array]::Reverse($startaddress) $startaddress = [System.BitConverter]::ToUInt32($startaddress, 0) [uint32]$startMask = ([System.math]::Pow(2, $maskPart)-1) * ([System.Math]::Pow(2,(32 - $maskPart))) $startAddress = $startAddress -band $startMask #in powershell 2.0 there are 4 0 bytes padded, so the [0..3] is necessary $startAddress = [System.BitConverter]::GetBytes($startaddress)[0..3] [array]::Reverse($startaddress) $address = [System.Net.IPAddress] [byte[]] $startAddress $hostList.Add($address.IPAddressToString) for ($i=0; $i -lt $numhosts-1; $i++) { $nextAddress = $address.GetAddressBytes() [array]::Reverse($nextAddress) $nextAddress = [System.BitConverter]::ToUInt32($nextAddress, 0) $nextAddress ++ $nextAddress = [System.BitConverter]::GetBytes($nextAddress)[0..3] [array]::Reverse($nextAddress) $address = [System.Net.IPAddress] [byte[]] $nextAddress $hostList.Add($address.IPAddressToString) } } else { $hostList.Add($iHost) } } } function Parse-ILHosts { Param ( [Parameter(Mandatory = $True)] [String] $HostFile ) Get-Content $HostFile | ForEach-Object { Parse-Hosts $_ } } function Exclude-Hosts { Param ( [Parameter(Mandatory = $True)] [String] $excludeHosts ) [String[]] $iHosts = $excludeHosts.Split(",") foreach($iHost in $iHosts) { $iHost = $iHost.Replace(" ", "") $hostList.Remove($iHost) } } function Get-TopPort { Param ( [Parameter(Mandatory = $True)] [ValidateRange(1,1000)] [int] $numPorts ) #list of top 1000 ports from nmap from Jun 2013 [int[]] $topPortList = @(80,23,443,21,3389,110,445,139,143,53,135,3306,8080,22 1723,111,995,993,5900,1025,1720,548,541,514,,8443,9443,10443,901,902, 389,636,113,81,6001,179,1026,2000, 8000,32768,554,26,1433,49152,2001,515,8008,49154,1027,5666,646,5000, 5631,631,49153,8081,2049,88,79,5800,106,2121,1110,49155,6000,513, 990,5357,49156,543,544,5101,144,7,8009,9999,5009,7070,5190,3000, 5432,1900,3986,13,1029,9,5051,6646,49157,1028,873,1755,2717,4899,9100, 119,37,1000,3001,5001,82,10010,1030,9090,2107,1024,2103,6004,1801, 5050,19,8031,1041,255,1048,1049,1053,1054,1056,1064,3703,17,808,3689, 1031,1044,1071,5901,100,9102,2869,4001,5120,8010,9000,2105,636,1038, 2601,1,7000,1066,1069,625,311,280,254,4000,1761,5003,2002,1998,2005, 1032,1050,6112,1521,2161,6002,2401,902,4045,787,7937,1058,2383,1033, 1040,1059,50000,5555,1494,3,593,2301,3268,7938,1022,1234,1035,1036,1037, 1074,8002,9001,464,497,1935,2003,6666,6543,24,1352,3269,1111,407,500, 20,2006,1034,1218,3260,15000,4444,264,33,2004,1042,42510,999,3052,1023, 222,1068,888,7100,1717,992,2008,7001,2007,8082,512,1043,2009,5801,1700, 7019,50001,4662,2065,42,2602,3333,9535,5100,2604,4002,5002,1047,1051,1052, 1055,1060,1062,1311,3283,4443,5225,5226,6059,6789,8089,8651,8652,8701,9415, 9593,9594,9595,16992,16993,20828,23502,32769,33354,35500,52869,55555,55600, 64623,64680,65000,65389,1067,13782,366,5902,9050,85,1002,5500,1863,1864, 5431,8085,10243,45100,49999,51103,49,90,6667,1503,6881,27000,340,1500,8021, 2222,5566,8088,8899,9071,5102,6005,9101,163,5679,146,648,1666,83,3476,5004, 5214,8001,8083,8084,9207,14238,30,912,12345,2030,2605,6,541,4,1248,3005,8007, 306,880,2500,1086,1088,2525,4242,8291,9009,52822,900,6101,2809,7200,211,800, 987,1083,12000,705,711,20005,6969,13783,1045,1046,1061,1063,1070,1072,1073, 1075,1077,1078,1079,1081,1082,1085,1093,1094,1096,1098,1099,1100,1104,1106, 1107,1108,1148,1169,1272,1310,1687,1718,1783,1840,2100,2119,2135,2144,2160, 2190,2260,2381,2399,2492,2607,2718,2811,2875,3017,3031,3071,3211,3300,3301, 3323,3325,3351,3404,3551,3580,3659,3766,3784,3801,3827,3998,4003,4126,4129, 4449,5222,5269,5633,5718,5810,5825,5877,5910,5911,5925,5959,5960,5961,5962, 5987,5988,5989,6123,6129,6156,6389,6580,6901,7106,7625,7777,7778,7911,8086, 8181,8222,8333,8400,8402,8600,8649,8873,8994,9002,9011,9080,9220,9290,9485, 9500,9502,9503,9618,9900,9968,10002,10012,10024,10025,10566,10616,10617,10621, 10626,10628,10629,11110,13456,14442,15002,15003,15660,16001,16016,16018,17988, 19101,19801,19842,20000,20031,20221,20222,21571,22939,24800,25734,27715,28201, 30000,30718,31038,32781,32782,33899,34571,34572,34573,40193,48080,49158,49159, 49160,50003,50006,50800,57294,58080,60020,63331,65129,691,212,1001,1999,2020, 2998,6003,7002,50002,32,2033,3372,99,425,749,5903,43,458,5405,6106,6502,7007, 13722,1087,1089,1124,1152,1183,1186,1247,1296,1334,1580,1782,2126,2179,2191,2251, 2522,3011,3030,3077,3261,3493,3546,3737,3828,3871,3880,3918,3995,4006,4111,4446, 5054,5200,5280,5298,5822,5859,5904,5915,5922,5963,7103,7402,7435,7443,7512,8011, 8090,8100,8180,8254,8500,8654,9091,9110,9666,9877,9943,9944,9998,10004,10778,15742, 16012,18988,19283,19315,19780,24444,27352,27353,27355,32784,49163,49165,49175, 50389,50636,51493,55055,56738,61532,61900,62078,1021,9040,666,700,84,545,1112, 1524,2040,4321,5802,38292,49400,1084,1600,2048,2111,3006,6547,6699,9111,16080, 555,667,720,801,1443,1533,2106,5560,6007,1090,1091,1114,1117,1119,1122,1131,1138, 1151,1175,1199,1201,1271,1862,2323,2393,2394,2608,2725,2909,3003,3168,3221,3322, 3324,3390,3517,3527,3800,3809,3814,3826,3869,3878,3889,3905,3914,3920,3945,3971, 4004,4005,4279,4445,4550,4567,4848,4900,5033,5080,5087,5221,5440,5544,5678,5730, 5811,5815,5850,5862,5906,5907,5950,5952,6025,6510,6565,6567,6689,6692,6779,6792, 6839,7025,7496,7676,7800,7920,7921,7999,8022,8042,8045,8093,8099,8200,8290,8292, 8300,8383,9003,9081,9099,9200,9418,9575,9878,9898,9917,10003,10180,10215,11111, 12174,12265,14441,15004,16000,16113,17877,18040,18101,19350,25735,26214,27356, 30951,32783,32785,40911,41511,44176,44501,49161,49167,49176,50300,50500,52673, 52848,54045,54328,55056,56737,57797,60443,70,417,714,722,777,981,1009,2022,4224, 4998,6346,301,524,668,765,2041,5999,10082,259,1007,1417,1434,1984,2038,2068,4343, 6009,7004,44443,109,687,726,911,1461,2035,4125,6006,7201,9103,125,481,683,903, 1011,1455,2013,2043,2047,6668,6669,256,406,843,2042,2045,5998,9929,31337,44442, 1092,1095,1102,1105,1113,1121,1123,1126,1130,1132,1137,1141,1145,1147,1149,1154, 1164,1165,1166,1174,1185,1187,1192,1198,1213,1216,1217,1233,1236,1244,1259,1277, 1287,1300,1301,1309,1322,1328,1556,1641,1688,1719,1721,1805,1812,1839,1875,1914, 1971,1972,1974,2099,2170,2196,2200,2288,2366,2382,2557,2800,2910,2920,2968,3007, 3013,3050,3119,3304,3307,3376,3400,3410,3514,3684,3697,3700,3824,3846,3848,3859, 3863,3870,3872,3888,3907,3916,3931,3941,3957,3963,3968,3969,3972,3990,3993,3994, 4009,4040,4080,4096,4143,4147,4200,4252,4430,4555,4600,4658,4875,4949,5040,5063, 5074,5151,5212,5223,5242,5279,5339,5353,5501,5807,5812,5818,5823,5868,5869,5899, 5905,5909,5914,5918,5938,5940,5968,5981,6051,6060,6068,6203,6247,6500,6504,6520, 6550,6600) $numPorts-- $portList.AddRange($topPortList[0..$numPorts]) } function Parse-Ports { Param ( [Parameter(Mandatory = $True)] [String] $Ports, [Parameter(Mandatory = $True)] $pList ) foreach ($pRange in $Ports.Split(",")) { #-1 is a special case for ping if ($pRange -eq "-1") { $pList.Add([int]$pRange) } elseif ($pRange.Contains("-")) { [int[]] $range = $pRange.Split("-") if ($range.Count -ne 2 -or $pRange.Split("-")[0] -eq "" -or $pRange.split("-")[1] -eq "") { throw "Invalid port range" } $pList.AddRange($range[0]..$range[1]) } else { $pList.Add([int]$pRange) } } foreach ($p in $pList) { if ($p -lt -1 -or $p -gt 65535) { throw "Port $p out of range" } } } function Parse-IpPorts { Param ( [Parameter(Mandatory = $True)] [String] $PortFile ) Get-Content $PortFile | ForEach-Object { Parse-Ports -Ports $_ -pList $portList } } function Remove-Ports { Param ( [Parameter(Mandatory = $True)] [string] $ExcludedPorts ) [int[]] $ExcludedPorts = $ExcludedPorts.Split(",") foreach ($x in $ExcludedPorts) { $portList.Remove($x) } } function Write-PortscanOut { Param ( [Parameter(Mandatory = $True, ParameterSetName="Comment")] [string] $comment, [Parameter(Mandatory = $True, ParameterSetName="HostOut")] [string] $outhost, [Parameter(Mandatory = $True, ParameterSetName="HostOut")] [bool] $isUp, [Parameter(Mandatory = $True, ParameterSetName="HostOut")] $openPorts, [Parameter(Mandatory = $True, ParameterSetName="HostOut")] $closedPorts, [Parameter(Mandatory = $True, ParameterSetName="HostOut")] $filteredPorts, [Parameter()] [bool] $SkipDiscovery, [Parameter()] [System.IO.StreamWriter] $grepStream, [Parameter()] [System.Xml.XmlWriter] $xmlStream, [Parameter()] [System.IO.StreamWriter] $readableStream ) switch ($PSCmdlet.ParameterSetName) { "Comment" { Write-Verbose $comment if ($grepStream) { $grepStream.WriteLine("# " + $comment) } if ($xmlStream) { $xmlStream.WriteComment($comment) } if ($readableStream) { $readableStream.WriteLine($comment) } } "HostOut" { $oPort = [string]::join(",", $openPorts.ToArray()) $cPort = [string]::join(",", $closedPorts.ToArray()) $fPort = [string]::join(",", $filteredPorts.ToArray()) if ($grepStream) { #for grepstream use tabs - can be ugly, but easier for regex if ($isUp -and !$SkipDiscovery) { $grepStream.writeline("Host: $outhost`tStatus: Up") } if ($isUp -or $SkipDiscovery) { if ($oPort -ne "") { $grepStream.writeline("Host: $outhost`tOpen Ports: $oPort") } if ($cPort -ne "") { $grepStream.writeline("Host: $outhost`tClosed Ports: $cPort") } if ($fPort -ne "") { $grepStream.writeline("Host: $outhost`tFiltered Ports: $fPort") } } elseif (!$SkipDiscovery) { $grepStream.writeline("Host: $outhost`tStatus: Down") } } if ($xmlStream) { $xmlStream.WriteStartElement("Host") $xmlStream.WriteAttributeString("id", $outhost) if (!$SkipDiscovery) { if ($isUp) { $xmlStream.WriteAttributeString("Status", "Up") } else { $xmlStream.WriteAttributeString("Status", "Downs") } } $xmlStream.WriteStartElement("Ports") foreach($p in $openPorts) { $xmlStream.writestartElement("Port") $xmlStream.WriteAttributeString("id", [string]$p) $xmlStream.WriteAttributeString("state", "open") $xmlStream.WriteEndElement() } foreach ($p in $closedPorts) { $xmlStream.writestartElement("Port") $xmlStream.WriteAttributeString("id", [string]$p) $xmlStream.WriteAttributeString("state", "closed") $xmlStream.WriteEndElement() } foreach ($p in $filteredPorts) { $xmlStream.writestartElement("Port") $xmlStream.WriteAttributeString("id", [string]$p) $xmlStream.WriteAttributeString("state", "filtered") $xmlStream.WriteEndElement() } $xmlStream.WriteEndElement() $xmlStream.WriteEndElement() } if ($readableStream) { $readableStream.writeline("Porscan.ps1 scan report for $outhost") if ($isUp) { $readableStream.writeline("Host is up") } if ($isUp -or $SkipDiscovery) { $readableStream.writeline(("{0,-10}{1,0}" -f "PORT", "STATE")) [int[]]$allports = $openPorts + $closedPorts + $filteredPorts foreach($p in ($allports| Sort-Object)) { if ($openPorts.Contains($p)) { $readableStream.writeline(("{0,-10}{1,0}" -f $p, "open")) } elseif ($closedPorts.Contains($p)) { $readableStream.writeline(("{0,-10}{1,0}" -f $p, "closed")) } elseif ($filteredPorts.Contains($p)) { $readableStream.writeline(("{0,-10}{1,0}" -f $p, "filtered")) } } } elseif(!$SkipDiscovery) { $readableStream.writeline("Host is Down") } $readableStream.writeline("") } } } } #function for Powershell v2.0 to work function Convert-SwitchtoBool { Param ( [Parameter(Mandatory = $True)] $switchValue ) If ($switchValue) { return $True } return $False } try { [bool] $SkipDiscovery = Convert-SwitchtoBool ($SkipDiscovery) [bool] $PingOnly = Convert-SwitchtoBool ($PingOnly) [bool] $quiet = Convert-SwitchtoBool ($quiet) [bool] $ForceOverwrite = Convert-SwitchtoBool ($ForceOverwrite) ######### #parse arguments ######### [Environment]::CurrentDirectory=(Get-Location -PSProvider FileSystem).ProviderPath if ($PsCmdlet.ParameterSetName -eq "cmdHosts") { foreach($h in $Hosts) { Parse-Hosts($h) | Out-Null } } else { Parse-ILHosts($HostFile) | Out-Null } if($ExcludeHosts) { Exclude-Hosts($ExcludeHosts) } if (($TopPorts -and $Ports) -or ($TopPorts -and $PortFile)) { throw "Cannot set topPorts with other specific ports" } if($Ports) { Parse-Ports -Ports $Ports -pList $portList | Out-Null } if($PortFile) { Parse-IpPorts($PortFile) | Out-Null } if($portList.Count -eq 0) { if ($TopPorts) { Get-TopPort($TopPorts) | Out-Null } else { #if the ports still aren't set, give the deftault, top 50 ports Get-TopPort(50) | Out-Null } } if ($ExcludedPorts) { Remove-Ports -ExcludedPorts $ExcludedPorts | Out-Null } if($T) { switch ($T) { 5 {$nHosts=30; $Threads = 1000; $Timeout = 750 } 4 {$nHosts=25; $Threads = 1000; $Timeout = 1200 } 3 {$nHosts=20; $Threads = 100; $Timeout = 2500 } 2 {$nHosts=15; $Threads = 32; $Timeout = 3000 } 1 {$nHosts=10; $Threads = 32; $Timeout = 5000 } default { throw "Invalid T parameter" } } } $grepStream = $null $xmlStream = $null $readableStream = $null if($AllformatsOut) { if ($GrepOut -or $XmlOut -or $ReadableOut) { Write-Warning "Both -oA specified with other output... going to ignore -oG/-oN/-oX" } $GrepOut = $AllformatsOut + ".gnmap" $XmlOut = $AllformatsOut + ".xml" $ReadableOut = $AllformatsOut + ".nmap" } if ($GrepOut) { if (!$ForceOverwrite -and (Test-Path $GrepOut)) { throw "Error: $AllformatsOut already exists. Either delete the file or specify the -f flag" } $grepStream = [System.IO.StreamWriter] $GrepOut } if ($ReadableOut) { if (!$ForceOverwrite -and (Test-Path $ReadableOut)) { throw "Error: $ReadableOut already exists. Either delete the file or specify the -f flag" } $readableStream = [System.IO.StreamWriter] $ReadableOut } if ($XmlOut) { if (!$ForceOverwrite -and (Test-Path $XmlOut)) { throw "Error: $XmlOut already exists. Either delete the file or specify the -f flag" } $xmlStream = [System.xml.xmlwriter]::Create([string]$XmlOut) $xmlStream.WriteStartDocument() $xmlStream.WriteStartElement("Portscanrun") $xmlStream.WriteAttributeString("version", $version) } Parse-Ports -Ports $DiscoveryPorts -pList $hostPortList | Out-Null $startdate = Get-Date $myInvocationLine = $PSCmdlet.MyInvocation.Line $startMsg = "Invoke-Portscan.ps1 v$version scan initiated $startdate as: $myInvocationLine" #TODO deal with output Write-PortscanOut -comment $startMsg -grepStream $grepStream -xmlStream $xmlStream -readableStream $readableStream #converting back from int array gives some argument error checking $sPortList = [string]::join(",", $portList) $sHostPortList = [string]::join(",", $hostPortList) ######## #Port Scan Code - run on a per host basis ######## $portScanCode = { param ( [Parameter( Mandatory = $True)] [string] $thost, [Parameter( Mandatory = $True)][bool] $SkipDiscovery, [Parameter( Mandatory = $True)][bool] $PingOnly, [Parameter( Mandatory = $True)][int] $Timeout, [Parameter( Mandatory = $True)] $PortList, [Parameter( Mandatory = $True)] $hostPortList, [Parameter( Mandatory = $True)][int] $maxthreads) Process { $openPorts = New-Object System.Collections.ArrayList $closedPorts = New-Object System.Collections.ArrayList $filteredPorts = New-Object System.Collections.ArrayList $sockets = @{} $timeouts = New-Object Hashtable #set maximum $async threads $fThreads = New-Object int $aThreads = New-Object int [System.Threading.ThreadPool]::GetMaxThreads([ref]$fThreads, [ref]$aThreads) | Out-Null [System.Threading.ThreadPool]::SetMaxThreads($fthreads,$maxthreads) | Out-Null function New-ScriptBlockCallback { param( [parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [scriptblock]$Callback ) #taken from http://www.nivot.org/blog/post/2009/10/09/PowerShell20AsynchronousCallbacksFromNET if (-not ("CallbackEventBridge" -as [type])) { Add-Type @" using System; public sealed class CallbackEventBridge { public event AsyncCallback CallbackComplete = delegate { }; private CallbackEventBridge() {} private void CallbackInternal(IAsyncResult result) { CallbackComplete(result); } public AsyncCallback Callback { get { return new AsyncCallback(CallbackInternal); } } public static CallbackEventBridge Create() { return new CallbackEventBridge(); } } "@ } $bridge = [CallbackEventBridge]::Create() Register-ObjectEvent -InputObject $bridge -EventName CallbackComplete -Action $Callback | Out-Null $bridge.Callback } function Test-Port { Param ( [Parameter(Mandatory = $True)] [String] $h, [Parameter(Mandatory = $True)] [int] $p, [Parameter(Mandatory = $True)] [int] $timeout ) try { $pAddress = [System.Net.IPAddress]::Parse($h) $sockets[$p] = new-object System.Net.Sockets.TcpClient $pAddress.AddressFamily } catch { #we're assuming this is a host name $sockets[$p] = new-object System.Net.Sockets.TcpClient } $scriptBlockAsString = @" #somewhat of a race condition with the timeout, but I don't think it matters if ( `$sockets[$p] -ne `$NULL) { if (!`$timeouts[$p].Disposed) { `$timeouts[$p].Dispose() } `$status = `$sockets[$p].Connected; if (`$status -eq `$True) { #write-host "$p is open" `$openPorts.Add($p) } else { #write-host "$p is closed" `$closedPorts.Add($p) } `$sockets[$p].Close(); `$sockets.Remove($p) } "@ $timeoutCallback = @" #write-host "$p is filtered" `$sockets[$p].Close() if (!`$timeouts[$p].Disposed) { `$timeouts[$p].Dispose() `$filteredPorts.Add($p) } `$sockets.Remove($p) "@ $timeoutCallback = [scriptblock]::Create($timeoutCallback) $timeouts[$p] = New-Object System.Timers.Timer Register-ObjectEvent -InputObject $timeouts[$p] -EventName Elapsed -Action $timeoutCallback | Out-Null $timeouts[$p].Interval = $timeout $timeouts[$p].Enabled = $true $myscriptblock = [scriptblock]::Create($scriptBlockAsString) $x = $sockets[$p].beginConnect($h, $p,(New-ScriptBlockCallback($myscriptblock)) , $null) } function PortScan-Alive { Param ( [Parameter(Mandatory = $True)] [String] $h ) Try { #ping if ($hostPortList.Contains(-1)) { $ping = new-object System.Net.NetworkInformation.Ping $pResult = $ping.send($h) if ($pResult.Status -eq "Success") { return $True } } foreach($Port in $hostPortList) { if ($Port -ne -1) { Test-Port -h $h -p $Port -timeout $Timeout } } do { Start-Sleep -Milli 100 if (($openPorts.Count -gt 0) -or ($closedPorts.Count -gt 0)) { return $True } } While ($sockets.Count -gt 0) } Catch { Write-Error "Exception trying to host scan $h" Write-Error $_.Exception.Message; } return $False } function Portscan-Port { Param ( [Parameter(Mandatory = $True)] [String] $h ) [string[]]$Ports = @() foreach($Port in $Portlist) { Try { Test-Port -h $h -p $Port -timeout $Timeout } Catch { Write-Error "Exception trying to scan $h port $Port" Write-Error $_.Exception.Message; } } } [bool] $hostResult = $False if(!$SkipDiscovery) { [bool] $hostResult = PortScan-Alive $thost $openPorts.clear() $closedPorts.clear() $filteredPorts.Clear() } if((!$PingOnly) -and ($hostResult -or $SkipDiscovery)) { Portscan-Port $thost } while ($sockets.Count -gt 0) { Start-Sleep -Milli 500 } return @($hostResult, $openPorts, $closedPorts, $filteredPorts) } } # the outer loop is to flush the loop. # Otherwise Get-Job | Wait-Job could clog, etc [int]$saveIteration = 0 [int]$computersDone=0 [int]$upHosts=0 while (($saveIteration * $SyncFreq) -lt $hostList.Count) { Get-Job | Remove-Job -Force |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |