Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบเครือข่าย >> ความปลอดภัยเครือข่าย

Azure Network Security Group (NSG) คืออะไร? คู่มือฉบับสมบูรณ์

Azure Network Security Group (NSG) คืออะไร?

Azure Network Security Group (NSG) เป็นบริการเครือข่ายของ Microsoft Azure ที่ทำหน้าที่เป็น Virtual Firewall (Layer 3-4) สำหรับควบคุมการไหลของ Network Traffic (Inbound/Outbound) ไปยัง Azure Resources เช่น Virtual Machines (VM), Virtual Machine Scale Sets, App Service Environment, AKS Nodes, และ Subnets ภายใน Virtual Network (VNet)

🎯 หน้าที่และความสามารถหลัก (Core Capabilities)

  • Stateful Packet Filtering: ตรวจสอบ 5-tuple (Source IP, Source Port, Destination IP, Destination Port, Protocol) และจำสถานะ Connection (Stateful) — ตอบกลับ Traffic ที่เป็น Established/Related ได้อัตโนมัติ
  • Rules Priority: กฎ (Rules) ประมวลผลตาม Priority (เลขน้อย = สำคัญกว่า) Range: 100–4096 (Default Rules อยู่ที่ 65000+)
  • Allow/Deny Actions: Allow (อนุญาต) หรือ Deny (บล็อก) — ไม่มี Action "Reject" (ส่ง RST/ICMP กลับ) เช่น On-prem Firewall
  • Association Scope: ผูกได้กับ Subnet (แนะนำ) หรือ Network Interface (NIC) ของ VM — ผูก Subnet จะครอบคลุมทุก NIC ใน Subnet นั้น
  • Default Rules: มี Default Rules ให้โดยอัตโนมัติ (Allow VNet Inbound, Allow Azure Load Balancer, Deny All Inbound, Allow Internet Outbound, Deny All Outbound)
  • Service Tags: ใช้ Service Tags (เช่น AzureCloud, AzureLoadBalancer, Storage, Sql, ApiManagement) แทน IP Range คงที่ — อัปเดตอัตโนมัติโดย Microsoft
  • Application Security Groups (ASG): จัดกลุ่ม VMs ตาม Application Role (Web, App, DB) ใช้อ้างอิงใน Rules แทน IP — จัดการง่ายขึ้น
  • Flow Logs: บันทึก Traffic Flow (Allowed/Denied) ไปยัง Storage Account, Log Analytics, หรือ Event Hub เพื่อ Analyze/Alert
  • Diagnostic Logs: Rule Evaluation Events, Counter Metrics

🆚 NSG vs Azure Firewall vs AWS Security Group

FeatureAzure NSGAzure FirewallAWS Security Group
LayerL3/L4 (Network/Transport)L3/L4/L7 (App Layer Inspection)L3/L4 (Stateful)
TypeDistributed (Per NIC/Subnet)Centralized (Hub-Spoke)Distributed (Per ENI)
FQDN Filtering❌ ไม่รองรับ✅ รองรับ (FQDN Tags, Custom FQDN)❌ (ต้องใช้ Network Firewall)
IDPS / IPS❌ ไม่มี✅ Built-in IDPS (Signature + Behavioral)❌ (ต้องใช้ Network Firewall + Suricata)
Threat Intelligence❌✅ Microsoft Threat Intel Feed❌ (GuardDuty แยก)
SNAT / DNAT❌✅ Full NAT Support✅ (NAT Gateway แยก)
High AvailabilityBuilt-in (Distributed)Built-in (Availability Zones)Built-in (Regional)
CostFree (จ่ายเฉพาะ Data Transfer)~$1.25/hr + Data ProcessingFree (จ่าย Data Transfer/NAT Gateway)
Use CaseMicro-segmentation, Subnet/NIC Level ControlCentralized Egress/Ingress, L7 Inspection, HybridEC2/ENI Level Control

⚙️ โครงสร้าง Rule (Rule Properties)

