CentOS fail2ban 安全防护加固 —— 筑梦之路

CentOS 6 版本:

#安装
yum  install epel-release -y

#安装fail2ban 
yum install fail2ban -y

#开机启动
chkconfig  fail2ban on

#配置
vim /etc/fail2ban/jail.d/jail.local
[Default]

#针对各服务的检查配置,如设置bantime、findtime、maxretry和全局冲突,服务优先级大于全局设置
[ssh-iptables]

#忽略的IP列表,不受设置限制(白名单)
ignoreip = 127.0.0.1

#日志修改检测机制(gamin、polling和auto这三种)
backend=auto

#是否激活此项(true/false)
enabled=true

#过滤规则filter的名字,对应filter.d目录下的sshd.conf
filter= sshd

#动作的相关参数
action= iptables[name=SSH, port=ssh, protocol=tcp]

#触发报警的收件人
#sendmail-whois[name=SSH, dest=root, sender=邮箱地址, sendername="Fail2Ban"]

#检测的系统的登陆日志文件
logpath=/var/log/secure

#禁止10分钟
bantime=600

#这个时间段内超过规定次数会被ban掉
findtime=300

#最大尝试次数
maxretry=3

-----------
启动服务:

service fail2ban start

#查看状态
service fail2ban status

#查看封禁列表
fail2ban-client status ssh-iptables

-----------其他配置参考:
#defalut这里是设定全局设置,如果下面的监控没有设置就以全局设置的值设置。
[DEFAULT]
# 用于指定哪些地址ip可以忽略 fail2ban 防御,以空格间隔。
ignoreip = 127.0.0.1/8
# 客户端主机被禁止的时长(默认单位为秒)
bantime  = 86400
# 过滤的时长(秒)
findtime  = 600
# 匹配到的阈值(次数)
maxretry = 5
    
[ssh-iptables]
# 是否开启
enabled  = true
# 过滤规则
filter   = sshd
port    = 22
# 动作  发送邮件一定要加在action 后面,一定要注意位置
action   = hostsdeny
# 日志文件的路径
logpath  = /var/log/secure
# 匹配到的阈值(次数)
maxretry = 8


CentOS 7版本:

#安装
yum  install epel-release 

yum install fail2ban-all

#编辑文件
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
vim /etc/fail2ban/jail.local

----------------------配置文件示例
[DEFAULT]
#IP白名单
ignoreip    = 127.0.0.1/8 ::1 

#封禁时间
bantime     = 24h

#在这个时间段内,超过最大重试次数,就封禁该操作的IP
findtime    = 1h

#最大重试次数
maxretry    = 3

#使用的防火墙规则
#Firewall(firewallcmd-ipset);
#Iptables(iptables-multiport);
#对应/etc/fail2ban/action.d目录下的文件
banaction  = firewallcmd-ipset

#触发规则后的选择行为:
#只封禁IP:action_
#封禁IP+邮件通知:action_mw 
#封禁IP+邮件通知+报告相关日志:action_mwl 
action      = %(action_)s



[sshd]
#需要监听的项,对应/etc/fail2ban/filter.d目录下的文件
filter      = sshd
enabled     = true
port        = 22

#监听的日志路径:
#Debian系:/var/log/auth.log
#RedHat系:/var/log/secure
logpath     = /var/log/secure
-------------------------------------
阻止SSH暴力破解:

 [ssh-iptables]

 enabled  = true
 filter   = sshd
 action   = iptables[name=SSH, port=22, protocol=tcp]
 logpath  = /var/log/secure
 maxretry = 3
 findtime  = 300


阻止恶意扫描:

新增[nginx-dir-scan]模块,配置信息如下。此处,port和logpath应按照实际情况填写

 [nginx-dir-scan]

 enabled = true
 filter = nginx-dir-scan
 action   = iptables[name=nginx-dir-scan, port=443, protocol=tcp]
 logpath = /path/to/nginx/access.log
 maxretry = 1
 bantime = 172800
 findtime  = 300

然后在filter.d目录下新建nginx-dir-scan.conf
cp /etc/fail2ban/filter.d/nginx-http-auth.conf /etc/fail2ban/filter.d/nginx-dir-scan.conf
vim /etc/fail2ban/filter.d/nginx-dir-scan.conf

对nginx-dir-scan.conf进行修改,具体配置信息如下

 [Definition]

 failregex = <HOST> -.*- .*Mozilla/4.0* .* .*$
 ignoreregex =

此处的正则匹配规则是根据nginx的访问日志进行撰写,不同的恶意扫描有不同的日志特征。

本文采用此规则是因为在特殊的应用场景下有绝大的把握可以肯定Mozilla/4.0是一些老旧的数据采集软件使用的UA,所以就针对其做了屏蔽。不可否认Mozilla/4.0 这样的客户端虽然是少数,但仍旧存在。因此,此规则并不适用于任何情况。

使用如下命令,可以测试正则规则的有效性。

fail2ban-regex /path/to/nginx/access.log /etc/fail2ban/filter.d/nginx-dir-scan.conf


