Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบเครือข่าย >> ความปลอดภัยเครือข่าย

วิธีแก้ไข Network Security Groups (NSG) ใน Microsoft Azure

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

คุณสมบัติNSGASGAzure Firewall
LayerL3, L4Object ใน NSGL3, L4, L7
Statefulใช่N/Aใช่ (Full Stateful)
Application Inspectionไม่ไม่ใช่ (L7)
Threat Intelligenceไม่ไม่ใช่
SNAT/DNATไม่ไม่ใช่
ราคาฟรีฟรีมีค่าใช้จ่าย

สร้าง NSG ใน Azure Portal

  1. Sign in Azure Portal
  2. คลิก Create a resource → Networking → Network Security Group
  3. กรอกข้อมูล:
    • Subscription
    • Resource Group (สร้างใหม่หรือเลือกมีอยู่)
    • Name (เช่น nsg-prod-web)
    • Region (ต้องตรงกับ Region ของ Resources)
  4. คลิก Review + create → Create

เพิ่ม/แก้ไข Security Rules ใน NSG

  1. เข้า NSG Resource ที่สร้างไว้
  2. ในเมนู Settings เลือก Inbound security rules หรือ Outbound security rules
  3. คลิก + Add
  4. กำหนดค่า:
    • 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: รายละเอียดเพิ่มเติม
  5. คลิก Add

ผูก NSG กับ Subnet หรือ NIC

ผูกกับ Subnet (แนะนำสำหรับ Production)

  1. ใน NSG → Settings → Subnets → + Associate
  2. เลือก Virtual Network → เลือก Subnet → OK

ผูกกับ NIC (Network Interface)

  1. ใน NSG → Settings → Network interfaces → + Associate
  2. เลือก NIC ของ VM → OK

ตั้งค่า ASG และใช้ใน NSG Rule

  1. สร้าง ASG: Create resource → Application Security Group → ตั้งชื่อ (เช่น asg-web, asg-db) → Region เดียวกับ VM
  2. ผูก ASG กับ NIC ของ VM: เข้า VM → Networking → Application security groups → Add → เลือก ASG
  3. ใน 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