AZ-500 LAB 22 ~75 phút Chương 05 Secure Networking

NSG Flow Logs + Traffic Analytics

Bật Network Watcher, tạo Storage Account với minimum TLS 1.2, bật NSG Flow Logs v2, kết nối Log Analytics Workspace để bật Traffic Analytics, tạo traffic test, phân tích dashboard allow/deny và query KQL.

🎯 Mục Tiêu Lab

Bật Network Watcher tại region southeastasia

Tạo Storage Account với minimum TLS version 1.2 để lưu flow logs

Bật NSG Flow Logs version 2 trên NSG từ lab 19

Tạo Log Analytics Workspace và bật Traffic Analytics

Kích hoạt traffic test, xem log JSON trong Storage

Query KQL trên Log Analytics để phân tích traffic allow/deny

📋 Chuẩn Bị

Yêu cầu:
  • Có NSG từ Lab 19 hoặc tạo NSG mới
  • Network Watcher provider đăng ký trong subscription
  • Quyền Contributor + Network Contributor
  • Traffic Analytics cần Log Analytics Workspace (có thể phát sinh chi phí)
Tài nguyên sẽ tạo:
  • RG: rg-az500-flowlogs-lab22
  • Storage: staz500flowlogs[random] (TLS 1.2)
  • Log Analytics: law-az500-lab22
  • NSG + VM từ lab 19 (hoặc tạo mới)

🏗️ Kịch Bản

Sau khi cấu hình NSG (Lab 19), HoaTranLab cần giám sát và điều tra traffic: ai đang kết nối, bao nhiêu lần bị chặn, có dấu hiệu scan hay tấn công không. NSG Flow Logs ghi lại tuple 5 thành phần (src IP, dst IP, src port, dst port, protocol) cùng với kết quả Allow/Deny vào Storage. Traffic Analytics xử lý dữ liệu này để tạo dashboard trực quan.

-- Flow log format (simplified) --
timestamp, src_ip, dst_ip, src_port, dst_port, protocol, traffic_flow, traffic_decision, flow_state
VD: 1716220800,1.2.3.4,10.19.1.4,54321,22,T,I,D,B (Deny, Begin)

🧪 Các Bước Thực Hiện

1

Chuẩn Bị: Bật Network Watcher và Tạo NSG/VM

Cách 1 — Portal
  1. 1.1Portal → Network Watcher → Xác nhận region Southeast Asia có trạng thái Enabled. Nếu chưa: click Enable.
  2. 1.2Tạo RG rg-az500-flowlogs-lab22 nếu chưa có lab 19. Nếu đã có từ lab 19, dùng lại NSG đó.
  3. 1.3Nếu tạo mới: tạo VNet vnet-az500-lab22 (10.22.0.0/16), subnet snet-app, NSG nsg-az500-lab22, và VM Ubuntu để tạo traffic
Cách 2 — Azure CLI
Azure CLI— Bật Network Watcher và tạo hạ tầng lab
RG="rg-az500-flowlogs-lab22"

# Đăng ký provider (nếu chưa)
az provider register --namespace Microsoft.Network
az provider register --namespace Microsoft.Insights

# Bật Network Watcher tại southeastasia
az network watcher configure \
  --resource-group NetworkWatcherRG \
  --locations southeastasia \
  --enabled true

# Tạo resource group
az group create --name $RG --location southeastasia \
  --tags "owner=hoatranlab" "env=lab" "module=az500-ch05"

# VNet + subnet
az network vnet create \
  --resource-group $RG \
  --name vnet-az500-lab22 \
  --address-prefix 10.22.0.0/16 \
  --subnet-name snet-app \
  --subnet-prefix 10.22.1.0/24

# Tạo NSG với rule SSH allow từ any (để tạo traffic test)
az network nsg create \
  --resource-group $RG \
  --name nsg-az500-lab22 \
  --location southeastasia

az network nsg rule create \
  --resource-group $RG --nsg-name nsg-az500-lab22 \
  --name Allow-SSH --priority 100 \
  --direction Inbound --access Allow --protocol Tcp \
  --source-address-prefixes "*" --destination-port-ranges 22

