<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>The Wombelix Post - Hardening</title><link href="https://dominik.wombacher.cc/" rel="alternate"/><link href="/feeds/tag_hardening.atom.xml" rel="self"/><id>https://dominik.wombacher.cc/</id><updated>2022-03-18T00:00:00+01:00</updated><entry><title>FreeBSD 13 Base System OpenSSH Server Hardening</title><link href="https://dominik.wombacher.cc/posts/freebsd_13_base_system_openssh_server_hardening.html" rel="alternate"/><published>2022-03-17T00:00:00+01:00</published><updated>2022-03-18T00:00:00+01:00</updated><author><name>Dominik Wombacher</name></author><id>tag:dominik.wombacher.cc,2022-03-17:/posts/freebsd_13_base_system_openssh_server_hardening.html</id><summary type="html">&lt;!-- SPDX-FileCopyrightText: 2023 Dominik Wombacher &lt;dominik@wombacher.cc&gt; --&gt;
&lt;!--  --&gt;
&lt;!-- SPDX-License-Identifier: CC-BY-SA-4.0 --&gt;
&lt;p&gt;Default sshd configs tend to focus more on compatibility instead security. Therefore hardening
should be one of the first things after setup a new system.&lt;/p&gt;
&lt;p&gt;I'm using the OpenSSH Daemon  ... &lt;a class="read-more" href="/posts/freebsd_13_base_system_openssh_server_hardening.html"&gt; [read more]&lt;/a&gt;&lt;/p&gt;</summary><content type="html">&lt;!-- SPDX-FileCopyrightText: 2023 Dominik Wombacher &lt;dominik@wombacher.cc&gt; --&gt;
&lt;!--  --&gt;
&lt;!-- SPDX-License-Identifier: CC-BY-SA-4.0 --&gt;
&lt;p&gt;Default sshd configs tend to focus more on compatibility instead security. Therefore hardening
should be one of the first things after setup a new system.&lt;/p&gt;
&lt;p&gt;I'm using the OpenSSH Daemon which comes with the FreeBSD Base System.
When you want to use the &lt;code&gt;openssh-portable&lt;/code&gt; port instead, skip &lt;em&gt;Step 3&lt;/em&gt;, the rest should be identical.&lt;/p&gt;
&lt;p&gt;Step 1) Delete existing host keys, generate new rsa and ed25519 key:&lt;/p&gt;
&lt;pre class="code bash literal-block"&gt;
rm&lt;span class="pygments-w"&gt; &lt;/span&gt;/etc/ssh/ssh_host_*&lt;span class="pygments-w"&gt;
&lt;/span&gt;ssh-keygen&lt;span class="pygments-w"&gt; &lt;/span&gt;-q&lt;span class="pygments-w"&gt; &lt;/span&gt;-t&lt;span class="pygments-w"&gt; &lt;/span&gt;rsa&lt;span class="pygments-w"&gt; &lt;/span&gt;-b&lt;span class="pygments-w"&gt; &lt;/span&gt;&lt;span class="pygments-m"&gt;4096&lt;/span&gt;&lt;span class="pygments-w"&gt; &lt;/span&gt;-f&lt;span class="pygments-w"&gt; &lt;/span&gt;ssh_host_rsa_key&lt;span class="pygments-w"&gt; &lt;/span&gt;-N&lt;span class="pygments-w"&gt; &lt;/span&gt;&lt;span class="pygments-s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="pygments-w"&gt;
&lt;/span&gt;ssh-keygen&lt;span class="pygments-w"&gt; &lt;/span&gt;-q&lt;span class="pygments-w"&gt; &lt;/span&gt;-t&lt;span class="pygments-w"&gt; &lt;/span&gt;ed25519&lt;span class="pygments-w"&gt; &lt;/span&gt;-f&lt;span class="pygments-w"&gt; &lt;/span&gt;ssh_host_ed25519_key&lt;span class="pygments-w"&gt; &lt;/span&gt;-N&lt;span class="pygments-w"&gt; &lt;/span&gt;&lt;span class="pygments-s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="pygments-w"&gt;
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Step 2) Create new Diffie-Hellman groups and avoid small moduli&lt;/p&gt;
&lt;pre class="code bash literal-block"&gt;
ssh-keygen&lt;span class="pygments-w"&gt; &lt;/span&gt;-G&lt;span class="pygments-w"&gt; &lt;/span&gt;moduli-3072.candidates&lt;span class="pygments-w"&gt; &lt;/span&gt;-b&lt;span class="pygments-w"&gt; &lt;/span&gt;&lt;span class="pygments-m"&gt;3072&lt;/span&gt;&lt;span class="pygments-w"&gt;
&lt;/span&gt;ssh-keygen&lt;span class="pygments-w"&gt; &lt;/span&gt;-T&lt;span class="pygments-w"&gt; &lt;/span&gt;moduli-3072&lt;span class="pygments-w"&gt; &lt;/span&gt;-f&lt;span class="pygments-w"&gt; &lt;/span&gt;moduli-3072.candidates&lt;span class="pygments-w"&gt;
&lt;/span&gt;mv&lt;span class="pygments-w"&gt; &lt;/span&gt;moduli-3072&lt;span class="pygments-w"&gt; &lt;/span&gt;/etc/ssh/moduli&lt;span class="pygments-w"&gt;
&lt;/span&gt;rm&lt;span class="pygments-w"&gt; &lt;/span&gt;moduli-3072.candidates&lt;span class="pygments-w"&gt;
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Step 3) Disable DSA and ECDSA host keys, only use RSA and ED25519&lt;/p&gt;
&lt;pre class="code bash literal-block"&gt;
sysrc&lt;span class="pygments-w"&gt; &lt;/span&gt;&lt;span class="pygments-nv"&gt;sshd_dsa_enable&lt;/span&gt;&lt;span class="pygments-o"&gt;=&lt;/span&gt;&lt;span class="pygments-s2"&gt;&amp;quot;NO&amp;quot;&lt;/span&gt;&lt;span class="pygments-w"&gt;
&lt;/span&gt;sysrc&lt;span class="pygments-w"&gt; &lt;/span&gt;&lt;span class="pygments-nv"&gt;sshd_ecdsa_enable&lt;/span&gt;&lt;span class="pygments-o"&gt;=&lt;/span&gt;&lt;span class="pygments-s2"&gt;&amp;quot;NO&amp;quot;&lt;/span&gt;&lt;span class="pygments-w"&gt;
&lt;/span&gt;sysrc&lt;span class="pygments-w"&gt; &lt;/span&gt;&lt;span class="pygments-nv"&gt;sshd_ed25519_enable&lt;/span&gt;&lt;span class="pygments-o"&gt;=&lt;/span&gt;&lt;span class="pygments-s2"&gt;&amp;quot;YES&amp;quot;&lt;/span&gt;&lt;span class="pygments-w"&gt;
&lt;/span&gt;sysrc&lt;span class="pygments-w"&gt; &lt;/span&gt;&lt;span class="pygments-nv"&gt;sshd_rsa_enable&lt;/span&gt;&lt;span class="pygments-o"&gt;=&lt;/span&gt;&lt;span class="pygments-s2"&gt;&amp;quot;YES&amp;quot;&lt;/span&gt;&lt;span class="pygments-w"&gt;
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Step 4) Optimize &lt;code&gt;/etc/ssh/sshd_config&lt;/code&gt;, improve security, restrict allowed key exchange, cipher and MAC algorithms&lt;/p&gt;
&lt;pre class="code text literal-block"&gt;
# Hardening
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
KexAlgorithms curve25519-sha256,curve25519-sha256&amp;#64;libssh.org,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256
Ciphers chacha20-poly1305&amp;#64;openssh.com,aes256-gcm&amp;#64;openssh.com,aes128-gcm&amp;#64;openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
MACs hmac-sha2-256-etm&amp;#64;openssh.com,hmac-sha2-512-etm&amp;#64;openssh.com,umac-128-etm&amp;#64;openssh.com
HostKeyAlgorithms ssh-ed25519,ssh-ed25519-cert-v01&amp;#64;openssh.com

