fail2ban(IP自动屏蔽工具)


2022年4月1日 16:59     admin

注意

fail2ban停止或重启后,会默认删除它此次创建的iptables规则

  1. systemctl stop fail2ban

安装

  1. yum -y install fail2ban

修改配置

  1. vim /etc/fail2ban/jail.conf

参数 说明
maxretry 在findtime参数指定的时间内最大错误连接数
findtime 日志文件中分析错误连接数的时间。(默认以秒为单位,10m为10分钟,10h为10小时)
bantime 封禁时间(默认以秒为单位,10m为10分钟,10h为10小时),-1是永久屏蔽
ignoreip 白名单
ignorecommand 不封堵的IP地址段,使用空格分隔,例如:192.168.0.0/24 192.168.1.0/24

SSH封禁例子

  1. [ssh-iptables]
  2. enabled = true
  3. filter = sshd
  4. #过滤规则 filter 名称,对应 filter.d 目录下的 sshd.conf
  5. action = iptables[name=SSH, port=22, protocol=tcp]
  6. action = iptables[name=SSH, port=2201:2219, protocol=tcp]
  7. #执行语句,多端口段使用":"连接。
  8. action = mail-whois[name=SSH, dest=wujiaqiang@wxchina.com, sender=cpaas@wxchina.com]
  9. iptables[name=SSH,port=2233, protocol=tcp]
  10. #执行多语句,空一行
  11. logpath = /var/log/secure
  12. maxretry = 3
  13. findtime = 300
  14. bantime = 10m

注意:mail-whois实为/etc/fail2ban/action.d/里面配置mail-whois.conf


启停Fail2ban

  1. #启动Fail2ban。
  2. systemctl start fail2ban
  3. #停止Fail2ban
  4. systemctl stop fail2ban
  5. #开机启动Fail2ban。
  6. systemctl enable fail2ban

重新加载

  1. fail2ban-client reload

查看

  1. fail2ban-client status
  2. fail2ban-client status ssh-iptables

解禁一个特定IP

  1. fail2ban-client set ssh-iptables unbanip 192.168.1.8

Fail2ban常用命令

  1. #启动Fail2ban。
  2. systemctl start fail2ban
  3. #停止Fail2ban
  4. systemctl stop fail2ban
  5. #开机启动Fail2ban。
  6. systemctl enable fail2ban
  7. #查看被ban IP,其中ssh-iptables为名称
  8. fail2ban-client status ssh-iptables
  9. #添加白名单。
  10. fail2ban-client set ssh-iptables addignoreip IP地址
  11. #删除白名单。
  12. fail2ban-client set ssh-iptables delignoreip IP地址
  13. #查看白名单
  14. fail2ban-client get ssh-iptables ignoreip
  15. #查看被禁止的IP地址。
  16. iptables -L -n
  17. #解禁一个特定IP
  18. fail2ban-client set ssh-iptables unbanip 192.168.1.8

目录

fail2ban的配置文件路径:/etc/fail2ban

fail2ban安装目录:/usr/share/fail2ban

日志文件:/var/log/fail2ban.log

(重要)达到阈值之后的执行的动作的配置文件: action.d/

包含所有的过滤规则:filter.d/


邮件告警【自定义动作】

安装mailx

  1. yum -y install mailx

备份配置

  1. cp /etc/mail.rc /etc/mail.rc.bak

修改配置

  1. vim /etc/mail.rc
  2. ========================================================
  3. #配置如下:
  4. ========================================================
  5. set ssl-verify=ignore
  6. set from=邮件地址@163.com
  7. set smtp=smtps://smtp.163.com:465
  8. set smtp-auth-user=邮件地址@163.com
  9. set smtp-auth-password=授权码
  10. set smtp-auth=login
  11. ========================================================
  12. 注意:smtp-auth-password不是填写邮箱密码,而是邮箱的授权码

测试

  1. #echo输入
  2. echo "正文"|mail -s "标题" -a 20220204-20220206_gbk.csv xx1@wxchina.com,xx2@wxchina.com
  3. #文件重定向输入
  4. mail -a 20220204-20220206_gbk.csv -s "消费" xx1@wxchina.com,xx2@wxchina.com<aa.log

创建action文件

  1. vim /etc/fail2ban/action.d/mail-whois.conf
  2. #内容:
  3. [Definition]
  4. actioncheck =
  5. actionban = printf "警告!!!\n
  6. 攻击者IP:<ip>\n
  7. 被攻击机器名:`uname -n` \n
  8. 被攻击机器IP:`/bin/curl ifconfig.co` \n
  9. 攻击服务:<name> \n
  10. 攻击次数:<failures> 次 \n
  11. 攻击方法:暴力破解,尝试弱口令\n
  12. 该IP:<ip>已经被Fail2Ban加入防火墙黑名单,屏蔽时间5分钟\n
  13. Fail2Ban邮件提醒"|/bin/mailx -s "服务器:<name>服务疑似遭到<ip>暴力攻击." <dest>
  14. actionunban =
  15. [Init]
  16. name = default
  17. dest = root

修改jail配置

  1. [ssh-iptables]
  2. enabled = true
  3. filter = sshd
  4. action = mail-whois[name=SSH, dest=wujiaqiang@wxchina.com, sender=cpaas@wxchina.com]
  5. iptables[name=SSH,port=2233, protocol=tcp]
  6. logpath = /var/log/secure
  7. maxretry = 3
  8. findtime = 300
  9. bantime = 100

注意:mail-whois要对应创建action文件名称。

例如:/etc/fail2ban/action.d/mail-whois.conf,则此处就为mail-whois


重启fail2ban

  1. systemctl restart fail2ban