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

[玩转系统] 如何在 AzureAD 中监视应用程序管理员权限同意

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

如何在 AzureAD 中监视应用程序管理员权限同意


在本教程中,我将向您展示如何结合使用 Microsoft Graph PowerShell、托管识别和 Azure 自动化,创建一种安全的方式,每周通过电子邮件报告应用程序管理员授予 Graph API 权限的同意情况。这将帮助任何信息安全团队确定管理员是否向 Azure AD 中的应用程序授予了超过必要的权限。

什么是管理员权限同意?

管理员权限同意是向 Azure AD 中的应用程序授予特定权限的“租户范围”同意。这意味着该应用程序的发布者将能够访问其已被授予访问权限的数据,以及任何能够对该应用程序进行身份验证的人。由于这种性质,管理员权限授予具有很高的特权,并且应该监视操作。

例如,我是一名 Azure 管理员,负责管理 Azure 中托管的自定义应用程序以及 Azure 订阅中的一些相关资源。我无权管理 Azure AD 中的目录对象,但我管理的自定义应用程序确实需要访问和修改某些 Azure AD 目录对象。为了满足此要求,我要求 Azure AD 管理员在 Azure AD 中注册一个应用程序(或服务主体)并同意其 user.readwrite.all 权限,从而允许我的应用程序修改 Azure AD 中的用户对象。无意中,现在已经创建了一条攻击路径,未经授权的用户(我)可能会更改我无权更改的资源,因为我对自定义应用程序拥有完全控制权。

[玩转系统] 如何在 AzureAD 中监视应用程序管理员权限同意

对于我们管理员来说幸运的是,每次管理员同意向应用程序授予权限时,企业应用程序审核日志中都会存储一个日志,其中包含我们调查该操作并对其有效性做出明智决定所需的所有信息。

[玩转系统] 如何在 AzureAD 中监视应用程序管理员权限同意

谁可以在 Azure AD 中授予管理员权限?

Azure AD 中有 2 个角色可以为应用程序授予管理员权限同意,它们是:

  • 全局管理员
  • 特权角色管理员

从实际意义上讲,这两个角色的特权都不比另一个角色低。虽然从表面上看,全局管理员角色拥有更多权限,但特权角色管理员可以根据需要简单地提升权限,而本身不拥有那么多的显式权限。

脚本权限

我编写的用于监视这些管理员权限同意日志的脚本使用了 3 个权限,这些权限需要分配给用于对运行该脚本的 Azure 自动化进行身份验证的托管标识,这些权限是:

  • 审核日志.read.all
  • 应用程序.read.all
  • 邮件发送

要创建托管标识并分配上述权限,请查看我的将托管标识与 Microsoft Graph PowerShell 结合使用的教程:

如何通过 Microsoft Graph PowerShell 使用托管身份

管理员权限同意报告脚本

您可以在下面找到脚本,该脚本将过滤企业应用程序日志以获取权限同意,将其整理成报告并将其通过电子邮件发送到目的地。请务必修改以下字段:

  • 托管_身份_ID
  • 收件人地址
  • SENDER_ADDRESS
<#
AUTHOR: Daniel Bradley
LINKEDIN: https://www.linkedin.com/in/danielbradley2/
TWITTER: https://twitter.com/DanielatOCN
WEBSITE: https://ourcloudnetwork.com/
Info: This script was written by Daniel Bradley for the ourcloudnetwork.com blog
#>

# Declare variables
$ManagedIdentityId = "!"
$RecipientAddress = "!"
$SenderAddress = "!"

#Connect to Microsoft Graph
Connect-MgGraph -Identity -ClientId "$ManagedIdentityId"

# Get date minus 7 days
$a = $(get-date).AddDays(-7).ToString("yyyy-MM-dd")

# Get application consent logs
$uri = "https://graph.microsoft.com/beta/auditLogs/directoryAudits?`$filter=ActivityDateTime ge $a and ActivityDisplayName eq 'Consent to application'"
$logs = Invoke-MgGraphRequest -uri $uri -Method Get -OutputType PSObject | select -expand value

# Create Report object
$Report = [System.Collections.Generic.List[Object]]::new()

# Loop through each log
ForEach ($log in $logs) {

    # null temporary variable
    $id = $null
    $app = $null

    # Get current app id
    $id = ($log.additionalDetails) | Where-Object {$_.Key -eq 'AppID'}

    # Get Application Name
    if ($log.targetResources.type -eq "ServicePrincipal"){
        $App = Get-MgServicePrincipal -Filter "AppId eq '$($id.value)'"
    } else {
        $app = Get-MgApplication -Filter "AppId eq '$($id.value)'"
    }
    
    # Extract consented permissions
    $perms = (($log.targetResources.modifiedproperties) | Where-Object {$_.DisplayName -eq 'ConsentAction.Permissions'}).newvalue
    $perms -match "(?<=Scope:)(.*?)(?=, CreatedDateTime:)" | out-null
    $permissions = $matches[1]   

    # build report object
    $obj = [PSCustomObject][ordered]@{
        "date"          = $log.activityDateTime
        "App Name"      = $app.DisplayName
        "Admin Consent" = (($log.targetResources.modifiedProperties) | Where-Object {$_.DisplayName -eq 'ConsentContext.IsAdminConsent'}).NewValue
        "Permissions"   = $permissions
        "Consented by"  = $log.initiatedBy.user.userPrincipalName
    }
    $report.Add($obj)
 }

# Convert report to HTML
$report = $report | ConvertTo-Html
$Report = $report -replace '<table>', '<table border="1">'

# Prepare report as attachment
$encodedBytes = [System.Text.Encoding]::UTF8.GetBytes($report)
$encodedText = [System.Convert]::ToBase64String($encodedBytes)

# Build message params
$params = @{
	message = @{
		subject = "Automated - Application Permissions Consents"
		body = @{
			contentType = "Text"
			content = "Please see the attached. Do no reply to this email."
		}
		toRecipients = @(
			@{
				emailAddress = @{
					address = "$RecipientAddress"
				}
			}
		)
    attachments = @(
			@{
				"@odata.type" = "#microsoft.graph.fileAttachment"
				name = "report.html"
				contentType = "text/plain"
				contentBytes = $encodedText
			}
    )
	}
	saveToSentItems = "false"
    
}

# Send message
Send-MgUserMail -UserId $SenderAddress -BodyParameter $params

使用 Azure 自动化自动生成报告

Azure 自动化允许您安排报告脚本定期运行。我们的想法是每周运行该报告,以评估哪些应用程序已获得哪些权限的同意。为此,您将需要 Azure 订阅以及全局管理员或特权角色管理员帐户来处理启用此脚本运行所需的托管标识的初始权限同意。

由于我在另一个教程中对此步骤进行了更详细的介绍,因此您应该查看我的以下帖子:

如何使用 Azure 自动化运行 Microsoft Graph PowerShell 脚本

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

取消回复欢迎 发表评论:

关灯