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
| Feature | Azure NSG | Azure Firewall | AWS Security Group |
|---|---|---|---|
| Layer | L3/L4 (Network/Transport) | L3/L4/L7 (App Layer Inspection) | L3/L4 (Stateful) |
| Type | Distributed (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 Availability | Built-in (Distributed) | Built-in (Availability Zones) | Built-in (Regional) |
| Cost | Free (จ่ายเฉพาะ Data Transfer) | ~$1.25/hr + Data Processing | Free (จ่าย Data Transfer/NAT Gateway) |
| Use Case | Micro-segmentation, Subnet/NIC Level Control | Centralized Egress/Ingress, L7 Inspection, Hybrid | EC2/ENI Level Control |
⚙️ โครงสร้าง Rule (Rule Properties)
| Property | คำอธิบาย | ตัวอย่าง |
|---|---|---|
| Name | ชื่อ Rule (Unique ใน NSG) | Allow-HTTP-Inbound |
| Priority | ลำดับการประมวลผล (100-4096) | 100 (High), 4000 (Low) |
| Source / Destination | IP Address, CIDR, Service Tag, ASG, Any (*) | Source: Internet (*), Dest: ASG:WebTier |
| Protocol | TCP, UDP, ICMP, ESP, AH, * (All) | TCP |
| Direction | Inbound / Outbound | Inbound |
| Port Range | Single (80), Range (80-8080), * (All) | 443 |
| Action | Allow / Deny | Allow |
| Description | คำอธิบาย (Optional แต่แนะนำ) | Allow HTTPS from Internet to Web Tier |
🔧 Default Rules (สร้างอัตโนมัติ ไม่สามารถลบได้ แต่ override ได้ด้วย Priority ต่ำกว่า)
| Name | Priority | Direction | Source/Dest | Protocol/Port | Action |
|---|---|---|---|---|---|
| AllowVnetInBound | 65000 | Inbound | VirtualNetwork | */ * | Allow |
| AllowAzureLoadBalancerInBound | 65001 | Inbound | AzureLoadBalancer | */ * | Allow |
| DenyAllInBound | 65500 | Inbound | * | */ * | Deny |
| AllowVnetOutBound | 65000 | Outbound | VirtualNetwork | */ * | Allow |
| AllowInternetOutBound | 65001 | Outbound | * | Internet | Allow |
| DenyAllOutBound | 65500 | Outbound | * | */ * | Deny |
หมายเหตุ: VirtualNetwork Service Tag รวม VNet Peered, On-prem via VPN/ExpressRoute, Service Endpoints
🏗️ Best Practices: การออกแบบและใช้งาน NSG
- Associate with Subnet (ไม่ใช่ NIC): จัดการง่ายกว่า Consistent ทุก VM ใน Subnet — ยกเว้นกรณีพิเศษที่ต้องการ Rule แบบ Per-VM
- Use Service Tags: แทน Hard-coded IP Ranges (เช่น
Storage.Region,Sql.Management,AzureActiveDirectory) - Leverage ASG (Application Security Groups): จัดกลุ่ม VMs ตาม Role (asg-web, asg-app, asg-db) → Rules อ้างอิง ASG แทน IP — Scale ง่ายเมื่อ Add/Remove VM
- Principle of Least Privilege: Deny by Default → Allow เฉพาะที่จำเป็น (Port, Protocol, Source/Dest)
- Reserve Priority Ranges: 100-999 (Critical/Management), 1000-1999 (App Traffic), 2000-2999 (Inter-tier), 3000-3999 (Monitoring/Backup), 4000-4096 (Catch-all/Deny)
- Enable Flow Logs: ส่งไป Log Analytics Workspace → KQL Queries, Workbooks, Alerts, Sentinel Integration
- Test with IP Flow Verify & Connection Troubleshoot: Azure Network Watcher Tools — Debug ทำไม Traffic ถูก Allow/Deny
- Automate with IaC: ARM/Bicep/Terraform/Pulumi — Version Control, Peer Review, CI/CD
- Document Rules: Description field + External Wiki/Runbook — ให้ทีมเข้าใจ Intent ของแต่ละ Rule
- Regular Review: Quarterly — Remove Unused Rules, Tighten Over-permissive Rules, Update Service Tags
🛠️ สร้าง NSG ผ่าน Azure Portal (Quick Steps)
- Azure Portal → Create a resource → ค้นหา Network Security Group → Create
- Basics: Subscription, Resource Group, Name, Region
- Review + Create → Create
- หลังสร้างเสร็จ: ไป Resource → Settings > Inbound security rules / Outbound security rules → Add
- กรอก Rule Properties → Add
- ไป 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