Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1.  
  2. function Invoke-PortScan {
  3. <#
  4. .SYNOPSIS
  5. Nihsang payload which Scan IP-Addresses, Ports and HostNames
  6.  
  7. .DESCRIPTION
  8. Scan for IP-Addresses, HostNames and open Ports in your Network.
  9.  
  10. .PARAMETER StartAddress
  11. StartAddress Range
  12.  
  13. .PARAMETER EndAddress
  14. EndAddress Range
  15.  
  16. .PARAMETER ResolveHost
  17. Resolve HostName
  18.  
  19. .PARAMETER ScanPort
  20. Perform a PortScan
  21.  
  22. .PARAMETER Ports
  23. Ports That should be scanned, default values are: 21,22,23,53,69,71,80,98,110,139,111,
  24. 389,443,445,1080,1433,2001,2049,3001,3128,5222,6667,6868,7777,7878,8080,1521,3306,3389,
  25. 5801,5900,5555,5901
  26.  
  27. .PARAMETER TimeOut
  28. Time (in MilliSeconds) before TimeOut, Default set to 100
  29.  
  30. .EXAMPLE
  31. PS > Invoke-PortScan -StartAddress 192.168.0.1 -EndAddress 192.168.0.254
  32.  
  33. .EXAMPLE
  34. PS > Invoke-PortScan -StartAddress 192.168.0.1 -EndAddress 192.168.0.254 -ResolveHost
  35.  
  36. .EXAMPLE
  37. PS > Invoke-PortScan -StartAddress 192.168.0.1 -EndAddress 192.168.0.254 -ResolveHost -ScanPort
  38. Use above to do a port scan on default ports.
  39.  
  40. .EXAMPLE
  41. PS > Invoke-PortScan -StartAddress 192.168.0.1 -EndAddress 192.168.0.254 -ResolveHost -ScanPort -TimeOut 500
  42.  
  43. .EXAMPLE
  44. PS > Invoke-PortScan -StartAddress 192.168.0.1 -EndAddress 192.168.10.254 -ResolveHost -ScanPort -Port 80
  45.  
  46. .LINK
  47. http://www.truesec.com
  48. http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/02/use-powershell-for-network-host-and-port-discovery-sweeps.aspx
  49. https://github.com/samratashok/nishang
  50.  
  51. .NOTES
  52. Goude 2012, TrueSec
  53. #>
  54. [CmdletBinding()] Param(
  55. [parameter(Mandatory = $true, Position = 0)]
  56. [ValidatePattern("\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")]
  57. [string]
  58. $StartAddress,
  59.  
  60. [parameter(Mandatory = $true, Position = 1)]
  61. [ValidatePattern("\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")]
  62. [string]
  63. $EndAddress,
  64.  
  65. [switch]
  66. $ResolveHost,
  67.  
  68. [switch]
  69. $ScanPort,
  70.  
  71. [int[]]
  72. $Ports = @(21,22,23,53,69,71,80,98,110,139,111,389,443,445,1080,1433,2001,2049,3001,3128,5222,6667,6868,7777,7878,8080,1521,3306,3389,5801,5900,5555,5901),
  73.  
  74. [int]
  75. $TimeOut = 100
  76. )
  77. Begin {
  78. $ping = New-Object System.Net.Networkinformation.Ping
  79. }
  80. Process {
  81. foreach($a in ($StartAddress.Split(".")[0]..$EndAddress.Split(".")[0])) {
  82. foreach($b in ($StartAddress.Split(".")[1]..$EndAddress.Split(".")[1])) {
  83. foreach($c in ($StartAddress.Split(".")[2]..$EndAddress.Split(".")[2])) {
  84. foreach($d in ($StartAddress.Split(".")[3]..$EndAddress.Split(".")[3])) {
  85. write-progress -activity PingSweep -status "$a.$b.$c.$d" -percentcomplete (($d/($EndAddress.Split(".")[3])) * 100)
  86. $pingStatus = $ping.Send("$a.$b.$c.$d",$TimeOut)
  87.  
  88. if($ResolveHost) {
  89. write-progress -activity ResolveHost -status "$a.$b.$c.$d" -percentcomplete (($d/($EndAddress.Split(".")[3])) * 100) -Id 1
  90. $getHostEntry = [Net.DNS]::BeginGetHostEntry("$a.$b.$c.$d", $null, $null)
  91. }
  92. if($ScanPort) {
  93. $openPorts = @()
  94. for($i = 1; $i -le $ports.Count;$i++) {
  95. $port = $Ports[($i-1)]
  96. write-progress -activity PortScan -status "$a.$b.$c.$d" -percentcomplete (($i/($Ports.Count)) * 100) -Id 2
  97. $client = New-Object System.Net.Sockets.TcpClient
  98. $beginConnect = $client.BeginConnect("$a.$b.$c.$d",$port,$null,$null)
  99. if($client.Connected) {
  100. $openPorts += $port
  101. } else {
  102. # Wait
  103. Start-Sleep -Milli $TimeOut
  104. if($client.Connected) {
  105. $openPorts += $port
  106. }
  107.  
  108. $client.Close()
  109. }
  110. }
  111. if($ResolveHost) {
  112. $hostName = ([Net.DNS]::EndGetHostEntry([IAsyncResult]$getHostEntry)).HostName
  113. }
  114. # Return Object
  115. New-Object PSObject -Property @{
  116. IPAddress = "$a.$b.$c.$d";
  117. HostName = $hostName;
  118. Ports = $openPorts
  119. Status= $pingStatus.Status
  120. } | Select-Object IPAddress, HostName, Ports, Status
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. End {
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement