1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#+title: SSH Hardening for Alpine Linux
#+date: 2023-10-16
** Overview
:PROPERTIES:
:CUSTOM_ID: overview
:END:
This guide follows the standard
[[https://www.ssh-audit.com/hardening_guides.html][ssh-audit]] hardening
guide, tweaked for Alpine Linux.
** Hardening Guide
:PROPERTIES:
:CUSTOM_ID: hardening-guide
:END:
These steps must be performed as root. You can try to use =doas= or
=sudo=, but there may be issues.
1. Re-generate the RSA and ED25519 keys
#+begin_src sh
rm /etc/ssh/ssh_host_*
ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key -N ""
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
#+end_src
2. [@2] Remove small Diffie-Hellman moduli
#+begin_src sh
awk '$5 >= 3071' /etc/ssh/moduli > /etc/ssh/moduli.safe
mv /etc/ssh/moduli.safe /etc/ssh/moduli
#+end_src
3. [@3] Enable the RSA and ED25519 HostKey directives in the
/etc/ssh/sshd_config file
#+begin_src sh
sed -i 's/^\#HostKey \/etc\/ssh\/ssh_host_\(rsa\|ed25519\)_key$/HostKey \/etc\/ssh\/ssh_host_\1_key/g' /etc/ssh/sshd_config
#+end_src
4. [@4] Restrict supported key exchange, cipher, and MAC algorithms
#+begin_src sh
echo -e "\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" > /etc/ssh/sshd_config.d/ssh-audit_hardening.conf
#+end_src
5. [@5] Include the /etc/ssh/sshd_config.d directory
#+begin_src sh
echo -e "Include /etc/ssh/sshd_config.d/*.conf" > /etc/ssh/sshd_config
#+end_src
6. [@6] Restart OpenSSH server
#+begin_src sh
rc-service sshd restart
#+end_src
** Testing SSH
:PROPERTIES:
:CUSTOM_ID: testing-ssh
:END:
You can test the results with the =ssh-audit= python script.
#+begin_src sh
pip3 install ssh-audit
ssh-audit localhost
#+end_src
If everything succeeded, the results will show as all green. If anything
is yellow, orange, or red, you may need to tweak additional settings.
#+caption: ssh audit
[[https://img.cleberg.net/blog/20231015-ssh-hardening/ssh-audit.png%20%22ssh-audit%22]]
|