Azure Network Security Group (NSG) คืออะไร?
Network Security Group (NSG) เป็นบริการของ Azure ที่ทำหน้าที่เป็น Virtual Firewall แบบ Distributed (L3-L4) สำหรับควบคุม Traffic เข้า-ออก (Inbound/Outbound) ของ Azure Resources ภายใน Virtual Network (VNet) ทำงานที่ OSI Layer 3 (Network/IP) และ Layer 4 (Transport/Port)
คุณสมบัติหลักของ NSG
- 📦 Stateless Packet Filtering: ตรวจสอบ 5-tuple (Source IP, Source Port, Destination IP, Destination Port, Protocol) ตาม Priority Rules
- 🔗 Association Points (จุดผูก):
- Subnet (แนะนำ): 1 NSG ต่อ 1 Subnet → ใช้ร่วมได้หลาย NIC/VM ใน Subnet เดียว (Centralized Management)
- Network Interface (NIC): 1 NSG ต่อ 1 NIC → Granular Control ต่อ VM (แต่มานูมากขึ้น)
- 📋 Rules (กฎ): มี Priority (100-4099, ยิ่งน้อยยิ่งประมวลผลก่อน), Name, Port, Protocol, Source/Destination (IP/CIDR/Service Tag/Application Security Group), Action (Allow/Deny)
- 🏷 Service Tags: กลุ่ม IP Range ของ Azure Services โดยอัตโนมัติ (เช่น
Internet, VirtualNetwork, AzureLoadBalancer, Storage, Sql, ApiManagement) - อัปเดตโดย Microsoft เอง
- 👥 Application Security Groups (ASG): จับกลุ่ม VMs ตาม Application Role (เช่น Web, App, DB) ใช้ใน Source/Destination ของ Rule แทน IP → จัดการง่ายขึ้นเมื่อ Scale
- 📊 Flow Logs: บันทึก Traffic Flows (Allowed/Denied) ไป Storage Account / Log Analytics / Event Hubs สำหรับ Analysis, Audit, SIEM
- 🔄 Default Rules: มี Default Rules ไว้แล้ว (Allow VNet Inbound, Allow Azure Load Balancer, Deny All Inbound, Allow VNet Outbound, Allow Internet Outbound, Deny All Outbound) - ไม่ลบได้ แต่อーバーラิดได้ด้วย Priority ต่ำกว่า
NSG vs Azure Firewall: ต่างกันอย่างไร?
| Feature | Network Security Group (NSG) | Azure Firewall |
| OSI Layer | Layer 3 (IP) + Layer 4 (Port/Protocol) | Layer 3, 4, 7 (Application) |
| Type | Distributed Virtual Firewall (บน Host/VFabric) | Managed Stateful Firewall Service (Centralized Appliance) |
| Inspection | Stateless (5-tuple) *Note: Azure มี Stateful ด้วยสำหรับ Allow Rules* | Stateful (Full Session Awareness) |
| Application Layer (L7) | ❌ ไม่มี | ✅ Application Rules (FQDN, HTTP/HTTPS/SQL), TLS Inspection, IDPS Signatures, Categories (URL Filtering) |
| Threat Intelligence | ❌ ไม่มี Built-in | ✅ Built-in Threat Intelligence (Microsoft/Third-party) - Alert/Deny Malicious IPs/Domains |
| Scalability | Auto-scale (Distributed) | Auto-scale (Firewall Policy), ต้อง Plan Capacity (Firewall Manager) |
| Management | Per NSG (หรือ Azure Firewall Manager สำหรับ Policy) | Azure Firewall Manager (Central Policy, Hierarchical) |
| Cost | 🆓 ฟรี (จ่ายเฉพาะ Data Transfer ปกติ) | 💰 มีค่าใช้จ่าย (Hourly ~$1.25/hr + Data Processing ~$0.016/GB) |
| Deployment | Simple, Instant | ต้องสร้าง AzureFirewallSubnet (/26 min), Public IP, Policy |
| Use Case | Micro-segmentation, Subnet-level Control, Basic Filtering, Dev/Test | Perimeter Security (Hub), Internet Egress/Ingress, L7 Inspection, Threat Intel, Hybrid (ExpressRoute/VPN) |
NSG vs ASG (Application Security Group) - ความสัมพันธ์
- NSG = Container ของ Rules (Policy Container)
- ASG = Logical Grouping ของ NICs/VMs (Object Reference ภายใน NSG Rules)
- ความสัมพันธ์: ASG อยู่ ภายใน NSG Rule (เป็น Source/Destination) | 1 NIC สามารถอยู่ในหลาย ASG ได้ | ASG ไม่มี Rule เอง
- ตัวอย่าง Rule ใช้ ASG: Source:
ASG:Web → Destination: ASG:App Port: 443 Action: Allow (แทนที่จะระบุ IP/CIDR)
NSG Rules: Priority และ Processing Logic
- Priority: 100 - 4099 (ยิ่งน้อย = ประมวลผลก่อน = Priority สูงกว่า)
- Default Rules มี Priority สูงมาก (65000+) → User Rules จะ Override ได้เสมอ
- Processing: First Match Wins (หาตรง Rule แรก → Action Allow/Deny → หยุด)
- Stateful for Allowed Traffic: ถ้า Allow Outbound → Return Traffic Inbound จะ Allow อัตโนมัติ (ไม่ต้องเขียน Rule กลับ) *เฉพาะ Traffic ที่ Match Allow Rule
Best Practices สำหรับ NSG Design
- 🎯 Attach at Subnet Level (Primary) → 1 NSG/Subnet → จัดการง่าย, Consistent, Scalable
- 🏷 ใช้ Service Tags และ ASG → ลด Hard-coded IP/CIDR, รองรับ Scale และ Azure Service IP Changes
- 📝 Naming Convention:
nsg--- (เช่น nsg-prod-sea-web), Rule: allow----
- 🔒 Default Deny (Implicit) → เฉพาะ Allow ที่จำเป็น (Least Privilege)
- 📊 Enable Flow Logs → ส่งไป Log Analytics Workspace → KQL Queries, Workbooks, SIEM Integration (Sentinel, Splunk)
- 🧪 Test ก่อน Deploy Production: Use
Get-AzNetworkWatcherFlowLogStatus, Test-AzNetworkWatcherConnectivity, Network Watcher IP Flow Verify
- 🔄 Infrastructure as Code: Deploy NSG ผ่าน Terraform/Bicep/ARM → Version Control, PR Review, Automated Testing
- 🚫 หลีกเลี่ยง NIC-level NSG ยกเว้นกรณีพิเศษ (Specific VM Hardening) → Management Overhead สูงมาก
ตัวอย่าง NSG Rules สำหรับ 3-Tier Architecture (Web/App/DB)
# Subnet: snet-web (ASG: asg-web) - NSG: nsg-prod-web
Priority 100: Allow Inbound Internet → asg-web TCP 443 (HTTPS) "Allow HTTPS from Internet"
Priority 110: Allow Inbound Internet → asg-web TCP 80 (HTTP) "Allow HTTP from Internet (Redirect)"
Priority 120: Deny Inbound Internet → asg-web * * "Deny All Other from Internet" (Implicit anyway)
# Subnet: snet-app (ASG: asg-app) - NSG: nsg-prod-app
Priority 100: Allow Inbound asg-web → asg-app TCP 443/8080 "Allow Web to App"
Priority 110: Allow Inbound AzureLoadBalancer → asg-app TCP * "Allow Health Probes"
Priority 120: Deny Inbound VirtualNetwork → asg-app * * "Deny Other VNet Traffic"
# Subnet: snet-db (ASG: asg-db) - NSG: nsg-prod-db
Priority 100: Allow Inbound asg-app → asg-db TCP 1433/3306/5432 "Allow App to DB"
Priority 110: Deny Inbound VirtualNetwork → asg-db * * "Deny All Other"
# Outbound: App → Internet (for patches/API) via NAT Gateway/Firewall, DB → No Internet
เครื่องมือจัดการและ Troubleshooting
- Azure Portal / Azure CLI / PowerShell / Terraform / Bicep / ARM Template
- Network Watcher: IP Flow Verify (Test Packet Allow/Deny), Next Hop, Packet Capture, Connection Troubleshoot, Flow Logs
- Effective Security Rules: ดู Rules ที่ Apply จริงถึง NIC (รวม Default Rules) → Diagnose ทำไม Block/Allow
- NSG Flow Logs Analytics: KQL Queries ใน Log Analytics สำหรับ Traffic Analysis, Anomaly Detection