freedombone-utils-ssh 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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@robotics.uk.to>
  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. # ssh (from https://stribika.github.io/2015/01/04/secure-secure-shell.html)
  32. SSH_CIPHERS="chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr"
  33. SSH_MACS="hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-ripemd160-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256,hmac-ripemd160,umac-128@openssh.com"
  34. SSH_KEX="curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256"
  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 grep -Fxq "configure_ssh" $COMPLETION_FILE; then
  38. return
  39. fi
  40. sed -i "s/Port .*/Port $SSH_PORT/g" /etc/ssh/sshd_config
  41. sed -i 's/PermitRootLogin.*/PermitRootLogin no/g' /etc/ssh/sshd_config
  42. sed -i 's/X11Forwarding.*/X11Forwarding no/g' /etc/ssh/sshd_config
  43. sed -i 's/ServerKeyBits.*/ServerKeyBits 4096/g' /etc/ssh/sshd_config
  44. sed -i 's/TCPKeepAlive.*/TCPKeepAlive no/g' /etc/ssh/sshd_config
  45. sed -i 's|HostKey /etc/ssh/ssh_host_dsa_key|#HostKey /etc/ssh/ssh_host_dsa_key|g' /etc/ssh/sshd_config
  46. sed -i 's|HostKey /etc/ssh/ssh_host_ecdsa_key|#HostKey /etc/ssh/ssh_host_ecdsa_key|g' /etc/ssh/sshd_config
  47. if ! grep -q 'DebianBanner' /etc/ssh/sshd_config; then
  48. echo 'DebianBanner no' >> /etc/ssh/sshd_config
  49. else
  50. sed -i 's|DebianBanner.*|DebianBanner no|g' /etc/ssh/sshd_config
  51. fi
  52. if grep -q 'ClientAliveInterval' /etc/ssh/sshd_config; then
  53. sed -i 's/ClientAliveInterval.*/ClientAliveInterval 60/g' /etc/ssh/sshd_config
  54. else
  55. echo 'ClientAliveInterval 60' >> /etc/ssh/sshd_config
  56. fi
  57. if grep -q 'ClientAliveCountMax' /etc/ssh/sshd_config; then
  58. sed -i 's/ClientAliveCountMax.*/ClientAliveCountMax 3/g' /etc/ssh/sshd_config
  59. else
  60. echo 'ClientAliveCountMax 3' >> /etc/ssh/sshd_config
  61. fi
  62. if grep -q 'Ciphers' /etc/ssh/sshd_config; then
  63. sed -i "s|Ciphers.*|Ciphers $SSH_CIPHERS|g" /etc/ssh/sshd_config
  64. else
  65. echo "Ciphers $SSH_CIPHERS" >> /etc/ssh/sshd_config
  66. fi
  67. if grep -q 'MACs' /etc/ssh/sshd_config; then
  68. sed -i "s|MACs.*|MACs $SSH_MACS|g" /etc/ssh/sshd_config
  69. else
  70. echo "MACs $SSH_MACS" >> /etc/ssh/sshd_config
  71. fi
  72. if grep -q 'KexAlgorithms' /etc/ssh/sshd_config; then
  73. sed -i "s|KexAlgorithms.*|KexAlgorithms $SSH_KEX|g" /etc/ssh/sshd_config
  74. else
  75. echo "KexAlgorithms $SSH_KEX" >> /etc/ssh/sshd_config
  76. fi
  77. apt-get -y install fail2ban
  78. function_check configure_firewall_for_ssh
  79. configure_firewall_for_ssh
  80. echo 'configure_ssh' >> $COMPLETION_FILE
  81. }
  82. # see https://stribika.github.io/2015/01/04/secure-secure-shell.html
  83. function ssh_remove_small_moduli {
  84. awk '$5 > 2000' /etc/ssh/moduli > ~/moduli
  85. mv ~/moduli /etc/ssh/moduli
  86. }
  87. function configure_ssh_client {
  88. if grep -Fxq "configure_ssh_client" $COMPLETION_FILE; then
  89. return
  90. fi
  91. #sed -i 's/# PasswordAuthentication.*/ PasswordAuthentication no/g' /etc/ssh/ssh_config
  92. #sed -i 's/# ChallengeResponseAuthentication.*/ ChallengeResponseAuthentication no/g' /etc/ssh/ssh_config
  93. sed -i "s/# HostKeyAlgorithms.*/ HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS/g" /etc/ssh/ssh_config
  94. sed -i "s/# Ciphers.*/ Ciphers $SSH_CIPHERS/g" /etc/ssh/ssh_config
  95. sed -i "s/# MACs.*/ MACs $SSH_MACS/g" /etc/ssh/ssh_config
  96. if ! grep -q "HostKeyAlgorithms" /etc/ssh/ssh_config; then
  97. echo " HostKeyAlgorithms $SSH_HOST_KEY_ALGORITHMS" >> /etc/ssh/ssh_config
  98. fi
  99. sed -i "s/Ciphers.*/Ciphers $SSH_CIPHERS/g" /etc/ssh/ssh_config
  100. if ! grep -q "Ciphers " /etc/ssh/ssh_config; then
  101. echo " Ciphers $SSH_CIPHERS" >> /etc/ssh/ssh_config
  102. fi
  103. sed -i "s/MACs.*/MACs $SSH_MACS/g" /etc/ssh/ssh_config
  104. if ! grep -q "MACs " /etc/ssh/ssh_config; then
  105. echo " MACs $SSH_MACS" >> /etc/ssh/ssh_config
  106. fi
  107. # Create ssh keys
  108. if [ ! -f ~/.ssh/id_ed25519 ]; then
  109. ssh-keygen -t ed25519 -o -a 100
  110. fi
  111. if [ ! -f ~/.ssh/id_rsa ]; then
  112. ssh-keygen -t rsa -b 4096 -o -a 100
  113. fi
  114. function_check ssh_remove_small_moduli
  115. ssh_remove_small_moduli
  116. echo 'configure_ssh_client' >> $COMPLETION_FILE
  117. }
  118. function regenerate_ssh_keys {
  119. if grep -Fxq "regenerate_ssh_keys" $COMPLETION_FILE; then
  120. return
  121. fi
  122. rm -f /etc/ssh/ssh_host_*
  123. dpkg-reconfigure openssh-server
  124. function_check ssh_remove_small_moduli
  125. ssh_remove_small_moduli
  126. systemctl restart ssh
  127. echo 'regenerate_ssh_keys' >> $COMPLETION_FILE
  128. }
  129. # NOTE: deliberately no exit 0