Fail2ban已经内置很多匹配规则,位于filter.d目录下,包含了常见的SSH/FTP/Nginx/Apache等日志匹配,如果都还无法满足需求,也可以自行新建规则来匹配异常IP。总之,使用Fail2ban+iptables来阻止恶意IP是行之有效的办法,可极大提高服务器安全

变更iptables封禁策略

Fail2ban的默认iptables封禁策略为 REJECT --reject-with icmp-port-unreachable,需要变更iptables封禁策略为DROP。

在/etc/fail2ban/action.d/目录下新建文件iptables-blocktype.local

cd /etc/fail2ban/action.d/
cp iptables-blocktype.conf iptables-blocktype.local
vim iptables-blocktype.local


修改内容如下:

[INCLUDES]

after = iptables-blocktype.local

[Init]

blocktype = DROP

最后,别忘记重启fail2ban使其生效

systemctl restart fail2ban


常用命令:

systemctl start fail2ban

systemctl stop fail2ban

systemctl enable fail2ban

查看被ban IP,其中ssh-iptables为名称,比如上面的[ssh-iptables]和[nginx-dir-scan]。

fail2ban-client status ssh-iptables


添加白名单:

fail2ban-client set ssh-iptables addignoreip IP地址 


删除白名单:

fail2ban-client set ssh-iptables delignoreip IP地址


查看被禁的ip:

iptables -L -n




封禁IP+发送邮箱

yum install -y mailx


配置Mailx

#编辑 (Debian系:/etc/s-nail.rc;RedHat系:/etc/mail.rc)

vim /etc/mail.rc

在文件末尾增加配置

#发件人邮箱
set [email protected]
#发件人邮箱的SMTP地址
set smtp=smtps://smtp.qq.com:465
#发件人邮箱登陆账号
set [email protected]
#发件人邮箱的授权码
set smtp-auth-password=e8765ds78c23
#认证方式
set smtp-auth=login   
#忽略证书警告
set ssl-verify=ignore
#证书所在目录
set nss-config-dir=/etc/pki/nssdb

#测试

echo '邮件内容' | mail -v -s '邮件标题' 收件人邮箱@qq.com

多个收件人用,分隔,刚刚发送的邮件在收件箱或垃圾箱中。

配置Fail2ban

vi /etc/fail2ban/jail.local

[DEFAULT]
ignoreip    = 127.0.0.1/8 ::1 
bantime     = 24h
findtime    = 1h
maxretry    = 3
banaction = firewallcmd-ipset

#邮件发送软件
mta         = mail
#发件人地址
sender      = [email protected]
#收件人地址
destemail   = [email protected]
#封禁IP+邮件通知:action_mw 
action      = %(action_mw)s


[sshd]
filter      = sshd
enabled     = true
port        = 22
logpath     = /var/log/secure


配置警告内容

#配置
vim /etc/fail2ban/action.d/mail-whois.conf

[INCLUDES]
before = mail-whois-common.conf

[Definition]
actionban = printf "警告!!!\n
            被攻击机器名:`uname -n` \n
            被攻击机器IP:`/bin/curl ifconfig.co` \n
            攻击服务:<name> \n
            时间范围:<findtime> 内 \n
            攻击次数:<failures> 次 \n
            攻击者IP:<ip> \n
            攻击方式:暴力破解,尝试弱口令。\n
            处理方式:已将<ip>加入防火墙黑名单,封禁时间为 <bantime> 秒。\n\n
            \t                                          ——来自fail2ban邮箱
            " | /bin/mailx -s "[<name>]服务器遭到<ip>暴力攻击" <dest>

[Init]
name = default
dest = root

# 重载配置

fail2ban-client reload

自定义拦截规则

防止CC攻击

以Nginx为例,使用fail2ban来监视nginx日志,匹配短时间内频繁请求的IP,并使用firewalld将其IP屏蔽,达到CC防护的作用。

增加规则

#配置
vi /etc/fail2ban/filter.d/nginx-cc.conf

[Definition]
failregex = <HOST> -.*- .*HTTP/1.* .* .*$
ignoreregex =

追加配置

vi /etc/fail2ban/jail.local

[nginx-cc]
filter      = nginx-cc
enabled     = true
port        = http,https
bantime     = 2h
findtime    = 60s
maxretry    = 120
action      = %(action_)s
logpath     = /usr/local/nginx/logs/access.log

意思是如果在60秒内,同一IP达到120次请求,则将其封禁2小时,具体根据实际情况修改。

重载配置:fail2ban-client reload

查看状态:fail2ban-client status nginx-cc

检测规则:

fail2ban-regex /usr/local/nginx/logs/access.log "<HOST> -.*- .*HTTP/1.* .* .*$"

fail2ban-regex /usr/local/nginx/logs/access.log /etc/fail2ban/filter.d/nginx-cc.conf 


参考资料:

https://www.jianshu.com/p/1a915cd95204

https://www.jianshu.com/p/3f9d1571a2ef

猜你喜欢

转载自blog.csdn.net/qq_34777982/article/details/125806113
今日推荐