Azure Network Security Group (NSG) - Tổng quan
Network Security Group (NSG) là tài nguyên Azure cho phép lọc traffic mạng (inbound/outbound) đến/từ các tài nguyên Azure (VM, Subnet, NIC, Scale Set, App Service Environment...) dựa trên quy tắc 5-tuple (Source/Destination IP, Source/Destination Port, Protocol). NSG hoạt động ở Layer 3 (Network) và Layer 4 (Transport) của mô hình OSI.
Chức năng chính
- Stateful Filtering: Tự động cho phép traffic trả lời (return traffic) của kết nối đã được allow (không cần rule outbound riêng cho response).
- Priority-based Rules: Quy tắc ưu tiên thấp hơn (số nhỏ hơn, 100-4096) được đánh giá trước. Default rules (65000+) luôn ở cuối.
- Application to Subnet hoặc NIC: Gán cho Subnet (áp dụng cho tất cả resource trong subnet) HOẶC NIC (chỉ VM đó). Best practice: Gán cho Subnet.
- Default Rules: Allow VNet Inbound, Allow Azure Load Balancer Inbound, Deny All Inbound / Allow VNet Outbound, Allow Internet Outbound, Deny All Outbound.
- Service Tags: Định nghĩa source/destination bằng tag logic của Azure (VirtualNetwork, AzureLoadBalancer, Internet, Storage, Sql, ApiManagement...) thay vì IP cụ thể → tự động cập nhật dải IP Azure.
- Application Security Groups (ASG): Gom nhóm VM theo logic ứng dụng (ví dụ: asg-web, asg-db) để viết rule dựa trên ASG thay vì IP → dễ quản lý khi scale.
- Flow Logs & Diagnostics: Ghi log allow/deny flows ra Storage Account, Log Analytics, Event Hub để audit, troubleshooting, SIEM.
NSG vs Azure Firewall: Khác biệt cốt lõi
| Đặc điểm | NSG | Azure Firewall |
|---|---|---|
| Layer OSI | Layer 3 & 4 (IP, Port, Protocol) | Layer 3, 4 & 7 (Application) |
| Loại Firewall | Basic, Stateless-ish (Stateful cho return traffic), Distributed | Managed, Stateful Fully, Centralized, L7 Inspection |
| L7 Inspection | KHÔNG | CÓ: FQDN filtering, SQL/HTTP inspection, TLS Inspection, IDPS (Signature-based), Web Categories |
| Threat Intelligence | KHÔNG | CÓ (Microsoft Threat Intelligence feed, block known malicious IP/Domain) |
| SNAT/DNAT | KHÔNG (cần NAT Gateway/Public IP) | CÓ (Built-in SNAT, DNAT, Forced Tunneling) |
| High Availability | Built-in (Distributed) | Built-in (Availability Zones, 99.99% SLA) |
| Chi phí | MIỄN PHÍ (chỉ trả tiền rule custom > 200 rules/NSG, flow logs) | CÓ CHI PHÍ: Hourly (~$1.25/hr) + Data processed (~$0.016/GB). Đắt hơn đáng kể. |
| Use Case | Micro-segmentation VNet, Subnet/NIC level control, Dev/Test, Simple workloads | Perimeter security (Hub VNet), Egress/Ingress inspection, L7 policy, Compliance, Hybrid/On-prem connectivity |
NSG vs ASG (Application Security Group)
- NSG: Là tài nguyên (resource) chứa tập hợp quy tắc bảo mật (Security Rules). Enforce traffic.
- ASG: Là đối tượng logic (object) BÊN TRONG NSG. Gom nhóm NIC/VM theo vai trò ứng dụng (Web, App, DB). Dùng làm Source/Destination trong rule NSG.
- Mối quan hệ: NSG chứa rules → Rules tham chiếu ASG làm Source/Destination. ASG không tự enforce traffic.
Cách tạo NSG trong Azure Portal
- Đăng nhập portal.azure.com.
- Search "Network Security Groups" → Create.
- Basics: Subscription, Resource Group, Name (vd: nsg-prod-web-subnet), Region (phải trùng region VNet/VM).
- Tags (tùy chọn) → Review + Create → Create.
- Sau khi tạo: Vào NSG → Inbound security rules / Outbound security rules → Add rule:
Source/Destination: Any / IP Addresses / Service Tag / Application Security Group.
Source/Destination Port Ranges: 80, 443, 22, 3389, 1433, *...
Protocol: TCP, UDP, Any, ICMP.
Action: Allow / Deny.
Priority: 100, 110, 120... (số nhỏ = ưu tiên cao).
Name: Mô tả rõ ràng (Allow-HTTP-Inbound, Deny-RDP-From-Internet).
Description: Lý do business/technical. - Associate NSG: Vào NSG → Subnets / Network interfaces → + Associate → chọn VNet → chọn Subnet (hoặc NIC).
Best Practices
- Một NSG cho một Subnet (không gán NSG cho từng NIC trừ trường hợp exception).
- Dùng Service Tags thay vì hardcode IP Azure services.
- Dùng ASG cho workload động (VMSS, AKS nodes, scaling apps).
- Naming convention:
nsg-<env>-<role>-<region>(vd: nsg-prod-web-weu). - Default Deny All Inbound/Outbound ở cuối → Chỉ allow cần thiết (Least Privilege).
- Enable Flow Logs gửi Log Analytics → Kusto query, Alert, Workbook.
- Test rule: IP Flow Verify (Network Watcher) → kiểm tra packet allow/deny bởi rule nào.
- Không quản lý NSG thủ công ở quy mô lớn → Dùng Infrastructure as Code (Bicep, Terraform, ARM) + Policy (Azure Policy) enforce tagging, naming, required rules.
Khi nào dùng NSG, khi nào dùng Azure Firewall?
- Dùng NSG: Mọi workload trong VNet. Micro-segmentation. Cost-sensitive. Layer 3/4 đủ.
- Dùng Azure Firewall: Hub-Spoke topology (Centralized egress/ingress). Cần L7 inspection (FQDN, SQL, TLS Inspect). Threat Intelligence. Compliance (PCI-DSS, etc). Hybrid VPN/ExpressRoute forced tunneling. High throughput centralized.
- Kết hợp: Hub VNet: Azure Firewall (Perimeter) → Spoke VNets: NSG + ASG (Micro-segmentation). Traffic spoke-to-spoke qua Firewall hoặc VNet Peering + NSG.