# Blob reads on the accounts you name, request by request, with the caller # and the bytes sent. The activity log does not record reads. These resource # logs do, and they are billed by volume. locals { reads = length(var.sensitive_storage_account_ids) > 0 # Bytes one caller read in the last hour, across the sensitive accounts, # counting blob downloads and Data Lake file reads. A request signed in # with Microsoft Entra ID names the caller. A request made with a key or a # SAS names no one, so those are totalled per account, wherever they came # from. The empty table keeps the query valid before the first read # arrives; Azure disables a rule whose query fails for a week. bulk_read_bytes = floor(var.bulk_read_gigabytes * 1000000000) bulk_read_query = "union isfuzzy=true (datatable(TimeGenerated: datetime, OperationName: string, StatusCode: string, ResponseBodySize: long, RequesterUpn: string, RequesterObjectId: string, AuthenticationType: string, AccountName: string)[]), StorageBlobLogs | where TimeGenerated > ago(1h) | where OperationName in (\"GetBlob\", \"ReadFile\") and toint(StatusCode) < 300 | extend Caller = iff(AuthenticationType in (\"AccountKey\", \"SAS\"), strcat(AuthenticationType, \" on \", AccountName), coalesce(RequesterUpn, RequesterObjectId, AuthenticationType)) | summarize Bytes = sum(ResponseBodySize), Blobs = count(), Accounts = make_set(AccountName, 10) by Caller | where Bytes > ${local.bulk_read_bytes}" } resource "azurerm_monitor_diagnostic_setting" "reads" { for_each = toset(var.sensitive_storage_account_ids) name = "insider-data-theft-reads" target_resource_id = "${each.value}/blobServices/default" log_analytics_workspace_id = azurerm_log_analytics_workspace.insider.id enabled_log { category = "StorageRead" } } # One alert per caller who read more than the threshold in an hour. resource "azurerm_monitor_scheduled_query_rules_alert_v2" "bulk_read" { count = local.reads ? 1 : 0 name = "insider-bulk-read" resource_group_name = data.azurerm_resource_group.security.name location = data.azurerm_resource_group.security.location scopes = [azurerm_log_analytics_workspace.insider.id] description = "One caller read more than ${var.bulk_read_gigabytes} GB of blobs from the sensitive accounts in an hour. The saved query insider-bulk-read shows who, how much, and from which accounts." severity = 2 evaluation_frequency = "PT15M" window_duration = "PT1H" skip_query_validation = true tags = local.tags identity { type = "SystemAssigned" } criteria { query = local.bulk_read_query time_aggregation_method = "Count" operator = "GreaterThan" threshold = 0 dimension { name = "Caller" operator = "Include" values = ["*"] } } action { action_groups = [azurerm_monitor_action_group.insider.id] } depends_on = [azurerm_monitor_diagnostic_setting.reads] } resource "azurerm_role_assignment" "bulk_read_reads_workspace" { count = local.reads ? 1 : 0 scope = azurerm_log_analytics_workspace.insider.id role_definition_name = "Log Analytics Reader" principal_id = azurerm_monitor_scheduled_query_rules_alert_v2.bulk_read[0].identity[0].principal_id principal_type = "ServicePrincipal" }