Network Security Group (NSG) trong Azure là gì?
Network Security Group (NSG) là bộ quy tắc bảo mật (Access Control List - ACL) cho phép hoặc từ chối lưu lượng mạng vào/ra các tài nguyên Azure (VM, subnet, NIC). NSG hoạt động ở Layer 3 (Network) và Layer 4 (Transport) của mô hình OSI.
Mỗi quy tắc NSG bao gồm:
- Source / Destination: IP address, IP range (CIDR), Service Tag, Application Security Group (ASG)
- Source Port / Destination Port: Cổng đơn, phạm vi cổng, hoặc * (tất cả)
- Protocol: TCP, UDP, ICMP, ESP, AH, * (tất cả)
- Action: Allow (cho phép) hoặc Deny (từ chối)
- Priority: 100-4099 (số càng nhỏ ưu tiên càng cao)
- Direction: Inbound (vào) hoặc Outbound (ra)
Cách tạo NSG trên Azure Portal
Bước 1: Tạo Network Security Group
- Đăng nhập Azure Portal (portal.azure.com)
- Tìm kiếm Network security groups → chọn
- Nhấn + Create
- Điền thông tin:
- Subscription: Chọn subscription
- Resource group: Chọn hoặc tạo mới
- Name: Tên NSG (ví dụ: nsg-web-prod)
- Region: Chọn region (phải trùng region của VNet/VM)
- Nhấn Review + create → Create
Bước 2: Thêm quy tắc bảo mật (Inbound/Outbound Rules)
- Vào NSG vừa tạo → menu bên trái chọn Inbound security rules hoặc Outbound security rules
- Nhấn + Add
- Điền quy tắc (ví dụ cho phép HTTP/HTTPS inbound):
- Source: Any / IP CIDR / Service Tag (Internet, VirtualNetwork, AzureLoadBalancer...)
- Source port ranges: *
- Destination: Any / IP CIDR / ASG
- Destination port ranges: 80,443 (hoặc 80-443)
- Protocol: TCP
- Action: Allow
- Priority: 100 (cao hơn quy tắc mặc định)
- Name: Allow-HTTP-HTTPS-Inbound
- Description: Mô tả quy tắc
- Nhấn Add
Bước 3: Gán NSG cho Subnet hoặc Network Interface (NIC)
Khuyên dùng: Gán cho Subnet (quản lý tập trung, áp dụng cho tất cả VM trong subnet)
- Vào NSG → Subnets → + Associate
- Chọn Virtual network → chọn Subnet → OK
Hoặc gán cho NIC cụ thể: NSG → Network interfaces → + Associate → chọn NIC của VM
NSG vs Azure Firewall
| Đặc điểm | NSG | Azure Firewall |
|---|---|---|
| Layer OSI | L3/L4 | L3/L4/L7 (Application) |
| Stateful | Có | Có (full stateful) |
| FQDN Filtering | Không | Có |
| Threat Intelligence | Không | Có (built-in) |
| SNAT/DNAT | Không | Có |
| Chi phí | Miễn phí | Có phí (per hour + data processed) |
| Phù hợp | Lọc traffic cơ bản, micro-segmentation | Bảo mật perimeter, egress control, app-layer inspection |
Quản lý NSG hiệu quả
- Dùng Service Tags (Internet, VirtualNetwork, AzureCloud, Storage...) thay vì hardcode IP
- Dùng Application Security Groups (ASG) để gom VM theo vai trò (web, app, db) thay vì IP
- Đặt priority hợp lý: quy tắc allow cụ thể (priority thấp) > quy tắc deny rộng (priority cao)
- Bật Flow Logs (NSG flow logs → Log Analytics/Storage) để audit, troubleshoot
- Test quy tắc với IP Flow Verify (Network Watcher)
- Xem Effective Security Rules trên NIC để debug thứ tự ưu tiên
Ví dụ quy tắc phổ biến
- Cho phép SSH (22) / RDP (3389) chỉ từ IP quản trị (Bastion/Jumpbox)
- Cho phép HTTP (80) / HTTPS (443) từ Internet đến Web tier
- Cho phép App tier truy cập DB tier (port 1433, 3306, 5432...) qua ASG
- Chặn outbound không cần thiết (chỉ cho phép đến Azure services, update servers)