Network Security Group Rules

Network Security Groups (NSGs) are a fundamental component of Azure's network security. They act as a virtual firewall for your Azure resources, allowing you to define inbound and outbound security rules that control network traffic at the subnet or network interface (NIC) level.

Note: NSGs can be associated with either a subnet or a network interface (NIC). If an NSG is associated with both, the rules defined for the NIC take precedence.

Understanding NSG Rules

Each NSG contains a list of security rules. These rules are evaluated based on a priority number. Lower numbers indicate higher priority. When traffic matches a rule, processing stops, and the action defined in that rule is applied.

Rule Components

An NSG rule is defined by the following parameters:

Default Rules

Every NSG comes with a set of default rules that are created automatically. These rules cannot be deleted but can be overridden by custom rules with lower priority numbers.

Priority Name Source Source Port Ranges Destination Destination Port Ranges Protocol Direction Action
65500 DenyAllInbound * * * * * Inbound Deny
65501 AllowVNetInbound VirtualNetwork * * * * Inbound Allow
65502 AllowAzureLoadBalancerInbound AzureLoadBalancer * * * * Inbound Allow
65503 DenyAllOutbound * * * * * Outbound Deny
65504 AllowVnetOutbound VirtualNetwork * * * * Outbound Allow
65505 AllowInternetOutbound Internet * * * * Outbound Allow

Service Tags

Service tags represent a group of IP addresses from a given Azure service. Microsoft manages the IP addresses and automatically updates the service tag as addresses change. This simplifies rule management significantly.

Examples of service tags include:

Application Security Groups (ASGs)

Application Security Groups allow you to group virtual machines and manage network security based on the application's identity. You can use ASGs as either source or destination in your NSG rules, treating the VMs within the ASG as a single network security entity.

Rule Evaluation Order

  1. NSGs associated with a NIC are evaluated first, followed by NSGs associated with the subnet.
  2. For inbound traffic, the rules are processed in the order of lowest priority number to highest. The first matching rule determines the action (Allow or Deny).
  3. For outbound traffic, the rules are also processed in the order of lowest priority number to highest.
  4. If no explicit rule matches, the default rules are applied. For inbound traffic, this means DenyAllInbound. For outbound traffic, this means AllowInternetOutbound (if not denied by a custom rule).
Important: It's crucial to define your rules carefully with appropriate priorities to ensure desired network access and security. A common practice is to create custom rules starting from priority 100 upwards and leave default rules for final explicit denials.

Creating and Managing NSG Rules

You can create and manage NSG rules through the Azure portal, Azure CLI, Azure PowerShell, or ARM templates. Each method offers a graphical interface or command-line tools to define your security policies.

Example: Allowing SSH Access

To allow inbound SSH (TCP port 22) from a specific IP address range:


Name: AllowSSHFromTrusted
Priority: 300
Source: 203.0.113.0/24
Source port ranges: *
Destination: *
Destination port ranges: 22
Protocol: Tcp
Direction: Inbound
Action: Allow
            

For more detailed information, refer to the official Azure Network Security Groups documentation.