Ensure that your production Microsoft Azure virtual machines are configured to use SSH keys instead of username/password credentials for SSH authentication.
To remediate the misconfiguration “Virtual Machines Should Only Allow SSH Based Authentication” for AZURE using AZURE CLI, please follow the below steps:
Open Azure CLI on your local machine or use the Azure Cloud Shell.
Run the following command to list all the virtual machines in your subscription:
Copy
Ask AI
az vm list --query "[].{Name:name, ResourceGroup:resourceGroup}"
Identify the virtual machine that you want to remediate and note down its name and resource group.
Run the following command to update the network security group of the virtual machine to allow only SSH-based authentication:
Copy
Ask AI
az vm update --name <vm-name> --resource-group <resource-group-name> --set "networkProfile.networkInterfaces[0].ipConfigurations[0].loadBalancerBackendAddressPools=[]" --no-wait
Replace <vm-name> with the name of your virtual machine and <resource-group-name> with the name of the resource group it belongs to.
This command will remove any load balancer backend address pools from the virtual machine’s network interface, which will restrict access to only SSH-based authentication.Note: This command will not affect any other network security groups that the virtual machine may be associated with.
Verify that the remediation is successful by checking the network security group of the virtual machine using the following command:
Copy
Ask AI
az vm show -d --name <vm-name> --resource-group <resource-group-name> --query "networkProfile.networkInterfaces[0].ipConfigurations[0].loadBalancerBackendAddressPools"
This command should return an empty array, indicating that there are no load balancer backend address pools associated with the virtual machine’s network interface.Congratulations, you have successfully remediated the misconfiguration “Virtual Machines Should Only Allow SSH Based Authentication” for AZURE using AZURE CLI.
Using Python
To remediate the misconfiguration “Virtual Machines Should Only Allow SSH Based Authentication” in Azure using Python, you can use the Azure Python SDK to update the Network Security Group (NSG) rules for the virtual machine. Here are the steps to follow:
Install the Azure Python SDK using pip:
Copy
Ask AI
pip install azure-mgmt-compute
Authenticate to your Azure account using the Azure CLI or by setting environment variables for your Azure credentials.
This code creates a new NSG rule that only allows inbound TCP traffic on port 22 (SSH) and deletes all other rules. The updated NSG is then created or updated in Azure.Note that this code assumes that the virtual machine is already configured to use SSH-based authentication. If not, you will need to configure SSH-based authentication on the virtual machine before updating the NSG rules.