FreeBSD 13 Base System OpenSSH Server Security Hardening

Sshd configurations by default tend to focus more on compatibility than security. Therefore Hardening is the main choice and must be prepared to improve the SSH security system. Because by default the OpenSSH system aggressively stops problematic algorithms. None of the algorithms enabled by default have any known security issues. Although most of the default OpenSSH settings related to security provide good security, they still have many drawbacks.

On a FreeBSD system, when you first install OpenSSH, it is included in the default FreeBSD repository. You just need to enable SSH in the /etc/rc.conf file. So in general, if you use FreeBSD, there is no need to install OpenSSH. However, unfortunately the OpenSSH application built into FreeBSD is believed to be less powerful in protecting the system from hacker attacks, so it needs to be hardened. To harden OpenSSH on FreeBSD, you must update OpenSSH to the latest version. Read our article about installing OpenSSH on FreeBSD.




This article will guide you through the Hardening process in OpenSSH. To follow this article, you are required to read the previous article, because the Hardening process requires FreeBSD's built-in SSH update.

OK, let's assume you have read the previous article and have updated OpenSSH to the latest version. The first step to perform Hardening is to install SSH Audit.

root@ns1:~ # cd /usr/ports/security/py-ssh-audit
root@ns1:/usr/ports/security/py-ssh-audit #
make install clean


1. OpenSSH Hardening Server

After the SSH Audit installation process is complete, continue by activating OpenSSH and SSH Audit.

root@ns1:~ # service openssh enable
root@ns1:~ #
service openssh restart
root@ns1:~ #
uname -a > ssh-audit.out
root@ns1:~ #
echo "# before hardening" >> ssh-audit.out
root@ns1:~ #
ssh-audit --no-colors localhost >> ssh-audit.out || true

Next, delete existing key-pairs and disable DSA & ECDSA.

root@ns1:~ # rm -f /usr/local/etc/ssh/ssh_host_*
root@ns1:~ #
sysrc sshd_dsa_enable="no"
root@ns1:~ #
sysrc sshd_ecdsa_enable="no"
root@ns1:~ #
sysrc sshd_ed25519_enable="yes"
root@ns1:~ #
sysrc sshd_rsa_enable="yes"

After that Regenerate RSA and Ed25519 keys.

root@ns1:~ # ssh-keygen -t rsa -b 4096 -f /usr/local/etc/ssh/ssh_host_rsa_key -N ""
Generating public/private rsa key pair.
/usr/local/etc/ssh/ssh_host_rsa_key already exists.
Overwrite (y/n)? y
Your identification has been saved in /usr/local/etc/ssh/ssh_host_rsa_key
Your public key has been saved in /usr/local/etc/ssh/ssh_host_rsa_key.pub
The key fingerprint is:
SHA256:M6NxMgV83tETp7n7bOOIAWJ9ra1HSZj68YuhO8xykfY root@ns1
The key's randomart image is:
+---[RSA 4096]----+
| .. .... |
| ... . o+ |
| o.. +o. |
| o. +... |
| * So..o. |
| . B=*.oo. |
| .+ +++o |
| . =.E*o+o |
| ooo+.o++. |
+----[SHA256]-----+

root@ns1:~ # ssh-keygen -t ed25519 -f /usr/local/etc/ssh/ssh_host_ed25519_key -N ""
Generating public/private ed25519 key pair.
/usr/local/etc/ssh/ssh_host_ed25519_key already exists.
Overwrite (y/n)? y
Your identification has been saved in /usr/local/etc/ssh/ssh_host_ed25519_key
Your public key has been saved in /usr/local/etc/ssh/ssh_host_ed25519_key.pub
The key fingerprint is:
SHA256:clHsvJFkxp8vcy2JUFzH9ffuEImjVgLlndMKgTrrSQs root@ns1
The key's randomart image is:
+--[ED25519 256]--+
| ++. ...+|
| .+*ooo .o|
| .o*o++.. o|
| o o*.o+ .o|
| .oS .+=ooo.|
| E oo .++.=o.|
| + o o +...|
| + . o |
| .|
+----[SHA256]-----+



Then you continue by deleting Diffie-Hellman moduli smaller than 3071. The script below applies if you already have a moduli file in the /usr/local/etc/ssh folder. Try checking in that folder to see if there is a moduli file in the /usr/local/etc/ssh folder, if there is, run the script below.

root@ns1:~ # awk '$5 >= 3072' /usr/local/etc/ssh/moduli > /etc/ssh/moduli.safe
root@ns1:~ #
mv /usr/local/etc/ssh/moduli.safe /etc/ssh/moduli