# Security
PermitRootLogin no
AuthenticationMethods publickey
ChallengeResponseAuthentication no
UsePAM no
VersionAddendum none
X11Forwarding no
AuthorizedKeysFile .ssh/authorized_keys
Subsystem sftp /usr/libexec/sftp-server

&lt;/pre&gt;
&lt;p&gt;Run &lt;code&gt;service sshd restart&lt;/code&gt; to apply the new settings, to verify the results of your hardening,
you can use the CLI Tool &lt;a class="reference external" href="https://github.com/jtesta/ssh-audit"&gt;ssh-audit&lt;/a&gt;
which is also available as &lt;a class="reference external" href="https://www.ssh-audit.com"&gt;Online Version&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Following a Custom Policy for ssh-audit based on the above recommendations:&lt;/p&gt;
&lt;pre class="code text literal-block"&gt;
name = &amp;quot;Custom Policy - FreeBSD 13 Base System OpenSSH Daemon (2022/03/17)&amp;quot;
version = 1
dh_modulus_size_diffie-hellman-group-exchange-sha256 = 2048
host keys = ssh-ed25519
key exchanges = curve25519-sha256, curve25519-sha256&amp;#64;libssh.org, diffie-hellman-group16-sha512, diffie-hellman-group18-sha512, diffie-hellman-group-exchange-sha256
ciphers = chacha20-poly1305&amp;#64;openssh.com, aes256-gcm&amp;#64;openssh.com, aes128-gcm&amp;#64;openssh.com, aes256-ctr, aes192-ctr, aes128-ctr
macs = hmac-sha2-256-etm&amp;#64;openssh.com, hmac-sha2-512-etm&amp;#64;openssh.com, umac-128-etm&amp;#64;openssh.com

