9

資訊安全筆記:盤點 Azure 訂用帳戶與角色指派的方法

 3 years ago
source link: https://blog.miniasp.com/post/2022/01/04/Azure-RBAC-security-check-for-role-assignments
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在 Azure 所有 使用者 (User) 與 服務 (Service) 都代表著一個 主體 (Principal),而 Azure 的 RBAC (以角色為主的存取控制) 主要就是針對 主體 (Principal) 來進行角色指派 (授權),因此我們可以對 使用者主體 (User Principal) 或 服務主體 (Service Principal) 設定在不同資源所擁有的角色。本篇文章我打算來分享幾個方法,教你如何在特定 訂用帳戶 (Subscription) 下查出有哪些主體被設定了哪些角色,以及特定主體已經被授權到了哪些訂用帳戶哪些資源哪些角色,藉此用來盤點是否有不當授權的狀況。

使用 Azure CLI 的準備工作

  1. 安裝 Azure CLI 命令列工具

  2. 清空帳戶快取資料 (🔥 重要 🔥)

    az account clear
    

    檢查角色指派必須要確認清除本機 Azure CLI 的所有快取資料,否則取得的訂用帳戶可能有權限不足的問題。

  3. 登入 Azure CLI

    $tenantId = '4b972557-b992-4f77-b743-41f0d251f741'
    az login --tenant $tenantId
    

    請務必指定 tenantId,一次針對一個 Tenant (租戶) 進行管理是比較建議的用法!

    想知道你目前登入的使用者帳戶加入了哪幾個 Tenants 可以用以下命令查詢 (只能查到 TenantId 而已):

    az account tenant list
    

    因為 az account tenant list 太弱了,可以查出的資訊太少,建議直接呼叫 Graph API 取得完整的 Tenants 清單:

    az rest --method get --url https://management.azure.com/tenants?api-version=2021-01-01 --query 'value' -o json
    

    想知道你目前登入了哪個使用者帳戶,可以用以下命令查詢:

    az ad signed-in-user show -o json
    az ad signed-in-user show --query 'userPrincipalName' -o tsv
    

    若想查詢目前選用 Tenant 的網域名稱,可以考慮以下任何一種命令:

    (az ad signed-in-user show --query 'userPrincipalName' -o tsv).Split('@')[1]
    
    az rest --method get --url https://graph.microsoft.com/v1.0/domains --query 'value[?isDefault].id'
    
  4. 切換 訂用帳戶 (Subscription)

    az account set --subscription 'SubscriptionId'
    

    你也可以使用 az account show 取得當前選取的 id 屬性:

    az account show -o json
    
  5. 取得所有 訂用帳戶 (Subscription) 清單

    如果你在執行 az login 登入時有加上 --tenant 參數,那麼這裡只會列出單一租戶下的訂用帳戶而已!

    az account list
    

    你也可以使用以下 JMESPath 查詢語法篩選出特定 tenantId 的訂用帳戶

    $tenantId = '4b972557-b992-4f77-b743-41f0d251f741'
    az account list --query "[?tenantId=='${tenantId}']" -o json
    

    你也可以使用以下命令,查詢目前指定 Tenant 下的所有訂用帳戶清單

    az account subscription list
    

