PS-Scan ports扫描网络端口

用PS写出端口扫描 TCP139/445 AND UDP 137/138

用法简单:在c:\temp\target.txt写入多台IP地址

端口可以自己定义

以下是代码:

<#

This script can be used to Scan port TCP139/445 AND UDP 137/138

Need to modify Ip address under C:\temp\Target.txt first

Date:2017-05-15

#>

function Test-PortUDP{

<#

.SYNOPSIS

Tests port on computer.

.DESCRIPTION

Tests port on computer.

.PARAMETER computer

Name of server to test the port connection on.

.PARAMETER port

Port to test

.PARAMETER tcp

Use tcp port

.PARAMETER udp

Use udp port

.PARAMETER UDPTimeOut

Sets a timeout for UDP port query. (In milliseconds, Default is 1000)

.PARAMETER TCPTimeOut

Sets a timeout for TCP port query. (In milliseconds, Default is 1000)

.NOTES

Name: Test-Port.ps1

Author: Boe Prox

DateCreated: 18Aug2010

List of Ports: http://www.iana.org/assignments/port-numbers

To Do:

Add capability to run background jobs for each host to shorten the time to scan.

.LINK

https://boeprox.wordpress.org

.EXAMPLE

Test-Port -computer ‘server‘ -port 80

Checks port 80 on server ‘server‘ to see if it is listening

.EXAMPLE

‘server‘ | Test-Port -port 80

Checks port 80 on server ‘server‘ to see if it is listening

.EXAMPLE

Test-Port -computer @("server1","server2") -port 80

Checks port 80 on server1 and server2 to see if it is listening

.EXAMPLE

Test-Port -comp dc1 -port 17 -udp -UDPtimeout 10000

Server   : dc1

Port     : 17

TypePort : UDP

Open     : True

Notes    : "My spelling is Wobbly.  It‘s good spelling but it Wobbles, and the letters

get in the wrong places." A. A. Milne (1882-1958)

Description

-----------

Queries port 17 (qotd) on the UDP port and returns whether port is open or not

.EXAMPLE

@("server1","server2") | Test-Port -port 80

Checks port 80 on server1 and server2 to see if it is listening

.EXAMPLE

(Get-Content hosts.txt) | Test-Port -port 80

Checks port 80 on servers in host file to see if it is listening

.EXAMPLE

Test-Port -computer (Get-Content hosts.txt) -port 80

Checks port 80 on servers in host file to see if it is listening

.EXAMPLE

Test-Port -computer (Get-Content hosts.txt) -port @(1..59)

Checks a range of ports from 1-59 on all servers in the hosts.txt file

#>

[cmdletbinding(

DefaultParameterSetName = ‘‘,

ConfirmImpact = ‘low‘

)]

Param(

[Parameter(

Mandatory = $True,

Position = 0,

ParameterSetName = ‘‘,

ValueFromPipeline = $True)]

[array]$computer,

[Parameter(

Position = 1,

Mandatory = $True,

ParameterSetName = ‘‘)]

[array]$port,

[Parameter(

Mandatory = $False,

ParameterSetName = ‘‘)]

[int]$TCPtimeout=1000,

[Parameter(

Mandatory = $False,

ParameterSetName = ‘‘)]

[int]$UDPtimeout=1000,

[Parameter(

Mandatory = $False,

ParameterSetName = ‘‘)]

[switch]$TCP,

[Parameter(

Mandatory = $False,

ParameterSetName = ‘‘)]

[switch]$UDP

)

Begin {

If (!$tcp -AND !$udp) {$tcp = $True}

#Typically you never do this, but in this case I felt it was for the benefit of the function

#as any errors will be noted in the output of the report

$ErrorActionPreference = "SilentlyContinue"

$report = @()

}

Process {

ForEach ($c in $computer) {

ForEach ($p in $port) {

If ($tcp) {

#Create temporary holder

$temp = "" | Select Server, Port, TypePort, Open, Notes

#Create object for connecting to port on computer

$tcpobject = new-Object system.Net.Sockets.TcpClient

#Connect to remote machine‘s port

$connect = $tcpobject.BeginConnect($c,$p,$null,$null)

#Configure a timeout before quitting

$wait = $connect.AsyncWaitHandle.WaitOne($TCPtimeout,$false)

#If timeout

If(!$wait) {

#Close connection

$tcpobject.Close()

Write-Verbose "Connection Timeout"

#Build report

$temp.Server = $c

$temp.Port = $p

$temp.TypePort = "TCP"

$temp.Open = "False"

$temp.Notes = "Connection to Port Timed Out"

} Else {

$error.Clear()

$tcpobject.EndConnect($connect) | out-Null

#If error

If($error[0]){

#Begin making error more readable in report

[string]$string = ($error[0].exception).message

$message = (($string.split(":")[1]).replace(‘"‘,"")).TrimStart()

$failed = $true

}

#Close connection

$tcpobject.Close()

#If unable to query port to due failure

If($failed){

#Build report

$temp.Server = $c

$temp.Port = $p

$temp.TypePort = "TCP"

$temp.Open = "False"

$temp.Notes = "$message"

} Else{

#Build report

$temp.Server = $c

$temp.Port = $p

$temp.TypePort = "TCP"

$temp.Open = "True"

$temp.Notes = ""

}

}

#Reset failed value

$failed = $Null

#Merge temp array with report

$report += $temp

}

If ($udp) {

#Create temporary holder

$temp = "" | Select Server, Port, TypePort, Open, Notes

#Create object for connecting to port on computer

$udpobject = new-Object system.Net.Sockets.Udpclient

#Set a timeout on receiving message

$udpobject.client.ReceiveTimeout = $UDPTimeout

#Connect to remote machine‘s port

Write-Verbose "Making UDP connection to remote server"

$udpobject.Connect("$c",$p)

#Sends a message to the host to which you have connected.

Write-Verbose "Sending message to remote host"

$a = new-object system.text.asciiencoding

$byte = $a.GetBytes("$(Get-Date)")

[void]$udpobject.Send($byte,$byte.length)

#IPEndPoint object will allow us to read datagrams sent from any source.

Write-Verbose "Creating remote endpoint"

$remoteendpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0)

Try {

#Blocks until a message returns on this socket from a remote host.

Write-Verbose "Waiting for message return"

$receivebytes = $udpobject.Receive([ref]$remoteendpoint)

[string]$returndata = $a.GetString($receivebytes)

If ($returndata) {

Write-Verbose "Connection Successful"

#Build report

$temp.Server = $c

$temp.Port = $p

$temp.TypePort = "UDP"

$temp.Open = "True"

$temp.Notes = $returndata

$udpobject.close()

}

} Catch {

If ($Error[0].ToString() -match "\bRespond after a period of time\b") {

#Close connection

$udpobject.Close()

#Make sure that the host is online and not a false positive that it is open

If (Test-Connection -comp $c -count 1 -quiet) {

Write-Verbose "Connection Open"

#Build report

$temp.Server = $c

$temp.Port = $p

$temp.TypePort = "UDP"

$temp.Open = "False"

$temp.Notes = ""

} Else {

<#

It is possible that the host is not online or that the host is online,

but ICMP is blocked by a firewall and this port is actually open.

#>

Write-Verbose "Host maybe unavailable"

#Build report

$temp.Server = $c

$temp.Port = $p

$temp.TypePort = "UDP"

$temp.Open = "False"

$temp.Notes = "Unable to verify if port is open or if host is unavailable."

}

} ElseIf ($Error[0].ToString() -match "forcibly closed by the remote host" ) {

#Close connection

$udpobject.Close()

Write-Verbose "Connection Timeout"

#Build report

$temp.Server = $c

$temp.Port = $p

$temp.TypePort = "UDP"

$temp.Open = "False"

$temp.Notes = "Connection to Port Timed Out"

} Else {

$udpobject.close()

}

}

#Merge temp array with report

$report += $temp

}

}

}

}

End {

#Generate Report

$report

}

}

function Test-PortTCP{

<#

.SYNOPSIS

Tests port on computer.

.DESCRIPTION

Tests port on computer.

.PARAMETER computer

Name of server to test the port connection on.

.PARAMETER port

Port to test

.PARAMETER tcp

Use tcp port

.PARAMETER udp

Use udp port

.PARAMETER UDPTimeOut

Sets a timeout for UDP port query. (In milliseconds, Default is 1000)

.PARAMETER TCPTimeOut

Sets a timeout for TCP port query. (In milliseconds, Default is 1000)

.NOTES

Name: Test-Port.ps1

Author: Boe Prox

DateCreated: 18Aug2010

List of Ports: http://www.iana.org/assignments/port-numbers

To Do:

Add capability to run background jobs for each host to shorten the time to scan.

.LINK

https://boeprox.wordpress.org

.EXAMPLE

Test-Port -computer ‘server‘ -port 80

Checks port 80 on server ‘server‘ to see if it is listening

.EXAMPLE

‘server‘ | Test-Port -port 80

Checks port 80 on server ‘server‘ to see if it is listening

.EXAMPLE

Test-Port -computer @("server1","server2") -port 80

Checks port 80 on server1 and server2 to see if it is listening

.EXAMPLE

Test-Port -comp dc1 -port 17 -udp -UDPtimeout 10000

Server   : dc1

Port     : 17

TypePort : UDP

Open     : True

Notes    : "My spelling is Wobbly.  It‘s good spelling but it Wobbles, and the letters

get in the wrong places." A. A. Milne (1882-1958)

Description

-----------

Queries port 17 (qotd) on the UDP port and returns whether port is open or not

.EXAMPLE

@("server1","server2") | Test-Port -port 80

Checks port 80 on server1 and server2 to see if it is listening

.EXAMPLE

(Get-Content hosts.txt) | Test-Port -port 80

Checks port 80 on servers in host file to see if it is listening

.EXAMPLE

Test-Port -computer (Get-Content hosts.txt) -port 80

Checks port 80 on servers in host file to see if it is listening

.EXAMPLE

Test-Port -computer (Get-Content hosts.txt) -port @(1..59)

Checks a range of ports from 1-59 on all servers in the hosts.txt file

#>

[cmdletbinding(

DefaultParameterSetName = ‘‘,

ConfirmImpact = ‘low‘

)]

Param(

[Parameter(

Mandatory = $True,

Position = 0,

ParameterSetName = ‘‘,

ValueFromPipeline = $True)]

[array]$computer,

[Parameter(

Position = 1,

Mandatory = $True,

ParameterSetName = ‘‘)]

[array]$port,

[Parameter(

Mandatory = $False,

ParameterSetName = ‘‘)]

[int]$TCPtimeout=1000,

[Parameter(

Mandatory = $False,

ParameterSetName = ‘‘)]

[int]$UDPtimeout=1000,

[Parameter(

Mandatory = $False,

ParameterSetName = ‘‘)]

[switch]$TCP,

[Parameter(

Mandatory = $False,

ParameterSetName = ‘‘)]

[switch]$UDP

)

Begin {

If (!$tcp -AND !$udp) {$tcp = $True}

#Typically you never do this, but in this case I felt it was for the benefit of the function

#as any errors will be noted in the output of the report

$ErrorActionPreference = "SilentlyContinue"

$report = @()

}

Process {

ForEach ($c in $computer) {

ForEach ($p in $port) {

If ($tcp) {

#Create temporary holder

$temp = "" | Select Server, Port, TypePort, Open, Notes

#Create object for connecting to port on computer

$tcpobject = new-Object system.Net.Sockets.TcpClient

#Connect to remote machine‘s port

$connect = $tcpobject.BeginConnect($c,$p,$null,$null)

#Configure a timeout before quitting

$wait = $connect.AsyncWaitHandle.WaitOne($TCPtimeout,$false)

#If timeout

If(!$wait) {

#Close connection

$tcpobject.Close()

Write-Verbose "Connection Timeout"

#Build report

$temp.Server = $c

$temp.Port = $p

$temp.TypePort = "TCP"

$temp.Open = "False"

$temp.Notes = "Connection to Port Timed Out"

} Else {

$error.Clear()

$tcpobject.EndConnect($connect) | out-Null

#If error

If($error[0]){

#Begin making error more readable in report

[string]$string = ($error[0].exception).message

$message = (($string.split(":")[1]).replace(‘"‘,"")).TrimStart()

$failed = $true

}

#Close connection

$tcpobject.Close()

#If unable to query port to due failure

If($failed){

#Build report

$temp.Server = $c

$temp.Port = $p

$temp.TypePort = "TCP"

$temp.Open = "False"

$temp.Notes = "$message"

} Else{

#Build report

$temp.Server = $c

$temp.Port = $p

$temp.TypePort = "TCP"

$temp.Open = "True"

$temp.Notes = ""

}

}

#Reset failed value

$failed = $Null

#Merge temp array with report

$report += $temp

}

If ($udp) {

#Create temporary holder

$temp = "" | Select Server, Port, TypePort, Open, Notes

#Create object for connecting to port on computer

$udpobject = new-Object system.Net.Sockets.Udpclient

#Set a timeout on receiving message

$udpobject.client.ReceiveTimeout = $UDPTimeout

#Connect to remote machine‘s port

Write-Verbose "Making UDP connection to remote server"

$udpobject.Connect("$c",$p)

#Sends a message to the host to which you have connected.

Write-Verbose "Sending message to remote host"

$a = new-object system.text.asciiencoding

$byte = $a.GetBytes("$(Get-Date)")

[void]$udpobject.Send($byte,$byte.length)

#IPEndPoint object will allow us to read datagrams sent from any source.

Write-Verbose "Creating remote endpoint"

$remoteendpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0)

Try {

#Blocks until a message returns on this socket from a remote host.

Write-Verbose "Waiting for message return"

$receivebytes = $udpobject.Receive([ref]$remoteendpoint)

[string]$returndata = $a.GetString($receivebytes)

If ($returndata) {

Write-Verbose "Connection Successful"

#Build report

$temp.Server = $c

$temp.Port = $p

$temp.TypePort = "UDP"

$temp.Open = "True"

$temp.Notes = $returndata

$udpobject.close()

}

} Catch {

If ($Error[0].ToString() -match "\bRespond after a period of time\b") {

#Close connection

$udpobject.Close()

#Make sure that the host is online and not a false positive that it is open

If (Test-Connection -comp $c -count 1 -quiet) {

Write-Verbose "Connection Open"

#Build report

$temp.Server = $c

$temp.Port = $p

$temp.TypePort = "UDP"

$temp.Open = "True"

$temp.Notes = ""

} Else {

<#

It is possible that the host is not online or that the host is online,

but ICMP is blocked by a firewall and this port is actually open.

#>

Write-Verbose "Host maybe unavailable"

#Build report

$temp.Server = $c

$temp.Port = $p

$temp.TypePort = "UDP"

$temp.Open = "False"

$temp.Notes = "Unable to verify if port is open or if host is unavailable."

}

} ElseIf ($Error[0].ToString() -match "forcibly closed by the remote host" ) {

#Close connection

$udpobject.Close()

Write-Verbose "Connection Timeout"

#Build report

$temp.Server = $c

$temp.Port = $p

$temp.TypePort = "UDP"

$temp.Open = "False"

$temp.Notes = "Connection to Port Timed Out"

} Else {

$udpobject.close()

}

}

#Merge temp array with report

$report += $temp

}

}

}

}

End {

#Generate Report

$report

}

}

Get-Content "C:\Temp\Target.txt" | ForEach-Object {

$tmp = Test-Connection $_ -Count 1 -Quiet

if ($tmp){

write-host $_" Target Can access"

$re1 = Test-PortTCP -computer $_ -port 139 -TCP -WarningAction Ignore -TCPtimeout 10000

$result1 = $re1.Open -eq "True"

# write-host $result1

if ($result1) {

write-host $_ "TCP 139 Port is opening"

}

$re2 = Test-PortTCP -computer $_ -port 445 -TCP -WarningAction Ignore -TCPtimeout 10000

$result2 = $re2.Open -eq "True"

# write-host $result2

if ($result2) {

write-host $_ "TCP 445 Port is opening"

}

$re3 = Test-PortUDP -computer $_ -port 138 -UDP -UDPtimeout 10000

$result3 = $re3.Open -eq "True"

# write-host $result3

if ($result3) {

write-host $_ "UDP 138 Port is opening"

}

$re4 = Test-PortUDP -computer $_ -port 137 -UDP -UDPtimeout 10000

$result4 = $re4.Open -eq "True"

# write-host $result4

if ($result4) {

write-host $_ "UDP 137 Port is opening"

}

$Total = $result1 -or $result2 -or $result3 -or $result4

if (-not $Total) {

write-host $_ "Related Ports are not opening"

}

del variable:re1 -Force

del variable:re2 -Force

del variable:re3 -Force

del variable:re4 -Force

del variable:result1 -Force

del variable:result2 -Force

del variable:result3 -Force

del variable:result4 -Force

del variable:Total -Force

}

}

时间: 2024-10-13 15:45:10

PS-Scan ports扫描网络端口的相关文章

查看网络端口

ICMP和IP协议是属于同一层次(网络层)的,其报文也是封在IP报文中传输,而端口 是传输层TCP/UDP协议的概念,所以Ping端口是不能成功的 ** Windows check the portstelnet ip portnetstat -na // 显示本机连接情况及打开的端口netstat -nab //显示每个连接都是由哪些程序创建的端口监视器: Tcpview.Port Reporter.绿鹰PC万能精灵.网络端口查看器等 Windows下的NmapWin等使用可以参考:[url]

NMAP扫描UDP123NTP端口详解

我用的nmap版本:Zenmap 6.25 例如命令: nmap -sU -pU:123 -Pn -n --script=ntp-monlist IP 如果要批量进行,可以把IP存放在ip.txt可以把命令改成: nmap -sU -pU:123 -Pn -n --script=ntp-monlist -iL ip.txt 运行结果: 如果要批量进行,并把生成结果写入指定文件ip_result.txt 可以把命令改成: nmap -sU -pU:123 -Pn -n --script=ntp-m

fuxi scanner 安装+修复不能扫描域名端口bug

fuxi scanner 安装+修复不能扫描域名端口bug 0x00 fuxi scanner 简介 扫描器链接:https://github.com/jeffzh3ng/Fuxi-Scanner伏羲扫描器是一款开源的网络安全扫描工具,非常适合中小型企业对资产进行管理已经安全检测.伏羲扫描器通过模块化提供了多种安全检查功能 基于pocsuite框架poc扫描 持续化的漏洞管理 多种协议的弱口令扫描,比如 ssh.mysql.ftp.vnc 基于爆破的子域名收集 企业内部资产管理(也可以进行src

Hacker(14)----扫描目标计算机端口

端口是目前计算机与外界的通道,因而黑客一旦锁定目标计算机,便会扫描该计算机中已经开放的端口,从而得到更多的有用信息.扫描目标计算机端口一般使用SuperScan.X-Scan等. 一.端口扫描原理 扫描端口之前,用户必须了解端口扫描的原理.简单讲,其原理就是利用数据包来分析目标计算机的响应,从而得到目标计算机的端口开放信息和系统内存在的弱点信息. 端口扫描指在本地计算机中向目标计算机所有的端口发送同一信息,然后根据返回的响应状态判断目标计算机中哪些端口已打开.哪些端口可以被使用.目前最常用的是利

网络端口号布局与分类

1. 什么是网络端口 在网络技术中,端口(Port)大致有两种意思: 一. 物理意义上的端口,比如,ADSL Modem.集线器.交换机.路由器用于连接其他网络设备的接口,如RJ-45端口.SC端口等等: 二. 逻辑意义上的端口,一般是指TCP/IP协议中的端口,端口号的范围从0到65535,比如用于浏览网页服务的80端口,用于FTP服务的21端口等等. 2.端口分类 (1)按端口号可分为3大类: <1> 公认端口(Well Known Ports):从0到1023,它们紧密绑定(bindin

网络端口号大全

网络端口号大全 1 tcpmux TCP 端口服务多路复用5 rje 远程作业入口7 echo Echo 服务9 discard 用于连接测试的空服务11 systat 用于列举连接了的端口的系统状态13 daytime 给请求主机发送日期和时间17 qotd 给连接了的主机发送每日格言18 msp 消息发送协议19 chargen 字符生成服务:发送无止境的字符流20 ftp-data FTP 数据端口21 ftp 文件传输协议(FTP)端口:有时被文件服务协议(FSP)使用22 ssh 安全

批量扫描IP端口程序 (适用于window&amp;linux)

批量扫描IP端口,根据扫描IP导出IP命名的文件的结果.假设1.txt文件内容为127.0.0.1192.168.1.1然后我们获取文件内容IP进行扫描window .bat版本 :1.txt为文件名,根据需求进行修改 :C:\nmap\nmap-6.46\nmap.exe 为namp的路径,根据需求进行修改 :把1.txt与该扫描脚本放一起 @echo off for /f "delims=." %%i in (1.txt) do C:\nmap\nmap-6.46\nmap.exe

Linux版VMware使用DHCP为虚拟机分配固定IP及实现NAT网络端口转发

首先介绍一下环境: 1. Host(宿主计算机)的操作系统是Scientific Linux,内核版本2.6.18,主机名SPVM02,IP地址135.100.101.102,部署的是VMware Workstation 8. 2. Guest(虚拟机)这边安装的操作系统是Windows 7. 3. Guest使用vmnet8这块网卡,通过NAT协议实现上网. 需要实现的功能是Host网络中的其他计算机使用Microsoft Remote Desktop远程登录Guest桌面.所以探索如何通过配

漏洞扫描原理——将主机扫描、端口扫描以及OS扫描、脆弱点扫描都统一放到了一起

漏洞扫描原理及程序 1 引言 网络扫描,是基于Internet的.探测远端网络或主机信息的一种技术,也是保证系统和网络安全必不可少的一种手段.主机扫描,是指对计算机主机或者其它网络设备进行安全性检测,以找出安全隐患和系统漏洞.总体而言,网络扫描和主机扫描都可归入漏洞扫描一类.漏洞扫描本质上是一把双刃剑:黑客利用它来寻找对网络或系统发起攻击的途径,而系统管理员则利用它来有效防范黑客入侵.通过漏洞扫描,扫描者能够发现远端网络或主机的配置信息. TCP/UDP端口的分配.提供的网络服务.服务器的具体信