Azure Networking Tutorials

Mastering Network Security Groups (NSGs)

Understanding Network Security Group (NSG) Security Rules

Network Security Groups (NSGs) are a fundamental component of Azure networking that allow you to filter network traffic to and from Azure resources in an Azure virtual network. You can associate NSGs with subnets, individual network interfaces (NICs), or both. NSGs contain a list of security rules that allow or deny network traffic based on criteria such as source and destination IP address, port, and protocol.

Key Concepts of NSG Security Rules

Each NSG contains a set of inbound and outbound security rules. These rules are processed in priority order, from lowest to highest. When a packet matches a rule, the corresponding allow or deny action is taken, and processing stops for that packet.

Rule Properties:

Default Rules:

When you create an NSG, it comes with a set of default rules. These rules are read-only and provide basic network access:

It's important to understand that the default rules are at the lowest priority. Any custom rules you create with lower priority numbers will be evaluated first. The DenyAllInbound and DenyAllOutbound rules act as a safety net.

Creating and Managing NSG Rules

You can create and manage NSG security rules using the Azure portal, Azure CLI, Azure PowerShell, or ARM templates.

Example: Allowing SSH Access to a VM

To allow Secure Shell (SSH) access (port 22) to a virtual machine, you would create an inbound security rule with the following properties:

Azure CLI example for allowing SSH:

az network nsg rule create --resource-group MyResourceGroup --nsg-name MyNsg --name AllowSSH --protocol Tcp --priority 300 --destination-port-range 22 --access Allow --direction Inbound

Best Practices for NSG Security

Remember that NSG rules are processed based on priority. If you have a lower priority rule that allows traffic, and a higher priority rule that denies it, the lower priority rule will take precedence.

Example: Denying access from a specific IP address

To block traffic from a known malicious IP address to your web servers on port 80 and 443:

NSG Flow Logs

NSG Flow Logs provide visibility into the IP traffic flowing through an NSG. They record information about the originating IP address, destination IP address, source port, destination port, protocol, and whether the traffic was allowed or denied. This is invaluable for troubleshooting connectivity issues and auditing traffic patterns.

By effectively implementing and managing Network Security Group rules, you can significantly enhance the security posture of your Azure deployments.