Azure NSG allows internet access to SSH

Restrict SSH administration to approved access paths.

Description

Remove the public TCP 22 allowance if internet SSH is unnecessary. Where administrators use SSH, allow only the source addresses the VM sees from the management network, VPN or Azure Bastion.

Potential impact

With a route to the VM and an SSH listener, external clients can attempt sign-in with passwords or compromised keys.

Remediation

Check key authentication and the effective subnet and NIC rules. Establish the management path first, then test new connections from it and from an unapproved source.

Examples

Both rules match every source at priority 100. The second rule’s Deny blocks new SSH connections even from private management networks. To retain SSH, allow the specific management source first and give the blanket deny a larger priority number.

Before

hcl
resource "azurerm_network_security_rule" "example" {
  name                        = "example"
  priority                    = 100
  direction                   = "Inbound"
  access                      = "Allow"
  protocol                    = "TCP"
  source_port_range           = "*"
  destination_port_range      = "22"
  source_address_prefix       = "*"
  destination_address_prefix  = "*"
  resource_group_name         = azurerm_resource_group.example.name
  network_security_group_name = azurerm_network_security_group.example.name
}

After

hcl
resource "azurerm_network_security_rule" "example" {
  name                        = "example"
  priority                    = 100
  direction                   = "Inbound"
  access                      = "Deny"
  protocol                    = "TCP"
  source_port_range           = "*"
  destination_port_range      = "22"
  source_address_prefix       = "*"
  destination_address_prefix  = "*"
  resource_group_name         = azurerm_resource_group.example.name
  network_security_group_name = azurerm_network_security_group.example.name
}

References