欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PowerShell小技巧之获取TCP响应(类Telnet)

程序员文章站 2022-06-10 08:02:40
通常情况下,为了检测指定的tcp端口是否存活,我们都是通过telnet指定的端口看是否有响应来确定,然而默认情况下win8以后的系统默认是不安装telnet的。设想一下如果...

通常情况下,为了检测指定的tcp端口是否存活,我们都是通过telnet指定的端口看是否有响应来确定,然而默认情况下win8以后的系统默认是不安装telnet的。设想一下如果你黑进了一个服务器,上面没装telnet,但是为了进一步渗透进内网,需要探测内部服务器特定端口是否打开,同时你还不愿意安装telnet,担心引起管理员注意。那么好吧,在这个情况下你需要我的这个脚本。由于它是原生态的powershell语句完成,木有telnet你也照样能检测tcp端口的情况了。

下面首先上代码,后面进行讲解:

复制代码 代码如下:

        =====文件名:get-tcpresponse.ps1=====
function get-tcpresponse {
<# author:fuhj(powershell#live.cn ,http://fuhaijun.com)
        .synopsis
            tests tcp port of remote or local system and returns a response header
            if applicable
        .description
            tests tcp port of remote or local system and returns a response header
            if applicable
            if server has no default response, then response property will be null
        .parameter computername
            local or remote system to test connection
        .parameter port
            tcp port to connect to
        .parameter tcptimeout
            time until connection should abort
        .example
        get-tcpresponse -computername pop.126.com -port 110

        computername : pop.126.com
        port         : 110
        isopen       : true
        response     : +ok welcome to coremail mail pop3 server (126coms[75c606d72bf436dfbce6.....])

        description
        -----------
        checks port 110 of an mail server and displays header response.
    #>
    [outputtype('net.tcpresponse')]
    [cmdletbinding()]
    param (
        [parameter(valuefrompipeline,valuefrompipelinebypropertyname)]
        [alias('__server','ipaddress','ip','domain')]
        [string[]]$computername = $env:computername,
        [int[]]$port = 25,
        [int]$tcptimeout = 1000
    )
    process {
        foreach ($computer in $computername) {
            foreach ($_port in $port) {
                $stringbuilder = new-object text.stringbuilder
                $tcpclient = new-object system.net.sockets.tcpclient
                $connect = $tcpclient.beginconnect($computer,$_port,$null,$null)
                $wait = $connect.asyncwaithandle.waitone($tcptimeout,$false)
                if (-not $wait) {
                    $object = [pscustomobject] @{
                        computername = $computer
                        port = $_port
                        isopen = $false
                        response = $null
                    }
                } else {
                    while ($true) {
                        #let buffer
                        start-sleep -milliseconds 1000
                        write-verbose "bytes available: $($tcpclient.available)"
                        if ([int64]$tcpclient.available -gt 0) {
                            $stream = $tcpclient.getstream()
                            $bindresponsebuffer = new-object byte[] -argumentlist $tcpclient.available
                            [int]$response = $stream.read($bindresponsebuffer, 0, $bindresponsebuffer.count)
                            $null = $stringbuilder.append(($bindresponsebuffer | foreach {[char][int]$_}) -join '')
                        } else {
                            break
                        }
                    }
                    $object = [pscustomobject] @{
                        computername = $computer
                        port = $_port
                        isopen = $true
                        response = $stringbuilder.tostring()
                    }
                }
                $object.pstypenames.insert(0,'net.tcpresponse')
                write-output $object
                if ($stream) {
                    $stream.close()
                    $stream.dispose()
                }
                $tcpclient.close()
                $tcpclient.dispose()
            }
        }
    }
}

首先创建一个system.net.sockets.tcpclient对象,去连接指定的域名和端口,瞬间断开的那是服务器没开那个端口,直接被拒绝了,如果没拒绝,那就等着服务器端给你响应,然后读取字节流拼接起来进行解析。
最后需要强调的是需要对打开的流和tcp连接进行关闭,以便释放资源
调用方法如下:

复制代码 代码如下:

get-tcpresponse -computername pop.126.com -port 110
 

PowerShell小技巧之获取TCP响应(类Telnet)

再对比一下telnet的结果

PowerShell小技巧之获取TCP响应(类Telnet)

结果是一样的,以后没有telnet也难不住大家了,have fun!^_^