&lt;/pre&gt;
&lt;p&gt;Copy the policy into a file and run &lt;code&gt;ssh-audit -P=&amp;lt;policy.txt&amp;gt; &amp;lt;servername&amp;gt;&lt;/code&gt;.
The result should be similar to following example without warnings or errors:&lt;/p&gt;
&lt;pre class="code text literal-block"&gt;
# general
(gen) banner: SSH-2.0-OpenSSH_8.8
(gen) software: OpenSSH 8.8
(gen) compatibility: OpenSSH 7.4+, Dropbear SSH 2018.76+
(gen) compression: enabled (zlib&amp;#64;openssh.com)

# key exchange algorithms
(kex) curve25519-sha256                     -- [info] available since OpenSSH 7.4, Dropbear SSH 2018.76
(kex) curve25519-sha256&amp;#64;libssh.org          -- [info] available since OpenSSH 6.5, Dropbear SSH 2013.62
(kex) diffie-hellman-group16-sha512         -- [info] available since OpenSSH 7.3, Dropbear SSH 2016.73
(kex) diffie-hellman-group18-sha512         -- [info] available since OpenSSH 7.3
(kex) diffie-hellman-group-exchange-sha256 (2048-bit) -- [info] available since OpenSSH 4.4

# host-key algorithms
(key) ssh-ed25519                           -- [info] available since OpenSSH 6.5

# encryption algorithms (ciphers)
(enc) chacha20-poly1305&amp;#64;openssh.com         -- [info] available since OpenSSH 6.5
                                            `- [info] default cipher since OpenSSH 6.9.
(enc) aes256-gcm&amp;#64;openssh.com                -- [info] available since OpenSSH 6.2
(enc) aes128-gcm&amp;#64;openssh.com                -- [info] available since OpenSSH 6.2
(enc) aes256-ctr                            -- [info] available since OpenSSH 3.7, Dropbear SSH 0.52
(enc) aes192-ctr                            -- [info] available since OpenSSH 3.7
(enc) aes128-ctr                            -- [info] available since OpenSSH 3.7, Dropbear SSH 0.52

# message authentication code algorithms
(mac) hmac-sha2-256-etm&amp;#64;openssh.com         -- [info] available since OpenSSH 6.2
(mac) hmac-sha2-512-etm&amp;#64;openssh.com         -- [info] available since OpenSSH 6.2
(mac) umac-128-etm&amp;#64;openssh.com              -- [info] available since OpenSSH 6.2

# fingerprints
(fin) ssh-ed25519: SHA256:uN9Oton+VmLL793KirVFB+ilD3Bndra4I/3yFntgX8k

# algorithm recommendations (for OpenSSH 8.8)
(rec) +diffie-hellman-group14-sha256        -- kex algorithm to append
(rec) +rsa-sha2-256                         -- key algorithm to append
(rec) +rsa-sha2-512                         -- key algorithm to append

&lt;/pre&gt;
&lt;p&gt;Sources:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://ozgurkazancci.com/ssh-server-security-audit-hardening-freebsd"&gt;https://ozgurkazancci.com/ssh-server-security-audit-hardening-freebsd&lt;/a&gt;
(Archive: &lt;a class="reference external" href="https://web.archive.org/web/20220311055553/https://ozgurkazancci.com/ssh-server-security-audit-hardening-freebsd/"&gt;[1]&lt;/a&gt;,
&lt;a class="reference external" href="https://archive.today/2022.03.17-211815/https://ozgurkazancci.com/ssh-server-security-audit-hardening-freebsd/"&gt;[2]&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://gist.github.com/koobs/e01cf8869484a095605404cd0051eb11"&gt;https://gist.github.com/koobs/e01cf8869484a095605404cd0051eb11&lt;/a&gt;
(Archive: &lt;a class="reference external" href="https://web.archive.org/web/20220313140130/https://gist.github.com/koobs/e01cf8869484a095605404cd0051eb11"&gt;[1]&lt;/a&gt;,
&lt;a class="reference external" href="https://archive.today/2021.09.16-082343/https://gist.github.com/koobs/e01cf8869484a095605404cd0051eb11"&gt;[2]&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://www.ssh-audit.com/hardening_guides.html"&gt;https://www.ssh-audit.com/hardening_guides.html&lt;/a&gt;
(Archive: &lt;a class="reference external" href="https://web.archive.org/web/20220113100935/https://www.ssh-audit.com/hardening_guides.html"&gt;[1]&lt;/a&gt;,
&lt;a class="reference external" href="https://archive.today/2022.03.17-211934/https://www.ssh-audit.com/hardening_guides.html"&gt;[2]&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
</content><category term="Unix"/><category term="FreeBSD"/><category term="OpenSSH"/><category term="Hardening"/><category term="SSH"/></entry></feed>