Azure Network Security Group (NSG) คืออะไร?
Network Security Group (NSG) เป็น Virtual Firewall ระดับ Network Layer (Layer 3/4) ใน Azure ใช้ควบคุม Inbound และ Outbound Traffic ไปยัง Azure Resources (VM, Subnet, NIC) ผ่าน Access Control Lists (ACLs)
NSG Rules: โครงสร้างและ Priority
แต่ละ Rule ประกอบด้วย:
- Name: ชื่อกฎ (Unique ใน NSG)
- Priority: หมายเลข 100-4096 (ยิ่งน้อยยิ่งประมวลผลก่อน)
- Source/Destination: IP Range (CIDR), Service Tag, Application Security Group (ASG)
- Protocol: TCP, UDP, ICMP, ESP, AH, * (All)
- Direction: Inbound / Outbound
- Port Range: 1-65535, * (All)
- Action: Allow / Deny
Default Rules (กฎเริ่มต้น) ใน NSG ใหม่
เมื่อสร้าง NSG ใหม่ จะมี Default Rules ดังนี้ (Priority สูง = ตัวเลขมาก = ประมวลผลทีหลัง):
Inbound Default Rules
| Priority | Name | Source | Destination | Service | Action |
|---|---|---|---|---|---|
| 65000 | AllowVNetInBound | VirtualNetwork | VirtualNetwork | Any | Allow |
| 65001 | AllowAzureLoadBalancerInBound | AzureLoadBalancer | Any | Any | Allow |
| 65500 | DenyAllInBound | Any | Any | Any | Deny |
Outbound Default Rules
| Priority | Name | Source | Destination | Service | Action |
|---|---|---|---|---|---|
| 65000 | AllowVNetOutBound | VirtualNetwork | VirtualNetwork | Any | Allow |
| 65001 | AllowInternetOutBound | Any | Internet | Any | Allow |
| 65500 | DenyAllOutBound | Any | Any | Any | Deny |
Final Rule (กฎสุดท้าย) คืออะไร?
Final Rule = DenyAllInBound (Priority 65500) และ DenyAllOutBound (Priority 65500)
นี่คือ Implicit Deny Rule ที่ Azure สร้างไว้โดยอัตโนมัติ ไม่สามารถลบ แก้ไข หรือปิดใช้งานได้ หาก Traffic ไม่ Match กับ Rule ใดๆ ที่ Priority ต่ำกว่า (Allow Rules) จะถูก Deny โดยกฎนี้
ลำดับการประมวลผล (Processing Order)
- Rules ที่ Priority ต่ำกว่า (ตัวเลขน้อย) → ประมวลผลก่อน
- หาก Match Rule ใด Rule หนึ่ง → หยุดประมวลผล (First Match Wins)
- หากไม่ Match Rule ใดๆ ทั้งสิ้น → ถูกจับโดย DenyAll (Final Rule)
NSG Limits และ Quotas
- สูงสุด 200 Rules ต่อ 1 NSG (ขอเพิ่มได้ถึง 1,000 ผ่าน Support Ticket)
- สูงสุด 100 NSG ต่อ Subscription (ขอเพิ่มได้)
- NSG 1 ตัว ผูกกับ Subnet ได้หลายตัว, NIC ได้หลายตัว
NSG vs ASG (Application Security Group)
- NSG: กฎระเบียบ Firewall (Allow/Deny Traffic)
- ASG: กลุ่ม VM ตาม Application (เช่น Web-Servers, DB-Servers) ใช้เป็น Source/Destination ใน NSG Rule แทน IP Address
NSG vs Azure Firewall
| Feature | NSG | Azure Firewall |
|---|---|---|
| OSI Layer | Layer 3/4 | Layer 3, 4, 7 |
| FQDN Filtering | ไม่รองรับ | รองรับ (SQL, FQDN Tags) |
| SNAT/DNAT | ไม่รองรับ | รองรับ |
| Threat Intelligence | Basic (Service Tags) | Advanced (IDPS, TLS Inspect) |
| Cost | ฟรี | จ่ายตามชั่วโมง + Data Processed |
| High Availability | N/A (Stateless) | Built-in (Availability Zones) |
Best Practices
- ใช้ Priority ห่างกัน (100, 200, 300...) เพื่อแทรก Rule ใหม่ได้
- ใช้ Service Tags (VirtualNetwork, AzureLoadBalancer, Internet, Storage, Sql) แทน IP คงที่
- ใช้ ASG จัดกลุ่ม Workload เพื่อลด Complexity ของ Rules
- แยก NSG ต่อ Subnet (Subnet-level) แทน NIC-level เพื่อจัดการง่ายขึ้น
- Log 流量ไป Log Analytics Workspace เพื่อ Analyze/Detection
- Review Rules เป็นประจำ ลบ Rules ที่ไม่ใช้
สรุป
Final Rule ใน NSG คือ DenyAllInBound และ DenyAllOutBound (Priority 65500) เป็น Implicit Deny ที่บังคับให้ Traffic ที่ไม่ได้ Allow ไว้โดยเฉพาะ จะถูก Block ทั้งหมด นี่คือหลักการ Default Deny / Zero Trust ของ Azure Network Security