fail2ban防ssh爆破或攻击

目的:防止ssh爆破

fail2ban

> Fail2ban是一个根据系统日志动态调整防火墙规则的安全工具,它具有响应迅速、使用方便的特性。  

0x00 环境说明

centos7.9

0x01 安装

yum install -y epel
yum
install -y fail2ban-firewalld #centos7以上默认是firewall防火墙,如果系统是iptables防火墙 yum install -y fail2ban

0x02 结构

配置文件位置:/etc/fail2ban/fail2ban.conf

jail配置文件:jail.conf 一般拷贝一份.local

cp jail.conf jail.local #因为.local可覆盖.conf配置,因此无需修改.conf 直接更改.local即可

自定义过滤规则目录:/etc/fail2ban/filter.d

自定义动作配置目录:/etc/fail2ban/action.d

0x03 jail.local配置

vi /etc/fail2ban/jail.local #默认配置 [DEFAULT] ignoreip = 127.0.0.1/8 bantime = 86400 findtime = 600 maxretry = 5 #这里banaction必须用firewallcmd-ipset,这是fiewalll支持的关键,如果是用Iptables请不要这样填写 banaction = firewallcmd-ipset action = %(action_mwl)s
ignoreip:IP白名单,白名单中的IP不会屏蔽,可填写多个以(,)分隔

 bantime:屏蔽时间,单位为秒(s)

 findtime:时间范围

 maxretry:最大次数

 banaction:屏蔽IP所使用的方法,上面使用firewalld屏蔽端口

 

# 默认配置 [DEFAULT] # 被ban时间 bantime = 15m # 检索时间 findtime = 10m # 最大尝试数,即失败5次就被ban15分钟 maxretry = 5 maxmatches = %(maxretry)s # 后端服务依赖systemd backend = systemd # 日志警告信息可以使用DNS记录访问主机地址 usedns = warn # 日志编码使用utf-8 logencoding = utf-8 # 启用监管 enabled = ture # 默认为普通模式 mode = normal # 过滤模块 filter = %(__name__)s[mode=%(mode)s] # 防火墙调用firewallcmd,默认为iptables banaction = firewallcmd-ipset[actiontype=<multiport>] banaction_allports = firewallcmd-ipset[actiontype=<allports>] # 管理SSHD [sshd] enabled = true filter = sshd port = 22 logpath = /var/log/secure #%(sshd_log)s 也可以

另一种简单设置:

enabled = true port = ssh logpath = %(sshd_log)s backend = %(sshd_backend)s bantime = 180 findtime = 180 maxretry = 3
[sshd]:名称,可以随便填写

 filter:规则名称,必须填写位于filter.d目录里面的规则,sshd是fail2ban内置规则


 port:对应的端口


 action:采取的行动


 logpath:需要监视的日志路径

 

重启fail2ban

systemctl restart fail2ban.service

查看状态

fail2ban-client status sshd #此时的sshd是jail.loal中设置的规则项
#手动ban掉指定IP
fail2ban-client set 规则项 banip x.x.x.x
fail2ban-client set 规则项 unbanip x.x.x.x
fail2ban-client set 规则项 delignoreip x.x.x.x

利用ssh 故意输错密码

fail2ban防ssh爆破或攻击

 

 fail2ban防ssh爆破或攻击

 

 0x04 常用配置

防护Wordpress

# 在指定目录下建立wordpress配置文件,指定监控wordpress的访问地址 vi /etc/fail2ban/filter.d/wordpress.conf [Definition] failregex = ^ -.* /wp-login.php.* HTTP/1.." ignoreregex =

# 在jail.conf中加入如下配置,让fail2ban监视指定日志文件,根据日志记录进行拦截 [wordpress] enabled = true port = http,https filter = wordpress action = %(action_mwl)s maxretry = 20 findtime = 60 bantime = 3600 logpath = /usr/local/nginx/logs/access.log

防止半连接攻击

# 需要先新建一个nginx日志匹配规则 vi /etc/fail2ban/filter.d/nginx-cc.conf #填写如下内容 新建filter.d下的规则 [Definition] failregex = -.*- .*HTTP/1.* .* .*$ ignoreregex =

# 加入如下配置 [nginx-cc] enabled = true port = http,https filter = nginx-cc action = %(action_mwl)s maxretry = 20 findtime = 60 bantime = 3600 logpath = /usr/local/nginx/logs/access.log

参考文章:https://www.cnblogs.com/ipyanthony/p/9336142.html

原文链接:https://www.cnblogs.com/DevilCisco/p/16941847.html

原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/20951

(0)
上一篇 2024年4月19日 02:31
下一篇 2024年4月19日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

优速盾注册领取大礼包www.cdnb.net
/sitemap.xml