forked from EricZimmerman/Get-ZimmermanTools
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet-KapeModuleBinaries.ps1
More file actions
412 lines (356 loc) · 14.4 KB
/
Copy pathGet-KapeModuleBinaries.ps1
File metadata and controls
412 lines (356 loc) · 14.4 KB
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
<#
.SYNOPSIS
This script will discover and download all available EXE, ZIP, and PS1 files referenced in KAPE Module files and download them to $Dest
or optionally can be fed a txt file containing URLs to download.
.DESCRIPTION
This script will discover and download all available EXE, ZIP, and PS1 files referenced in KAPE Module files and download them to $Dest
or optionally can be fed a txt file containing URLs to download.
A file will also be created in $Dest that tracks the SHA-1 of each file, so rerunning the script will only download new versions.
To redownload, remove lines from or delete the CSV file created under $Dest and rerun. Note this only works for Eric Zimmerman's tools. All others will be downloaded each time.
.PARAMETER Dest
The path you want to save the programs to. Typically this will be the Bin folder in your KAPE\Modules directory
.PARAMETER ModulePath
The path containing KAPE module files.
.PARAMETER CreateBinaryList
Optional switch which scans mkape file and dumps binary urls found to console.
.PARAMETER UseBinaryList
Optional switch to enable use of txt file to specify which binaries to download.
.PARAMETER BinaryListPath
The path of txt file containing Binary URLs.
.EXAMPLE
PS C:\Tools> .\Get-KapeModuleBinaries.ps1 -Dest "C:\Forensic Program Files\Zimmerman\Kape\Modules\Bin" -ModulePath "C:\Forensic Program Files\Zimmerman\Kape\Modules"
Downloads/extracts and saves binaries and binary details to "C:\Forensic Program Files\Zimmerman\Kape\Modules\Bin" directory.
.EXAMPLE
PS C:\Tools> .\Get-KapeModuleBinaries.ps1 -ModulePath "C:\Forensic Program Files\Zimmerman\Kape\Modules" -CreateBinaryList
Scans modules directory for mkape files, extracts URLs and dumps to console. This can be used to create a text file for use
with the -UseBinaryList and -BinaryList path parameters or just to verify which tools will be downloaded prior to running
.EXAMPLE
PS C:\Tools> .\Get-KapeModuleBinaries.ps1 -Dest "C:\Forensic Program Files\Zimmerman\Kape\Modules\Bin" -UseBinaryList -BinaryListPath C:\tools\binarylist.txt
Downloads/extracts and saves binaries and binary details for files specified in C:\tools\binarylist.txt to C:\Forensic Program Files\Zimmerman\Kape\Modules\Bin directory.
.NOTES
Author: Mike Cary
This script is a fork of Eric Zimmerman's Get-ZimmermanTools script which has been modified to parse mkape files or other txt files for urls to download
#>
[Cmdletbinding()]
Param
(
[Parameter()]
[string]$Dest= (Resolve-Path "."), # Where to save programs to
[Parameter()]
[string]$ModulePath, # Path to Kape Modules directory
[Parameter()]
[switch]$CreateBinaryList, # Extracts list of Binary URLs and dumps to console
[Parameter()]
[switch]$UseBinaryList, # Optional switch to enable use of txt file to specify which binaries to download
[string]$BinaryListPath # Path of txt file containing Binary URLs
)
# Some download sources require TLS 1.2 which PowerShell doesn't support out of the box so we are adding that here
function Enable-SSLRequirements
{
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
}
Enable-SSLRequirements
Write-Host "`nThis script will automate the downloading of binaries used by KAPE module files to $Dest" -BackgroundColor Blue
$newInstall = $false
if(!(Test-Path -Path $Dest ))
{
write-host $Dest " does not exist. Creating..."
New-Item -ItemType directory -Path $Dest > $null
$newInstall = $true
}
$WebKeyCollection = @()
$localDetailsFile = Join-Path $Dest -ChildPath "!!!RemoteFileDetails.csv"
if (Test-Path -Path $localDetailsFile)
{
write-host "Loading local details from '$Dest'..."
$LocalKeyCollection = Import-Csv -Path $localDetailsFile
}
$toDownload = @()
# Check if $UseBinaryList switch was used and import binary URLs
if($UseBinaryList){
Try
{
$BinaryContent = Get-Content $BinaryListPath -ErrorAction Stop
}
Catch
{
Write-Host "Unable to import list of Binary URLs. Verify file exists in $BinaryListPath or that you have access to this file"
}
$progressPreference = 'Continue'
$regex = [regex] '(?i)\b(http.)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$].(zip|txt|ps1|exe)'
$matchdetails = $regex.Match($BinaryContent)
}
#If $CreateBinaryList switch is used dump list of Binary URLs to console
elseif($CreateBinaryList){
Write-Host "`nDumping list of Binary URLs to console" -BackgroundColor Blue
try
{
$mkapePath = Get-ChildItem $modulePath -Recurse -Filter *.mkape -ErrorAction Stop
$mkapeContent = Get-Content $mkapePath.FullName -ErrorAction Stop
}
catch
{
Write-Host "Unable to import list of Binary URLs. Verify path to modules folder is correct or that you have access to this directory" -ForegroundColor Yellow
}
$progressPreference = 'Continue'
$regex = [regex] '(?i)\b(http.)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$].(zip|txt|ps1|exe)'
$matchdetails = $regex.Matches($mkapeContent) | Select-Object -Unique
Write-Output $matchdetails.value
break
}
# If $UseBinaryList switch wasn't used, scan mkape files for binary URLs and download
else
{
$progressPreference = 'silentlyContinue'
try
{
$mkapePath = Get-ChildItem $modulePath -Recurse -Filter *.mkape -ErrorAction Stop
$mkapeContent = Get-Content $mkapePath.FullName -ErrorAction Stop
}
catch
{
Write-Host "Unable to import list of Binary URLs. Verify path to modules folder is correct or that you have access to this directory" -ForegroundColor Yellow
}
$progressPreference = 'Continue'
$regex = [regex] '(?i)\b(http.)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$].(zip|txt|ps1|exe)'
$matchdetails = $regex.Match($mkapeContent)
}
write-host "Getting available programs..."
$progressPreference = 'silentlyContinue'
while ($matchdetails.Success) {
try
{
$headers = Invoke-WebRequest -Uri $matchdetails.Value -UseBasicParsing -Method Head -ErrorAction SilentlyContinue
# Checking to verify data was returned before updating $headers variable
if ($headers -ne $null){
$headers = $headers.headers
}
}
catch
{
$headers = @{}
$headers."x-bz-content-sha1" = "n/a"
$headers.'Content-Length' = "n/a"
$headers."x-bz-file-name" = $matchdetails.Value | Split-Path -Leaf
}
# Eric's tools have the hash available in the header so we can check these to see if we have the current version already
if($matchdetails.Value -like '*EricZimmermanTools*') {
$getUrl = $matchdetails.Value
$sha = $headers["x-bz-content-sha1"]
$name = $headers["x-bz-file-name"]
$size = $headers["Content-Length"]
$details = @{
Name = $name
SHA1 = $sha
URL = $getUrl
Size = $size
}
}
# Downloading
else
{
$getUrl = $matchdetails.Value
$sha = "N/A"
$name = $matchdetails.Value | Split-Path -Leaf
# test to verify $name doesn't contain illegal characters for a file name
$name = $name.split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$size = $headers["Content-Length"]
$details = @{
Name = $name
SHA1 = $sha
URL = $getUrl
Size = $size
}
}
$webKeyCollection += New-Object PSObject -Property $details
$matchdetails = $matchdetails.NextMatch()
}
$progressPreference = 'Continue'
$WebKeyCollection = $WebKeyCollection | Select-Object * -Unique
Foreach ($webKey in $webKeyCollection)
{
if ($newInstall)
{
$toDownload+= $webKey
continue
}
$localFile = $LocalKeyCollection | Where-Object {$_.Name -eq $webKey.Name}
if ($null -eq $localFile -or $localFile.SHA1 -ne $webKey.SHA1 -or $localFile.SHA1 -eq "N/A")
{
#Needs to be downloaded since file doesn't exist, SHA is different, or SHA is not in header to compare
$toDownload+= $webKey
}
}
if ($toDownload.Count -eq 0)
{
write-host "`nAll files current. Exiting.`n" -BackgroundColor Blue
return
}
$downloadedOK = @()
foreach($td in $toDownload)
{
try
{
$dUrl = $td.URL
$size = $td.Size
$name = $td.Name
write-host "Downloading $name (Size: $size)" -ForegroundColor Green
$destFile = Join-Path -Path $dest -ChildPath $td.Name
$progressPreference = 'silentlyContinue'
try
{
Invoke-WebRequest -Uri $dUrl -OutFile $destFile -ErrorAction Stop -UseBasicParsing
}
catch
{
$ErrorMessage = $_.Exception.Message
Write-Host "Error downloading $name : ($ErrorMessage). Verify Binary URL is correct and try again" -ForegroundColor Yellow
continue
}
$downloadedOK += $td
if ( $name.endswith("zip") )
{
# Test for Archiving cmdlets and if installed, use instead of 7zip
if (!(Get-Command Expand-Archive -ErrorAction SilentlyContinue)){
Write-Host "Archive Cmdlets not found. Verify PowerShell version 5 or greater is installed" -ForegroundColor Yellow
return
}
# Archiving cmdlets found so 7zip will not be used
else
{
$global:progressPreference = 'silentlyContinue'
try
{
Expand-Archive -Path $destFile -DestinationPath $Dest -Force -ErrorAction Stop
}
catch
{
write-host "Unable to extract file:$destFile. Verify file is not in use and that you have access to $Dest." -ForegroundColor Yellow
}
}
}
}
catch
{
$ErrorMessage = $_.Exception.Message
write-host "Error downloading $name : ($ErrorMessage). Verify Binary URL is correct and try again" -ForegroundColor Yellow
}
finally
{
$progressPreference = 'Continue'
if ( $name -ne $null){
if ( $name.endswith("zip") )
{
try
{
remove-item -Path $destFile -ErrorAction SilentlyContinue
}
catch
{
write-host "Unable to remove item: $destFile"
}
}
}
}
}
#Downloaded ok contains new stuff, but we need to account for existing stuff too
foreach($webItems in $webKeyCollection)
{
#Check what we have locally to see if it also contains what is in the web collection
$localFile = $LocalKeyCollection | Where-Object {$_.SHA1 -eq $webItems.SHA1}
#if its not null, we have a local file match against what is on the website, so its ok
if ($null -ne $localFile)
{
#consider it downloaded since SHAs match
$downloadedOK+=$webItems
}
}
# Doing some cleanup to remove files not needed by KAPE and reorganize directory names and structure in line with modules
# EvtxECmd Directory rename
# Check to make sure \EvtxExplorer is in $dest before doing anything
if (Test-Path "$Dest\EvtxExplorer"){
# Rename EvtxExplorer directory to EvtxECmd to match path in EvtxECmd.mkape
try
{
Rename-Item -Path "$Dest\EvtxExplorer" -NewName "$Dest\EvtxECmd" -Force -ErrorAction Stop
}
catch
{
Write-Host "Unable to rename $Dest\EvtxExplorer to $Dest\EvtxECmd. Directory may need to be manually renamed for EvtxECmd.mkape to function properly"
}
}
# Registry Explorer Cleanup and Reorg
# Check to make sure \RegistryExplorer is in $dest before doing anything
if (Test-Path "$Dest\RegistryExplorer"){
$reCmdDir = "$dest\ReCmd"
if(!(Test-Path -Path $ReCmdDir))
{
try
{
New-Item -ItemType directory -Path $ReCmdDir -ErrorAction Stop > $null
}
catch
{
Write-Host "Unable to create directory path: $RECmdDir. You may need to manually create \Kape\Modules\Bin\ReCmd" -ForegroundColor Yellow
}
}
$reCmdChanges = @("$Dest\RegistryExplorer\ReCmd.exe","$Dest\RegistryExplorer\BatchExamples\*.reb","$Dest\RegistryExplorer\Plugins")
foreach($change in $reCmdChanges) {
try
{
Move-Item -Path $change -Destination $ReCmdDir -Force -ErrorAction Stop
}
catch
{
Write-Host "Unable to move $change to $RECmdDir. You may need to manually move this for RECmd.mkape to function properly" -ForegroundColor Yellow
}
}
# Delete RegistryExplorer Directory
try
{
Remove-Item -Path "$Dest\RegistryExplorer" -Recurse -Force -ErrorAction Stop
}
catch
{
Write-Host "Unable to delete $Dest\RegistryExplorer" -ForegroundColor Yellow
}
}
# Additonal cleanup of tools that must reside directly in \Kape\Modules\Bin
$toolPath = @("$Dest\ShellBagsExplorer\SBECmd.exe","$Dest\win64\densityscout.exe","$Dest\sqlite-tools-win32-x86-3270200\*.exe")
foreach($tool in $toolPath){
# Check to make sure each $tool is in $dest before doing anything
if (Test-Path $tool){
try
{
Move-Item -Path $tool -Destination $Dest -Force -ErrorAction Stop
}
catch
{
Write-Host "Unable to move $tool to $Dest. You may need to manually move this for the module to function properly" -ForegroundColor Yellow
}
# Delete Tool Directory
try
{
$toolDir = $tool | Split-Path -Parent
Remove-Item -Path $toolDir -Recurse -Force -ErrorAction Stop
}
catch
{
Write-Host "Unable to delete $toolDir" -ForegroundColor Yellow
}
}
}
Write-host "`nSaving downloaded version information to $localDetailsFile`n" -ForegroundColor Red
$downloadedOK | export-csv -Path $localDetailsFile