# forensic-workstation / azure / t1 "Standard" # # The isolated network: no public IP, no peering, no route to the internet, # Bastion as the only way in, a private endpoint as the only way to the # evidence, and every refused connection logged and alerted. Native alerts # read the activity log for the changes that would quiet that alert, and # locks keep the record and the alerts from being deleted. # # Quick start lets the workstation reach any storage account in the region. # Here it reaches one: the evidence account, through a private endpoint. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "restricted" managed_by = "terraform" }, var.extra_tags) bastion_prefix = cidrsubnet(var.address_space, 2, 0) analysis_prefix = cidrsubnet(var.address_space, 3, 2) endpoints_prefix = cidrsubnet(var.address_space, 4, 6) # The rules Microsoft documents for AzureBastionSubnet. Bastion does not work # in a subnet whose group is missing one. bastion_rules = { in-https-internet = { priority = 120, direction = "Inbound", ports = ["443"], source = "Internet", destination = "*" } in-gateway-manager = { priority = 130, direction = "Inbound", ports = ["443"], source = "GatewayManager", destination = "*" } in-load-balancer = { priority = 140, direction = "Inbound", ports = ["443"], source = "AzureLoadBalancer", destination = "*" } in-bastion-host-traffic = { priority = 150, direction = "Inbound", ports = ["8080", "5701"], source = "VirtualNetwork", destination = "VirtualNetwork" } out-ssh-rdp = { priority = 100, direction = "Outbound", ports = ["22", "3389"], source = "*", destination = "VirtualNetwork" } out-azure-cloud = { priority = 110, direction = "Outbound", ports = ["443"], source = "*", destination = "AzureCloud" } out-bastion-host = { priority = 120, direction = "Outbound", ports = ["8080", "5701"], source = "VirtualNetwork", destination = "VirtualNetwork" } out-session-info = { priority = 130, direction = "Outbound", ports = ["80"], source = "*", destination = "Internet" } } # The workstation: SSH in from Bastion, HTTPS out to the evidence endpoint, # and nothing else in either direction. Priority 4000 and 4010 sit above # Azure's default rules, which would otherwise allow the whole network. analysis_rules = { in-ssh-from-bastion = { priority = 100, direction = "Inbound", access = "Allow", protocol = "Tcp", ports = ["22"], source = local.bastion_prefix, destination = "*" } in-deny-all = { priority = 4000, direction = "Inbound", access = "Deny", protocol = "*", ports = ["*"], source = "*", destination = "*" } out-evidence = { priority = 100, direction = "Outbound", access = "Allow", protocol = "Tcp", ports = ["443"], source = "*", destination = local.endpoints_prefix } out-deny-network = { priority = 4000, direction = "Outbound", access = "Deny", protocol = "*", ports = ["*"], source = "*", destination = "VirtualNetwork" } out-deny-everything = { priority = 4010, direction = "Outbound", access = "Deny", protocol = "*", ports = ["*"], source = "*", destination = "*" } } # Changes the workspace might never hear of. A deleted or rewritten flow # log or diagnostic setting stops the record of its own change, and a # disabled rule does not run. native = { flow-log-deleted = { operation = "Microsoft.Network/networkWatchers/flowLogs/delete" doc = "A flow log was deleted in the subscription, possibly the one the refused-connection alert reads." } flow-log-written = { operation = "Microsoft.Network/networkWatchers/flowLogs/write" doc = "A flow log was created or changed in the subscription. A change can turn off traffic analytics or send it to another workspace." } diagnostic-setting-deleted = { operation = "Microsoft.Insights/diagnosticSettings/delete" doc = "A diagnostic setting was deleted in the subscription, possibly the one that records Bastion sessions." } # Policy remediation and other teams' monitoring write diagnostic # settings too, so expect some of these. diagnostic-setting-written = { operation = "Microsoft.Insights/diagnosticSettings/write" doc = "A diagnostic setting was created or changed in the subscription. A rewrite can drop a category or send the logs somewhere else." } # Azure writes this when a log search rule's query has failed for a week. rule-disabled = { operation = "Microsoft.Insights/ScheduledQueryRules/disable/action" doc = "Azure disabled a log search alert rule in the subscription because its query kept failing." } # Deleting a lock is the step before deleting what it protects. lock-deleted = { operation = "Microsoft.Authorization/locks/delete" doc = "A management lock was deleted in the subscription, possibly one that keeps the forensic record or its alerts from being deleted." } } } resource "random_string" "suffix" { length = 6 special = false upper = false } resource "azurerm_resource_group" "forensics" { name = "rg-forensics" location = var.location tags = local.tags } resource "azurerm_virtual_network" "forensics" { name = "vnet-forensics" location = azurerm_resource_group.forensics.location resource_group_name = azurerm_resource_group.forensics.name address_space = [var.address_space] tags = local.tags } resource "azurerm_subnet" "bastion" { name = "AzureBastionSubnet" resource_group_name = azurerm_resource_group.forensics.name virtual_network_name = azurerm_virtual_network.forensics.name address_prefixes = [local.bastion_prefix] } resource "azurerm_subnet" "analysis" { name = "analysis" resource_group_name = azurerm_resource_group.forensics.name virtual_network_name = azurerm_virtual_network.forensics.name address_prefixes = [local.analysis_prefix] default_outbound_access_enabled = false } resource "azurerm_subnet" "endpoints" { name = "endpoints" resource_group_name = azurerm_resource_group.forensics.name virtual_network_name = azurerm_virtual_network.forensics.name address_prefixes = [local.endpoints_prefix] default_outbound_access_enabled = false # Without this, network security groups do not apply to private endpoint # traffic, and nsg-forensics-endpoints would do nothing. private_endpoint_network_policies = "Enabled" } resource "azurerm_network_security_group" "bastion" { name = "nsg-forensics-bastion" location = azurerm_resource_group.forensics.location resource_group_name = azurerm_resource_group.forensics.name tags = local.tags } resource "azurerm_network_security_rule" "bastion" { for_each = local.bastion_rules name = each.key network_security_group_name = azurerm_network_security_group.bastion.name resource_group_name = azurerm_resource_group.forensics.name priority = each.value.priority direction = each.value.direction access = "Allow" protocol = "*" source_port_range = "*" destination_port_ranges = each.value.ports source_address_prefix = each.value.source destination_address_prefix = each.value.destination description = "Required by Azure Bastion." } resource "azurerm_subnet_network_security_group_association" "bastion" { subnet_id = azurerm_subnet.bastion.id network_security_group_id = azurerm_network_security_group.bastion.id depends_on = [azurerm_network_security_rule.bastion] } resource "azurerm_network_security_group" "analysis" { name = "nsg-forensics-analysis" location = azurerm_resource_group.forensics.location resource_group_name = azurerm_resource_group.forensics.name tags = local.tags } resource "azurerm_network_security_rule" "analysis" { for_each = local.analysis_rules name = each.key network_security_group_name = azurerm_network_security_group.analysis.name resource_group_name = azurerm_resource_group.forensics.name priority = each.value.priority direction = each.value.direction access = each.value.access protocol = each.value.protocol source_port_range = "*" destination_port_ranges = each.value.ports source_address_prefix = each.value.source destination_address_prefix = each.value.destination } resource "azurerm_subnet_network_security_group_association" "analysis" { subnet_id = azurerm_subnet.analysis.id network_security_group_id = azurerm_network_security_group.analysis.id depends_on = [azurerm_network_security_rule.analysis] } # The endpoint accepts HTTPS from the workstation only. resource "azurerm_network_security_group" "endpoints" { name = "nsg-forensics-endpoints" location = azurerm_resource_group.forensics.location resource_group_name = azurerm_resource_group.forensics.name tags = local.tags security_rule { name = "in-https-from-workstation" priority = 100 direction = "Inbound" access = "Allow" protocol = "Tcp" source_port_range = "*" destination_port_range = "443" source_address_prefix = local.analysis_prefix destination_address_prefix = "*" } security_rule { name = "in-deny-all" priority = 4000 direction = "Inbound" access = "Deny" protocol = "*" source_port_range = "*" destination_port_range = "*" source_address_prefix = "*" destination_address_prefix = "*" } } resource "azurerm_subnet_network_security_group_association" "endpoints" { subnet_id = azurerm_subnet.endpoints.id network_security_group_id = azurerm_network_security_group.endpoints.id } # --------------------------------------------------------------------------- # The only way to the evidence # --------------------------------------------------------------------------- resource "azurerm_private_dns_zone" "blob" { name = "privatelink.blob.core.windows.net" resource_group_name = azurerm_resource_group.forensics.name tags = local.tags } resource "azurerm_private_dns_zone_virtual_network_link" "blob" { name = "vnet-forensics" private_dns_zone_id = azurerm_private_dns_zone.blob.id virtual_network_id = azurerm_virtual_network.forensics.id tags = local.tags } resource "azurerm_private_endpoint" "evidence" { name = "pe-evidence-blob" location = azurerm_resource_group.forensics.location resource_group_name = azurerm_resource_group.forensics.name subnet_id = azurerm_subnet.endpoints.id tags = local.tags private_service_connection { name = "evidence-blob" private_connection_resource_id = var.evidence_storage_account_id subresource_names = ["blob"] is_manual_connection = false } private_dns_zone_group { name = "blob" private_dns_zone_ids = [azurerm_private_dns_zone.blob.id] } } # --------------------------------------------------------------------------- # Every refused connection, logged and alerted # --------------------------------------------------------------------------- data "azurerm_network_watcher" "region" { name = "NetworkWatcher_${var.location}" resource_group_name = "NetworkWatcherRG" } resource "azurerm_storage_account" "flows" { name = "forensicflows${random_string.suffix.result}" resource_group_name = azurerm_resource_group.forensics.name location = azurerm_resource_group.forensics.location account_tier = "Standard" account_replication_type = "ZRS" min_tls_version = "TLS1_2" allow_nested_items_to_be_public = false cross_tenant_replication_enabled = false tags = local.tags # Left on for Network Watcher; see the scan decision on CKV2_AZURE_40. # Nothing here hands out SAS tokens. One made to last longer than a day is # logged as a policy violation. shared_access_key_enabled = true sas_policy { expiration_period = "01.00:00:00" } blob_properties { delete_retention_policy { days = 30 } } } resource "azurerm_log_analytics_workspace" "forensics" { name = "law-forensics-${random_string.suffix.result}" location = azurerm_resource_group.forensics.location resource_group_name = azurerm_resource_group.forensics.name sku = "PerGB2018" retention_in_days = var.log_retention_days tags = local.tags } resource "azurerm_network_watcher_flow_log" "forensics" { name = "flow-forensics" network_watcher_name = data.azurerm_network_watcher.region.name resource_group_name = data.azurerm_network_watcher.region.resource_group_name location = var.location target_resource_id = azurerm_virtual_network.forensics.id storage_account_id = azurerm_storage_account.flows.id enabled = true version = 2 tags = local.tags retention_policy { enabled = true days = var.log_retention_days } traffic_analytics { enabled = true workspace_id = azurerm_log_analytics_workspace.forensics.workspace_id workspace_region = azurerm_log_analytics_workspace.forensics.location workspace_resource_id = azurerm_log_analytics_workspace.forensics.id interval_in_minutes = 10 } } resource "azurerm_monitor_action_group" "forensics" { name = "forensics-alerts" resource_group_name = azurerm_resource_group.forensics.name short_name = "forensics" tags = local.tags # Receiver names are numbered, because names must differ regardless of # case. dynamic "email_receiver" { for_each = var.alert_emails content { name = "email-${email_receiver.key}" email_address = email_receiver.value } } } # Azure evaluates these alerts on the activity log itself, with no workspace # in between, and they cost nothing. They still notify through the one action # group, so someone who can edit it can disable it or change its addresses. resource "azurerm_monitor_activity_log_alert" "native" { for_each = local.native name = "forensic-workstation-${each.key}" resource_group_name = azurerm_resource_group.forensics.name location = "global" scopes = ["/subscriptions/${var.subscription_id}"] tags = local.tags description = each.value.doc criteria { category = "Administrative" operation_name = each.value.operation } action { action_group_id = azurerm_monitor_action_group.forensics.id } } # Traffic analytics writes a flow up to 10 minutes after it happens. The query # looks back an hour and keeps rows that arrived in the last 15 minutes, so a # late row is neither missed nor alerted twice. The alert follows the attempt # by 10 to 25 minutes, once traffic analytics is writing. The rule is made # after the flow log, though the first flows take a while longer to arrive. # The status filter accepts the short and long forms of "denied"; after the # first test, check which one your workspace contains. resource "azurerm_monitor_scheduled_query_rules_alert_v2" "refused" { name = "forensic-workstation-refused-connection" resource_group_name = azurerm_resource_group.forensics.name location = azurerm_resource_group.forensics.location scopes = [azurerm_log_analytics_workspace.forensics.id] description = "The forensic workstation tried to connect somewhere its network rules refuse." severity = 2 evaluation_frequency = "PT15M" window_duration = "PT1H" skip_query_validation = true tags = local.tags criteria { query = <<-KQL NTANetAnalytics | where ingestion_time() > ago(15m) | where SubType == "FlowLog" | where SrcIp == "${azurerm_network_interface.workstation.private_ip_address}" | where FlowStatus in~ ("D", "Denied") | project TimeGenerated, SrcIp, DestIp, DestPort, L4Protocol KQL time_aggregation_method = "Count" operator = "GreaterThan" threshold = 0 } action { action_groups = [azurerm_monitor_action_group.forensics.id] } depends_on = [azurerm_network_watcher_flow_log.forensics] } # The record and the alerts cannot be deleted while these locks are on: the # workspace, the flow log storage, the action group, and every alert. Each # lock sits on one resource, so the workstation can still be rebuilt between # cases. Deleting a lock first fires forensic-workstation-lock-deleted. A # cannot-delete lock does not stop an edit, such as disabling the action # group or an alert, or a workspace purge. # # The locks are made last and removed first, so terraform destroy can run. # Any change that replaces or removes a locked resource fails while its lock # is on. resource "azurerm_management_lock" "record" { for_each = merge({ workspace = azurerm_log_analytics_workspace.forensics.id flow-storage = azurerm_storage_account.flows.id action-group = azurerm_monitor_action_group.forensics.id refused = azurerm_monitor_scheduled_query_rules_alert_v2.refused.id }, { for k, a in azurerm_monitor_activity_log_alert.native : k => a.id }) name = "forensic-workstation-${each.key}" scope = each.value lock_level = "CanNotDelete" notes = "Keeps the forensic record and its alerts from being deleted. Managed by Terraform." }