Azure Network Security Group (NSG) คืออะไร?
Azure Network Security Group (NSG) เป็นบริการ Network Filtering (Layer 3 & 4) ใน Azure ที่ใช้ควบคุม Inbound และ Outbound Traffic ไป/มาจาก Azure Resources (VM, VMSS, App Service Environment, AKS, Azure SQL, ฯลฯ) ผ่าน Security Rules
NSG ทำงานอย่างไร?
โครงสร้าง Security Rule
| Property | คำอธิบาย | ตัวอย่าง |
| Name | ชื่อกฎ (Unique ใน NSG) | Allow-HTTP-Inbound |
| Priority | ลำดับประมวลผล (100-4096) เลขน้อย = ประมวลผลก่อน | 100, 200, 300 |
| Source / Destination | IP Address, CIDR, Service Tag, Application Security Group (ASG) | Source: Internet, Destination: 10.0.1.0/24 |
| Protocol | TCP, UDP, ICMP, ESP, AH, * (All) | TCP |
| Direction | Inbound (เข้ามา) / Outbound (ออกไป) | Inbound |
| Port Range | Port หรือ Range (80, 443, 1000-2000, *) | 443 |
| Action | Allow / Deny | Allow |
การประมวลผล Rules (Rule Processing)
- เรียงตาม Priority (Ascending) 100 → 4096
- จับคู่ 5-tuple: Source IP, Source Port, Dest IP, Dest Port, Protocol
- First Match Wins - หาตรงกฎแรก หยุดประมวลผล (Allow หรือ Deny)
- Default Rules (Implicit): Priority 65000+ (ไม่เห็น/ลบไม่ได้)
- AllowVNetInBound: Allow ทุกอย่างภายใน VNet
- AllowAzureLoadBalancerInBound: Allow Azure LB Health Probes
- DenyAllInBound: Deny ทุกอย่างจากภายนอก VNet
- AllowVNetOutBound: Allow ออกไปภายใน VNet
- AllowInternetOutBound: Allow ออก Internet
- DenyAllOutBound: Deny ออกไปที่ไม่ใช่ VNet/Internet
NSG Association (การเชื่อมโยง)
| Scope | รายละเอียด |
| Subnet (แนะนำ) | 1 NSG ต่อ 1 Subnet ใช้ร่วมได้หลาย Subnet ใช้กับทุก NIC ใน Subnet นั้น |
| Network Interface (NIC) | 1 NSG ต่อ 1 NIC เฉพาะเจาะจง VM นั้นๆ (Override Subnet NSG) |
| ไม่รองรับ | Resource Group, VNet Level, Subscription Level |
NSG vs Azure Firewall - ความแตกต่างสำคัญ
| Feature | NSG (Network Security Group) | Azure Firewall |
| OSI Layer | Layer 3 (Network) + Layer 4 (Transport) | Layer 3, 4, 7 (Application) |
| Inspection | Stateless/Stateful (5-tuple) | Stateful L7 Deep Packet Inspection |
| FQDN Filtering | ❌ ไม่ได้ (ต้องใช้ Service Tag) | ✅ FQDN Tags, Custom FQDN, SQL/Storage FQDN |
| IDPS (Intrusion Detection/Prevention) | ❌ ไม่มี | ✅ Signature-based IDPS (Managed Rules) |
| Threat Intelligence | ❌ | ✅ Microsoft Threat Intelligence Feed |
| SNAT/DNAT | ❌ | ✅ Source/Destination NAT |
| Logging | Flow Logs (NSG Flow Logs → Storage/Log Analytics) | Structured Logs (App/Network/DNS/Threat) → Log Analytics/Event Hub/Storage |
| High Availability | Regional (Implicit) | Zone Redundant (AZ), Forced Tunneling |
| Cost | Free (จ่ายแค่ Flow Logs Storage) | ~$1.25/hr + Data Processing (~$0.016/GB) |
| Deployment | Instant, No Provisioning | ~5-10 นาที (Firewall Policy + Firewall) |
| Use Case | Micro-segmentation, Subnet/NIC Level Control, Basic Filtering | Perimeter Security, Hub-spoke, L7 Inspection, Threat Protection, Centralized Logging |
Application Security Groups (ASG) - เสริม NSG
- Logical Grouping ของ VMs/NICs ตาม Application Role (Web, App, DB, Mgmt)
- ใช้เป็น Source/Destination ใน NSG Rules แทน IP Address
- ตัวอย่าง: Source: ASG-Web, Dest: ASG-App, Port: 8080, Allow
- Scale ได้ดีกว่า IP-based Rules
Service Tags - จัดการ IP Ranges อัตโนมัติ
- Microsoft จัดการ IP Ranges ของ Azure Services ให้: AzureCloud, AzureLoadBalancer, VirtualNetwork, Internet, Storage, Sql, KeyVault, EventHub, ServiceBus, ApiManagement, AppService, AzureActiveDirectory, AzureMonitor, ...
- ใช้เป็น Source/Destination ใน NSG Rules แทน CIDR Blocks
- Region-specific:
AzureCloud.EastUS, Storage.WestEurope
NSG Flow Logs & Monitoring
- Enable: NSG > Settings > Flow logs > On (Log Analytics / Storage Account)
- Version 2: ระบุ Throughput (Bytes/Packets), Rule Name, Direction
- Traffic Analytics: Microsoft-managed Analytics บน Flow Logs (Geo-map, Top Talkers, Malicious IP, Port Scanning)
- Integrate: Microsoft Sentinel, Defender for Cloud, Third-party SIEM
Best Practices
- Attach NSG to Subnet (ไม่ใช่ NIC) เพื่อ Consistency และ Manageability
- Use ASGs สำหรับ Application-tier Rules
- Use Service Tags แทน Hard-coded IPs
- Deny by Default: Explicit Allow Rules เฉพาะที่จำเป็น Priority 100-1000
- Reserve Priority Gaps: 100, 200, 300... เพื่อ Insert Rules ระหว่างได้
- Enable Flow Logs + Traffic Analytics สำหรับ Visibility
- Test with Network Watcher: IP Flow Verify, Next Hop, Connection Troubleshoot, Packet Capture
- Combine with Azure Firewall: NSG (Micro-seg) + Azure Firewall (Perimeter/L7) = Defense in Depth
สรุป
NSG = Basic Network Filtering (L3/L4) ฟรี ใช้ทำ Micro-segmentation ระดับ Subnet/NIC
Azure Firewall = Full-featured L3/L4/L7 Firewall แบบ Managed จ่ายเงิน ใช้ทำ Perimeter/Hub Security
Best Practice: ใช้ ทั้งคู่ร่วมกัน (NSG ใกล้ Workload + Azure Firewall ที่ Hub) + ASG + Service Tags + Flow Logs + Traffic Analytics