-
Notifications
You must be signed in to change notification settings - Fork 24
/
Get-AzureADGraphApps.ps1
210 lines (184 loc) · 9.21 KB
/
Get-AzureADGraphApps.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
#Requires -PSEdition Desktop
<#
.SYNOPSIS
Returns all the apps and service principles that currently use Azure AD Graph
.EXAMPLE
PS C:\> .\Get-AzureADGraphApps.ps1
Returns a collection of all the apps that have Azure AD Graph permissions assigned to them
#>
[cmdletbinding()]
param()
function Load-Module ($m) {
Write-Progress -Activity "Loading dependencies..."
# If module is imported say that and do nothing
if (Get-Module | Where-Object { $_.Name -eq $m }) {
Write-Verbose "Module $m is already imported."
}
else {
# If module is not imported, but available on disk then import
if (Get-Module -ListAvailable | Where-Object { $_.Name -eq $m }) {
Import-Module $m
}
else {
# If module is not imported, not available on disk, but is in online gallery then install and import
if (Find-Module -Name $m | Where-Object { $_.Name -eq $m }) {
Install-Module -Name $m -Force -Scope CurrentUser
Import-Module $m
}
else {
# If module is not imported, not available and not in online gallery then abort
Write-Host "Module $m not imported, not available and not in online gallery, exiting."
EXIT 1
}
}
}
}
function Get-MSCloudIdConsentGrantList {
# An in-memory cache of objects by {object ID} andy by {object class, object ID}
$script:ObjectByObjectId = @{}
$script:ObjectByObjectClassId = @{}
# Function to add an object to the cache
function CacheObject($Object) {
if ($Object) {
if (-not $script:ObjectByObjectClassId.ContainsKey($Object.ObjectType)) {
$script:ObjectByObjectClassId[$Object.ObjectType] = @{}
}
$script:ObjectByObjectClassId[$Object.ObjectType][$Object.ObjectId] = $Object
$script:ObjectByObjectId[$Object.ObjectId] = $Object
}
}
# Function to retrieve an object from the cache (if it's there), or from Azure AD (if not).
function GetObjectByObjectId($ObjectId) {
if (-not $script:ObjectByObjectId.ContainsKey($ObjectId)) {
Write-Verbose ("Querying Azure AD for object '{0}'" -f $ObjectId)
try {
$object = Get-AzureADObjectByObjectId -ObjectId $ObjectId
CacheObject -Object $object
}
catch {
Write-Verbose "Object not found."
}
}
return $script:ObjectByObjectId[$ObjectId]
}
# Get all ServicePrincipal objects and add to the cache
Write-Progress -Activity "Retrieving Service Principal objects. Please wait..."
$servicePrincipals = Get-AzureADServicePrincipal -All $true
$tenantId = (Get-AzureADTenantDetail).ObjectId
$Oauth2PermGrants = @()
$count = 0
foreach ($sp in $servicePrincipals) {
CacheObject -Object $sp
$spPermGrants = Get-AzureADServicePrincipalOAuth2PermissionGrant -ObjectId $sp.ObjectId -All $true
$Oauth2PermGrants += $spPermGrants
$count++
Write-Progress -Activity "Getting Service Principal Delegate Permissions..." -Status "$count of $($servicePrincipals.Count) - $($sp.DisplayName)" -percentComplete (($count / $servicePrincipals.Count) * 100)
if ($sp.AppId -eq "00000002-0000-0000-c000-000000000000") {
#Azure Active Directory Graph API app
$aadGraphSp = $sp
}
}
# Get all existing OAuth2 permission grants, get the client, resource and scope details
Write-Progress -Activity "Checking Delegated Permission Grants..."
foreach ($grant in $Oauth2PermGrants) {
if ($grant.ResourceId -eq $aadGraphSp.ObjectId -and $grant.Scope) {
$grant.Scope.Split(" ") | Where-Object { $_ } | ForEach-Object {
$scope = $_
$client = GetObjectByObjectId -ObjectId $grant.ClientId
$isAppProxyApp = $false
if ($client.Tags -contains "WindowsAzureActiveDirectoryOnPremApp") {
$isAppProxyApp = $true
}
if(-not $isAppProxyApp)
{
$ownerUPN = (Get-AzureADServicePrincipalOwner -ObjectId $client.ObjectId -Top 1).UserPrincipalName
Write-Progress -Activity "Checking Delegate Permissions - $($client.DisplayName)"
# Determine if the object comes from the Microsoft Services tenant, and flag it if true
$MicrosoftRegisteredClientApp = @()
if ($client.AppOwnerTenantId -eq "f8cdef31-a31e-4b4a-93e4-5f571e91255a" -or $client.AppOwnerTenantId -eq "72f988bf-86f1-41af-91ab-2d7cd011db47") {
$MicrosoftRegisteredClientApp = $true
}
else {
$MicrosoftRegisteredClientApp = $false
}
if(-not $MicrosoftRegisteredClientApp) {
$isTenantOwnedApp = $false
if ($client.AppOwnerTenantId -eq $tenantId) {
$isTenantOwnedApp = $true
}
$resource = GetObjectByObjectId -ObjectId $grant.ResourceId
if ($grant.ConsentType -eq "AllPrincipals") {
$simplifiedgranttype = "Delegated-AllPrincipals"
}
elseif ($grant.ConsentType -eq "Principal") {
$simplifiedgranttype = "Delegated-Principal"
}
$global:hasData = $true
New-Object PSObject -Property ([ordered]@{
"ObjectId" = $grant.ClientId
"DisplayName" = $client.DisplayName
"ApplicationId" = $client.AppId
"PermissionType" = $simplifiedgranttype
"Resource" = $resource.DisplayName
"Permission" = $scope
"Owner" = $ownerUPN
"TenantOwned" = $isTenantOwnedApp
})
}
}
}
}
}
# Iterate over all ServicePrincipal objects and get app permissions
Write-Progress -Activity "Getting Application Permission Grants..."
$script:ObjectByObjectClassId['ServicePrincipal'].GetEnumerator() | ForEach-Object {
$sp = $_.Value
Write-Progress -Activity "Checking Application Permissions - $($sp.DisplayName)"
Get-AzureADServiceAppRoleAssignedTo -ObjectId $sp.ObjectId -All $true `
| Where-Object { $_.PrincipalType -eq "ServicePrincipal" -and $_.ResourceId -eq $aadGraphSp.ObjectId } | ForEach-Object {
$assignment = $_
$client = GetObjectByObjectId -ObjectId $assignment.PrincipalId
$isAppProxyApp = $false
if ($client.Tags -contains "WindowsAzureActiveDirectoryOnPremApp") {
$isAppProxyApp = $true
}
if(-not $isAppProxyApp)
{
$ownerUPN = (Get-AzureADServicePrincipalOwner -ObjectId $client.ObjectId -Top 1).UserPrincipalName
# Determine if the object comes from the Microsoft Services tenant, and flag it if true
$MicrosoftRegisteredClientApp = @()
if ($client.AppOwnerTenantId -eq "f8cdef31-a31e-4b4a-93e4-5f571e91255a" -or $client.AppOwnerTenantId -eq "72f988bf-86f1-41af-91ab-2d7cd011db47") {
$MicrosoftRegisteredClientApp = $true
}
else {
$MicrosoftRegisteredClientApp = $false
}
if(-not $MicrosoftRegisteredClientApp) {
$isTenantOwnedApp = $false
if ($client.AppOwnerTenantId -eq $tenantId) {
$isTenantOwnedApp = $true
}
$resource = GetObjectByObjectId -ObjectId $assignment.ResourceId
$appRole = $resource.AppRoles | Where-Object { $_.Id -eq $assignment.Id }
$global:hasData = $true
New-Object PSObject -Property ([ordered]@{
"ObjectId" = $assignment.PrincipalId
"DisplayName" = $client.DisplayName
"ApplicationId" = $client.AppId
"PermissionType" = "Application"
"Resource" = $resource.DisplayName
"Permission" = $appRole.Value
"Owner" = $ownerUPN
"TenantOwned" = $isTenantOwnedApp
})
}
}
}
}
}
Load-Module "AzureAD"
$global:hasData = $false
Get-MSCloudIdConsentGrantList
if($global:hasData -eq $false){
Write-Warning "This tenant does not have any apps using Azure AD Graph"
}