Azure Network Security Group (NSG) & Application Security Group (ASG) - Hướng dẫn cấu hình
NSG là tường lửa phân tán (distributed firewall) cấp độ L3/L4 của Azure, cho phép kiểm soát traffic inbound/outbound đến tài nguyên Azure (VM, Subnet, NIC). ASG cho phép gom nhóm VM theo ứng dụng để áp dụng policy NSG linh hoạt hơn.
Khái niệm cốt lõi
| Thành phần | Mô tả | Phạm vi áp dụng |
|---|---|---|
| NSG (Network Security Group) | Chứa danh sách quy tắc bảo mật (Security Rules) Allow/Deny traffic dựa trên: Source/Dest IP, Port, Protocol (TCP/UDP/ICMP), Priority, Direction | Subnet hoặc Network Interface (NIC) |
| ASG (Application Security Group) | Logical grouping của VMs theo vai trò ứng dụng (Web, App, DB, Mgmt). Dùng làm Source/Destination trong NSG rules thay vì IP CIDR | VM (NIC level), trong cùng Region & Subscription |
| Azure Firewall | Managed firewall service L3/L4/L7 (FQDN, TLS Inspection, IDPS, Threat Intel). Khác NSG: NSG = basic L3/L4, Azure FW = full L7, centralized | VNet level (Hub-spoke), nhiều VNet |
Tạo NSG trên Azure Portal
- Azure Portal > Create a resource > Tìm Network Security Group > Create
- Subscription, Resource Group, Name (vd: nsg-prod-web), Region
- Review + Create
- Sau khi tạo: Vào NSG > Inbound/Outbound security rules > Add
Quy tắc NSG quan trọng (Priority: số nhỏ = ưu tiên cao)
# Ví dụ quy tắc Inbound cho Web Tier (ASG: asg-web) Priority: 100 Name: Allow-HTTP-HTTPS Source: Any / Internet / Service Tag (Internet) Source Port: * Destination: asg-web (Application Security Group) Destination Port: 80,443 Protocol: TCP Action: Allow Priority: 110 Name: Allow-SSH-Admin Source: Admin-IP-Range (vd: 203.0.113.0/24) hoặc asg-mgmt Source Port: * Destination: asg-web Destination Port: 22 Protocol: TCP Action: Allow Priority: 4096 (Default) Name: DenyAllInBound Source: Any Destination: Any Action: Deny (Implicit default rule)
Tạo & Gán ASG cho VM
- Portal > Create a resource > Application Security Group
- Name: asg-web, Resource Group, Region (phải trùng Region VM)
- Vào VM > Networking > Application security groups > Configure > Chọn asg-web > Save
- Lặp lại cho: asg-app, asg-db, asg-mgmt
Áp dụng NSG cho Subnet hoặc NIC
- Subnet level (Khuyến nghị cho production): VNet > Subnets > Chọn Subnet > Network Security Group > Chọn NSG > Save. Áp dụng cho TẤT CẢ NIC trong subnet.
- NIC level: VM > Networking > Network Interface > Network Security Group > Chọn NSG > Save. Chỉ áp dụng cho NIC đó.
Cấu hình NSG cho kiến trúc 3-tier (Web-App-DB)
| Tier | ASG | Inbound Rules (Priority) | Outbound Rules |
|---|---|---|---|
| Web | asg-web | 100: Allow TCP 80,443 from Internet 110: Allow TCP 22 from asg-mgmt 120: Allow TCP 443 from AzureLoadBalancer (Health Probe) | Allow TCP 443 to asg-app (API) Allow TCP 53 to AzureDNS Deny All Else |
| App | asg-app | 100: Allow TCP 443/8080 from asg-web 110: Allow TCP 22 from asg-mgmt | Allow TCP 1433/3306 to asg-db Allow TCP 443 to Azure Key Vault/Storage Deny All Else |
| DB | asg-db | 100: Allow TCP 1433/3306 from asg-app 110: Allow TCP 22 from asg-mgmt | Allow TCP 443 to Azure Backup/Monitor Deny All Else |
| Mgmt | asg-mgmt | 100: Allow TCP 22/3389 from Corp IP / Bastion / Azure Bastion | Allow to all tiers for management |
Service Tags - Thay thế IP CIDR cứng nhắc
- Internet - Traffic từ internet công cộng
- VirtualNetwork - Traffic trong VNet (kể cả peered)
- AzureLoadBalancer - Health probe từ Azure LB
- AzureCloud - Traffic từ/dến Azure services (region specific)
- Storage / Sql / KeyVault / EventHub / ApiManagement - Service endpoints
Debug & Troubleshooting NSG
- Effective Security Rules: VM > Networking > Effective security rules (Xem quy tắc áp dụng thực tế cho NIC)
- NSG Flow Logs: Bật Flow Logs > Log Analytics Workspace / Storage Account > Query KQL:
AzureNetworkAnalytics_CL | where FlowDirection_s == "Inbound" and Decision_s == "Deny" - IP Flow Verify: Network Watcher > IP flow verify > Test source/dest IP, port, protocol
- Connection Troubleshoot: Network Watcher > Connection troubleshoot > Test VM-to-VM, VM-to-Internet
Best Practices
- Dùng ASG thay vì IP CIDR trong NSG rules (dễ quản lý, scale)
- NSG ở Subnet level cho baseline, NIC level cho exception
- Default Deny All (Implicit) - Chỉ Allow cái cần thiết (Zero Trust)
- Priority spacing: Cách nhau 100 (100, 200, 300...) để chèn rule mới
- Naming convention:
Allow-HTTP-asg-web-from-Internet,Deny-RDP-from-Internet - Bật Flow Logs + Log Analytics cho audit, compliance, threat hunting
- Test rule mới trên Non-prod trước, dùng "Disable rule" thay vì xóa
- Kết hợp Azure Firewall (L7) + NSG (L3/L4) cho Defense in Depth