If the moduli file is not available in the /usr/local/etc/ssh folder, you can create a key for the moduli file. Below is how to create a key for the moduli file.

root@ns1:/usr/local/etc/ssh # /usr/local/bin/ssh-keygen -M generate -O bits=3072 moduli
Tue Jul 25 16:29:22 2023 Sieve next 150896640 plus 3071-bit
Tue Jul 25 16:38:58 2023 Sieved with 203277289 small primes in 576 seconds
Tue Jul 25 16:39:07 2023 Found 127434 candidates


root@ns1:/usr/local/etc/ssh # /usr/local/bin/ssh-keygen -M screen -f moduli moduli-final
Tue Jul 25 16:49:39 2023 processed 12698 of 127434 (9%) in 0:05, ETA 0:45
Tue Jul 25 16:54:39 2023 processed 25203 of 127434 (19%) in 0:10, ETA 0:40
Tue Jul 25 16:59:39 2023 processed 37404 of 127434 (29%) in 0:15, ETA 0:36
Tue Jul 25 17:04:39 2023 processed 49979 of 127434 (39%) in 0:20, ETA 0:30
Tue Jul 25 17:09:39 2023 processed 62716 of 127434 (49%) in 0:25, ETA 0:25
Tue Jul 25 17:14:39 2023 processed 75377 of 127434 (59%) in 0:30, ETA 0:20
Tue Jul 25 17:19:39 2023 processed 88223 of 127434 (69%) in 0:35, ETA 0:15
Tue Jul 25 17:24:39 2023 processed 99937 of 127434 (78%) in 0:40, ETA 0:11
Tue Jul 25 17:29:39 2023 processed 112008 of 127434 (87%) in 0:45, ETA 0:06
Tue Jul 25 17:34:39 2023 processed 124967 of 127434 (98%) in 0:50, ETA 0:00
Tue Jul 25 17:35:36 2023 Found 30 safe primes of 106029 candidates in 3056 seconds


The two processes above require quite a long time to generate the key, so you have to be patient and wait until the generation process is complete.

The two scripts above will create a new moduli file. Most users do not change this moduli file. By default, the moduli file has a very short hash of 2048 bits, making it very vulnerable to compromise and making it easier for attackers to compromise. Ideally the minimum file hash moduli is 3072 bits which will provide 128 bits of security.

Once everything is configured, we will now limit the key exchange, cipher, and MAC algorithms supported by OpenSSH.

root@ns1:~ # printf "\n# Restrict key exchange, cipher, and MAC algorithms, as per sshaudit.com\n# hardening guide.\nKexAlgorithms sntrup761x25519-sha512@openssh.com,curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256\nCiphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr\nMACs hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,umac-128-etm@openssh.com\nHostKeyAlgorithms ssh-ed25519,ssh-ed25519-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256,rsa-sha2-256-cert-v01@openssh.com\n" >> /usr/local/etc/ssh/sshd_config


Now we restart openssh and ssh-audit.

root@ns1:~ # service openssh restart
root@ns1:~ #
echo "# after hardening" >> ssh-audit.out
root@ns1:~ #
ssh-audit --no-colors localhost >> ssh-audit.out



2. OpenSSH Hardening Client

OpenSSH Client is processed sequentially and checked for any available configuration settings. There are 3 ways to configure OpenSSH Client.
  1. Via the Command Line command line.
  2. The OpenSSH Client configuration file in the home folder is located at /root/.ssh/config or you can also create a folder elsewhere such as /usr/home/.ssh/config.
  3. The configuration files for all users are located in the /usr/local/etc/ssh/ssh_config folder.

After knowing and determining the location of the OpenSSH clinet folder, now we will carry out the Hardening process. We assume the OpenSSH client folder is located at /root/.ssh. Use the following script to Hardening OpenSSH Client.

root@ns1:~ # mkdir -p -m 0700 ~/.ssh ; printf "\nHost *\n  Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr\n  KexAlgorithms sntrup761x25519-sha512@openssh.com,curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256\n  MACs hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,umac-128-etm@openssh.com\n  HostKeyAlgorithms ssh-ed25519,ssh-ed25519-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,rsa-sha2-256,rsa-sha2-256-cert-v01@openssh.com,rsa-sha2-512,rsa-sha2-512-cert-v01@openssh.com\n" >> ~/.ssh/config


By implementing all the steps above, you will be able to improve the security of OpenSSH, so that the FreeBSD system becomes more secure and difficult to hack. The method above is not one way to improve OpenSSH security, there are still other ways such as using VPN, Proxy and others.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post