Propertyคำอธิบายตัวอย่าง
Nameชื่อ Rule (Unique ใน NSG)Allow-HTTP-Inbound
Priorityลำดับการประมวลผล (100-4096)100 (High), 4000 (Low)
Source / DestinationIP Address, CIDR, Service Tag, ASG, Any (*)Source: Internet (*), Dest: ASG:WebTier
ProtocolTCP, UDP, ICMP, ESP, AH, * (All)TCP
DirectionInbound / OutboundInbound
Port RangeSingle (80), Range (80-8080), * (All)443
ActionAllow / DenyAllow
Descriptionคำอธิบาย (Optional แต่แนะนำ)Allow HTTPS from Internet to Web Tier

🔧 Default Rules (สร้างอัตโนมัติ ไม่สามารถลบได้ แต่ override ได้ด้วย Priority ต่ำกว่า)

NamePriorityDirectionSource/DestProtocol/PortAction
AllowVnetInBound65000InboundVirtualNetwork*/ *Allow
AllowAzureLoadBalancerInBound65001InboundAzureLoadBalancer*/ *Allow
DenyAllInBound65500Inbound**/ *Deny
AllowVnetOutBound65000OutboundVirtualNetwork*/ *Allow
AllowInternetOutBound65001Outbound*InternetAllow
DenyAllOutBound65500Outbound**/ *Deny

หมายเหตุ: VirtualNetwork Service Tag รวม VNet Peered, On-prem via VPN/ExpressRoute, Service Endpoints

🏗️ Best Practices: การออกแบบและใช้งาน NSG

  1. Associate with Subnet (ไม่ใช่ NIC): จัดการง่ายกว่า Consistent ทุก VM ใน Subnet — ยกเว้นกรณีพิเศษที่ต้องการ Rule แบบ Per-VM
  2. Use Service Tags: แทน Hard-coded IP Ranges (เช่น Storage.Region, Sql.Management, AzureActiveDirectory)
  3. Leverage ASG (Application Security Groups): จัดกลุ่ม VMs ตาม Role (asg-web, asg-app, asg-db) → Rules อ้างอิง ASG แทน IP — Scale ง่ายเมื่อ Add/Remove VM
  4. Principle of Least Privilege: Deny by Default → Allow เฉพาะที่จำเป็น (Port, Protocol, Source/Dest)
  5. Reserve Priority Ranges: 100-999 (Critical/Management), 1000-1999 (App Traffic), 2000-2999 (Inter-tier), 3000-3999 (Monitoring/Backup), 4000-4096 (Catch-all/Deny)
  6. Enable Flow Logs: ส่งไป Log Analytics Workspace → KQL Queries, Workbooks, Alerts, Sentinel Integration
  7. Test with IP Flow Verify & Connection Troubleshoot: Azure Network Watcher Tools — Debug ทำไม Traffic ถูก Allow/Deny
  8. Automate with IaC: ARM/Bicep/Terraform/Pulumi — Version Control, Peer Review, CI/CD
  9. Document Rules: Description field + External Wiki/Runbook — ให้ทีมเข้าใจ Intent ของแต่ละ Rule
  10. Regular Review: Quarterly — Remove Unused Rules, Tighten Over-permissive Rules, Update Service Tags

🛠️ สร้าง NSG ผ่าน Azure Portal (Quick Steps)

  1. Azure Portal → Create a resource → ค้นหา Network Security Group → Create
  2. Basics: Subscription, Resource Group, Name, Region
  3. Review + Create → Create
  4. หลังสร้างเสร็จ: ไป Resource → Settings > Inbound security rules / Outbound security rules → Add
  5. กรอก Rule Properties → Add
  6. ไป Settings > Subnets → + Associate → เลือก VNet + Subnet → OK

📝 Terraform Example (NSG + Rules + Subnet Association)

