-
Notifications
You must be signed in to change notification settings - Fork 2
/
AccessToken_utils.ps1
582 lines (493 loc) · 20.7 KB
/
AccessToken_utils.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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# This script contains functions for handling access tokens
# and some utility functions
# Exports Teams access tokens from the Teams cache
# Sep 26th 2022
function Export-TeamsTokens
{
<#
.SYNOPSIS
Exports Teams tokens from the provided Cookie database, or from current user's local database.
.DESCRIPTION
Exports Teams tokens from the provided Cookie database, or from current user's local database.
The Teams Cookies database is SQLite database.
.PARAMETER CookieDatabase
Full name of the Teams cookie database. If not provided, uses current user's database.
.PARAMETER AddToCache
Adds the tokens to AADInternals token cache
.PARAMETER CopyToClipboard
Copies the tokens to clipboard as JSON string
.EXAMPLE
PS\:>Export-AADIntTeamsTokens
User: [email protected]
Name Value
---- -----
office_access_token eyJ0eXAiOiJKV1QiLCJub25jZSI6InlsUjJWRmp4SWFqeVVqeklZa3R...
skypetoken_asm eyJhbGciOiJSUzI1NiIsImtpZCI6IjEwNiIsIng1dCI6Im9QMWFxQnl...
authtoken eyJ0eXAiOiJKV1QiLCJub25jZSI6InpsUFY2bnRCUDR5NTFLTkNQR2l...
SSOAUTHCOOKIE eyJ0eXAiOiJKV1QiLCJub25jZSI6Ik5sbHJiaFlzYl9rVnU3VzVSa01...
.EXAMPLE
PS\:>Export-AADIntTeamsTokens -CookieDatabase C:\Cookies
User: [email protected]
Name Value
---- -----
office_access_token eyJ0eXAiOiJKV1QiLCJub25jZSI6InlsUjJWRmp4SWFqeVVqeklZa3R...
skypetoken_asm eyJhbGciOiJSUzI1NiIsImtpZCI6IjEwNiIsIng1dCI6Im9QMWFxQnl...
authtoken eyJ0eXAiOiJKV1QiLCJub25jZSI6InpsUFY2bnRCUDR5NTFLTkNQR2l...
SSOAUTHCOOKIE eyJ0eXAiOiJKV1QiLCJub25jZSI6Ik5sbHJiaFlzYl9rVnU3VzVSa01...
.EXAMPLE
PS\:>Export-AADIntTeamsTokens -AddToCache
User: [email protected]
3 access tokens added to cache
.EXAMPLE
PS\:>Export-AADIntTeamsTokens -AddToCache -CopyToClipboard
User: [email protected]
3 access tokens added to cache
4 access tokens copied to clipboard
.EXAMPLE
PS\:>Export-AADIntTeamsTokens -CopyToClipboard
User: [email protected]
4 access tokens copied to clipboard
#>
[cmdletbinding()]
Param(
[switch]$AddToCache,
[switch]$CopyToClipboard,
[String]$CookieDatabase
)
Begin
{
}
Process
{
# Set the path if database was not provided, depends on the OS we are running.
if([string]::IsNullOrEmpty($CookieDatabase))
{
switch([system.environment]::OSversion.Platform)
{
("Linux")
{
$CookieDatabase="~/.config/Microsoft/Microsoft Teams/Cookies"
break
}
("Unix")
{
$CookieDatabase="~/Library/Application Support/Microsoft/Teams/Cookies"
break
}
default # Defaults to Windows
{
$CookieDatabase="$env:APPDATA\Microsoft\Teams\Cookies"
break
}
}
}
# Test whether the cookie database exists
if(-not (Test-Path $CookieDatabase))
{
Throw "The Cookie database does not exist: $CookieDatabase"
}
try
{
# Parse the database
Write-Verbose "Loading and parsing database $CookieDataBase"
$parsedDb = Parse-SQLiteDatabase -Data (Get-BinaryContent -Path $CookieDatabase)
Write-Verbose "Looking for tokens"
$access_tokens = [ordered]@{}
foreach($page in $parsedDb.Pages)
{
# Cookies data is stored on Table Leaf
if($page.PageType -eq "Table Leaf" -and $page.CellsOnPage -gt 0)
{
# Which has exactly 19 columns (the last is empty)
if($page.Cells[0].Payload.Count -ge 19)
{
Write-Verbose "Found Table Leaf page with $($page.CellsOnPage) cells"
<# Columns - updated Oct 20th 2022
0: creation_utc
1: top_frame_site_key
2: host_key
3: name
4: value
5: encrypted_value
6: path
7: expires_utc
8: is_secure
9: is_httponly
10: last_access_utc
12: has_expires
13: is_persistent
14: priority
15: encrypted_value
16: samesite
17: source_scheme
18: source_port
19: is_same_party
#>
foreach($cell in $page.Cells)
{
$name = $cell.Payload[3]
$value = $cell.Payload[4]
if($name -like "*token*" -or $name -eq "SSOAUTHCOOKIE")
{
# Strip the Bearer= and query parameters from the "authToken"
if($name -eq "authToken")
{
$value = [System.Net.WebUtility]::UrlDecode($value).Split("=")[1].Split("&")[0]
$userName = (Read-AccessToken -AccessToken $value).upn
}
# Add access tokens to cache as needed
if($AddToCache -and $name -ne "skypetoken_asm")
{
Add-AccessTokenToCache -AccessToken $value | Out-Null
$cached += 1
}
$access_tokens[$name] = $value
}
}
}
}
}
# Print out the username
Write-Host "User: $userName"
# Print count cached tokens
if($AddToCache)
{
Write-Host "$cached access tokens added to cache"
}
# Copy tokens to clipboard and print the count
if($CopyToClipboard)
{
$access_tokens | ConvertTo-Json | Set-Clipboard
Write-Host "$($access_tokens.Count) access tokens copied to clipboard"
}
# Return
if(-not $AddToCache -and -not $CopyToClipboard)
{
return $access_tokens
}
}
catch
{
Throw $_
}
}
}
# Exports Azure CLI access tokens from the msal_token_cache.bin cache
# Sep 29th 2022
function Export-AzureCliTokens
{
<#
.SYNOPSIS
Exports Azure CLI access tokens from the msal_token_cache.bin cache.
.DESCRIPTION
Exports Azure CLI access tokens from the msal_token_cache.bin cache.
msal_token_cache.bin is a json file protected with DPAPI in LocalUser context.
.PARAMETER MSALCache
Full name of the MSAL token cache. If not provided, uses msal_token_cache.bin from current user's profile under .Azure
.PARAMETER AddToCache
Adds the tokens to AADInternals token cache
.PARAMETER CopyToClipboard
Copies the tokens to clipboard as JSON string
.EXAMPLE
PS\:>Export-AADIntAzureCliTokens
UserName access_token
-------- ------------
[email protected] eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGx...
[email protected] eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGx...
[email protected] eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGx...
[email protected] eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGx...
.EXAMPLE
PS\:>Export-AADIntAzureCliTokens -MSALCache "C:\Users\user\.Azure\msal_token_cache.bin.old"
UserName access_token
-------- ------------
[email protected] eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGx...
[email protected] eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGx...
[email protected] eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGx...
[email protected] eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGx...
.EXAMPLE
PS\:>Export-AADIntAzureCliTokens -AddToCache
4 access tokens added to cache
.EXAMPLE
PS\:>Export-AADIntAzureCliTokens -AddToCache -CopyToClipboard
4 access tokens added to cache
4 access tokens copied to clipboard
.EXAMPLE
PS\:>Export-AADIntAzureCliTokens -CopyToClipboard
4 access tokens copied to clipboard
#>
[cmdletbinding()]
Param(
[switch]$AddToCache,
[switch]$CopyToClipboard,
[switch]$DPAPI,
[String]$MSALCache
)
Begin
{
# Load system.security assembly
Add-Type -AssemblyName System.Security
}
Process
{
# Parses the object definition string
# Sep 29th 2022
function Parse-ObjectDefinition
{
Param(
[Parameter(Mandatory=$True)]
[String]$Definition
)
Process
{
# Get the array
$Definition = $Definition.Substring($Definition.IndexOf("@"))
# Get the string between @{ and }
$Definition = $Definition.Substring(2,$Definition.Length-3)
$attributes = [ordered]@{}
if(-not [string]::IsNullOrEmpty($Definition))
{
# Split to array of name=value pairs
$properties = $Definition.Split("; ")
# Loop through the properties
foreach($property in $properties)
{
# Split & add to attributes
$parts = $property.Split("=")
if(-not [string]::IsNullOrEmpty($parts[0]))
{
$attributes[$parts[0]] = $parts[1]
}
}
}
return New-Object -TypeName psobject -Property $attributes
}
}
# Set the path if database was not provided, depends on the OS we are running.
if([string]::IsNullOrEmpty($MSALCache))
{
switch([system.environment]::OSversion.Platform)
{
("Linux")
{
$MSALCache="~/.azure/msal_token_cache.json"
break
}
("Unix")
{
$MSALCache="~/.azure/msal_token_cache.json"
break
}
default # Defaults to Windows
{
$MSALCache="$env:HOMEDRIVE$env:HOMEPATH\.Azure\msal_token_cache.bin"
$DPAPI = $true
break
}
}
}
# Test whether the MSAL cache exists
if(-not (Test-Path $MSALCache))
{
Throw "The MSAL token cache does not exist: $MSALCache"
}
try
{
Write-Verbose "Loading and parsing cache $MSALCache"
# Unprotect the token cache
if($DPAPI)
{
$decTokens = Get-BinaryContent $MSALCache
$tokens = [text.encoding]::UTF8.GetString([Security.Cryptography.ProtectedData]::Unprotect($decTokens,$null,'CurrentUser'))
}
else
{
$tokens = Get-Content $MSALCache -Encoding UTF8
}
$objTokens = $tokens | ConvertFrom-Json
$users = [ordered]@{}
foreach($account in ($objtokens.Account | Get-Member -MemberType NoteProperty))
{
# Need to parse the definition manually :(
$properties = Parse-ObjectDefinition -Definition $account.Definition
$users[$properties.home_account_id] = $properties.username
}
Write-Verbose "Found tokens for $($users.Count) users"
Write-Verbose "Looking for tokens"
$access_tokens = @()
foreach($access_token in ($objtokens.AccessToken | Get-Member -MemberType NoteProperty))
{
Write-Verbose "Parsing access token $($access_token.name)"
# Need to parse the definition manually :(
$at_properties = Parse-ObjectDefinition -Definition $access_token.Definition
# Get the refresh token and parse properties if found
$rt_properties = $null
$tenantId = $at_properties.home_account_id.Split(".")[1]
$rt_name = $access_token.name.Replace("accesstoken","refreshtoken").Replace("-organizations-","--").Replace("-$tenantId-","--")
$refresh_token = $objtokens.RefreshToken | Get-Member -Name $rt_name
if($refresh_token)
{
Write-Verbose "Parsing refresh token $rt_name"
$rt_properties = Parse-ObjectDefinition -Definition $refresh_token.Definition
}
# Form the return object
$attributes = [ordered]@{
"UserName" = $users[$at_properties.home_account_id]
"access_token" = $at_properties.secret
"refresh_token" = $rt_properties.secret
}
if($AddToCache)
{
Add-AccessTokenToCache -AccessToken $at_properties.secret -RefreshToken $rt_properties.secret | Out-Null
}
$access_tokens += New-Object psobject -Property $attributes
}
# Print out the usernames
Write-Host "Users: $($users.Values -Join ",")"
# Print count cached tokens
if($AddToCache)
{
Write-Host "$($access_tokens.Count) access tokens added to cache."
Write-Host "Note: AADInternals only stores tokens for one user! The token of last added user is used."
}
# Copy tokens to clipboard and print the count
if($CopyToClipboard)
{
$access_tokens | ConvertTo-Json | Set-Clipboard
Write-Host "$($access_tokens.Count) access tokens copied to clipboard"
}
# Return
if(-not $AddToCache -and -not $CopyToClipboard)
{
return $access_tokens
}
}
catch
{
Throw $_
}
}
}
# Exports access tokens from the Token Broker cache
# Oct 20th 2022
function Export-TokenBrokerTokens
{
<#
.SYNOPSIS
Exports access tokens from the Token Broker cache.
.DESCRIPTION
Exports access tokens from the Token Broker cache.
.PARAMETER AddToCache
Adds the tokens to AADInternals token cache
.PARAMETER CopyToClipboard
Copies the tokens to clipboard as JSON string
.EXAMPLE
PS\:>Export-AADIntTokenBrokerTokens
UserName access_token
-------- ------------
[email protected] eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGx...
[email protected] eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGx...
[email protected] eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGx...
[email protected] eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGx...
.EXAMPLE
PS\:>Export-AADIntTokenBrokerTokens -AddToCache
4 access tokens added to cache
.EXAMPLE
PS\:>Export-AADIntTokenBrokerTokens -AddToCache -CopyToClipboard
4 access tokens added to cache
4 access tokens copied to clipboard
.EXAMPLE
PS\:>Export-AADIntTokenBrokerTokens -CopyToClipboard
4 access tokens copied to clipboard
#>
[cmdletbinding()]
Param(
[switch]$AddToCache,
[switch]$CopyToClipboard
)
Begin
{
# Load system.security assembly
Add-Type -AssemblyName System.Security
}
Process
{
# Test whether the Token Broker cache exists
$TBRES = "$env:LOCALAPPDATA\Microsoft\TokenBroker\Cache\*.tbres"
if(-not (Test-Path $TBRES))
{
Throw "The Token Broker cache does not exist: $TBRES"
}
$access_tokens = @()
$users = [ordered]@{}
# Get the cache files
$files = Get-Item -Path $TBRES
foreach($file in $files)
{
try
{
Write-Verbose "Parsing $file"
$data = Get-BinaryContent -Path $file.FullName
$content = Parse-TBRES -Data $data
if($content.WTRes_Token -ne $null -and $content.WTRes_Token -ne "No Token")
{
$parsedToken = Read-AccessToken -AccessToken $content.WTRes_Token
# Could be JWE which can't be parsed
if($parsedToken)
{
$users[$parsedToken.oid] = $parsedToken.unique_name
# Form the return object
$attributes = [ordered]@{
"UserName" = $parsedToken.unique_name
"access_token" = $content.WTRes_Token
}
if($AddToCache)
{
Add-AccessTokenToCache -AccessToken $content.WTRes_Token | Out-Null
}
$access_tokens += [PSCustomObject] $attributes
}
}
}
catch
{
Write-Verbose "Got exception: $_"
}
}
Write-Verbose "Found tokens for $($users.Count) users"
# Print out the usernames
if($users.Count -gt 0)
{
Write-Host "Users: $($users.Values -Join ",")"
}
else
{
Write-Host "No tokens found."
}
# Print count cached tokens
if($AddToCache)
{
Write-Host "$($access_tokens.Count) access tokens added to cache."
Write-Host "Note: AADInternals only stores tokens for one user! The token of last added user is used."
}
# Copy tokens to clipboard and print the count
if($CopyToClipboard)
{
$access_tokens | ConvertTo-Json | Set-Clipboard
Write-Host "$($access_tokens.Count) access tokens copied to clipboard"
}
# Return
if(-not $AddToCache -and -not $CopyToClipboard)
{
return $access_tokens
}
}
}