使用 Azure Az PowerShell 的準備工作

  1. 安裝 Az 模組

    Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
    
  2. 匯入所有 Az 模組

    Import-Module Az
    

    這個步驟會匯入一堆 Az. 開頭的 PowerShell 模組

    Get-Module 'Az*'
    
    ModuleType Version    PreRelease Name
    ---------- -------    ---------- ----
    Script     7.0.0                 Az
    Script     2.7.0                 Az.Accounts
    Script     1.1.2                 Az.Advisor
    Script     3.0.0                 Az.Aks
    Script     1.1.4                 Az.AnalysisServices
    Script     2.3.1                 Az.ApiManagement
    Script     1.0.0                 Az.AppConfiguration
    Script     1.3.0                 Az.ApplicationInsights
    Script     1.0.0                 Az.Attestation
    Script     1.7.2                 Az.Automation
    Script     3.1.1                 Az.Batch
    Script     2.0.0                 Az.Billing
    Script     1.8.1                 Az.Cdn
    Script     1.0.0                 Az.CloudService
    Script     1.9.0                 Az.CognitiveServices
    Script     4.21.0                Az.Compute
    Script     3.0.0                 Az.ContainerInstance
    Script     2.2.3                 Az.ContainerRegistry
    Script     1.5.0                 Az.CosmosDB
    Script     1.1.0                 Az.DataBoxEdge
    Script     1.1.0                 Az.Databricks
    Script     1.16.1                Az.DataFactory
    Script     1.0.2                 Az.DataLakeAnalytics
    Script     1.3.0                 Az.DataLakeStore
    Script     1.0.0                 Az.DataShare
    Script     1.1.0                 Az.DeploymentManager
    Script     3.1.0                 Az.DesktopVirtualization
    Script     1.0.2                 Az.DevTestLabs
    Script     1.1.2                 Az.Dns
    Script     1.3.0                 Az.EventGrid
    Script     1.9.1                 Az.EventHub
    Script     1.9.0                 Az.FrontDoor
    Script     4.0.0                 Az.Functions
    Script     5.0.0                 Az.HDInsight
    Script     1.3.1                 Az.HealthcareApis
    Script     2.7.4                 Az.IotHub
    Script     4.1.0                 Az.KeyVault
    Script     2.0.0                 Az.Kusto
    Script     1.5.0                 Az.LogicApp
    Script     1.1.3                 Az.MachineLearning
    Script     1.2.0                 Az.Maintenance
    Script     3.0.0                 Az.ManagedServices
    Script     1.0.2                 Az.MarketplaceOrdering
    Script     1.1.1                 Az.Media
    Script     1.1.2                 Az.Migrate
    Script     3.0.0                 Az.Monitor
    Script     4.13.0                Az.Network
    Script     1.1.1                 Az.NotificationHubs
    Script     3.0.0                 Az.OperationalInsights
    Script     1.4.1                 Az.PolicyInsights
    Script     1.1.2                 Az.PowerBIEmbedded
    Script     1.0.3                 Az.PrivateDns
    Script     5.0.0                 Az.RecoveryServices
    Script     1.5.1                 Az.RedisCache
    Script     1.0.0                 Az.RedisEnterpriseCache
    Script     1.0.3                 Az.Relay
    Script     1.0.0                 Az.ResourceMover
    Script     5.1.0                 Az.Resources
    Script     1.1.1                 Az.Security
    Script     1.1.0                 Az.SecurityInsights
    Script     1.6.0                 Az.ServiceBus
    Script     3.0.1                 Az.ServiceFabric
    Script     1.4.0                 Az.SignalR
    Script     3.6.0                 Az.Sql
    Script     1.1.0                 Az.SqlVirtualMachine
    Script     1.0.0                 Az.StackHCI
    Script     4.1.0                 Az.Storage
    Script     1.6.1                 Az.StorageSync
    Script     2.0.0                 Az.StreamAnalytics
    Script     1.0.0                 Az.Support
    Script     1.0.4                 Az.TrafficManager
    Script     2.9.0                 Az.Websites
    
  3. 登出 Azure 帳戶

    Logout-AzAccount
    
  4. 連接 Azure 帳戶

    $tenantId = '4b972557-b992-4f77-b743-41f0d251f741'
    Connect-AzAccount -Tenant $tenantId
    

    想知道你目前登入的使用者帳戶加入了哪幾個 Tenants 可以用以下命令查詢 (可以查到相當完整的資訊):

    Get-AzTenant
    
  5. 取得所有 訂用帳戶 (Subscription) 清單

    即便你在連接 Azure 帳戶時有加上 -Tenant 參數,這裡依然會列出你的登入帳號中隸屬的所有租戶下的所有訂用帳戶喔!

    Get-AzSubscription
    

    建議使用 -TenantId 參數篩選出特定 tenantId訂用帳戶

    $tenantId = '4b972557-b992-4f77-b743-41f0d251f741'
    Get-AzSubscription -TenantId $tenantId
    

