当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何在 Microsoft Entra 中报告用户同意的应用程序权限

作者:精品下载站 日期:2024-12-14 06:39:36 浏览:12 分类:玩电脑

如何在 Microsoft Entra 中报告用户同意的应用程序权限


默认情况下,组织中的用户可以授权应用程序访问 Microsoft Entra 中有关其自身的数据。不仅如此,默认情况下,用户还可以授权应用程序代表他们执行操作,例如创建 Microsoft Teams 并以所述用户的身份发送电子邮件。

在 99% 的场景中,不需要应用程序的此要求,除非应用程序是为非常自定义的工作流程创建的,其中管理员仍应配置为手动授予同意(或者再次通过特定工作流程)。

作为管理员,您应该限制用户可以自行授予应用程序访问权限的权限。这仍然应该仔细审查并与应用程序的要求保持一致。虽然我的单独帖子中对此进行了介绍,但在本文中,我将向您展示如何生成租户中应用程序的用户同意权限报告,然后将其导出到 CSV。

要求

本教程使用 Microsoft Graph PowerShell SDK。如果您尚未安装它,或者安装它已经有一段时间了,请查看我的帖子以指导您安装和更新模块。如何安装 Microsoft Graph PowerShell SDK。

您还需要访问租户中的全局管理员帐户。这样您就可以向 Microsoft Graph 命令行工具应用程序授予启动租户范围的权限。

用户同意的权限报告脚本

您可以复制以下脚本并根据您的喜好进行修改,也可以按原样运行它并生成报告。

该脚本利用批处理请求来提高检查 Microsoft Entra 中近 500 个服务主体的权限的性能。然后,它将过滤掉在用户级别同意的任何权限授予。

由于此用户授权信息不包含用户的用户名或应用程序名称,因此脚本将循环遍历每个对象 ID 以获取有意义的名称并将其添加到名为 $final 的报告中。

然后,系统将提示您选择导出和保存报告的位置。

在我的公共 GitHub 存储库中找到此脚本。

<#
.SYNOPSIS
Generates a report of user-consented permissions to application in Microsoft Entra
.LINK
   https://ourcloudnetwork.com
   https://www.linkedin.com/in/danielbradley2/
.NOTES
   Version:        0.1
   Author:         Daniel Bradley
   Creation Date:  Monday, January 8th 2024, 6:54:33 am
   File: ApplicationUserConsentReport.ps1
   Copyright (c) 2024 Your Company

HISTORY:
2024-01-08	File Generated

.INPUTS
.OUTPUTS

.COMPONENT
 Required Modules: 
 Microsoft.Graph.Applications
 Microsoft.Graph.Users
 
.EXAMPLE
.\ApplicationUserConsentReport.ps1

.LICENSE
Use this code free of charge at your own risk.
Never deploy code into production if you do not know what it does.
#>

#Connect to graph
Connect-MgGraph -Scopes Application.Read.All, user.read.all

#Define output file name
$filename = "User_Permissions_Grant_Report.csv"

#Function will launch Windows explorer to select a folder
Function SelectFolder {
    $global:folderselection = New-Object System.Windows.Forms.OpenFileDialog -Property @{  
    InitialDirectory = [Environment]::GetFolderPath('Desktop')  
    CheckFileExists = 0  
    ValidateNames = 0  
    FileName = $filename  
    }
    $folderselection.ShowDialog() | Out-Null
}

#Gets all service principals with limited info
Write-host "Gathering service principals.." -ForegroundColor Yellow
$AllServicePrincipals = Get-MgServicePrincipal -All | Select Id, appid, DisplayName

#Send batch requests to gather all user grants, due to large number of service principals
Write-host "Indentifying user grants..." -ForegroundColor Yellow
$Report = @()
for($i=0;$i -lt $AllServicePrincipals.count;$i+=20){
    $batch = @{}
    $batch['requests'] = ($AllServicePrincipals[$i..($i+19)] | select @{n='id';e={$_.id}},@{n='method';e={'GET'}},`
		@{n='url';e={"/servicePrincipals/$($_.id)/oauth2PermissionGrants"}})
    $response = invoke-mggraphrequest -Method POST -URI "https://graph.microsoft.com/v1.0/`$batch" -body ($batch | convertto-json) -OutputType PSObject
    $Report += $response.responses
}
$UserGrants = $Report.body.value | Where {$_.ConsentType -eq "Principal"}

#Uses loop function to gather additional information such as the userprincipalname. It also correlates the service principal name.
Write-host "correlating information..." -ForegroundColor Yellow
$final = [System.Collections.Generic.List[Object]]::new()
Foreach ($Grant in $UserGrants){
    $user = $null
    $app = $null
    $user = Get-MgUser -UserId $Grant.principalId | Select UserPrincipalName
    $app = $AllServicePrincipals | Where {$_.id -eq $($grant.clientid)}
    $obj = [PSCustomObject][ordered]@{
        "Application" = $app.DisplayName
        "user" = $user.UserPrincipalName
        "permission" = ($grant.scope.Trim() -replace " ",", ")
    }
    $final.Add($obj)
}

#Run select folder function
Write-host "Please select a folder `n" -ForegroundColor Green
Start-Sleep -Seconds 2
SelectFolder

#Export file
$final | Export-CSV  -Path $folderselection.FileName -NoTypeInformation
Write-host "File saved! Click Enter to close... `n" -ForegroundColor Green
Write-host "        Script by  >>  Daniel Bradley   " -ForegroundColor cyan -BackgroundColor black
Write-host "            Visit  >>  ourcloudnetwork.com  " -ForegroundColor cyan -BackgroundColor black
Write-host "          Connect  >>  https://www.linkedin.com/in/danielbradley2/  " -ForegroundColor cyan -BackgroundColor black
Write-host "`n"
Pause

脚本运行后,您将收到如下所示的报告。

[玩转系统] 如何在 Microsoft Entra 中报告用户同意的应用程序权限

包起来

收回对环境中应用程序的控制非常重要。即使您阻止用户在租户中创建应用程序并限制应用程序用户同意,应用程序中也可能潜伏着一些权限,此脚本将帮助您找到这些权限!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