freedombone-utils-ssh 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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-2016 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
  32. SSH_CIPHERS="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,hmac-ripemd160"
  34. SSH_KEX="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. sed -i "s/Port .*/Port $SSH_PORT/g" /etc/ssh/sshd_config
  41. sed -i "s/#Port ${SSH_PORT}/Port ${SSH_PORT}/g" /etc/ssh/sshd_config
  42. sed -i 's/PermitRootLogin.*/PermitRootLogin no/g' /etc/ssh/sshd_config
  43. sed -i 's/#PermitRootLogin no/PermitRootLogin no/g' /etc/ssh/sshd_config
  44. sed -i 's/X11Forwarding.*/X11Forwarding no/g' /etc/ssh/sshd_config
  45. sed -i 's/#X11Forwarding no/X11Forwarding no/g' /etc/ssh/sshd_config
  46. sed -i 's/ServerKeyBits.*/ServerKeyBits 4096/g' /etc/ssh/sshd_config
  47. sed -i 's/#ServerKeyBits 4096/ServerKeyBits 4096/g' /etc/ssh/sshd_config
  48. sed -i 's/TCPKeepAlive.*/TCPKeepAlive no/g' /etc/ssh/sshd_config
  49. sed -i 's/#TCPKeepAlive no/TCPKeepAlive no/g' /etc/ssh/sshd_config
  50. sed -i 's|HostKey /etc/ssh/ssh_host_dsa_key|#HostKey /etc/ssh/ssh_host_dsa_key|g' /etc/ssh/sshd_config
  51. sed -i 's|HostKey /etc/ssh/ssh_host_ecdsa_key|#HostKey /etc/ssh/ssh_host_ecdsa_key|g' /etc/ssh/sshd_config
  52. if ! grep -q 'DebianBanner' /etc/ssh/sshd_config; then
  53. echo 'DebianBanner no' >> /etc/ssh/sshd_config
  54. else
  55. sed -i 's|DebianBanner.*|DebianBanner no|g' /etc/ssh/sshd_config
  56. fi
  57. if grep -q 'ClientAliveInterval' /etc/ssh/sshd_config; then
  58. sed -i 's/ClientAliveInterval.*/ClientAliveInterval 60/g' /etc/ssh/sshd_config
  59. else
  60. echo 'ClientAliveInterval 60' >> /etc/ssh/sshd_config
  61. fi
  62. sed -i 's/#ClientAliveInterval 60/ClientAliveInterval 60/g' /etc/ssh/sshd_config
  63. if grep -q 'ClientAliveCountMax' /etc/ssh/sshd_config; then
  64. sed -i 's/ClientAliveCountMax.*/ClientAliveCountMax 3/g' /etc/ssh/sshd_config
  65. else
  66. echo 'ClientAliveCountMax 3' >> /etc/ssh/sshd_config
  67. fi
  68. sed -i 's/#ClientAliveCountMax 3/ClientAliveCountMax 3/g' /etc/ssh/sshd_config
  69. if grep -q 'Ciphers' /etc/ssh/sshd_config; then
  70. sed -i "s|Ciphers.*|Ciphers $SSH_CIPHERS|g" /etc/ssh/sshd_config
  71. else
  72. echo "Ciphers $SSH_CIPHERS" >> /etc/ssh/sshd_config
  73. fi
  74. sed -i "s|#Ciphers $SSH_CIPHERS|Ciphers $SSH_CIPHERS|g" /etc/ssh/sshd_config
  75. if grep -q 'MACs' /etc/ssh/sshd_config; then
  76. sed -i "s|MACs.*|MACs $SSH_MACS|g" /etc/ssh/sshd_config
  77. else
  78. echo "MACs $SSH_MACS" >> /etc/ssh/sshd_config
  79. fi
  80. sed -i "s|#MACs $SSH_MACS|MACs $SSH_MACS|g" /etc/ssh/sshd_config
  81. if grep -q 'KexAlgorithms' /etc/ssh/sshd_config; then
  82. sed -i "s|KexAlgorithms.*|KexAlgorithms $SSH_KEX|g" /etc/ssh/sshd_config
  83. else
  84. echo "KexAlgorithms $SSH_KEX" >> /etc/ssh/sshd_config
  85. fi
  86. sed -i "s|#KexAlgorithms $SSH_KEX|KexAlgorithms $SSH_KEX|g" /etc/ssh/sshd_config
  87. apt-get -yq install fail2ban vim-common
  88. function_check configure_firewall_for_ssh
  89. configure_firewall_for_ssh
  90. mark_completed $FUNCNAME
  91. }
  92. # see https://stribika.github.io/2015/01/04/secure-secure-shell.html
  93. function ssh_remove_small_moduli {
  94. awk '$5 > 2000' /etc/ssh/moduli > ~/moduli
  95. mv ~/moduli /etc/ssh/moduli
  96. }
  97. function configure_ssh_client {
  98. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  99. return
  100. fi
  101. #sed -i 's/# PasswordAuthentication.*/ PasswordAuthentication no/g' /etc/ssh/ssh_config
  102. #sed -i 's/# ChallengeResponseAuthentication.*/ ChallengeResponseAuthentication no/g' /etc/ssh/ssh_config
  103. sed -i "s/# HostKeyAlgorithms.*/ HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS/g" /etc/ssh/ssh_config
  104. sed -i "s/# Ciphers.*/ Ciphers $SSH_CIPHERS/g" /etc/ssh/ssh_config
  105. sed -i "s/# MACs.*/ MACs $SSH_MACS/g" /etc/ssh/ssh_config
  106. if ! grep -q "HostKeyAlgorithms" /etc/ssh/ssh_config; then
  107. echo " HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS" >> /etc/ssh/ssh_config
  108. fi
  109. sed -i "s/Ciphers.*/Ciphers $SSH_CIPHERS/g" /etc/ssh/ssh_config
  110. if ! grep -q "Ciphers " /etc/ssh/ssh_config; then
  111. echo " Ciphers $SSH_CIPHERS" >> /etc/ssh/ssh_config
  112. fi
  113. sed -i "s/MACs.*/MACs $SSH_MACS/g" /etc/ssh/ssh_config
  114. if ! grep -q "MACs " /etc/ssh/ssh_config; then
  115. echo " MACs $SSH_MACS" >> /etc/ssh/ssh_config
  116. fi
  117. # Create ssh keys
  118. if [ ! -f ~/.ssh/id_ed25519 ]; then
  119. ssh-keygen -t ed25519 -o -a 100
  120. fi
  121. if [ ! -f ~/.ssh/id_rsa ]; then
  122. ssh-keygen -t rsa -b 4096 -o -a 100
  123. fi
  124. function_check ssh_remove_small_moduli
  125. ssh_remove_small_moduli
  126. mark_completed $FUNCNAME
  127. }
  128. function regenerate_ssh_keys {
  129. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  130. return
  131. fi
  132. rm -f /etc/ssh/ssh_host_*
  133. dpkg-reconfigure openssh-server
  134. function_check ssh_remove_small_moduli
  135. ssh_remove_small_moduli
  136. systemctl restart ssh
  137. mark_completed $FUNCNAME
  138. }
  139. function configure_firewall_for_ssh {
  140. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  141. return
  142. fi
  143. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  144. # docker does its own firewalling
  145. return
  146. fi
  147. firewall_add SSH ${SSH_PORT} tcp
  148. mark_completed $FUNCNAME
  149. }
  150. function get_ssh_server_key {
  151. if [ -f /etc/ssh/ssh_host_rsa_key.pub ]; then
  152. echo "RSA Md5: $(ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub | awk -F ' ' '{print $2}')"
  153. 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')"
  154. fi
  155. if [ -f /etc/ssh/ssh_host_ed25519_key.pub ]; then
  156. echo "ED25519 Md5: $(ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub | awk -F ' ' '{print $2}')"
  157. 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')"
  158. fi
  159. }
  160. # NOTE: deliberately no exit 0