Fail2ban 防止暴力破解centos服务器的SSH或者FTP账户

时间:2023-03-09 03:58:03
Fail2ban 防止暴力破解centos服务器的SSH或者FTP账户
第一次将CentOS服务器配上外网可访问的网络配置,发现每次通过外网地址登陆ssh,链接时间长,同时爆出以下信息,显示从上一次登陆成功到本次登陆成功,中间有3896次尝试登陆root账户失败的情况。【说明服务器被攻击了】


Last failed login: Sat Mar 19 09:34:18 CST 2016 from 222.186.15.82 on ssh:notty
There were 3896 failed login attempts since the last successful login.
Last login: Sat Mar 19 09:12:33 2016 from 117.114.129.166

基于CentOS 6或者7版本的系统,我们可以安装Fail2ban工具来阻止一定的暴力破解SSH或者FTP账户问题,也许不能足够的解决问题,但至少可以解决一般的问题。

第一、Fail2ban安装

A - CentOS 6

rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum install fail2ban

B - CentOS 7

rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-2.noarch.rpm
yum install fail2ban

[说明]执行以上第一句,若找不到是因为版本更新后,旧版本就删除了。所有打开“http://dl.fedoraproject.org/pub/epel/7/x86_64/e/”找找相应的rpm

第二、Fail2ban设置

(1)各配置文件用途

  1. /etc/fail2ban/action.d            #动作文件夹,内含默认文件。iptables以及mail等动作配置
  2. /etc/fail2ban/fail2ban.conf        #定义了fai2ban日志级别、日志位置及sock文件位置
  3. /etc/fail2ban/filter.d            #条件文件夹,内含默认文件。过滤日志关键内容设置
  4. /etc/fail2ban/jail.conf            #主要配置文件,模块化。主要设置启用ban动作的服务及动作阀值
  5. /etc/rc.d/init.d/fail2ban          #启动脚本文件


(2)、fail2ban.conf一般不用改,再确认以下几项:
  1. vi /etc/fail2ban/fail2ban.conf
  2. [Definition] 
  3. loglevel =3 
  4. logtarget = SYSLOG  #我们需要做的就是把这行改成/var/log/fail2ban.log,方便用来记录日志信息
  5. socket =/var/run/fail2ban/fail2ban.sock

(3)编辑/etc/fail2b    an/jail.conf文件,修改或添加防攻击规则。(后面有规则的举例)
【特别说明】 
   (1)“[INCLUDES]”以前的不要动(都是注释),否则fail2ban服务就无法启动。可以用命令来检查语法错误 #fail2ban-client -v -v start
(2)“[DEFAULT]”里的 “enabled = false”不能改为true,否则fail2ban服务就无法启动。
  1. centos Job for fail2ban.service failed because the control process exited with error code. See "systemctl status fail2ban.service" and "journalctl -xe" for details


第三、重启Fail2ban服务,立即生效。
systemctl restart fail2ban.service
第四、防攻击规则,举例
关键词
  1. [DEFAULT]               #全局设置
  2. ignoreip = 127.0.0.1       #忽略的IP列表,不受设置限制,多个以空格隔开
  3. bantime  = 600             #屏蔽时间,单位:秒
  4. findtime  = 600             #这个时间段内超过规定次数会被ban掉
  5. maxretry = 3                #最大尝试次数
  6. backend = auto            #日志修改检测机制(gamin、polling和auto这三种)
  7. [sshd]                   #单个服务检查设置,如设置bantime、findtime、maxretry和全局冲突,服务优先级大于全局设置。
  8. enabled  = true             #是否激活此项(true/false)
  9. filter   = sshd              #过滤规则filter的名字,对应filter.d目录下的sshd.conf
  10. action   = iptables[name=SSH, port=ssh, protocol=tcp]#动作的相关参数,对应action.d/iptables.conf文件
  11. logpath  = /var/log/secure         #检测的日志文件path
  12. bantime  = 3600
  13. findtime  = 300
  14. maxretry = 3

