fail2ban.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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=http://mirrors.linuxeye.com/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. sed -i 's@for i in xrange(50)@for i in range(50)@' fail2ban/__init__.py
  16. ${python_install_dir}/bin/python setup.py install
  17. /bin/cp build/fail2ban.service /lib/systemd/system/
  18. systemctl enable fail2ban
  19. [ -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`
  20. if [ "${PM}" == 'yum' ]; then
  21. cat > /etc/fail2ban/jail.local << EOF
  22. [DEFAULT]
  23. ignoreip = 127.0.0.1/8
  24. bantime = 86400
  25. findtime = 600
  26. maxretry = 5
  27. backend = auto
  28. banaction = firewallcmd-ipset
  29. action = %(action_mwl)s
  30. [sshd]
  31. enabled = true
  32. filter = sshd
  33. port = ${now_ssh_port}
  34. action = %(action_mwl)s
  35. logpath = /var/log/secure
  36. bantime = 86400
  37. findtime = 600
  38. maxretry = 5
  39. EOF
  40. elif [ "${PM}" == 'apt-get' ]; then
  41. if ufw status | grep -wq inactive; then
  42. ufw default allow incoming
  43. ufw --force enable
  44. fi
  45. cat > /etc/fail2ban/jail.local << EOF
  46. [DEFAULT]
  47. ignoreip = 127.0.0.1/8
  48. bantime = 86400
  49. findtime = 600
  50. maxretry = 5
  51. backend = auto
  52. banaction = ufw
  53. action = %(action_mwl)s
  54. [sshd]
  55. enabled = true
  56. filter = sshd
  57. port = ${now_ssh_port}
  58. action = %(action_mwl)s
  59. logpath = /var/log/auth.log
  60. bantime = 86400
  61. findtime = 600
  62. maxretry = 5
  63. EOF
  64. fi
  65. cat > /etc/logrotate.d/fail2ban << EOF
  66. /var/log/fail2ban.log {
  67. missingok
  68. notifempty
  69. postrotate
  70. ${python_install_dir}/bin/fail2ban-client flushlogs >/dev/null || true
  71. endscript
  72. }
  73. EOF
  74. kill -9 `ps -ef | grep fail2ban | grep -v grep | awk '{print $2}'` > /dev/null 2>&1
  75. systemctl start fail2ban
  76. popd > /dev/null
  77. if [ -e "${python_install_dir}/bin/fail2ban-server" ]; then
  78. echo; echo "${CSUCCESS}fail2ban installed successfully! ${CEND}"
  79. else
  80. echo; echo "${CFAILURE}fail2ban install failed, Please try again! ${CEND}"
  81. fi
  82. popd > /dev/null
  83. }
  84. Uninstall_fail2ban() {
  85. service fail2ban stop
  86. ${python_install_dir}/bin/pip uninstall -y fail2ban > /dev/null 2>&1
  87. rm -rf /etc/init.d/fail2ban /etc/fail2ban /etc/logrotate.d/fail2ban /var/log/fail2ban.* /var/run/fail2ban
  88. echo; echo "${CMSG}fail2ban uninstall completed${CEND}";
  89. }