Azure Documentation

Microsoft Learn

Network Security Groups Best Practices

Last updated: October 26, 2023 | Article ID: 500479

Network Security Groups (NSGs) are a fundamental component of Azure networking, providing network segmentation and security by allowing or denying network traffic to Azure resources in an Azure virtual network. Implementing NSGs effectively is crucial for maintaining a secure and well-architected cloud environment.

Core Principles of NSG Management

Adhering to these principles will help you build a robust and maintainable NSG strategy:

1. Least Privilege Principle

Apply the principle of least privilege to your NSG rules. Only allow the necessary ports and protocols required for your applications to function. Avoid using broad rules like AllowAny unless absolutely necessary and with strict source/destination IP restrictions.

2. Rule Priority and Ordering

NSG rules are evaluated based on their priority number, from lowest (0) to highest (4096). The first rule that matches the traffic is applied, and processing stops. Therefore, the order of your rules is critical.

For example, a rule with priority 100 to deny specific traffic should be placed before a rule with priority 200 to allow general inbound traffic from a VNet.

3. Tagging and Naming Conventions

Implement a clear and consistent naming convention for your NSG rules and NSGs themselves. This significantly improves manageability and auditability.

4. Use of Service Tags

Service tags are Microsoft-managed collections of IP prefixes that represent a given Azure service. Using service tags in NSGs is a recommended best practice as it simplifies rule management and automatically updates as the service's IP addresses change.

Examples include:

Reference these tags as source or destination for your rules instead of hardcoding IP addresses where applicable.

5. Network Security Groups Association

NSGs can be associated with Network Interfaces (NICs) or Subnets. Understand the implications of each:

Be aware that if an NSG is associated with both a subnet and a NIC, the rules are processed in a specific order: NIC rules are applied first, then subnet rules.

Advanced Best Practices

1. NSG Flow Logs

Enable NSG Flow Logs to capture information about IP traffic flowing to and from NSG resources. This is invaluable for troubleshooting, security analysis, and compliance.

2. Application Security Groups (ASGs)

For applications with many VMs and complex security requirements, Application Security Groups (ASGs) simplify NSG management. ASGs allow you to group VMs logically and define security rules based on these groups rather than individual IPs.

Example:

3. Regular Auditing and Review

Network security is an ongoing process. Regularly audit your NSG configurations to ensure they align with your current security posture and business requirements.

4. Defense in Depth

NSGs are a critical layer of defense, but they should be part of a broader defense-in-depth strategy. Combine NSGs with other Azure security features:

Important Consideration

Remember that NSGs do not filter traffic between subnets within the same Virtual Network by default. To segment traffic between subnets, you must explicitly configure NSG rules or use Azure Firewall.

Example NSG Rule Structure

Here’s an example of a well-structured NSG rule for allowing inbound HTTP traffic to a web server subnet:


{
    "properties": {
        "priority": 110,
        "protocol": "Tcp",
        "access": "Allow",
        "direction": "Inbound",
        "sourceAddressPrefix": "*",
        "sourcePortRange": "*",
        "destinationAddressPrefix": "*",
        "destinationPortRange": "80",
        "name": "Allow-HTTP-Inbound"
    }
}
            

Recommendation: For better security, replace sourceAddressPrefix: "*" with a specific IP range or service tag representing your allowed public access points.

Conclusion

By implementing these best practices, you can leverage Network Security Groups effectively to enhance the security posture of your Azure deployments, ensuring only authorized traffic reaches your resources.