Network Security Group (NSG) คืออะไร?
Network Security Group (NSG) เป็นบริการของ Microsoft Azure ที่ทำหน้าที่เป็น Virtual Firewall แบบ Layer 3/4 (Network/Transport Layer) ใช้ควบคุมทราฟฟิกเครือข่ายเข้า-ออก (Inbound/Outbound) ไปยัง Azure Resources เช่น Virtual Machines (VM), Subnets, Network Interfaces (NIC) ผ่านกฎ (Rules) ที่กำหนดเอง
คุณสมบัติหลักของ NSG
- Stateful: ติดตามสถานะการเชื่อมต่อ (Allow return traffic อัตโนมัติ)
- Priority-based: ประมวลผลกฎตาม Priority (เลขน้อย = สำคัญกว่า) 100-4096
- 5-Tuple Matching: Source IP/Port, Destination IP/Port, Protocol (TCP/UDP/ICMP/*)
- Default Rules: มีกฎเริ่มต้น (Allow VNet Inbound, Allow Internet Outbound, Deny All Inbound, Deny All Outbound) ไม่สามารถลบได้ แต่ override ได้
- Association: ผูกกับ Subnet (ผลกระทบทุก Resource ใน Subnet) หรือ NIC (เฉพาะ VM นั้น)
โครงสร้างกฎ NSG (NSG Rule Structure)
| ฟิลด์ | คำอธิบาย | ตัวอย่าง |
|---|---|---|
| Name | ชื่อกฎ (Unique ใน NSG) | Allow-HTTP-Inbound |
| Priority | ลำดับความสำคัญ (100-4096) | 100 |
| Source / Destination | IP Address, CIDR, Service Tag, ASG | 10.0.0.0/16, Internet, VirtualNetwork |
| Protocol | TCP, UDP, ICMP, ESP, AH, * (All) | TCP |
| Direction | Inbound / Outbound | Inbound |
| Port Range | Port หรือ Range (1-65535, *) | 80, 443, 80-8080 |
| Action | Allow / Deny | Allow |
| คุณสมบัติ | NSG | Azure Firewall |
|---|---|---|
| OSI Layer | Layer 3/4 (L3/L4) | Layer 3/4/7 (L3/L4/L7) |
| Stateful | ใช่ | ใช่ |
| Application Inspection (L7) | ไม่ | ใช่ (FQDN, SQL, HTTP Headers) |
| Threat Intelligence | ไม่ | ใช่ (Built-in) |
| SNAT/DNAT | ไม่ | ใช่ |
| Cost | ฟรี (คิดค่า Data Transfer ปกติ) | มีค่าใช้จ่ายต่อชั่วโมง + Data |
| Use Case | Basic Segmentation, Micro-segmentation | Centralized Egress/Ingress, L7 Inspection, Hybrid |
ลำดับการประมวลผลกฎ (Rule Processing Order)
Inbound Traffic:
- NSG ผูกกับ NIC (ถ้ามี)
- NSG ผูกกับ Subnet (ถ้ามี)
- กฎ Default
Outbound Traffic:
- NSG ผูกกับ Subnet (ถ้ามี)
- NSG ผูกกับ NIC (ถ้ามี)
- กฎ Default
กฎแรกที่ Match (Priority ต่ำสุด) จะถูก Execute แล้วหยุด (First Match Wins)
สร้าง NSG (Azure Portal / CLI / PowerShell / Bicep / Terraform)
Azure CLI
# สร้าง NSG
az network nsg create -g MyResourceGroup -n MyNSG -l eastus
# สร้างกฎ Allow HTTP
az network nsg rule create -g MyResourceGroup --nsg-name MyNSG \
-n Allow-HTTP --priority 100 --direction Inbound \
--protocol Tcp --destination-port-range 80 \
--source-address-prefix Internet --destination-address-prefix '*' \
--access Allow
# ผูกกับ Subnet
az network vnet subnet update -g MyResourceGroup --vnet-name MyVNet \
-n MySubnet --network-security-group MyNSG
Terraform
resource "azurerm_network_security_group" "example" {
name = "example-nsg"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
security_rule {
name = "AllowHTTP"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "80"
source_address_prefix = "Internet"
destination_address_prefix = "*"
}
}
Best Practices
- Least Privilege: เปิด Port/Source น้อยที่สุดเท่าที่จำเป็น
- ใช้ Service Tags / ASG: ลดความซับซ้อนของ IP Management
- แยก NSG ต่อ Environment/Tier: Prod/Dev, Web/App/DB
- Log Analytics: เปิด NSG Flow Logs ส่งไป Log Analytics Workspace เพื่อ Audit/Alert
- Test ก่อน Deploy: ใช้
az network nsg rule listและ Effective Security Rules ใน Portal - Deny by Default: ปิดทั้งหมด เปิดเฉพาะที่ต้องการ (Default Deny All Inbound ช่วยอยู่แล้ว)
- Naming Convention: Allow-HTTP-Inbound, Deny-SSH-From-Internet, Allow-DB-From-ASG-App
Troubleshooting
- Effective Security Rules: ใน Portal VM > Networking > Effective security rules (ดูกฎที่รวม NIC+Subnet+Default)
- NSG Flow Logs: บันทึกทราฟฟิก Allow/Deny ไป Storage/Log Analytics
- Connection Troubleshoot: Network Watcher > Connection Troubleshoot / IP Flow Verify
- Priority Conflict: ตรวจสอบ Priority ซ้ำหรือทับซ้อน
สรุป
NSG เป็น Virtual Firewall พื้นฐานของ Azure ควบคุมทราฟฟิก L3/L4 ด้วยกฎ Priority-based ใช้ร่วมกับ Service Tags และ ASG เพื่อจัดการ Scale ได้ ควรใช้ร่วมกับ Azure Firewall สำหรับ L7/Threat Intel และ Centralized Egress NSG ฟรี (คิดค่า Data Transfer) เหมาะสำหรับ Micro-segmentation และ Basic Network Security