forked from DFIR-ORC/dfir-orc-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.ps1
237 lines (201 loc) · 6.28 KB
/
configure.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
function Configure-Orc
{
<#
.SYNOPSIS
Configure DFIR-ORC
.PARAMETER RawBin
Path to DFIR-ORC raw binaries. Default is 'tools\' directory in
configuration directory
.PARAMETER Configuration
Path to DFIR-ORC configuration root directory containing 'tools\'
and 'config\' subdirectories. Default is '.\'
.PARAMETER ToolEmbedXml
Path to ToolEmbed's XML configuration file. If not specified the
script will lookup in this order in configuration directory:
- ORC_embed.xml
- DFIR-ORC_embed.xml
- *.xml with '<toolembed>' as root element
.PARAMETER Destination
Path to configured DFIR-ORC output file. Default is '.\output\'
or 'output\' in configuration root directory if provided with
Configuration parameter
.PARAMETER Force
DFIR-ORC raw binaries present in tools directory will be replaced
if -RawBin option is used
.EXAMPLE
.\configure.ps1 `
-RawBin D:\dfir-orc\build\dev\latest\bin\MinSizeRel\ `
-Configuration C:\dev\dfir-orc\ `
-Destination C:\dev\dfir-orc\release\DFIR-Orc.exe
#>
[cmdletbinding()]
Param (
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[System.IO.DirectoryInfo]
$RawBin,
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[System.IO.DirectoryInfo]
$Configuration = ".",
[Parameter(Mandatory = $false)]
[System.IO.FileInfo]
$ToolEmbedXml,
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]
$Destination,
[Switch]
$Force
)
$ErrorActionPreference = "Stop"
$OrcRawBinaries = @(
"DFIR-Orc_x64.exe"
"DFIR-Orc_x86.exe"
)
$OrcRawBinariesToClean = @()
if($RawBin)
{
foreach($OrcRawBinary in $OrcRawBinaries)
{
if(Test-Path "${RawBin}\${OrcRawBinary}")
{
if($Force -Or -Not (Test-Path "${Configuration}\tools\${OrcRawBinary}"))
{
Copy-Item -Force -Path "${RawBin}\${OrcRawBinary}" -Destination "${Configuration}/tools"
$OrcRawBinariesToClean += $OrcRawBinary
}
else
{
Write-Warning "DFIR-ORC raw binary seems already present at ${Configuration}\tools\${OrcRawBinary} and will be used to configure DFIR-ORC."
Write-Warning "Use -Force option if you want to replace it"
}
}
else
{
Write-Error "Cannot find ${OrcRawBinary} raw binary"
}
}
}
else
{
$RawBin = "./tools"
}
foreach($OrcRawBinary in $OrcRawBinaries)
{
if(-Not (Test-Path "${Configuration}\tools\${OrcRawBinary}"))
{
Write-Error "DFIR-ORC raw binary ${OrcRawBinary} is not present in ${Configuration}\tools directory"
}
}
Push-Location $Configuration
try
{
if(-Not $ToolEmbedXml)
{
$ToolEmbedXml = Get-ToolEmbedXml -Path ".\config"
}
if(-Not $ToolEmbedXml -Or -Not (Test-Path $ToolEmbedXml))
{
Write-Error "Cannot find ToolEmbed configuration"
}
else
{
Write-Output "Found ToolEmbed configuration: '${ToolEmbedXml}'"
}
$ENV:__COMPAT_LAYER = "RUNASINVOKER"
# XML configuration files usually references some environment variables
$ENV:ORC_CONFIG_FOLDER = "config"
$ENV:ORC_OUTPUT = "DFIR-Orc.exe"
. tools\DFIR-Orc_x64.exe ToolEmbed /Config="${ToolEmbedXml}"
$ToolEmbedOutput = Get-ToolEmbedOutput -Path $ToolEmbedXml
}
finally
{
Pop-Location
}
if(! $Destination)
{
$Destination = "${Configuration}\output"
}
if($Destination -notmatch '\.exe$')
{
$DestinationDir = $Destination
$ExecutableName = Split-Path $ToolEmbedOutput -leaf
$Destination = "$Destination/$ExecutableName"
}
else
{
$DestinationDir = Split-Path $Destination
}
if(-Not (Test-Path $DestinationDir))
{
New-Item -ItemType Directory $DestinationDir | Out-Null
Write-Output "Create '$DestinationDir' directory"
}
if("${Configuration}/${ToolEmbedOutput}" -ne $Destination)
{
Move-Item -Force -Path "${Configuration}/${ToolEmbedOutput}" -Destination $Destination
}
Write-Output `
"`n`nDFIR-ORC configuration is done: '$(Resolve-Path ${Destination})'`n"
# Clean DFIR-ORC raw binaries
if($RawBin)
{
foreach($OrcRawBinary in $OrcRawBinariesToClean)
{
if(Test-Path "$Configuration\tools\$OrcRawBinary")
{
Remove-Item -Path "${Configuration}\tools\${OrcRawBinary}"
}
}
}
}
function Get-ToolEmbedXml
{
param(
[System.IO.DirectoryInfo]
$Path
)
$EmbedFileNames = @(
"ORC_embed.xml"
"DFIR-ORC_embed.xml"
"Embed.xml"
)
foreach($EmbedFileName in $EmbedFileNames)
{
if(Test-Path "$Path\$EmbedFileName")
{
$ToolEmbedXml = "$Path/$EmbedFileName"
if(Get-Item -Path $ToolEmbedXml | Select-Xml -XPath toolembed)
{
return $ToolEmbedXml
}
}
}
# Lookup for an xml file with 'toolembed' as root element
$ToolEmbedXml = `
Get-ChildItem -Filter *.xml ${Path}/ | `
Select-Xml -XPath toolembed | `
Select-Object -First 1 | `
Select-Object -ExpandProperty Path
return $ToolEmbedXml
}
function Get-ToolEmbedOutput
{
<#
.SYNOPSIS
Look into ToolEmbed XML configuration for '<output>' element value
#>
[OutputType([String])]
param(
[System.IO.FileInfo]
$Path
)
$Output = `
Select-Xml -XPath "toolembed/output" ${Path} | `
Select-Object -ExpandProperty Node | `
Select-Object -ExpandProperty InnerText
return [Environment]::ExpandEnvironmentVariables($Output)
}
Configure-Orc @args