freedombone-utils-ssh 9.5KB

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