盤點一個 訂用帳戶 (Subscription) 下有多少 角色指派 (Role Assignments)

  • 透過 Azure Portal 查詢

    進入 訂用帳戶 (Subscription) 的 Access Control (IAM) 就可以查詢到該訂用帳戶下所有的角色指派!

    這裡要注意 Scope 欄位,從該欄位可以看出這個角色被套用在哪個範圍,可能是 Root, Subscription, Resource GroupResource 等等。

  • 透過 Azure CLI 查詢

    列出所有角色授權清單 (包含所有服務主體在內)

    az role assignment list
    

    列出所有角色授權清單 (包含繼承的主體)

    az role assignment list --include-inherited
    

    列出所有角色授權清單 (包含繼承的主體傳統管理員)

    az role assignment list --include-inherited --include-classic-administrators
    

    列出所有角色授權清單 (包含繼承的主體傳統管理員資源群組的角色授權)

    az role assignment list --include-inherited --include-classic-administrators --include-groups
    

    列出所有角色授權清單 (包含繼承的主體傳統管理員資源群組所有資源的角色授權)

    az role assignment list --include-inherited --include-classic-administrators --include-groups --all
    

    列出所有角色指派清單中無法找到的主體的無效項目

    az role assignment list --include-groups --all
    
  • Azure Az PowerShell

    列出所有角色授權清單 (包含繼承的主體資源群組所有資源的角色授權)

    $subscriptionId = '95970ee4-4d92-4f5f-b253-395c986c5fca'
    Get-AzRoleAssignment -Scope "/subscriptions/${subscriptionId}" -WarningAction Ignore
    

    列出所有角色授權清單 (包含繼承的主體資源群組所有資源傳統管理員的角色授權)

    $subscriptionId = '95970ee4-4d92-4f5f-b253-395c986c5fca'
    Get-AzRoleAssignment -Scope "/subscriptions/${subscriptionId}" -IncludeClassicAdministrators -WarningAction Ignore
    

    列出所有角色指派清單中無法找到的主體的無效項目

    $subscriptionId = '95970ee4-4d92-4f5f-b253-395c986c5fca'
    Get-AzRoleAssignment -Scope "/subscriptions/${subscriptionId}" -WarningAction Ignore | Where-Object { $_.DisplayName -eq $null }
    

盤點一個 使用者主體 (User Principal) 有多少 角色指派 (Role Assignments)

  • 透過 Azure Portal 查詢

    進入 Azure Active Directory > Users > 選取使用者 > Azure role assignments 之後,這裡有個下拉選單可以讓你切換不同的訂用帳戶,此時你就可以看到該使用者在此訂用帳戶被指派的角色。

盤點一個 服務主體 (Service Principal) 有多少 角色指派 (Role Assignments)

  • 建立 服務主體 (Service Principal) 的方法

    最簡單的方法,預設會建立名為 azure-cli-當天UTC日期時間服務主體 (Service Principal),並自動將該 服務主體 (Service Principal) 指派 目前選用訂用帳戶Contributor 角色:

    az ad sp create-for-rbac -o json
    

    你也可以明確指定預設要在目前訂用帳戶角色指派

    az ad sp create-for-rbac --role Contributor -o json
    

    你也可以指定自訂的 服務主體 (Service Principal) 名稱

    az ad sp create-for-rbac --name 'my-service-principal' --role Contributor -o json
    
  • 透過 Azure CLI 查詢 服務主體 (Service Principal) 指派了多少角色

    $subscriptions = (az account list -o json | ConvertFrom-Json)
    $subscriptions | ForEach-Object {
        echo "正在列出訂用帳戶 $($_.name) 的角色指派清單"
        az role assignment list --subscription $_.id --query "[?principalType=='ServicePrincipal']" -o json
    }
    

    目前 Azure CLI 取得任何資料若有包含 non-English 字元會遇到字元無法轉換成 Unicode 的問題,這個問題目前有三種解決方案,但是都不完美,所以在 Windows 應該是無解了,建議改用 Linux 或 Azure Az PowerShell 來解決此問題。

  • 透過 Azure Az PowerShell 查詢 服務主體 (Service Principal) 指派了多少角色

    # 取得所有訂用帳戶
    $tenantId = '4b972557-b992-4f77-b743-41f0d251f741'
    $subscriptions = Get-AzSubscription -TenantId $tenantId
    
    # 取得所有角色指派
    $subscriptions | ForEach-Object {
        echo "正在列出訂用帳戶 $($_.Name) 的角色指派清單"
        Get-AzRoleAssignment -Scope "/subscriptions/$($_.Id)" -WarningAction Ignore | Where-Object { $_.ObjectType -eq 'ServicePrincipal' }
    }
    
  • 列出所有訂用帳戶中所有的角色指派中無法找到的主體的無效項目

    Azure Az PowerShell

    # 取得所有訂用帳戶
    $tenantId = '4b972557-b992-4f77-b743-41f0d251f741'
    $subscriptions = Get-AzSubscription -TenantId $tenantId
    
    # 取得所有角色指派
    $subscriptions | ForEach-Object {
        echo "正在列出訂用帳戶 $($_.Name) 的無效角色指派清單"
        Get-AzRoleAssignment -Scope "/subscriptions/$($_.Id)" -WarningAction Ignore | Where-Object { $_.DisplayName -eq $null }
    }
    

    Azure CLI

    $subscriptions = (az account list -o json | ConvertFrom-Json)
    $subscriptions | ForEach-Object {
        echo "正在列出訂用帳戶 $($_.name) 的角色指派清單"
        az role assignment list --subscription $_.id --query "[?principalName=='']" -o json
    }
    