az network vnet subnet update \
  --resource-group $RG --vnet-name vnet-az500-lab22 \
  --name snet-app --network-security-group nsg-az500-lab22

# Tạo VM để sinh traffic
az network public-ip create --resource-group $RG --name pip-vm-lab22 --sku Standard --allocation-method Static
az network nic create --resource-group $RG --name nic-vm-lab22 \
  --vnet-name vnet-az500-lab22 --subnet snet-app --public-ip-address pip-vm-lab22
az vm create --resource-group $RG --name vm-az500-lab22 \
  --nics nic-vm-lab22 --image Ubuntu2204 --size Standard_B1s \
  --admin-username azureadmin --generate-ssh-keys --no-wait
2

Tạo Storage Account (TLS 1.2) và Log Analytics Workspace

Cách 1 — Portal
  1. 2.1Portal → Storage accounts+ Create → RG: lab22, Name: staz500flowlogs[suffix], Region: Southeast Asia, Performance: Standard, Redundancy: LRS
  2. 2.2Tab AdvancedMinimum TLS version: chọn Version 1.2 (bắt buộc cho NSG Flow Logs 2026) → Review + Create
  3. 2.3Portal → search "Log Analytics workspaces"+ Create → RG: lab22, Name: law-az500-lab22, Region: Southeast Asia → Create
Cách 2 — Azure CLI
Azure CLI— Storage TLS1_2 + Log Analytics Workspace
RG="rg-az500-flowlogs-lab22"
# Suffix ngẫu nhiên để tên storage unique
SUFFIX=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 6 | head -n 1)
SA_NAME="staz500flowlogs${SUFFIX}"
echo "Storage name: $SA_NAME"

# Tạo Storage Account với minimum TLS 1.2 (bắt buộc 2026)
az storage account create \
  --resource-group $RG \
  --name $SA_NAME \
  --location southeastasia \
  --sku Standard_LRS \
  --kind StorageV2 \
  --min-tls-version TLS1_2 \
  --allow-blob-public-access false \
  --https-only true

# Tạo Log Analytics Workspace cho Traffic Analytics
az monitor log-analytics workspace create \
  --resource-group $RG \
  --workspace-name law-az500-lab22 \
  --location southeastasia \
  --sku PerGB2018 \
  --retention-time 30

# Lấy workspace ID và key
LAW_ID=$(az monitor log-analytics workspace show \
  --resource-group $RG --workspace-name law-az500-lab22 \
  --query customerId -o tsv)
echo "Log Analytics Workspace ID: $LAW_ID"
3

Bật NSG Flow Logs v2 và Traffic Analytics

Cách 1 — Portal
  1. 3.1Portal → Network WatcherNSG flow logs+ Create
  2. 3.2Target resource type: NSG → chọn nsg-az500-lab22
  3. 3.3Flow log version: Version 2 → Storage account: staz500flowlogs[suffix] → Retention: 7 days
  4. 3.4Traffic Analytics: Enable → Workspace: law-az500-lab22 → Interval: 10 minutes → Review + Create
  5. 3.5Xác nhận: NSG flow log xuất hiện trong danh sách với trạng thái Enabled
Cách 2 — Azure CLI
Azure CLI— Bật NSG Flow Logs v2 + Traffic Analytics
RG="rg-az500-flowlogs-lab22"
SA_NAME="staz500flowlogs[suffix]"   # thay bằng tên thực

# Lấy IDs cần thiết
NSG_ID=$(az network nsg show --resource-group $RG \
  --name nsg-az500-lab22 --query id -o tsv)
SA_ID=$(az storage account show --resource-group $RG \
  --name $SA_NAME --query id -o tsv)
LAW_ID=$(az monitor log-analytics workspace show \
  --resource-group $RG --workspace-name law-az500-lab22 \
  --query id -o tsv)

