-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke_KeyCipher.psm1
More file actions
502 lines (373 loc) · 16.5 KB
/
Invoke_KeyCipher.psm1
File metadata and controls
502 lines (373 loc) · 16.5 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
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
function Invoke-KeyCipher(){
<#
.SYNOPSIS
Invoke-KeyCipher [mode (encrypt | derypt)] [key secret] [inFilePath | string_stream] [outFilePath path_to_save]
.DESCRIPTION
Encrypt / Decrypt files
.FUNCTIONALITY
Invoke KeyCipher enciphering / deciphering python script
.EXAMPLE
File Encryption/Decryption Examples
Invoke-KeyCipher decrypt pa55w0rd .\input\File.ext .\out\File.ext
PS > Invoke-KeyCipher encrypt pa55w0rd .\input\File.ext
PS > Invoke-KeyCipher encrypt my_Secret_key 5ecret@pa55w0rd
PS > $(ls "C:\Users\ERIC\Desktop\WindowsUpdate.log") | Invoke-KeyCipher -mode encrypt -key ricoTush -lineBufferSize 9
PS > $(ls "C:\Users\ERIC\Desktop\WindowsUpdate.log") | Invoke-KeyCipher -mode encrypt -key ricoTush -fullEncryption $true
Password Hashing Examples
PS > Invoke-KeyCipher hash ricoTUSH ricoPASS
PS > Invoke-KeyCipher unhash ricoTUSH ricoPASS
.NOTES
Author: Eric Mutua
Date: 22.04.2019
Version: 0.1.0
#>
[CmdLetBinding()]
Param(
[parameter(position = 1, mandatory = $true)]
[String] $mode,
[parameter(position = 2, mandatory = $true)]
[String] $key,
[parameter(position = 3,
mandatory = $true,
ValueFromPipelineByPropertyName = $true
)]
[Alias('FullName')]
[String] $inFilePath,
[parameter(
position = 4,
mandatory = $false,
ValueFromPipelineByPropertyName = $true
)]
[Alias('DirectoryName')]
[String]
$outFilePath = $(
# Checking mode parameter
if($($mode -eq "hash") -or $($mode -eq "unhash")){
return '-'
}
else
{
# The Default Output path
$defaultOutPath = $([string]$(Join-Path $env:PUBLIC $("\Documents\"+$(@($inFilePath.split('\'))[@($inFilePath.split('\')).count - 1])).split('.')[0]));
if(-not $(Test-Path $defaultOutPath)){
mkdir $defaultOutPath
}
return $([System.String]$defaultOutPath)
}
),
[parameter(position = 5, mandatory = $false)]
[int] $lineBufferSize = 10,
[parameter(position = 6, mandatory = $false)]
[switch] $fullEncription = $false
)
begin {
$ErrorActionPreference = "Silently Continue"
$isCertUtil = $(Test-Path $env:windir\System32\certutil.exe)
Set-Alias certutil $(Join-path $env:windir "\System32\certutil.exe")
$inputFileName = $(@($inFilePath.split('\'))[@($inFilePath.split('\')).count - 1])
$inFileExt = $($inputFileName.split('.')[$inputFileName.Split('.').Length -1])
# Temporary Directory
if($outFilePath -ne '-'){
if(-not $(Test-Path $env:TEMP\$($inputFileName.split('.')[0]))){
mkdir $env:TEMP\$($inputFileName.split('.')[0]) | Out-Null
}
}
$ErrorActionPreference = "SilentlyContinue"
$base64EncodeFilePath = $(Join-path $env:TEMP\$($inputFileName.split('.')[0]) "Encoded.bs64enc")
$bsDecodeName = $inputFileName.replace($inFileExt, $($("dec64.")+$inFileExt))
$base64DecodeFilePath = $(Join-path $outFilePath $bsDecodeName)
$pt_ext = $('enc-')+$([string]$lineBufferSize)
$possibleEncipherPath = $(Join-path $env:TEMP\$($inputFileName.split('.')[0]) $inputFileName.replace($inFileExt, $($($pt_ext+'.')+$inFileExt)))
if(Test-Path $possibleEncipherPath){
$encipherFilePath = $(Join-path $env:TEMP\$($inputFileName.split('.')[0]) $inputFileName.replace($inFileExt, $($($pt_ext+'.')+$inFileExt)))
}
else
{
$encipherFilePath = $(Join-path $env:TEMP\$($inputFileName.split('.')[0]) $inputFileName.replace($inFileExt, $('enc.')+$inFileExt))
}
$decipherFilePath = $(Join-path $env:TEMP\$($inputFileName.split('.')[0]) "Deciphered.dec")
$isPython = $($($env:PATH | Select-String 'python27').Matches.Success)
$LogPath = ""
$version = $([string]$(Find-Module Invoke_KeyCipher).Version)
# Setting the module installation path
if(Test-Path $(Join-Path $PSHOME\Modules Invoke_KeyCipher)){
$moduleInstallationPath = $(Join-Path $PSHOME\Modules Invoke_KeyCipher\$version\)
}
if(Test-Path $(Join-Path ${env:ProgramFiles(x86)}\WindowsPowerShell\Modules Invoke_KeyCipher)){
$moduleInstallationPath = $(Join-Path ${env:ProgramFiles(x86)}\WindowsPowerShell\Modules Invoke_KeyCipher\$version\)
}
if(Test-Path $(Join-Path $env:ProgramFiles\WindowsPowerShell\Modules Invoke_KeyCipher)){
$moduleInstallationPath = $(Join-Path $env:ProgramFiles\WindowsPowerShell\Modules Invoke_KeyCipher\$version\)
}
}
process
{
# Check if $base64EncodeFilePath is complete to take care of pipeline values
# For pipeline values only
# Begin Section for pipelined input parameters
if(-not $base64EncodeFilePath.Contains($(@($inFilePath.split('\'))[@($inFilePath.split('\')).count - 1]))){
$ErrorActionPreference = "Silently Continue"
$isCertUtil = $(Test-Path $env:windir\System32\certutil.exe)
Set-Alias certutil $(Join-path $env:windir "\System32\certutil.exe")
$iFileName = $(@($inFilePath.split('\'))[@($inFilePath.split('\')).count - 1])
# Make the temp directory
if($outFilePath -ne '-'){
if(-not $(Test-Path $env:TEMP\$($iFileName.split('.')[0]))){
mkdir $env:TEMP\$($iFileName.split('.')[0]) | Out-Null
}
}
$p_ext = $('enc-')+$([string]$lineBufferSize)
$iFileExt = $($iFileName.split('.')[$iFileName.Split('.').Length - 1])
$bsDecodeName = $iFileName.replace($iFileExt, $($("dec64.")+$iFileExt))
# Set paths
$base64EncodeFilePath = $(Join-path $env:TEMP\$($iFileName.split('.')[0]) "Encoded.bs64enc")
$psbEncipherPath = $(Join-path $env:TEMP\$($iFileName.split('.')[0]) $iFileName.replace($iFileExt, $($($p_ext+'.')+$iFileExt)))
if(Test-Path $psbEncipherPath){
$encipherFilePath = $(Join-path $env:TEMP\$($iFileName.split('.')[0]) $iFileName.replace($iFileExt, $($($p_ext+'.')+$iFileExt)))
}
else
{
$encipherFilePath = $(Join-path $env:TEMP\$($iFileName.split('.')[0]) $iFileName.replace($iFileExt, $('enc.')+$iFileExt))
}
$decipherFilePath = $(Join-path $env:TEMP\$($iFileName.split('.')[0]) "Deciphered.dec")
$base64DecodeFilePath = $(Join-path $outFilePath $bsDecodeName)
$isPython = $($($env:PATH | Select-String 'python').Matches.Success)
$LogPath = ""
$isEncipherFilePartial = $($encipherFilePath.Split('enc')[1] -eq $($p_ext.Split('enc')[1]+'.'+$iFileExt))
$inputFileName = $iFileName;
$pt_ext = $p_ext;
$inFileExt = $iFileExt;
}
# End Begin Section for pipelined values
if( -not $(Test-Path $(Join-path $env:TEMP "\KeyCipher"))){
mkdir $(Join-path $env:TEMP "\KeyCipher") | Out-Null
}
# Instantiate Log file
$LogPath = $(Join-path $env:TEMP "\KeyCipher\KeyCipher.log")
switch ($mode) {
$("encrypt")
{
# Encrypting Files
Write-Host "[+] Beginning File Encryption ..." -ForegroundColor Green
base64Encode($isCertUtil, $inFilePath, $LogPath)
encipherFile($base64EncodeFilePath, $encipherFilePath, $key, $LogPath, $isPython, $pt_ext)
Write-Host "[+] Done Encrypting" -ForegroundColor Green
}
$("decrypt")
{
# Decrypting File
Write-Host "[+] Beginning File Decryption ..." -ForegroundColor Cyan
decipherFile($decipherFilePath, $encipherFilePath, $key, $LogPath, $isPython, $pt_ext, $inFileExt, $isEncipherFilePartial)
$decryptionStatus = base64Decode($decipherFilePath, $base64DecodeFilePath, $LogPath)
if($($decryptionStatus -ne 'Nul') -or $($decryptionStatus) -ne ''){
Write-Host $($("[+] ")+$decryptionStatus) -ForegroundColor Magenta
}
else
{
Write-Host "[+] Done Decrypting" -ForegroundColor Magenta
}
}
$("hash")
{
# Password hashing
Write-Host "[+] Beginning Password hashing ..." -ForegroundColor Green
$keyCipherStream = $(Join-Path $moduleInstallationPath 'KeyCipher_stream_encrypter.py')
if($isPython -and $(Test-Path $keyCipherStream)){
if($inFilePath.Contains('/') -or $inFilePath.Contains('\')){
Write-Host "[!] Warning: The string you are trying to encrypt could be a path" -ForegroundColor Yellow
}
$passHash = $(python $keyCipherStream --encrypt $key $inFilePath -m)
Set-Content -Path $(Join-Path $env:TEMP passHash) -Value $passHash
return "[Hash] "+$passHash+"`n[+] Done"
}
}
$("unhash")
{
#Password unhashing
Write-Host "[+] Begninnig Password unhashing ..." -ForegroundColor Cyan
$keyCipherStream = $(Join-Path $moduleInstallationPath 'KeyCipher_stream_encrypter.py')
if($isPython -and $(Test-Path $keyCipherStream)){
$unhashedPass = $(Get-Content -Path $(Join-Path $env:TEMP passHash))
if(Test-Path $(Join-Path $env:TEMP passHash)){Remove-Item $(Join-Path $env:TEMP passHash)}
return "[Password] "+$(python $keyCipherStream --decrypt $key $unhashedPass -m)+"`n[+] Done"
}
}
Default {Write-Host "[!] Please Refer to the help for appropriate mode" -ForegroundColor Yellow}
}
}
end
{
}
}
# Encode function
function base64Encode(){
if($isCertUtil)
{
certutil -encode $inFilePath $base64EncodeFilePath
}
else
{
"["+$(Get-Date)+"][base64Encode] :: ERROR :: File Not Found (certutil.exe).`n" >> $LogPath
# Alternative for certutil.exe
Out-File -InputObject $(python $(Join-Path $moduleInstallationPath '/base64.py') -e $inFilePath) -Path $base64EncodeFilePath
}
}
# Decode function
function base64Decode()
{
$isdecipherFilePath = $(Test-path $decipherFilePath)
if($isCertUtil)
{
if($isdecipherFilePath)
{
certutil -decode $decipherFilePath $base64DecodeFilePath | Out-Null
if($? -ne $true){
"["+$(Get-Date)+"][base64Decode] :: ERROR :: Incorrect Key attempted decryption.`n" >> $LogPath
return "Wrong Key! You are not authorised to Decrypt File"
}
else{
Write-host "[+] Removing temporary files..." -ForegroundColor Gray
if(Test-Path $(Join-path $outFilePath $inputFileName.replace($inFileExt, $($($pt_ext+'.')+$inFileExt)))){Remove-Item $(Join-path $outFilePath $inputFileName.replace($inFileExt, $($($pt_ext+'.')+$inFileExt)))}
if(Test-Path $(Join-path $outFilePath $inputFileName.replace($inFileExt, $('enc.')+$inFileExt))){Remove-Item $(Join-path $outFilePath $inputFileName.replace($inFileExt, $('enc.')+$inFileExt))}
if(Test-Path $env:TEMP\$($inputFileName.split('.')[0])){Remove-Item -Recurse $env:TEMP\$($inputFileName.split('.')[0]) }
return "Done"
}
}
else
{
"["+$(Get-Date)+"][base64Decode] :: ERROR :: File Not Found ("+ $decipherFilePath +").`n" >> $LogPath
return 'Nul'
}
}
else
{
"["+$(Get-Date)+"][base64Decode] :: ERROR :: File Not Found (certutil.exe).`n" >> $LogPath
# Alternative for certutil.exe
Out-File -InputObject $(python $(Join-Path $moduleInstallationPath '/base64.py') -d $decipherFilePath) -Path $base64DecodeFilePath
if(Test-Path $base64DecodeFilePath){Copy-Item $base64DecodeFilePath $outFilePath}
if(Test-Path $(Join-path $outFilePath $inputFileName.replace($inFileExt, $($($pt_ext+'.')+$inFileExt)))){Remove-Item $(Join-path $outFilePath $inputFileName.replace($inFileExt, $($($pt_ext+'.')+$inFileExt)))}
if(Test-Path $(Join-path $outFilePath $inputFileName.replace($inFileExt, $('enc.')+$inFileExt))){Remove-Item $(Join-path $outFilePath $inputFileName.replace($inFileExt, $('enc.')+$inFileExt))}
if(Test-Path $env:TEMP\$($inputFileName.split('.')[0])){Remove-Item -Recurse $env:TEMP\$($inputFileName.split('.')[0]) }
return "Done"
}
}
function encipherFile()
{
$isBase64EncodeFilePath = $(Test-Path $base64EncodeFilePath)
if($isBase64EncodeFilePath){
if($isPython)
{
Write-host "[+] File Enciphering ..." -ForegroundColor Gray
$base64EncBuffer = $(Get-Content $base64EncodeFilePath)
$sizeBase64Buffer = $base64EncBuffer.Count
# Partial File Encryption
if ($($sizeBase64Buffer -gt $lineBufferSize) -and $(-not $fullEncription)){
$innerLineBuffer = $($base64EncBuffer | Select-Object -First $lineBufferSize)
$_divisor = [int]$innerLineBuffer.Count / 10
forEach($innerLine in $innerLineBuffer){
$actual_pcnt = [int]$innerLineBuffer.indexOf($innerLine) / $_divisor
# Show Progress
Show-ProgressBar($actual_pcnt, "Encrypting ")
$enc_line = $(python $(Join-Path $moduleInstallationPath 'KeyCipher_stream_encrypter.py') --encrypt $key $innerLine -m)
Out-File -FilePath $($encipherFilePath.Replace('enc', $pt_ext)) -Append -InputObject $enc_line -Encoding string
}
# Append the unecrypted text
$unencryptedLines = $($base64EncBuffer | Select-Object -Last $($sizeBase64Buffer - $lineBufferSize))
Out-File -FilePath $($encipherFilePath.Replace('enc', $pt_ext)) -Append -InputObject $unencryptedLines -Encoding string
Write-host "[+] Verifying and Saving Encrypted File..." -ForegroundColor Gray
if(Test-Path $base64EncodeFilePath){Remove-Item $base64EncodeFilePath}
if(Test-Path $($encipherFilePath.Replace('enc', $pt_ext))){Copy-Item $($encipherFilePath.Replace('enc', $pt_ext)) $outFilePath}
}
else
{
# Full file Encryption
$divisor = [int]$sizeBase64Buffer / 10
forEach($line in $base64EncBuffer)
{
$actual_pcnt = [int]$base64EncBuffer.indexOf($line) / $divisor
# Show Progress
Show-ProgressBar($actual_pcnt, "Encrypting ")
# Encryption is done line by line
$enc_line = $(python $(Join-Path $moduleInstallationPath 'KeyCipher_stream_encrypter.py') --encrypt $key $line -m)
Out-File -FilePath $encipherFilePath -Append -InputObject $enc_line -Encoding string
}
Write-host "[+] Verifying and Saving Encrypted File..." -ForegroundColor Gray
if(Test-Path $base64EncodeFilePath){Remove-Item $base64EncodeFilePath}
#Debug
Write-Host $encipherFilePath, $outFilePath
if(Test-Path $encipherFilePath){Copy-Item $encipherFilePath $outFilePath}
}
}
else
{
"["+$(Get-Date)+"][encipherFile] :: ERROR :: keymode not set or Python not found.`n" >> $LogPath
}
}
else
{
"["+$(Get-Date)+"][encipherFile] :: ERROR :: File Not Found ("+ $base64EncodeFilePath +").`n" >> $LogPath
}
}
function decipherFile(){
$isEncipherFilePath = $(Test-Path $encipherFilePath)
if($isEncipherFilePath){
if($isPython)
{
Write-host "[+] File Deciphering ..." -ForegroundColor Gray
$encipherFileBuffer = $(Get-Content $encipherFilePath)
$sizeEncipherBuffer = $encipherFileBuffer.Count
if(-not $(Get-Variable -Name isEncipherFilePartial).IsValidValue($isEncipherFilePartial)){
$isEncipherFilePartial = $($encipherFilePath.Split('enc')[1] -eq $($pt_ext.Split('enc')[1]+'.'+$inFileExt))
}
# For partial File Decryption
if($isEncipherFilePartial){
$partialEncFileBuffer = $($encipherFileBuffer | Select-Object -First $lineBufferSize)
$dvs = [int]$partialEncFileBuffer.Count / 10
forEach($line in $partialEncFileBuffer){
$actual_pcnt = [int]$partialEncFileBuffer.indexOf($line) / $dvs
# Show Progress
Show-ProgressBar($actual_pcnt, "Decrypting ")
$dec_line = $(python $(Join-Path $moduleInstallationPath 'KeyCipher_stream_encrypter.py') --decrypt $key $line -m)
Out-File -FilePath $decipherFilePath -Append -InputObject $dec_line -Encoding string
}
$base64FileBufferRem = $($encipherFileBuffer | Select-Object -Last $($sizeEncipherBuffer - $lineBufferSize))
Out-File -FilePath $decipherFilePath -Append -InputObject $base64FileBufferRem -Encoding string
# Clean up
Write-host "[+] Removing temporary files..." -ForegroundColor Gray
if(Test-Path $base64DecodeFilePath){Remove-Item $base64DecodeFilePath}
}
else
{
$divisor = [int]$sizeEncipherBuffer / 10
# For full File Decryption
forEach($line in $encipherFileBuffer)
{
$actual_pcnt = [int]$encipherFileBuffer.indexOf($line) / $divisor
# Show Progress
Show-ProgressBar($actual_pcnt, "Decrypting ")
$dec_line = $(python $(Join-Path $moduleInstallationPath 'KeyCipher_stream_encrypter.py') --decrypt $key $line -m)
Out-File -FilePath $decipherFilePath -Append -InputObject $dec_line -Encoding string
}
# Clean up
Write-host "[+] Removing temporary files..." -ForegroundColor Gray
if(Test-Path $base64DecodeFilePath){Remove-Item $base64DecodeFilePath}
}
}
else
{
"["+$(Get-Date)+"][decipherFile] :: ERROR :: keymode not set or Python not found.`n" >> $LogPath
}
}
else
{
"["+$(Get-Date)+"][decipherFile]:: ERROR :: File Not Found ("+ $encipherFilePath +").`n" >> $LogPath
}
}
function Show-ProgressBar($params){
$act_pcnt = $params[0]
$action = $params[1]
$show_pcnt = [int]$($($act_pcnt) * 10)
Write-Progress -Activity $($action+$([string]$inputFileName))`
-Status $([string]$show_pcnt+"% Complete")`
-PercentComplete $($act_pcnt * 10)
}