使用 Azure CLI 刪除角色指派的方法

  1. 先找出訂用帳戶中的 role assignment ids

    az role assignment list --include-inherited --include-classic-administrators --include-groups --all -o json
    

    注意: 請找出角色指派的 id 屬性,該屬性長相很像網址路徑 (PATH_INFO)。

  2. 刪除角色指派

    $subscriptionId = '90418710-4b33-401d-bfe2-5141bda70b64'
    $roleAssignmentsId = '0f2ce15f-1c21-4842-a84f-3884cc49258a'
    az role assignment delete --ids "/subscriptions/${subscriptionId}/providers/Microsoft.Authorization/roleAssignments/${roleAssignmentsId}"
    

    如果要一次刪除兩個角色指派,可以這樣用:

    az role assignment delete --ids "/subscriptions/90418710-4b33-401d-bfe2-5141bda70b64/providers/Microsoft.Authorization/roleAssignments/78ff8eb5-1adc-4a61-a1a3-0cca6a25d652" "/subscriptions/90418710-4b33-401d-bfe2-5141bda70b64/providers/Microsoft.Authorization/roleAssignments/4449d6f8-7781-49c1-a4e2-249232220a30"
    

Recommend

  • 18

    還原被碼掉的 PEM 資訊 (SSH RSA key) 在「Recovering a full PEM Private Key when half of it is redacted」這邊看到的,起因是

  • 5

    盤點及比較 DLL 版本差異-黑暗執行緒 遇到盤點 .NET 組件(.dll)版號比對多個環境版本差異的需求,看起來是 PowerShell 的主場,試寫一下,比想像還簡單: param ( [Parameter(Mandatory = $true)] [string] $path ) Get-C...

  • 7

    利用 SynchronizationContext 讓 ASP.NET 背景執行緒取用 HttpContext 資訊我們在寫 ASP.NET (.NET Framework) 的時候,可能會需要利用非同步作業在背景執行一些工作,同時可能也需要偶爾取用 HttpContext 相關資訊。事實上當我們在 ASP.NET (.NET...

  • 14

    用 Akamai 提供的 akahelp 分析 DNS Resolver 的資訊 整理資料的時候看到以前就看到的資訊,Akamai 有提供工具,可以看 DNS resolver 的資訊:「

  • 8

    Windows 10/11 安裝小技巧 過去在安裝 Windows 10 時,我們可以選擇使用 Hotmail/Outlook 等 Microsoft 帳號登入,或在本機建立離線帳戶。傳說某版 Windows 10 之後乃至於 Windows 11,只要安裝期間有連上 Internet,安裝介面便...

  • 8

    Account Abstraction 抽象帳戶:EIP-2938 簡介本文主要由 EIP-2938 介紹 Account Abstraction (AA) 的相關內容。

  • 9

    Account Abstraction 抽象帳戶:EIP-3074 與 EIP-4337 簡介以太坊的社群其實討論了好一陣子究竟要如何設計新一代的帳戶,不同的思路也對應了不同的提案,本文主要介紹 AA 在以下這兩種方向的相關內容:

  • 4
    • blog.darkthread.net 2 years ago
    • Cache

    我的 2022 技術盤點

    轉眼就要邁入 2023 了,年紀愈大感覺時間過得愈快,記得才剛過完年沒多久,怎麼一年又要過完了。(依據科學家研究,可能原因包含多巴胺減少、同時間長度佔總人生的

  • 9

    Y2Q 倒數與 Q-Day,當代資訊安全防線即將崩潰-黑暗執行緒 之前在 YT 筆記 - 搞懂量子糾纏、疊加態、測不準原理提到,以量子位元...

  • 3
    • blog.darkthread.net 1 year ago
    • Cache

    我的 2023 技術盤點

    又到了年末盤點自己技術成長的時間。2023 真是充滿驚奇的一年,年初 ChatGPT 橫空而出,在江湖捲起千堆雪,緊接 LK-99 常溫超導體的新聞,有種人類文明居然要在一年內翻轉兩次的錯覺。...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK