# container-evidence / azure / t1 "Standard" # # Evidence from a compromised container on AKS: an immutable blob container # encrypted with your own key, a year of retention you can lock, responder # groups that can snapshot node disks and add evidence but not read or delete # it, and capture.sh, which runs the steps in order and logs each one. # # Kubernetes rights come from the incident-responder cluster role in the # Kubernetes evidence template. Bind it to each responder group by object ID # (kind: Group, name: ). # # terraform output capture the command to run # terraform output verify checks on a test pod data "azurerm_client_config" "current" {} data "azurerm_kubernetes_cluster" "this" { name = var.cluster_name resource_group_name = var.cluster_resource_group } locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "restricted" managed_by = "terraform" }, var.extra_tags) network_default = length(var.allowed_ip_ranges) > 0 ? "Deny" : "Allow" node_group = data.azurerm_kubernetes_cluster.this.node_resource_group_id # Keys are known at plan time: the role name and the group's object ID. grants = merge( { for g in var.responder_group_object_ids : "node-reader/${g}" => { scope = local.node_group, role = azurerm_role_definition.node_reader.role_definition_resource_id, group = g } }, { for g in var.responder_group_object_ids : "responder/${g}" => { scope = azurerm_resource_group.evidence.id, role = azurerm_role_definition.responder.role_definition_resource_id, group = g } }, ) } resource "random_string" "suffix" { length = 6 special = false upper = false } # Snapshots go here, outside the node resource group. Deleting the cluster # deletes that group and everything in it, and AKS does not support changes # made there. A snapshot must be in its disk's region, so the group follows # the cluster. resource "azurerm_resource_group" "evidence" { name = "rg-container-evidence-${random_string.suffix.result}" location = data.azurerm_kubernetes_cluster.this.location tags = local.tags } resource "azurerm_storage_account" "evidence" { name = "ctrevidence${random_string.suffix.result}" resource_group_name = azurerm_resource_group.evidence.name location = azurerm_resource_group.evidence.location account_tier = "Standard" account_replication_type = "GRS" account_kind = "StorageV2" min_tls_version = "TLS1_2" https_traffic_only_enabled = true allow_nested_items_to_be_public = false shared_access_key_enabled = false default_to_oauth_authentication = true infrastructure_encryption_enabled = true tags = local.tags identity { type = "UserAssigned" identity_ids = [azurerm_user_assigned_identity.storage.id] } # versionless_id means the account follows the key as it rotates. customer_managed_key { key_vault_key_id = azurerm_key_vault_key.evidence.versionless_id user_assigned_identity_id = azurerm_user_assigned_identity.storage.id } blob_properties { delete_retention_policy { days = 30 } container_delete_retention_policy { days = 30 } } network_rules { default_action = local.network_default bypass = ["AzureServices", "Logging", "Metrics"] ip_rules = var.allowed_ip_ranges } lifecycle { prevent_destroy = true } depends_on = [azurerm_role_assignment.storage_key_user] } resource "azurerm_storage_container" "evidence" { name = "evidence" storage_account_id = azurerm_storage_account.evidence.id container_access_type = "private" } # Every blob is fixed for retention_days from the moment it is written. Appends # stay off: evidence is written once, and a second capture of the same pod # goes to a new folder. resource "azurerm_storage_container_immutability_policy" "evidence" { storage_container_resource_manager_id = azurerm_storage_container.evidence.id immutability_period_in_days = var.retention_days locked = var.lock_retention } # Who read, wrote, or tried to delete evidence, and from where. resource "azurerm_monitor_diagnostic_setting" "evidence_blobs" { count = var.log_analytics_workspace_id == "" ? 0 : 1 name = "evidence-access" target_resource_id = "${azurerm_storage_account.evidence.id}/blobServices/default" log_analytics_workspace_id = var.log_analytics_workspace_id enabled_log { category = "StorageRead" } enabled_log { category = "StorageWrite" } enabled_log { category = "StorageDelete" } } # Read a node and its disks, in the cluster's node resource group. Nothing # here can change, stop, or delete them. resource "azurerm_role_definition" "node_reader" { name = "Container evidence node reader ${random_string.suffix.result}" scope = local.node_group assignable_scopes = [local.node_group] description = "Reads AKS node VMs and their disks, so their disks can be snapshotted elsewhere." permissions { actions = [ "Microsoft.Compute/virtualMachineScaleSets/read", "Microsoft.Compute/virtualMachineScaleSets/virtualMachines/read", "Microsoft.Compute/virtualMachines/read", "Microsoft.Compute/disks/read", "Microsoft.Compute/disks/beginGetAccess/action", ] } } # In the evidence group: create snapshots and add blobs. No read of blobs, no # delete of anything. resource "azurerm_role_definition" "responder" { name = "Container evidence responder ${random_string.suffix.result}" scope = azurerm_resource_group.evidence.id assignable_scopes = [azurerm_resource_group.evidence.id] description = "Creates snapshots and adds evidence blobs. Cannot read, change, or delete evidence." permissions { actions = [ "Microsoft.Resources/subscriptions/resourceGroups/read", "Microsoft.Compute/snapshots/read", "Microsoft.Compute/snapshots/write", ] data_actions = [ "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/write", "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/add/action", ] } } resource "azurerm_role_assignment" "responders" { for_each = local.grants scope = each.value.scope role_definition_id = each.value.role principal_id = each.value.group principal_type = "Group" } # Enough to fetch a kubeconfig. What responders may do inside the cluster is # set by the incident-responder role. resource "azurerm_role_assignment" "cluster_user" { for_each = toset(var.responder_group_object_ids) scope = data.azurerm_kubernetes_cluster.this.id role_definition_name = "Azure Kubernetes Service Cluster User Role" principal_id = each.value principal_type = "Group" }