-
Notifications
You must be signed in to change notification settings - Fork 218
/
Device_utils.ps1
181 lines (160 loc) · 6.5 KB
/
Device_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
# This file contains utility functions for local AAD Joined devices
# Exports the transport key of the local device
# Dec 17th 2021
function Get-LocalDeviceTransportKeys
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$True)]
[ValidateSet('Joined','Registered')]
[String]$JoinType,
[Parameter(Mandatory=$True)]
[String]$IdpDomain,
[Parameter(Mandatory=$True)]
[String]$TenantId,
[Parameter(Mandatory=$True)]
[String]$UserEmail
)
Begin
{
$sha256 = [System.Security.Cryptography.SHA256]::Create()
}
Process
{
# Calculate registry key parts
$idp = Convert-ByteArrayToHex -Bytes ($sha256.ComputeHash([text.encoding]::Unicode.GetBytes($IdpDomain)))
$tenant = Convert-ByteArrayToHex -Bytes ($sha256.ComputeHash([text.encoding]::Unicode.GetBytes($TenantId)))
$email = Convert-ByteArrayToHex -Bytes ($sha256.ComputeHash([text.encoding]::Unicode.GetBytes($UserEmail)))
$sid = Convert-ByteArrayToHex -Bytes ($sha256.ComputeHash([text.encoding]::Unicode.GetBytes(([System.Security.Principal.WindowsIdentity]::GetCurrent()).User.Value)))
if($JoinType -eq "Joined")
{
$registryPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Cryptography\Ngc\KeyTransportKey\PerDeviceKeyTransportKey\$Idp\$tenant"
}
else
{
$registryPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Cryptography\Ngc\KeyTransportKey\$sid\$idp\$($tenant)_$($email)"
}
if((Test-Path -Path $registryPath) -eq $false)
{
Throw "The device seems not to be Azure AD joined or registered. Registry key not found: $registryPath"
}
# Get the Transport Key name from registry
try
{
$transPortKeyName = Get-ItemPropertyValue -Path "$registryPath" -Name "SoftwareKeyTransportKeyName"
}
catch
{
# This machine probably has a TPM, so the value name would be "TpmKeyTransportKeyName"
Throw "Unable to get SoftwareTransportKeyName from $registryPath"
}
Write-Verbose "TransportKey name: $transportKeyName`n"
# Loop through the system keys
$systemKeys = Get-ChildItem -Path "$env:ALLUSERSPROFILE\Microsoft\Crypto\SystemKeys"
foreach($systemKey in $systemKeys)
{
Write-Verbose "Parsing $($systemKey.FullName)"
$keyBlob = Get-BinaryContent $systemKey.FullName
# Parse the blob to get the name
$key = Parse-CngBlob -Data $keyBlob
if($key.name -eq $transPortKeyName)
{
Write-Verbose "Transport Key found! Decrypting.."
# Decrypt the found key
$transPortKey = Parse-CngBlob -Data $keyBlob -Decrypt -LocalMachine
return $transPortKey
}
}
}
End
{
$sha256.Dispose()
}
}
# Parses the oid values of the given certificate
# Dec 23rd 2021
function Parse-CertificateOIDs
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True,ValueFromPipeline)]
[System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate
)
Process
{
function Get-OidRawValue
{
Param([byte[]]$RawValue)
Process
{
# Is this DER value?
if($RawValue.Length -gt 2 -and ($RawValue[2] -eq $RawValue.Length-3 ))
{
return $RawValue[3..($RawValue.Length-1)]
}
else
{
return $RawValue
}
}
}
$retVal = New-Object psobject
foreach($ext in $Certificate.Extensions)
{
switch($ext.Oid.Value)
{
"1.2.840.113556.1.5.284.2" {
$retVal | Add-Member -NotePropertyName "DeviceId" -NotePropertyValue ([guid][byte[]](Get-OidRawValue -RawValue $ext.RawData))
}
# "The objectGuid of the user object ([MS-ADSC] section 2.268) on the directory server that corresponds to the authenticating user."
# https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-dvrj/850786b9-2525-4047-a5ff-8c3093b46b88
# https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-dvre/76747b5c-06c2-4c73-9207-8ebb6ee891ea
# I.e. the object ID in AAD of the user who joined/registered the device
"1.2.840.113556.1.5.284.3" {
$retVal | Add-Member -NotePropertyName "AuthUserObjectId" -NotePropertyValue ([guid][byte[]](Get-OidRawValue -RawValue $ext.RawData))
}
"1.2.840.113556.1.5.284.5" {
$retVal | Add-Member -NotePropertyName "TenantId" -NotePropertyValue ([guid][byte[]](Get-OidRawValue -RawValue $ext.RawData))
}
"1.2.840.113556.1.5.284.8" {
# Tenant region
# AF = Africa
# AS = Asia
# AP = Australia/Pasific
# EU = Europe
# ME = Middle East
# NA = North America
# SA = South America
$retVal | Add-Member -NotePropertyName "Region" -NotePropertyValue ([text.encoding]::UTF8.getString([byte[]](Get-OidRawValue -RawValue $ext.RawData)))
}
"1.2.840.113556.1.5.284.7" {
# JoinType
# 0 = Registered
# 1 = Joined
$retVal | Add-Member -NotePropertyName "JoinType" -NotePropertyValue ([int]([text.encoding]::UTF8.getString([byte[]](Get-OidRawValue -RawValue $ext.RawData))))
}
}
}
return $retVal
}
}
# Gets service account names for all services
# Aug 29th 2022
function Get-ServiceAccountNames
{
[cmdletbinding()]
Param()
Process
{
foreach($service in Get-ChildItem -Path "HKLM:\SYSTEM\CurrentControlSet\Services\")
{
$svcName = $service.PSChildName
$svcAccount = $service.GetValue("ObjectName")
if(![string]::IsNullOrEmpty($svcAccount))
{
Write-Debug "Service: '$svcName', AccountName: '$svcAccount'"
New-Object psobject -Property ([ordered]@{"Service" = $svcName; "AccountName" = $svcAccount})
}
}
}
}