Azure Network Security Group (NSG) คืออะไร?
NSG เป็นบริการของ Azure ที่ใช้ ควบคุมและกรอง Network Traffic เข้าและออกจาก Azure Resources (VMs, Subnets, NICs) ทำงานที่ Layer 3 (Network) และ Layer 4 (Transport) ของโมเดล OSI ประกอบด้วยรายการกฎความปลอดภัย (Security Rules) ที่กำหนด Allow/Deny ตาม Priority
Application Security Group (ASG) คืออะไร?
ASG เป็นการจัดกลุ่ม VMs ตามหน้าที่/แอปพลิเคชัน (เช่น Web Servers, Database Servers) เพื่อนำไปใช้ใน NSG Rules แทนการระบุ IP Address โดยตรง ช่วยลดความซับซ้อนเมื่อ IP เปลี่ยนแปลง
ความแตกต่าง NSG vs ASG vs Azure Firewall
| คุณสมบัติ | NSG | ASG | Azure Firewall |
|---|---|---|---|
| Layer | L3, L4 | Object ใน NSG | L3, L4, L7 |
| Stateful | ใช่ | N/A | ใช่ (Full Stateful) |
| Application Inspection | ไม่ | ไม่ | ใช่ (L7) |
| Threat Intelligence | ไม่ | ไม่ | ใช่ |
| SNAT/DNAT | ไม่ | ไม่ | ใช่ |
| ราคา | ฟรี | ฟรี | มีค่าใช้จ่าย |
สร้าง NSG ใน Azure Portal
- Sign in Azure Portal
- คลิก Create a resource → Networking → Network Security Group
- กรอกข้อมูล:
- Subscription
- Resource Group (สร้างใหม่หรือเลือกมีอยู่)
- Name (เช่น nsg-prod-web)
- Region (ต้องตรงกับ Region ของ Resources)
- คลิก Review + create → Create
เพิ่ม/แก้ไข Security Rules ใน NSG
- เข้า NSG Resource ที่สร้างไว้
- ในเมนู Settings เลือก Inbound security rules หรือ Outbound security rules
- คลิก + Add
- กำหนดค่า:
- Source/Destination: Any, IP Addresses, Service Tag, Application Security Group
- Protocol: TCP, UDP, ICMP, Any
- Port ranges: 80, 443, 22, 3389, 1000-2000, *
- Action: Allow / Deny
- Priority: 100-4096 (ตัวเลขน้อย = ความสำคัญสูง ประมวลผลก่อน)
- Name: ใส่ชื่อที่อธิบายได้ (เช่น Allow-HTTP-Inbound)
- Description: รายละเอียดเพิ่มเติม
- คลิก Add
ผูก NSG กับ Subnet หรือ NIC
ผูกกับ Subnet (แนะนำสำหรับ Production)
- ใน NSG → Settings → Subnets → + Associate
- เลือก Virtual Network → เลือก Subnet → OK
ผูกกับ NIC (Network Interface)
- ใน NSG → Settings → Network interfaces → + Associate
- เลือก NIC ของ VM → OK
ตั้งค่า ASG และใช้ใน NSG Rule
- สร้าง ASG: Create resource → Application Security Group → ตั้งชื่อ (เช่น asg-web, asg-db) → Region เดียวกับ VM
- ผูก ASG กับ NIC ของ VM: เข้า VM → Networking → Application security groups → Add → เลือก ASG
- ใน NSG Rule: Source/Destination เลือก Application Security Group → เลือก ASG ที่สร้างไว้
Default Rules ใน NSG (ไม่สามารถลบได้ แต่อัปเดต Priority ได้)
Inbound Default Rules:
- AllowVNetInBound (Priority 65000) - อนุญาต Traffic ภายใน VNet
- AllowAzureLoadBalancerInBound (65001) - Azure LB Health Probes
- DenyAllInBound (65500) - บล็อกทั้งหมดที่เหลือ
Outbound Default Rules:
- AllowVNetOutBound (65000) - อนุญาตออกภายใน VNet
- AllowInternetOutBound (65001) - อนุญาตออก Internet
- DenyAllOutBound (65500) - บล็อกทั้งหมดที่เหลือ
Best Practices
- ใช้ ASG แทน IP Address เพื่อความยืดหยุ่น
- ใช้ Service Tags (VirtualNetwork, AzureLoadBalancer, Internet, Storage, Sql, กว่า 50+ tags) แทน IP Range
- จัด Priority ให้มีช่องว่าง (เช่น 100, 200, 300) เพื่อเพิ่ม Rule ระหว่างทางได้ง่าย
- ปฏิบัติตาม Least Privilege - เปิด Port ที่จำเป็นเท่านั้น
- Enable NSG Flow Logs ส่งไป Log Analytics / Storage Account เพื่อ Audit และ Troubleshoot
- ใช้ Azure Policy บังคับให้ NSG ถูกสร้าง/ผูกอัตโนมัติ
- ทดสอบ Rules ด้วย IP Flow Verify (Network Watcher)
PowerShell / CLI คำสั่งสำคัญ
# PowerShell
New-AzNetworkSecurityGroup -ResourceGroupName rg-prod -Name nsg-web -Location "Southeast Asia"
Add-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $nsg -Name "Allow-HTTP" -Access Allow -Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 80
# Azure CLI
az network nsg create -g rg-prod -n nsg-web -l southeastasia
az network nsg rule create -g rg-prod --nsg-name nsg-web -n Allow-HTTP --priority 100 --access Allow --protocol Tcp --direction Inbound --source-address-prefixes * --source-port-ranges * --destination-address-prefixes * --destination-port-ranges 80