Lesson 3 of 6 Securing Azure Virtual Networks

Evaluating Effective Security Rules

This lesson explains how subnet and NIC NSGs combine and how to view the effective result.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain the order in which subnet and NIC NSGs are evaluated.
  • Describe the tools that show effective rules.
  • Work out whether traffic is allowed when two NSGs apply.

Evaluation order

For inbound traffic to a VM, Azure first evaluates the NSG associated with the subnet, then the NSG associated with the network interface. For outbound traffic, it evaluates the NIC NSG first, then the subnet NSG. Traffic is allowed only if every NSG in the path allows it; if either denies it, it is dropped. If no NSG is associated at a level, that level does not filter.

Traffic between VMs in the same subnet is also evaluated by the subnet NSG, which is easily overlooked: a subnet NSG rule that denies a port blocks it between neighbours too.

The following diagram traces RDP traffic through the subnet NSG and the NIC NSG to two VMs in the same subnet.

Why RDP reaches VM2 but not VM1 in the same subnet. Inbound traffic passes the subnet NSG first and then the NIC NSG. VM1's NIC NSG has only default rules, so DenyAllInBound drops the internet traffic; VM2 has no NIC NSG, so the subnet rule alone applies. An allow rule for TCP 3389 from 203.0.113.0/24 on NSG-NIC fixes VM1.
The scenario is illustrative.
Scenario questions

Two NSGs in the path

Exam skills 4.2.2

Work out the result when a subnet NSG and a NIC NSG both apply.

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

Viewing effective rules

The network interface's Effective security rules page (also available in Network Watcher) shows the rules of each NSG that applies to the NIC, with service tags and ASGs expanded into address prefixes, and can be downloaded. IP flow verify tests one packet, given direction, protocol, local and remote IP and port, and reports Allow or Deny with the name of the deciding rule. NSG diagnostics shows how each NSG along the path treats a flow. The VM must be running for these tools to report.

True or false

Effective rules tools

Exam skills 4.2.2

Check what the effective rules tools show.

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

Two NSGs

VM1's subnet has NSG-Subnet with an inbound rule allowing TCP 3389 from 203.0.113.0/24 at priority 100. VM1's NIC has NSG-NIC with only the default rules. A user at 203.0.113.25 cannot connect over RDP. VM2, in the same subnet with no NIC NSG, accepts RDP from the same user. Explain the result and the fix.

Inbound RDP to VM1 passes NSG-Subnet but then reaches NSG-NIC, where the default DenyAllInBound rule drops it, because the internet is not part of VirtualNetwork. VM2 has no NIC NSG, so the subnet rule alone applies. Add an allow rule for TCP 3389 from 203.0.113.0/24 to NSG-NIC, or remove NSG-NIC if per-VM rules are not needed. Better still, use Azure Bastion instead of exposing RDP.

Check yourself

For outbound traffic from a VM, which NSG is evaluated first?

Show answer

The NSG on the network interface, then the NSG on the subnet.

Exam Tips

Remember for the exam

  • Inbound: subnet NSG, then NIC NSG. Outbound: NIC NSG, then subnet NSG.
  • Traffic must be allowed by every NSG in its path.
  • IP flow verify names the rule that allows or denies a packet.
  • A level without an NSG does not filter.