-
Notifications
You must be signed in to change notification settings - Fork 1
/
PSTrueCrypt.psm1
474 lines (402 loc) · 14.8 KB
/
PSTrueCrypt.psm1
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
using namespace 'System.Management.Automation'
using module .\src\CIM\PSTrueCrypt.CIM.psm1
using module .\src\Storage\PSTrueCrypt.Storage.psm1
using module .\src\Utility\PSTrueCrypt.Utility.psm1
using module .\src\Writer\PSTrueCrypt.Writer.psm1
using module .\src\PSTrueCrypt.CommandLine.psm1
$SUT = $False
#.ExternalHelp PSTrueCrypt-help.xml
function Mount-TrueCrypt
{
[CmdletBinding(PositionalBinding=$False)]
Param
(
[Parameter(Mandatory = $False)]
[array]$KeyfilePath,
[Parameter(Mandatory = $False)]
[System.Security.SecureString]$Password
)
DynamicParam
{
return Get-DynamicParameterValues -IsSystemUnderTest:$SUT
}
begin
{
Invoke-BeginBlock -IsSystemUnderTest:$SUT
}
process
{
try
{
$Container = Get-RegistrySubKeys | Get-SubKeyByPropertyValue -Name $PSBoundParameters.Name | Read-Container
}
catch [ItemNotFoundException]
{
Out-Error 'NoPSTrueCryptContainerFound' -Action Stop
}
# construct arguments for expression and insert token in for password...
[string]$Expression = Get-TrueCryptMountParams -TrueCryptContainerPath $Container.Location -PreferredMountDrive $Container.MountLetter -Product $Container.Product -KeyfilePath $KeyfilePath -Timestamp $Container.Timestamp
# if no password was given, then we need to start the process for of prompting for one...
if ([string]::IsNullOrEmpty($Password) -eq $True)
{
$WasConsolePromptingPrior
# check to see if session is in admin mode for console prompting...
if (Test-IsAdmin -eq $True)
{
$WasConsolePromptingPrior = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds" | Select-Object -ExpandProperty ConsolePrompting
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds" -Name ConsolePrompting -Value $True
}
[securestring]$Password = Read-Host -Prompt "Enter password" -AsSecureString
}
# this method of handling password securely has been mentioned at the following links:
# https://msdn.microsoft.com/en-us/library/system.security.securestring(v=vs.110).aspx
# https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.securestringtobstr(v=vs.110).aspx
# https://msdn.microsoft.com/en-us/library/system.intptr(v=vs.110).aspx
# https://msdn.microsoft.com/en-us/library/ewyktcaa(v=vs.110).aspx
try
{
# Create IntPassword and dispose $Password...
[System.IntPtr]$IntPassword = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)
}
catch [System.NotSupportedException]
{
# The current computer is not running Windows 2000 Service Pack 3 or later.
Out-Error 'NotSupportedException'
}
catch [System.OutOfMemoryException]
{
# OutOfMemoryException
Out-Error 'OutOfMemoryException'
}
finally
{
$Password.Dispose()
}
Start-CimLogicalDiskWatch $Container.KeyId -InstanceType 'Creation'
try
{
# Execute Expression
Invoke-Expression ($Expression -f [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($IntPassword))
}
catch [System.Exception]
{
Out-Error 'UnknownException', 'EnsureFileRecommendment'
}
finally
{
# TODO: this is crashing CLS. Is this to be called when dismount is done? Perhaps TrueCrypt is
# holding on to this pointer while container is open.
# [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemAnsi($IntPassword)
}
# if console prompting was set to false prior to this module, then set it back to false...
if ($WasConsolePromptingPrior -eq $False)
{
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds" -Name ConsolePrompting -Value $False
}
if($KeyfilePath -ne $null)
{
Edit-HistoryFile
}
}
end
{
Invoke-EndBlock -IsSystemUnderTest:$SUT
}
}
#.ExternalHelp PSTrueCrypt-help.xml
function Dismount-TrueCrypt
{
[CmdletBinding()]
Param()
DynamicParam
{
return Get-DynamicParameterValues -IsSystemUnderTest:$SUT
}
begin
{
Invoke-BeginBlock -IsSystemUnderTest:$SUT
}
process
{
$Container = Get-RegistrySubKeys | Get-SubKeyByPropertyValue -Name $PSBoundParameters.Name | Read-Container
Start-CimLogicalDiskWatch $Container.KeyId -InstanceType 'Deletion'
# construct arguments and execute expression...
[string]$Expression = Get-TrueCryptDismountParams -Drive $Container.MountLetter -Product $Container.Product
Invoke-Expression $Expression
}
end
{
Invoke-EndBlock -IsSystemUnderTest:$SUT
}
}
#.ExternalHelp PSTrueCrypt-help.xml
function Dismount-TrueCryptForceAll
{
Invoke-DismountAll -Product TrueCrypt
Invoke-DismountAll -Product VeraCrypt
}
# internal function
function Invoke-DismountAll
{
[CmdletBinding()]
Param
(
[ValidateSet("TrueCrypt", "VeraCrypt")]
[string]$Product
)
# construct arguments for Force dismount(s)...
[string]$Expression = Get-TrueCryptDismountParams -Product $Product
try
{
Invoke-Expression $Expression
$HasXCryptDismountFailed = $False
}
catch
{
$HasXCryptDismountFailed = $True
}
finally
{
if($HasXCryptDismountFailed -eq $False)
{
Out-Information 'AllProductContainersDismounted' -Format $Product
}
else
{
Out-Error 'DismountException' -Format $Product
}
}
}
#.ExternalHelp PSTrueCrypt-help.xml
function New-PSTrueCryptContainer
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $True, Position = 1)]
[ValidateNotNullOrEmpty()]
[string]$Name,
[Parameter(Mandatory = $True, Position = 2)]
[ValidateNotNullOrEmpty()]
[string]$Location,
[Parameter(Mandatory = $True, Position = 3)]
[ValidatePattern("^[a-zA-Z]$")]
[string]$MountLetter,
[Parameter(Mandatory = $True, Position = 4)]
[ValidateSet("TrueCrypt", "VeraCrypt")]
[string]$Product,
[switch]$Timestamp
)
begin
{
Invoke-BeginBlock -IsSystemUnderTest:$SUT
}
process
{
$SubKeyName = Get-RegistrySubKeys | Get-SubKeyByPropertyValue -Name $Name
if(-not ($SubKeyName) )
{
$Decision = Get-Confirmation -Message "New-PSTrueCryptContainer will add a new subkey in the following of your registry: HKCU:\SOFTWARE\PSTrueCrypt"
try
{
if ($Decision -eq $True)
{
New-Container -Name $Name -Location $Location -MountLetter $MountLetter -Product $Product -Timestamp:$Timestamp.IsPresent
Out-Information 'NewContainerOperationSucceeded' -Format $Name
}
else
{
Out-Warning 'NewContainerOperationCancelled'
}
}
catch [System.UnauthorizedAccessException]
{
# TODO: append to this message of options for a solution. solution will be determined if the user is in an elevated CLS.
Out-Error 'UnauthorizedAccessException'
}
} else {
Out-Warning 'ContainerNameAlreadyExists' -Format $Name
}
}
end
{
Invoke-EndBlock -IsSystemUnderTest:$SUT
}
}
#.ExternalHelp PSTrueCrypt-help.xml
function Edit-PSTrueCryptContainer
{
[CmdletBinding(PositionalBinding=$False)]
Param
(
[Parameter(Mandatory = $False)]
[string]$NewName,
[Parameter(Mandatory = $False)]
[string]$Location,
[Parameter(Mandatory = $False)]
[ValidatePattern("^[a-zA-Z]$")]
[string]$MountLetter,
[Parameter(Mandatory = $False)]
[ValidateSet("TrueCrypt", "VeraCrypt")]
[string]$Product,
[switch]$Timestamp
)
DynamicParam
{
return Get-DynamicParameterValues -IsSystemUnderTest:$SUT
}
begin
{
Invoke-BeginBlock -IsSystemUnderTest:$SUT
}
process
{
$Container = Get-RegistrySubKeys | Get-SubKeyByPropertyValue -Name $PSBoundParameters.Name | Read-Container
if($Container)
{
$ProposedParameterValues = [PSCustomObject]@{ Name = ($NewName, $Container.Name, 1 -notmatch "^$")[0]
Location = ($Location, $Container.Location, 1 -notmatch "^$")[0]
MountLetter = ($MountLetter, $Container.MountLetter, 1 -notmatch "^$")[0]
Product = ($Product, $Container.Product, 1 -notmatch "^$")[0]
Timestamp = $Timestamp.IsPresent }
Format-Table @{Label="Name";Expression={($_.Name)}},`
@{Label="Location";Expression={($_.Location)}},`
@{Label="MountLetter";Expression={($_.MountLetter)}},`
@{Label="Product";Expression={($_.Product)}},`
@{Label="Timestamp";Expression={($_.Timestamp)}}`
-AutoSize -InputObject $ProposedParameterValues
$Decision = Get-Confirmation -Message ("Edit-PSTrueCryptContainer will set "+$Container.Name+" with the above values.")
try
{
if ($Decision -eq $True)
{
Write-Container -KeyId $Container.KeyId `
-Name $ProposedParameterValues.Name `
-Location $ProposedParameterValues.Location `
-MountLetter $ProposedParameterValues.MountLetter `
-Product $ProposedParameterValues.Product `
-Timestamp $ProposedParameterValues.Timestamp
Out-Information 'EditContainerOperationSucceeded' -Format $ProposedParameterValues.Name
}
else
{
Out-Warning 'EditContainerOperationCancelled'
}
}
catch [System.UnauthorizedAccessException]
{
Out-Error 'UnauthorizedAccessException'
}
} else {
Out-Error 'NoPSTrueCryptContainerFound'
}
}
end
{
Invoke-EndBlock -IsSystemUnderTest:$SUT
}
}
#.ExternalHelp PSTrueCrypt-help.xml
function Remove-PSTrueCryptContainer
{
[CmdletBinding()]
Param () # value is selected from DynamicParam block
DynamicParam
{
return Get-DynamicParameterValues -IsSystemUnderTest:$SUT
}
begin
{
Invoke-BeginBlock -IsSystemUnderTest:$SUT
}
process
{
try
{
$Decision = Get-Confirmation -Message ("Remove-PSTrueCryptContainer will remove the "+$PSBoundParameters.Name+" from your registry: HKCU:\SOFTWARE\PSTrueCrypt")
if ($Decision -eq $True)
{
Get-RegistrySubKeys | Remove-SubKeyByPropertyValue -Name $PSBoundParameters.Name
Out-Information 'ContainerSettingsDeleted'
}
else
{
Out-Information 'RemoveContainerOperationCancelled'
}
}
catch [System.ObjectDisposedException]
{
#The RegistryKey being manipulated is closed (closed keys cannot be accessed).
Out-Error 'ObjectDisposedException'
}
catch [System.ArgumentException],[System.ArgumentNullException]
{
#subkey does not specify a valid registry key, and throwOnMissingSubKey is true.
#subkey is null.
Out-Error 'UnableToFindPSTrueCryptContainer' -Format $PSBoundParameters.Name
}
catch [System.Security.SecurityException]
{
#The user does not have the permissions required to delete the key.
Out-Error 'SecurityException' -Recommendment 'SecurityRecommendment'
}
catch [System.InvalidOperationException]
{
# subkey has child subkeys.
Out-Error 'InvalidOperationException'
}
catch [System.UnauthorizedAccessException]
{
#The user does not have the necessary registry rights.
Out-Error 'UnauthorizedRegistryAccessException'
}
catch
{
Out-Error 'UnknownException'
}
}
end
{
Invoke-EndBlock -IsSystemUnderTest:$SUT
}
}
#.ExternalHelp PSTrueCrypt-help.xml
function Show-PSTrueCryptContainers
{
[CmdletBinding()]
Param ()
begin
{
Invoke-BeginBlock -IsSystemUnderTest:$SUT
}
end
{
try
{
Restart-LogicalDiskCheck
$SortedContainers = Get-RegistrySubKeys | `
Read-Container | `
Sort-Object -Property @{ Expression = {$_.Name} }
Format-Table @{Label="Name";Expression={($_.Name)}},`
@{Label="Location";Expression={($_.Location)}},`
@{Label="MountLetter";Expression={($_.MountLetter)}},`
@{Label="Product";Expression={($_.Product)}},`
@{Label="Timestamp";Expression={($_.Timestamp)}},`
@{Label="IsMounted";Expression={($_.IsMounted)}},`
@{Label="Last Activity";Expression={[DateTime]($_.LastActivity)}}`
-AutoSize -InputObject $SortedContainers
}
catch [System.NullReferenceException]
{
Out-Information 'NoPSTrueCryptContainerFound'
}
finally
{
Invoke-EndBlock -IsSystemUnderTest:$SUT
}
}
}
Set-Alias -Name mt -Value Mount-TrueCrypt
Set-Alias -Name dmt -Value Dismount-TrueCrypt
Set-Alias -Name dmt* -Value Dismount-TrueCryptForceAll
Start-SystemCheck