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

Azure Network Security Group (NSG): สร้าง จัดการ และ Best Practices

Azure Network Security Group (NSG) คืออะไร?

NSG เป็น Virtual Firewall แบบ Layer 3/4 (L3/L4) ควบคุม Traffics เข้า/ออก Azure Resources (VM, Subnet, NIC, App Gateway, API Management, AKS, VNet) ผ่าน Security Rules (Allow/Deny) ตาม 5-tuple: Source IP/Port, Destination IP/Port, Protocol (TCP/UDP/ICMP/*)

🏗️ โครงสร้าง NSG

  • Security Rules: Priority (100-4096), Name, Port, Protocol, Source/Dest (IP/CIDR/Service Tag/Application Security Group), Action (Allow/Deny), Direction (Inbound/Outbound)
  • Default Rules: มี Default Rules คงที่ (Priority 65000+) Allow VNet Inbound/Outbound, Allow Azure Load Balancer, Deny All Inbound/Outbound
  • Association: ผูกกับ Subnet (แนะนำ) หรือ NIC (VM เดียว) ได้ พร้อมกันได้ (Subnet + NIC) → Rule ทั้งสองชุดทำงาน (Effective Rules)
  • Service Tags: กลุ่ม IP ของ Azure Services (AzureCloud, AzureLoadBalancer, Storage, Sql, ApiManagement, etc.) อัปเดตอัตโนมัติโดย Microsoft
  • Application Security Groups (ASG): จัดกลุ่ม VM ตาม Application Logic (เช่น Web, App, DB) ใช้ใน Source/Destination แทน IP

⚙️ สร้างและจัดการ NSG (Portal / CLI / IaC)

Portal

  1. Create a Resource → Networking → Network Security Group
  2. Subscription, Resource Group, Name, Region
  3. Create → ไปที่ NSG → Settings → Inbound/Outbound Security Rules → Add
  4. Associate: Subnets → + Associate → เลือก VNet + Subnet

Azure CLI

# Create NSG
az network nsg create -g MyRG -n MyNSG -l eastus

# Add Rule (Allow HTTPS Inbound)
az network nsg rule create -g MyRG --nsg-name MyNSG -n Allow-HTTPS-Inbound \
  --priority 100 --direction Inbound --access Allow --protocol Tcp \
  --source-address-prefixes Internet --source-port-ranges '*' \
  --destination-address-prefixes '*' --destination-port-ranges 443

# Associate to Subnet
az network vnet subnet update -g MyRG --vnet-name MyVNet -n MySubnet \
  --network-security-group MyNSG

Terraform (IaC แนะนำ)

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                       = "AllowHTTPSInbound"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "443"
    source_address_prefix      = "Internet"
    destination_address_prefix = "*"
  }
}

resource "azurerm_subnet_network_security_group_association" "example" {
  subnet_id                 = azurerm_subnet.example.id
  network_security_group_id = azurerm_network_security_group.example.id
}

🔄 NSG vs Azure Firewall vs ASG

FeatureNSGAzure FirewallASG
LayerL3/L4L3/L4/L7 (App ID, FQDN, TLS Inspection)Logical Grouping (Object in NSG)
StatefulYesYesN/A
Threat IntelligenceNoYes (Microsoft/3rd party)No
SNAT/DNATNoYesNo
CostFree (Rules only)~$1.25/hr + DataFree
Use CaseMicro-segmentation, Basic FilteringCentralized Egress/Ingress, L7 InspectionSimplify NSG Rules (App-tier logic)

🏆 Best Practices

  1. Associate at Subnet Level (ไม่ใช่ NIC) เพื่อ Consistency และ Manageability
  2. ใช้ Service Tags แทน Hard-coded IP (เช่น AzureCloud, Storage.Region)
  3. ใช้ ASG สำหรับ Application Tier (Web, App, DB) ลด Complexity ของ Rules
  4. Priority Spacing: เว้น 100 (100, 200, 300...) เพื่อ Insert Rule ง่าย
  5. Explicit Deny All Inbound ท้ายสุด (Priority 4000) ยกเว้นที่จำเป็น
  6. Log to Log Analytics / Storage Account → Diagnostic Settings → NSG Flow Logs
  7. Test ก่อน Apply:ใช้ IP Flow Verify (Network Watcher) และ Effective Security Rules
  8. Automate with IaC (Terraform, Bicep, ARM) + Policy (Azure Policy: Deny NSG w/o Flow Logs, Deny RDP/SSH from Internet)

🔍 Troubleshooting

  • Effective Security Rules: ดู Rule ที่奏效จริงบน NIC/Subnet (รวม Default Rules)
  • IP Flow Verify: ทดสอบ Source/Dest IP/Port/Protocol ว่า Allow/Deny
  • NSG Flow Logs: ส่งไป Log Analytics → KQL Query / Workbook
  • Connection Troubleshoot: Network Watcher → Connection Troubleshoot

❌ ไม่สามารถ Rename NSG ได้

ต้องสร้าง NSG ใหม่ → Copy Rules → Associate ใหม่ → ลบเก่า (ใช้ IaC จะง่ายกว่า)

สรุป

NSG = L3/L4 Firewall ฟรีใน Azure ควบคุม Traffic ด้วย Rules (5-tuple) ผูกกับ Subnet (Best Practice) ใช้ Service Tags + ASG ลด Complexity Logging ด้วย Flow Logs ทดสอบด้วย IP Flow Verify ไม่ Rename ได้ ต้องสร้างใหม่ เปรียบเทียบ: NSG (Micro-seg) vs Azure Firewall (Central L7) vs ASG (Logical Grouping)