Lesson 2 of 4 Network Traffic Filtering

Implementing NSGs and Application Security Groups

This lesson explains how NSG rules are evaluated and combined, and how ASGs simplify design.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain priority and first-match evaluation.
  • Describe how subnet and NIC NSGs combine.
  • Use application security groups in rules.
  • Identify the limits of NSGs.

How an NSG works

A network security group (NSG) holds rules that allow or deny traffic. It can be associated with a subnet, with a network interface, or with both. A rule has a priority, source, destination, port, protocol, direction and action. Rules are stateful: if an inbound connection is allowed, its return traffic is allowed automatically, with no outbound rule.

Rules are evaluated in order of priority, from the lowest number to the highest. Priorities range from 100 to 4,096, and processing stops at the first match, so a low-numbered deny hides a later allow. Every NSG also holds default rules that cannot be removed, at priorities from 65,000 to 65,500, as the following table shows.

DirectionDefault ruleEffect
InboundAllowVnetInBoundAllows traffic from the virtual network, which includes peered networks
InboundAllowAzureLoadBalancerInBoundAllows the load balancer's health probes
InboundDenyAllInBoundDenies everything else
OutboundAllowVnetOutBoundAllows traffic to the virtual network
OutboundAllowInternetOutBoundAllows traffic to the internet
OutboundDenyAllOutBoundDenies everything else

The default rules show two points that are worth remembering. Traffic within a virtual network is allowed by default, and outbound traffic to the internet is allowed by default. If segmentation or outbound control is required, these defaults must be overridden with custom rules at lower numbers.

True or false

NSG defaults

Exam skills 2.3.1

Check how NSG rules and default rules behave.

Log in to try this activity Interactive activities are free with an account.

Combining subnet and NIC NSGs

If a subnet and a network interface both have an NSG, then both must allow a flow. The order depends on direction. For inbound traffic, the subnet NSG is evaluated first, then the NIC NSG. For outbound traffic, the NIC NSG is evaluated first, then the subnet NSG. If either one denies the flow, it stops. A person can therefore allow port 443 on a VM's NIC and still see failures, since the subnet NSG may be blocking it.

A simple design associates NSGs with subnets, and avoids NIC-level NSGs unless there is a reason. Two NSGs on the same path double the work of troubleshooting. Traffic within a subnet is also evaluated by that subnet's NSG, so a subnet NSG can isolate VMs from each other.

The following diagram shows the evaluation order in each direction.

How subnet and network interface NSGs combine. Both must allow a flow. Inbound traffic is evaluated by the subnet NSG first and then the NIC NSG; outbound traffic by the NIC NSG first and then the subnet NSG. If either denies it, the flow stops, so allowing port 443 on a VM's NIC still fails if the subnet NSG blocks it.
Both NSGs must allow a flow.
Put in order

Inbound evaluation order

Exam skills 2.3.1

Put the stages of inbound filtering for a VM in order.

Log in to try this activity Interactive activities are free with an account.

Application security groups

Rules based on IP addresses are hard to read and they break when addresses change. An application security group (ASG) groups network interfaces under a name. A rule can then say that asg-web may reach asg-app on port 8443, whatever the addresses of the machines are. Adding a VM to the tier needs only an ASG membership, with no rule change.

ASGs have limits. All network interfaces in an ASG must be in the same virtual network as the first interface added, and an ASG used in a rule as both source and destination must be in the same network. They contain NICs only, so they cannot represent on-premises addresses, and they work inside NSG rules, not by themselves. The following table compares ways of naming a source or destination.

OptionWhat it representsBest for
IP address or CIDRA fixed rangeOn-premises or partner networks
Service tagA Microsoft-maintained range for a serviceAzure services and the internet
Application security groupNICs that you group by roleTiers of an application
Scenario questions

Naming sources and destinations

Exam skills 2.3.1

Choose how to name the source or destination in each rule.

Log in to try this activity Interactive activities are free with an account.

Designing rules

A sound rule set starts from least privilege. Allow only the flows that an application needs, deny the rest with a custom rule of your own, and leave gaps between priority numbers, such as 100, 200 and 300, so that later rules can fit between them. Do not expose management ports such as 3389 and 22 to the internet: use Azure Bastion or just-in-time access. Use augmented rules, which accept several addresses and ports in one rule, to keep rule sets short.

The following commands create an ASG and a rule that uses it. The values in capitals are placeholders.

az network asg create --resource-group RG --name asg-web
az network asg create --resource-group RG --name asg-app
az network nsg rule create --resource-group RG --nsg-name NSG --name allow-web-to-app \
  --priority 200 --direction Inbound --access Allow --protocol Tcp \
  --source-asgs asg-web --destination-asgs asg-app --destination-port-ranges 8443

Limits of NSGs

NSGs are powerful, but they have limits. They work on addresses and ports, so they cannot see a domain name or inspect content. They are also local: each team usually manages its own, so a central security team has no guarantee that a rule stays in place. Azure Virtual Network Manager addresses that problem, as the next lesson explains, and Azure Firewall addresses filtering by FQDN.

Worked example

Tracing a rule set

A VM's subnet NSG has an inbound rule at priority 100 that allows TCP 443 from the internet. The VM's NIC NSG has only the default rules. A client on the internet cannot connect on port 443. What is the cause, and how should you fix it?

Inbound traffic passes the subnet NSG, which allows it, and then the NIC NSG. The NIC NSG has no allow rule for internet traffic, so its DenyAllInBound default rule blocks it, because AllowVnetInBound does not match an internet source.

Either add an allow rule to the NIC NSG, or remove the NIC NSG if it is not needed, which is the simpler design. Raising the priority of the subnet rule has no effect, because each NSG is evaluated separately, and both must allow the flow.

Check yourself

A rule at priority 300 allows port 443, and a rule at 200 denies the same traffic. What is the result?

Show answer

Denied. Lower numbers run first, and the first match ends evaluation.

Exam Tips

Remember for the exam

  • Lower priority numbers are processed first, and the first match wins.
  • Inbound runs subnet then NIC; outbound runs NIC then subnet.
  • All NICs in an ASG must be in the same virtual network.
  • NSGs are stateful, so return traffic needs no rule.