# network-flow-logs / azure / t1 "Standard" # # Flow logs for several virtual networks into one storage account that # refuses public access and older TLS, records moved to the cool tier after # 30 days, traffic analytics in a workspace, and saved queries for the # questions an investigation starts with. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "confidential" managed_by = "terraform" }, var.extra_tags) searches = { malicious-flows = { name = "Flows to or from addresses Microsoft rates malicious" query = "NTANetAnalytics | where FlowType == \"MaliciousFlow\" | project TimeGenerated, SrcIp, DestIp, DestPort, L4Protocol, FlowDirection" } top-outbound-destinations = { name = "Busiest outbound destinations on the internet, last day" query = "NTANetAnalytics | where TimeGenerated > ago(1d) and FlowType == \"ExternalPublic\" and FlowDirection == \"O\" | summarize Flows = count() by DestIp, DestPort | top 50 by Flows" } one-address = { name = "Everything one private address talked to" query = "NTANetAnalytics | where SrcIp == \"REPLACE-WITH-IP\" or DestIp == \"REPLACE-WITH-IP\" | project TimeGenerated, SrcIp, DestIp, DestPort, L4Protocol, FlowType" } } } resource "random_string" "suffix" { length = 6 special = false upper = false } data "azurerm_network_watcher" "region" { name = "NetworkWatcher_${var.location}" resource_group_name = "NetworkWatcherRG" } resource "azurerm_resource_group" "flow" { name = "rg-flow-logs" location = var.location tags = local.tags } resource "azurerm_storage_account" "flow" { name = "flowlogs${random_string.suffix.result}" resource_group_name = azurerm_resource_group.flow.name location = azurerm_resource_group.flow.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_storage_management_policy" "flow" { storage_account_id = azurerm_storage_account.flow.id rule { name = "cool-after-30-days" enabled = true filters { blob_types = ["blockBlob"] } actions { base_blob { tier_to_cool_after_days_since_modification_greater_than = 30 } } } } resource "azurerm_log_analytics_workspace" "flow" { name = "law-flow-${random_string.suffix.result}" location = azurerm_resource_group.flow.location resource_group_name = azurerm_resource_group.flow.name sku = "PerGB2018" retention_in_days = 90 tags = local.tags } resource "azurerm_network_watcher_flow_log" "vnet" { for_each = var.vnet_ids name = "flow-${each.key}" 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 = each.value storage_account_id = azurerm_storage_account.flow.id enabled = true version = 2 tags = local.tags retention_policy { enabled = true days = var.retention_days } traffic_analytics { enabled = true workspace_id = azurerm_log_analytics_workspace.flow.workspace_id workspace_region = azurerm_log_analytics_workspace.flow.location workspace_resource_id = azurerm_log_analytics_workspace.flow.id interval_in_minutes = var.analytics_interval_minutes } } resource "azurerm_log_analytics_saved_search" "investigation" { for_each = local.searches name = each.key log_analytics_workspace_id = azurerm_log_analytics_workspace.flow.id category = "Investigation" display_name = each.value.name query = each.value.query tags = local.tags }