-
Notifications
You must be signed in to change notification settings - Fork 14
/
ParseIR.ps1
357 lines (330 loc) · 13.1 KB
/
ParseIR.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
# The successful file copies csv is used as a basis for telling ParseIR which files should be parsed by which utility
# The 'name' of the JSON object is used to match the 'parser' configuration in the evidence collection JSON
# In Evidence Collection directives, parser can be a comma-delimited list of values - but still string, not array.
# A 'parser' can be any type of script invoked by cmd.exe \c - powershell scripts, etc.
[CmdletBinding()]
param
(
[Parameter(Mandatory = $false, HelpMessage = 'The fully-qualified file-path where evidence is stored, defaults to $PSScriptRoot\evidence')]
[string]$evidence_dir = "$PSScriptRoot\evidence",
[Parameter(Mandatory = $false, HelpMessage = 'The fully-qualified directory where parsed evidence will be stored, defaults to $PSScriptRoot\parsed_evidence')]
[string]$parsed_evidence_dir = "$PSScriptRoot\parsed_evidence",
[Parameter(Mandatory = $false, HelpMessage = 'The fully-qualified file-path for the configuration file, defaults to $PSScriptRoot\parse_config.json')]
[string]$config = "$PSScriptRoot\parsing_config.json",
[Parameter(Mandatory = $false, HelpMessage = 'Tell RetrievIR to ignore missing dependencies.')]
[switch]$ignoremissing,
[Parameter(Mandatory = $false, HelpMessage = 'The fully-qualified directory where third-party parsers are/will be stored - defaults to $PSScriptRoot\utils_dir')]
[string]$utilities_dir = "$PSScriptRoot\utilities"
)
function Get-Configuration {
if (-not (Test-Path $config)){
Log-Message "Could not find specified configuration path: $config" $true
Log-Message "[*] Please double check the specified file exists!"
Log-Message "[!] Exiting..."
exit
}
try {
$data = Get-Content -Path $config -Raw | ConvertFrom-Json
} catch {
Log-Message "[!] Error reading specified configuration path" $true
Log-Message "[!] Exiting..."
exit
}
return $data
}
function Validate-Configuration($data) {
$error_count = 0
ForEach ($object in $data){
$object_name = $object.psobject.Properties.Name
ForEach ($name in $object_name){
if (-not $object.$name.name){
Log-Message "[!] Config object missing name!" $false "red"
$error_count +=1
Log-Message $object
}
if (-not $object.$name.evidence_type){
Log-Message "[!] Config object missing evidence_type!" $false "red"
$error_count +=1
Log-Message $object
}
if (-not $object.$name.executable){
Log-Message "[!] Config object missing parser executable!" $false "red"
$error_count +=1
Log-Message $object
}
if (-not $object.$name.cmdline){
Log-Message "[!] Config object missing parser commandline!" $false "red"
$error_count +=1
Log-Message $object
}
if (-not $object.$name.url){
Log-Message "[!] Config object missing binary url!" $false "red"
$error_count +=1
Log-Message $object
}
if (-not $object.$name.if_missing){
Log-Message "[!] Config object missing 'if_missing'!" $false "red"
$error_count +=1
Log-Message $object
}
if (-not $object.$name.dl_type){
Log-Message "[!] Config object missing 'dl_type'!" $false "red"
$error_count +=1
Log-Message $object
}
if (-not $object.$name.operates_on){
Log-Message "[!] Config object missing 'operates_on'!" $false "red"
$error_count +=1
Log-Message $object
}
}
}
if ($error_count -ne 0){
Log-Message "[!] Configuration Errors Detected: $error_count" $false "red"
Log-Message "[!] Please rectify before continuing!" $false "red"
exit
} else {
Log-Message "[!] Configuration Validated!"
}
}
function Log-Message ($msg, $error, $color){
$timestamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
$log_path = "$PSScriptRoot\ParseIR.log"
if (-not $color){
$color = "White"
}
if ($error){
Write-Warning $msg
} else {
Write-Host $msg -ForegroundColor $color
}
Add-Content -Path $log_path -Value "$timestamp - $msg"
}
function Get-Successful-Copy-File {
$output_path = $evidence_dir+"\successful_file_copies.csv"
Log-Message "[!] Reading File Copy CSV: $output_path"
try {
$data = Import-CSV -Path $output_path
} catch {
Log-Message "[!] Fatal error parsing file copies CSV!" $false "red"
exit
}
return $data
}
function Create-Directory ($dir) {
if (Test-Path $dir){
return
}
try {
New-Item -Path $dir -ItemType Directory | Out-Null
Log-Message "[!] Directory Created: $dir" -quiet $true
}catch{
Log-Message "[!] Could not create directory: $dir" $true
exit
}
}
function Download-Binary($url, $destination_dir, $final_name, $type, $parse_type) {
Log-Message "[+] Downloading $final_name from $url"
if ($type -eq "zip"){
$zip_path = "$destination_dir\$final_name.zip"
Invoke-WebRequest "$url" -OutFile $zip_path
Expand-Archive -Path $zip_path -DestinationPath "$destination_dir\$final_name" -Force
Remove-Item -Path $zip_path
} elseif ($type -eq "exe"){
$exe_path = "$destination_dir\$final_name"
Invoke-WebRequest "$url" -OutFile $exe_path
}
if ($parse_type -eq "standalone"){
Start-Sleep 1
try {
Move-Item -Path "$destination_dir\$final_name" -Destination "$destination_dir\standalone"
} catch {
Log-Message "[!] Unable to move directory/item to standalones!"
}
}
}
function Check-Tools ($config_data){
if (-not (Test-Path $utilities_dir)){
Create-Directory $utilities_dir
}
$util_contents = Get-ChildItem -Path $utilities_dir -Recurse | Where {! $_.PSIsContainer }
if ($util_contents.GetType().Name -eq "String"){
$util_contents = @($util_contents)
}
ForEach ($object in $config_data)
{
$object_name = $object.psobject.Properties.Name
ForEach ($name in $object_name) {
$exe_name = $object.$name.executable
$found = $false
ForEach ($file in $util_contents){
if ($file.Name -eq $exe_name){
$found = $true
break
}
}
if (-not ($found) -and ($object.$name.if_missing -eq "download")){
Download-Binary $object.$name.url $utilities_dir $object.$name.executable $object.$name.dl_type $object.$name.type
} elseif (-not ($found) -and -not ($object.$name.type -eq "native")){
Log-Message "[!] Missing Binary without downloading: $exe_name"
} else {
Log-Message "[!] Found Binary: $exe_name"
}
}
}
$parser_locations = @{}
$util_contents = Get-ChildItem -Path $utilities_dir -Recurse | Where {! $_.PSIsContainer }
if ($util_contents.GetType().Name -eq "String"){
$util_contents = @($util_contents)
}
ForEach ($object in $config_data) {
$object_name = $object.psobject.Properties.Name
ForEach ($name in $object_name) {
$exe_name = $object.$name.executable
ForEach ($file in $util_contents){
if ($file.Name -eq $exe_name){
$parser_locations[$name] = $file.FullName
}
}
}
}
return $parser_locations
}
function Start-Processing ($parse_config, $file_data, $exe_locations){
if (-not (Test-Path $parsed_evidence_dir)){
Create-Directory $parsed_evidence_dir
}
$files_parsed = New-Object -TypeName 'System.Collections.ArrayList'
$dirs_parsed = New-Object -TypeName 'System.Collections.ArrayList'
ForEach ($record in $file_data){
#continue
# TODO - Remove Above
if ($record.Parser -eq "N/A"){
# Skip parsing files that don't have an assigned parser in their output
continue
}
if ($record.Parser -match ".*,.*"){
$parsers = $record.Parser -split ","
} else {
$parsers = @($record.Parser)
}
ForEach ($parser in $parsers){
if (-not ($parse_config.$parser)){
Log-Message "No Available Parser Configured for type: $parser" $false "Red"
continue
}
$parse_object = $parse_config.$($parser)
if ($parse_object.type -eq "standalone"){
continue
}
$pass = $true
$file_basename = Split-Path -Leaf $record.FileName
if ($parse_object.file_filter[0] -eq "*"){
$pass = $false
}else{
ForEach ($filter in $parse_object.file_filter){
if ($file_basename -like $filter){
$pass = $false
}
}
}
if ($pass){
continue
}
$base_evidence_path = $parsed_evidence_dir + "\" + $record.Computer
#Write-Host $base_evidence_path
#if (-not (Test-Path ))
if ($parse_object.operates_on -eq "dir"){
$source_target = Split-Path -Parent $record.FileName
if ($source_target -in $dirs_parsed){
continue
}
$dirs_parsed.Add($source_target) | Out-Null
} elseif ($parse_object.operates_on -eq "file") {
$source_target = $record.FileName
if ($source_target -in $files_parsed){
continue
}
$files_parsed.Add($source_target) | Out-Null
}
Parse $parse_object $record $base_evidence_path $source_target $exe_locations[$parser]
}
}
#Standalone Parsers
ForEach ($object in $parse_config) {
$object_name = $object.psobject.Properties.Name
ForEach ($name in $object_name) {
$exe_name = $object.$name.executable
$type = $object.$name.type
if($type -ne "standalone"){
continue
}
Execute-Standalone-Parser $object.$name $exe_locations[$name]
}
}
}
function Execute-Standalone-Parser ($parser, $bin_loc){
$commandline = $parser.cmdline
if ($commandline -eq "N/A"){
return
}
if ($commandline -match ".*#EVIDENCE_DIR#.*"){
$commandline = $commandline -replace ("#EVIDENCE_DIR#", "`"$evidence_dir`"")
}
if ($commandline -match ".*#PARSER#.*"){
$commandline = $commandline -replace ("#PARSER#", "`"$bin_loc`"")
}
if ($commandline -match ".*#PARSED_EVIDENCE_DIR#.*"){
$commandline = $commandline -replace ("#PARSED_EVIDENCE_DIR#", "`"$parsed_evidence_dir`"")
}
Log-Message "[+] Executing: $commandline"
& powershell.exe $commandline
}
function Parse ($parser, $record, $base_evidence_dir, $target, $exe_full_path){
Log-Message "[+] Parsing: $($record.FileName)"
$tmp_evidence_storage = $base_evidence_dir + "\" + $parser.evidence_type
Create-Directory $tmp_evidence_storage
$current_location = Get-Location
if ($record.User -ne "N/A"){
$tmp_evidence_storage += "\$($record.User)"
Create-Directory $tmp_evidence_storage
}
$commandline = $parser.cmdline
if ($commandline -match ".*#SOURCE_FILE#.*"){
$commandline = $commandline -replace ("#SOURCE_FILE#", "`"$target`"")
} elseif ($commandline -match ".*#SOURCE_DIR#.*"){
$commandline = $commandline -replace ("#SOURCE_DIR#", "`"$target`"")
}
if ($exe_full_path){
$commandline = $commandline -replace ("#PARSER#", "`"$exe_full_path`"")
} else {
$commandline = $commandline -replace ("#PARSER#", "`"$($parser.executable)`"")
}
$tmp_evidence_storage = $tmp_evidence_storage.TrimEnd("\")
if ($commandline -match ".*#DESTINATION_DIR#.*"){
$commandline = $commandline -replace ("#DESTINATION_DIR#", "`"$tmp_evidence_storage`"")
} elseif ($commandline -match ".*#DESTINATION_FILE#.*"){
$commandline = $commandline -replace ("#DESTINATION_FILE#", "`"$tmp_evidence_storage`"")
}
#Write-Host $commandline
Log-Message "[+] Executing: $commandline"
if ($parser.type -eq "inline"){
& cmd.exe /c $commandline
} elseif ($parser.type -eq "native"){
Invoke-Expression $commandline
}
#& cmd.exe /c $commandline
#Start-Job {cmd.exe /c $commandline}
}
function Main{
Log-Message "[!] Starting Evidence Parsing..."
Log-Message "[+] Using Configuration: $config"
Log-Message "[+] Using Evidence Directory: $evidence_dir"
Log-Message "[+] Reading Configuration Data..."
$config_data = Get-Configuration
Validate-Configuration $config_data
$file_copies = Get-Successful-Copy-File
$exe_locations = Check-Tools $config_data
Start-Processing $config_data $file_copies $exe_locations
}
Main