resource "azurerm_network_security_group" "nsg_web" {
  name                = "nsg-web-prod"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  security_rule {
    name                       = "Allow-HTTP-Inbound"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "80"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
    description                = "Allow HTTP from Internet to Web Tier"
  }

  security_rule {
    name                       = "Allow-SSH-From-Bastion"
    priority                   = 200
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "10.0.10.0/24"  # Bastion Subnet
    destination_address_prefix = "*"
    description                = "Allow SSH from Bastion Host Subnet"
  }
}

resource "azurerm_subnet_network_security_group_association" "web_nsg_assoc" {
  subnet_id                 = azurerm_subnet.web.id
  network_security_group_id = azurerm_network_security_group.nsg_web.id
}

🔍 Troubleshooting & Debugging Tools

  • IP Flow Verify (Network Watcher): ทดสอบว่า Packet จาก Source IP/Port ไป Dest IP/Port จะถูก Allow/Deny โดย NSG ไหน — ให้ Rule Name และ Direction ที่ Match
  • Connection Troubleshoot (Network Watcher): Test Connectivity ระหว่าง VM ↔ VM, VM ↔ Internet, On-prem ↔ Azure — ให้ Detailed Hop-by-hop Analysis
  • NSG Flow Logs + Traffic Analytics: Visualize Traffic Patterns, Top Talkers, Malicious Traffic Detection, Geo-mapping
  • Effective Security Rules: ดู Rules ที่ Effective จริงบน NIC (รวม Default Rules, Subnet NSG, NIC NSG) — Portal > NIC > Effective security rules

❓ FAQ

Q: NSG ฟรีจริงไหม? ทำไมบิล Azure มีค่าใช้จ่าย Network?

A: NSG Resource เองฟรี (ไม่มีค่า Compute Hour) แต่ Data Transfer (Egress) ออก Internet, Cross-region, Cross-VNet Peering คิดค่าใช้จ่ายตามปกติ — NSG ไม่เพิ่มค่าใช้จ่าย Data Transfer

Q: NSG บล็อก Traffic ระหว่าง VM ใน Subnet เดียวกันได้ไหม?

A: ได้ — หาก NSG ผูกกับ Subnet Default Rule AllowVnetInBound จะ Allow ทุก Traffic ใน VNet (รวม Subnet เดียวกัน) — ต้องสร้าง Deny Rule Priority ต่ำกว่า 65000 เพื่อบล็อก (เช่น Priority 4000 Deny Inbound from VirtualNetwork to ASG:DB on Port 1433)

Q: ASG vs NSG ต่างกันยังไง?

A: NSG = Resource ที่เก็บ Rules (Firewall Policy) — ASG = Logical Grouping ของ VMs (Tag/Label) ใช้อ้างอิงใน NSG Rules แทน IP — 1 NSG อ้างอิง ASG หลายตัวได้, 1 VM อยู่ใน ASG หลายตัวได้

Q: NSG รองรับ Stateful Inspection จริงไหม?

A: ใช่ NSG เป็น Stateful — หาก Inbound Rule Allow TCP 443, Response Traffic (Outbound from Server Ephemeral Port) จะ Allow อัตโนมัติ แม้ Outbound Rule จะ Deny All — ยกเว้น UDP/ICMP ที่บางกรณีอาจต้องการ Outbound Rule เปิดให้ (ขึ้นกับ Implementation)

Q: ควรใช้ NSG หรือ Azure Firewall?

A: ทั้งคู่รวมกัน (Defense in Depth): NSG = Micro-segmentation (Subnet/NIC Level, L3/L4) + Azure Firewall = Centralized Egress/Ingress Inspection (L3/L4/L7, IDPS, Threat Intel, FQDN Filtering, NAT) — NSG ฟรี Azure Firewall จ่ายเงิน

สรุป: Azure NSG เป็น Free, Distributed, L3/L4 Stateful Firewall สำหรับ Micro-segmentation ใน VNet ใช้ร่วมกับ Service Tags และ ASG เพื่อจัดการ Rule ให้ Scale ได้ ควรใช้คู่กับ Azure Firewall สำหรับ L7/Threat Protection และ Centralized Control