# Bật NSG Flow Logs version 2 với Traffic Analytics
az network watcher flow-log create \
  --resource-group NetworkWatcherRG \
  --name flowlog-nsg-lab22 \
  --nsg $NSG_ID \
  --storage-account $SA_ID \
  --enabled true \
  --retention 7 \
  --format JSON \
  --log-version 2 \
  --traffic-analytics true \
  --workspace $LAW_ID \
  --interval 10 \
  --location southeastasia

# Xác nhận flow log được bật
az network watcher flow-log show \
  --resource-group NetworkWatcherRG \
  --name flowlog-nsg-lab22 \
  --query "{Enabled:enabled,Version:format.version,RetentionDays:retentionPolicy.days,TrafficAnalytics:flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.enabled}" \
  --output table
4

Tạo Traffic Test và Phân Tích Log

Cách 1 — Portal
  1. 4.1Chờ 10–15 phút sau khi bật flow logs. Flow logs ghi vào Storage theo chu kỳ.
  2. 4.2SSH vào VM hoặc thử kết nối từ nhiều IP khác nhau để tạo traffic (Allow và Deny)
  3. 4.3Portal → Storage Account → Containersinsights-logs-networksecuritygroupflowevent → duyệt cấu trúc thư mục theo năm/tháng/ngày/giờ → download file JSON
  4. 4.4Network Watcher → Traffic Analytics → Xem dashboard: Allowed flows, Blocked flows, Top talkers, Geo map
  5. 4.5Portal → Log Analytics Workspaceslaw-az500-lab22Logs → chạy KQL query bên dưới
Cách 2 — Azure CLI + KQL
Azure CLI— Sinh traffic test và download flow log mẫu
RG="rg-az500-flowlogs-lab22"
SA_NAME="staz500flowlogs[suffix]"

# Lấy VM public IP
VM_PIP=$(az network public-ip show \
  --resource-group $RG --name pip-vm-lab22 --query ipAddress -o tsv)
echo "VM Public IP: $VM_PIP"

# Sinh traffic: SSH thử kết nối (tạo Allow flow)
ssh -o ConnectTimeout=5 azureadmin@$VM_PIP "echo 'traffic test ok'" 2>/dev/null || true

# Sinh denied traffic: thử cổng bị chặn
nc -z -w 3 $VM_PIP 8080 2>/dev/null || echo "Port 8080 denied (expected)"
nc -z -w 3 $VM_PIP 3389 2>/dev/null || echo "Port 3389 denied (expected)"

# Chờ 5-10 phút, sau đó liệt kê blobs trong container flow logs
az storage blob list \
  --account-name $SA_NAME \
  --container-name "insights-logs-networksecuritygroupflowevent" \
  --output table \
  --auth-mode login 2>/dev/null | head -20
KQL — Log Analytics— Phân tích Traffic Analytics data
// Query 1: Tất cả flows trong 1 giờ qua, phân loại Allow/Deny
AzureNetworkAnalytics_CL
| where TimeGenerated > ago(1h)
| where SubType_s == "FlowLog"
| project TimeGenerated, SrcIP_s, DestIP_s, DestPort_d, Protocol_s,
          FlowDirection_s, FlowStatus_s, NSGName_s
| order by TimeGenerated desc
| take 50

// Query 2: Đếm Denied flows theo source IP (phát hiện scan)
AzureNetworkAnalytics_CL
| where TimeGenerated > ago(24h)
| where SubType_s == "FlowLog" and FlowStatus_s == "D"
| summarize DeniedCount=count() by SrcIP_s, DestPort_d
| order by DeniedCount desc
| take 20

