How to Protect Your OpenSSH Server from the Remote Code Execution Vulnerability Before Authentication
Recently, a critical vulnerability has been discovered in OpenSSH that allows remote code execution before authentication. This means an attacker could compromise your server without valid credentials if the SSH port is open to the internet. This tutorial will guide you through the steps to identify whether your system is vulnerable, how to apply the necessary mitigations, and verify that your server is protected.
1. Identify the OpenSSH Version on Your Server
The first step is to determine which version of OpenSSH is running on your server. The vulnerability affects specific versions, so knowing your version is crucial.
To do this, connect to your server and run the following command:
ssh -VThis will show you the version of the SSH client you are using. To get the version of the SSH server (sshd), which is the one we care about, you can try the following (although not all systems expose it directly, package information is usually more reliable):
dpkg -l | grep openssh-server # For Debian/Ubuntu-based systems
rpm -qa | grep openssh-server # For RedHat/CentOS-based systemsNote: Vulnerable versions are typically earlier than those that include the patch. It is essential to consult the security advisories for your specific distribution to know the exact affected versions.
2. Update OpenSSH to the Latest Patched Version
The most effective way to protect your server is to update OpenSSH to a version that includes the patch for this vulnerability. Linux distribution maintainers usually release updates quickly for this type of critical flaw.
For Debian/Ubuntu-based systems:
sudo apt update
sudo apt upgrade openssh-server
sudo systemctl restart sshdFor RedHat/CentOS-based systems:
sudo yum update openssh-server
sudo systemctl restart sshdImportant: Make sure the openssh-server package is updated to a version that your distribution's security advisories have confirmed fixes the vulnerability. In some cases, you may need to restart the entire server if there are kernel or system dependencies that require it, although a simple restart of the sshd service is usually enough.
3. Implement Additional Mitigation Measures (Firewall and Restrictions)
Although updating is the main solution, it is always good practice to implement additional layers of security. Restricting access to your SSH server to only trusted IP addresses is a fundamental security measure.
Configure a firewall (e.g., UFW on Ubuntu):
sudo ufw enable
sudo ufw default deny incoming
sudo ufw allow from TU_IP_PUBLICA_O_RANGO_DE_IP_CONFIABLE to any port 22
sudo ufw status verboseReplace TU_IP_PUBLICA_O_RANGO_DE_IP_CONFIABLE with the IP address or range of IPs from which you will connect. If you need access from multiple locations, you can add multiple allow rules.
Configure a firewall (e.g., firewalld on CentOS/RHEL):
sudo systemctl start firewalld
sudo systemctl enable firewalld
sudo firewall-cmd --permanent --add-source=TU_IP_PUBLICA_O_RANGO_DE_IP_CONFIABLE --add-service=ssh
sudo firewall-cmd --reload4. Verify Server Protection
After updating and applying the mitigations, it is crucial to verify that your server is protected. Although we cannot test the vulnerability directly without exploiting it, we can confirm that the OpenSSH version is correct and that the firewall rules are active.
First, check the OpenSSH version again:
ssh -VConfirm that the version is the patched one. Then, check the status of your firewall:
sudo ufw status verbose # For UFW
sudo firewall-cmd --list-all # For firewalldMake sure that only allowed IPs can access port 22. If you try to connect from a non-allowed IP, the connection should be rejected.
Finally, you can use a tool like nmap from another machine (not from the server) to confirm that port 22 is open only to the expected IPs:
nmap -p 22 TU_IP_DEL_SERVIDORIf port 22 appears as filtered or closed from unauthorized IPs, your firewall is working correctly. If it appears as open from an unauthorized IP, review your firewall rules.