Auditing/Bitlocker.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
function Get-CSBitlockerKeyProtector {
<#
.SYNOPSIS
 
Obtains Bitlocker volume key material.
 
Author: Matthew Graeber (@mattifestation)
Inspiration/Motivation: Jared Atkinson (@jaredcatkinson), Brian Reitz (@brian_psu)
License: BSD 3-Clause
 
.DESCRIPTION
 
Get-CSBitlockerKeyProtector retrieves key material for BitLocker volumes. This function is used for the purposes of remotely decrypting and mounting Bitlocker volumes. Depending on the Bitlocker key protector used, Get-CSBitlockerKeyProtector will return any combination of the NumericalPassword, ExternalKey, KeyPackage, or Certificate.
 
.PARAMETER DriveLetter
 
Specifies the drive letter for the volume you want to obtain key Bitlocker key material from. If DriveLetter is not specified, key material for all encryptable volumes will be returned.
 
.PARAMETER CimSession
 
Specifies the CIM session to use for this cmdlet. Enter a variable that contains the CIM session or a command that creates or gets the CIM session, such as the New-CimSession or Get-CimSession cmdlets. For more information, see about_CimSessions.
 
.EXAMPLE
 
Get-CSBitlockerKeyProtector
 
List all recoverable Bitlocker key material for each encryptable volume.
 
.EXAMPLE
 
Get-CSBitlockerKeyProtector -DriveLetter C:
 
List all recoverable Bitlocker key material for the C: volume.
 
.OUTPUTS
 
CimSweep.BitlockerKeyProtector
 
Outputs objects representing Bitlocker volume key material.
#>


    [CmdletBinding()]
    [OutputType('CimSweep.BitlockerKeyProtector')]
    param(
        [ValidatePattern('^[A-Z]:?$')]
        [String]
        $DriveLetter,

        [Alias('Session')]
        [ValidateNotNullOrEmpty()]
        [Microsoft.Management.Infrastructure.CimSession[]]
        $CimSession
    )

    BEGIN {
        # If a CIM session is not provided, trick the function into thinking there is one.
        if (-not $PSBoundParameters['CimSession']) {
            $CimSession = ''
            $CIMSessionCount = 1
        } else {
            $CIMSessionCount = $CimSession.Count
        }

        $CurrentCIMSession = 0

        $KeyProtectorTypes = @{
            0 = 'Unknown or other protector type'
            1 = 'Trusted Platform Module (TPM)'
            2 = 'External key'
            3 = 'Numerical password'
            4 = 'TPM And PIN'
            5 = 'TPM And Startup Key'
            6 = 'TPM And PIN And Startup Key'
            7 = 'Public Key'
            8 = 'Passphrase'
            9 = 'TPM Certificate'
            10 = 'CryptoAPI Next Generation (CNG) Protector'
        }
    }

    PROCESS {
        foreach ($Session in $CimSession) {
            $ComputerName = $Session.ComputerName
            if (-not $Session.ComputerName) { $ComputerName = 'localhost' }

            # Display a progress activity for each CIM session
            Write-Progress -Id 1 -Activity 'CimSweep - Bitlocker key protector sweep' -Status "($($CurrentCIMSession+1)/$($CIMSessionCount)) Current computer: $ComputerName" -PercentComplete (($CurrentCIMSession / $CIMSessionCount) * 100)
            $CurrentCIMSession++

            $CommonArgs = @{}
            if ($Session.Id) { $CommonArgs['CimSession'] = $Session }
            if ($OperationTimeoutSec) { $CommonArgs['OperationTimeoutSec'] = $OperationTimeoutSec }

            $VolumeArgs = @{
                Namespace = 'root/cimv2/security/microsoftvolumeencryption'
                ClassName = 'Win32_EncryptableVolume'
                Property = 'ProtectionStatus', 'DriveLetter', 'DeviceID'
            }

            if ($DriveLetter) { $VolumeArgs['Filter'] = "DriveLetter='$($DriveLetter[0]):'" }

            $Volumes = @(Get-CimInstance @CommonArgs @VolumeArgs)

            $VolCount = 0

            foreach ($CurrentVolume in $Volumes) {
                Write-Progress -Id 2 -ParentId 1 -Activity "Current volume:" -Status "($($VolCount+1)/$($Volumes.Count)) $($CurrentVolume.DriveLetter)" -PercentComplete (($VolCount / $Volumes.Count) * 100)
                $VolCount++

                if ($CurrentVolume.ProtectionStatus -ne 1) {
                    Write-Warning "[$ComputerName] Drive letter $($CurrentVolume.DriveLetter) is not Bitlocker protected."
                    continue
                }

                $Result = Invoke-CimMethod -CimInstance $CurrentVolume -MethodName GetKeyProtectors @CommonArgs

                if ($Result.ReturnValue) {
                    Write-Error "[$ComputerName] Unable to obtain key protectors from drive letter: $($CurrentVolume.DriveLetter)"
                    continue
                }

                $ProtectorCount = 0
                $KeyProtectorIDs = @($Result.VolumeKeyProtectorID)

                foreach ($ProtectorID in $KeyProtectorIDs) {
                    Write-Progress -Id 3 -ParentId 2 -Activity "Current key protector ID:" -Status "($($ProtectorCount+1)/$($KeyProtectorIDs.Count)) $ProtectorID" -PercentComplete (($ProtectorCount / $KeyProtectorIDs.Count) * 100)
                    $ProtectorCount++

                    $MethodArgs = @{
                        CimInstance = $CurrentVolume
                        Arguments = @{ VolumeKeyProtectorID = $ProtectorID }
                    }

                    $Result = Invoke-CimMethod -MethodName GetKeyProtectorType @CommonArgs @MethodArgs

                    if ($Result.ReturnValue) {
                        Write-Error "[$ComputerName] Unable to obtain key protector type for drive letter: $($CurrentVolume.DriveLetter), VolumeKeyProtectorID: $ProtectorID"
                        continue
                    }

                    $KeyProtectorFriendlyName = $KeyProtectorTypes[[Int] $Result.KeyProtectorType]

                    $ExternalKeyFileName = $null
                    $ExternalKeyBytes = $null
                    $NumericalPassword = $null
                    $KeyPackage = $null
                    $KeyProtectorCertificate = $null

                    $ErrorTemplate = "for drive letter: $($CurrentVolume.DriveLetter), VolumeKeyProtectorID: $ProtectorID"

                    switch ($KeyProtectorFriendlyName) {
                        'External key' {
                            $Result = Invoke-CimMethod -MethodName GetExternalKeyFileName @CommonArgs @MethodArgs
                            
                            if ($Result.ReturnValue) {
                                Write-Error "[$ComputerName] Unable to obtain external key file name $ErrorTemplate"
                                continue
                            }

                            $ExternalKeyFileName = $Result.FileName

                            $Result = Invoke-CimMethod -MethodName GetKeyProtectorExternalKey @CommonArgs @MethodArgs

                            if ($Result.ReturnValue) {
                                Write-Error "[$ComputerName] Unable to obtain external key bytes $ErrorTemplate"
                                continue
                            }

                            $ExternalKeyBytes = $Result.ExternalKey

                            $Result = Invoke-CimMethod -MethodName GetKeyPackage @CommonArgs @MethodArgs

                            if ($Result.ReturnValue) {
                                Write-Error "[$ComputerName] Unable to obtain key package $ErrorTemplate"
                                continue
                            }

                            $KeyPackage = $Result.KeyPackage
                        }

                        'Numerical password' {
                            $Result = Invoke-CimMethod -MethodName GetKeyProtectorNumericalPassword @CommonArgs @MethodArgs

                            if ($Result.ReturnValue) {
                                Write-Error "[$ComputerName] Unable to obtain numerical password $ErrorTemplate"
                                continue
                            }

                            $NumericalPassword = $Result.NumericalPassword

                            $Result = Invoke-CimMethod -MethodName GetKeyPackage @CommonArgs @MethodArgs

                            if ($Result.ReturnValue) {
                                Write-Error "[$ComputerName] Unable to obtain key package $ErrorTemplate"
                                continue
                            }

                            $KeyPackage = $Result.KeyPackage
                        }

                        'TPM And Startup Key' {
                            $Result = Invoke-CimMethod -MethodName GetExternalKeyFileName @CommonArgs @MethodArgs

                            if ($Result.ReturnValue) {
                                Write-Error "[$ComputerName] Unable to obtain external key file name $ErrorTemplate"
                                continue
                            }
        
                            $ExternalKeyFileName = $Result.FileName

                            $Result = Invoke-CimMethod -MethodName GetKeyProtectorExternalKey @CommonArgs @MethodArgs

                            if ($Result.ReturnValue) {
                                Write-Error "[$ComputerName] Unable to obtain external key bytes $ErrorTemplate"
                                continue
                            }

                            $ExternalKeyBytes = $Result.ExternalKey
                        }

                        'TPM And PIN And Startup Key' {
                            $Result = Invoke-CimMethod -MethodName GetExternalKeyFileName @CommonArgs @MethodArgs

                            if ($Result.ReturnValue) {
                                Write-Error "[$ComputerName] Unable to obtain external key file name $ErrorTemplate"
                                continue
                            }
        
                            $ExternalKeyFileName = $Result.FileName

                            $Result = Invoke-CimMethod -MethodName GetKeyProtectorExternalKey @CommonArgs @MethodArgs

                            if ($Result.ReturnValue) {
                                Write-Error "[$ComputerName] Unable to obtain external key bytes $ErrorTemplate"
                                continue
                            }

                            $ExternalKeyBytes = $Result.ExternalKey
                        }

                        'Public Key' {
                            $Result = Invoke-CimMethod -CimInstance $CurrentVolume -MethodName GetKeyProtectorCertificate -Arguments @{ VolumeKeyProtectorID = $ProtectorID } @CommonArgs
        
                            if ($Result.ReturnValue) {
                                Write-Error "[$ComputerName] Unable to obtain protector certificate $ErrorTemplate"
                                continue
                            }

                            $KeyProtectorCertificate = [PSCustomObject] @{
                                PSTypeName = 'CimSweep.KeyProtector.BitlockerKeyProtector'
                                PublicKey = $Result.PublicKey
                                CertThumbprint = $Result.CertThumbprint
                                CertType = if ($Result.CertType -eq 1) { 'DataRecoveryAgent' } else { 'UserCertificate' }
                            }
                        }
                    }

                    $ProtectorProperties = [Ordered] @{
                        PSTypeName = 'CimSweep.BitlockerKeyProtector'
                        DriveLetter = $CurrentVolume.DriveLetter
                        DeviceID = $CurrentVolume.DeviceID
                        KeyProtectorID = $ProtectorID
                        KeyProtectorFriendlyName = $KeyProtectorFriendlyName
                        NumericalPassword = $NumericalPassword
                        ExternalKeyFileName = $ExternalKeyFileName
                        ExternalKeyBytes = $ExternalKeyBytes
                        KeyPackage = $KeyPackage
                        Certificate = $KeyProtectorCertificate
                    }

                    if ($CurrentVolume.PSComputerName) { $ProtectorProperties['PSComputerName'] = $CurrentVolume.PSComputerName }

                    [PSCustomObject] $ProtectorProperties
                }
            }
        }
    }
}

Export-ModuleMember -Function Get-CSBitlockerKeyProtector