PowerShell批量掃描IP和端口

前面的文章中曾經發布了對指定IP進行批量端口掃描的方法和腳本,過PowerShell收發TCP和UDP消息包的方法以及經過PowerShell嘗試登陸SQLServer服務的方法,這構成了PSNet程序集用於經過PowerShell對網絡狀態進行操做。最近在不斷嘗試之下,找到了對指定範圍的IP段進行掃描和對端口進行掃描的方法,本文將會介紹如何經過PowerShell批量掃描IP及其對應的端口。shell

依然在PSNet程序集的基礎上進行擴展,首先在$env:PSSpace/PSNet/TCPOp下建立腳本文件Invoke-ScanIPPort.ps1,並在$env:PSSpace/PSNet/TCPOp/PSNet.psm1中添加對腳本文件的調用:網絡

. $env:PSSpace/PSNet/TCPOp/Invoke-ScanIPPort.ps1ide

首先對後面代碼中將會出現的變量進行介紹:
-StartAddress[掃描的起始IP地址],與-EndAddress配合使用,【此參數必須】
-EndAddress[掃描的結束IP地址],【此參數必須】
-ResolveHost[是否嘗試對主機名嘗試進行解析]
-ScanPort[是否進行端口掃描],若是要掃描端口此選項必須
-AllPort[是否對全部端口進行掃描],範圍爲1~65534(注意此選項掃描時間很長建議在選中單個IP的狀況下進行使用,而且儘可能少使用)
-StartPort[掃描的起始端口端口],與-EndPort配合使用,若是此選項與-Ports選項同時存在則-Port參數失效
-EndPort[掃描的結束端口]
-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若是後續帶多個以逗號分割的多個數字則會掃描數字對應的端口,若是隻掃描默認的端口,則不需此參數
-TimeOut超時時間,默認值爲100ms(毫秒)函數

此函數的調用方式以下:
Invoke-ScanIPPort -StartAddress 192.168.10.1 -EndAddress 192.168.10.254#掃描IP段
Invoke-ScanIPPort -StartAddress 192.168.10.1 -EndAddress 192.168.10.254 –ResolveHost#掃描IP段,並嘗試解析IP對應主機名
Invoke-ScanIPPort -StartAddress 192.168.10.1 -EndAddress 192.168.10.254 -ResolveHost –ScanPort#掃描IP段,並嘗試掃描默認端口
Invoke-ScanIPPort -StartAddress 192.168.10.1 -EndAddress 192.168.10.254 -ResolveHost -ScanPort -TimeOut 50 #掃描IP段,嘗試掃描默認端口,端口掃描50ms超時
Invoke-ScanIPPort -StartAddress 192.168.10.1 -EndAddress 192.168.10.254 -ResolveHost -ScanPort -Port 80 #掃描IP段,並嘗試掃描80端口
Invoke-ScanIPPort -StartAddress 192.168.10.1 -EndAddress 192.168.10.1 -ResolveHost -ScanPort –AllPort#掃描ip,並嘗試掃描全部1~65534之間端口
Invoke-ScanIPPort -StartAddress 192.168.10.1 -EndAddress 192.168.10.254 -ScanPort -StarPort 21 -EndPort 81#掃描IP段之間主機全部21至81之間的端口網站

上圖來一張掃描過程當中的圖片
invoke-scanipport spa

掃描結束後的結果:orm

invoke-scanipport1
代碼以下:blog

        =====文件名:Invoke-ScanIPPort.ps1=====
function Invoke-ScanIPPort {
  Param(
    [parameter(Mandatory = $true,
      Position = 0)]
    [ValidatePattern("\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")]
    [string]$StartAddress,
    [parameter(Mandatory = $true,
      Position = 1)]
    [ValidatePattern("\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")]
    [string]$EndAddress,
    [switch]$ResolveHost,
    [switch]$ScanPort,
    [switch]$AllPort,
    [int]$StartPort,
    [int]$EndPort,
    [int[]]$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),
    [int]$TimeOut = 100
  )
  Begin {
    $ping = New-Object System.Net.Networkinformation.Ping
  }
  Process {
    foreach($a in ($StartAddress.Split(".")[0]..$EndAddress.Split(".")[0])) {
      foreach($b in ($StartAddress.Split(".")[1]..$EndAddress.Split(".")[1])) {
        foreach($c in ($StartAddress.Split(".")[2]..$EndAddress.Split(".")[2])) {
          foreach($d in ($StartAddress.Split(".")[3]..$EndAddress.Split(".")[3])) {
            $ip = "$a.$b.$c.$d"
            write-progress -activity "ScanIP Ping" -status "$ip" -percentcomplete (($d/($EndAddress.Split(".")[3])) * 100)            
            $pingStatus = $ping.Send("$ip",$TimeOut)
            if($pingStatus.Status -eq "Success") {
              if($ResolveHost) {
                write-progress -activity ResolveHost -status "$ip" -percentcomplete (($d/($EndAddress.Split(".")[3])) * 100) -Id 1
                $getHostEntry = [Net.DNS]::BeginGetHostEntry($pingStatus.Address, $null, $null)
              }
              if($ScanPort) {
                if($AllPort) {
                    $Ports = @(1..65534)
                }
                if($StartPort -ne $null -and $EndPort -ne $null){
                    $Ports = @($StartPort..$EndPort)
                }
                $openPorts = @()
                for($i = 1; $i -le $Ports.Count;$i++) {
                  $port = $Ports[($i-1)]
                  write-progress -activity "PortScan[$port]$result" -status "$ip" -percentcomplete (($i/($Ports.Count)) * 100) -Id 2
                  $client = New-Object System.Net.Sockets.TcpClient
                  $beginConnect = $client.BeginConnect($pingStatus.Address,$port,$null,$null)
                  if($client.Connected) {
                    $openPorts += $port
                  } else {
                    # Wait
                    Start-Sleep -Milli $TimeOut
                    if($client.Connected) {
                      $openPorts += $port
                      $length=$openPorts.length
                      $result="[find $length ports.Last port $port]"
                    }
                  }
                  $client.Close()
                }
              }
              if($ResolveHost) {
                $hostName = ([Net.DNS]::EndGetHostEntry([IAsyncResult]$getHostEntry)).HostName
              }
              # Return Object
              if ($openPorts -ne $null)
              {
              write-host "IPAddress" "$ip"
              if ($getHostEntry -ne $null)
              {write-host "HostName" $getHostEntry}
              write-host "Ports" $openPorts 
              }
           }
          }
        }
      }
    }
  }
  End {
  }
}

 

做者: 付海軍
出處:http://fuhj02.blog.51cto.com
版權:本文版權歸做者和51cto共有
轉載:歡迎轉載,爲了保存做者的創做熱情,請按要求【轉載】,謝謝
要求:未經做者贊成,必須保留此段聲明;必須在文章中給出原文鏈接且保證內容完整!不然必究法律責任!
我的網站: http://txj.shell.tor.hu/圖片

相關文章
相關標籤/搜索