PowerShell小技巧之发送TCP请求_PowerShell

很多时候我们需要通过Socket发送特定的TCP请求给服务器的特定端口来实现探测服务器的指定端口所开启的服务。很多语言都有相应的方法实现上述需求,当然,PowerShell也不例外,比如我们要发送一个简单的http请求到指定的web服务器:
GET / HTTP/1.1
Host:cn.bing.com

这里我们想请求微软必应的中文首页,如果需要通过PowerShell向cn.bing.com服务器发送get请求,就需要创建一个System.Net.Sockets.TcpClient对象,向指定的服务器和端口发送请求。

具体代码如下:

复制代码 代码如下:

        =====文件名:Send-TcpRequest.ps1=====
########################################
# Send-TcpRequest.ps1
## Send a TCP request to a remote computer, and return the response.
## If you do not supply input to this script (via either the pipeline, or the
## -InputObject parameter,) the script operates in interactive mode.
##
## Example:
##
## $http = @"
## GET / HTTP/1.1
## Host:cn.bing.com 
## `n`n
## "@
##
## $http | .\Send-TcpRequest cn.bing.com  80
########################################
param(
        [string] $remoteHost = "localhost",
        [int] $port = 80,
        [switch] $UseSSL,
        [string] $inputObject,
        [int] $commandDelay = 100
     )

[string] $output = ""

## Store the input into an array that we can scan over. If there was no input,
## then we will be in interactive mode.
$currentInput = $inputObject
if(-not $currentInput)
{
    $SCRIPT:currentInput = @($input)
}
$scriptedMode = [bool] $currentInput

function Main
{
    ## Open the socket, and connect to the computer on the specified port
    if(-not $scriptedMode)
    {
        write-host "Connecting to $remoteHost on port $port"
    }

    trap { Write-Error "Could not connect to remote computer: $_"; exit }
    $socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)

    if(-not $scriptedMode)
    {
        write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
    }

    $stream = $socket.GetStream()

    if($UseSSL)
    {
        $sslStream = New-Object System.Net.Security.SslStream $stream,$false
        $sslStream.AuthenticateAsClient($remoteHost)
        $stream = $sslStream
    }

    $writer = new-object System.IO.StreamWriter $stream

    while($true)
    {
        ## Receive the output that has buffered so far
        $SCRIPT:output += GetOutput

        ## If we're in scripted mode, send the commands,
        ## receive the output, and exit.
        if($scriptedMode)
        {
            foreach($line in $currentInput)
            {
                $writer.WriteLine($line)
                $writer.Flush()
                Start-Sleep -m $commandDelay
                $SCRIPT:output += GetOutput
            }

            break
        }
        ## If we're in interactive mode, write the buffered
        ## output, and respond to input.
        else
        {
            if($output)
            {
                foreach($line in $output.Split("`n"))
                {
                    write-host $line
                }
                $SCRIPT:output = ""
            }

            ## Read the user's command, quitting if they hit ^D
            $command = read-host
            if($command -eq ([char] 4)) { break; }

            ## Otherwise, Write their command to the remote host
            $writer.WriteLine($command)
            $writer.Flush()
        }
    }

    ## Close the streams
    $writer.Close()
    $stream.Close()

    ## If we're in scripted mode, return the output
    if($scriptedMode)
    {
        $output
    }
}

## Read output from a remote host
function GetOutput
{
    ## Create a buffer to receive the response
    $buffer = new-object System.Byte[] 1024
    $encoding = new-object System.Text.AsciiEncoding

    $outputBuffer = ""
    $foundMore = $false

    ## Read all the data available from the stream, writing it to the
    ## output buffer when done.
    do
    {
        ## Allow data to buffer for a bit
        start-sleep -m 1000

        ## Read what data is available
        $foundmore = $false
        $stream.ReadTimeout = 1000

        do
        {
            try
            {
                $read = $stream.Read($buffer, 0, 1024)

                if($read -gt 0)
                {
                    $foundmore = $true
                    $outputBuffer += ($encoding.GetString($buffer, 0, $read))
                }
            } catch { $foundMore = $false; $read = 0 }
        } while($read -gt 0)
    } while($foundmore)

    $outputBuffer
}
. Main
该脚本使用方法如下:
$http = @"

