-
Notifications
You must be signed in to change notification settings - Fork 90
/
Get-AzureAccessToken.ps1
241 lines (183 loc) · 8.53 KB
/
Get-AzureAccessToken.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
Function Get-AzureAccessToken {
<#
.SYNOPSIS
This cmdlet is used to retrieve an access token from Azure using a client secret or client certificate
.DESCRIPTION
Use a client secret or certificate to obtain an Azure access token
.PARAMETER ClientID
Define the Azure Client ID GUID value of your application that has a secret it can use to authenticate
.PARAMETER ClientSecret
Enter your Client Secret value which can only be obtained after first generating the secret
.PARAMETER ApplicationID
Define the Application ID that has the certificate associated with it for authentication (Client ID)
.PARAMETER CertificateThumbprint
Enter the certificate thumbprint value to use for authentication
.PARAMETER TenantId
Define your Azure tenant ID GUID value
.PARAMETER Resource
Define the resource to retrieve
.PARAMETER Scope
Define the scope to retrieve
.EXAMPLE
PS> Get-AzureAccessToken -ClientId '319d7802-578a-4705-bda7-b902ee2ecd65' -ClientSecret 'tYZ8Q~zs0h_OhYIekLmjVOK.vLshIJET8TPQccK-' -TenantId 'a3af5c78-3ebb-41d7-a295-81b464b6d923 -Scope 'https://vault.azure.net/.default'
# This example obtains an Azure Access Token using a client ID and Secret
.EXAMPLE
PS> Get-AzureAccessToken -ApplicationID '319d7802-578a-4705-bda7-b902ee2ecd65' -CertificateThumbprint 'a909502dd82ae41433e6f83886b00d4277a32a7b -TenantId 'a3af5c78-3ebb-41d7-a295-81b464b6d923' -Scope 'https://vault.azure.net/.default
# This example obtains an Azure Access Token using an Application ID and Certificate
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: rosborne@osbornepro.com
.LINK
https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4
https://learn.microsoft.com/en-us/entra/identity-platform/certificate-credentials
https://powershell.org/2020/11/writing-your-own-powershell-functions-cmdlets/
https://osbornepro.com
https://writeups.osbornepro.com
https://btpssecpack.osbornepro.com
https://github.com/tobor88
https://gitlab.com/tobor88
https://www.powershellgallery.com/profiles/tobor
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
https://www.hackthebox.eu/profile/52286
.INPUTS
None
.OUTPUTS
None
#>
[CmdletBinding()]
param (
[Parameter(
Mandatory=$True,
ParameterSetName='Resource'
)] # End Parameter
[Parameter(
Mandatory=$True,
ParameterSetName='Scope'
)] # End Parameter
[String]$ClientId,
[Parameter(
Mandatory=$True,
ParameterSetName='Resource'
)] # End Parameter
[Parameter(
Mandatory=$True,
ParameterSetName='Scope'
)] # End Parameter
[String]$ClientSecret,
[Parameter(
Mandatory=$True,
ParameterSetName='Certificate'
)] # End Parameter
[String]$ApplicationID,
[Parameter(
Mandatory=$True,
ParameterSetName='Certificate'
)] # End Parameter
[String]$CertificateThumbprint,
[Parameter(
Mandatory=$True,
ParameterSetName='Certificate'
)] # End Parameter
[Parameter(
Mandatory=$True,
ParameterSetName='Resource'
)] # End Parameter
[Parameter(
Mandatory=$True,
ParameterSetName='Scope'
)] # End Parameter
[String]$TenantId,
[Parameter(
Mandatory=$True,
ParameterSetName='Resource'
)] # End Parameter
[String]$Resource,
[Parameter(
Mandatory=$False,
ParameterSetName='Certificate'
)] # End Parameter
[Parameter(
Mandatory=$False,
ParameterSetName='Scope'
)] # End Parameter
[ValidateSet('https://vault.azure.net/.default', 'https://management.azure.com/.default', 'https://graph.microsoft.com/.default')]
[String]$Scope = 'https://vault.azure.net/.default'
) # End param
BEGIN {
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
$ContentType = "application/x-www-form-urlencoded"
$UserAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::FireFox
} PROCESS {
If ($CertificateThumbprint) {
$Certificate = Get-ChildItem -Path "Cert:\LocalMachine\My\$CertificateThumbprint"
$CertificateBase64Hash = [System.Convert]::ToBase64String($Certificate.GetCertHash())
Write-Debug -Message "[D] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Creating JWT header"
$JWTHeader = @{
alg = "RS256"
typ = "JWT"
x5t = $CertificateBase64Hash -Replace '\+','-' -Replace '/','_' -Replace '='
} # End JWTHeader
Write-Debug -Message "[D] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Creating JWT payload"
$JWTPayLoad = @{
aud = "https://login.microsoftonline.com/$($TenantID)/oauth2/token"
exp = ([System.DateTimeOffset](Get-date).AddMinutes(5)).ToUnixTimeSeconds()
iss = $ApplicationID
jti = [System.Guid]::NewGuid()
nbf = ([System.DateTimeOffset](Get-Date).ToUniversalTime()).ToUnixTimeSeconds()
sub = $ApplicationID
}
Write-Debug -Message "[D] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Converting header and payload to base64"
$JWTHeaderToByte = [System.Text.Encoding]::UTF8.GetBytes(($JWTHeader | ConvertTo-Json))
$EncodedHeader = [System.Convert]::ToBase64String($JWTHeaderToByte)
$JWTPayLoadToByte = [System.Text.Encoding]::UTF8.GetBytes(($JWTPayload | ConvertTo-Json))
$EncodedPayload = [System.Convert]::ToBase64String($JWTPayLoadToByte)
Write-Debug -Message "[D] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Joining header and Payload with '.' to create a valid (unsigned) JWT"
$JWT = $EncodedHeader + "." + $EncodedPayload
Write-Debug -Message "[D] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Obtain the private key object of your certificate"
$PrivateKey = ([System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPrivateKey($Certificate))
Write-Debug -Message "[D] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Define RSA signature and hashing algorithm"
$RSAPadding = [System.Security.Cryptography.RSASignaturePadding]::Pkcs1
$HashAlgorithm = [System.Security.Cryptography.HashAlgorithmName]::SHA256
Write-Debug -Message "[D] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Creating a signature of the JWT"
$Signature = [System.Convert]::ToBase64String(
$PrivateKey.SignData([System.Text.Encoding]::UTF8.GetBytes($JWT),$HashAlgorithm,$RSAPadding)
) -Replace '\+','-' -Replace '/','_' -Replace '='
Write-Debug -Message "[D] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Joining the signature to the JWT with '.'"
$JWT = $JWT + "." + $Signature
Write-Debug -Message "[D] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Creating a hash with body parameters"
$Body = @{
client_id = $ApplicationID
client_assertion = $JWT
client_assertion_type = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
scope = $Scope
grant_type = "client_credentials"
} # End Body
} Else {
$Body = @{
"grant_type" = "client_credentials"
"client_id" = $ClientId
"client_secret" = $ClientSecret
} # End Body
} # End If Else
Switch ($PSCmdlet.ParameterSetName) {
"Resource" {
$Body["Resource"] = $Resource
} "Scope" {
$Body["scope"] = $Scope
} # End Switch Options
} # End Switch
$Uri = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
$Header = @{
Authorization = "Bearer $JWT"
} # End Header
$Token = Invoke-RestMethod -UseBasicParsing -Method POST -UserAgent $UserAgent -ContentType $ContentType -Uri $Uri -Headers $Header -Body $Body -Verbose:$False | Select-Object -ExpandProperty access_token
} END {
$Script:GraphHeader = @{
Authorization = "Bearer $Token"
} # End Header
$Script:GraphAccessToken = $Token
Return $Token
} # End B P E
} # End Function Get-AzureAccessToken