Computer >> Hướng Dẫn Máy Tính >  >> Kết Nối Mạng >> An Ninh Mạng

Azure Network Security Group (NSG) là gì? Chức năng, so sánh Firewall, ASG và hướng dẫn tạo

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ểmNSGAzure Firewall
Layer OSILayer 3 & 4 (IP, Port, Protocol)Layer 3, 4 & 7 (Application)
Loại FirewallBasic, Stateless-ish (Stateful cho return traffic), DistributedManaged, Stateful Fully, Centralized, L7 Inspection
L7 InspectionKHÔNGCÓ: FQDN filtering, SQL/HTTP inspection, TLS Inspection, IDPS (Signature-based), Web Categories
Threat IntelligenceKHÔNGCÓ (Microsoft Threat Intelligence feed, block known malicious IP/Domain)
SNAT/DNATKHÔNG (cần NAT Gateway/Public IP)CÓ (Built-in SNAT, DNAT, Forced Tunneling)
High AvailabilityBuilt-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 CaseMicro-segmentation VNet, Subnet/NIC level control, Dev/Test, Simple workloadsPerimeter 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

  1. Đăng nhập portal.azure.com.
  2. Search "Network Security Groups" → Create.
  3. Basics: Subscription, Resource Group, Name (vd: nsg-prod-web-subnet), Region (phải trùng region VNet/VM).
  4. Tags (tùy chọn) → Review + Create → Create.
  5. 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.
  6. 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.