-
Notifications
You must be signed in to change notification settings - Fork 4
/
user-key-onboarding.ps1
315 lines (223 loc) · 9.48 KB
/
user-key-onboarding.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
function Get-ADUserMasterKeyLocation {
$userSID = (New-Object System.Security.Principal.NTAccount($env:username)).Translate([System.Security.Principal.SecurityIdentifier]).value
$mkPath = $env:APPDATA + "\Microsoft\Protect\" + $userSID
return $mkPath;
}
function Get-CurrentFileTimeBytes {
$ticksNow = (get-date).ToFileTimeUtc();
[byte[]] $ftNowByte = [System.BitConverter]::GetBytes($ticksNow);
return $ftNowByte
}
function Describe-PreferredFile {
param(
[Parameter (Mandatory = $true)] [Byte[]]$preferredBytes
)
# Debug
#Format-Hex -InputObject $preferredBytes
## Describe Guid
[byte[]] $guidBytes = $preferredBytes[0..15]
[Guid] $mkGuid = [Guid]($guidBytes);
Write-Output ("[-] Preferred Master key Guid: " + $mkGuid.Guid);
## Describe current expiration date
[byte[]] $ftMK = $preferredBytes[16..23];
[int64] $ftMKTicks = [System.BitConverter]::ToInt64($ftMK,0);
$currExp = [datetime]::FromFileTimeUTC($ftMKTicks);
Write-Output ("[-] Preferred Master key Expiration: " + $currExp + " UTC");
}
function Get-MasterKeyBkpGuid {
param(
[Parameter (Mandatory = $true)] [Byte[]]$masterKeyBytes
)
[int] $mkHeaderLengthsOffset = 12+72+12
[int] $mkHeaderLen = $mkHeaderLengthsOffset + (8*4);
[byte[]] $mkSec1LenByte = $masterKeyBytes[$mkHeaderLengthsOffset..($mkHeaderLengthsOffset+7)]
[uint64] $mkSec1Len = [System.BitConverter]::ToUint64($mkSec1LenByte, 0);
[byte[]] $mkSec2LenByte = $masterKeyBytes[($mkHeaderLengthsOffset+8)..($mkHeaderLengthsOffset+7+8)]
[uint64] $mkSec2Len = [System.BitConverter]::ToUint64($mkSec2LenByte, 0);
[byte[]] $mkSec3LenByte = $masterKeyBytes[($mkHeaderLengthsOffset+16)..($mkHeaderLengthsOffset+7+16)]
[uint64] $mkSec3Len = [System.BitConverter]::ToUint64($mkSec3LenByte, 0);
[byte[]] $mkSec4LenByte = $masterKeyBytes[($mkHeaderLengthsOffset+24)..($mkHeaderLengthsOffset+7+24)]
[uint64] $mkSec4Len = [System.BitConverter]::ToUint64($mkSec4LenByte, 0);
[int] $domBkpKeyOffset = $mkHeaderLen + $mkSec1Len + $mkSec2Len + $mkSec3Len
[byte[]] $domBkpKeyBytes = $masterKeyBytes[$domBkpKeyOffset..($domBkpKeyOffset+$mkSec4Len)]
[byte[]] $domBkpKeyGuidBytes = $domBkpKeyBytes[12..27]
Try { [Guid] $bkpGuid = [Guid]($domBkpKeyGuidBytes); }
Catch { [Guid] $bkpGuid = [GUID]::Empty; }
return $bkpGuid
}
function Get-PreferredFile {
param(
[Parameter (Mandatory = $true)] [String]$preferredPath
)
$preferredPath += "\Preferred";
#Write-OUtput $preferredPath
[byte[]]$preferredFile = Get-Content $preferredPath -Encoding byte
return ,$preferredFile
}
function Get-MasterKeyFile {
param(
[Parameter (Mandatory = $true)] $mkLocation,
[Parameter (Mandatory = $true)] [String]$mkName
)
$mkPath = $mkLocation + "\" + $mkName;
[byte[]]$mkFile = Get-Content $mkPath -Encoding byte
return ,$mkFile
}
function Get-PreferredMasterKeyGuid {
param(
[Parameter (Mandatory = $true)] [String]$mkLocation
)
[byte[]] $prefBytes = Get-PreferredFile($mkLocation);
[byte[]] $guidBytes = $prefBytes[0..15]
[Guid] $prefGuid = [Guid]($guidBytes);
return $prefGuid
}
function Map-ADUserMasterKeys {
param(
[Parameter (Mandatory = $true)] [String]$mkPath
)
$myMKs = Get-ChildItem -Path $mkPath -Hidden | Where{$_.Name -match "[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}"};
$mkMap = [System.Collections.ArrayList]::new();
ForEach ($mk in $myMKs)
{
$prefFlag = $false;
$preferredGuidStr = (Get-PreferredMasterKeyGuid($mkPath)).ToString();
if ($preferredGuidStr -eq $mk.Name) { $prefFlag = $true};
[byte[]] $mkFileBytes = Get-MasterKeyFile $mkPath $mk.Name;
[Guid] $mkBkpGuid = Get-MasterKeyBkpGuid($mkFileBytes);
$null = $mkMap.Add(
[PSCustomObject]@{
PSTypeName = "MasterKeyBkp"
isPreferred = $prefFlag
Modified = $mk.LastWriteTimeUTC
Guid = $mk.Name
Backup_Guid = $mkBkpGuid
})
}
return $mkMap;
}
function Expire-MasterKey {
param(
[Parameter (Mandatory = $true)] [Byte[]]$GuidBytes,
[Parameter (Mandatory = $true)] [String]$preferredPath
)
Write-Output ("[+] Setting expiration to: " + [datetime]::UtcNow);
$preferredPath += "\Preferred";
[byte[]] $newExpiration = Get-CurrentFileTimeBytes
[byte[]] $newPreferred = $GuidBytes + $newExpiration;
Write-Output ("[+] Saving Data to: " + $preferredPath);
Set-Content -Path $preferredPath -Value $newPreferred -Encoding Byte;
}
function Get-BKPublicLocation {
param(
[Parameter (Mandatory = $true)] [String]$mkPath
)
$nbDomainName = $env:userdomain;
$bkName = "BK-" + $nbDomainName;
$bkPath = ($mkPath + "\" + $bkName);
return $bkPath;
}
function Rename-BKPublic {
param(
[Parameter (Mandatory = $true)] [String]$bkPath
)
$bkName = Split-Path -Path $bkPath -Leaf;
$newbkName = $bkName + "_" + (get-date).ticks;
Write-Output ("[+] Renaming old public key: " + $bkName + " to " + $newbkName );
Rename-Item -Path $bkPath -NewName ($newbkName);
}
function Trigger-DPAPIProtect {
[byte[]] $dummyData = @(0x44, 0x55, 0x4D, 0x4D, 0x59);
[byte[]] $dummyEntropy = @(0x1);
$scope = [System.Security.Cryptography.DataProtectionScope]::CurrentUser;
[System.Security.Cryptography.ProtectedData]::Protect($dummyData, $dummyEntropy, $scope);
}
function Invoke-BkpOnboard {
param(
[Parameter (Mandatory = $true)] [String]$Mode,
[Parameter (Mandatory = $false)] [String]$BackupGuid
)
if (($env:USERDNSDOMAIN).length -le 2){
Write-Output ("[!] ERROR: Not in a domain context");
exit;
}
Add-Type -AssemblyName System.Security
# Vars
[String] $mkLocation = (Get-ADUserMasterKeyLocation);
[byte[]] $prefBytes = Get-PreferredFile($mkLocation);
$initialMkMap = Map-ADUserMasterKeys($mkLocation) | sort Modified -Descending;
if ($Mode -eq "Info")
{
Write-Output ("");
Describe-PreferredFile($prefBytes);
Write-Output ("");
Write-Output ("[-] Mapping User Master keys");
$initialMkMap;
}
if (($Mode -eq "Check") -and ($BackupGuid))
{
Write-Output ("");
Write-Output ("[-] Checking for Master keys encrypted with Backup key: $BackupGuid");
$checkMkwithBkp = $initialMkMap | Where-Object {$_.Backup_Guid -eq $BackupGuid};
$checkMkwithBkp;
$checkMkwithBkpLength = 0;
foreach ($obj in $checkMkwithBkp) {$checkMkwithBkpLength += 1}
if ($checkMkwithBkpLength -lt 1)
{
Write-Output ("[+] No Master keys found!");
}
}
if ($Mode -eq "Exec")
{
# AD connection test
Write-Output ("");
Write-Output ("[-] Checking connection to the Active Directory: " + $env:USERDNSDOMAIN);
$adconnection = Test-Connection -ComputerName $env:USERDNSDOMAIN -Protocol DCOM -Count 2 -Quiet;
if (-Not $adconnection){
Write-Output ("[!] Cannot reach the Active Directory, exiting!");
return;
}
# Expire current Master key
Write-Output ("");
Expire-MasterKey $prefBytes[0..15] $mkLocation;
# Rename the older public Bakcup key
Write-Output ("");
$bkPublicPath = Get-BKPublicLocation -mkPath $mkLocation;
$bkPublicPath;
if (Test-Path -Path $bkPublicPath -PathType leaf) {
Rename-BKPublic -bkPath $bkPublicPath
}
else {
Write-Output ("[!] Public backup key does not exist or not in the correct path: " + $bkPath);
return;
}
Start-Sleep -Seconds 2 # Wait for file operations
# Trigger DPAPI
Write-Output ("");
Write-Output ("[-] Triggering DPAPI Master key generation...");
$dpapi = Trigger-DPAPIProtect;
Start-Sleep -Seconds 3 # Wait for DPAPI operation.
if (Test-Path -Path $bkPublicPath -PathType leaf) {
Write-Output ("[+] DPAPI latest public key fetched successfully");
}
else {
Write-Output ("[!] Error: public backup key was not fetchedt! Master Key Backup will be downgraded to the legacy key.");
Get-ChildItem -Path $mkLocation -Force;
return;
}
Write-Output ("");
[byte[]] $newPrefBytes = Get-PreferredFile($mkLocation);
Describe-PreferredFile($newPrefBytes);
$newMkMap = Map-ADUserMasterKeys($mkLocation) | sort Modified -Descending;
Write-Output ("");
if ($initialMkMap[0].Backup_Guid -eq $newMkMap[0].Backup_Guid)
{
Write-Output ("[?] Looks like user already has Master keys onboarded to this Backup key: " + $newMkMap[0].Backup_Guid);
$newMkMap | Where-Object {$_.Backup_Guid -eq $newMkMap[0].Backup_Guid}
}
else
{
Write-Output ("[+] SUCCESS: User onboarded to Backup key: " + $newMkMap[0].Backup_Guid);
}
}
}