freedombone-utils-ssh 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # ssh functions
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. SSH_PORT=2222
  29. # Settings from bettercrypto.org openssh 6.6+
  30. SSH_CIPHERS="chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes128-ctr"
  31. SSH_MACS="hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256"
  32. SSH_KEX="curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1"
  33. SSH_HOST_KEY_ALGORITHMS="ssh-ed25519-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-rsa-cert-v00@openssh.com,ssh-ed25519,ssh-rsa"
  34. function configure_ssh {
  35. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  36. return
  37. fi
  38. if ! grep -q 'RhostsRSAAuthentication' /etc/ssh/sshd_config; then
  39. echo 'RhostsRSAAuthentication yes' >> /etc/ssh/sshd_config
  40. fi
  41. sed -i 's|#RhostsRSAAuthentication.*|RhostsRSAAuthentication yes|g' /etc/ssh/sshd_config
  42. sed -i 's|RhostsRSAAuthentication.*|RhostsRSAAuthentication yes|g' /etc/ssh/sshd_config
  43. sed -i 's|#StrictModes.*|StrictModes yes|g' /etc/ssh/sshd_config
  44. sed -i 's|StrictModes.*|StrictModes yes|g' /etc/ssh/sshd_config
  45. sed -i 's|#KerberosAuthentication.*|KerberosAuthentication no|g' /etc/ssh/sshd_config
  46. sed -i 's|KerberosAuthentication.*|KerberosAuthentication no|g' /etc/ssh/sshd_config
  47. sed -i 's|#GSSAPIAuthentication.*|GSSAPIAuthentication no|g' /etc/ssh/sshd_config
  48. sed -i 's|GSSAPIAuthentication.*|GSSAPIAuthentication no|g' /etc/ssh/sshd_config
  49. sed -i 's|#IgnoreUserKnownHosts.*|IgnoreUserKnownHosts yes|g' /etc/ssh/sshd_config
  50. sed -i 's|IgnoreUserKnownHosts.*|IgnoreUserKnownHosts yes|g' /etc/ssh/sshd_config
  51. sed -i 's|#Compression.*|Compression delayed|g' /etc/ssh/sshd_config
  52. sed -i 's|Compression.*|Compression delayed|g' /etc/ssh/sshd_config
  53. if ! grep -q 'HostbasedAuthentication' /etc/ssh/sshd_config; then
  54. echo 'HostbasedAuthentication no' >> /etc/ssh/sshd_config
  55. fi
  56. sed -i 's|#HostbasedAuthentication.*|HostbasedAuthentication no|g' /etc/ssh/sshd_config
  57. sed -i 's|HostbasedAuthentication.*|HostbasedAuthentication no|g' /etc/ssh/sshd_config
  58. sed -i 's|#PrintLastLog.*|PrintLastLog yes|g' /etc/ssh/sshd_config
  59. sed -i 's|PrintLastLog.*|PrintLastLog yes|g' /etc/ssh/sshd_config
  60. sed -i 's|#IgnoreRhosts.*|IgnoreRhosts yes|g' /etc/ssh/sshd_config
  61. sed -i 's|IgnoreRhosts.*|IgnoreRhosts yes|g' /etc/ssh/sshd_config
  62. sed -i "s/Port .*/Port $SSH_PORT/g" /etc/ssh/sshd_config
  63. sed -i "s/#Port ${SSH_PORT}/Port ${SSH_PORT}/g" /etc/ssh/sshd_config
  64. sed -i 's|#PermitEmptyPasswords.*|PermitEmptyPasswords no|g' /etc/ssh/sshd_config
  65. sed -i 's|PermitEmptyPasswords.*|PermitEmptyPasswords no|g' /etc/ssh/sshd_config
  66. sed -i 's/PermitRootLogin.*/PermitRootLogin no/g' /etc/ssh/sshd_config
  67. sed -i 's/#PermitRootLogin no/PermitRootLogin no/g' /etc/ssh/sshd_config
  68. sed -i 's/X11Forwarding.*/X11Forwarding no/g' /etc/ssh/sshd_config
  69. sed -i 's/#X11Forwarding no/X11Forwarding no/g' /etc/ssh/sshd_config
  70. sed -i 's/ServerKeyBits.*/ServerKeyBits 2048/g' /etc/ssh/sshd_config
  71. sed -i 's/#ServerKeyBits 2048/ServerKeyBits 2048/g' /etc/ssh/sshd_config
  72. sed -i 's/TCPKeepAlive.*/TCPKeepAlive no/g' /etc/ssh/sshd_config
  73. sed -i 's/#TCPKeepAlive no/TCPKeepAlive no/g' /etc/ssh/sshd_config
  74. sed -i 's|HostKey /etc/ssh/ssh_host_dsa_key|#HostKey /etc/ssh/ssh_host_dsa_key|g' /etc/ssh/sshd_config
  75. sed -i 's|HostKey /etc/ssh/ssh_host_ecdsa_key|#HostKey /etc/ssh/ssh_host_ecdsa_key|g' /etc/ssh/sshd_config
  76. sed -i 's|#HostKey /etc/ssh/ssh_host_ed25519_key|HostKey /etc/ssh/ssh_host_ed25519_key|g' /etc/ssh/sshd_config
  77. if ! grep -q 'DebianBanner' /etc/ssh/sshd_config; then
  78. echo 'DebianBanner no' >> /etc/ssh/sshd_config
  79. else
  80. sed -i 's|DebianBanner.*|DebianBanner no|g' /etc/ssh/sshd_config
  81. fi
  82. if grep -q 'ClientAliveInterval' /etc/ssh/sshd_config; then
  83. sed -i 's/ClientAliveInterval.*/ClientAliveInterval 60/g' /etc/ssh/sshd_config
  84. else
  85. echo 'ClientAliveInterval 60' >> /etc/ssh/sshd_config
  86. fi
  87. sed -i 's/#ClientAliveInterval 60/ClientAliveInterval 60/g' /etc/ssh/sshd_config
  88. if grep -q 'ClientAliveCountMax' /etc/ssh/sshd_config; then
  89. sed -i 's/ClientAliveCountMax.*/ClientAliveCountMax 3/g' /etc/ssh/sshd_config
  90. else
  91. echo 'ClientAliveCountMax 3' >> /etc/ssh/sshd_config
  92. fi
  93. sed -i 's/#ClientAliveCountMax 3/ClientAliveCountMax 3/g' /etc/ssh/sshd_config
  94. if grep -q 'Ciphers' /etc/ssh/sshd_config; then
  95. sed -i "s|Ciphers.*|Ciphers $SSH_CIPHERS|g" /etc/ssh/sshd_config
  96. else
  97. echo "Ciphers $SSH_CIPHERS" >> /etc/ssh/sshd_config
  98. fi
  99. sed -i "s|#Ciphers $SSH_CIPHERS|Ciphers $SSH_CIPHERS|g" /etc/ssh/sshd_config
  100. if grep -q 'MACs' /etc/ssh/sshd_config; then
  101. sed -i "s|MACs.*|MACs $SSH_MACS|g" /etc/ssh/sshd_config
  102. else
  103. echo "MACs $SSH_MACS" >> /etc/ssh/sshd_config
  104. fi
  105. sed -i "s|#MACs $SSH_MACS|MACs $SSH_MACS|g" /etc/ssh/sshd_config
  106. if grep -q 'KexAlgorithms' /etc/ssh/sshd_config; then
  107. sed -i "s|KexAlgorithms.*|KexAlgorithms $SSH_KEX|g" /etc/ssh/sshd_config
  108. else
  109. echo "KexAlgorithms $SSH_KEX" >> /etc/ssh/sshd_config
  110. fi
  111. sed -i "s|#KexAlgorithms $SSH_KEX|KexAlgorithms $SSH_KEX|g" /etc/ssh/sshd_config
  112. if ! grep -q 'UsePrivilegeSeparation' /etc/ssh/sshd_config; then
  113. echo 'UsePrivilegeSeparation sandbox' >> /etc/ssh/sshd_config
  114. fi
  115. sed -i 's|#UsePrivilegeSeparation .*|UsePrivilegeSeparation sandbox|g' /etc/ssh/sshd_config
  116. sed -i 's|UsePrivilegeSeparation .*|UsePrivilegeSeparation sandbox|g' /etc/ssh/sshd_config
  117. apt-get -yq install vim-common
  118. function_check configure_firewall_for_ssh
  119. configure_firewall_for_ssh
  120. mark_completed "${FUNCNAME[0]}"
  121. }
  122. # see https://stribika.github.io/2015/01/04/secure-secure-shell.html
  123. function ssh_remove_small_moduli {
  124. awk '$5 > 2000' /etc/ssh/moduli > ~/moduli
  125. mv ~/moduli /etc/ssh/moduli
  126. }
  127. function configure_ssh_client {
  128. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  129. return
  130. fi
  131. #sed -i 's/# PasswordAuthentication.*/ PasswordAuthentication no/g' /etc/ssh/ssh_config
  132. #sed -i 's/# ChallengeResponseAuthentication.*/ ChallengeResponseAuthentication no/g' /etc/ssh/ssh_config
  133. sed -i "s/# HostKeyAlgorithms.*/ HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS/g" /etc/ssh/ssh_config
  134. sed -i "s/# Ciphers.*/ Ciphers $SSH_CIPHERS/g" /etc/ssh/ssh_config
  135. sed -i "s/# MACs.*/ MACs $SSH_MACS/g" /etc/ssh/ssh_config
  136. if ! grep -q "HostKeyAlgorithms" /etc/ssh/ssh_config; then
  137. echo " HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS" >> /etc/ssh/ssh_config
  138. fi
  139. sed -i "s/Ciphers.*/Ciphers $SSH_CIPHERS/g" /etc/ssh/ssh_config
  140. if ! grep -q "Ciphers " /etc/ssh/ssh_config; then
  141. echo " Ciphers $SSH_CIPHERS" >> /etc/ssh/ssh_config
  142. fi
  143. sed -i "s/MACs.*/MACs $SSH_MACS/g" /etc/ssh/ssh_config
  144. if ! grep -q "MACs " /etc/ssh/ssh_config; then
  145. echo " MACs $SSH_MACS" >> /etc/ssh/ssh_config
  146. fi
  147. # Create ssh keys
  148. if [ ! -f ~/.ssh/id_ed25519 ]; then
  149. ssh-keygen -t ed25519 -o -a 100
  150. fi
  151. #if [ ! -f ~/.ssh/id_rsa ]; then
  152. # ssh-keygen -t rsa -b 2048 -o -a 100
  153. #fi
  154. function_check ssh_remove_small_moduli
  155. ssh_remove_small_moduli
  156. mark_completed "${FUNCNAME[0]}"
  157. }
  158. function regenerate_ssh_keys {
  159. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  160. return
  161. fi
  162. rm -f /etc/ssh/ssh_host_*
  163. dpkg-reconfigure openssh-server
  164. function_check ssh_remove_small_moduli
  165. ssh_remove_small_moduli
  166. systemctl restart ssh
  167. mark_completed "${FUNCNAME[0]}"
  168. }
  169. function configure_firewall_for_ssh {
  170. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  171. return
  172. fi
  173. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  174. # docker does its own firewalling
  175. return
  176. fi
  177. firewall_add SSH ${SSH_PORT} tcp
  178. mark_completed "${FUNCNAME[0]}"
  179. }
  180. function get_ssh_server_key {
  181. if [ -f /etc/ssh/ssh_host_rsa_key.pub ]; then
  182. echo "RSA Md5: $(ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub | awk -F ' ' '{print $2}')"
  183. echo "RSA SHA256: $(awk '{print $2}' /etc/ssh/ssh_host_rsa_key.pub | base64 -d | sha256sum -b | awk '{print $1}' | xxd -r -p | base64 | sed 's|=||g')"
  184. fi
  185. if [ -f /etc/ssh/ssh_host_ed25519_key.pub ]; then
  186. echo "ED25519 Md5: $(ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub | awk -F ' ' '{print $2}')"
  187. echo "ED25519 SHA256:$(awk '{print $2}' /etc/ssh/ssh_host_ed25519_key.pub | base64 -d | sha256sum -b | awk '{print $1}' | xxd -r -p | base64 | sed 's|=||g')"
  188. fi
  189. }
  190. # NOTE: deliberately no exit 0