防攻击规则参见:
(1)SSH防攻击规则
  1. [ssh-iptables]
  2. enabled  = true
  3. filter   = sshd
  4. action   = iptables[name=SSH, port=ssh, protocol=tcp]
  5.            sendmail-whois[name=SSH, dest=root, sender=fail2ban@example.com, sendername="Fail2Ban"]
  6. logpath  = /var/log/secure
  7. maxretry = 5
  8. [ssh-ddos]
  9. enabled = true
  10. filter  = sshd-ddos
  11. action  = iptables[name=ssh-ddos, port=ssh,sftp protocol=tcp,udp]
  12. logpath  = /var/log/messages
  13. maxretry = 2
  14. [osx-ssh-ipfw]
  15. enabled  = true
  16. filter   = sshd
  17. action   = osx-ipfw
  18. logpath  = /var/log/secure.log
  19. maxretry = 5
  20. [ssh-apf]
  21. enabled = true
  22. filter  = sshd
  23. action  = apf[name=SSH]
  24. logpath = /var/log/secure
  25. maxretry = 5
  26. [osx-ssh-afctl]
  27. enabled  = true
  28. filter   = sshd
  29. action   = osx-afctl[bantime=600]
  30. logpath  = /var/log/secure.log
  31. maxretry = 5
  32. [selinux-ssh]
  33. enabled = true
  34. filter  = selinux-ssh
  35. action  = iptables[name=SELINUX-SSH, port=ssh, protocol=tcp]
  36. logpath  = /var/log/audit/audit.log
  37. maxretry = 5
(2)proftp防攻击规则
  1. [proftpd-iptables]
  2. enabled  = true
  3. filter   = proftpd
  4. action   = iptables[name=ProFTPD, port=ftp, protocol=tcp]
  5.            sendmail-whois[name=ProFTPD, dest=you@example.com]
  6. logpath  = /var/log/proftpd/proftpd.log
  7. maxretry = 6

(3)邮件防攻击规则
  1. [sasl-iptables]
  2. enabled  = true
  3. filter   = postfix-sasl
  4. backend  = polling
  5. action   = iptables[name=sasl, port=smtp, protocol=tcp]
  6.            sendmail-whois[name=sasl, dest=you@example.com]
  7. logpath  = /var/log/mail.log
  8. [dovecot]
  9. enabled = true
  10. filter  = dovecot
  11. action  = iptables-multiport[name=dovecot, port="pop3,pop3s,imap,imaps,submission,smtps,sieve", protocol=tcp]
  12. logpath = /var/log/mail.log
  13. [dovecot-auth]
  14. enabled = true
  15. filter  = dovecot
  16. action  = iptables-multiport[name=dovecot-auth, port="pop3,pop3s,imap,imaps,submission,smtps,sieve", protocol=tcp]
  17. logpath = /var/log/secure
  18. [perdition]
  19. enabled = true
  20. filter  = perdition
  21. action  = iptables-multiport[name=perdition,port="110,143,993,995"]
  22. logpath = /var/log/maillog
  23. [uwimap-auth]
  24. enabled = true
  25. filter  = uwimap-auth
  26. action  = iptables-multiport[name=uwimap-auth,port="110,143,993,995"]
  27. logpath = /var/log/maillog
(4)apache防攻击规则
  1. [apache-tcpwrapper]
  2. enabled  = true
  3. filter  = apache-auth
  4. action   = hostsdeny
  5. logpath  = /var/log/httpd/error_log
  6. maxretry = 6
  7. [apache-badbots]
  8. enabled  = true
  9. filter   = apache-badbots
  10. action   = iptables-multiport[name=BadBots, port="http,https"]
  11.            sendmail-buffered[name=BadBots, lines=5, dest=you@example.com]
  12. logpath  = /var/log/httpd/access_log
  13. bantime  = 172800
  14. maxretry = 1
  15. [apache-shorewall]
  16. enabled  = true
  17. filter   = apache-noscript
  18. action   = shorewall
  19.            sendmail[name=Postfix, dest=you@example.com]
  20. logpath  = /var/log/httpd/error_log