GET / HTTP/1.1
Host:cn.bing.com
`n`n
"@
$http | .\Send-TcpRequest cn.bing.com 80

执行效果如图所示:

需要说明的是,由于页面返回的内容太长了,这里至少是将返回的内容缓存在一个变量里,并只输出了变量的头10行。
有了这个脚本,我们就可以向指定的web服务器发送特定的请求,来实现模拟登陆和操作的功能了。

时间: 2024-07-29 19:52:02

PowerShell小技巧之发送TCP请求_PowerShell的相关文章

PowerShell小技巧之执行SOAP请求_PowerShell

SOAP的请求在Web Service是无处不在的,像WCF服务和传统ASMX asp.net的web Service.如果要测试SOAP服务是否好用通过web编程来实现就显得太过于复杂了,下面的脚本片段(snippet)将会轻而易举的完成通过powershell测试和调用SOAP服务: 这是一段程序代码. 复制代码 代码如下: function Execute-SOAPRequest (         [Xml]    $SOAPRequest,         [String] $URL

PowerShell小技巧实现IE Web自动化_PowerShell

Windows 系统自带的Internet Explore +加上PowerShell 即可搞定. 今天就分享下这几天自己写的几个小函数,欢迎拍砖: # # 打开IE窗口 # function New-IEWindow { param( [string]$Url, [switch]$Visible, [switch]$FullScreen ) $Global:IEHost = new-object -com "InternetExplorer.Application" $Global:

Powershell小技巧之查询AD用户_PowerShell

假若你登录到了一个AD中你可以轻松的查询AD目录.在先前的技巧中我们阐述了一个基本脚本.这里有一个可以自定义根目录的扩展方法(设置你的查询点),它支持同步查询(而不是递归到一个容器). 它同时也阐述了怎么从一个活动目录查找结果再将其转化成用户对象: $SAMAccountName = 'tobias' $SearchRoot = 'LDAP://OU=customer,DC=company,DC=com' $SearchScope = 'OneLevel' $ldap = "(&(obj

PowerShell小技巧之观察UNC路径_PowerShell

许多命令能支持UNC路径,但是UNC路径在脚本中看起来很奇怪.看看下面代码: PS> Test-Path -Path \\127.0.0.1\c$ True 它返回了真,证明UNC路径存在.接着改变当前的路径指向一个非文件系统路径接着测试: PS> cd hkcu:\ PS> Test-Path -Path \\127.0.0.1\c$ False 一样的路径但现在却获得了失败.这是因为UNC路径没有包含盘符,而PS需要根据盘符去分配当前合适位置.如果当前路径没有包含在磁盘路径下,Pow

Powershell小技巧之设置IE代理_PowerShell

IE的代理设置位于注册表中:"HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"下.关键键值为ProxyEnable和ProxyServer.所以通过更改注册表即可完成IE代理的设置. Function Set-IEProxy { param( [bool]$Enable=$false, [string]$ProxyServer, [ValidateRange(1,65535)] [int]$port,

Powershell小技巧之删除不规则字符_PowerShell

在路径中,其中有像引号冒号等不规则字符.如果你脚本中的活动路径是来自某段信息,你一定会需要返回符合规则的路径. 这里有段函数教你在任何路径中使用下划线去替换其中不规则部分: function Get-LegalPathName($Path) { $illegalChars = [System.IO.Path]::GetInvalidFileNameChars() foreach($illegalChar in $illegalChars) { $Path = $Path.Replace($ill

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

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

Powershell小技巧之获取MAC地址_PowerShell

在Powershell中获取MAC地址不是很难.这里就有一种方法: 复制代码 代码如下: PS> getmac /FO CSV | ConvertFrom-Csv Physical Address                        Transport Name                         ----------------                        --------------                         5C-51-4F-6

Powershell小技巧之创建短网址_PowerShell

你可能听说过缩短过的长连接.它有几个免费的不错的服务商.这里有一个脚本能将任何URL变成一个缩写URL. $OriginalURL = 'http://www.jb51.net/isesteroids2' $url = "http://tinyurl.com/api-create.php?url=$OriginalURL" $webclient = New-Object -TypeName System.Net.WebClient $webclient.DownloadString($