fail2ban.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/oneinstack
  10. Install_fail2ban() {
  11. pushd ${oneinstack_dir}/src > /dev/null
  12. src_url=${mirror_link}/oneinstack/src/fail2ban-${fail2ban_ver}.tar.gz && Download_src
  13. tar xzf fail2ban-${fail2ban_ver}.tar.gz
  14. pushd fail2ban-${fail2ban_ver} > /dev/null
  15. if command -v python3 > /dev/null 2>&1; then
  16. python3 setup.py install
  17. else
  18. python setup.py install
  19. fi
  20. /bin/cp build/fail2ban.service /lib/systemd/system/
  21. systemctl enable fail2ban
  22. [ -z "`grep ^Port /etc/ssh/sshd_config`" ] && now_ssh_port=22 || now_ssh_port=`grep ^Port /etc/ssh/sshd_config | awk '{print $2}' | head -1`
  23. if [ "${PM}" == 'yum' ]; then
  24. cat > /etc/fail2ban/jail.local << EOF
  25. [DEFAULT]
  26. ignoreip = 127.0.0.1/8
  27. bantime = 86400
  28. findtime = 600
  29. maxretry = 5
  30. backend = auto
  31. banaction = firewallcmd-ipset
  32. action = %(action_mwl)s
  33. [sshd]
  34. enabled = true
  35. filter = sshd
  36. port = ${now_ssh_port}
  37. action = %(action_mwl)s
  38. logpath = /var/log/secure
  39. bantime = 86400
  40. findtime = 600
  41. maxretry = 5
  42. EOF
  43. elif [ "${PM}" == 'apt-get' ]; then
  44. if ufw status | grep -wq inactive; then
  45. ufw default allow incoming
  46. ufw --force enable
  47. fi
  48. cat > /etc/fail2ban/jail.local << EOF
  49. [DEFAULT]
  50. ignoreip = 127.0.0.1/8
  51. bantime = 86400
  52. findtime = 600
  53. maxretry = 5
  54. backend = auto
  55. banaction = ufw
  56. action = %(action_mwl)s
  57. [sshd]
  58. enabled = true
  59. filter = sshd
  60. port = ${now_ssh_port}
  61. action = %(action_mwl)s
  62. logpath = /var/log/auth.log
  63. bantime = 86400
  64. findtime = 600
  65. maxretry = 5
  66. EOF
  67. fi
  68. cat > /etc/logrotate.d/fail2ban << EOF
  69. /var/log/fail2ban.log {
  70. missingok
  71. notifempty
  72. postrotate
  73. /usr/local/bin/fail2ban-client flushlogs >/dev/null || true
  74. endscript
  75. }
  76. EOF
  77. kill -9 `ps -ef | grep fail2ban | grep -v grep | awk '{print $2}'` > /dev/null 2>&1
  78. systemctl start fail2ban
  79. popd > /dev/null
  80. if [ -e "/usr/local/bin/fail2ban-server" ]; then
  81. echo; echo "${CSUCCESS}fail2ban installed successfully! ${CEND}"
  82. else
  83. echo; echo "${CFAILURE}fail2ban install failed, Please try again! ${CEND}"
  84. fi
  85. popd > /dev/null
  86. }
  87. Uninstall_fail2ban() {
  88. systemctl stop fail2ban
  89. systemctl disable fail2ban
  90. rm -rf /usr/local/bin/fail2ban* /etc/init.d/fail2ban /etc/fail2ban /etc/logrotate.d/fail2ban /var/log/fail2ban.* /var/run/fail2ban /lib/systemd/system/fail2ban.service
  91. echo; echo "${CMSG}fail2ban uninstall completed${CEND}";
  92. }