More Info:
Check your Microsoft Azure network security groups (NSGs) for inbound rules that allow unrestricted access (i.e. 0.0.0.0/0) on TCP port 22 and restrain access to only those IP addresses that require it in order to implement the principle of least privilege and reduce the possibility of a breach. TCP port 22 is used for secure remote login by connecting an SSH client application with an SSH server.Risk Level
CriticalAddress
SecurityCompliance Standards
HITRUST, CISAZURE, CBP, SOC2, PCIDSS, ISO27001, HIPAA, GDPR, NISTCSF, FedRAMPTriage and Remediation
- Remediation
Remediation
Using Console
Using Console
To remediate unrestricted SSH access in Azure, you can follow these steps:
- Log in to the Azure portal (https://portal.azure.com/).
- Navigate to the virtual machine that has unrestricted SSH access.
- Click on the “Networking” tab in the left-hand menu.
- Under “Inbound port rules,” click on “Add inbound port rule.”
- In the “Add inbound security rule” window, enter a name for the rule (e.g., “SSH access restricted”).
- Under “Destination port ranges,” enter “22” (or the port number that SSH is using).
- Under “Source,” select “IP Addresses.”
- Under “Source IP addresses,” enter the IP address or range that should have access to SSH (e.g., your own IP address or a specific subnet).
- Under “Action,” select “Allow.”
- Click “Add” to create the rule.
- Repeat steps 4-10 for any other virtual machines that have unrestricted SSH access.
Using CLI
Using CLI
The following are the steps to remediate unrestricted SSH access in AZURE using AZURE CLI:
-
Login to your Azure account using the Azure CLI command:
az login -
Once you are logged in, select the subscription that contains the virtual machine you want to remediate by using the command:
az account set --subscription <subscription-id> -
Get the name of the virtual machine you want to remediate by using the command:
az vm list --query "[].{name:name}" -o table -
Once you have identified the virtual machine, get the resource group name by using the command:
az vm show --name <vm-name> --query "resourceGroup" -o tsv -
Next, get the network security group associated with the virtual machine by using the command:
az vm show --name <vm-name> --resource-group <resource-group-name> --query "networkProfile.networkInterfaces[].id" -o tsv | xargs az network nic show --ids --query "networkSecurityGroup.id" -o tsv -
Get the name of the network security group by using the command:
az network nsg list --query "[].{name:name}" -o table -
Once you have identified the network security group, get the name of the security rule that allows unrestricted SSH access by using the command:
az network nsg rule list --nsg-name <nsg-name> --query "[?access=='Allow' && protocol=='Tcp' && destinationPortRange=='22' && destinationAddressPrefix=='*'].name" -o tsv -
Finally, delete the security rule that allows unrestricted SSH access by using the command:
az network nsg rule delete --name <rule-name> --nsg-name <nsg-name>Note: Replace<subscription-id>,<vm-name>,<resource-group-name>,<nsg-name>, and<rule-name>with the actual values from your Azure environment.
Using Python
Using Python
To remediate unrestricted SSH access in Azure using Python, you can follow these steps:This will remediate unrestricted SSH access in Azure by creating a new NSG rule that allows SSH access only from a specific IP range and deleting the old NSG rule that allows unrestricted SSH access.
- Import the necessary libraries:
- Authenticate to Azure using the DefaultAzureCredential:
- Get the network security group (NSG) that needs to be remediated:
- Get the NSG rules and find the rule that allows unrestricted SSH access:
- Create a new NSG rule that allows SSH access only from a specific IP range:
- Delete the old NSG rule that allows unrestricted SSH access:
- Verify that the new NSG rule has been created and the old rule has been deleted:

