Eliminate Password-Based Attacks on Azure Linux VMs
Published Apr 15 2021 01:49 PM 13.2K Views
Microsoft

Yanelis Lopez, Security Software Engineer, Cloud & AI Security Green Team

 

Refer to this link for the latest guidance for securing Linux VMs.

 

A common tactic we observe used by adversaries against customers running Linux Virtual Machines (VMs) in Azure is password-based attacks. This article will explain how to help protect Linux VMs in Azure from these types of attacks at every step of the deployment pipeline.

 

Flow chart picturing a password being used on multiple machines and working with one. This is a simplified depiction of a password spray attack.Flow chart picturing a password being used on multiple machines and working with one. This is a simplified depiction of a password spray attack.

 

Many tools exist that are commonly used to brute force user account passwords over SSH, including those used in cases of Linux brute force compromise in Azure. A specific example is password spray attacks, where an attacker uses one or a few passwords against multiple well-known user accounts in an environment. These attacks typically attempt common passwords hoping one or more accounts can be compromised across multiple virtual machines. Attackers limit the number of password guess attempts to avoid account lockout and detection by defenders.


Although mitigating controls such as password complexity, routine password rotations, disallowing common passwords, etc. help, using password-less authentication is the safer approach.


On Linux, authentication via SSH can be password-less, using SSH key-based authentication instead. SSH keys, are non-human generated, inherently unique, and significantly harder to be brute forced or guessed.


Below, we will show you how to use SSH key-based authentication pre-deployment, enforce it during deployment, and detect non-compliance post-deployment.


Pre-Deployment – Secure from the start: Creating Linux virtual machines

In Azure, there are a few methods for creating Linux VMs: via the Azure Portal, Azure CLI, Powershell, or Azure Resource Manager (ARM) Templates. All have the ability (and document how) to deploy a Linux VM with SSH key as a default option for authentication. To update an ARM template that uses password authentication to instead use SSH keys, follow the below steps.


How to transform your ARM template to use SSH key

Replace the admin password parameter with the 'adminSSHKey' parameter

"parameters": {
...
"adminUsername": {
"type": "string",
"metadata": {
"description": "Username for the Virtual Machine."
}
},
"adminPassword": {
"type": "securestring",
"metadata": {
"description": "Admin password on all VMs."
}
},
"adminSSHKey": {
"type": "securestring",
"metadata": {
"description": "SSH Key for the Virtual Machine."
}
}
...
}

Next, add the ‘linuxConfiguration’ property to the variables of the template

"variables": {
...
"linuxConfiguration": {
"disablePasswordAuthentication": true,
"ssh": {
"publicKeys": [
{
"path": "[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]",
"keyData": "[parameters('adminSSHKey')]"
}
]
}
},
...
}

Finally, add the ‘linuxConfiguration’ property to the VM resource in the template. In the ‘osProfile’ property remove the 'adminPassword' property and add the following ‘linuxConfiguration’ property

"properties": {
...
"osProfile": {
...
"adminPassword": "[parameters('adminPassword')]"
"linuxConfiguration": "[variables('linuxConfiguration')]"
}
...
}

Now that the template is updated to use SSH key authentication, generate a SSH key using the following command

ssh-keygen -m PEM -t rsa -b 4096

Check out this documentation for more information about generating SSH keys on Windows.


Pass the public key of the SSH key as the value for the ‘adminSSHKey’ parameter described above. Now all VMs deployed using these transformed ARM templates will be safer from password-based attacks.


You can find many examples of ARM templates that deploy VMs with SSH key-based authentication online. When searching for ARM Template examples for Linux VMs, one of the top results is the Azure Quickstart GitHub Repo. These templates are used as starting points for deployments by many individuals. Almost 200 templates in this 800-template repo, deploy a VM or VMSS and previously had SSH authentication configured as password only, making VMs deployed with these templates vulnerable to password-based attacks. We updated these sample templates to disable password-based authentication by default, adding SSH key as the default option.


At Deployment – SSH key enforcer: Using Azure Policy

As part of Microsoft’s quest to get rid of passwords, we have published an Azure Policy which helps ensure Azure Linux VMs use SSH key authentication instead of passwords. You can deploy this policy to your subscription or management group to prevent creation of Linux VMs with password as the SSH authentication type. As seen below, this policy blocks the deployment if the ‘disablePasswordAuthentication’ property in a VM is not defined or is set to ‘false’ when the VM image publisher and offer are well-known Linux offerings. After deploying this policy, any new deployments which include a Linux VM with password-based authentication fail.

 

"policyRule": {
  "if": {
    "allOf": [
      {
        "field": "type",
        "equals": "Microsoft.Compute/virtualMachines"
      },
      {
        "anyof": [
          {
            "field": "Microsoft.Compute/virtualMachines/osProfile.linuxConfiguration.disablePasswordAuthentication",
            "exists": "False"
          },
          {
            "field": "Microsoft.Compute/virtualMachines/osProfile.linuxConfiguration.disablePasswordAuthentication",
            "equals": "false"
          }
        ]
      },
      {
        "anyOf": [
          {
            "allOf": [
              {
                "field": "Microsoft.Compute/imagePublisher",
                "equals": "Canonical"
              },
              {
                "field": "Microsoft.Compute/imageOffer",
                "in": [
                  "UbuntuServer",
                  "Ubuntu_Core"
                ]
              }
            ]
          },
          ...
          {
            "allOf": [
              {
                "field": "Microsoft.Compute/imagePublisher",
                "equals": "RedHat"
              },
              {
                "field": "Microsoft.Compute/imageOffer",
                "in": [
                  "osa",
                  "RHEL",
                  "rhel-byos",
                  "rhel-ocp-marketplace",
                  "RHEL-SAP",
                  "RHEL-SAP-APPS",
                  "RHEL-SAP-HANA"
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  "then": {
    "effect": "deny"
  }
}

 

Post-Deployment – Staying Secure: The Microsoft Defender for Cloud Recommendation

We have discussed solutions for new deployments, but what if you have VMs already deployed which use password-based SSH authentication? A new Microsoft Defender for Cloud recommendation, “Authentication to Linux machines should require SSH keys”, checks for the use of SSH authentication after a VM has been deployed. This recommendation is part of the ASC Secure Score* and follows Azure Security Benchmark best practices. The recommendation is a built-in Guest Configuration policy that monitors changes made to the SSH password authentication setting on the machine itself; if a VM was originally deployed with SSH key-based authentication and later password-based authentication is enabled in the SSH settings, this recommendation will report the VM as non-compliant.


For the recommendation to run, the Guest Configuration extension must be installed. You can follow this documentation for installing the prerequisite Guest Configuration extension. There is also a Defender for Cloud recommendation suggesting downloading the extension that includes a Quick Fix capability named “Guest Configuration extension should be installed on your machines”. It can be found under the “Manage access and permissions” control. Likewise, the Linux SSH policy recommendation can be found under the “Manage access and permissions” control. The figure below shows this Linux SSH policy recommendation:

ASC Linux SSH Recommendation - Screenshot of the recommendation in the Azure Security Center blade in the Azure Portal. You can find the recommendation by searching for "ssh" in the search bar.ASC Linux SSH Recommendation - Screenshot of the recommendation in the Azure Security Center blade in the Azure Portal. You can find the recommendation by searching for "ssh" in the search bar.

 

The policy can also be found in the virtual machine resource under the Policies blade. This can be useful for understanding the Compliance state, Compliance reason, and Last evaluated time. The figure below shows a Non-compliant machine using password authentication:

ASC Linux SSH Policy - Screenshot of the Azure Portal showing the Compliance information for the ASC recommendation. This example shows a VM as non-complaint for this recommendation.ASC Linux SSH Policy - Screenshot of the Azure Portal showing the Compliance information for the ASC recommendation. This example shows a VM as non-complaint for this recommendation.

 

To remediate this recommendation, you must add an SSH key to the non-compliant VM and disable password authentication by following the below steps.

  1. SSH into the existing VM
  2. Copy the SSH public key from your host into ~/.ssh/authorized_keys
  3. Edit /etc/ssh/sshd_config (with sudo) and update the value of "PasswordAuthentication" to "no".
  4. Restart the SSH service on the VM

Azure Active Directory (Azure AD) alternative

An alternative to SSH keys is using Azure Active Directory (Azure AD) authentication. This allows for the use of Azure AD credentials to log in to Azure Linux VMs; this feature is currently in public preview.


Conclusion

Password-based attacks on Azure Linux VMs is an active problem, but solutions are available to help protect against these attacks. We’ve demonstrated how to fix ARM templates to use SSH Key instead of passwords, how to deploy a security gate using Azure Policy which disallows Linux VMs that use passwords, and how to detect and remediate existing VMs that have password authentication enabled. Together, these recommendations provide a solid defense against password-based attacks on Azure Linux VMs. Take action and do all you can to be as secure as possible!

 

Footnotes:

*The policy does not impact the Defender for Cloud Secure Score during public preview. Secure Score will only be impacted when the policy becomes Generally Available (GA).

 

Reviewers

Johnathon Mohr, Security Software Engineer, Cloud & AI Security Green Team

4 Comments
Co-Authors
Version history
Last update:
‎May 05 2022 01:53 PM
Updated by: