文章目录
安装failban最新版本(自v1.0版本后对ipv6的支持)
#下载代码,并安装
yum install git -y
git clone https://github.com/fail2ban/fail2ban.git
cd fail2ban
python setup.py install
#这是yum安装,版本是 v0.9.7,你可以忽略
#yum install epel-release
#yum install fail2ban fail2ban-systemd
#设置centos的systemctl服务,以便自启动及后台运行(用最新的git安装方式才配置,yum安装的不需要配置)
#centos7,文件在(/fail2ban/build/)
cp /root/fail2ban/build/fail2ban.service /usr/lib/systemd/system/
chmod +x /usr/lib/systemd/system/fail2ban.service
#centos6,文件在(/fail2ban/files/)
cp /root/fail2ban/files/redhat-initd /etc/init.d/fail2ban
chkconfig fail2ban on
启动fail2ban,及简单封堵情况查询
#先启动防火墙
systemctl enable firewalld
systemctl start firewalld
#执行以下命令以在服务器上运行Fail2Ban保护软件。
systemctl enable fail2ban
systemctl start fail2ban
#检查Fail2Ban禁止的IP
iptables -L -n
#检查Fal2Ban状态,使用以下命令检查Fail2Ban监狱的状态:
fail2ban-client status
#查看某个防护模块,屏蔽的ip列表状态
fail2ban-client status ssh-iptables
[root@localhost jail.d]# fail2ban-client status ssh-iptables
Status for the jail: ssh-iptables
|- Filter
| |- Currently failed: 0
| |- Total failed: 3
| `- File list: /var/log/secure
`- Actions
|- Currently banned: 1
|- Total banned: 1
`- Banned IP list: 192.168.60.5
#如果配置出错,请用此条命令启动,可以看到错误日志
(如果重启 iptables ,必须重启 fail2ban)
/usr/bin/fail2ban-client -x start
#取消IP地址
为了从禁止列表中删除IP地址,将参数IPADDRESS设置为需要禁止的适当IP。名称“ sshd”是监狱的名称,在这种情况下,就是我们上面配置的“ sshd”监狱。以下命令完成该工作。
fail2ban-client set sshd unbanip IP地址
#添加白名单
fail2ban-client set ssh-iptables addignoreip IP地址
#删除白名单
fail2ban-client set ssh-iptables delignoreip IP地址
示例:使用fail2ban防御ssh暴力破解,及其fail2ban的配置文件讲解
第一部分:配置文件及其介绍
#fail2ban的日志所在目录/var/log/audit ,下面这个日志是记录ssh认证登录的相关信息
cat /var/log/audit/audit.log
#fail2ban的主配置文件
/etc/fail2ban fail2ban 服务配置目录
/etc/fail2ban/action.d iptables 、mail 等动作文件目录
/etc/fail2ban/fail2ban.conf fail2ban 配置文件,定义日志级别、日志、sock 文件位置等
/etc/fail2ban/filter.d 条件匹配文件目录,过滤日志关键内容
/etc/fail2ban/jail.conf fail2ban 防护配置文件,主要编辑这个文件,配置防护
#在配置时,我们应该避免修改由fail2ban安装创建的文件,我们应该去编写具有.local扩展名的新文件。在.local新文件里配置的内容会覆盖jail.conf内容里相同的值。
cat /etc/fail2ban/jail.conf #该文件可以作为我们的示例文件,建议不要修改不要删除。
第二部分:fail2ban 结合 iptables 实现防暴力破解,适用于centos6
#配置ssh防暴力破解的配置文件,这个可以是单独的文件,也可以是把多个模块功能写到一个文件中
vi /etc/fail2ban/jail.d/jail.local
[DEFAULT]
ignoreip = 127.0.0.1/8
bantime = 3600
findtime = 600
maxretry = 5
# ignoreip :用于指定哪些地址ip可以忽略 fail2ban 防御,以空格间隔。
# bantime :非法 IP 被屏蔽时间(秒),-1 代表永远封锁
# findtime :设置多长时间(秒)内超过 maxretry 限制次数即被封锁
# maxretry :最大尝试次数
# backend = auto ## 日志修改检测机制(gamin 、polling 、auto 三种)
[ssh-iptables]
enabled = true
filter = sshd
# port = 22
action = iptables[name=SSH, port=ssh, protocol=tcp]
logpath = /var/log/secure
maxretry = 3
# sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.com, sendername="Fail2Ban"] ## 邮件通知参数 ## 收件人地址 ## 发件人地址
# banaction = firewallcmd-ipset #这里banaction必须用firewallcmd-ipset,这是fiewalll支持的关键,如果是用Iptables请不要这样填写
# [ssh-iptables] :分类设置(基于 SSHD 服务的防护)
# enabled :是否开启防护,false 为关闭
# filter :过滤规则 filter 名称,对应 filter.d 目录下的 sshd.conf
# action :动作参数
# logpath :检测系统登陆日志文件
# banaction :屏蔽IP所使用的方法,上面使用firewalld屏蔽端口
#重载fail2ban,当配置文件发生变更时
fail2ban-client reload
第三部分:fail2ban 结合firewalld 实现防暴力破解,适用于centos7
fail2ban 防止CC & DDos
如果文章对你有帮助,欢迎点击上方按钮打赏作者
暂无评论