// Query 3: Top destination ports bị tấn công
AzureNetworkAnalytics_CL
| where TimeGenerated > ago(24h)
| where SubType_s == "FlowLog" and FlowStatus_s == "D"
| summarize Attempts=count() by DestPort_d, Protocol_s
| order by Attempts desc
Mẫu Flow Log JSON (v2)
{
  "records": [{
    "time": "2026-05-21T10:30:00Z",
    "systemId": "...",
    "macAddress": "...",
    "category": "NetworkSecurityGroupFlowEvent",
    "resourceId": "/subscriptions/.../nsg-az500-lab22",
    "operationName": "NetworkSecurityGroupFlowEvents",
    "properties": {
      "Version": 2,
      "flows": [{
        "rule": "Allow-SSH",
        "flows": [{
          "mac": "...",
          "flowTuples": [
            "1716288600,YOUR_IP,10.22.1.4,54321,22,T,I,A,B",
            "1716288601,1.2.3.4,10.22.1.4,44444,22,T,I,D,B"
          ]
        }]
      }]
    }
  }]
}
// Format tuple: timestamp,src,dst,srcport,dstport,proto,dir,decision,state
// dir: I=Inbound O=Outbound | decision: A=Allow D=Deny | state: B=Begin E=End C=Continue

📊 Kết Quả Đầu Ra

NSG Flow Logs bật thành công

Network Watcher → NSG flow logs → Status: Enabled, Version 2

Log JSON xuất hiện trong Storage

Container insights-logs-networksecuritygroupflowevent có file JSON với flow tuples

Traffic Analytics dashboard

Network Watcher → Traffic Analytics hiển thị allowed/blocked flows, geo map

KQL query trả về kết quả

AzureNetworkAnalytics_CL có dữ liệu flows với FlowStatus A/D

🧹 Dọn Dẹp Tài Nguyên

Lưu ý: Tắt NSG Flow Logs trước khi xóa Storage Account để tránh lỗi. Traffic Analytics và Log Analytics Workspace phát sinh chi phí theo ingestion data.
Azure CLI— Tắt flow logs trước, rồi xóa RG
# Tắt NSG Flow Log trước
az network watcher flow-log update \
  --resource-group NetworkWatcherRG \
  --name flowlog-nsg-lab22 \
  --enabled false \
  --location southeastasia

# Xóa resource group (xóa tất cả tài nguyên trong lab)
az group delete --name rg-az500-flowlogs-lab22 --yes --no-wait

# Xóa flow log definition
az network watcher flow-log delete \
  --resource-group NetworkWatcherRG \
  --name flowlog-nsg-lab22 \
  --location southeastasia

❓ Câu Hỏi Ôn Tập

1. NSG Flow Logs version 1 và version 2 khác nhau điểm gì quan trọng nhất?

Gợi ý: V2 thêm throughput information (bytes và packets) trong flow tuple, cho phép tính bandwidth và phát hiện data exfiltration tốt hơn.

2. Tại sao phải cấu hình Storage Account với minimum TLS 1.2 cho NSG Flow Logs?

Gợi ý: Yêu cầu bảo mật của Microsoft từ 2025 — TLS 1.0/1.1 bị deprecate. Flow logs service từ chối ghi vào storage account không đáp ứng yêu cầu TLS tối thiểu.

3. Traffic Analytics khác gì với chỉ đọc file JSON trong Storage? Khi nào nên bật Traffic Analytics?

Gợi ý: Traffic Analytics xử lý và aggregate data từ Storage vào Log Analytics, cung cấp dashboard, map, KQL query. Nên bật khi cần điều tra, phát hiện anomaly hoặc compliance reporting.

4. Flow log chứa thông tin gì? Ghi vào thư mục nào trong Storage Account?

Gợi ý: Tuple 5 thành phần + direction + decision + state + (v2) bytes/packets. Container: insights-logs-networksecuritygroupflowevent, path: subscriptionId/resourceGroups/.../year/month/day/hour/

5. Bạn thấy nhiều Denied flows từ nhiều IP khác nhau đến port 22 của VM. Đây là dấu hiệu của tấn công gì? Biện pháp khắc phục?

Gợi ý: SSH brute force / port scanning từ internet. Khắc phục: chặn port 22 public, dùng Azure Bastion, hoặc JIT VM Access để chỉ mở port khi cần và từ IP cụ thể.

Lab 21: UDR Route qua Firewall Thư viện Labs Lab 23: Azure Firewall Egress
Zalo