(5)nginx防攻击规则
  1. [nginx-http-auth]
  2. enabled = true
  3. filter  = nginx-http-auth
  4. action  = iptables-multiport[name=nginx-http-auth,port="80,443"]
  5. logpath = /var/log/nginx/error.log
(6)lighttpd防规击规则
  1. [suhosin]
  2. enabled  = true
  3. filter   = suhosin
  4. action   = iptables-multiport[name=suhosin, port="http,https"]
  5. # adapt the following two items as needed
  6. logpath  = /var/log/lighttpd/error.log
  7. maxretry = 2
  8. [lighttpd-auth]
  9. enabled  = true
  10. filter   = lighttpd-auth
  11. action   = iptables-multiport[name=lighttpd-auth, port="http,https"]
  12. # adapt the following two items as needed
  13. logpath  = /var/log/lighttpd/error.log
  14. maxretry = 2
(7)vsftpd防攻击规则
  1. [vsftpd-notification]
  2. enabled  = true
  3. filter   = vsftpd
  4. action   = sendmail-whois[name=VSFTPD, dest=you@example.com]
  5. logpath  = /var/log/vsftpd.log
  6. maxretry = 5
  7. bantime  = 1800
  8. [vsftpd-iptables]
  9. enabled  = true
  10. filter   = vsftpd
  11. action   = iptables[name=VSFTPD, port=ftp, protocol=tcp]
  12.            sendmail-whois[name=VSFTPD, dest=you@example.com]
  13. logpath  = /var/log/vsftpd.log
  14. maxretry = 5
  15. bantime  = 1800
(8)pure-ftpd防攻击规则
  1. [pure-ftpd]
  2. enabled  = true
  3. filter   = pure-ftpd
  4. action   = iptables[name=pure-ftpd, port=ftp, protocol=tcp]
  5. logpath  = /var/log/pureftpd.log
  6. maxretry = 2
  7. bantime  = 86400
(9)mysql防攻击规则
  1. [mysqld-iptables]
  2. enabled  = true
  3. filter   = mysqld-auth
  4. action   = iptables[name=mysql, port=3306, protocol=tcp]
  5.            sendmail-whois[name=MySQL, dest=root, sender=fail2ban@example.com]
  6. logpath  = /var/log/mysqld.log
  7. maxretry = 5
(10)apache phpmyadmin防攻击规则
  1. [apache-phpmyadmin]
  2. enabled  = true
  3. filter   = apache-phpmyadmin
  4. action  = iptables[name=phpmyadmin, port=http,https protocol=tcp]
  5. logpath  = /var/log/httpd/error_log
  6. maxretry = 3
  7. # /etc/fail2ban/filter.d/apache-phpmyadmin.conf
  8. 将以下内容粘贴到apache-phpmyadmin.conf里保存即可以创建一个apache-phpmyadmin.conf文件.
  9. # Fail2Ban configuration file
  10. #
  11. # Bans bots scanning for non-existing phpMyAdmin installations on your webhost.
  12. #
  13. # Author: Gina Haeussge
  14. #
  15. [Definition]
  16. docroot = /var/www
  17. badadmin = PMA|phpmyadmin|myadmin|mysql|mysqladmin|sqladmin|mypma|admin|xampp|mysqldb|mydb|db|pmadb|phpmyadmin1|phpmyadmin2
  18. # Option:  failregex
  19. # Notes.:  Regexp to match often probed and not available phpmyadmin paths.
  20. # Values:  TEXT
  21. #
  22. failregex = [[]client []] File does not exist: %(docroot)s/(?:%(badadmin)s)
  23. # Option:  ignoreregex
  24. # Notes.:  regex to ignore. If this regex matches, the line is ignored.
  25. # Values:  TEXT
  26. #
  27. ignoreregex =
  28. # service fail2ban restart
第五、新建防攻击规则
 以nginx为例,具体设置方法如下:
(1)首先在jail.conf文件下追加以下内容:
  1. [nginx]    ;规则名字
  2. enabled = true ;是否户用
  3. port = http,https ;监控端口
  4. filter = nginx    ;需要过滤匹配规则
  5. logpath = /var/log/nginx/access_log; 日志路径
  6. findtime =60    ;检测周期 单位秒 以下一样
  7. bantime =300    ;iptable封禁IP时间
  8. maxretry =10    ;最大尝试次数
  9. action = iptables[name=nginx, port=http, protocal=tcp] ;发现暴力破解采取iptalbes封禁IP的措施
  10.          sendmail[name=nginx, dest=my-email@xx.com]    ;发现暴力破解后采取sendmail发送邮件的措施,需要注意的是:iptables和sendmail必须对齐,要不然会发生错误;不要问我为什么会知道。
(2)然后创建 /etc/fail2ban/filter.d/nginx.conf文件,并添加以下内容:
  1. [Definition]
  2. failregex =<HOST>.*-.*-.*POST.*/login_check.do.* HTTP\/1.*http://test.com.*$ ;需要匹配日志发现攻击行为的正则,<HOST>为fail2ban内置变量匹配IP,不可修改
  3. ignoreregex =    ;需要忽略的正则
(3)systemctl restart fail2ban 立即生效。
第六、查询限制列表   iptables -L --line-numbers
红色的表示已经由fail2ban限制了。
  1. [root@bogon ~]#  iptables -L --line-numbers
  2. Chain INPUT (policy ACCEPT)
  3. num  target     prot opt source               destination         
  4. 1    f2b-SSH    tcp  --  anywhere             anywhere             tcp dpt:ssh
  5. 2    f2b-SSH    tcp  --  anywhere             anywhere             tcp dpt:ssh
  6. 3    ACCEPT     udp  --  anywhere             anywhere             udp dpt:domain
  7. 4    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:domain
  8. 5    ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootps
  9. 6    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:bootps
  10. 7    ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
  11. 8    ACCEPT     all  --  anywhere             anywhere            
  12. 9    INPUT_direct  all  --  anywhere             anywhere            
  13. 10   INPUT_ZONES_SOURCE  all  --  anywhere             anywhere            
  14. 11   INPUT_ZONES  all  --  anywhere             anywhere            
  15. 12   ACCEPT     icmp --  anywhere             anywhere            
  16. 13   REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
  17. Chain f2b-SSH (2 references)
  18. num  target     prot opt source               destination         
  19. 1    REJECT     all  --  58.218.211.38        anywhere             reject-with icmp-port-unreachable
  20. 2    RETURN     all  --  anywhere             anywhere            
  21. You have new mail in /var/spool/mail/root

第七、解除fail2ban绑定的IP

查询限制列表
  1. # iptables -L --line-numbers
  2. Chain f2b-SSH (2 references)
  3. num  target     prot opt source               destination         
  4. 1    REJECT     all  --  58.218.211.38        anywhere             reject-with icmp-port-unreachable
  5. 2    RETURN     all  --  anywhere             anywhere    
解除限制
  1. # iptables -D f2b-SSH 1
  2. 上面是解除第一个拒绝的IP

第八、统计登陆失败的IP
  1. [root@bogon ~]# find /var/log -name 'secure*' -type f | while read line;do awk '/Failed/{print $(NF-3)}' $line;done | awk '{a[$0]++}END{for (j in a) if(a[j] > 20) print j"="a[j]}' | sort -n -t'=' -k 2
  2. 202.99.172.155=24
  3. 123.126.110.69=25
  4. 222.74.228.85=136
  5. 5.189.139.232=557
  6. 222.178.229.67=1005
  7. 222.186.15.82=1883
  8. 59.63.188.44=6748
  9. 58.218.211.38=6752
  10. [root@bogon ~]# 
有上面几个IP尝试通过不同的端口攻击我的服务器
另外给root账户设置24位随机密码:
  1. rootpass=`date +%s | sha256sum | base64 | head -c 24` && echo root:$rootpass  | chpasswd && echo $rootpass